Skip to content

Commit 32f95d3

Browse files
committed
[ENH] Refactor data handling in orientations and surface points
Replaced `SkipValidation` with `Field` for better data validation and structure. Simplified serialization by excluding large data arrays and removed redundant field validators. Updated test references accordingly.
1 parent 36c6091 commit 32f95d3

File tree

3 files changed

+22
-137
lines changed

3 files changed

+22
-137
lines changed

gempy/core/data/orientations.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from dataclasses import dataclass
2-
from pydantic import field_validator, SkipValidation
3-
from typing import Optional, Sequence, Union, Annotated
2+
from typing import Optional, Sequence, Union
43

54
import numpy as np
5+
from gempy_engine.core.data.transforms import Transform
6+
from pydantic import Field
67

8+
from ...optional_dependencies import require_pandas
79
from ._data_points_helpers import generate_ids_from_names
8-
from .encoders.converters import numpy_array_short_validator
9-
from gempy_engine.core.data.transforms import Transform
10-
from gempy.optional_dependencies import require_pandas
1110

1211
DEFAULT_ORI_NUGGET = 0.01
1312

@@ -21,10 +20,15 @@ class OrientationsTable:
2120
A dataclass to represent a table of orientations in a geological model.
2221
2322
"""
24-
data: SkipValidation[np.ndarray] #: A structured NumPy array holding the X, Y, Z coordinates, gradients G_x, G_y, G_z, id, and nugget of each orientation.
25-
name_id_map: Optional[dict[str, int]] = None #: A mapping between orientation names and ids.
2623

2724
dt = np.dtype([('X', 'f8'), ('Y', 'f8'), ('Z', 'f8'), ('G_x', 'f8'), ('G_y', 'f8'), ('G_z', 'f8'), ('id', 'i4'), ('nugget', 'f8')]) #: The custom data type for the data array.
25+
data: np.ndarray = Field(
26+
default=np.zeros(0, dtype=dt),
27+
exclude=True,
28+
description="A structured NumPy array holding the X, Y, Z coordinates, gradients G_x, G_y, G_z, id, and nugget of each orientation.",
29+
) #: A structured NumPy array holding the X, Y, Z coordinates, id, and nugget of each surface point.
30+
name_id_map: Optional[dict[str, int]] = None #: A mapping between orientation names and ids.
31+
2832

2933
_model_transform: Optional[Transform] = None
3034

@@ -58,11 +62,6 @@ def from_arrays(cls, x: np.ndarray, y: np.ndarray, z: np.ndarray,
5862
data, name_id_map = cls._data_from_arrays(x, y, z, G_x, G_y, G_z, names, nugget, name_id_map)
5963
return cls(data, name_id_map)
6064

61-
@field_validator('data', mode='after')
62-
@classmethod
63-
def parse_short_array(cls, _: list[list]) -> str:
64-
# Now just build a structured array
65-
return np.zeros(0, dtype=OrientationsTable.dt)
6665

6766
@classmethod
6867
def _data_from_arrays(cls, x, y, z, G_x, G_y, G_z, names, nugget, name_id_map=None) -> tuple[np.ndarray, dict[str, int]]:

gempy/core/data/surface_points.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import numpy as np
2-
from dataclasses import dataclass
3-
from pydantic import field_validator, SkipValidation
1+
from dataclasses import dataclass
42
from typing import Optional, Union, Sequence
53

4+
import numpy as np
65
from gempy_engine.core.data.transforms import Transform
6+
from pydantic import Field
77

8-
from ...optional_dependencies import require_pandas
98
from ._data_points_helpers import generate_ids_from_names
9+
from ...optional_dependencies import require_pandas
1010

1111
DEFAULT_SP_NUGGET = 0.00002
1212

@@ -22,10 +22,14 @@ class SurfacePointsTable:
2222
A dataclass to represent a table of surface points in a geological model.
2323
2424
"""
25-
data: SkipValidation[np.ndarray] #: A structured NumPy array holding the X, Y, Z coordinates, id, and nugget of each surface point.
26-
name_id_map: Optional[dict[str, int]] = None #: A mapping between surface point names and ids.
27-
2825
dt = np.dtype([('X', 'f8'), ('Y', 'f8'), ('Z', 'f8'), ('id', 'i4'), ('nugget', 'f8')]) #: The custom data type for the data array.
26+
27+
data: np.ndarray = Field(
28+
default=np.zeros(0, dtype=dt),
29+
exclude=True,
30+
description="A structured NumPy array holding the X, Y, Z coordinates, id, and nugget of each surface point."
31+
) #: A structured NumPy array holding the X, Y, Z coordinates, id, and nugget of each surface point.
32+
name_id_map: Optional[dict[str, int]] = None #: A mapping between surface point names and ids.
2933
_model_transform: Optional[Transform] = None
3034

