The data includes ECG data, recording with X1 and X2 (channel names)
Therefore, real ECG data is the subtraction from X1 to X2.
I would like to find ecg_events (R waves) as below.
However, this code shows the events based on X2, not subtraction from X1 to X2.
I was wondering if it could be possible to let me know the best code for R detection.
MNE-Python version: 0.23.0
operating system: macOS Big Sur
I look forward to hearing from you.
Thanks,
Akihiro
Hi @Rokore, I think you should first create a new channel X_ECG based on the subtraction of your X1 from X2 (or the other way around). Then feed that new channel into the function.
As for “R detection”, I need more information to help.
To further help you, could you please turn your pipeline into a minimum working example? See below:
… minimal working example MWE to
replicate your problem, using one of the built-in datasets, preferably the
one called sample. If you can’t replicate on a built-in dataset, provide also
a link to a small, anonymized portion of your data that does yield the error.
posting such an example here will help us to figure out what’s wrong.
two more points:
Please use markdown formatting with your upcoming MWE
The docs you are linking to are outdated (0.18), assuming that you use an up to date MNE-Python (which I highly recommend), you should be using the docs of version 0.23: MNE — MNE 0.23.0 documentation
Thanks.
First, I am sorry that I don’t understand how to attach sample data here.
My pipeline was as below:
import mne
import matplotlib.pyplot as plt
import numpy as np
xxx = "ErrorSample.EEG"
eegdata=mne.io.read_raw_nihon(xxx,preload=True) # xxx is a name of raw data.
eegdata.set_eeg_reference()
eegdata.filter(1, 40., fir_design='firwin')
eegdata.set_channel_types(mapping={'X1': 'ecg'})
eegdata.set_channel_types(mapping={'X2': 'ecg'})
ecg=eegdata.pick_types(ecg=True)
ecg.plot(start=20, duration=5)
Thanks for sharing the example @Rokore - note that I made a few edits that were needed to actually make your example work: Next time, please also include the imports that you need, double check that the formatting renders the codeblocks correctly, and try copy-pasting your example yourself to see if it runs “as is” → that can help other people who are trying to help you
Having that said: I replicated your problem even on the current MNE-Python from the main branch.
Looking at your X1 and X2 channels I see that they are already properly referenced, one can clearly see the ECG signal → are you sure that you need to re-reference them?
However even by just using X1 and X2, I get the “misaligned” ECG epochs. I have no idea what’s happening here. Let’s ping some more people who may know about this: @cbrnr
I agree that subtracting X1 and X2 shouldn’t be necessary, both channels seem to contain different ECG derivations already. I’d use X2 because the signal looks nicer. Here’s a slightly more condensed version of the example:
This still yields four epochs that are shifted, but these might be caused by the peak detector getting these detections slightly wrong. I’d try to use a different detector to verify or to see if you get better results.
Thanks a lot.
Sample data includes only small part of all data, and many epochs, not only four, are shifted.
In addition, the shift is not at random and is based on downward peak, suggesting inverse detection of QRS, not simply wrong detection. I guess there should be solution to prevent the inverse.
Your recommended package looks good. I will try it. Thanks.
I have applied “detectors.pan_tompkins_detector(unfiltered_ecg)” in GitHub to my sample data, but unfortunately, R detection was miserable as below. Other detectors returned similar results.
I hope your new package is better.
Or I need another solution to prevent the inverse detection.
Using their implementation of the Pan-Tompkins (1985) algorithm (by passing method='pantompkins1985' to ecg_process()), the results don’t look as great, but still much better than what you got with MNE:
I’ve just released SleepECG, which contains the R peak detector I was talking about. You can install it simply with pip install sleepecg and see if you get better results on your full data sets.
because I also use Nihon Koden EEG system and there are two channels of ECG within the data. Sometimes, clinically, I saw clinicians use EKG2-EKG1. @Rokore
Hi, a technician said EKG 2(X 2)-EKG (X1) was more real, although data from one channel was acceptable. However, considering several posted opinions, I have changed my mind. In this context (not clinical), easier R detection might be more important.