Skip to content

Commit 08ea901

Browse files
committed
add tests
1 parent b23589a commit 08ea901

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

tests/test_core.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,6 +4156,138 @@ def test_extreme_absmax(self):
41564156

41574157
assert extreme_out == pytest.approx(extreme_expect)
41584158

4159+
def test_reshape(self):
4160+
a = 7
4161+
b = 6
4162+
4163+
yp = np.linspace(0.0, 2.0, 20)
4164+
xp = np.linspace(0.0, 359.0, 10)
4165+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
4166+
spectrum = DirectionalSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
4167+
4168+
y = np.linspace(0.5, 1.0, 20)
4169+
x = np.linspace(5.0, 15.0, 10)
4170+
grid_reshaped = spectrum.reshape(y, x, freq_hz=True, degrees=True)
4171+
4172+
freq_expect = (2.0 * np.pi) * y
4173+
dirs_expect = (np.pi / 180.0) * x
4174+
vals_expect = np.array([[a * x_i + b * y_i for x_i in x] for y_i in y]) * (180.0 / np.pi) / (2.0 * np.pi)
4175+
4176+
freq_out = grid_reshaped._freq
4177+
dirs_out = grid_reshaped._dirs
4178+
vals_out = grid_reshaped._vals
4179+
4180+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
4181+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
4182+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
4183+
4184+
def test_reshape2(self):
4185+
a = 7
4186+
b = 6
4187+
4188+
yp = np.linspace(0.0, 2.0, 20)
4189+
xp = np.linspace(0.0, 359.0, 10)
4190+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
4191+
spectrum = DirectionalSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
4192+
4193+
y = np.linspace(0.5, 1.0, 20)
4194+
x = np.linspace(5.0, 15.0, 10)
4195+
y_ = (2.0 * np.pi) * y
4196+
x_ = (np.pi / 180.0) * x
4197+
grid_reshaped = spectrum.reshape(y_, x_, freq_hz=False, degrees=False)
4198+
4199+
freq_expect = (2.0 * np.pi) * y
4200+
dirs_expect = (np.pi / 180.0) * x
4201+
vals_expect = np.array([[a * x_i + b * y_i for x_i in x] for y_i in y]) * (180.0 / np.pi) / (2.0 * np.pi)
4202+
4203+
freq_out = grid_reshaped._freq
4204+
dirs_out = grid_reshaped._dirs
4205+
vals_out = grid_reshaped._vals
4206+
4207+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
4208+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
4209+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
4210+
4211+
def test_reshape_complex_rectangular(self):
4212+
a_real = 7
4213+
b_real = 6
4214+
a_imag = 3
4215+
b_imag = 9
4216+
4217+
yp = np.linspace(0.0, 2.0, 20)
4218+
xp = np.linspace(0.0, 359.0, 10)
4219+
vp_real = np.array([[a_real * x_i + b_real * y_i for x_i in xp] for y_i in yp])
4220+
vp_imag = np.array([[a_imag * x_i + b_imag * y_i for x_i in xp] for y_i in yp])
4221+
vp = vp_real + 1j * vp_imag
4222+
spectrum = DirectionalSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
4223+
4224+
y = np.linspace(0.5, 1.0, 20)
4225+
x = np.linspace(5.0, 15.0, 10)
4226+
grid_reshaped = spectrum.reshape(
4227+
y, x, freq_hz=True, degrees=True, complex_convert="rectangular"
4228+
)
4229+
4230+
freq_out = grid_reshaped._freq
4231+
dirs_out = grid_reshaped._dirs
4232+
vals_out = grid_reshaped._vals
4233+
4234+
freq_expect = (2.0 * np.pi) * y
4235+
dirs_expect = (np.pi / 180.0) * x
4236+
vals_real_expect = np.array(
4237+
[[a_real * x_i + b_real * y_i for x_i in x] for y_i in y]
4238+
)
4239+
vals_imag_expect = np.array(
4240+
[[a_imag * x_i + b_imag * y_i for x_i in x] for y_i in y]
4241+
)
4242+
vals_expect = (vals_real_expect + 1j * vals_imag_expect) * (180.0 / np.pi) / (2.0 * np.pi)
4243+
4244+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
4245+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
4246+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
4247+
4248+
def test_reshape_complex_polar(self):
4249+
a_amp = 7
4250+
b_amp = 6
4251+
a_phase = 0.01
4252+
b_phase = 0.03
4253+
4254+
yp = np.linspace(0.0, 2.0, 20)
4255+
xp = np.linspace(0.0, 359.0, 10)
4256+
vp_amp = np.array([[a_amp * x_i + b_amp * y_i for x_i in xp] for y_i in yp])
4257+
vp_phase = np.array(
4258+
[[a_phase * x_i + b_phase * y_i for x_i in xp] for y_i in yp]
4259+
)
4260+
vp = vp_amp * (np.cos(vp_phase) + 1j * np.sin(vp_phase))
4261+
spectrum = DirectionalSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
4262+
4263+
y = np.linspace(0.5, 1.0, 20)
4264+
x = np.linspace(5.0, 15.0, 10)
4265+
grid_reshaped = spectrum.reshape(
4266+
y, x, freq_hz=True, degrees=True, complex_convert="polar"
4267+
)
4268+
4269+
freq_out = grid_reshaped._freq
4270+
dirs_out = grid_reshaped._dirs
4271+
vals_out = grid_reshaped._vals
4272+
4273+
freq_expect = (2.0 * np.pi) * y
4274+
dirs_expect = (np.pi / 180.0) * x
4275+
vals_amp_expect = np.array(
4276+
[[a_amp * x_i + b_amp * y_i for x_i in x] for y_i in y]
4277+
) * (180.0 / np.pi) / (2.0 * np.pi)
4278+
x_, y_ = np.meshgrid(x, y, indexing="ij", sparse=True)
4279+
vals_phase_cos_expect = RGI((xp, yp), np.cos(vp_phase).T)((x_, y_)).T
4280+
vals_phase_sin_expect = RGI((xp, yp), np.sin(vp_phase).T)((x_, y_)).T
4281+
4282+
vals_expect = (
4283+
vals_amp_expect
4284+
* (vals_phase_cos_expect + 1j * vals_phase_sin_expect)
4285+
/ np.abs(vals_phase_cos_expect + 1j * vals_phase_sin_expect)
4286+
)
4287+
4288+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
4289+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
4290+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
41594291

41604292
class Test_DirectionalBinSpectrum:
41614293
def test__init___hz_deg(self):

0 commit comments

Comments
 (0)