Skip to content

Commit 5bbf54b

Browse files
committed
[WIP] Making topography a data class
1 parent 39ac3de commit 5bbf54b

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

gempy/core/data/grid_modules/topography.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
1+
import dataclasses
2+
13
import warnings
2-
from typing import Optional
4+
from pydantic import Field
5+
from typing import Optional, Tuple
36

47
import numpy as np
58

69
from .grid_types import RegularGrid
710
from ....modules.grids.create_topography import _LoadDEMArtificial
811

912
from ....optional_dependencies import require_skimage
13+
from dataclasses import field, dataclass
1014

1115

16+
@dataclass
1217
class Topography:
13-
"""
14-
Object to include topography in the model.
15-
16-
Notes:
17-
This always assumes that the topography we pass fits perfectly the extent
1818

1919
"""
20+
Object to include topography in the model.
21+
Notes:
22+
This always assumes that the topography we pass fits perfectly the extent.
23+
"""
24+
25+
regular_grid: RegularGrid
26+
values_2d: np.ndarray = Field(exclude=True, default_factory=lambda: np.zeros((0, 0, 3)))
27+
source: Optional[str] = None
28+
29+
# Fields managed internally
30+
values: np.ndarray = field(init=False, default_factory=lambda: np.zeros((0, 3)))
31+
resolution: Tuple[int, int] = field(init=False, default=(0, 0))
32+
raster_shape: Tuple[int, ...] = field(init=False, default=())
33+
_mask_topo: Optional[np.ndarray] = field(init=False, default=None, repr=False)
34+
_x: Optional[np.ndarray] = field(init=False, default=None, repr=False)
35+
_y: Optional[np.ndarray] = field(init=False, default=None, repr=False)
36+
37+
def __post_init__(self):
38+
# if a non-empty array was provided, initialize the flattened values
39+
if self.values_2d.size:
40+
self.set_values(self.values_2d)
2041

21-
def __init__(self, regular_grid: RegularGrid, values_2d: Optional[np.ndarray] = None):
22-
23-
self._mask_topo = None
24-
self._regular_grid = regular_grid
25-
26-
# Values (n, 3)
27-
self.values = np.zeros((0, 3))
28-
29-
# Values (n, n, 3)
30-
self.values_2d = np.zeros((0, 0, 3))
31-
32-
# Shape original
33-
self.raster_shape = tuple()
34-
35-
# Topography Resolution
36-
self.resolution = np.zeros((0, 3))
37-
38-
# Source for the
39-
self.source = None
40-
41-
# Coords
42-
self._x = None
43-
self._y = None
44-
45-
if values_2d is not None:
46-
self.set_values(values_2d)
4742

4843
@classmethod
4944
def from_subsurface_structured_data(cls, structured_data: 'subsurface.StructuredData', regular_grid: RegularGrid):

test/test_modules/test_grids/test_grids_sections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def test_section_grids():
2020
evaluation_options=geo_model.interpolation_options.evaluation_options
2121
)
2222

23+
model = geo_model
24+
model_json = model.model_dump_json(by_alias=True, indent=4)
2325
gp.set_section_grid(
2426
grid=geo_model.grid,
2527
section_dict={
@@ -28,13 +30,15 @@ def test_section_grids():
2830
}
2931
)
3032

33+
model_json = model.model_dump_json(by_alias=True, indent=4)
3134
gp.set_topography_from_random(
3235
grid=geo_model.grid,
3336
fractal_dimension=1.2,
3437
d_z=np.array([200, 1000]),
3538
topography_resolution=np.array([60, 60])
3639
)
3740

41+
model_json = model.model_dump_json(by_alias=True, indent=4)
3842
gp.compute_model(geo_model)
3943
gpv.plot_2d(
4044
model=geo_model,

0 commit comments

Comments
 (0)