Skip to content

Commit f1745c1

Browse files
committed
[CLN] Simplify binary deserialization
1 parent 03a87f9 commit f1745c1

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
3+
from .. import SurfacePointsTable, OrientationsTable
4+
5+
6+
def deserialize_input_data_tables(binary_array: bytes, name_id_map: dict, sp_binary_length_: int) -> tuple[OrientationsTable, SurfacePointsTable]:
7+
sp_binary = binary_array[:sp_binary_length_]
8+
ori_binary = binary_array[sp_binary_length_:]
9+
# Reconstruct arrays
10+
sp_data: np.ndarray = np.frombuffer(sp_binary, dtype=SurfacePointsTable.dt)
11+
ori_data: np.ndarray = np.frombuffer(ori_binary, dtype=OrientationsTable.dt)
12+
surface_points_table = SurfacePointsTable(data=sp_data, name_id_map=name_id_map)
13+
orientations_table = OrientationsTable(data=ori_data, name_id_map=name_id_map)
14+
return orientations_table, surface_points_table

gempy/core/data/structural_frame.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from gempy_engine.core.data.input_data_descriptor import InputDataDescriptor
1111
from gempy_engine.core.data.kernel_classes.faults import FaultsData
1212
from gempy_engine.core.data.stack_relation_type import StackRelationType
13+
14+
from .encoders.binary_encoder import deserialize_input_data_tables
1315
from .encoders.converters import loading_model_context
1416
from .orientations import OrientationsTable
1517
from .structural_element import StructuralElement
@@ -469,29 +471,15 @@ def deserialize_binary(cls, data: Union["StructuralFrame", dict], constructor: M
469471
case dict():
470472
instance: StructuralFrame = constructor(data)
471473
metadata = data.get('binary_meta_data', {})
472-
473474
context = loading_model_context.get()
474475

475476
if 'binary_body' not in context:
476477
return instance
477478

478-
binary_array = context['binary_body']
479-
480-
sp_binary = binary_array[:metadata["sp_binary_length"]]
481-
ori_binary = binary_array[metadata["sp_binary_length"]:]
482-
483-
# Reconstruct arrays
484-
sp_data: np.ndarray = np.frombuffer(sp_binary, dtype=SurfacePointsTable.dt)
485-
ori_data: np.ndarray = np.frombuffer(ori_binary, dtype=OrientationsTable.dt)
486-
487-
instance.surface_points = SurfacePointsTable(
488-
data=sp_data,
489-
name_id_map=instance.surface_points_copy.name_id_map
490-
)
491-
492-
instance.orientations = OrientationsTable(
493-
data=ori_data,
494-
name_id_map=instance.orientations_copy.name_id_map
479+
instance.orientations, instance.surface_points = deserialize_input_data_tables(
480+
binary_array=context['binary_body'],
481+
name_id_map=instance.surface_points_copy.name_id_map,
482+
sp_binary_length_=metadata["sp_binary_length"]
495483
)
496484

497485
return instance
@@ -500,6 +488,7 @@ def deserialize_binary(cls, data: Union["StructuralFrame", dict], constructor: M
500488

501489
# Access the context variable to get injected data
502490

491+
503492
@model_validator(mode="after")
504493
def deserialize_surface_points(self: "StructuralFrame"):
505494
# Access the context variable to get injected data
@@ -545,27 +534,11 @@ def deserialize_orientations(self: "StructuralFrame"):
545534

546535
@computed_field
547536
def binary_meta_data(self) -> dict:
548-
sp_data = self.surface_points_copy.data
549-
ori_data = self.orientations_copy.data
550537
return {
551-
'sp_shape' : sp_data.shape,
552-
'sp_dtype' : str(sp_data.dtype),
553-
'sp_binary_length' : len(sp_data.tobytes()),
554-
'ori_shape' : ori_data.shape,
555-
'ori_dtype' : str(ori_data.dtype),
556-
'ori_binary_length': len(ori_data.tobytes())
538+
'sp_binary_length': len(self.surface_points_copy.data.tobytes()),
539+
# 'ori_binary_length': len(self.orientations_copy.data.tobytes()) * (miguel May 2025) This is not necessary at the moment
557540
}
558541

559-
@computed_field
560-
@property
561-
def serialize_sp(self) -> int:
562-
return int(hashlib.md5(self.surface_points_copy.data.tobytes()).hexdigest()[:8], 16)
563-
564-
@computed_field
565-
@property
566-
def serialize_orientations(self) -> int:
567-
return int(hashlib.md5(self.orientations_copy.data.tobytes()).hexdigest()[:8], 16)
568-
569542
# endregion
570543

571544
def _validate_faults_relations(self):

test/test_modules/test_serialize_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_generate_horizontal_stratigraphic_model():
2020
with open(file_path, "w") as f:
2121
f.write(model_json)
2222

23+
# TODO: modify this for the binary
2324
with loading_model_injection(
2425
surface_points_binary=model.structural_frame.surface_points_copy.data, # TODO: Here we need to pass the binary array
2526
orientations_binary=model.structural_frame.orientations_copy.data

0 commit comments

Comments
 (0)