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

Error raised by rsp_process() with short input signal #740

Closed
danibene opened this issue Oct 31, 2022 · 1 comment
Closed

Error raised by rsp_process() with short input signal #740

danibene opened this issue Oct 31, 2022 · 1 comment

Comments

@danibene
Copy link
Collaborator

Describe the bug
Given a short respiration signal in which only one peak is detected, a ValueError is raised by rsp_process() when interpolating the amplitude to match the signal length (since there is only one amplitude value corresponding to the single peak detected).

error log

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

[<ipython-input-4-b24b2adbbe1f>](https://localhost:8080/#) in <module>
----> 1 nk.rsp_process(rsp, sampling_rate=sampling_rate)

[/usr/local/lib/python3.7/dist-packages/neurokit2/rsp/rsp_process.py](https://localhost:8080/#) in rsp_process(rsp_signal, sampling_rate, method)
     80     # Get additional parameters
     81     phase = rsp_phase(peak_signal, desired_length=len(rsp_signal))
---> 82     amplitude = rsp_amplitude(rsp_cleaned, peak_signal)
     83     rate = signal_rate(info["RSP_Troughs"], sampling_rate=sampling_rate, desired_length=len(rsp_signal))
     84 

[/usr/local/lib/python3.7/dist-packages/neurokit2/rsp/rsp_amplitude.py](https://localhost:8080/#) in rsp_amplitude(rsp_cleaned, peaks, troughs, interpolation_method)
     75     # Interpolate amplitude to length of rsp_cleaned.
     76     amplitude = signal_interpolate(
---> 77         peaks, amplitude, x_new=np.arange(len(rsp_cleaned)), method=interpolation_method
     78     )
     79 

[/usr/local/lib/python3.7/dist-packages/neurokit2/signal/signal_interpolate.py](https://localhost:8080/#) in signal_interpolate(x_values, y_values, x_new, method, fill_value)
     95     if method == "monotone_cubic":
     96         interpolation_function = scipy.interpolate.PchipInterpolator(
---> 97             x_values, y_values, extrapolate=True
     98         )
     99     else:

[/usr/local/lib/python3.7/dist-packages/scipy/interpolate/_cubic.py](https://localhost:8080/#) in __init__(self, x, y, axis, extrapolate)
    230     """
    231     def __init__(self, x, y, axis=0, extrapolate=None):
--> 232         x, _, y, axis, _ = prepare_input(x, y, axis)
    233         xp = x.reshape((x.shape[0],) + (1,)*(y.ndim-1))
    234         dk = self._find_derivatives(xp, y)

[/usr/local/lib/python3.7/dist-packages/scipy/interpolate/_cubic.py](https://localhost:8080/#) in prepare_input(x, y, axis, dydx)
     44         raise ValueError("`x` must be 1-dimensional.")
     45     if x.shape[0] < 2:
---> 46         raise ValueError("`x` must contain at least 2 elements.")
     47     if x.shape[0] != y.shape[axis]:
     48         raise ValueError("The length of `y` along `axis`={0} doesn't "

ValueError: `x` must contain at least 2 elements.

To Reproduce

import neurokit2 as nk
sampling_rate = 100
duration = 10
rsp = nk.rsp_simulate(sampling_rate=sampling_rate, duration=duration)
nk.rsp_process(rsp, sampling_rate=sampling_rate)

System Specifications

- OS: Linux ( 64bit) 
- Python: 3.7.15 
- NeuroKit2: 0.2.1 

- NumPy: 1.21.6 
- Pandas: 1.3.5 
- SciPy: 1.7.3 
- sklearn: 1.0.2 
- matplotlib: 3.2.2

Expected behaviour
I assume that rather than raising an error, we would like to return the single amplitude value, just repeated so that it matched the signal length.

Suggestion

Modify the following lines in rsp_amplitude() such that if there is only one peak detected, the amplitude value will be repeated:
https://github.com/neuropsychology/NeuroKit/blob/dev/neurokit2/rsp/rsp_amplitude.py#L75-L78

if len(peaks) == 1:
    amplitude = amplitude * np.ones(rsp_cleaned.shape)
else:
    amplitude = signal_interpolate(
        peaks, amplitude, x_new=np.arange(len(rsp_cleaned)), method=interpolation_method
    )

Let me know your thoughts & I'll submit a PR!

@DominiqueMakowski
Copy link
Member

I missed this. Yes that's good! I'll add it in #746

DominiqueMakowski added a commit that referenced this issue Nov 14, 2022
Co-Authored-By: danibene <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants