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
[TEST] Fixed grid test
  • Loading branch information
Leguark committed May 25, 2025
commit 501f04effabce1f06cb82549da34a7d457e64b2a
4 changes: 2 additions & 2 deletions gempy/core/data/core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


def calculate_line_coordinates_2points(p1, p2, res):
if isinstance(p1, list):
if isinstance(p1, list) or isinstance(p1, tuple):
p1 = np.array(p1)
if isinstance(p2, list):
if isinstance(p2, list) or isinstance(p2, tuple):
p2 = np.array(p2)
v = p2 - p1 # vector pointing from p1 to p2
u = v / np.linalg.norm(v) # normalize it
Expand Down
37 changes: 31 additions & 6 deletions gempy/core/data/grid_modules/topography.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Topography:

# Fields managed internally
values: short_array_type = field(init=False, default=np.zeros((0, 3)))
resolution: Tuple[int, int] = field(init=False, default=(0, 0))
resolution: Tuple[int, int] = Field(init=True, default=(0, 0))
raster_shape: Tuple[int, ...] = field(init=False, default=())
_mask_topo: Optional[np.ndarray] = field(init=False, default=None, repr=False)
_x: Optional[np.ndarray] = field(init=False, default=None, repr=False)
Expand Down Expand Up @@ -95,7 +95,7 @@ def from_unstructured_mesh(cls, regular_grid, xyz_vertices):
# Reshape the grid for compatibility with existing structure
values_2d = np.stack((x_regular, y_regular, z_regular), axis=-1)

return cls(regular_grid=regular_grid, values_2d=values_2d)
return cls(_regular_grid=regular_grid, values_2d=values_2d)


@classmethod
Expand All @@ -107,7 +107,7 @@ def from_arrays(cls, regular_grid, x_coordinates, y_coordinates, height_values,)
topography_vals = height_values.values[:, :, np.newaxis] # shape (73, 34, 1)
# Stack along the last dimension
result = np.concatenate([x_vals, y_vals, topography_vals], axis=2) # shape (73, 34, 3)
return cls(regular_grid=regular_grid, values_2d=result)
return cls(_regular_grid=regular_grid, values_2d=result)

@property
def extent(self):
Expand Down Expand Up @@ -154,9 +154,34 @@ def set_values(self, values_2d: np.ndarray):
# n,3 array
self.values = values_2d.reshape((-1, 3), order='C')
return self

def set_values2d(self, values: np.ndarray):
self.values_2d = values.reshape(self.resolution)

def set_values2d(self, values: np.ndarray) -> "Topography":
"""
Reconstruct the 2D topography (shape = resolution + [3]) from
a flat Nx3 array (or from self.values if none is provided).
"""
# default to the already-flattened XYZ array

# compute expected size
nx, ny = self.resolution
expected = nx * ny * 3
if values.size != expected:
raise ValueError(
f"Cannot reshape array of size {values.size} into shape {(nx, ny, 3)}."
)

# reshape in C-order to (nx, ny, 3)
self.set_values(
values_2d=values.reshape(nx, ny, 3, order="C")
)

# invalidate any cached mask
self._mask_topo = None
return self

def set_values2d_(self, values: np.ndarray):
resolution = (60, 60)
self.values_2d = values.reshape(*resolution, 3)

@property
def topography_mask(self):
Expand Down
7 changes: 4 additions & 3 deletions test/test_modules/test_grids/test_grids_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def test_section_grids():
gp.set_section_grid(
grid=geo_model.grid,
section_dict={
'section_SW-NE': ([250, 250], [1750, 1750], [100, 100]),
'section_NW-SE': ([250, 1750], [1750, 250], [100, 100])
'section_SW-NE': ((250., 250.), (1750., 1750.), (100, 100)),
'section_NW-SE': ((250., 1750.), (1750., 250.), (100, 100))
}
)

Expand All @@ -41,7 +41,8 @@ def test_section_grids():
verify_moment="after",
file_name=f"verify/{geo_model.meta.name}"
)
gp.compute_model(geo_model, validate_serialization=False)

gp.compute_model(geo_model, validate_serialization=True)
gpv.plot_2d(
model=geo_model,
section_names=['section_SW-NE', 'section_NW-SE', 'topography'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@
],
"is_dirty": true,
"basement_color": "#ffbe00",
"_input_binary_size": 233,
"binary_meta_data": {
"sp_binary_length": 1296
"sp_binary_length": 1296,
"ori_binary_length": 120,
"input_binary_size": 233
}
},
"grid": {
Expand Down Expand Up @@ -104,8 +107,8 @@
"source": null,
"values": [],
"resolution": [
0,
0
60,
60
],
"raster_shape": [],
"_mask_topo": null,
Expand Down Expand Up @@ -151,6 +154,12 @@
"_centered_grid": null,
"_transform": null,
"_octree_levels": -1,
"_grid_binary_size": 37758,
"binary_meta_data": {
"custom_grid_binary_length": 0,
"topography_binary_length": 86400,
"grid_binary_size": 37758
},
"active_grids": 1049
},
"geophysics_input": null,
Expand Down