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

hrv_time of an epoch with 0 or 1 r_peaks and issue with sanitizing data. #1068

Open
LaskawiecPiotr opened this issue Feb 4, 2025 · 1 comment

Comments

@LaskawiecPiotr
Copy link

LaskawiecPiotr commented 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:

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.

Copy link

welcome bot commented Feb 4, 2025

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

@LaskawiecPiotr 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
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
@LaskawiecPiotr and others