Skip to content

Commit 7614867

Browse files
committed
Revert "Ensure interpolation values are within range when sampling contours (MHKiT-Software#278)"
This reverts commit 2387a25.
1 parent ae6b715 commit 7614867

File tree

2 files changed

+27
-66
lines changed

2 files changed

+27
-66
lines changed

mhkit/tests/wave/test_contours.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
from os.path import abspath, dirname, join, isfile, normpath, relpath
2-
import unittest
3-
import pickle
4-
import json
5-
import os
6-
2+
from pandas.testing import assert_frame_equal
73
from numpy.testing import assert_allclose
4+
from scipy.interpolate import interp1d
5+
from random import seed, randint
86
import matplotlib.pylab as plt
7+
from datetime import datetime
8+
import xarray.testing as xrt
9+
import mhkit.wave as wave
10+
from io import StringIO
911
import pandas as pd
1012
import numpy as np
11-
12-
import mhkit.wave as wave
13+
import contextlib
14+
import unittest
15+
import netCDF4
16+
import inspect
17+
import pickle
18+
import time
19+
import json
20+
import sys
21+
import os
1322

1423

1524
testdir = dirname(abspath(__file__))
@@ -45,10 +54,6 @@ def setUpClass(self):
4554
self.wdrt_dt = 3600
4655
self.wdrt_period = 50
4756

48-
# `samples_contour`Example data
49-
self.hs_contour = np.array([8.56637939, 9.27612515, 8.70427774])
50-
self.te_contour = np.array([10, 15, 20])
51-
5257
@classmethod
5358
def tearDownClass(self):
5459
pass
@@ -235,28 +240,7 @@ def test_kde_copulas(self):
235240
self.wdrt_copulas['bivariate_KDE_log_x2'])]
236241
self.assertTrue(all(close))
237242

238-
def test_samples_contours_type_validation(self):
239-
with self.assertRaises(TypeError):
240-
wave.contours.samples_contour(
241-
'not an array', self.te_contour, self.hs_contour)
242-
with self.assertRaises(TypeError):
243-
wave.contours.samples_contour(
244-
self.te_contour, 'not an array', self.hs_contour)
245-
with self.assertRaises(TypeError):
246-
wave.contours.samples_contour(
247-
self.te_contour, self.hs_contour, 'not an array')
248-
249-
def test_samples_contours_length_mismatch(self):
250-
with self.assertRaises(ValueError):
251-
wave.contours.samples_contour(
252-
self.te_contour, self.hs_contour, np.array([1, 2]))
253-
254-
def test_samples_contours_range_validation(self):
255-
with self.assertRaises(ValueError):
256-
wave.contours.samples_contour(
257-
np.array([5, 25]), self.te_contour, self.hs_contour)
258-
259-
def test_samples_contours_correct_interpolation(self):
243+
def test_samples_contours(self):
260244
te_samples = np.array([10, 15, 20])
261245
hs_samples_0 = np.array([8.56637939, 9.27612515, 8.70427774])
262246
hs_contour = np.array(self.wdrt_copulas["gaussian_x1"])

mhkit/wave/contours.py

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,51 +1792,28 @@ def samples_contour(t_samples, t_contour, hs_contour):
17921792
raise TypeError(f't_contour must be of type np.ndarray. Got: {type(t_contour)}')
17931793
if not isinstance(hs_contour, np.ndarray):
17941794
raise TypeError(f'hs_contour must be of type np.ndarray. Got: {type(hs_contour)}')
1795-
if len(t_contour) != len(hs_contour):
1796-
raise ValueError(
1797-
"t_contour and hs_contour must be of the same length.")
1798-
if np.any(t_samples < np.min(t_contour)) or np.any(t_samples > np.max(t_contour)):
1799-
raise ValueError(
1800-
"All t_samples must be within the range of t_contour.")
1801-
1802-
18031795

1804-
# Find minimum and maximum energy period values
1796+
# finds minimum and maximum energy period values
18051797
amin = np.argmin(t_contour)
18061798
amax = np.argmax(t_contour)
18071799
aamin = np.min([amin, amax])
18081800
aamax = np.max([amin, amax])
1809-
1810-
# Separate points along the contour into upper & lower half
1801+
# finds points along the contour
18111802
w1 = hs_contour[aamin:aamax]
18121803
w2 = np.concatenate((hs_contour[aamax:], hs_contour[:aamin]))
1813-
1814-
# Get samples min and max
1815-
t_min, t_max = np.min(t_samples), np.max(t_samples)
1816-
1817-
# Choose the half of the contour with the largest wave height
1818-
if np.max(w1) > np.max(w2):
1819-
# Check if the max or min Tp values are within the contour half
1820-
include_aamax = t_max >= t_contour[aamax] or t_min <= t_contour[aamin]
1821-
# Set the x and y values for interpolation
1822-
x1 = t_contour[aamin:aamax + int(include_aamax)]
1823-
y1 = hs_contour[aamin:aamax + int(include_aamax)]
1804+
if (np.max(w1) > np.max(w2)):
1805+
x1 = t_contour[aamin:aamax]
1806+
y1 = hs_contour[aamin:aamax]
18241807
else:
1825-
# Check if the max or min Tp values are within the contour half
1826-
include_aamin = t_max >= t_contour[aamin] or t_min <= t_contour[aamax]
1827-
# Set the x and y values for interpolation
1828-
x1 = np.concatenate(
1829-
(t_contour[aamax:], t_contour[:aamin + int(include_aamin)]))
1830-
y1 = np.concatenate(
1831-
(hs_contour[aamax:], hs_contour[:aamin + int(include_aamin)]))
1832-
1833-
# Sort data based on the max and min Tp values
1808+
x1 = np.concatenate((t_contour[aamax:], t_contour[:aamin]))
1809+
y1 = np.concatenate((hs_contour[aamax:], hs_contour[:aamin]))
1810+
# sorts data based on the max and min energy period values
18341811
ms = np.argsort(x1)
18351812
x = x1[ms]
18361813
y = y1[ms]
1837-
# Interpolation function
1814+
# interpolates the sorted data
18381815
si = interp.interp1d(x, y)
1839-
# Interpolate Tp samples values to get Hs values
1816+
# finds the wave height based on the user specified energy period values
18401817
hs_samples = si(t_samples)
18411818

18421819
return hs_samples

0 commit comments

Comments
 (0)