Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Squeeze y_values in signal_interpolate #1055

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion neurokit2/signal/signal_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def signal_interpolate(
x_values = np.squeeze(x_values.values)
if isinstance(x_new, pd.Series):
x_new = np.squeeze(x_new.values)
if isinstance(y_values, pd.Series):
y_values = np.squeeze(y_values.values)

if len(x_values) != len(y_values):
raise ValueError("x_values and y_values must be of the same length.")
Expand Down Expand Up @@ -158,7 +160,7 @@ def signal_interpolate(
# scipy.interpolate.PchipInterpolator for constant extrapolation akin to the behavior of
# scipy.interpolate.interp1d with fill_value=([y_values[0]], [y_values[-1]].
fill_value = ([interpolated[first_index]], [interpolated[last_index]])
elif isinstance(fill_value, float) or isinstance(fill_value, int):
elif isinstance(fill_value, (float, int)):
# if only a single integer or float is provided as a fill value, format as a tuple
fill_value = ([fill_value], [fill_value])

Expand Down
13 changes: 12 additions & 1 deletion tests/tests_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,25 @@ def test_signal_filter_with_missing():

def test_signal_interpolate():

# Test with arrays
x_axis = np.linspace(start=10, stop=30, num=10)
signal = np.cos(x_axis)
x_new = np.arange(1000)

interpolated = nk.signal_interpolate(x_axis, signal, x_new=np.arange(1000))
interpolated = nk.signal_interpolate(x_axis, signal, x_new)
assert len(interpolated) == 1000
assert interpolated[0] == signal[0]
assert interpolated[-1] == signal[-1]

# Test with Series
x_axis = pd.Series(x_axis)
signal = pd.Series(signal)
x_new = pd.Series(x_new)

interpolated = nk.signal_interpolate(x_axis, signal, x_new)
assert len(interpolated) == 1000
assert interpolated[0] == signal.iloc[0]
assert interpolated[-1] == signal.iloc[-1]

def test_signal_findpeaks():

Expand Down
Loading