From b908a4b169821628f414b7d64109524d404306aa Mon Sep 17 00:00:00 2001 From: benjamink Date: Mon, 27 Oct 2025 13:02:36 -0700 Subject: [PATCH 1/2] Add validation for grid2d --- simpeg_drivers/options.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/simpeg_drivers/options.py b/simpeg_drivers/options.py index ad91e7ce..cea89a28 100644 --- a/simpeg_drivers/options.py +++ b/simpeg_drivers/options.py @@ -17,7 +17,6 @@ import numpy as np from geoapps_utils.base import Options -from geoapps_utils.utils.importing import GeoAppsError from geoh5py.data import ( BooleanData, DataAssociationEnum, @@ -89,7 +88,17 @@ class ActiveCellsOptions(BaseModel): @classmethod def at_least_one(cls, data): if all(v is None for v in data.values()): - raise GeoAppsError("Must provide either topography or active model.") + raise ValueError("Must provide either topography or active model.") + return data + + @model_validator(mode="before") + @classmethod + def grid_topo_must_have_elevation_channel(cls, data): + if isinstance(data["topography_object"], Grid2D): + if data["topography"] is None: + raise ValueError( + "Grid2D topography_object must have an 'elevation' channel." + ) return data @model_serializer(mode="wrap") @@ -208,7 +217,7 @@ def _component_name(self, component: str) -> str: @classmethod def mesh_cannot_be_rotated(cls, value: Octree): if isinstance(value, Octree) and value.rotation not in [0.0, None]: - raise GeoAppsError( + raise ValueError( "Rotated meshes are not supported. Please use a mesh with an angle of 0.0." ) return value @@ -491,13 +500,13 @@ class LineSelectionOptions(BaseModel): @classmethod def validate_cell_association(cls, value): if value.association is not DataAssociationEnum.CELL: - raise GeoAppsError("Line identifier must be associated with cells.") + raise ValueError("Line identifier must be associated with cells.") return value @model_validator(mode="after") def line_id_referenced(self): if self.line_id not in self.line_object.values: - raise GeoAppsError("Line id isn't referenced in the line object.") + raise ValueError("Line id isn't referenced in the line object.") return self From 98437e4f8a6496b335cdf3c771cb7d7c4d755ef2 Mon Sep 17 00:00:00 2001 From: benjamink Date: Mon, 27 Oct 2025 13:38:43 -0700 Subject: [PATCH 2/2] add validator and test --- simpeg_drivers/options.py | 8 ++++---- tests/validations_test.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/simpeg_drivers/options.py b/simpeg_drivers/options.py index cea89a28..6c56f093 100644 --- a/simpeg_drivers/options.py +++ b/simpeg_drivers/options.py @@ -93,11 +93,11 @@ def at_least_one(cls, data): @model_validator(mode="before") @classmethod - def grid_topo_must_have_elevation_channel(cls, data): - if isinstance(data["topography_object"], Grid2D): - if data["topography"] is None: + def topo_grid_must_have_elevation_channel(cls, data): + if isinstance(data.get("topography_object", None), Grid2D): + if data.get("topography", None) is None: raise ValueError( - "Grid2D topography_object must have an 'elevation' channel." + "Grid2D topography must be accompanied by a valid elevation channel." ) return data diff --git a/tests/validations_test.py b/tests/validations_test.py index c01beeae..a97ba3ef 100644 --- a/tests/validations_test.py +++ b/tests/validations_test.py @@ -11,6 +11,7 @@ import pytest from geoapps_utils.utils.importing import GeoAppsError from geoh5py import Workspace +from geoh5py.objects import Grid2D from simpeg_drivers.options import CoreOptions @@ -23,3 +24,25 @@ def test_topo_or_active_validation(tmp_path): } with pytest.raises(GeoAppsError, match="active_cells: Value error, Must"): CoreOptions.build(data) + + +def test_topo_grid_missing_elevation(tmp_path): + with Workspace(tmp_path / "test.geoh5") as workspace: + grid = Grid2D.create( + workspace, + name="grid", + u_cell_size=10, + v_cell_size=10, + u_count=10, + v_count=10, + origin=[0, 0, 0], + ) + + data = { + "geoh5": workspace, + "inversion_type": "mvi", + "topography_object": grid, + "topography": None, + } + with pytest.raises(GeoAppsError, match="active_cells: Value error, Grid2D"): + CoreOptions.build(data)