Skip to content

Commit 082f76f

Browse files
committed
add GridInterpolator tests
1 parent 7ff7c48 commit 082f76f

File tree

2 files changed

+194
-40
lines changed

2 files changed

+194
-40
lines changed

src/waveresponse/_core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _sort(dirs, vals):
142142
return dirs[sorted_args], vals[:, sorted_args]
143143

144144

145-
class _GridInterpolate:
145+
class _GridInterpolator:
146146
def __init__(self, freq, dirs, vals, complex_convert="rectangular", **kwargs):
147147
"""
148148
Interpolation function based on ``scipy.interpolate.RegularGridInterpolator``.
@@ -763,7 +763,7 @@ def interpolate(
763763
self._check_freq(freq)
764764
self._check_dirs(dirs)
765765

766-
interp_fun = _GridInterpolate(
766+
interp_fun = _GridInterpolator(
767767
self._freq,
768768
self._dirs,
769769
self._vals,
@@ -836,7 +836,7 @@ def reshape(
836836
self._check_freq(freq_new)
837837
self._check_dirs(dirs_new)
838838

839-
interp_fun = _GridInterpolate(
839+
interp_fun = _GridInterpolator(
840840
self._freq,
841841
self._dirs,
842842
self._vals,
@@ -1074,7 +1074,7 @@ def reshape(
10741074
self._check_freq(freq_new)
10751075
self._check_dirs(dirs_new)
10761076

1077-
interp_fun = _GridInterpolate(
1077+
interp_fun = _GridInterpolator(
10781078
freq_new,
10791079
dirs_new,
10801080
self._vals,
@@ -1732,7 +1732,7 @@ def reshape(
17321732
self._check_freq(freq_new)
17331733
self._check_dirs(dirs_new)
17341734

1735-
interp_fun = _GridInterpolate(
1735+
interp_fun = _GridInterpolator(
17361736
self._freq,
17371737
self._dirs,
17381738
self._vals,
@@ -1920,7 +1920,7 @@ def reshape(
19201920

19211921
self._check_freq(freq_new)
19221922

1923-
interp_fun = _GridInterpolate(
1923+
interp_fun = _GridInterpolator(
19241924
self._freq,
19251925
self._dirs,
19261926
self._vals,

tests/test_core.py

Lines changed: 188 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
calculate_response,
2222
mirror,
2323
)
24-
from waveresponse._core import _check_foldable, _check_is_similar
24+
from waveresponse._core import _check_foldable, _check_is_similar, _GridInterpolator
2525

2626
TEST_PATH = Path(__file__).parent
2727

@@ -37,22 +37,6 @@ def freq_dirs():
3737
return freq, dirs
3838

3939

40-
# @pytest.fixture
41-
# def base_grid(freq_dirs):
42-
# freq, dirs = freq_dirs
43-
# vals = np.random.random((len(freq), len(dirs)))
44-
# grid = _BaseGrid(
45-
# freq,
46-
# dirs,
47-
# vals,
48-
# freq_hz=True,
49-
# degrees=True,
50-
# clockwise=True,
51-
# waves_coming_from=True,
52-
# )
53-
# return grid
54-
55-
5640
@pytest.fixture
5741
def grid(freq_dirs):
5842
freq, dirs = freq_dirs
@@ -69,22 +53,6 @@ def grid(freq_dirs):
6953
return grid
7054

7155

72-
# @pytest.fixture
73-
# def bingrid(freq_dirs):
74-
# freq, dirs = freq_dirs
75-
# vals = np.random.random((len(freq), len(dirs)))
76-
# grid = BinGrid(
77-
# freq,
78-
# dirs,
79-
# vals,
80-
# freq_hz=True,
81-
# degrees=True,
82-
# clockwise=True,
83-
# waves_coming_from=True,
84-
# )
85-
# return grid
86-
87-
8856
@pytest.fixture
8957
def rao(freq_dirs):
9058
freq, dirs = freq_dirs
@@ -764,7 +732,193 @@ def test_dirs_empty(self):
764732
_check_foldable(np.array([]), degrees=True, sym_plane="xz")
765733

766734

767-
class Test__Grid:
735+
class Test__GridInterpolator:
736+
def test_interpolate(self):
737+
a = 7
738+
b = 6
739+
740+
yp = np.linspace(0.0, 2.0, 20)
741+
xp = np.linspace(0.0, 359.0, 10)
742+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
743+
744+
y = np.linspace(0.5, 1.0, 20)
745+
x = np.linspace(5.0, 15.0, 10)
746+
vals_expect = np.array([[a * x_i + b * y_i for x_i in x] for y_i in y])
747+
748+
interp_fun = _GridInterpolator(
749+
yp * 2.*np.pi,
750+
np.radians(xp),
751+
vp
752+
)
753+
vals_out = interp_fun(y * 2. * np.pi, np.radians(x))
754+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
755+
756+
def test_interpolate_single_coordinate(self):
757+
a = 7
758+
b = 6
759+
760+
yp = np.linspace(0.0, 2.0, 20)
761+
xp = np.linspace(0.0, 359.0, 10)
762+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
763+
764+
interp_fun = _GridInterpolator(
765+
yp * 2.*np.pi,
766+
np.radians(xp),
767+
vp
768+
)
769+
vals_out = interp_fun(1.8 * 2. * np.pi, np.radians(12.1))
770+
vals_expect = np.array(a * 12.1 + b * 1.8)
771+
772+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
773+
774+
def test_interpolate_fill_value(self):
775+
freq = np.array([0, 1, 2])
776+
dirs = np.array([0, 90, 180, 270])
777+
vals = np.array(
778+
[
779+
[1, 2, 3, 4],
780+
[1, 2, 3, 4],
781+
[1, 2, 3, 4],
782+
]
783+
)
784+
interp_fun = _GridInterpolator(
785+
freq * 2.*np.pi,
786+
np.radians(dirs),
787+
vals,
788+
fill_value=0.0,
789+
bounds_error=False,
790+
)
791+
vals_out = interp_fun(np.array([10, 20]) * 2. * np.pi, np.radians([0, 90]))
792+
793+
vals_expect = np.array(
794+
[
795+
[0.0, 0.0],
796+
[0.0, 0.0],
797+
]
798+
)
799+
800+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
801+
802+
def test_interpolate_fill_value_None(self):
803+
freq = np.array([0, 1, 2])
804+
dirs = np.array([0, 90, 180, 270])
805+
vals = np.array(
806+
[
807+
[1, 2, 3, 4],
808+
[1, 2, 3, 4],
809+
[1, 2, 3, 4],
810+
]
811+
)
812+
interp_fun = _GridInterpolator(
813+
freq * 2.*np.pi,
814+
np.radians(dirs),
815+
vals,
816+
fill_value=None,
817+
bounds_error=False,
818+
)
819+
vals_out = interp_fun(np.array([10, 20]) * 2. * np.pi, np.radians([0, 90]))
820+
821+
vals_expect = np.array(
822+
[
823+
[1.0, 2.0],
824+
[1.0, 2.0],
825+
]
826+
)
827+
828+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
829+
830+
def test_interpolate_complex_rectangular(self):
831+
a_real = 7
832+
b_real = 6
833+
a_imag = 3
834+
b_imag = 9
835+
836+
yp = np.linspace(0.0, 2.0, 20)
837+
xp = np.linspace(0.0, 359.0, 10)
838+
vp_real = np.array([[a_real * x_i + b_real * y_i for x_i in xp] for y_i in yp])
839+
vp_imag = np.array([[a_imag * x_i + b_imag * y_i for x_i in xp] for y_i in yp])
840+
vp = vp_real + 1j * vp_imag
841+
842+
y = np.linspace(0.5, 1.0, 20)
843+
x = np.linspace(5.0, 15.0, 10)
844+
vals_real_expect = np.array(
845+
[[a_real * x_i + b_real * y_i for x_i in x] for y_i in y]
846+
)
847+
vals_imag_expect = np.array(
848+
[[a_imag * x_i + b_imag * y_i for x_i in x] for y_i in y]
849+
)
850+
vals_expect = vals_real_expect + 1j * vals_imag_expect
851+
852+
interp_fun = _GridInterpolator(
853+
yp * 2.*np.pi,
854+
np.radians(xp),
855+
vp,
856+
complex_convert="rectangular"
857+
)
858+
vals_out = interp_fun(y * 2. * np.pi, np.radians(x))
859+
860+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
861+
862+
def test_interpolate_complex_polar(self):
863+
a_amp = 7
864+
b_amp = 6
865+
a_phase = 0.01
866+
b_phase = 0.03
867+
868+
yp = np.linspace(0.0, 2.0, 20)
869+
xp = np.linspace(0.0, 359.0, 10)
870+
vp_amp = np.array([[a_amp * x_i + b_amp * y_i for x_i in xp] for y_i in yp])
871+
vp_phase = np.array(
872+
[[a_phase * x_i + b_phase * y_i for x_i in xp] for y_i in yp]
873+
)
874+
vp = vp_amp * (np.cos(vp_phase) + 1j * np.sin(vp_phase))
875+
876+
y = np.linspace(0.0, 2.0, 200)
877+
x = np.linspace(0.0, 359.0, 100)
878+
vals_amp_expect = np.array(
879+
[[a_amp * x_i + b_amp * y_i for x_i in x] for y_i in y]
880+
)
881+
x_, y_ = np.meshgrid(x, y, indexing="ij", sparse=True)
882+
vals_phase_cos_expect = RGI((xp, yp), np.cos(vp_phase).T)((x_, y_)).T
883+
vals_phase_sin_expect = RGI((xp, yp), np.sin(vp_phase).T)((x_, y_)).T
884+
885+
vals_expect = (
886+
vals_amp_expect
887+
* (vals_phase_cos_expect + 1j * vals_phase_sin_expect)
888+
/ np.abs(vals_phase_cos_expect + 1j * vals_phase_sin_expect)
889+
)
890+
891+
interp_fun = _GridInterpolator(
892+
yp * 2.*np.pi,
893+
np.radians(xp),
894+
vp,
895+
complex_convert="polar"
896+
)
897+
vals_out = interp_fun(y * 2. * np.pi, np.radians(x))
898+
899+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
900+
901+
def test_interpolate_raises_outside_bound(self):
902+
freq = np.linspace(0, 1.0, 10)
903+
dirs = np.linspace(0, 360.0, 15, endpoint=False)
904+
vals = np.zeros((10, 15))
905+
interp_fun = _GridInterpolator(
906+
freq * 2.*np.pi,
907+
np.radians(dirs),
908+
vals,
909+
bounds_error=True,
910+
)
911+
912+
with pytest.raises(ValueError):
913+
interp_fun(np.array([0, 0.5]) * 2. * np.pi, np.radians([0, 1, 2, 400]))
914+
915+
with pytest.raises(ValueError):
916+
interp_fun(np.array([0, 2.]) * 2. * np.pi, np.radians([0, 1, 2, 100]))
917+
918+
919+
920+
921+
class Test_Grid:
768922
def test__init__(self):
769923
freq = np.linspace(0, 1.0, 10)
770924
dirs = np.linspace(0, 360.0, 15, endpoint=False)

0 commit comments

Comments
 (0)