Skip to content

Commit 64b4746

Browse files
committed
[WIP] First implementation
1 parent 13894bd commit 64b4746

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

gempy/modules/serialization/__init__.py

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from ...core.data import GeoModel
2+
from ...core.data.encoders.converters import loading_model_injection
3+
4+
5+
def save_model(model: GeoModel, path: str):
6+
7+
# TODO: Serialize to json
8+
model_json = model.model_dump_json(by_alias=True, indent=4)
9+
10+
# TODO: Serialize to binary
11+
sp_binary = model.structural_frame.surface_points_copy.data.tobytes()
12+
ori_binary = model.structural_frame.orientations_copy.data.tobytes()
13+
14+
# TODO: Putting both together
15+
binary_file = _to_binary(model_json, sp_binary + ori_binary)
16+
with open(path, 'wb') as f:
17+
f.write(binary_file)
18+
19+
def load_model(path: str) -> GeoModel:
20+
with open(path, 'rb') as f:
21+
binary_file = f.read()
22+
23+
# Get header length from first 4 bytes
24+
header_length = int.from_bytes(binary_file[:4], byteorder='little')
25+
26+
# Split header and body
27+
header_json = binary_file[4:4 + header_length].decode('utf-8')
28+
body = binary_file[4 + header_length:]
29+
30+
# Split body into surface points and orientations
31+
# They are equal size so we can split in half
32+
sp_binary = body[:len(body) // 2]
33+
ori_binary = body[len(body) // 2:]
34+
35+
with loading_model_injection(
36+
surface_points_binary=sp_binary,
37+
orientations_binary=ori_binary
38+
):
39+
model = GeoModel.model_validate_json(header_json)
40+
41+
return model
42+
43+
44+
def _to_binary(header_json, body_) -> bytes:
45+
header_json_bytes = header_json.encode('utf-8')
46+
header_json_length = len(header_json_bytes)
47+
header_json_length_bytes = header_json_length.to_bytes(4, byteorder='little')
48+
file = header_json_length_bytes + header_json_bytes + body_
49+
return file
50+
51+

test/test_modules/test_serialize_model.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
import gempy as gp
66
from gempy.core.data.encoders.converters import loading_model_injection
77
from gempy.core.data.enumerators import ExampleModel
8+
from gempy.modules.serialization.save_load import save_model, load_model
89
from gempy_engine.core.data import InterpolationOptions
910
from test.verify_helper import verify_json
1011

1112

1213
def test_generate_horizontal_stratigraphic_model():
1314
model: gp.data.GeoModel = gp.generate_example_model(ExampleModel.HORIZONTAL_STRAT, compute_model=False)
14-
model_json = model.model_dump_json(by_alias=True, indent=4, exclude={"*data"})
15+
model_json = model.model_dump_json(by_alias=True, indent=4)
1516

1617
# Write the JSON to disk
17-
if False:
18+
if True:
1819
file_path = os.path.join("temp", "horizontal_stratigraphic_model.json")
1920
with open(file_path, "w") as f:
2021
f.write(model_json)
@@ -44,6 +45,16 @@ def test_generate_horizontal_stratigraphic_model():
4445
verify_json(json.dumps(verify_model, indent=4), name="verify/Horizontal Stratigraphic Model serialization")
4546

4647

48+
def test_save_model_to_disk():
49+
model = gp.generate_example_model(ExampleModel.COMBINATION, compute_model=False)
50+
save_model(model, "temp/test_save_model_to_disk.json")
51+
52+
# Load the model from disk
53+
loaded_model = load_model("temp/test_save_model_to_disk.json")
54+
assert loaded_model.__str__() == model.__str__()
55+
56+
57+
4758

4859
def test_interpolation_options():
4960
options = InterpolationOptions.from_args(

0 commit comments

Comments
 (0)