Skip to content

Commit

Permalink
Merge pull request #164 from robintibor/lazy-preproc
Browse files Browse the repository at this point in the history
allow lazy preproc
  • Loading branch information
robintibor committed Sep 23, 2020
2 parents 258d975 + 7c68826 commit e5e7439
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions braindecode/datautil/preprocess.py
Expand Up @@ -35,13 +35,27 @@ def __init__(self, fn, **kwargs):
self.kwargs = kwargs

def apply(self, raw_or_epochs):
try:
self._try_apply(raw_or_epochs)
except RuntimeError:
# Maybe the function needs the data to be loaded
# and the data was not loaded yet
# Not all mne functions need data to be loaded,
# most importantly the 'crop' function can be
# lazily applied without preloading data
# which can make overall preprocessing pipeline
# substantially faster
raw_or_epochs.load_data()
self._try_apply(raw_or_epochs)

def _try_apply(self, raw_or_epochs):
if callable(self.fn):
self.fn(raw_or_epochs.load_data(), **self.kwargs)
self.fn(raw_or_epochs, **self.kwargs)
else:
if not hasattr(raw_or_epochs, self.fn):
raise AttributeError(
f'MNE object does not have {self.fn} method.')
getattr(raw_or_epochs.load_data(), self.fn)(**self.kwargs)
getattr(raw_or_epochs, self.fn)(**self.kwargs)


class NumpyPreproc(MNEPreproc):
Expand Down

0 comments on commit e5e7439

Please sign in to comment.