Skip to content

Commit 7bab614

Browse files
bug: fix dirm degrees default and wrong scaling when .reshape is called (#85)
1 parent a4fce9c commit 7bab614

File tree

2 files changed

+279
-9
lines changed

2 files changed

+279
-9
lines changed

src/waveresponse/_core.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,12 +1818,6 @@ def reshape(
18181818

18191819
vals_new = interp_fun(freq_new, dirs_new)
18201820

1821-
if freq_hz:
1822-
vals_new *= 2.0 * np.pi
1823-
1824-
if degrees:
1825-
vals_new *= np.pi / 180.0
1826-
18271821
new = self.copy()
18281822
new._freq, new._dirs, new._vals = freq_new, dirs_new, vals_new
18291823
return new
@@ -2003,9 +1997,6 @@ def reshape(
20031997

20041998
vals_new = interp_fun(freq_new, self._dirs)
20051999

2006-
if freq_hz:
2007-
vals_new *= 2.0 * np.pi
2008-
20092000
new = self.copy()
20102001
new._freq, new._vals = freq_new, vals_new
20112002
return new
@@ -2149,6 +2140,9 @@ def dirm(self, degrees=None):
21492140
during instantiation.
21502141
"""
21512142

2143+
if degrees is None:
2144+
degrees = self._degrees
2145+
21522146
dp, sp = self.spectrum1d(axis=0, degrees=False)
21532147

21542148
d = self._full_range_dir(dp)
@@ -2297,6 +2291,9 @@ def dirm(self, degrees=None):
22972291
during instantiation.
22982292
"""
22992293

2294+
if degrees is None:
2295+
degrees = self._degrees
2296+
23002297
dirm = self._mean_direction(*self.spectrum1d(axis=0, degrees=False))
23012298

23022299
if degrees:

tests/test_core.py

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

41604305
class Test_DirectionalBinSpectrum:
41614306
def test__init___hz_deg(self):
@@ -4956,6 +5101,134 @@ def test_extreme_absmax(self):
49565101

49575102
assert extreme_out == pytest.approx(extreme_expect)
49585103

5104+
def test_reshape(self):
5105+
a = 7
5106+
b = 6
5107+
5108+
yp = np.linspace(0.0, 2.0, 20)
5109+
xp = np.linspace(0.0, 359.0, 10)
5110+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
5111+
spectrum = DirectionalBinSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
5112+
5113+
y = np.linspace(0.5, 1.0, 20)
5114+
grid_reshaped = spectrum.reshape(y, freq_hz=True)
5115+
5116+
freq_expect = (2.0 * np.pi) * y
5117+
dirs_expect = (np.pi / 180.0) * xp
5118+
vals_expect = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in y]) / (
5119+
2.0 * np.pi
5120+
)
5121+
5122+
freq_out = grid_reshaped._freq
5123+
dirs_out = grid_reshaped._dirs
5124+
vals_out = grid_reshaped._vals
5125+
5126+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
5127+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
5128+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
5129+
5130+
def test_reshape2(self):
5131+
a = 7
5132+
b = 6
5133+
5134+
yp = np.linspace(0.0, 2.0, 20)
5135+
xp = np.linspace(0.0, 359.0, 10)
5136+
vp = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in yp])
5137+
spectrum = DirectionalBinSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
5138+
5139+
y = np.linspace(0.5, 1.0, 20)
5140+
y_ = (2.0 * np.pi) * y
5141+
grid_reshaped = spectrum.reshape(y_, freq_hz=False)
5142+
5143+
freq_expect = (2.0 * np.pi) * y
5144+
dirs_expect = (np.pi / 180.0) * xp
5145+
vals_expect = np.array([[a * x_i + b * y_i for x_i in xp] for y_i in y]) / (
5146+
2.0 * np.pi
5147+
)
5148+
5149+
freq_out = grid_reshaped._freq
5150+
dirs_out = grid_reshaped._dirs
5151+
vals_out = grid_reshaped._vals
5152+
5153+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
5154+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
5155+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
5156+
5157+
def test_reshape_complex_rectangular(self):
5158+
a_real = 7
5159+
b_real = 6
5160+
a_imag = 3
5161+
b_imag = 9
5162+
5163+
yp = np.linspace(0.0, 2.0, 20)
5164+
xp = np.linspace(0.0, 359.0, 10)
5165+
vp_real = np.array([[a_real * x_i + b_real * y_i for x_i in xp] for y_i in yp])
5166+
vp_imag = np.array([[a_imag * x_i + b_imag * y_i for x_i in xp] for y_i in yp])
5167+
vp = vp_real + 1j * vp_imag
5168+
spectrum = DirectionalBinSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
5169+
5170+
y = np.linspace(0.5, 1.0, 20)
5171+
grid_reshaped = spectrum.reshape(y, freq_hz=True, complex_convert="rectangular")
5172+
5173+
freq_out = grid_reshaped._freq
5174+
dirs_out = grid_reshaped._dirs
5175+
vals_out = grid_reshaped._vals
5176+
5177+
freq_expect = (2.0 * np.pi) * y
5178+
dirs_expect = (np.pi / 180.0) * xp
5179+
vals_real_expect = np.array(
5180+
[[a_real * x_i + b_real * y_i for x_i in xp] for y_i in y]
5181+
)
5182+
vals_imag_expect = np.array(
5183+
[[a_imag * x_i + b_imag * y_i for x_i in xp] for y_i in y]
5184+
)
5185+
vals_expect = (vals_real_expect + 1j * vals_imag_expect) / (2.0 * np.pi)
5186+
5187+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
5188+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
5189+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
5190+
5191+
def test_reshape_complex_polar(self):
5192+
a_amp = 7
5193+
b_amp = 6
5194+
a_phase = 0.01
5195+
b_phase = 0.03
5196+
5197+
yp = np.linspace(0.0, 2.0, 20)
5198+
xp = np.linspace(0.0, 359.0, 10)
5199+
vp_amp = np.array([[a_amp * x_i + b_amp * y_i for x_i in xp] for y_i in yp])
5200+
vp_phase = np.array(
5201+
[[a_phase * x_i + b_phase * y_i for x_i in xp] for y_i in yp]
5202+
)
5203+
vp = vp_amp * (np.cos(vp_phase) + 1j * np.sin(vp_phase))
5204+
spectrum = DirectionalBinSpectrum(yp, xp, vp, freq_hz=True, degrees=True)
5205+
5206+
y = np.linspace(0.5, 1.0, 20)
5207+
grid_reshaped = spectrum.reshape(y, freq_hz=True, complex_convert="polar")
5208+
5209+
freq_out = grid_reshaped._freq
5210+
dirs_out = grid_reshaped._dirs
5211+
vals_out = grid_reshaped._vals
5212+
5213+
freq_expect = (2.0 * np.pi) * y
5214+
dirs_expect = (np.pi / 180.0) * xp
5215+
vals_amp_expect = np.array(
5216+
[[a_amp * x_i + b_amp * y_i for x_i in xp] for y_i in y]
5217+
) / (2.0 * np.pi)
5218+
x_, y_ = np.meshgrid(xp, y, indexing="ij", sparse=True)
5219+
vals_phase_cos_expect = RGI((xp, yp), np.cos(vp_phase).T)((x_, y_)).T
5220+
vals_phase_sin_expect = RGI((xp, yp), np.sin(vp_phase).T)((x_, y_)).T
5221+
5222+
vals_expect = (
5223+
vals_amp_expect
5224+
* (vals_phase_cos_expect + 1j * vals_phase_sin_expect)
5225+
/ np.abs(vals_phase_cos_expect + 1j * vals_phase_sin_expect)
5226+
)
5227+
5228+
np.testing.assert_array_almost_equal(freq_out, freq_expect)
5229+
np.testing.assert_array_almost_equal(dirs_out, dirs_expect)
5230+
np.testing.assert_array_almost_equal(vals_out, vals_expect)
5231+
49595232

49605233
class Test_WaveSpectrum:
49615234
def test__init__(self):

0 commit comments

Comments
 (0)