Skip to content

Commit 39ac3de

Browse files
committed
[WIP] Dealing with custom grids
1 parent 727d40e commit 39ac3de

File tree

7 files changed

+182
-27
lines changed

7 files changed

+182
-27
lines changed

gempy/API/compute_API.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
dotenv.load_dotenv()
2121

2222

23-
def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConfig] = None) -> Solutions:
23+
def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConfig] = None,
24+
**kwargs) -> Solutions:
2425
"""
2526
Compute the geological model given the provided GemPy model.
2627
@@ -61,7 +62,7 @@ def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConf
6162
case _:
6263
raise ValueError(f'Backend {engine_config} not supported')
6364

64-
if os.getenv("VALIDATE_SERIALIZATION", False):
65+
if os.getenv("VALIDATE_SERIALIZATION", False) and kwargs.get("validate_serialization", True):
6566
from ..modules.serialization.save_load import save_model
6667
import tempfile
6768
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as tmp:
@@ -90,7 +91,7 @@ def compute_model_at(gempy_model: GeoModel, at: np.ndarray,
9091
xyz_coord=at
9192
)
9293

93-
sol = compute_model(gempy_model, engine_config)
94+
sol = compute_model(gempy_model, engine_config, validate_serialization=False)
9495
return sol.raw_arrays.custom
9596

9697

gempy/API/grid_API.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def set_topography_from_file(grid: Grid, filepath: str, crop_to_extent: Union[Se
8686

8787

8888
def set_custom_grid(grid: Grid, xyz_coord: np.ndarray):
89-
custom_grid = CustomGrid(xyx_coords=xyz_coord)
89+
custom_grid = CustomGrid(values=xyz_coord)
9090
grid.custom_grid = custom_grid
9191

9292
set_active_grid(grid, [Grid.GridTypes.CUSTOM])

gempy/core/data/grid_modules/grid_types.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def plot_rotation(regular_grid, pivot, point_x_axis, point_y_axis):
255255
plt.show()
256256

257257

258+
@dataclasses.dataclass
258259
class Sections:
259260
"""
260261
Object that creates a grid of cross sections between two points.
@@ -364,6 +365,7 @@ def get_section_grid(self, section_name: str):
364365
return self.values[l0:l1]
365366

366367

368+
@dataclasses.dataclass
367369
class CustomGrid:
368370
"""Object that contains arbitrary XYZ coordinates.
369371
@@ -374,26 +376,20 @@ class CustomGrid:
374376
values (np.ndarray): XYZ coordinates
375377
"""
376378

377-
def __init__(self, xyx_coords: np.ndarray):
378-
self.values = np.zeros((0, 3))
379-
self.set_custom_grid(xyx_coords)
380-
381-
def set_custom_grid(self, custom_grid: np.ndarray):
382-
"""
383-
Give the coordinates of an external generated grid
384-
385-
Args:
386-
custom_grid (numpy.ndarray like): XYZ (in columns) of the desired coordinates
387-
388-
Returns:
389-
numpy.ndarray: Unraveled 3D numpy array where every row correspond to the xyz coordinates of a regular
390-
grid
391-
"""
392-
custom_grid = np.atleast_2d(custom_grid)
379+
values: np.ndarray = Field(
380+
exclude=True,
381+
default_factory=lambda: np.zeros((0, 3)),
382+
repr=False
383+
)
384+
385+
386+
def __post_init__(self):
387+
custom_grid = np.atleast_2d(self.values)
393388
assert type(custom_grid) is np.ndarray and custom_grid.shape[1] == 3, \
394389
'The shape of new grid must be (n,3) where n is the number of' \
395390
' points of the grid'
396391

397-
self.values = custom_grid
398-
self.length = self.values.shape[0]
399-
return self.values
392+
393+
@property
394+
def length(self):
395+
return self.values.shape[0]

gempy/modules/serialization/save_load.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from typing import Literal
24

35
import warnings
@@ -142,15 +144,15 @@ def _validate_serialization(original_model, model_deserialized):
142144
o_b = hash(model_deserialized.structural_frame.orientations_copy.data.tobytes())
143145
assert a == b, "Hashes for surface points are not equal"
144146
assert o_a == o_b, "Hashes for orientations are not equal"
145-
original_model___str__ = original_model.__str__()
146-
deserialized___str__ = model_deserialized.__str__()
147+
original_model___str__ = re.sub(r'\s+', ' ', original_model.__str__())
148+
deserialized___str__ = re.sub(r'\s+', ' ', model_deserialized.__str__())
147149
if original_model___str__ != deserialized___str__:
148150
# Find first char that is not the same
149151
for i in range(min(len(original_model___str__), len(deserialized___str__))):
150152
if original_model___str__[i] != deserialized___str__[i]:
151153
break
152154
print(f"First difference at index {i}:")
153-
i1 = 50
155+
i1 = 10
154156
print(f"Original: {original_model___str__[i - i1:i + i1]}")
155157
print(f"Deserialized: {deserialized___str__[i - i1:i + i1]}")
156158

