Skip to content

Commit 69c5b58

Browse files
committed
[ENH] Update data validation in structural elements & tests
Refactored `StructuralElement` attributes to use Pydantic's `Field` for improved data validation and added `exclude=True` where necessary. Enhanced serialization test to include model save after computation with validation.
1 parent b6598ff commit 69c5b58

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

gempy/core/data/structural_element.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
from dataclasses import dataclass, field
3+
from pydantic import Field
34
from typing import Optional
45

56
import numpy as np
@@ -29,9 +30,9 @@ class StructuralElement:
2930

3031
# Output
3132
# ? Should we extract this to a separate class?
32-
vertices: Optional[np.ndarray] = None #: The vertices of the element in 3D space.
33-
edges: Optional[np.ndarray] = None #: The edges of the element in 3D space.
34-
scalar_field_at_interface: Optional[float] = None #: The scalar field value for the element.
33+
vertices: np.ndarray | None = Field(default=None, exclude=True) #: The vertices of the element in 3D space.
34+
edges: np.ndarray | None = Field(default=None, exclude=True) #: The edges of the element in 3D space.
35+
scalar_field_at_interface: float | None = None #: The scalar field value for the element.
3536

3637
_id: int = -1
3738

test/test_modules/test_serialize_model.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_generate_horizontal_stratigraphic_model():
2222
f.write(model_json)
2323

2424
with loading_model_from_binary(
25-
binary_body=model.structural_frame.input_tables_binary
25+
binary_body=model.structural_frame.input_tables_binary
2626
):
2727
model_deserialized = gp.data.GeoModel.model_validate_json(model_json)
2828

@@ -50,17 +50,22 @@ def _validate_serialization(original_model, model_deserialized):
5050
def test_save_model_to_disk():
5151
model = gp.generate_example_model(ExampleModel.COMBINATION, compute_model=False)
5252
save_model(model, "temp/test_save_model_to_disk.gempy")
53-
53+
5454
# Load the model from disk
5555
loaded_model = load_model("temp/test_save_model_to_disk.gempy")
5656
_validate_serialization(model, loaded_model)
57-
57+
5858
gp.compute_model(loaded_model)
5959
if True:
6060
import gempy_viewer as gpv
6161
gpv.plot_3d(loaded_model, image=True)
62-
6362

63+
# Test save after compute
64+
save_model(
65+
model=model,
66+
path="temp/test_save_model_to_disk.gempy",
67+
validate_serialization=True
68+
)
6469

6570

6671
def test_interpolation_options():

0 commit comments

Comments
 (0)