[med-svn] [python-mne] 141/376: new plot_evoked function + nicer examples

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:22:22 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 ffdce874fc76f02f61017b7f20161e779f13826a
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Wed Mar 16 12:11:19 2011 -0400

    new plot_evoked function + nicer examples
---
 examples/plot_read_epochs.py | 65 ++++++++------------------------------------
 examples/plot_read_evoked.py | 31 ++++-----------------
 mne/fiff/pick.py             | 27 ++++++++++++++++++
 mne/viz.py                   | 40 +++++++++++++++++++++++++++
 4 files changed, 84 insertions(+), 79 deletions(-)

diff --git a/examples/plot_read_epochs.py b/examples/plot_read_epochs.py
index 856dc7d..884eadf 100644
--- a/examples/plot_read_epochs.py
+++ b/examples/plot_read_epochs.py
@@ -17,6 +17,7 @@ print __doc__
 
 import mne
 from mne import fiff
+from mne.viz import plot_evoked
 from mne.datasets import sample
 data_path = sample.data_path('.')
 
@@ -24,66 +25,22 @@ 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
+event_id, tmin, tmax = 1, -0.2, 0.5
 
 #   Setup for reading the raw data
 raw = fiff.Raw(raw_fname)
 events = mne.read_events(event_fname)
 
-#   Set up pick list: MEG + STI 014 - bad channels (modify to your needs)
-include = [] # or stim channels ['STI 014']
+# Set up pick list: EEG + MEG - bad channels (modify to your needs)
 exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+picks = fiff.pick_types(raw.info, meg=True, eeg=True, stim=False,
+                            exclude=exclude)
 
-# EEG
-eeg_picks = fiff.pick_types(raw.info, meg=False, eeg=True, stim=False,
-                                            include=include, exclude=exclude)
-eeg_epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
-                        picks=eeg_picks, baseline=(None, 0), preload=False)
-eeg_evoked = eeg_epochs.average()
-eeg_evoked_data = eeg_evoked.data
-
-# MEG Magnetometers
-meg_mag_picks = fiff.pick_types(raw.info, meg='mag', eeg=False, stim=False,
-                                            include=include, exclude=exclude)
-meg_mag_epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
-                        picks=meg_mag_picks, baseline=(None, 0), preload=False)
-meg_mag_evoked = meg_mag_epochs.average()
-meg_mag_evoked_data = meg_mag_evoked.data
-
-# MEG
-meg_grad_picks = fiff.pick_types(raw.info, meg='grad', eeg=False,
-                                stim=False, include=include, exclude=exclude)
-meg_grad_epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
-                        picks=meg_grad_picks, baseline=(None, 0), preload=False)
-meg_grad_evoked = meg_grad_epochs.average()
-meg_grad_evoked_data = meg_grad_evoked.data
+# Read epochs
+epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
+                    picks=picks, baseline=(None, 0), preload=False)
+evoked = epochs.average() # average epochs to get the evoked response
 
 ###############################################################################
-# View evoked response
-times = 1e3 * eeg_epochs.times # time in ms
-import pylab as pl
-pl.clf()
-pl.subplot(3, 1, 1)
-pl.plot(times, 1e13*meg_grad_evoked_data.T)
-pl.ylim([-200, 200])
-pl.xlim([times[0], times[-1]])
-pl.xlabel('time (ms)')
-pl.ylabel('Magnetic Field (fT/cm)')
-pl.title('MEG (Gradiometers) evoked field')
-pl.subplot(3, 1, 2)
-pl.plot(times, 1e15*meg_mag_evoked_data.T)
-pl.ylim([-600, 600])
-pl.xlim([times[0], times[-1]])
-pl.xlabel('time (ms)')
-pl.ylabel('Magnetic Field (fT)')
-pl.title('MEG (Magnetometers) evoked field')
-pl.subplot(3, 1, 3)
-pl.plot(times, 1e6*eeg_evoked_data.T)
-pl.xlim([times[0], times[-1]])
-pl.xlabel('time (ms)')
-pl.ylabel('Potential (uV)')
-pl.title('EEG evoked potential')
-pl.subplots_adjust(0.175, 0.07, 0.94, 0.94, 0.2, 0.53)
-pl.show()
+# Show result
+plot_evoked(evoked)
diff --git a/examples/plot_read_evoked.py b/examples/plot_read_evoked.py
index 7227732..0680186 100644
--- a/examples/plot_read_evoked.py
+++ b/examples/plot_read_evoked.py
@@ -12,38 +12,19 @@ print __doc__
 
 from mne import fiff
 from mne.datasets import sample