test/test_modules/test_grids/test_custom_grid.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from gempy.modules.serialization.save_load import verify_model_serialization
45
from test.conftest import TEST_SPEED, TestSpeed
56
import gempy as gp
67
from gempy.core.data.enumerators import ExampleModel
@@ -32,7 +33,13 @@ def test_custom_grid():
3233
xyz_coord=xyz_coord
3334
)
3435

35-
sol: gp.data.Solutions = gp.compute_model(geo_model)
36+
verify_model_serialization(
37+
model=geo_model,
38+
verify_moment="after",
39+
file_name=f"verify/{geo_model.meta.name}"
40+
)
41+
42+
sol: gp.data.Solutions = gp.compute_model(geo_model, validate_serialization=False)
3643
np.testing.assert_array_equal(
3744
sol.raw_arrays.custom,
3845
np.array([3., 3., 3., 3., 1., 1., 1., 1.])
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"meta": {
3+
"name": "fold",
4+
"creation_date": "<DATE_IGNORED>",
5+
"last_modification_date": null,
6+
"owner": null
7+
},
8+
"structural_frame": {
9+
"structural_groups": [
10+
{
11+
"name": "Strat_Series",
12+
"elements": [
13+
{
14+
"name": "rock2",
15+
"is_active": true,
16+
"_color": "#9f0052",
17+
"surface_points": {
18+
"name_id_map": {
19+
"rock1": 67239155,
20+
"rock2": 117776925
21+
},
22+
"_model_transform": null
23+
},
24+
"orientations": {
25+
"name_id_map": {
26+
"rock1": 67239155,
27+
"rock2": 117776925
28+
},
29+
"_model_transform": null
30+
},
31+
"scalar_field_at_interface": null,
32+
"_id": 117776925
33+
},
34+
{
35+
"name": "rock1",
36+
"is_active": true,
37+
"_color": "#015482",
38+
"surface_points": {
39+
"name_id_map": {
40+
"rock1": 67239155,
41+
"rock2": 117776925
42+
},
43+
"_model_transform": null
44+
},
45+
"orientations": {
46+
"name_id_map": {
47+
"rock1": 67239155,
48+
"rock2": 117776925
49+
},
50+
"_model_transform": null
51+
},
52+
"scalar_field_at_interface": null,
53+
"_id": 67239155
54+
}
55+
],
56+
"structural_relation": 1,
57+
"fault_relations": null,
58+
"faults_input_data": null,
59+
"solution": null
60+
}
61+
],
62+
"is_dirty": true,
63+
"basement_color": "#ffbe00",
64+
"binary_meta_data": {
65+
"sp_binary_length": 1296
66+
}
67+
},
68+
"grid": {
69+
"_octree_grid": {
70+
"resolution": [
71+
32,
72+
32,
73+
32
74+
],
75+
"extent": [
76+
0.0,
77+
1000.0,
78+
0.0,
79+
1000.0,
80+
0.0,
81+
1000.0
82+
],
83+
"_transform": null
84+
},
85+
"_dense_grid": null,
86+
"_custom_grid": {},
87+
"_topography": null,
88+
"_sections": null,
89+
"_centered_grid": null,
90+
"_transform": null,
91+
"_octree_levels": -1,
92+
"active_grids": 1029
93+
},
94+
"geophysics_input": null,
95+
"input_transform": {
96+
"position": [
97+
-500.0,
98+
-500.0,
99+
-510.0
100+
],
101+
"rotation": [
102+
0.0,
103+
0.0,
104+
0.0
105+
],
106+
"scale": [
107+
0.0005,
108+
0.0005,
109+
0.0005
110+
],
111+
"_is_default_transform": false,
112+
"_cached_pivot": null
113+
},
114+
"_interpolation_options": {
115+
"kernel_options": {
116+
"range": 1.7,
117+
"c_o": 10.0,
118+
"uni_degree": 1,
119+
"i_res": 4.0,
120+
"gi_res": 2.0,
121+
"number_dimensions": 3,
122+
"kernel_function": "cubic",
123+
"kernel_solver": 1,
124+
"compute_condition_number": false,
125+
"optimizing_condition_number": false,
126+
"condition_number": null
127+
},
128+
"evaluation_options": {
129+
"_number_octree_levels": 2,
130+
"_number_octree_levels_surface": 4,
131+
"octree_curvature_threshold": -1.0,
132+
"octree_error_threshold": 1.0,
133+
"octree_min_level": 2,
134+
"mesh_extraction": true,
135+
"mesh_extraction_masking_options": 3,
136+
"mesh_extraction_fancy": true,
137+
"evaluation_chunk_size": 500000,
138+
"compute_scalar_gradient": false,
139+
"verbose": false
140+
},
141+
"debug": true,
142+
"cache_mode": 3,
143+
"cache_model_name": "fold",
144+
"block_solutions_type": 1,
145+
"sigmoid_slope": 5000000,
146+
"debug_water_tight": false
147+
}
148+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"_octree_levels": -1,
9292
"active_grids": 1026
9393
},
94+
"geophysics_input": null,
9495
"input_transform": {
9596
"position": [
9697
-500.0,

0 commit comments

Comments
 (0)