neuropsychology / NeuroKit

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

Unvevenly spaced Lomb, new feature issue. #799

Closed RichardBJ closed 1 year ago

RichardBJ commented 1 year ago

Describe the bug Running HRV analysis (frequency) with the Lomb method, including unevenly spaced data. I fully appreciate it could be my inputs, but in that case perhaps the doc string could be spelt out more.

error log (the message related to the error). I've tried many different attempts and so different errors, but for example: ValueError: NeuroKit error: _hrv_sanitize_input(): The peak indices passed were detected as non-consecutive. You might have passed RR intervals instead of peaks. If so, convert RRIs into peaks using nk.intervals_to_peaks(). To Reproduce Some data: rrs.xlsx So this works:

from astropy.timeseries import LombScargle
import matplotlib.pyplot as plt  
import pandas as pd
import neurokit2 as mykit
HRs=pd.read_excel("rrs.xlsx")
frequency, power = LombScargle(HRs["Time"], HRs["RR"], normalization="psd").autopower(
    minimum_frequency=0.1, maximum_frequency=3)
plt.plot(frequency, power) 

BUT this does not:

hrv_freq = mykit.hrv_frequency(HRs["RR"],
        psd_method="lombscargle", show=True, silent=False, normalize=True,
       interpolation_rate=None,t=HRs["Time"])

Expected behaviour would expect a periodograph produced, as with astropy, but prettier. So to confirm; works with a Welch and with astropy's lomb, but not my unevenly spaced lomb. Could be the way I am calling the function, but I have tried several different methods.

System Specifications iMac: Python 3.9?

danibene commented 1 year ago

Hi @RichardBJ thanks for this! I submitted a PR (https://github.com/neuropsychology/NeuroKit/pull/800) and tested the new version of the code with your data: https://gist.github.com/danibene/abbc76fdf6d3b447a216b1d69f791325

Running HRV analysis (frequency) with the Lomb method, including unevenly spaced data. I fully appreciate it could be my inputs, but in that case perhaps the doc string could be spelt out more.

Yes, to have the RRI intervals and their timestamps as input, the first argument needed to be a dictionary containing both the R-R intervals and their timestamps with the keys RRI and RRI_Time, rather than using t as a separate argument

https://github.com/neuropsychology/NeuroKit/blob/8c564938c5fc2b4a377fd89bb14ac07a430983b9/neurokit2/hrv/hrv_frequency.py#L61-L66

Any suggestions for how to improve would be appreciated :) thanks again!

stale[bot] commented 1 year ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.