Skip to content

Commit 300ce43

Browse files
committed
updated epochs.average with np.nanmean
1 parent 6e6c4a4 commit 300ce43

1 file changed

Lines changed: 35 additions & 37 deletions

File tree

mne/epochs.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ def _compute_aggregate(self, picks, mode="mean"):
11671167
n_events += 1
11681168

11691169
if n_events > 0:
1170-
data /= n_events
1170+
data = np.nanmean(data)
11711171
else:
11721172
data.fill(np.nan)
11731173

@@ -2044,6 +2044,40 @@ def _repr_html_(self):
20442044
t = t.render(epochs=self, baseline=baseline, events=event_strings)
20452045
return t
20462046

2047+
def channel_specific_epoch_rejection(self, outliers: float):
2048+
"""Mask outlier epochs for each channel.
2049+
2050+
Parameters
2051+
----------
2052+
data : np.ndarray
2053+
The data to find outliers in. The data should be in the shape
2054+
(epochs X channels X (frequency) X time).
2055+
outliers : float
2056+
The number of standard deviations to use as a cutoff for outliers.
2057+
2058+
Returns
2059+
-------
2060+
epochs : instance of Epochs
2061+
The masked epochs object, modified in-place.
2062+
mask: np.ndarray
2063+
The array used to mask the epochs. True == Keep epochs,
2064+
False = reject epochs.
2065+
"""
2066+
# extract data from Epochs object
2067+
# get absolut values
2068+
abs_data = np.abs(self.get_data()) # (epochs X channels X (frequency) X time)
2069+
# get the maximum voltage per epoch
2070+
max = np.max(abs_data, axis=-1) # (epochs X channels X (frequency))
2071+
# get the standard deviation per channel
2072+
std = np.std(abs_data, axis=(-1, 0)) # (channels X (frequency))
2073+
# get the mean per channel
2074+
mean = np.mean(abs_data, axis=(-1, 0)) # (channels X (frequency))
2075+
# keep epochs where the maximum voltage is smaller than the mean + (outliers * std)
2076+
keep = max < ((outliers * std) + mean) # (epochs X channels X (frequency))
2077+
# set values to NaN where 2D mask is False in the 3D data array
2078+
self.get_data()[keep is False] = np.nan
2079+
return self, keep
2080+
20472081
@verbose
20482082
def crop(self, tmin=None, tmax=None, include_tmax=True, verbose=None):
20492083
"""Crop a time interval from the epochs.
@@ -4643,39 +4677,3 @@ def make_fixed_length_epochs(
46434677
proj=proj,
46444678
verbose=verbose,
46454679
)
4646-
4647-
4648-
def channel_specific_epoch_rejection(
4649-
data: np.ndarray, outliers: float
4650-
) -> np.ndarray[bool]:
4651-
"""Mask outlier epochs for each channel.
4652-
4653-
Parameters
4654-
----------
4655-
data : np.ndarray
4656-
The data to find outliers in. The data should be in the shape
4657-
(epochs X channels X (frequency) X time).
4658-
outliers : float
4659-
The number of standard deviations to use as a cutoff for outliers.
4660-
4661-
Returns
4662-
-------
4663-
np.ndarray[bool] with the shape epochs x channels x (frequency).
4664-
A boolean array with the first dimension epochs and the second
4665-
dimension channels. The third dimension is only present if the data
4666-
is frequency data. The boolean array indicates which epochs should be
4667-
kept (True) and which should be rejected (False) for each channel.
4668-
"""
4669-
# get absolut values
4670-
abs_data = np.abs(data) # (epochs X channels X (frequency) X time)
4671-
# get the maximum voltage per epoch
4672-
max = np.max(abs_data, axis=-1) # (epochs X channels X (frequency))
4673-
# get the standard deviation per channel
4674-
std = np.std(abs_data, axis=(-1, 0)) # (channels X (frequency))
4675-
# get the mean per channel
4676-
mean = np.mean(abs_data, axis=(-1, 0)) # (channels X (frequency))
4677-
# keep epochs where the maximum voltage is smaller than the mean + (outliers * std)
4678-
keep = max < ((outliers * std) + mean) # (epochs X channels X (frequency))
4679-
# set values to NaN where 2D mask is False in the 3D data array
4680-
data[keep is False] = np.nan
4681-
return keep

0 commit comments

Comments
 (0)