[med-svn] [python-mne] 92/376: first attempt to extract signal in ROIs with label files
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:22:15 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 208cd1b5d86410b6d9eb2ac6ee2673b083855fa4
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Sun Feb 20 21:37:32 2011 -0500
first attempt to extract signal in ROIs with label files
---
examples/plot_roi_label_activations.py | 32 +++++++++++++++
mne/__init__.py | 1 +
mne/label.py | 75 ++++++++++++++++++++++++++++++++++
3 files changed, 108 insertions(+)
diff --git a/examples/plot_roi_label_activations.py b/examples/plot_roi_label_activations.py
new file mode 100644
index 0000000..b8ca654
--- /dev/null
+++ b/examples/plot_roi_label_activations.py
@@ -0,0 +1,32 @@
+"""
+====================================================
+Extracting the time series of activations in a label
+====================================================
+
+
+"""
+# Author: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
+#
+# License: BSD (3-clause)
+
+print __doc__
+
+import os
+import numpy as np
+import mne
+from mne.datasets import sample
+
+data_path = sample.data_path('.')
+stc_fname = data_path + '/MEG/sample/sample_audvis-meg-lh.stc'
+label_fname = data_path + '/subjects/sample/label/lh.BA1.label'
+
+values, times, vertices = mne.label_time_courses(label_fname, stc_fname)
+
+print "Number of vertices : %d" % len(vertices)
+
+# View source activations
+import pylab as pl
+pl.plot(times, values.T)
+pl.xlabel('time (ms)')
+pl.ylabel('Source amplitude')
+pl.show()
diff --git a/mne/__init__.py b/mne/__init__.py
index f6e87f4..9f2fef4 100644
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -8,4 +8,5 @@ from .bem_surfaces import read_bem_surfaces
from .inverse import read_inverse_operator, compute_inverse
from .epochs import read_epochs
from .tfr import time_frequency
+from .label import label_time_courses, read_label
import fiff
diff --git a/mne/label.py b/mne/label.py
new file mode 100644
index 0000000..d46ad48
--- /dev/null
+++ b/mne/label.py
@@ -0,0 +1,75 @@
+import numpy as np
+
+from .stc import read_stc
+
+
+def read_label(filename):
+ """Read FreeSurfer Label file
+
+ Parameters
+ ----------
+ filename : string
+ Path to label file.
+
+ Returns
+ -------
+ label : dict
+ Label dictionaries with keys:
+ comment comment from the first line of the label file
+ vertices vertex indices (0 based, column 1)
+ pos locations in meters (columns 2 - 4 divided by 1000)
+ values values at the vertices (column 5)
+
+ """
+ fid = open(filename, 'r')
+ comment = fid.readline().replace('\n', '')
+ nv = int(fid.readline())
+ data = np.empty((5, nv))
+ for i, line in enumerate(fid):
+ data[:, i] = line.split()
+
+ label = dict()
+ label['comment'] = comment[1:]
+ label['vertices'] = np.array(data[0], dtype=np.int32)
+ label['pos'] = 1e-3 * data[1:4].T
+ label['values'] = data[4]
+ fid.close()
+
+ return label
+
+
+def label_time_courses(labelfile, stcfile):
+ """Extract the time courses corresponding to a label file from an stc file
+
+ Parameters
+ ----------
+ labelfile : string
+ Path to the label file
+
+ stcfile : string
+ Path to the stc file. The name of the stc file (must be on the
+ same subject and hemisphere as the stc file)
+
+ Returns
+ -------
+ values : 2d array
+ The time courses
+ times : 1d array
+ The time points
+ vertices : array
+ The indices of the vertices corresponding to the time points
+ """
+ stc = read_stc(stcfile)
+ lab = read_label(labelfile)
+
+ vertices = np.intersect1d(stc['vertices'], lab['vertices'])
+ idx = [k for k in range(len(stc['vertices']))
+ if stc['vertices'][k] in vertices]
+
+ if len(vertices) == 0:
+ raise ValueError, 'No vertices match the label in the stc file'
+
+ values = stc['data'][idx]
+ times = stc.tmin + stc.tstep * np.arange(stc.data.shape[1])
+
+ return values, times, vertices
--
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