From 7e9899d5a2e3b9286be576f10cd76f2f0629db3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yorguin=20Jos=C3=A9=20Mantilla=20Ramos?= <36543115+yjmantilla@users.noreply.github.com> Date: Wed, 8 Apr 2020 03:05:10 -0500 Subject: [PATCH] detrend_correction (#9) --- .gitignore | 1 + docs/whats_new.rst | 9 +++++++++ pyprep/find_noisy_channels.py | 10 +++++++--- pyprep/reference.py | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 298e1007..958ad6ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* +/idea /src /.pytest_* docs/generated diff --git a/docs/whats_new.rst b/docs/whats_new.rst index 2b131382..3bed6630 100644 --- a/docs/whats_new.rst +++ b/docs/whats_new.rst @@ -21,6 +21,7 @@ Current Changelog ~~~~~~~~~ +- Included a boolean which indicates if detrend should be done internally or not for the use of find_noisy_channels in reference.py. `Yorguin Mantilla`_ - Robust average referencing + tests by `Victor Xiang`_ - Removing trend in the EEG data by high pass filtering and local linear regression + tests `Aamna Lawrence`_ - Finding noisy channels with comparable output to Matlab +tests-including test for ransac `Aamna Lawrence`_ @@ -30,6 +31,12 @@ Changelog - Finding the reason for the difference between the Matlab and Pyprep’s output- Probably minor differences in the filter functions and also rounding done by functions like quantile `Victor Xiang`_ and `Aamna Lawrence`_ - Overall debugging `Victor Xiang`_ and `Aamna Lawrence`_ + +Bug +~~~ + +- In find_noisy_channels the signal detrend is accidentally undone which destabilized the RANSAC. The detrend is now done over the NoisyChannels.raw_mne object to avoid this bug and to force that any signal in there is detrended like in matlab's prep. By `Yorguin Mantilla`_ (`#29 `_) + API ~~~ @@ -75,8 +82,10 @@ People who contributed to this software (in alphabetical order) * Aamna Lawrence * Adam Li * Victor Xiang +* Yorguin Mantilla .. _Stefan Appelhoff: http://stefanappelhoff.com/ .. _Aamna Lawrence: https://github.com/AamnaLawrence .. _Adam Li: https://github.com/adam2392/ .. _Victor Xiang: https://github.com/Nick3151 +.. _Yorguin Mantilla: https://github.com/yjmantilla diff --git a/pyprep/find_noisy_channels.py b/pyprep/find_noisy_channels.py index f805e6c1..730a8fa6 100644 --- a/pyprep/find_noisy_channels.py +++ b/pyprep/find_noisy_channels.py @@ -24,15 +24,19 @@ class NoisyChannels: """ - def __init__(self, raw): + def __init__(self, raw, do_detrend=True): """Initialize the class.""" # Make sure that we got an MNE object assert isinstance(raw, mne.io.BaseRaw) self.raw_mne = raw.copy() self.sample_rate = raw.info["sfreq"] + if do_detrend: + self.raw_mne._data = removeTrend( + self.raw_mne.get_data(), sample_rate=self.sample_rate + ) + self.EEGData = self.raw_mne.get_data(picks="eeg") - self.EEGData = removeTrend(self.EEGData, sample_rate=self.sample_rate) self.EEGData_beforeFilt = self.EEGData self.ch_names_original = np.asarray(raw.info["ch_names"]) self.n_chans_original = len(self.ch_names_original) @@ -237,7 +241,7 @@ def find_bad_by_hfnoise(self, HF_zscore_threshold=5.0): return None def find_bad_by_correlation( - self, correlation_secs=1.0, correlation_threshold=0.4, frac_bad=0.1 + self, correlation_secs=1.0, correlation_threshold=0.4, frac_bad=0.01 ): """Find correlation between the low frequency components of the EEG below 50 Hz. diff --git a/pyprep/reference.py b/pyprep/reference.py index 7935c991..7650d3a4 100644 --- a/pyprep/reference.py +++ b/pyprep/reference.py @@ -131,7 +131,7 @@ def robust_reference(self): raw._data = removeTrend(raw.get_data(), sample_rate=self.sfreq) # Determine unusable channels and remove them from the reference channels - noisy_detector = NoisyChannels(raw) + noisy_detector = NoisyChannels(raw, do_detrend=False) noisy_detector.find_all_bads(ransac=self.ransac) self.noisy_channels_original = { "bad_by_nan": noisy_detector.bad_by_nan,