Skip to content

Commit

Permalink
fix most test formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
akeeste committed Sep 24, 2024
1 parent e860034 commit 3382825
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
31 changes: 16 additions & 15 deletions mhkit/tests/wave/test_resource_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def test_kfromw(self):

expected = self.valdata1[i]["k"]
k = wave.resource.wave_number(f, h, rho)
calculated = k.loc[:, "k"].values
# calculated = k.loc[:, "k"].values
calculated = k
error = ((expected - calculated) ** 2).sum() # SSE

self.assertLess(error, 1e-6)
Expand All @@ -106,7 +107,7 @@ def test_kfromw_one_freq(self):
h = 1e9
w = np.pi * 2 * f # deep water dispersion
expected = w**2 / g
calculated = wave.resource.wave_number(f=f, h=h, g=g).values[0][0]
calculated = wave.resource.wave_number(f=f, h=h, g=g).item()
error = np.abs(expected - calculated)
self.assertLess(error, 1e-6)

Expand Down Expand Up @@ -218,7 +219,7 @@ def test_moments(self):

calculated = wave.resource.frequency_moment(
S, int(m), frequency_bins=f_bins
).iloc[0, 0]
).item()
error = np.abs(expected - calculated) / expected

self.assertLess(error, 0.01)
Expand All @@ -238,7 +239,7 @@ def test_energy_period_to_peak_period(self):
f = np.linspace(1 / (10 * Tp), 3 / Tp, 100)
S = wave.resource.jonswap_spectrum(f, Tp, Hs, g)

Te_calc = wave.resource.energy_period(S).values[0][0]
Te_calc = wave.resource.energy_period(S).item()

error = np.abs(T - Te_calc) / Te_calc
self.assertLess(error, 0.01)
Expand All @@ -259,16 +260,16 @@ def test_metrics(self):
expected = data["metrics"]["Hm0"]
calculated = wave.resource.significant_wave_height(
S, frequency_bins=f_bins
).iloc[0, 0]
)
error = np.abs(expected - calculated) / expected
# print('Hm0', expected, calculated, error)
self.assertLess(error, 0.01)

# Te
expected = data["metrics"]["Te"]
calculated = wave.resource.energy_period(S, frequency_bins=f_bins).iloc[
0, 0
]
calculated = wave.resource.energy_period(
S, frequency_bins=f_bins
).item()
error = np.abs(expected - calculated) / expected
# print('Te', expected, calculated, error)
self.assertLess(error, 0.01)
Expand All @@ -277,7 +278,7 @@ def test_metrics(self):
expected = data["metrics"]["T0"]
calculated = wave.resource.average_zero_crossing_period(
S, frequency_bins=f_bins
).iloc[0, 0]
).item()
error = np.abs(expected - calculated) / expected
# print('T0', expected, calculated, error)
self.assertLess(error, 0.01)
Expand All @@ -289,7 +290,7 @@ def test_metrics(self):
S,
# Tc = Tavg**2
frequency_bins=f_bins,
).iloc[0, 0]
).item()
** 2
)
error = np.abs(expected - calculated) / expected
Expand All @@ -300,14 +301,14 @@ def test_metrics(self):
expected = np.sqrt(data["metrics"]["Tm"])
calculated = wave.resource.average_wave_period(
S, frequency_bins=f_bins
).iloc[0, 0]
).item()
error = np.abs(expected - calculated) / expected
# print('Tm', expected, calculated, error)
self.assertLess(error, 0.01)

# Tp
expected = data["metrics"]["Tp"]
calculated = wave.resource.peak_period(S).iloc[0, 0]
calculated = wave.resource.peak_period(S)
error = np.abs(expected - calculated) / expected
# print('Tp', expected, calculated, error)
self.assertLess(error, 0.001)
Expand All @@ -316,7 +317,7 @@ def test_metrics(self):
expected = data["metrics"]["e"]
calculated = wave.resource.spectral_bandwidth(
S, frequency_bins=f_bins
).iloc[0, 0]
).item()
error = np.abs(expected - calculated) / expected
# print('e', expected, calculated, error)
self.assertLess(error, 0.001)
Expand All @@ -335,14 +336,14 @@ def test_metrics(self):
expected = data["metrics"]["v"]
calculated = wave.resource.spectral_width(
S, frequency_bins=f_bins
).iloc[0, 0]
).item()
error = np.abs(expected - calculated) / expected
self.assertLess(error, 0.01)

if file_i == "MC":
expected = data["metrics"]["v"]
# testing that default uniform frequency bin widths works
calculated = wave.resource.spectral_width(S).iloc[0, 0]
calculated = wave.resource.spectral_width(S).item()
error = np.abs(expected - calculated) / expected
self.assertLess(error, 0.01)

Expand Down
12 changes: 6 additions & 6 deletions mhkit/wave/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ def elevation_spectrum(
eta = _signal.detrend(
eta.dropna(dim=time_dimension), axis=-1, type="linear", bp=0
)
[f, S] = _signal.welch(
[f, wave_spec_measured] = _signal.welch(
eta,
fs=sample_rate,
window=window,
nperseg=nnft,
nfft=nnft,
noverlap=noverlap,
)
# S = (["Frequency"], wave_spec_measured)
# S = S.assign_coords({"Frequency": f})
S = xr.DataArray(data=S, coords=["Frequency"], dims={"Frequency": f})
S = xr.DataArray(
data=wave_spec_measured, dims=["Frequency"], coords={"Frequency": f}
)

if to_pandas:
S = S.to_pandas()
Expand Down Expand Up @@ -1162,10 +1162,10 @@ def func(kk):
if not ier == 1:
raise ValueError("Wave number not found. " + mesg)
k0[mask] = k
k = k0

if isinstance(k0, (pd.Series, xr.DataArray)):
k0.name = "k"
# k = k0.to_dataset()
k.name = "k"

# if to_pandas:
# k = k.to_pandas()
Expand Down

0 comments on commit 3382825

Please sign in to comment.