Skip to content

Commit d1e5495

Browse files
committed
Update 2d tests
1 parent b2cfc62 commit d1e5495

3 files changed

Lines changed: 107 additions & 75 deletions

File tree

tests/run_tests/driver_dc_2d_rotated_gradients_test.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
from __future__ import annotations
1313

14-
import json
1514
from pathlib import Path
1615

1716
import numpy as np
1817
from geoapps_utils.modelling.plates import PlateModel
19-
from geoh5py.groups import PropertyGroup, SimPEGGroup
18+
from geoh5py.groups import PropertyGroup
2019
from geoh5py.workspace import Workspace
2120

2221
from simpeg_drivers.electricals.direct_current.two_dimensions.driver import (
@@ -44,7 +43,7 @@
4443
# To test the full run and validate the inversion.
4544
# Move this file out of the test directory and run.
4645

47-
target_run = {"data_norm": 1.1067294238524659, "phi_d": 55.6, "phi_m": 7.08}
46+
target_run = {"data_norm": 10.305373769233688, "phi_d": 187000, "phi_m": 410}
4847

4948

5049
def test_dc_rotated_2d_fwr_run(tmp_path: Path, n_electrodes=10, n_lines=3):
@@ -56,16 +55,17 @@ def test_dc_rotated_2d_fwr_run(tmp_path: Path, n_electrodes=10, n_lines=3):
5655
v_cell_size=5.0,
5756
depth_core=50.0,
5857
expansion_factor=1.1,
59-
vertical_padding=100.0,
58+
vertical_padding=200.0,
59+
horizontal_padding=200.0,
6060
),
6161
model=ModelOptions(
62-
background=0.01,
63-
anomaly=10.0,
62+
background=0.001,
63+
anomaly=1.0,
6464
plate=PlateModel(
6565
strike_length=1000.0,
66-
dip_length=150.0,
66+
dip_length=50.0,
6767
width=20.0,
68-
origin=(0.0, 0.0, -50),
68+
origin=(0.0, 0.0, 0.0),
6969
direction=90,
7070
dip=45,
7171
),
@@ -101,6 +101,13 @@ def test_dc_rotated_gradient_2d_run(
101101
fwr_group = geoh5.get_entity("Direct Current 2D Forward")[0]
102102
survey = fwr_group.get_entity("survey")[0]
103103
potential = survey.get_data("Iteration_0_potential")[0]
104+
uncertainties = survey.add_data(
105+
{
106+
"Uncertainties": {
107+
"values": np.abs(potential.values) * 0.05 + 1e-4,
108+
}
109+
}
110+
)
104111
# Create property group with orientation
105112
dip = np.ones(components.mesh.n_cells) * 45
106113
azimuth = np.ones(components.mesh.n_cells) * 90
@@ -121,47 +128,31 @@ def test_dc_rotated_gradient_2d_run(
121128
params = DC2DInversionOptions.build(
122129
geoh5=geoh5,
123130
mesh=components.mesh,
124-
drape_model=DrapeModelOptions(
125-
u_cell_size=5.0,
126-
v_cell_size=5.0,
127-
depth_core=100.0,
128-
expansion_factor=1.1,
129-
horizontal_padding=1000.0,
130-
vertical_padding=1000.0,
131-
),
132131
topography_object=components.topography,
133132
data_object=potential.parent,
134133
gradient_rotation=pg,
135134
potential_channel=potential,
136-
potential_uncertainty=1e-3,
137-
starting_model=1e-2,
138-
reference_model=1e-2,
135+
potential_uncertainty=uncertainties,
136+
starting_model=1e-3,
137+
reference_model=1e-3,
139138
s_norm=0.0,
140139
x_norm=0.0,
141140
z_norm=0.0,
141+
length_scale_z=0.1,
142142
max_global_iterations=max_iterations,
143143
initial_beta=None,
144144
initial_beta_ratio=10.0,
145145
percentile=100,
146146
upper_bound=10,
147147
cooling_rate=1,
148+
sens_wts_threshold=1.0,
148149
)
149150
params.write_ui_json(path=tmp_path / "Inv_run.ui.json")
150151

151-
DC2DInversionDriver.start(str(tmp_path / "Inv_run.ui.json"))
152-
153-
basepath = workpath.parent
154-
with open(basepath / "lookup.json", encoding="utf8") as f:
155-
lookup = json.load(f)
156-
middle_line_id = next(k for k, v in lookup.items() if v["line_id"] == 101)
157-
158-
with Workspace(basepath / f"{middle_line_id}.ui.geoh5", mode="r") as workspace:
159-
middle_inversion_group = next(
160-
k for k in workspace.groups if isinstance(k, SimPEGGroup)
161-
)
152+
driver = DC2DInversionDriver.start(str(tmp_path / "Inv_run.ui.json"))
162153

163154
output = get_inversion_output(
164-
basepath / f"{middle_line_id}.ui.geoh5", middle_inversion_group.uid
155+
driver.params.geoh5.h5file, driver.params.out_group.uid
165156
)
166157
if geoh5.open():
167158
output["data"] = potential.values

tests/run_tests/driver_dc_2d_test.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from pathlib import Path
1515

16+
import numpy as np
17+
from geoapps_utils.modelling.plates import PlateModel
1618
from geoh5py.workspace import Workspace
1719

1820
from simpeg_drivers.electricals.direct_current.two_dimensions.driver import (
@@ -41,10 +43,10 @@
4143
# To test the full run and validate the inversion.
4244
# Move this file out of the test directory and run.
4345

44-
target_run = {"data_norm": 1.101767837151429, "phi_d": 2210, "phi_m": 21.4}
46+
target_run = {"data_norm": 11.14351536256954, "phi_d": 6360, "phi_m": 245}
4547

4648

47-
def test_dc_p3d_fwr_run(
49+
def test_dc_2d_fwr_run(
4850
tmp_path: Path,
4951
n_electrodes=10,
5052
n_lines=3,
@@ -58,9 +60,21 @@ def test_dc_p3d_fwr_run(
5860
v_cell_size=5.0,
5961
depth_core=50.0,
6062
expansion_factor=1.1,
61-
vertical_padding=100.0,
63+
vertical_padding=200.0,
64+
horizontal_padding=200.0,
65+
),
66+
model=ModelOptions(
67+
background=0.001,
68+
anomaly=1.0,
69+
plate=PlateModel(
70+
strike_length=1000.0,
71+
dip_length=20.0,
72+
width=20.0,
73+
origin=(0.0, 0.0, 0.0),
74+
direction=90,
75+
dip=90,
76+
),
6277
),
63-
model=ModelOptions(background=0.01, anomaly=10.0),
6478
)
6579
with get_workspace(tmp_path / "inversion_test.ui.geoh5") as geoh5:
6680
components = SyntheticsComponents(geoh5=geoh5, options=opts)
@@ -76,34 +90,40 @@ def test_dc_p3d_fwr_run(
7690
fwr_driver.run()
7791

7892

79-
def test_dc_p3d_run(
93+
def test_dc_2d_run(
8094
tmp_path: Path,
8195
max_iterations=1,
8296
pytest=True,
8397
):
8498
workpath = tmp_path / "inversion_test.ui.geoh5"
8599
if pytest:
86-
workpath = tmp_path.parent / "test_dc_p3d_fwr_run0" / "inversion_test.ui.geoh5"
100+
workpath = tmp_path.parent / "test_dc_2d_fwr_run0" / "inversion_test.ui.geoh5"
87101

88102
with Workspace(workpath) as geoh5:
89103
components = SyntheticsComponents(geoh5)
90104
fwr_group = geoh5.get_entity("Direct Current 2D Forward")[0]
91105
survey = fwr_group.get_entity("survey")[0]
92106
potential = survey.get_data("Iteration_0_potential")[0]
93-
107+
uncertainties = survey.add_data(
108+
{
109+
"Uncertainties": {
110+
"values": np.abs(potential.values) * 0.05 + 1e-4,
111+
}
112+
}
113+
)
94114
# Run the inverse
95115
params = DC2DInversionOptions.build(
96116
geoh5=geoh5,
97117
mesh=components.mesh,
98118
topography_object=components.topography,
99119
data_object=potential.parent,
100120
potential_channel=potential,
101-
potential_uncertainty=1e-3,
121+
potential_uncertainty=uncertainties,
102122
line_selection=LineSelectionOptions(
103123
line_object=potential.parent.get_entity("Line IDs")[0]
104124
),
105-
starting_model=1e-2,
106-
reference_model=1e-2,
125+
starting_model=1e-3,
126+
reference_model=1e-3,
107127
s_norm=0.0,
108128
x_norm=1.0,
109129
z_norm=1.0,
@@ -134,14 +154,20 @@ def test_dc_single_run(
134154
):
135155
workpath = tmp_path / "inversion_test.ui.geoh5"
136156
if pytest:
137-
workpath = tmp_path.parent / "test_dc_p3d_fwr_run0" / "inversion_test.ui.geoh5"
157+
workpath = tmp_path.parent / "test_dc_2d_fwr_run0" / "inversion_test.ui.geoh5"
138158

139159
with Workspace(workpath) as geoh5:
140160
components = SyntheticsComponents(geoh5)
141161
fwr_group = geoh5.get_entity("Direct Current 2D Forward")[0]
142162
survey = fwr_group.get_entity("survey")[0]
143163
potential = survey.get_data("Iteration_0_potential")[0]
144-
164+
uncertainties = survey.add_data(
165+
{
166+
"Uncertainties": {
167+
"values": np.abs(potential.values) * 0.05 + 1e-4,
168+
}
169+
}
170+
)
145171
# Run the inverse
146172
params = DC2DInversionOptions.build(
147173
geoh5=geoh5,
@@ -150,12 +176,12 @@ def test_dc_single_run(
150176
topography_object=components.topography,
151177
data_object=potential.parent,
152178
potential_channel=potential,
153-
potential_uncertainty=1e-3,
179+
potential_uncertainty=uncertainties,
154180
line_selection=LineSelectionOptions(
155181
line_object=potential.parent.get_entity("Line IDs")[0], line_id=2
156182
),
157-
starting_model=1e-2,
158-
reference_model=1e-2,
183+
starting_model=1e-3,
184+
reference_model=1e-3,
159185
s_norm=0.0,
160186
x_norm=1.0,
161187
z_norm=1.0,
@@ -168,25 +194,27 @@ def test_dc_single_run(
168194
)
169195
params.write_ui_json(path=tmp_path / "Inv_run.ui.json")
170196

171-
driver = DC2DInversionDriver.start(str(tmp_path / "Inv_run.ui.json"))
197+
DC2DInversionDriver.start(str(tmp_path / "Inv_run.ui.json"))
172198

173-
output = get_inversion_output(
174-
driver.params.geoh5.h5file, driver.params.out_group.uid
175-
)
176-
if geoh5.open():
177-
output["data"] = potential.values
178-
if pytest:
179-
check_target(output, target_run)
199+
with Workspace(workpath) as geoh5:
200+
inv_group = geoh5.get_entity("Direct Current Single 2D Inversion")[0]
201+
mesh = inv_group.get_entity("mesh")[0]
202+
model = mesh.get_entity("Iteration_1_model")[0]
203+
204+
# Check that model values for lines 1 and 3 are close to the starting model (1e-3) and that line 2 has been updated.
205+
np.testing.assert_almost_equal(np.nanmin(model.values[:2369]), 1e-3, decimal=3)
206+
np.testing.assert_almost_equal(np.nanmin(model.values[-2368:]), 1e-3, decimal=3)
207+
assert np.nanmax(model.values[2368:-2368]) > 1e-3
180208

181209

182210
if __name__ == "__main__":
183211
# Full run
184-
test_dc_p3d_fwr_run(
212+
test_dc_2d_fwr_run(
185213
Path("./"),
186214
n_electrodes=20,
187215
n_lines=3,
188216
)
189-
test_dc_p3d_run(
217+
test_dc_2d_run(
190218
Path("./"),
191219
max_iterations=20,
192220
pytest=False,

0 commit comments

Comments
 (0)