Skip to content

Commit b5a2a96

Browse files
larsoneragramfort
authored andcommitted
MRG: Expose drop log (#4652)
* FIX: Expose drop log * FIX: Add versionadded
1 parent 2a98e81 commit b5a2a96

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

doc/whats_new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Changelog
113113

114114
- Enable morphing between hemispheres with :func:`mne.compute_morph_matrix` by `Christian Brodbeck`_
115115

116+
- Add ``return_drop_log`` to :func:`mne.preprocessing.compute_proj_eog` and :func:`mne.preprocessing.compute_proj_ecg` by `Eric Larson`_
117+
116118
BUG
117119
~~~
118120

mne/preprocessing/ssp.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def _compute_exg_proj(mode, raw, raw_event, tmin, tmax,
3232
average, filter_length, n_jobs, ch_name,
3333
reject, flat, bads, avg_ref, no_proj, event_id,
3434
exg_l_freq, exg_h_freq, tstart, qrs_threshold,
35-
filter_method, iir_params=None, verbose=None):
35+
filter_method, iir_params=None, return_drop_log=False,
36+
verbose=None):
3637
"""Compute SSP/PCA projections for ECG or EOG artifacts.
3738
3839
.. note:: raw data must be preloaded.
@@ -95,6 +96,8 @@ def _compute_exg_proj(mode, raw, raw_event, tmin, tmax,
9596
Dictionary of parameters to use for IIR filtering.
9697
See mne.filter.construct_iir_filter for details. If iir_params
9798
is None and method="iir", 4th order Butterworth will be used.
99+
return_drop_log : bool
100+
If True, return the drop log.
98101
verbose : bool, str, int, or None
99102
If not None, override default verbose level (see :func:`mne.verbose`
100103
and :ref:`Logging documentation <tut_logging>` for more).
@@ -105,6 +108,8 @@ def _compute_exg_proj(mode, raw, raw_event, tmin, tmax,
105108
Computed SSP projectors.
106109
events : ndarray
107110
Detected events.
111+
drop_log : list
112+
The drop log, if requested.
108113
"""
109114
if not raw.preload:
110115
raise ValueError('raw needs to be preloaded, '
@@ -188,10 +193,10 @@ def _compute_exg_proj(mode, raw, raw_event, tmin, tmax,
188193
epochs = Epochs(raw, events, None, tmin, tmax, baseline=None, preload=True,
189194
picks=picks, reject=reject, flat=flat, proj=True)
190195

191-
epochs.drop_bad()
196+
drop_log = epochs.drop_log
192197
if epochs.events.shape[0] < 1:
193198
warn('No good epochs found, returning None for projs')
194-
return None, events
199+
return (None, events) + ((drop_log,) if return_drop_log else ())
195200

196201
if average:
197202
evoked = epochs.average()
@@ -205,10 +210,8 @@ def _compute_exg_proj(mode, raw, raw_event, tmin, tmax,
205210
p['desc'] = mode + "-" + p['desc']
206211

207212
projs.extend(ev_projs)
208-
209213
logger.info('Done.')
210-
211-
return projs, events
214+
return (projs, events) + ((drop_log,) if return_drop_log else ())
212215

213216

214217
@verbose
@@ -220,7 +223,8 @@ def compute_proj_ecg(raw, raw_event=None, tmin=-0.2, tmax=0.4,
220223
flat=None, bads=[], avg_ref=False,
221224
no_proj=False, event_id=999, ecg_l_freq=5, ecg_h_freq=35,
222225
tstart=0., qrs_threshold='auto', filter_method='fft',
223-
iir_params=None, copy=True, verbose=None):
226+
iir_params=None, copy=True, return_drop_log=False,
227+
verbose=None):
224228
"""Compute SSP/PCA projections for ECG artifacts.
225229
226230
.. note:: raw data must be preloaded.
@@ -283,6 +287,10 @@ def compute_proj_ecg(raw, raw_event=None, tmin=-0.2, tmax=0.4,
283287
is None and method="iir", 4th order Butterworth will be used.
284288
copy : bool
285289
If False, filtering raw data is done in place. Defaults to True.
290+
return_drop_log : bool
291+
If True, return the drop log.
292+
293+
.. versionadded:: 0.15
286294
verbose : bool, str, int, or None
287295
If not None, override default verbose level (see :func:`mne.verbose`
288296
and :ref:`Logging documentation <tut_logging>` for more).
@@ -293,19 +301,15 @@ def compute_proj_ecg(raw, raw_event=None, tmin=-0.2, tmax=0.4,
293301
Computed SSP projectors.
294302
ecg_events : ndarray
295303
Detected ECG events.
304+
drop_log : list
305+
The drop log, if requested.
296306
"""
297-
if copy is True:
298-
raw = raw.copy()
299-
300-
projs, ecg_events = _compute_exg_proj('ECG', raw, raw_event, tmin, tmax,
301-
n_grad, n_mag, n_eeg, l_freq, h_freq,
302-
average, filter_length, n_jobs,
303-
ch_name, reject, flat, bads, avg_ref,
304-
no_proj, event_id, ecg_l_freq,
305-
ecg_h_freq, tstart, qrs_threshold,
306-
filter_method, iir_params)
307-
308-
return projs, ecg_events
307+
raw = raw.copy() if copy else raw
308+
return _compute_exg_proj(
309+
'ECG', raw, raw_event, tmin, tmax, n_grad, n_mag, n_eeg,
310+
l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, flat,
311+
bads, avg_ref, no_proj, event_id, ecg_l_freq, ecg_h_freq, tstart,
312+
qrs_threshold, filter_method, iir_params, return_drop_log)
309313

310314

311315
@verbose
@@ -316,7 +320,8 @@ def compute_proj_eog(raw, raw_event=None, tmin=-0.2, tmax=0.2,
316320
eog=np.inf), flat=None, bads=[],
317321
avg_ref=False, no_proj=False, event_id=998, eog_l_freq=1,
318322
eog_h_freq=10, tstart=0., filter_method='fft',
319-
iir_params=None, ch_name=None, copy=True, verbose=None):
323+
iir_params=None, ch_name=None, copy=True,
324+
return_drop_log=False, verbose=None):
320325
"""Compute SSP/PCA projections for EOG artifacts.
321326
322327
.. note:: raw data must be preloaded.
@@ -375,6 +380,10 @@ def compute_proj_eog(raw, raw_event=None, tmin=-0.2, tmax=0.2,
375380
If not None, specify EOG channel name.
376381
copy : bool
377382
If False, filtering raw data is done in place. Defaults to True.
383+
return_drop_log : bool
384+
If True, return the drop log.
385+
386+
.. versionadded:: 0.15
378387
verbose : bool, str, int, or None
379388
If not None, override default verbose level (see :func:`mne.verbose`
380389
and :ref:`Logging documentation <tut_logging>` for more).
@@ -385,17 +394,13 @@ def compute_proj_eog(raw, raw_event=None, tmin=-0.2, tmax=0.2,
385394
Computed SSP projectors.
386395
eog_events: ndarray
387396
Detected EOG events.
397+
drop_log : list
398+
The drop log, if requested.
388399
"""
389-
if copy is True:
390-
raw = raw.copy()
391-
projs, eog_events = _compute_exg_proj('EOG', raw, raw_event, tmin, tmax,
392-
n_grad, n_mag, n_eeg, l_freq, h_freq,
393-
average, filter_length, n_jobs,
394-
ch_name, reject, flat, bads, avg_ref,
395-
no_proj, event_id, eog_l_freq,
396-
eog_h_freq, tstart,
397-
qrs_threshold='auto',
398-
filter_method=filter_method,
399-
iir_params=iir_params)
400-
401-
return projs, eog_events
400+
raw = raw.copy() if copy else raw
401+
return _compute_exg_proj(
402+
'EOG', raw, raw_event, tmin, tmax, n_grad, n_mag, n_eeg,
403+
l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, flat,
404+
bads, avg_ref, no_proj, event_id, eog_l_freq, eog_h_freq, tstart,
405+
qrs_threshold='auto', filter_method=filter_method,
406+
iir_params=iir_params, return_drop_log=return_drop_log)

mne/preprocessing/tests/test_ssp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ def test_compute_proj_ecg():
4949
# without setting a bad channel, this should throw a warning
5050
with warnings.catch_warnings(record=True) as w:
5151
warnings.simplefilter('always')
52-
projs, events = compute_proj_ecg(
52+
projs, events, drop_log = compute_proj_ecg(
5353
raw, n_mag=2, n_grad=2, n_eeg=2, ch_name='MEG 1531', bads=[],
5454
average=average, avg_ref=True, no_proj=True, l_freq=None,
55-
h_freq=None, tmax=dur_use)
55+
h_freq=None, tmax=dur_use, return_drop_log=True)
5656
assert_true(len(w) >= 1)
5757
assert_equal(projs, None)
58+
assert_equal(len(events), len(drop_log))
5859

5960

6061
def test_compute_proj_eog():

0 commit comments

Comments
 (0)