[med-svn] [python-mne] 120/376: ENH : Epochs.average returns an Evoked object
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:22:19 UTC 2015
This is an automated email from the git hooks/post-receive script.
yoh pushed a commit to annotated tag v0.1
in repository python-mne.
commit 20e60b81d4469c675985ff9457247933cd53e54d
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Tue Mar 8 21:45:38 2011 -0500
ENH : Epochs.average returns an Evoked object
---
examples/plot_from_raw_to_epochs_to_evoked.py | 58 +++++++++++++++++++++++++++
mne/epochs.py | 34 ++++++++++++++--
mne/fiff/evoked.py | 12 +++---
mne/tests/test_forward.py | 1 +
4 files changed, 95 insertions(+), 10 deletions(-)
diff --git a/examples/plot_from_raw_to_epochs_to_evoked.py b/examples/plot_from_raw_to_epochs_to_evoked.py
new file mode 100644
index 0000000..6582ed7
--- /dev/null
+++ b/examples/plot_from_raw_to_epochs_to_evoked.py
@@ -0,0 +1,58 @@
+"""
+========================================================
+Extract epochs, average and save evoked response to disk
+========================================================
+
+This script shows how to read the epochs from a raw file given
+a list of events. The epochs are averaged to produce evoked
+data and then saved to disk.
+
+"""
+# Authors: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
+#
+# License: BSD (3-clause)
+
+print __doc__
+
+import mne
+from mne import fiff
+from mne.datasets import sample
+data_path = sample.data_path('.')
+
+###############################################################################
+# Set parameters
+raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
+event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
+event_id = 1
+tmin = -0.2
+tmax = 0.5
+
+# Setup for reading the raw data
+raw = fiff.Raw(raw_fname)
+events = mne.read_events(event_fname)
+
+# Set up pick list: EEG + STI 014 - bad channels (modify to your needs)
+include = [] # or stim channels ['STI 014']
+exclude = raw.info['bads'] + ['EEG 053'] # bads + 1 more
+
+# pick EEG channels
+picks = fiff.pick_types(raw.info, meg=False, eeg=True, stim=False,
+ include=include, exclude=exclude)
+# Read epochs
+epochs = mne.Epochs(raw, events, event_id,
+ tmin, tmax, picks=picks, baseline=(None, 0))
+evoked = epochs.average() # average epochs and get an Evoked dataset.
+
+evoked.save('sample_audvis_eeg-ave.fif') # save evoked data to disk
+
+###############################################################################
+# View evoked response
+times = 1e3 * epochs.times # time in miliseconds
+import pylab as pl
+pl.clf()
+pl.plot(times, 1e6*evoked.data.T)
+pl.xlim([times[0], times[-1]])
+pl.xlabel('time (ms)')
+pl.ylabel('Potential (uV)')
+pl.title('EEG evoked potential')
+pl.show()
diff --git a/mne/epochs.py b/mne/epochs.py
index 364ad8c..3d4bd80 100644
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -3,8 +3,10 @@
#
# License: BSD (3-clause)
+import copy
import numpy as np
import fiff
+from .fiff import Evoked
class Epochs(object):
@@ -30,6 +32,9 @@ class Epochs(object):
keep_comp : boolean
Apply CTF gradient compensation
+ info : dict
+ Measurement info
+
baseline: None (default) or tuple of length 2
The time interval to apply baseline correction.
If None do not apply it. If baseline is (a, b)
@@ -53,7 +58,8 @@ class Epochs(object):
Return all epochs as a 3D array [n_epochs x n_channels x n_times].
average() : self
- Return averaged epochs as a 2D array [n_channels x n_times].
+ Return Evoked object containing averaged epochs as a
+ 2D array [n_channels x n_times].
"""
@@ -71,6 +77,13 @@ class Epochs(object):
self.baseline = baseline
self.preload = preload
+ # Handle measurement info
+ self.info = copy.copy(raw.info)
+ if picks is not None:
+ self.info['chs'] = [self.info['chs'][k] for k in picks]
+ self.info['ch_names'] = [self.info['ch_names'][k] for k in picks]
+ self.info['nchan'] = len(picks)
+
if picks is None:
picks = range(len(raw.info['ch_names']))
self.ch_names = raw.info['ch_names']
@@ -205,19 +218,32 @@ class Epochs(object):
self._current += 1
return epoch
- def average(self):
+ def average(self, comment="Epoked data"):
"""Compute average of epochs
+ Parameters
+ ----------
+ comment : string
+ Comment that describes the Evoked data created.
+
Returns
-------
data : array of shape [n_channels, n_times]
The averaged epochs
"""
+ evoked = Evoked(None)
+ evoked.info = copy.copy(self.info)
n_channels = len(self.ch_names)
n_times = len(self.times)
n_events = len(self.events)
data = np.zeros((n_channels, n_times))
for e in self:
data += e
- return data / n_events
-
+ evoked.data = data / n_events
+ evoked.times = self.times.copy()
+ evoked.comment = comment
+ evoked.aspect_kind = np.array([100]) # XXX
+ evoked.nave = n_events
+ evoked.first = - np.sum(self.times < 0)
+ evoked.last = np.sum(self.times > 0)
+ return evoked
diff --git a/mne/fiff/evoked.py b/mne/fiff/evoked.py
index abb6f2b..716c444 100644
--- a/mne/fiff/evoked.py
+++ b/mne/fiff/evoked.py
@@ -25,8 +25,6 @@ class Evoked(object):
Attributes
----------
- fname :
-
nave : int
Number of averaged epochs
@@ -48,18 +46,21 @@ class Evoked(object):
data : 2D array of shape [n_channels x n_times]
Evoked response.
"""
+
def __init__(self, fname, setno=0, baseline=None):
"""
Parameters
----------
fname : string
- Name of evoked/average FIF file
+ Name of evoked/average FIF file to load.
+ If None no data is loaded.
setno : int
Dataset ID number
"""
- self.fname = fname
- self.fname = setno
+
+ if fname is None:
+ return
if setno < 0:
raise ValueError, 'Data set selector must be positive'
@@ -390,7 +391,6 @@ class Evoked(object):
end_file(fid)
-
def read_evoked(fname, setno=0, baseline=None):
"""Read an evoked dataset
diff --git a/mne/tests/test_forward.py b/mne/tests/test_forward.py
index 15e3006..6c074b6 100644
--- a/mne/tests/test_forward.py
+++ b/mne/tests/test_forward.py
@@ -9,6 +9,7 @@ examples_folder = op.join(op.dirname(__file__), '..', '..', 'examples')
data_path = sample.data_path(examples_folder)
fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis-meg-oct-6-fwd.fif')
+
def test_io_forward():
"""Test IO for forward solutions
"""
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git
More information about the debian-med-commit
mailing list