Skip to content

Commit e60169d

Browse files
committed
Merge branch 'develop' into GEOPY-2045
2 parents e251196 + 077d40e commit e60169d

7 files changed

Lines changed: 306 additions & 289 deletions

File tree

tests/data_test.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ def get_mvi_params(tmp_path: Path, **kwargs) -> MVIInversionOptions:
4242
n_lines=2,
4343
inversion_type="magnetic_vector",
4444
)
45-
tmi_channel = survey.add_data(
46-
{"tmi": {"values": np.random.rand(survey.n_vertices)}}
47-
)
45+
with geoh5.open():
46+
tmi_channel = survey.add_data(
47+
{"tmi": {"values": np.random.rand(survey.n_vertices)}}
48+
)
4849
params = MVIInversionOptions(
4950
geoh5=geoh5,
5051
data_object=survey,
@@ -60,9 +61,10 @@ def get_mvi_params(tmp_path: Path, **kwargs) -> MVIInversionOptions:
6061
def test_save_data(tmp_path: Path):
6162
params = get_mvi_params(tmp_path)
6263
geoh5 = params.geoh5
63-
data = InversionData(geoh5, params)
64+
with geoh5.open():
65+
data = InversionData(geoh5, params)
6466

65-
assert len(data.entity.vertices) > 0
67+
assert len(data.entity.vertices) > 0
6668

6769

6870
def test_survey_data(tmp_path: Path):
@@ -202,26 +204,29 @@ def test_has_tensor():
202204
def test_get_uncertainty_component(tmp_path: Path):
203205
params = get_mvi_params(tmp_path, tmi_uncertainty=1.0)
204206
geoh5 = params.geoh5
205-
data = InversionData(geoh5, params)
206-
unc = params.uncertainties["tmi"]
207-
assert len(np.unique(unc)) == 1
208-
assert np.unique(unc)[0] == 1
209-
assert len(unc) == data.entity.n_vertices
207+
with geoh5.open():
208+
data = InversionData(geoh5, params)
209+
unc = params.uncertainties["tmi"]
210+
assert len(np.unique(unc)) == 1
211+
assert np.unique(unc)[0] == 1
212+
assert len(unc) == data.entity.n_vertices
210213

211214

212215
def test_normalize(tmp_path: Path):
213216
params = get_mvi_params(tmp_path)
214217
geoh5 = params.geoh5
215-
data = InversionData(geoh5, params)
216-
data.normalizations = data.get_normalizations()
217-
test_data = data.normalize(data.observed)
218-
assert all(test_data["tmi"] == params.data["tmi"])
219-
assert len(test_data) == 1
218+
with geoh5.open():
219+
data = InversionData(geoh5, params)
220+
data.normalizations = data.get_normalizations()
221+
test_data = data.normalize(data.observed)
222+
assert all(test_data["tmi"] == params.data["tmi"])
223+
assert len(test_data) == 1
220224

221225

222226
def test_get_survey(tmp_path: Path):
223227
params = get_mvi_params(tmp_path, tmi_uncertainty=1.0)
224228
geoh5 = params.geoh5
225-
data = InversionData(geoh5, params)
226-
survey = data.create_survey()
227-
assert isinstance(survey[0], simpeg.potential_fields.magnetics.Survey)
229+
with geoh5.open():
230+
data = InversionData(geoh5, params)
231+
survey = data.create_survey()
232+
assert isinstance(survey[0], simpeg.potential_fields.magnetics.Survey)

tests/driver_test.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,29 @@ def test_smallness_terms(tmp_path: Path):
3232
flatten=False,
3333
)
3434

35-
gz = survey.add_data({"gz": {"values": np.ones(survey.n_vertices)}})
36-
mesh = model.parent
37-
active_cells = ActiveCellsOptions(topography_object=topography)
38-
params = GravityInversionOptions(
39-
geoh5=geoh5,
40-
mesh=mesh,
41-
active_cells=active_cells,
42-
data_object=gz.parent,
43-
starting_model=1e-4,
44-
reference_model=0.0,
45-
alpha_s=1.0,
46-
s_norm=0.0,
47-
gradient_type="components",
48-
gz_channel_bool=True,
49-
gz_channel=gz,
50-
gz_uncertainty=2e-3,
51-
lower_bound=0.0,
52-
max_global_iterations=1,
53-
initial_beta_ratio=1e-2,
54-
prctile=100,
55-
store_sensitivities="ram",
56-
)
57-
params.alpha_s = None
58-
driver = GravityInversionDriver(params)
59-
assert driver.regularization.objfcts[0].alpha_s == 0.0
35+
with geoh5.open():
36+
gz = survey.add_data({"gz": {"values": np.ones(survey.n_vertices)}})
37+
mesh = model.parent
38+
active_cells = ActiveCellsOptions(topography_object=topography)
39+
params = GravityInversionOptions(
40+
geoh5=geoh5,
41+
mesh=mesh,
42+
active_cells=active_cells,
43+
data_object=gz.parent,
44+
starting_model=1e-4,
45+
reference_model=0.0,
46+
alpha_s=1.0,
47+
s_norm=0.0,
48+
gradient_type="components",
49+
gz_channel_bool=True,
50+
gz_channel=gz,
51+
gz_uncertainty=2e-3,
52+
lower_bound=0.0,
53+
max_global_iterations=1,
54+
initial_beta_ratio=1e-2,
55+
prctile=100,
56+
store_sensitivities="ram",
57+
)
58+
params.alpha_s = None
59+
driver = GravityInversionDriver(params)
60+
assert driver.regularization.objfcts[0].alpha_s == 0.0

tests/locations_test.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ def get_mvi_params(tmp_path: Path) -> MVIInversionOptions:
3232
n_lines=2,
3333
inversion_type="magnetic_vector",
3434
)
35-
tmi_channel = survey.add_data(
36-
{"tmi": {"values": np.random.rand(survey.n_vertices)}}
37-
)
35+
with geoh5.open():
36+
tmi_channel = survey.add_data(
37+
{"tmi": {"values": np.random.rand(survey.n_vertices)}}
38+
)
3839
params = MVIInversionOptions(
3940
geoh5=geoh5,
4041
data_object=survey,
@@ -50,51 +51,54 @@ def get_mvi_params(tmp_path: Path) -> MVIInversionOptions:
5051
def test_mask(tmp_path: Path):
5152
params = get_mvi_params(tmp_path)
5253
geoh5 = params.geoh5
53-
locations = InversionLocations(geoh5, params)
54-
loc_mask = [0, 1, 1, 0]
55-
locations.mask = loc_mask
56-
assert isinstance(locations.mask, np.ndarray)
57-
assert locations.mask.dtype == bool
58-
loc_mask = [0, 1, 2, 3]
59-
with pytest.raises(ValueError) as excinfo:
54+
with geoh5.open():
55+
locations = InversionLocations(geoh5, params)
56+
loc_mask = [0, 1, 1, 0]
6057
locations.mask = loc_mask
61-
assert "Badly formed" in str(excinfo.value)
58+
assert isinstance(locations.mask, np.ndarray)
59+
assert locations.mask.dtype == bool
60+
loc_mask = [0, 1, 2, 3]
61+
with pytest.raises(ValueError) as excinfo:
62+
locations.mask = loc_mask
63+
assert "Badly formed" in str(excinfo.value)
6264

6365

6466
def test_get_locations(tmp_path: Path):
6567
params = get_mvi_params(tmp_path)
6668
geoh5 = params.geoh5
67-
locs = np.ones((10, 3), dtype=float)
68-
points_object = Points.create(geoh5, name="test-data", vertices=locs)
69-
locations = InversionLocations(geoh5, params)
70-
dlocs = locations.get_locations(points_object)
71-
np.testing.assert_allclose(locs, dlocs)
69+
with geoh5.open():
70+
locs = np.ones((10, 3), dtype=float)
71+
points_object = Points.create(geoh5, name="test-data", vertices=locs)
72+
locations = InversionLocations(geoh5, params)
73+
dlocs = locations.get_locations(points_object)
74+
np.testing.assert_allclose(locs, dlocs)
7275

73-
xg, yg = np.meshgrid(np.arange(5) + 0.5, np.arange(5) + 0.5)
74-
locs = np.c_[xg.ravel(), yg.ravel(), np.zeros(25)]
75-
grid_object = Grid2D.create(
76-
geoh5,
77-
origin=[0, 0, 0],
78-
u_cell_size=1.0,
79-
v_cell_size=1.0,
80-
u_count=5,
81-
v_count=5,
82-
rotation=0.0,
83-
dip=0.0,
84-
)
85-
dlocs = locations.get_locations(grid_object)
86-
np.testing.assert_allclose(dlocs, locs)
76+
xg, yg = np.meshgrid(np.arange(5) + 0.5, np.arange(5) + 0.5)
77+
locs = np.c_[xg.ravel(), yg.ravel(), np.zeros(25)]
78+
grid_object = Grid2D.create(
79+
geoh5,
80+
origin=[0, 0, 0],
81+
u_cell_size=1.0,
82+
v_cell_size=1.0,
83+
u_count=5,
84+
v_count=5,
85+
rotation=0.0,
86+
dip=0.0,
87+
)
88+
dlocs = locations.get_locations(grid_object)
89+
np.testing.assert_allclose(dlocs, locs)
8790

8891

8992
def test_filter(tmp_path: Path):
9093
params = get_mvi_params(tmp_path)
9194
geoh5 = params.geoh5
92-
locations = InversionLocations(geoh5, params)
93-
test_data = np.array([0, 1, 2, 3, 4, 5])
94-
locations.mask = np.array([0, 0, 1, 1, 1, 0])
95-
filtered_data = locations.filter(test_data)
96-
assert np.all(filtered_data == [2, 3, 4])
95+
with geoh5.open():
96+
locations = InversionLocations(geoh5, params)
97+
test_data = np.array([0, 1, 2, 3, 4, 5])
98+
locations.mask = np.array([0, 0, 1, 1, 1, 0])
99+
filtered_data = locations.filter(test_data)
100+
assert np.all(filtered_data == [2, 3, 4])
97101

98-
test_data = {"key": test_data}
99-
filtered_data = locations.filter(test_data)
100-
assert np.all(filtered_data["key"] == [2, 3, 4])
102+
test_data = {"key": test_data}
103+
filtered_data = locations.filter(test_data)
104+
assert np.all(filtered_data["key"] == [2, 3, 4])

0 commit comments

Comments
 (0)