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

Issue when computing rsp_rate from troughs as np.array or list #1067

Open
me-pic opened this issue Jan 31, 2025 · 6 comments
Open

Issue when computing rsp_rate from troughs as np.array or list #1067

me-pic opened this issue Jan 31, 2025 · 6 comments

Comments

@me-pic
Copy link

me-pic commented Jan 31, 2025

Describe the bug
A description of what the bug is. Try coping the last steps that caused the error. Don't forget to also copy if possible the error log (the message related to the error).

In the description of the rsp_rate function, it is mentioned that troughs can either be list, np.array, pd.Series, pd.DataFrame. However, troughs are extracted in the code as followed:

troughs["RSP_Troughs"]

Hence, an error is thrown if a list or an array is specified when calling the function.

To Reproduce
Example steps to reproduce the behaviour. For instance:

import neurokit2 as nk

rsp = nk.rsp_simulate(duration=60, sampling_rate=1000)
peak_signal, info = nk.rsp_peaks(rsp, sampling_rate=1000)
rate = nk.rsp_rate(rsp, info['RSP_Troughs'], sampling_rate=1000)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[36], line 3
      1 rsp = nk.rsp_simulate(duration=60, sampling_rate=1000)
      2 peak_signal, info = nk.rsp_peaks(rsp, sampling_rate=1000)
----> 3 rate = nk.rsp_rate(rsp, info['RSP_Troughs'], sampling_rate=1000)

File ~/.virtualenvs/biosignal2/lib/python3.10/site-packages/neurokit2/rsp/rsp_rate.py:74, in rsp_rate(rsp_cleaned, troughs, sampling_rate, window, hop_size, method, peak_method, interpolation_method)
     71     if troughs is None:
     72         _, troughs = rsp_peaks(rsp_cleaned, sampling_rate=sampling_rate, method=peak_method)
     73     rate = signal_rate(
---> 74         troughs["RSP_Troughs"],
     75         sampling_rate=sampling_rate,
     76         desired_length=len(rsp_cleaned),
     77         interpolation_method=interpolation_method,
     78     )
     80 elif method.lower() in ["cross-correlation", "xcorr"]:
     81     rate = _rsp_rate_xcorr(
     82         rsp_cleaned,
     83         sampling_rate=sampling_rate,
   (...)
     86         interpolation_method=interpolation_method,
     87     )

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

System Specifications

It's important that you give us some information about the system you are using. For that you can run:

import neurokit2 as nk
nk.version()
- OS: Linux (ELF 64bit) 
- Python: 3.10.12 
- NeuroKit2: 0.2.10 

- NumPy: 2.2.2 
- Pandas: 2.2.3 
- SciPy: 1.15.0 
- sklearn: 1.6.1 
- matplotlib: 3.10.0
Copy link

welcome bot commented Jan 31, 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

@DerAndereJohannes
Copy link
Collaborator

Good catch! Thank you for reporting this. It seems as if this is a documentation error, though perhaps we should change the code to better reflect the actual documentation and to therefore allow for your use of the function to work. Right now it seems to assume a DataFrame and directly uses the dictionary key. Here is the quick fix for now:

# Remove dictionary lookup
# rate = nk.rsp_rate(rsp, info['RSP_Troughs'], sampling_rate=1000)
rate = nk.rsp_rate(rsp, info, sampling_rate=1000)

@me-pic
Copy link
Author

me-pic commented Jan 31, 2025

@DerAndereJohannes Thank you for your quick feedback ! Should I open a PR ? Also if your proposed change is implemented, should the doc be updated to remove pd.DataFrame as a valid format ? Just making sure that there is no confusion, and that it will not be possible in that case to directly pass, for example, the output of rsp_process function in rsp_rate ?

@DerAndereJohannes
Copy link
Collaborator

DerAndereJohannes commented Feb 10, 2025

Sorry for the late response! A documentation change should definitely happen since it is currently incorrect. We can still make the function more lenient and accept all of the below:

  • list
  • np.ndarray
  • pd.Series
  • pd.DataFrame (this would still keep the current behaviour and we can just direct it to df["RSP_Troughs"])

This would make the current documentation correct again. If you feel up to the task, feel free to give it a go too ;-) Otherwise, I can do it on your behalf.

@me-pic
Copy link
Author

me-pic commented Feb 10, 2025

Thank you ! I can open the PR :)

@DominiqueMakowski
Copy link
Member

Go for it Marie-Eve and thanks 🙏

@me-pic me-pic mentioned this issue Feb 11, 2025
4 tasks
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

3 participants