Skip to content

Commit 53dc3bd

Browse files
committed
Add tests for theta_map function in test_transformations.py
Includes: - Validation against synthetic model using finite differences and FFT - Input error handling for 1D, 3D, and NaN-containing grids - Integration of theta_map import for test discovery
1 parent 0bb4a5b commit 53dc3bd

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

harmonica/tests/test_transformations.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
gaussian_lowpass,
2727
reduction_to_pole,
2828
tilt_angle,
29+
theta_map,
2930
total_gradient_amplitude,
3031
total_horizontal_gradient,
3132
upward_continuation,
@@ -737,6 +738,70 @@ def test_invalid_grid_with_nans(self, sample_potential):
737738
with pytest.raises(ValueError, match="Found nan"):
738739
tilt_angle(sample_potential)
739740

741+
class TestThetaMap:
742+
"""
743+
Test theta_map function
744+
"""
745+
746+
def test_against_synthetic(
747+
self, sample_potential, sample_g_n, sample_g_e, sample_g_z
748+
):
749+
"""
750+
Test theta_map function against the synthetic model
751+
"""
752+
pad_width = {
753+
"easting": sample_potential.easting.size // 3,
754+
"northing": sample_potential.northing.size // 3,
755+
}
756+
potential_padded = xrft.pad(
757+
sample_potential.drop_vars("upward"),
758+
pad_width=pad_width,
759+
)
760+
theta_grid = theta_map(potential_padded)
761+
theta_grid = xrft.unpad(theta_grid, pad_width)
762+
763+
trim = 6
764+
theta_grid = theta_grid[trim:-trim, trim:-trim]
765+
g_e = sample_g_e[trim:-trim, trim:-trim] * 1e-5 # SI units
766+
g_n = sample_g_n[trim:-trim, trim:-trim] * 1e-5
767+
g_z = sample_g_z[trim:-trim, trim:-trim] * 1e-5
768+
g_thdr = np.sqrt(g_e**2 + g_n**2)
769+
g_total = np.sqrt(g_thdr**2 + g_z**2)
770+
g_theta = np.arctan2(g_thdr, g_total)
771+
rms = root_mean_square_error(theta_grid, g_theta)
772+
assert rms / np.abs(g_theta).max() < 0.1
773+
774+
def test_invalid_grid_single_dimension(self):
775+
"""
776+
Check if theta_map raises error on grid with single dimension
777+
"""
778+
x = np.linspace(0, 10, 11)
779+
y = x**2
780+
grid = xr.DataArray(y, coords={"x": x}, dims=("x",))
781+
with pytest.raises(ValueError, match="Invalid grid with 1 dimensions."):
782+
theta_map(grid)
783+
784+
def test_invalid_grid_three_dimensions(self):
785+
"""
786+
Check if theta_map raises error on grid with three dimensions
787+
"""
788+
x = np.linspace(0, 10, 11)
789+
y = np.linspace(-4, 4, 9)
790+
z = np.linspace(20, 30, 5)
791+
xx, yy, zz = np.meshgrid(x, y, z)
792+
data = xx + yy + zz
793+
grid = xr.DataArray(data, coords={"x": x, "y": y, "z": z}, dims=("y", "x", "z"))
794+
with pytest.raises(ValueError, match="Invalid grid with 3 dimensions."):
795+
theta_map(grid)
796+
797+
def test_invalid_grid_with_nans(self, sample_potential):
798+
"""
799+
Check if theta_map raises error if grid contains nans
800+
"""
801+
sample_potential.values[0, 0] = np.nan
802+
with pytest.raises(ValueError, match="Found nan"):
803+
theta_map(sample_potential)
804+
740805

741806
class Testfilter:
742807
"""

0 commit comments

Comments
 (0)