Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement channel specific epoch rejection #12219

Draft
wants to merge 48 commits into
base: main
Choose a base branch
from
Draft
Changes from 10 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d7ccfd4
function to reject epochs per channel
CarinaFo Nov 16, 2023
f94c7e4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 16, 2023
32a87f8
added masked data to channel specific rejection
CarinaFo Nov 16, 2023
8fccf71
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Nov 16, 2023
6e6c4a4
added masked data to function and to return
CarinaFo Nov 16, 2023
7da2ab5
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Nov 16, 2023
300ce43
updated epochs.average with np.nanmean
CarinaFo Nov 16, 2023
afd2c8e
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Nov 16, 2023
f5804ab
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Nov 17, 2023
880d157
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Nov 17, 2023
afed69d
Update mne/epochs.py
CarinaFo Nov 17, 2023
194c83a
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Nov 18, 2023
e054119
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Dec 1, 2023
f742c1e
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Dec 5, 2023
b74d4de
updated changelog for PR11776
CarinaFo Feb 7, 2024
cbd7083
updated github account name
CarinaFo Feb 7, 2024
8a3eea5
resolve merge conflict - forgot to pull before adding to changelog
CarinaFo Feb 7, 2024
eb6ac3d
added contribution to changelog
CarinaFo Feb 7, 2024
7d17f89
added second PR number to close both
CarinaFo Feb 7, 2024
c84bebb
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 13, 2024
9e00065
Merge branch 'mne-tools:main' into channel_specific_epoch_rejection
CarinaFo Feb 13, 2024
0c20484
Merge branch 'mne-tools:main' into channel_specific_epoch_rejection
CarinaFo Feb 14, 2024
0d03885
deleted bad epochs function
CarinaFo Feb 14, 2024
e16cf74
added interpolate bad epochs method
CarinaFo Feb 14, 2024
b00f2b0
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 14, 2024
0cb0ed5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 14, 2024
8a2f95b
fixed bug in interpolate epochs method
CarinaFo Feb 14, 2024
3e9435b
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 14, 2024
341fd25
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Feb 15, 2024
1e2252e
added interpolate bad epochs nan to interpolate_bads function
CarinaFo Feb 15, 2024
dfbba00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 15, 2024
cc58b73
removed interpolate bad epochs from interpolate_bads due to MixinClass
CarinaFo Feb 15, 2024
167f38c
removed interpolate bad epochs
CarinaFo Feb 15, 2024
4e675cc
added set bad epochs to NaN method for epochs class
CarinaFo Feb 15, 2024
3b5d67b
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 15, 2024
2868197
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 15, 2024
7fc2be1
removed return statement (operates in-place)
CarinaFo Feb 15, 2024
91302bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 15, 2024
b22cf11
deleted epoch based rejection from doc string
CarinaFo Feb 15, 2024
b011e6a
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Feb 15, 2024
c794710
DW initial revisions
dominikwelke Feb 21, 2024
a27b135
Merge branch 'main' into channel_specific_epoch_rejection
CarinaFo Feb 21, 2024
f2ea3c4
Merge branch 'mne-tools:main' into channel_specific_epoch_rejection
CarinaFo Feb 24, 2024
7feea14
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 26, 2024
1856286
Merge branch 'channel_specific_epoch_rejection' of github.com:CarinaF…
CarinaFo Feb 26, 2024
caac9f4
Merge pull request #3 from dominikwelke/carinafo/channel_specific_epo…
CarinaFo Feb 26, 2024
fa9567a
fixed docstring
CarinaFo Feb 28, 2024
4abf7fd
fixed dostring set_bad_epochs_to_NaN
CarinaFo Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion mne/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ def _compute_aggregate(self, picks, mode="mean"):
n_events += 1

if n_events > 0:
data /= n_events
data = np.nanmean(data)
else:
data.fill(np.nan)

Expand Down Expand Up @@ -2047,6 +2047,40 @@ def _repr_html_(self):
t = t.render(epochs=self, baseline=baseline, events=event_strings)
return t

def channel_specific_epoch_rejection(self, outliers: float):
"""Mask outlier epochs for each channel.

Parameters
----------
data : np.ndarray
The data to find outliers in. The data should be in the shape
(epochs X channels X (frequency) X time).
outliers : float
The number of standard deviations to use as a cutoff for outliers.

Returns
-------
epochs : instance of Epochs
The masked epochs object, modified in-place.
mask: np.ndarray
The array used to mask the epochs. True == Keep epochs,
False = reject epochs.
"""
CarinaFo marked this conversation as resolved.
Show resolved Hide resolved
# extract data from Epochs object
# get absolut values
abs_data = np.abs(self.get_data()) # (epochs X channels X (frequency) X time)
# get the maximum voltage per epoch
max = np.max(abs_data, axis=-1) # (epochs X channels X (frequency))
# get the standard deviation per channel
std = np.std(abs_data, axis=(-1, 0)) # (channels X (frequency))
# get the mean per channel
mean = np.mean(abs_data, axis=(-1, 0)) # (channels X (frequency))
# keep epochs where the maximum voltage is smaller than the mean + (outliers * std)
keep = max < ((outliers * std) + mean) # (epochs X channels X (frequency))
# set values to NaN where 2D mask is False in the 3D data array
self.get_data()[keep is False] = np.nan
return self, keep

@verbose
def crop(self, tmin=None, tmax=None, include_tmax=True, verbose=None):
"""Crop a time interval from the epochs.
Expand Down