neuropsychology / NeuroKit

NeuroKit2: The Python Toolbox for Neurophysiological Signal Processing
https://neuropsychology.github.io/NeuroKit
MIT License
1.58k stars 420 forks source link

Continuous HRV RSA Interpolation ValueError #660

Closed alexk101 closed 1 year ago

alexk101 commented 2 years ago

When calculating hrv_rsa with the continuous parameter set to True, a ValueError related to the interpolation of the signal is raised.

To Reproduce

rate = 1000
rsp, _ = nk.rsp_process(rsp_good, sampling_rate=rate)
ecg, info = nk.ecg_process(ecg_good, sampling_rate=rate)
rsa_good = nk.hrv_rsa(ecg, rsp, info, sampling_rate=rate, continuous=True)

Expected behaviour

The method should correctly compute the continuous RSA signal when provided with valid ecg and rsp signals.

System Specifications

The dataset used can be provided if necessary.

Associated Stacktrace

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [17], in <cell line: 6>()
      4 rsp, _ = nk.rsp_process(rsp_good, sampling_rate=rate)
      5 ecg, info = nk.ecg_process(ecg_good, sampling_rate=rate)
----> 6 rsa_good = nk.hrv_rsa(ecg, rsp, info, sampling_rate=1000, continuous=True)
      8 print(rsa_good)

File ~/.miniconda3/envs/psychoviz/lib/python3.9/site-packages/neurokit2/hrv/hrv_rsa.py:170, in hrv_rsa(ecg_signals, rsp_signals, rpeaks, sampling_rate, continuous, window, window_number)
    162     warn(
    163         "Couldn't find rsp cycles onsets and centers. Check your RSP signal.",
    164         category=NeuroKitWarning,
    165     )
    167 # Methods ------------------------
    168 
    169 # Peak-to-Trough
--> 170 rsa_p2t = _hrv_rsa_p2t(
    171     rsp_onsets,
    172     rpeaks,
    173     sampling_rate,
    174     continuous=continuous,
    175     ecg_period=ecg_period,
    176     rsp_peaks=rsp_peaks,
    177 )
    178 # Porges-Bohrer
    179 rsa_pb = _hrv_rsa_pb(ecg_period, sampling_rate, continuous=continuous)

File ~/.miniconda3/envs/psychoviz/lib/python3.9/site-packages/neurokit2/hrv/hrv_rsa.py:245, in _hrv_rsa_p2t(rsp_onsets, rpeaks, sampling_rate, continuous, ecg_period, rsp_peaks)
    243     rsa["RSA_P2T_NoRSA"] = len(pd.Series(rsa_values).index[pd.Series(rsa_values).isnull()])
    244 else:
--> 245     rsa = signal_interpolate(
    246         x_values=rsp_peaks[~np.isnan(rsa_values)],
    247         y_values=rsa_values[~np.isnan(rsa_values)],
    248         x_new=np.arange(len(ecg_period)),
    249     )
    251 return rsa

File ~/.miniconda3/envs/psychoviz/lib/python3.9/site-packages/neurokit2/signal/signal_interpolate.py:88, in signal_interpolate(x_values, y_values, x_new, method)
     84     interpolation_function = scipy.interpolate.PchipInterpolator(
     85         x_values, y_values, extrapolate=True
     86     )
     87 else:
---> 88     interpolation_function = scipy.interpolate.interp1d(
     89         x_values,
     90         y_values,
     91         kind=method,
     92         bounds_error=False,
     93         fill_value=([y_values[0]], [y_values[-1]]),
     94     )
     96 if isinstance(x_new, int):
     97     x_new = np.linspace(x_values[0], x_values[-1], x_new)

File ~/.miniconda3/envs/psychoviz/lib/python3.9/site-packages/scipy/interpolate/_interpolate.py:564, in interp1d.__init__(***failed resolving arguments***)
    561         yy = np.ones_like(self._y)
    562         rewrite_nan = True
--> 564 self._spline = make_interp_spline(xx, yy, k=order,
    565                                   check_finite=False)
    566 if rewrite_nan:
    567     self._call = self.__class__._call_nan_spline

File ~/.miniconda3/envs/psychoviz/lib/python3.9/site-packages/scipy/interpolate/_bsplines.py:1326, in make_interp_spline(x, y, k, t, bc_type, axis, check_finite)
   1323 nt = t.size - k - 1
   1325 if nt - n != nleft + nright:
-> 1326     raise ValueError("The number of derivatives at boundaries does not "
   1327                      "match: expected %s, got %s+%s" % (nt-n, nleft, nright))
   1329 # set up the LHS: the collocation matrix + derivatives at boundaries
   1330 kl = ku = k

ValueError: The number of derivatives at boundaries does not match: expected 2, got 0+0
welcome[bot] commented 2 years ago

Hi 👋 Thanks for reaching out and opening your first issue here! We'll try to come back to you as soon as possible. ❤️ kenobi

DominiqueMakowski commented 2 years ago

Hi sorry for the late answer, would you have a reproducible example?

alexk101 commented 2 years ago

I will have get back to you once I find the problem again, as it has been a while since I last looked at the code.

stale[bot] commented 1 year ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.