Skip to content

Commit 59edc16

Browse files
committed
[ENH] Added function to convert the model to binary
1 parent 16bd67f commit 59edc16

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

gempy/modules/serialization/save_load.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,7 @@ def save_model(model: GeoModel, path: str | None = None, validate_serialization:
4444
# If no extension, add the valid extension
4545
path = str(path_obj) + VALID_EXTENSION
4646

47-
model_json = model.model_dump_json(by_alias=True, indent=4)
48-
49-
# Compress the binary data
50-
zlib = require_zlib()
51-
compressed_binary_input = zlib.compress(model.structural_frame.input_tables_binary)
52-
compressed_binary_grid = zlib.compress(model.grid.grid_binary)
53-
54-
binary_file = _to_binary(
55-
header_json=model_json,
56-
body_input=compressed_binary_input,
57-
body_grid=compressed_binary_grid
58-
)
47+
binary_file = model_to_binary(model)
5948

6049
if validate_serialization:
6150
model_deserialized = _deserialize_binary_file(binary_file)
@@ -72,6 +61,20 @@ def save_model(model: GeoModel, path: str | None = None, validate_serialization:
7261
return path # Return the actual path used (helpful if extension was added)
7362

7463

64+
def model_to_binary(model: GeoModel) -> bytes:
65+
model_json = model.model_dump_json(by_alias=True, indent=4)
66+
# Compress the binary data
67+
zlib = require_zlib()
68+
compressed_binary_input = zlib.compress(model.structural_frame.input_tables_binary)
69+
compressed_binary_grid = zlib.compress(model.grid.grid_binary)
70+
binary_file = _to_binary(
71+
header_json=model_json,
72+
body_input=compressed_binary_input,
73+
body_grid=compressed_binary_grid
74+
)
75+
return binary_file
76+
77+
7578
def load_model(path: str) -> GeoModel:
7679
"""
7780
Load a GeoModel from a file with extension validation.

test/verify_helper.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from approvaltests.reporters import GenericDiffReporter, GenericDiffReporterConfig
1212

1313
from gempy.core.data import GeoModel
14-
from gempy.modules.serialization.save_load import _to_binary, _deserialize_binary_file
14+
from gempy.modules.serialization.save_load import _to_binary, _deserialize_binary_file, model_to_binary
1515
from gempy.optional_dependencies import require_zlib
1616

1717

@@ -120,14 +120,7 @@ def verify_model_serialization(model: GeoModel, verify_moment: Literal["before",
120120
Raises:
121121
ValueError: If `verify_moment` is not set to "before" or "after".
122122
"""
123-
model_json = model.model_dump_json(by_alias=True, indent=4)
124-
125-
# Compress the binary data
126-
zlib = require_zlib()
127-
compressed_binary = zlib.compress(model.structural_frame.input_tables_binary)
128-
129-
binary_file = _to_binary(model_json, compressed_binary)
130-
123+
binary_file = model_to_binary(model)
131124

132125
original_model = model
133126
original_model.meta.creation_date = "<DATE_IGNORED>"

0 commit comments

Comments
 (0)