This is an automated email from the git hooks/post-receive script. yoh pushed a commit to tag 0.4 in repository python-mne.
commit 7befeb085eb9140c3f8624e2ece4c1979ccd1e07 Author: Martin Luessi <[email protected]> Date: Fri Dec 2 10:38:51 2011 -0500 ENH: support loading of .w files for SourceEstimate --- mne/source_estimate.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 1f6a315..089df7e 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -103,10 +103,64 @@ def write_stc(filename, tmin, tstep, vertices, data): fid.close() +def _read_3(fid): + """ Read 3 byte integer from file + """ + data = np.fromfile(fid, dtype=np.uint8, count=3).astype(np.int32) + + out = np.left_shift(data[0], 16) + np.left_shift(data[1], 8) + data[2] + + return out + + +def read_w(filename): + """Read a w file + + w files contain activations or source reconstructions for a single time + point + + Parameters + ---------- + filename: string + The name of the w file + + Returns + ------- + data: dict + The w structure. It has the following keys: + vertices vertex indices (0 based) + data The data matrix (nvert) + """ + fid = open(filename, 'rb') + + # skip first 2 bytes + fid.read(2) + + # read number of vertices/sources (3 byte integer) + vertices_n = int(_read_3(fid)) + + vertices = np.zeros((vertices_n), dtype=np.int32) + data = np.zeros((vertices_n), dtype=np.float32) + + # read the data + for i in range(vertices_n): + vertices[i] = _read_3(fid) + data[i] = np.fromfile(fid, dtype=">f4", count=1) + + w = dict() + w['vertices'] = vertices + w['data'] = data + + # close the file + fid.close() + return w + + class SourceEstimate(object): """SourceEstimate container - Can be saved and loaded from .stc files + Can be saved and loaded from .stc files. Loading of .w files is supported + as well. Attributes ---------- @@ -127,9 +181,9 @@ class SourceEstimate(object): self.times = self.tmin + (self.tstep * np.arange(self.data.shape[1])) self.vertno = [vl['vertices']] - else: # surface source spaces - if fname.endswith('-lh.stc') or fname.endswith('-rh.stc'): - fname = fname[:-7] + elif fname.endswith('-lh.stc') or fname.endswith('-rh.stc'): + # stc file with surface source spaces + fname = fname[:-7] lh = read_stc(fname + '-lh.stc') rh = read_stc(fname + '-rh.stc') self.data = np.r_[lh['data'], rh['data']] @@ -140,6 +194,20 @@ class SourceEstimate(object): self.times = self.tmin + (self.tstep * np.arange(self.data.shape[1])) self.vertno = [lh['vertices'], rh['vertices']] + elif fname.endswith('-lh.w') or fname.endswith('-rh.w'): + # w file with surface source spaces + fname = fname[:-5] + lh = read_w(fname + '-lh.w') + rh = read_w(fname + '-rh.w') + self.data = np.atleast_2d(np.r_[lh['data'], rh['data']]).T + + # w files only have a single time point + self.tmin = 0.0 + self.tstep = 1.0 + self.times = np.array([0.0]) + self.vertno = [lh['vertices'], rh['vertices']] + else: + raise ValueError('file type not supported') def _init_times(self): """create self.times""" -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