3135
def __post_init__(self):
@@ -77,12 +81,6 @@ def from_arrays(cls, x: np.ndarray, y: np.ndarray, z: np.ndarray,
7781
return cls(data, name_id_map)
7882

7983

80-
@field_validator('data', mode='after')
81-
@classmethod
82-
def parse_short_array(cls, _: list[list]) -> str:
83-
# Now just build a structured array
84-
return np.zeros(0, dtype=SurfacePointsTable.dt)
85-
8684
@classmethod
8785
def _data_from_arrays(cls, x: np.ndarray, y: np.ndarray, z: np.ndarray,
8886
names: Union[Sequence | str], nugget: Optional[np.ndarray] = None,

test/test_modules/test_serialize_model.test_generate_horizontal_stratigraphic_model.verify/Horizontal Stratigraphic Model serialization.approved.txt

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,13 @@
1515
"is_active": true,
1616
"_color": "#9f0052",
1717
"surface_points": {
18-
"data": [
19-
[
20-
100.0,
21-
200.0,
22-
600.0,
23-
117776925,
24-
2e-05
25-
],
26-
[
27-
500.0,
28-
200.0,
29-
600.0,
30-
117776925,
31-
2e-05
32-
],
33-
[
34-
900.0,
35-
200.0,
36-
600.0,
37-
117776925,
38-
2e-05
39-
],
40-
[
41-
100.0,
42-
800.0,
43-
600.0,
44-
117776925,
45-
2e-05
46-
],
47-
[
48-
500.0,
49-
800.0,
50-
600.0,
51-
117776925,
52-
2e-05
53-
],
54-
[
55-
900.0,
56-
800.0,
57-
600.0,
58-
117776925,
59-
2e-05
60-
]
61-
],
6218
"name_id_map": {
6319
"rock1": 67239155,
6420
"rock2": 117776925
6521
},
6622
"_model_transform": null
6723
},
6824
"orientations": {
69-
"data": [
70-
[
71-
500.0,
72-
500.0,
73-
600.0,
74-
0.0,
75-
0.0,
76-
1.0,
77-
117776925,
78-
0.01
79-
]
80-
],
8125
"name_id_map": {
8226
"rock1": 67239155,
8327
"rock2": 117776925
@@ -94,69 +38,13 @@
9438
"is_active": true,
9539
"_color": "#015482",
9640
"surface_points": {
97-
"data": [
98-
[
99-
100.0,
100-
200.0,
101-
400.0,
102-
67239155,
103-
2e-05
104-
],
105-
[
106-
500.0,
107-
200.0,
108-
400.0,
109-
67239155,
110-
2e-05
111-
],
112-
[
113-
900.0,
114-
200.0,
115-
400.0,
116-
67239155,
117-
2e-05
118-
],
119-
[
120-
100.0,
121-
800.0,
122-
400.0,
123-
67239155,
124-
2e-05
125-
],
126-
[
127-
500.0,
128-
800.0,
129-
400.0,
130-
67239155,
131-
2e-05
132-
],
133-
[
134-
900.0,
135-
800.0,
136-
400.0,
137-
67239155,
138-
2e-05
139-
]
140-
],
14141
"name_id_map": {
14242
"rock1": 67239155,
14343
"rock2": 117776925
14444
},
14545
"_model_transform": null
14646
},
14747
"orientations": {
148-
"data": [
149-
[
150-
500.0,
151-
500.0,
152-
400.0,
153-
0.0,
154-
0.0,
155-
1.0,
156-
67239155,
157-
0.01
158-
]
159-
],
16048
"name_id_map": {
16149
"rock1": 67239155,
16250
"rock2": 117776925

0 commit comments

Comments
 (0)