You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am not sure if this is a bug or actually intended. For some reason the first 70 epochs of my data have 0 r_peaks. This leads to an empty rri if I am not mistaken. Therefore when running hrv_time on one of the epochs it returns the error:
IndexError Traceback (most recent call last)
Cell In[102], line 1
----> 1 nk.hrv_time(epochs["70"])
File ~\anaconda3\Lib\site-packages\neurokit2\hrv\hrv_time.py:164, in hrv_time(peaks, sampling_rate, show, **kwargs)
162 out["SDNN"] = np.nanstd(rri, ddof=1)
163 for i in [1, 2, 5]:
--> 164 out["SDANN" + str(i)] = _sdann(rri, window=i)
165 out["SDNNI" + str(i)] = _sdnni(rri, window=i)
167 # Difference-based
File ~\anaconda3\Lib\site-packages\neurokit2\hrv\hrv_time.py:228, in _sdann(rri, rri_time, window)
226 rri_time = np.nancumsum(rri / 1000)
227 # Convert timestamps to milliseconds and ensure first timestamp is equal to first interval
--> 228 rri_cumsum = (rri_time - rri_time[0]) * 1000 + rri[0]
229 n_windows = int(np.round(rri_cumsum[-1] / window_size))
230 if n_windows < 3:
IndexError: index 0 is out of bounds for axis 0 with size 0
It would be nice if the code just returned NaN or issued a warning that this is what happened.
Also I think there is an issue with sanitizing data. I took my signal, I cleaned it using ecg_clean and then got the peaks=ecg_peaks. Therefore peaks is now in a tuple of df and a dictionary. According to the documentation ths is a valid input for hrv but I get an error. hrv(peaks[0]) does work. After following the code I think i realized what is the problem ( I might be wrong I am very new to this). peaks is a tuple so _hrv_santizie_tuple is called to sanitize them.
def _hrv_sanitize_tuple(peaks, sampling_rate=1000):
# Get sampling rate
info = [i for i in peaks if isinstance(i, dict)]
sampling_rate = info[0]["sampling_rate"]
# Detect actual sampling rate
if len(info) < 1:
peaks, sampling_rate = peaks[0], peaks[1]
# Get peaks
if isinstance(peaks[0], (dict, pd.DataFrame)):
try:
peaks = _hrv_sanitize_dict_or_df(peaks[0])
except NameError:
if isinstance(peaks[1], (dict, pd.DataFrame)):
try:
peaks = _hrv_sanitize_dict_or_df(peaks[1])
except NameError:
peaks = _hrv_sanitize_peaks(peaks[1])
else:
peaks = _hrv_sanitize_peaks(peaks[0])
rri, rri_time, rri_missing = _hrv_get_rri(peaks=peaks, sampling_rate=sampling_rate)
return rri, rri_time, rri_missing, sampling_rate
Therefore here, the input peaks will go into peaks = _hrv_sanitize_dict_or_df(peaks[0])
But hrv_sanitize_dict_df outputs rri and not peaks which leads to an error. Therefore shouldn't it be
def _hrv_sanitize_tuple(peaks, sampling_rate=1000):
# Get sampling rate
info = [i for i in peaks if isinstance(i, dict)]
sampling_rate = info[0]["sampling_rate"]
# Detect actual sampling rate
if len(info) < 1:
peaks, sampling_rate = peaks[0], peaks[1]
# Get peaks
if isinstance(peaks[0], (dict, pd.DataFrame)):
try:
rri, rri_time, rri_missing, sampling_rate = _hrv_sanitize_dict_or_df(peaks[0],sampling_rate=sampling_rate)
return rri, rri_time, rri_missing, sampling_rate
except NameError:
if isinstance(peaks[1], (dict, pd.DataFrame)):
try:
peaks = _hrv_sanitize_dict_or_df(peaks[1])
except NameError:
peaks = _hrv_sanitize_peaks(peaks[1])
else:
peaks = _hrv_sanitize_peaks(peaks[0])
rri, rri_time, rri_missing = _hrv_get_rri(peaks=peaks, sampling_rate=sampling_rate)
return rri, rri_time, rri_missing, sampling_rate
I do not know what are the dependencies but this fixed it for me at least.
Update: I apologize I think i know what happened. I guess you want people to do peaks,info= ecg_peaks and then pass the peaks into hrv. I did peaks=ecg_peaks which led this problem. I guess this is a non issue if you make the documentation more precise.
The text was updated successfully, but these errors were encountered:
Hi 👋 Thanks for reaching out and opening your first issue here! We'll try to come back to you as soon as possible. ❤️
LaskawiecPiotr
changed the title
hrv_time of an epoch with 0 or 1 r_peaks
hrv_time of an epoch with 0 or 1 r_peaks and issue with sanitizing data.
Feb 4, 2025
Hi,
I am not sure if this is a bug or actually intended. For some reason the first 70 epochs of my data have 0 r_peaks. This leads to an empty rri if I am not mistaken. Therefore when running hrv_time on one of the epochs it returns the error:
It would be nice if the code just returned NaN or issued a warning that this is what happened.
Also I think there is an issue with sanitizing data. I took my signal, I cleaned it using ecg_clean and then got the peaks=ecg_peaks. Therefore peaks is now in a tuple of df and a dictionary. According to the documentation ths is a valid input for hrv but I get an error. hrv(peaks[0]) does work. After following the code I think i realized what is the problem ( I might be wrong I am very new to this). peaks is a tuple so _hrv_santizie_tuple is called to sanitize them.
Therefore here, the input peaks will go into peaks = _hrv_sanitize_dict_or_df(peaks[0])
But hrv_sanitize_dict_df outputs rri and not peaks which leads to an error. Therefore shouldn't it be
I do not know what are the dependencies but this fixed it for me at least.
Update: I apologize I think i know what happened. I guess you want people to do peaks,info= ecg_peaks and then pass the peaks into hrv. I did peaks=ecg_peaks which led this problem. I guess this is a non issue if you make the documentation more precise.
The text was updated successfully, but these errors were encountered: