Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[CLN] Simplify binary deserialization
  • Loading branch information
Leguark committed May 23, 2025
commit f1745c199b9c4b1d1de050ffa25c76f291e067c0
14 changes: 14 additions & 0 deletions gempy/core/data/encoders/binary_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np

from .. import SurfacePointsTable, OrientationsTable


def deserialize_input_data_tables(binary_array: bytes, name_id_map: dict, sp_binary_length_: int) -> tuple[OrientationsTable, SurfacePointsTable]:
sp_binary = binary_array[:sp_binary_length_]
ori_binary = binary_array[sp_binary_length_:]
# Reconstruct arrays
sp_data: np.ndarray = np.frombuffer(sp_binary, dtype=SurfacePointsTable.dt)
ori_data: np.ndarray = np.frombuffer(ori_binary, dtype=OrientationsTable.dt)
surface_points_table = SurfacePointsTable(data=sp_data, name_id_map=name_id_map)
orientations_table = OrientationsTable(data=ori_data, name_id_map=name_id_map)
return orientations_table, surface_points_table
45 changes: 9 additions & 36 deletions gempy/core/data/structural_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from gempy_engine.core.data.input_data_descriptor import InputDataDescriptor
from gempy_engine.core.data.kernel_classes.faults import FaultsData
from gempy_engine.core.data.stack_relation_type import StackRelationType

from .encoders.binary_encoder import deserialize_input_data_tables
from .encoders.converters import loading_model_context
from .orientations import OrientationsTable
from .structural_element import StructuralElement
Expand Down Expand Up @@ -469,29 +471,15 @@ def deserialize_binary(cls, data: Union["StructuralFrame", dict], constructor: M
case dict():
instance: StructuralFrame = constructor(data)
metadata = data.get('binary_meta_data', {})

context = loading_model_context.get()

if 'binary_body' not in context:
return instance

binary_array = context['binary_body']

sp_binary = binary_array[:metadata["sp_binary_length"]]
ori_binary = binary_array[metadata["sp_binary_length"]:]

# Reconstruct arrays
sp_data: np.ndarray = np.frombuffer(sp_binary, dtype=SurfacePointsTable.dt)
ori_data: np.ndarray = np.frombuffer(ori_binary, dtype=OrientationsTable.dt)

instance.surface_points = SurfacePointsTable(
data=sp_data,
name_id_map=instance.surface_points_copy.name_id_map
)

instance.orientations = OrientationsTable(
data=ori_data,
name_id_map=instance.orientations_copy.name_id_map
instance.orientations, instance.surface_points = deserialize_input_data_tables(
binary_array=context['binary_body'],
name_id_map=instance.surface_points_copy.name_id_map,
sp_binary_length_=metadata["sp_binary_length"]
)

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

# Access the context variable to get injected data


@model_validator(mode="after")
def deserialize_surface_points(self: "StructuralFrame"):
# Access the context variable to get injected data
Expand Down Expand Up @@ -545,27 +534,11 @@ def deserialize_orientations(self: "StructuralFrame"):

@computed_field
def binary_meta_data(self) -> dict:
sp_data = self.surface_points_copy.data
ori_data = self.orientations_copy.data
return {
'sp_shape' : sp_data.shape,
'sp_dtype' : str(sp_data.dtype),
'sp_binary_length' : len(sp_data.tobytes()),
'ori_shape' : ori_data.shape,
'ori_dtype' : str(ori_data.dtype),
'ori_binary_length': len(ori_data.tobytes())
'sp_binary_length': len(self.surface_points_copy.data.tobytes()),
# 'ori_binary_length': len(self.orientations_copy.data.tobytes()) * (miguel May 2025) This is not necessary at the moment
}

@computed_field
@property
def serialize_sp(self) -> int:
return int(hashlib.md5(self.surface_points_copy.data.tobytes()).hexdigest()[:8], 16)

@computed_field
@property
def serialize_orientations(self) -> int:
return int(hashlib.md5(self.orientations_copy.data.tobytes()).hexdigest()[:8], 16)

# endregion

def _validate_faults_relations(self):
Expand Down
1 change: 1 addition & 0 deletions test/test_modules/test_serialize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_generate_horizontal_stratigraphic_model():
with open(file_path, "w") as f:
f.write(model_json)

# TODO: modify this for the binary
with loading_model_injection(
surface_points_binary=model.structural_frame.surface_points_copy.data, # TODO: Here we need to pass the binary array
orientations_binary=model.structural_frame.orientations_copy.data
Expand Down