Skip to content

Commit 58f9ea9

Browse files
committed
Fix tests
1 parent a8ee950 commit 58f9ea9

4 files changed

Lines changed: 63 additions & 200 deletions

File tree

simpeg_drivers-assets/uijson/plate_simulation.ui.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"title": "Plate simulation",
44
"icon": "maxwellplate",
55
"documentation": "https://mirageoscience-plate-simulation.readthedocs-hosted.com/en/latest/",
6-
"conda_environment": "plate_simulation",
7-
"run_command": "plate_simulation.driver",
6+
"conda_environment": "simpeg_drivers",
7+
"run_command": "simpeg_drivers.plate_simulation.driver",
88
"geoh5": "",
99
"monitoring_directory": "",
1010
"simulation": {

tests/plate_simulation/runtest/__init__.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,3 @@
77
# (see LICENSE file at the root of this source code package). '
88
# '
99
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
10-
11-
import numpy as np
12-
from geoh5py.objects import Points, Surface
13-
14-
15-
def get_survey(workspace, n_receivers, n_lines):
16-
X, Y = np.meshgrid( # pylint: disable=invalid-name
17-
np.linspace(-250, 250, n_receivers), np.linspace(-250, 250, n_lines)
18-
)
19-
Z = np.zeros_like(X) # pylint: disable=invalid-name
20-
21-
vertices = np.c_[X.T.flatten(), Y.T.flatten(), Z.T.flatten()]
22-
23-
return Points.create(
24-
workspace,
25-
vertices=vertices,
26-
name="survey",
27-
)
28-
29-
30-
def get_topography(workspace):
31-
vertices = np.array(
32-
[
33-
[-500.0, -500.0, 0.0],
34-
[500.0, -500.0, 0.0],
35-
[500.0, 500.0, 0.0],
36-
[-500.0, 500.0, 0.0],
37-
]
38-
)
39-
cells = np.array([[0, 1, 2], [0, 2, 3]])
40-
41-
return Surface.create(workspace, name="topo", vertices=vertices, cells=cells)

tests/plate_simulation/runtest/driver_test.py

Lines changed: 50 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
# (see LICENSE file at the root of this source code package). '
88
# '
99
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
10-
import os
11-
from pathlib import Path
12-
from uuid import UUID
1310

1411
import numpy as np
1512
from geoh5py import Workspace
@@ -26,128 +23,22 @@
2623
from simpeg_drivers.plate_simulation.models.options import ModelOptions
2724
from simpeg_drivers.plate_simulation.options import MeshOptions
2825
from simpeg_drivers.potential_fields.gravity.options import GravityForwardOptions
29-
30-
from . import get_survey, get_topography
31-
32-
33-
# pylint: disable=duplicate-code
34-
35-
36-
def get_simulation_group(workspace: Workspace, survey: ObjectBase, topography: Surface):
37-
tem_inversion = SimPEGGroup.create(workspace)
38-
options = TDEMForwardOptions.model_construct()
39-
ifile = InputFile.read_ui_json(options.default_ui_json)
40-
options_dict = ifile.ui_json
41-
options_dict["inversion_type"] = "tdem"
42-
options_dict["forward_only"] = True
43-
options_dict["geoh5"] = str(workspace.h5file)
44-
options_dict["topography_object"]["value"] = str(topography.uid)
45-
options_dict["data_object"]["value"] = str(survey.uid)
46-
options_dict["x_channel_bool"] = True
47-
options_dict["y_channel_bool"] = True
48-
options_dict["z_channel_bool"] = True
49-
tem_inversion.options = options_dict
50-
51-
return tem_inversion
52-
53-
54-
def get_input_file(filepath: Path) -> InputFile:
55-
with Workspace(filepath / "test.geoh5") as ws:
56-
with Workspace(
57-
assets_path() / "plate_simulation_demo.geoh5", mode="r"
58-
) as demo_workspace:
59-
survey = demo_workspace.get_entity("Simulation rx")[0].copy(
60-
parent=ws, copy_children=False
61-
)
62-
topography = demo_workspace.get_entity("Topography")[0].copy(parent=ws)
63-
mask = np.zeros(survey.n_vertices, dtype=bool)
64-
mask[::10] = True
65-
new_survey = survey.copy(vertices=survey.vertices[mask, :], cells=None)
66-
67-
simulation = get_simulation_group(ws, new_survey, topography)
68-
ifile = InputFile.read_ui_json(
69-
assets_path() / "uijson" / "plate_simulation.ui.json", validate=False
70-
)
71-
ifile.set_data_value("name", "test_tem_plate_simulation")
72-
ifile.set_data_value("geoh5", ws)
73-
ifile.set_data_value("simulation", simulation)
74-
ifile.set_data_value("u_cell_size", 50.0)
75-
ifile.set_data_value("v_cell_size", 50.0)
76-
ifile.set_data_value("w_cell_size", 50.0)
77-
ifile.set_data_value("depth_core", 600.0)
78-
ifile.set_data_value("max_distance", 200.0)
79-
ifile.set_data_value("padding_distance", 1000.0)
80-
ifile.set_data_value("background", 2000.0)
81-
ifile.set_data_value("overburden", 7500.0)
82-
ifile.set_data_value("thickness", 50.0)
83-
ifile.set_data_value("plate", 20.0)
84-
ifile.set_data_value("width", 100.0)
85-
ifile.set_data_value("strike_length", 1000.0)
86-
ifile.set_data_value("dip_length", 300.0)
87-
ifile.set_data_value("dip", 65.0)
88-
ifile.set_data_value("dip_direction", 65.0)
89-
ifile.set_data_value("number", 2)
90-
ifile.set_data_value("spacing", 600.0)
91-
ifile.set_data_value("relative_locations", True)
92-
ifile.set_data_value("easting", 100.0)
93-
ifile.set_data_value("northing", 100.0)
94-
ifile.set_data_value("elevation", -100.0)
95-
ifile.set_data_value("reference_surface", "overburden")
96-
ifile.set_data_value("reference_type", "min")
97-
98-
return ifile
99-
100-
101-
def test_plate_simulation(tmp_path):
102-
mon_dir = tmp_path / "monitoring"
103-
mon_dir.mkdir(parents=True, exist_ok=True)
104-
105-
ifile = get_input_file(tmp_path)
106-
ifile.set_data_value("monitoring_directory", str(mon_dir))
107-
108-
ifile.write_ui_json("test_plate_simulation.ui.json", path=tmp_path)
109-
result = PlateSimulationDriver.start(
110-
Path(tmp_path / "test_plate_simulation.ui.json")
111-
)
112-
with Workspace(result.out_group.options["geoh5"]) as ws:
113-
out_group = ws.get_entity(UUID(result.out_group.options["out_group"]["value"]))[
114-
0
115-
]
116-
data = next(
117-
obj for obj in out_group.children if isinstance(obj, AirborneTEMReceivers)
118-
)
119-
mesh = next(obj for obj in out_group.children if isinstance(obj, Octree))
120-
model = next(k for k in mesh.children if k.name == "starting_model")
121-
assert data.property_groups is not None
122-
assert len(data.property_groups) == 3
123-
assert all(
124-
k.name in [f"Iteration_0_{i}" for i in "xyz"] for k in data.property_groups
125-
)
126-
assert all(
127-
k.properties is not None and len(k.properties) == 20
128-
for k in data.property_groups
129-
)
130-
assert mesh.n_cells == 16263
131-
assert len(np.unique(model.values)) == 4
132-
assert all(k in np.unique(model.values) for k in [7500, 2000, 20])
133-
assert any(np.isnan(np.unique(model.values)))
134-
135-
# Test the monitoring directory
136-
files = [f for f in os.listdir(mon_dir) if f.endswith("geoh5")]
137-
assert len(files) == 1
138-
with Workspace(mon_dir / files[0]) as ws:
139-
fwr_group = ws.get_entity("Tdem Forward")[0]
140-
141-
# Should only contain octree, files and a survey
142-
assert len(fwr_group.children) == 5
26+
from tests.testing_utils import setup_inversion_workspace
14327

14428

14529
# pylint: disable=too-many-statements
14630
def test_plate_simulation_params_from_input_file(tmp_path):
147-
with Workspace(tmp_path / "test.geoh5") as ws:
148-
topography = get_topography(ws)
149-
survey = get_survey(ws, 10, 10)
31+
geoh5, mesh, model, survey, topography = setup_inversion_workspace(
32+
tmp_path,
33+
background=0.0,
34+
anomaly=0.0,
35+
n_electrodes=8,
36+
n_lines=8,
37+
inversion_type="gravity",
38+
flatten=False,
39+
)
15040

41+
with geoh5.open() as ws:
15142
ifile = InputFile.read_ui_json(
15243
assets_path() / "uijson" / "plate_simulation.ui.json", validate=False
15344
)
@@ -197,44 +88,42 @@ def test_plate_simulation_params_from_input_file(tmp_path):
19788
ifile.data["reference_surface"] = "topography"
19889
ifile.data["reference_type"] = "mean"
19990

200-
params = PlateSimulationOptions.build(ifile)
201-
assert isinstance(params.simulation, SimPEGGroup)
202-
203-
simulation_parameters = params.simulation_parameters()
204-
205-
assert simulation_parameters.inversion_type == "gravity"
206-
assert simulation_parameters.forward_only
207-
assert simulation_parameters.geoh5.h5file == ws.h5file
208-
assert (
209-
simulation_parameters.active_cells.topography_object.uid == topography.uid
210-
)
211-
assert simulation_parameters.data_object.uid == survey.uid
212-
213-
assert isinstance(params.mesh, MeshOptions)
214-
assert params.mesh.u_cell_size == 10.0
215-
assert params.mesh.v_cell_size == 10.0
216-
assert params.mesh.w_cell_size == 10.0
217-
assert params.mesh.depth_core == 400.0
218-
assert params.mesh.max_distance == 200.0
219-
assert params.mesh.padding_distance == 1500.0
220-
assert params.mesh.minimum_level == 8
221-
assert not params.mesh.diagonal_balance
222-
223-
assert isinstance(params.model, ModelOptions)
224-
assert params.model.plate_model.name == "test_gravity_plate_simulation"
225-
assert params.model.background == 1000.0
226-
assert params.model.overburden_model.thickness == 50.0
227-
assert params.model.overburden_model.overburden == 5.0
228-
assert params.model.plate_model.plate == 2.0
229-
assert params.model.plate_model.width == 100.0
230-
assert params.model.plate_model.strike_length == 100.0
231-
assert params.model.plate_model.dip_length == 100.0
232-
assert params.model.plate_model.dip == 0.0
233-
assert params.model.plate_model.dip_direction == 0.0
234-
235-
assert params.model.plate_model.number == 9
236-
assert params.model.plate_model.spacing == 10.0
237-
assert params.model.plate_model.relative_locations
238-
assert params.model.plate_model.easting == 10.0
239-
assert params.model.plate_model.northing == 10.0
240-
assert params.model.plate_model.elevation == -250.0
91+
params = PlateSimulationOptions.build(ifile)
92+
assert isinstance(params.simulation, SimPEGGroup)
93+
94+
simulation_parameters = params.simulation_parameters()
95+
96+
assert simulation_parameters.inversion_type == "gravity"
97+
assert simulation_parameters.forward_only
98+
assert simulation_parameters.geoh5.h5file == ws.h5file
99+
assert simulation_parameters.active_cells.topography_object.uid == topography.uid
100+
assert simulation_parameters.data_object.uid == survey.uid
101+
102+
assert isinstance(params.mesh, MeshOptions)
103+
assert params.mesh.u_cell_size == 10.0
104+
assert params.mesh.v_cell_size == 10.0
105+
assert params.mesh.w_cell_size == 10.0
106+
assert params.mesh.depth_core == 400.0
107+
assert params.mesh.max_distance == 200.0
108+
assert params.mesh.padding_distance == 1500.0
109+
assert params.mesh.minimum_level == 8
110+
assert not params.mesh.diagonal_balance
111+
112+
assert isinstance(params.model, ModelOptions)
113+
assert params.model.plate_model.name == "test_gravity_plate_simulation"
114+
assert params.model.background == 1000.0
115+
assert params.model.overburden_model.thickness == 50.0
116+
assert params.model.overburden_model.overburden == 5.0
117+
assert params.model.plate_model.plate == 2.0
118+
assert params.model.plate_model.width == 100.0
119+
assert params.model.plate_model.strike_length == 100.0
120+
assert params.model.plate_model.dip_length == 100.0
121+
assert params.model.plate_model.dip == 0.0
122+
assert params.model.plate_model.dip_direction == 0.0
123+
124+
assert params.model.plate_model.number == 9
125+
assert params.model.plate_model.spacing == 10.0
126+
assert params.model.plate_model.relative_locations
127+
assert params.model.plate_model.easting == 10.0
128+
assert params.model.plate_model.northing == 10.0
129+
assert params.model.plate_model.elevation == -250.0

tests/plate_simulation/runtest/gravity_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
)
2121
from simpeg_drivers.plate_simulation.options import MeshOptions, PlateSimulationOptions
2222
from simpeg_drivers.potential_fields.gravity.options import GravityForwardOptions
23-
24-
from . import get_survey, get_topography
23+
from tests.testing_utils import setup_inversion_workspace
2524

2625

2726
def test_gravity_plate_simulation(tmp_path):
28-
with Workspace(tmp_path / "test.geoh5") as ws:
29-
topography = get_topography(ws)
30-
survey = get_survey(ws, 10, 10)
27+
geoh5, mesh, model, survey, topography = setup_inversion_workspace(
28+
tmp_path,
29+
background=0.0,
30+
anomaly=0.0,
31+
n_electrodes=8,
32+
n_lines=8,
33+
inversion_type="gravity",
34+
flatten=False,
35+
)
3136

37+
with geoh5.open() as ws:
3238
mesh_params = MeshOptions(
3339
u_cell_size=10.0,
3440
v_cell_size=10.0,

0 commit comments

Comments
 (0)