neuropsychology / NeuroKit

NeuroKit2: The Python Toolbox for Neurophysiological Signal Processing
https://neuropsychology.github.io/NeuroKit
MIT License
1.58k stars 420 forks source link

How to compute the ratio of LF/HF with Neurokit #152

Closed yywangvr closed 4 years ago

yywangvr commented 4 years ago

Dear Developers Describe the solution you'd like A clear and concise description of what you want to happen. In many articles the signal is transformed into Fourier domain for analysis, and some can compute the ratio between LF (low frequency) and HF (high frequency).

How could we do it? I don't if this feature has been developed or not, I wish to analyse my signal in this way. But I have no idea how to do it. Thank you in advance if you could give me a hand.

What is a signal rate? By the way, I saw you have a function "signal_rate" which calculates signal rate from a series of peaks, but what is the "signal rate"?

DominiqueMakowski commented 4 years ago

@Yuyang01 You are right on time, as we just started implementing this kind of tools in our functions for heart rate variability. In it, we compute LF/HF using the nk.signal_power() function, you can see how it's done in the code and apply it to your own signal:

https://github.com/neuropsychology/NeuroKit/blob/35be5e99dc3e7c408f2d4842c271492372d7ea32/neurokit2/ecg/ecg_hrv.py#L142-L144

https://github.com/neuropsychology/NeuroKit/blob/35be5e99dc3e7c408f2d4842c271492372d7ea32/neurokit2/ecg/ecg_hrv.py#L148

PS: it still needs to be thoroughly tested, so do not hesitate to give any feedback

yywangvr commented 4 years ago
  1. @DominiqueMakowski It works. I leave the code (from your file) here for future readers to get start:
ecg = nk.ecg_simulate(duration=240)
ecg, info = nk.ecg_process(ecg)
hrv = nk.ecg_hrv(ecg, show=True)

Thank you.

instant vs continuous? In the function

def signal_power(signal, frequency_band, sampling_rate=1000, continuous=False, **kwargs):

Despite the comment in the file, I still don't understand the difference between continuous and instant. Could you explain me also?

BVP vs ECG ? Right now I am collecting the BVP for computing HRV; may I directly use the ECG function? I am sorry because I don't much about the difference. As far as I know, I can because they are the same logic, but I am not sure. I wish to confirm with you.

ecg_process reports errors when I use different sampling_rate?

ecg.txt

# works
bop = np.loadtxt('ecg.txt', delimiter=',')
bvp, info = nk.ecg_process(bap, sampling_rate=1000)
# error, when I set the sampling_rate to its real sampling rate. 
bvp = np.loadtxt('ecg.txt', delimiter=',')
bvp, info = nk.ecg_process(bvp,sampling_rate=64)

and I got the following error when "sampling_rate=64"

/Users/yuyang01/miniconda3/envs/py37/lib/python3.7/site-packages/numpy/core/fromnumeric.py:3335: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
Traceback (most recent call last):

  File "<ipython-input-65-8b67981c61ce>", line 1, in <module>
    ecg, info = nk.ecg_process(e4BVP,sampling_rate=64)

  File "/Users/yuyang01/miniconda3/envs/py37/lib/python3.7/site-packages/neurokit2/ecg/ecg_process.py", line 56, in ecg_process
    correct_artifacts=True)

  File "/Users/yuyang01/miniconda3/envs/py37/lib/python3.7/site-packages/neurokit2/ecg/ecg_peaks.py", line 74, in ecg_peaks
    peak_indices=rpeaks["ECG_R_Peaks"])

  File "/Users/yuyang01/miniconda3/envs/py37/lib/python3.7/site-packages/neurokit2/signal/signal_formatpeaks.py", line 16, in signal_formatpeaks
    signals[feature] = _signal_from_indices(info[feature], desired_length, 1)

  File "/Users/yuyang01/miniconda3/envs/py37/lib/python3.7/site-packages/neurokit2/signal/signal_formatpeaks.py", line 35, in _signal_from_indices
    if isinstance(indices[0], np.float):

IndexError: index 0 is out of bounds for axis 0 with size 0

Do you know why it works only in 1000hz sampling rate, not in 64 sampling rate?

Best regards,

DominiqueMakowski commented 4 years ago

instant vs continuous?

Instant get the amount of power in a given frequency band. It returns a point value. On the contrary, continuous estimates the fluctuations of power in a given band throughout the signal (returning a signal-like shaped array).

BVP vs ECG ?

For now we don't have (yet) support for PPG/BVP. So for now, I'd recommend using other packages for that, such as for instance the excellent systole, to at least get your preprocessing done.

Once you have detected the peaks, you could potentially plug that into ecg_hrv to then get HRV indices.

You could also consider contributing and getting started on implementing BVP processing support 😉

ecg_process reports errors when I use different sampling_rate?

Yeah, it's likely due to the fact that ECG and BVP signals are very different, and that you cannot really use the ECG pipeline for BVP.

yywangvr commented 4 years ago

Dear @DominiqueMakowski ,

Thank you for your helpful explanation. I am glad to have your invitation to contribute the package, but as I am very new to signal processing at this stage, I would postpone this for a while.

Best regards, Yuyang