Skip to content

Commit cf9c36d

Browse files
committed
Addapt angular distance to calculete the distancde beween individual arrays
1 parent cf8e442 commit cf9c36d

File tree

4 files changed

+2038
-35
lines changed

4 files changed

+2038
-35
lines changed

src/magali/_inversion.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ def _jacobian_nonlinear(x, y, z, xc, yc, zc, mx, my, mz, result):
441441
def iterative_nonlinear_inversion(
442442
data_up,
443443
bounding_boxes,
444+
height_difference=5.0,
444445
copy_data=True,
445446
):
446447
"""

src/magali/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def angular_distance(vectors_a, vectors_b, degrees=True):
114114
ndarray of shape (N,)
115115
Angular distance between the corresponding vector pairs.
116116
"""
117-
vectors_a = np.asarray(vectors_a)
118-
vectors_b = np.asarray(vectors_b)
117+
vectors_a = np.atleast_2d(vectors_a)
118+
vectors_b = np.atleast_2d(vectors_b)
119119

120120
dot_products = np.sum(vectors_a * vectors_b, axis=1)
121121
norms_a = np.linalg.norm(vectors_a, axis=1)

src/magali/temporary/iterative_inversion.ipynb

Lines changed: 2006 additions & 0 deletions
Large diffs are not rendered by default.

test/test_inversion.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,32 @@ def test_nonlinear_magnetic_dipole_bz_inversion():
102102

103103
sensor_sample_distance = 5.0
104104
region = [0, 2000, 0, 2000]
105-
spacing = 2.0
106-
true_inclination = 30
107-
true_declination = 40
108-
true_dispersion_angle = 5
109-
size = 5
105+
spacing = 2
110106

111-
directions_inclination, directions_declination = random_directions(
112-
true_inclination,
113-
true_declination,
114-
true_dispersion_angle,
115-
size=size,
116-
random_state=SEED,
107+
dipole_coordinates = (
108+
np.concatenate([[1250, 1300, 500, 500, 1252, 1250, 1790, 1782, 1720]]),
109+
np.concatenate([[500, 1750, 1000, 890, 380, 230, 1210, 1122, 1255]]),
110+
np.concatenate([[-15, -15, -30, -30, -30, -15, -15, -15, -15]]),
117111
)
118112

119-
dipoles_amplitude = abs(rng.normal(0, 100, size)) * 1.0e-14
120-
dipole_coordinates = (
121-
rng.integers(30, 1970, size),
122-
rng.integers(30, 1970, size),
123-
rng.integers(-20, -1, size),
113+
true_inclination = np.concatenate([[10, -10, -5, 10, 10, 10, -10, -160, -10]])
114+
true_declination = np.concatenate([[10, 170, 190, 170, 170, 10, -10, -10, -170]])
115+
true_intensity = np.concatenate(
116+
[[5e-11, 5e-11, 5e-11, 2e-12, 2e-12, 5e-11, 5e-13, 5e-12, 5e-13]]
124117
)
125118

126119
dipole_moments = hm.magnetic_angles_to_vec(
127-
inclination=directions_inclination,
128-
declination=directions_declination,
129-
intensity=dipoles_amplitude,
120+
inclination=true_inclination,
121+
declination=true_declination,
122+
intensity=true_intensity,
130123
)
131-
132124
data = dipole_bz_grid(
133125
region, spacing, sensor_sample_distance, dipole_coordinates, dipole_moments
134126
)
135127
noise_std_dev = 100
136128
data.values += rng.normal(loc=0, scale=noise_std_dev, size=data.shape)
137129

138130
height_difference = 5.0
139-
140131
data_up = (
141132
hm.upward_continuation(data, height_difference)
142133
.assign_attrs(data.attrs)
@@ -152,16 +143,14 @@ def test_nonlinear_magnetic_dipole_bz_inversion():
152143
tga, in_range=tuple(np.percentile(tga, (1, 99)))
153144
)
154145
data_tga_stretched = xr.DataArray(stretched, coords=data_up.coords)
155-
156-
bounding_boxes = detect_anomalies(
157-
data_tga_stretched,
158-
size_range=[50, 150],
159-
detection_threshold=0.2,
160-
border_exclusion=2,
161-
size_multiplier=0.75,
162-
)
146+
bounding_box = [
147+
np.float64(1206.6619048833757),
148+
np.float64(1393.3380951166243),
149+
np.float64(1656.6619048833757),
150+
np.float64(1843.3380951166243),
151+
]
163152
anomaly = data_up.sel(
164-
x=slice(*bounding_boxes[1][:2]), y=slice(*bounding_boxes[1][2:])
153+
x=slice(*bounding_box[:2]), y=slice(*bounding_box[2:])
165154
)
166155

167156
table = vd.grid_to_table(anomaly)
@@ -175,6 +164,7 @@ def test_nonlinear_magnetic_dipole_bz_inversion():
175164
initial_location=euler.location_, max_iter=1000
176165
)
177166

167+
178168
# Check uninitialized attributes
179169
assert not hasattr(model_nl, "location_")
180170
assert not hasattr(model_nl, "dipole_moment_")
@@ -183,14 +173,20 @@ def test_nonlinear_magnetic_dipole_bz_inversion():
183173
# Run the inversion
184174
model_nl.fit(coordinates, bz_corrected)
185175

176+
true_moment = np.array([ 8.55050358e-12, -4.84923155e-11, 8.68240888e-12])
177+
186178
# Check that attributes are now set
187179
assert model_nl.location_ is not None
188180
assert model_nl.dipole_moment_ is not None
189181
assert model_nl.r2_ is not None
182+
183+
ang_dist = angular_distance(true_moment, np.array(model_nl.dipole_moment_))
190184

191185
# Assert that results are close to the truth
192-
np.testing.assert_allclose(model_nl.location_[2], -6, atol=0.05)
193-
np.testing.assert_allclose(model_nl.dipole_moment_[2], -10, rtol=0.05)
186+
np.testing.assert_allclose(model_nl.location_[0], 1300, rtol=0.05)
187+
np.testing.assert_allclose(model_nl.location_[1], 1750, rtol=0.05)
188+
np.testing.assert_allclose(model_nl.location_[2], -15, rtol=0.05)
189+
np.testing.assert_allclose(ang_dist, 0.5, rtol=0.05)
194190
np.testing.assert_allclose(model_nl.r2_, 1, rtol=0.05)
195191

196192

@@ -305,6 +301,7 @@ def test_nonlinear_magnetic_moment_gz_jacobian():
305301
np.testing.assert_allclose(np.std(residual), 31.744971328845484, atol=1e-3)
306302

307303

304+
308305
def test_iterative_nonlinear_inversion():
309306
"""
310307
Test the complete iterative nonlinear inversion workflow on synthetic data.
@@ -450,7 +447,6 @@ def test_iterative_nonlinear_inversion():
450447
assert np.all(intensity_misfit < 20)
451448
assert np.all(ang_dist < 5)
452449

453-
454450
def test_nonlinear_inner_loop_no_step_taken():
455451
"""
456452
Test the scenario where inner loop fails to find a step that reduces misfit.

0 commit comments

Comments
 (0)