cbrnr / sleepecg

Sleep stage detection using ECG
BSD 3-Clause "New" or "Revised" License
90 stars 24 forks source link

Heart rate variability features #185

Closed PhD-GOAT closed 1 year ago

PhD-GOAT commented 1 year ago

Hi. I want to get the time or frequency domain features of hrv with your API. However, in the sleepecg.extract_features function, the data is only available in your API record. The question is, can't use the user's data yet? When using a function, it only displays an error message of AttributeError: 'numpy.int64' object has no attribute 'sleep_stages'.

cbrnr commented 1 year ago

Could you be more specific? I'm not sure I understand what you are trying to do. Could you provide a small code snippet to show what you tried and what did not work as expected?

PhD-GOAT commented 1 year ago

I used your code as it is.

sleepecg.extract_features(records, lookback=0, lookforward=30, sleep_stage_duration=30, feature_selection=None, fs_rri_resample=4, min_rri=None, max_rri=None, max_nans=0, n_jobs=1)

So if I ask you a different question, how do you convert user data that is not your API dataset in the form of Iterable[SleepRecord] parameters for characteristic extraction?

cbrnr commented 1 year ago

I'm still not sure what you are trying to do. Do you want to use sleepecg.extract_feaures() with your own data? Then you should create a SleepRecord object, which contains detected heart beat times. For example:

record = sleepecg.SleepRecord(
    sleep_stage_duration=30,
    recording_start_time=start,
    heartbeat_times=beats / fs,
)

In this example, I'm passing heartbeat_times=beats / fs, where beats is the output of our ECG peak detector (in samples), and dividing that by fs converts the beats to seconds. I'm also passing a recording_start_time and sleep_stage_duration, but these are optional.

PhD-GOAT commented 1 year ago

Oh, I see. I asked because I didn't know how to handle user data. Then can your API be used only as raw data without qrs detection filtering?

cbrnr commented 1 year ago

What do you mean by "user data"? ECG recordings? SleepECG handles everything starting with raw ECG time series, including R peak detection, feature extraction, and classification.

I know that this fundamental use case ("How do I use SleepECG with my own ECG data?") is not really represented in our docs, so I will add an example which shows how to do that.

PhD-GOAT commented 1 year ago

I am sorry that the English composition is not smooth because I used the translation program. The meaning of user data is not your example data set, but directly collected ecg data. So I wonder if your API can be used as a raw signal or you need to use a qrs detection algorithm.

cbrnr commented 1 year ago

Yes, you can use raw ECG data, but prior to extracting features, you need to detect R peaks. You can do everything with SleepECG, which means you can start with your raw data. I will add an example to show how to do that step by step soon.