Skip to content

Commit

Permalink
Merge pull request mne-tools#3335 from gamazeps/fix-issue-3268
Browse files Browse the repository at this point in the history
[WIP][FIX] Adds apply_baseline to Evoked
  • Loading branch information
dengemann authored Jul 5, 2016
2 parents 5d33ad2 + 060db92 commit 01fa573
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mne/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def decimate(self, decim, offset=0):
return self

@verbose
def apply_baseline(self, baseline, verbose=None):
def apply_baseline(self, baseline=(None, 0), verbose=None):
"""Baseline correct epochs
Parameters
Expand Down
39 changes: 36 additions & 3 deletions mne/evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ class Evoked(ProjMixin, ContainsMixin, UpdateChannelsMixin,
Dataset ID number (int) or comment/name (str). Optional if there is
only one data set in file.
baseline : tuple or list of length 2, or None
This parameter has been deprecated and will be removed in 0.14
Use inst.apply_baseline(baseline) instead.
The time interval to apply rescaling / baseline correction.
If None do not apply it. If baseline is (a, b)
the interval is between "a (s)" and "b (s)".
If a is None the beginning of the data is used
and if b is None then b is set to the end of the interval.
If baseline is equal ot (None, None) all the time
If baseline is equal to (None, None) all the time
interval is used. If None, no correction is applied.
proj : bool, optional
Apply SSP projection vectors
Expand Down Expand Up @@ -113,7 +115,38 @@ def __init__(self, fname, condition=None, baseline=None, proj=True,
# project and baseline correct
if proj:
self.apply_proj()
self.apply_baseline(baseline, self.verbose)

@verbose
def apply_baseline(self, baseline=(None, 0), verbose=None):
"""Baseline correct evoked data
Parameters
----------
baseline : tuple of length 2
The time interval to apply rescaling / baseline correction.
If None do not apply it. If baseline is (a, b)
the interval is between "a (s)" and "b (s)".
If a is None the beginning of the data is used
and if b is None then b is set to the end of the interval.
If baseline is equal to (None, None) all the time
interval is used. If None, no correction is applied.
verbose : bool, str, int, or None
If not None, override default verbose level (see mne.verbose).
Returns
-------
evoked : instance of Evoked
The baseline-corrected Evoked object.
Notes
-----
Baseline correction can be done multiple times.
.. versionadded:: 0.13.0
"""
self.data = rescale(self.data, self.times, baseline, copy=False)
return self

def save(self, fname):
"""Save dataset to file.
Expand Down Expand Up @@ -1258,8 +1291,8 @@ def read_evokeds(fname, condition=None, baseline=None, kind='average',
condition = [condition]
return_list = False

out = [Evoked(fname, c, baseline=baseline, kind=kind, proj=proj,
verbose=verbose) for c in condition]
out = [Evoked(fname, c, kind=kind, proj=proj, verbose=verbose)
.apply_baseline(baseline) for c in condition]

return out if return_list else out[0]

Expand Down
2 changes: 2 additions & 0 deletions mne/io/tests/data/test-ave-2.log
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Created an SSP operator (subspace dimension = 4)
4 projection items activated
SSP projectors applied...
No baseline correction applied...
No baseline correction applied...
Reading mne/fiff/tests/data/test-ave.fif ...
Read a total of 4 projection items:
PCA-v1 (1 x 102) idle
Expand All @@ -26,3 +27,4 @@ Created an SSP operator (subspace dimension = 4)
4 projection items activated
SSP projectors applied...
No baseline correction applied...
No baseline correction applied...
1 change: 1 addition & 0 deletions mne/io/tests/data/test-ave.log
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Created an SSP operator (subspace dimension = 4)
4 projection items activated
SSP projectors applied...
No baseline correction applied...
No baseline correction applied...
13 changes: 13 additions & 0 deletions mne/tests/test_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,17 @@ def test_add_channels():
assert_raises(AssertionError, evoked_meg.add_channels, evoked_badsf)


def test_evoked_baseline():
"""Test evoked baseline"""
evoked = read_evokeds(fname, condition=0, baseline=None)

# Here we create a data_set with constant data.
evoked = EvokedArray(np.ones_like(evoked.data), evoked.info, evoked.times[0])

# Mean baseline correction is applied, since the data is equal to its mean
# the resulting data should be a matrix of zeroes.
evoked.apply_baseline((None, None))

assert_allclose(evoked.data, np.zeros_like(evoked.data))

run_tests_if_main()

0 comments on commit 01fa573

Please sign in to comment.