Skip to content

[Bug] RSP Rate Calculation gives strange result - Potentially buggy _rsp_findpeaks_outliers #1082

@DerAndereJohannes

Description

@DerAndereJohannes

Describe the bug
Using the nk.rsp_rate function on both the trough and xcorr methods gives strange results, especially at the edges of the signal. This leads to the potential bug on _rsp_findpeaks_outliers as this seems to remove the last peak (or trough) regardless as it bases computing vertical distance on a difference function which naturally removes a value.

To Reproduce
Please see below a code snippet and resulting image of how the results of the functions look:

import sys
import neurokit2 as nk
import numpy as np
import matplotlib.pyplot as plt

sampling_rate = 1000
rsp_rate = 10
rsp = nk.rsp_simulate(
    duration=60, sampling_rate=sampling_rate, respiratory_rate=rsp_rate,
    random_state=42, noise=0
)

rsp = nk.rsp_clean(rsp, sampling_rate=sampling_rate)

_, info = nk.rsp_peaks(rsp, sampling_rate=1000)

print(info["RSP_Troughs"])

# Testing alt. test method for troughs (only check the rate between actual data points)
# start, end = info['RSP_Troughs'][0], info['RSP_Troughs'][-1]
# rate_trough = nk.rsp_rate(rsp, troughs=info['RSP_Troughs'], sampling_rate=sampling_rate, method='trough')
# print(rate_trough.shape, rsp.size)
# print(rate_trough[start:end].mean(), rsp_rate, rate_trough[start:end].mean() - rsp_rate)
# print(np.abs(rate_trough[start:end].mean()-rsp_rate))

# Test Troughs
rate_trough = nk.rsp_rate(rsp, troughs=info['RSP_Troughs'], sampling_rate=sampling_rate, method='trough')
print(rate_trough.shape, rsp.size)
print(rate_trough.mean(), rsp_rate, rate_trough.mean() - rsp_rate)
print(np.abs(rate_trough.mean()-rsp_rate))

# Test xcorr
rate_xcorr = nk.rsp_rate(rsp, troughs=info['RSP_Troughs'], sampling_rate=sampling_rate, method='xcorr')
print(rate_xcorr.shape, rsp.size)
print(rate_xcorr.mean(), rsp_rate, rate_xcorr.mean() - rsp_rate)
print(np.abs(rate_xcorr.mean()-rsp_rate))

fig, axs = plt.subplots(2)
fig.suptitle("Comparison between rsp_rate methods")
axs[0].set_title("RSP Signal")
axs[0].plot(rsp)
axs[0].scatter(x=info['RSP_Troughs'], y = [rsp[i] for i in info["RSP_Troughs"]], color='red')
axs[0].scatter(x=info['RSP_Peaks'], y = [rsp[i] for i in info["RSP_Peaks"]], color='green')
axs[0].legend(["rsp signal", "troughs", "peals"], loc="best")
axs[1].set_title("Comparison of Rate Computation methods")
axs[1].plot(rate_xcorr)
axs[1].plot(rate_trough)
axs[1].legend(["xcorr", "trough"], loc="best")
plt.show()

Image

Expected behaviour
The result of RSP Rate should be closer to the actual rsp rate value and the edge breaths should also be accounted for

System Specifications

- OS: Linux (ELF 64bit) 
- Python: 3.13.2 
- NeuroKit2: 0.2.11 

- NumPy: 2.1.3 
- Pandas: 2.2.3 
- SciPy: 1.15.2 
- sklearn: 1.6.1 
- matplotlib: 3.10.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions