NeuroKit: hrv function: ValueError: Expect x to not have duplicates

When calling nk.hrv on my RRI data, I get

ValueError: Expect x to not have duplicates

The RRI data appears to satisfy the criteria of the HRV function. It looks like so 443, 443, 443, 446, 438, 437,

The complete error log is as follows:


ValueError Traceback (most recent call last) <ipython-input-152-f3330677ee28> in <module>() ----> 1 hrv_welch=nk.hrv_frequency(data,sampling_rate=1000,show=True,psd_method=“welch”)

~/anaconda3/lib/python3.6/site-packages/neurokit2/hrv/hrv_frequency.py in hrv_frequency(peaks, sampling_rate, ulf, vlf, lf, hf, vhf, psd_method, show, silent, normalize, order_criteria, **kwargs) 125 126 # Compute R-R intervals (also referred to as NN) in milliseconds (interpolated at 1000 Hz by default) –> 127 rri, sampling_rate = _hrv_get_rri(peaks, sampling_rate=sampling_rate, interpolate=True, **kwargs) 128 129 frequency_band = [ulf, vlf, lf, hf, vhf]

~/anaconda3/lib/python3.6/site-packages/neurokit2/hrv/hrv_utils.py in _hrv_get_rri(peaks, sampling_rate, interpolate, **kwargs) 26 rri, 27 x_new=np.arange(desired_length), —> 28 **kwargs 29 ) 30 return rri, sampling_rate

~/anaconda3/lib/python3.6/site-packages/neurokit2/signal/signal_interpolate.py in signal_interpolate(x_values, y_values, x_new, method) 75 else: 76 interpolation_function = scipy.interpolate.interp1d( —> 77 x_values, y_values, kind=method, bounds_error=False, fill_value=([y_values[0]], [y_values[-1]]) 78 ) 79

~/anaconda3/lib/python3.6/site-packages/scipy/interpolate/interpolate.py in init(failed resolving arguments) 527 528 self._spline = make_interp_spline(xx, yy, k=order, –> 529 check_finite=False) 530 if rewrite_nan: 531 self._call = self.class._call_nan_spline

~/anaconda3/lib/python3.6/site-packages/scipy/interpolate/_bsplines.py in make_interp_spline(x, y, k, t, bc_type, axis, check_finite) 787 raise ValueError(“Expect x to be a 1-D sorted array_like.”) 788 if np.any(x[1:] == x[:-1]): –> 789 raise ValueError(“Expect x to not have duplicates”) 790 if k < 0: 791 raise ValueError(“Expect non-negative k.”)

ValueError: Expect x to not have duplicates

System Specifications

  • OS: Linux ( 64bit)

  • Python: 3.6.8

  • NeuroKit2: 0.1.3

  • NumPy: 1.18.4

  • Pandas: 1.0.3

  • SciPy: 1.5.4

  • sklearn: 0.22.2.post1

  • matplotlib: 3.1.1

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (12 by maintainers)

Commits related to this issue

Most upvoted comments

that false negatives may occur in the (potentially rare) situation where the RRI input fed is continuously increasing

Yeah that’s a possibility, but then I’d say it’s okay, we still catch most of the cases where RRIs are mistakenly passed as peaks. And if the RRis that are all consecutive, well then too bad, it’s like what if people pass mistakenly provide an RSP signal instead of ECG, we cannot catch all the user-errors either ^^

OR b) throw an error?

I’d say we throw an error “The peaks indices passed to nk.hrv were detected as non-consecutive. You might have passed RR intervals instead of peaks, in which case you need to convert them using …” (suggesting to use the rri -> peaks idx converted function)

Hi @martinfrasch I may be misunderstanding your goal here but just to clarify, hrv functions take peaks data as the default input, whereas the purpose of ecg_peaks() is to locate the R-peaks in the ECG signal using the input of a cleaned ECG signal - so these two functions are qualitatively different 😄 Here is how an example pipeline looks like:

signal = nk.data("bio_resting_5min_100hz")["ECG"] # get ecg signal
cleaned = nk.ecg_clean(signal, sampling_rate=100)  # clean
peaks, info = nk.ecg_peaks(cleaned, sampling_rate=100, correct_artifacts=True) # generate peaks
hrv_indices = nk.hrv(peaks, sampling_rate=100, show=True) # compute hrv

So if you already have peaks data, you won’t need to pass them through ecg_peaks(). Are you trying to fix the peaks data in this case? If you are, you can use signal_fixpeaks() instead. Do refer to our documentation too!

Regarding the suggestion to flag an error if RRIs are fed instead of peak data, my thought was that peaks time series are always increasing in value, but RRIs are not [they may accidentally do so from time to time but not consistently], so if the input does not detect such increase in each case, say between [n+1] - [n], an error would be raised.

Regarding this, would we want to a) throw a warning and then compute them anyway assuming they are rri data, OR b) throw an error? @DominiqueMakowski 🤔 I agree with this, though I was also thinking that false negatives may occur in the (potentially rare) situation where the RRI input fed is continuously increasing (if say, there are not a lot of data points).