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
[WIP] More work towards converting topography into a proper dataclass
  • Loading branch information
Leguark committed May 25, 2025
commit 57b9984f728c6d0008e698c19945b1e03470b4d5
5 changes: 5 additions & 0 deletions gempy/core/data/encoders/converters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Annotated

from contextlib import contextmanager

from contextvars import ContextVar
Expand All @@ -17,6 +19,9 @@ def validate_numpy_array(v):
return np.array(v) if v is not None else None


short_array_type = Annotated[np.ndarray, (BeforeValidator(lambda v: np.array(v) if v is not None else None))]


def instantiate_if_necessary(data: dict, key: str, type: type) -> None:
"""
Creates instances of the specified type for a dictionary key if the key exists and its
Expand Down
3 changes: 2 additions & 1 deletion gempy/core/data/grid_modules/topography.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ....optional_dependencies import require_skimage
from dataclasses import field, dataclass
from ..encoders.converters import short_array_type


@dataclass
Expand All @@ -27,7 +28,7 @@ class Topography:
source: Optional[str] = None

# Fields managed internally
values: np.ndarray = field(init=False, default_factory=lambda: np.zeros((0, 3)))
values: short_array_type = field(init=False, default=np.zeros((0, 3)))
resolution: Tuple[int, int] = field(init=False, default=(0, 0))
raster_shape: Tuple[int, ...] = field(init=False, default=())
_mask_topo: Optional[np.ndarray] = field(init=False, default=None, repr=False)
Expand Down
5 changes: 3 additions & 2 deletions gempy/modules/serialization/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,19 @@ def verify_model_serialization(model: GeoModel, verify_moment: Literal["before",

binary_file = _to_binary(model_json, compressed_binary)

model_deserialized = _deserialize_binary_file(binary_file)

original_model = model
original_model.meta.creation_date = "<DATE_IGNORED>"
model_deserialized.meta.creation_date = "<DATE_IGNORED>"

from verify_helper import verify_json
if verify_moment == "before":
verify_json(
item=original_model.model_dump_json(by_alias=True, indent=4),
name=file_name
)
elif verify_moment == "after":
model_deserialized = _deserialize_binary_file(binary_file)
model_deserialized.meta.creation_date = "<DATE_IGNORED>"
verify_json(
item=model_deserialized.model_dump_json(by_alias=True, indent=4),
name=file_name
Expand Down
11 changes: 7 additions & 4 deletions test/test_modules/test_grids/test_grids_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gempy as gp
import gempy_viewer as gpv
from gempy.core.data.enumerators import ExampleModel
from gempy.modules.serialization.save_load import verify_model_serialization

from test.conftest import TEST_SPEED, TestSpeed

Expand All @@ -20,8 +21,6 @@ def test_section_grids():
evaluation_options=geo_model.interpolation_options.evaluation_options
)

model = geo_model
model_json = model.model_dump_json(by_alias=True, indent=4)
gp.set_section_grid(
grid=geo_model.grid,
section_dict={
Expand All @@ -30,15 +29,19 @@ def test_section_grids():
}
)

model_json = model.model_dump_json(by_alias=True, indent=4)
gp.set_topography_from_random(
grid=geo_model.grid,
fractal_dimension=1.2,
d_z=np.array([200, 1000]),
topography_resolution=np.array([60, 60])
)

model_json = model.model_dump_json(by_alias=True, indent=4)
verify_model_serialization(
model=geo_model,
verify_moment="after",
file_name=f"verify/{geo_model.meta.name}"
)
return
gp.compute_model(geo_model)
gpv.plot_2d(
model=geo_model,
Expand Down