+from mne.viz import plot_evoked
+
 data_path = sample.data_path('.')
 
 fname = data_path + '/MEG/sample/sample_audvis-ave.fif'
 
 # Reading
 evoked = fiff.Evoked(fname, setno=0, baseline=(None, 0))
-times = 1e3*evoked.times # times in ms
-data = evoked.data
 
-evoked.save('test-ave.fif')
+evoked.save('test-ave.fif') # save file to disk
 
 ###############################################################################
 # Show result
-import pylab as pl
-pl.clf()
-pl.subplot(3, 1, 1)
-pl.plot(times, 1e13*data[0:306:3,:].T)
-pl.ylim([-200, 200])
-pl.title('Planar Gradiometers 1')
-pl.xlabel('time (ms)')
-pl.ylabel('MEG data (fT/cm)')
-pl.subplot(3, 1, 2)
-pl.plot(times, 1e13*data[1:306:3,:].T)
-pl.ylim([-200, 200])
-pl.title('Planar Gradiometers 2')
-pl.xlabel('time (ms)')
-pl.ylabel('MEG data (fT/cm)')
-pl.subplot(3, 1, 3)
-pl.plot(times, 1e15*data[2:306:3,:].T)
-pl.ylim([-600, 600])
-pl.title('Magnetometers')
-pl.xlabel('time (ms)')
-pl.ylabel('MEG data (fT)')
-pl.subplots_adjust(0.175, 0.08, 0.94, 0.94, 0.2, 0.63)
-pl.show()
+picks = fiff.pick_types(evoked.info, meg=True, eeg=True,
+                        exclude=evoked.info['bads']) # Pick channels to view
+plot_evoked(evoked, picks=picks)
diff --git a/mne/fiff/pick.py b/mne/fiff/pick.py
index 894762a..c7df02e 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -8,6 +8,33 @@ from copy import copy
 import numpy as np
 from .constants import FIFF
 
+def channel_type(info, idx):
+    """Get channel type
+
+    Parameters
+    ----------
+    info : dict
+        Measurement info
+    idx : int
+        Index of channel
+
+    Returns
+    -------
+    type : 'grad' | 'mag' | 'eeg' | 'stim'
+        Type of channel
+    """
+
+    kind = info['chs'][idx].kind
+    if (kind == FIFF.FIFFV_MEG_CH or kind == FIFF.FIFFV_REF_MEG_CH):
+        if info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_T_M:
+            return 'grad'
+        elif info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_T:
+            return 'mag'
+    elif kind == FIFF.FIFFV_EEG_CH:
+        return 'eeg'
+    elif kind == FIFF.FIFFV_STIM_CH:
+        return 'stim'
+
 
 def pick_channels(ch_names, include, exclude):
     """Pick channels by names
diff --git a/mne/viz.py b/mne/viz.py
index 6fbf190..ea5c231 100644
--- a/mne/viz.py
+++ b/mne/viz.py
@@ -6,6 +6,7 @@
 # License: Simplified BSD
 
 import pylab as pl
+from .fiff.pick import channel_type
 
 
 def plot_topo(evoked, layout):
@@ -27,3 +28,42 @@ def plot_topo(evoked, layout):
 
     pl.rcParams['axes.edgecolor'] = 'k'
 
+def plot_evoked(evoked, picks=None):
+    """Plot evoked data
+
+    Parameters
+    ----------
+    evoked : instance of Evoked
+        The evoked data
+    picks : None | array-like of int
+        The indices of channels to plot. If None show all.
+    """
+    pl.clf()
+    if picks is None:
+        picks = range(evoked.info['nchan'])
+    types = [channel_type(evoked.info, idx) for idx in picks]
+    n_channel_types = 0
+    channel_types = []
+    for t in ['eeg', 'grad', 'mag']:
+        if t in types:
+            n_channel_types += 1
+            channel_types.append(t)
+
+    counter = 1
+    times = 1e3 * evoked.times # time in miliseconds
+    for t, scaling, name, unit in zip(['eeg', 'grad', 'mag'],
+                           [1e6, 1e13, 1e15],
+                           ['EEG', 'Gradiometers', 'Magnetometers'],
+                           ['uV', 'fT/cm', 'fT']):
+        idx = [picks[i] for i in range(len(picks)) if types[i] is t]
+        if len(idx) > 0:
+            pl.subplot(n_channel_types, 1, counter)
+            pl.plot(times, scaling*evoked.data[idx,:].T)
+            pl.title(name)
+            pl.xlabel('time (ms)')
+            counter += 1
+            pl.ylabel('data (%s)' % unit)
+
+    pl.subplots_adjust(0.175, 0.08, 0.94, 0.94, 0.2, 0.63)
+    pl.show()
+

-- 
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