[med-svn] [python-mne] 192/376: ENH : refactoring IO of evoked, API : new crop method for evoked
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:22:35 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 19097ed6b0ed6b2ddb8a3edeac11d79739c28303
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Tue Apr 12 11:50:05 2011 -0400
ENH : refactoring IO of evoked, API : new crop method for evoked
---
examples/plot_read_evoked.py | 2 -
mne/fiff/evoked.py | 240 +++++++++++++-----------------------------
mne/fiff/meas_info.py | 83 ++++++++++++++-
mne/fiff/proj.py | 2 +-
mne/fiff/tag.py | 38 +++----
mne/fiff/tests/test_evoked.py | 4 +-
mne/fiff/write.py | 207 +++++++-----------------------------
7 files changed, 212 insertions(+), 364 deletions(-)
diff --git a/examples/plot_read_evoked.py b/examples/plot_read_evoked.py
index 0680186..8573406 100755
--- a/examples/plot_read_evoked.py
+++ b/examples/plot_read_evoked.py
@@ -21,8 +21,6 @@ fname = data_path + '/MEG/sample/sample_audvis-ave.fif'
# Reading
evoked = fiff.Evoked(fname, setno=0, baseline=(None, 0))
-evoked.save('test-ave.fif') # save file to disk
-
###############################################################################
# Show result
picks = fiff.pick_types(evoked.info, meg=True, eeg=True,
diff --git a/mne/fiff/evoked.py b/mne/fiff/evoked.py
index 9d2871e..915c866 100755
--- a/mne/fiff/evoked.py
+++ b/mne/fiff/evoked.py
@@ -9,15 +9,11 @@ from .constants import FIFF
from .open import fiff_open
from .tag import read_tag
from .tree import dir_tree_find
-from .meas_info import read_meas_info
+from .meas_info import read_meas_info, write_meas_info
-from .tree import copy_tree
-from .write import start_file, start_block, end_file, end_block, write_id, \
- write_float, write_int, write_coord_trans, write_ch_info, \
- write_dig_point, write_name_list, write_string, \
- write_float_matrix
-from .proj import write_proj
-from .ctf import write_ctf_comp
+from .write import start_file, start_block, end_file, end_block, \
+ write_int, write_string, write_float_matrix, \
+ write_id
class Evoked(object):
@@ -47,7 +43,7 @@ class Evoked(object):
Evoked response.
"""
- def __init__(self, fname, setno=0, baseline=None):
+ def __init__(self, fname, setno=None, baseline=None):
"""
Parameters
----------
@@ -56,15 +52,13 @@ class Evoked(object):
If None no data is loaded.
setno : int
- Dataset ID number
+ Dataset ID number. Optional if there is only one data set
+ in file.
"""
if fname is None:
return
- if setno < 0:
- raise ValueError, 'Data set selector must be positive'
-
print 'Reading %s ...' % fname
fid, tree, _ = fiff_open(fname)
@@ -80,63 +74,31 @@ class Evoked(object):
evoked_node = dir_tree_find(meas, FIFF.FIFFB_EVOKED)
if len(evoked_node) == 0:
- fid.close(fid)
+ fid.close()
raise ValueError, 'Could not find evoked data'
- # Identify the aspects
- naspect = 0
- is_smsh = None
- sets = []
- for k in range(len(evoked_node)):
- aspects_k = dir_tree_find(evoked_node[k], FIFF.FIFFB_ASPECT)
- set_k = dict(aspects=aspects_k,
- naspect=len(aspects_k))
- sets.append(set_k)
-
- if sets[k]['naspect'] > 0:
- if is_smsh is None:
- is_smsh = np.zeros((1, sets[k]['naspect']))
- else:
- is_smsh = np.r_[is_smsh, np.zeros((1, sets[k]['naspect']))]
- naspect += sets[k]['naspect']
-
- saspects = dir_tree_find(evoked_node[k], FIFF.FIFFB_SMSH_ASPECT)
- nsaspects = len(saspects)
- if nsaspects > 0:
- sets[k]['naspect'] += nsaspects
- sets[k]['naspect'] = [sets[k]['naspect'], saspects] # XXX : potential bug
- is_smsh = np.r_[is_smsh, np.ones(1, sets[k]['naspect'])]
- naspect += nsaspects
-
- print '\t%d evoked data sets containing a total of %d data aspects' \
- ' in %s' % (len(evoked_node), naspect, fname)
-
- if setno >= naspect or setno < 0:
+ if setno is None:
+ if len(evoked_node) > 1:
+ fid.close()
+ raise ValueError, '%d datasets present. setno parameter mush be set'
+ else:
+ setno = 0
+
+ if setno >= len(evoked_node) or setno < 0:
fid.close()
raise ValueError, 'Data set selector out of range'
- # Next locate the evoked data set
- p = 0
- goon = True
- for k in range(len(evoked_node)):
- for a in range(sets[k]['naspect']):
- if p == setno:
- my_evoked = evoked_node[k]
- my_aspect = sets[k]['aspects'][a]
- goon = False
- break
- p += 1
- if not goon:
- break
- else:
- # The desired data should have been found but better to check
- fid.close(fid)
- raise ValueError, 'Desired data set not found'
+ my_evoked = evoked_node[setno]
- # Now find the data in the evoked block
+ # Identify the aspects
+ aspects = dir_tree_find(my_evoked, FIFF.FIFFB_ASPECT)
+ if len(aspects) > 1:
+ print 'Multiple aspects found. Taking first one.'
+ my_aspect = aspects[0]
+
+ # Now find the data in the evoked block
nchan = 0
sfreq = -1
- q = 0
chs = []
comment = None
for k in range(my_evoked.nent):
@@ -147,20 +109,19 @@ class Evoked(object):
comment = tag.data
elif kind == FIFF.FIFF_FIRST_SAMPLE:
tag = read_tag(fid, pos)
- first = tag.data
+ first = int(tag.data)
elif kind == FIFF.FIFF_LAST_SAMPLE:
tag = read_tag(fid, pos)
- last = tag.data
+ last = int(tag.data)
elif kind == FIFF.FIFF_NCHAN:
tag = read_tag(fid, pos)
nchan = tag.data
elif kind == FIFF.FIFF_SFREQ:
tag = read_tag(fid, pos)
sfreq = tag.data
- elif kind ==FIFF.FIFF_CH_INFO:
+ elif kind == FIFF.FIFF_CH_INFO:
tag = read_tag(fid, pos)
chs.append(tag.data)
- q += 1
if comment is None:
comment = 'No comment'
@@ -177,8 +138,8 @@ class Evoked(object):
raise ValueError, 'Number of channels and number of channel ' \
'definitions are different'
- info.chs = chs
- info.nchan = nchan
+ info['chs'] = chs
+ info['nchan'] = nchan
print '\tFound channel information in evoked data. nchan = %d' % nchan
if sfreq > 0:
info['sfreq'] = sfreq
@@ -201,15 +162,15 @@ class Evoked(object):
comment = tag.data
elif kind == FIFF.FIFF_ASPECT_KIND:
tag = read_tag(fid, pos)
- aspect_kind = tag.data
+ aspect_kind = int(tag.data)
elif kind == FIFF.FIFF_NAVE:
tag = read_tag(fid, pos)
- nave = tag.data
+ nave = int(tag.data)
elif kind == FIFF.FIFF_EPOCH:
tag = read_tag(fid, pos)
epoch.append(tag)
- print '\t\tnave = %d aspect type = %d' % (nave, aspect_kind)
+ print '\t\tnave = %d - aspect type = %d' % (nave, aspect_kind)
nepoch = len(epoch)
if nepoch != 1 and nepoch != info.nchan:
@@ -218,14 +179,14 @@ class Evoked(object):
'(nepoch = %d nchan = %d)' % (nepoch, info.nchan)
if nepoch == 1:
- # Only one epoch
+ # Only one epoch
all_data = epoch[0].data
- # May need a transpose if the number of channels is one
+ # May need a transpose if the number of channels is one
if all_data.shape[1] == 1 and info.nchan == 1:
all_data = all_data.T
else:
- # Put the old style epochs together
+ # Put the old style epochs together
all_data = epoch[0].data.T
for k in range(1, nepoch):
all_data = np.r_[all_data, epoch[k].data.T]
@@ -235,11 +196,11 @@ class Evoked(object):
raise ValueError, 'Incorrect number of samples (%d instead of %d)' % (
all_data.shape[1], nsamp)
- # Calibrate
+ # Calibrate
cals = np.array([info['chs'][k].cal for k in range(info['nchan'])])
- all_data = np.dot(np.diag(cals.ravel()), all_data)
+ all_data = np.dot(np.diag(cals.ravel()), all_data) # XXX : can be better
- times = np.arange(first, last+1, dtype=np.float) / info['sfreq']
+ times = np.arange(first, last + 1, dtype=np.float) / info['sfreq']
# Run baseline correction
if baseline is not None:
@@ -281,112 +242,42 @@ class Evoked(object):
# Create the file and save the essentials
fid = start_file(fname)
+
start_block(fid, FIFF.FIFFB_MEAS)
write_id(fid, FIFF.FIFF_BLOCK_ID)
if self.info['meas_id'] is not None:
write_id(fid, FIFF.FIFF_PARENT_BLOCK_ID, self.info['meas_id'])
- # Measurement info
- start_block(fid, FIFF.FIFFB_MEAS_INFO)
-
- # Blocks from the original
- blocks = [FIFF.FIFFB_SUBJECT, FIFF.FIFFB_HPI_MEAS, FIFF.FIFFB_HPI_RESULT,
- FIFF.FIFFB_ISOTRAK, FIFF.FIFFB_PROCESSING_HISTORY]
-
- have_hpi_result = False
- have_isotrak = False
- if len(blocks) > 0 and 'filename' in self.info and \
- self.info['filename'] is not None:
- fid2, tree, _ = fiff_open(self.info['filename'])
- for block in blocks:
- nodes = dir_tree_find(tree, block)
- copy_tree(fid2, tree['id'], nodes, fid)
- if block == FIFF.FIFFB_HPI_RESULT and len(nodes) > 0:
- have_hpi_result = True
- if block == FIFF.FIFFB_ISOTRAK and len(nodes) > 0:
- have_isotrak = True
- fid2.close()
-
- # General
- write_float(fid, FIFF.FIFF_SFREQ, self.info['sfreq'])
- write_float(fid, FIFF.FIFF_HIGHPASS, self.info['highpass'])
- write_float(fid, FIFF.FIFF_LOWPASS, self.info['lowpass'])
- write_int(fid, FIFF.FIFF_NCHAN, self.info['nchan'])
- if self.info['meas_date'] is not None:
- write_int(fid, FIFF.FIFF_MEAS_DATE, self.info['meas_date'])
-
- # Coordinate transformations if the HPI result block was not there
- if not have_hpi_result:
- if self.info['dev_head_t'] is not None:
- write_coord_trans(fid, self.info['dev_head_t'])
-
- if self.info['ctf_head_t'] is not None:
- write_coord_trans(fid, self.info['ctf_head_t'])
-
- # Channel information
- for k in range(self.info['nchan']):
- # Scan numbers may have been messed up
- self.info['chs'][k]['scanno'] = k
- write_ch_info(fid, self.info['chs'][k])
-
- # Polhemus data
- if self.info['dig'] is not None and not have_isotrak:
- start_block(fid, FIFF.FIFFB_ISOTRAK)
- for d in self.info['dig']:
- write_dig_point(fid, d)
-
- end_block(fid, FIFF.FIFFB_ISOTRAK)
-
- # Projectors
- write_proj(fid, self.info['projs'])
-
- # CTF compensation info
- write_ctf_comp(fid, self.info['comps'])
-
- # Bad channels
- if len(self.info['bads']) > 0:
- start_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS)
- write_name_list(fid, FIFF.FIFF_MNE_CH_NAME_LIST, self.info['bads'])
- end_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS)
-
- end_block(fid, FIFF.FIFFB_MEAS_INFO)
+ # Write measurement info
+ write_meas_info(fid, self.info)
# One or more evoked data sets
start_block(fid, FIFF.FIFFB_PROCESSED_DATA)
- data_evoked = self.data
- if not isinstance(data_evoked, list):
- data_evoked = [data_evoked]
+ start_block(fid, FIFF.FIFFB_EVOKED)
- for evoked in data_evoked:
- start_block(fid, FIFF.FIFFB_EVOKED)
+ # Comment is optional
+ if len(self.comment) > 0:
+ write_string(fid, FIFF.FIFF_COMMENT, self.comment)
- # Comment is optional
- if len(self.comment) > 0:
- write_string(fid, FIFF.FIFF_COMMENT, self.comment)
+ # First and last sample
+ write_int(fid, FIFF.FIFF_FIRST_SAMPLE, self.first)
+ write_int(fid, FIFF.FIFF_LAST_SAMPLE, self.last)
- # First and last sample
- write_int(fid, FIFF.FIFF_FIRST_SAMPLE, self.first)
- write_int(fid, FIFF.FIFF_LAST_SAMPLE, self.last)
+ # The epoch itself
+ start_block(fid, FIFF.FIFFB_ASPECT)
- # The epoch itself
- start_block(fid, FIFF.FIFFB_ASPECT)
+ write_int(fid, FIFF.FIFF_ASPECT_KIND, self.aspect_kind)
+ write_int(fid, FIFF.FIFF_NAVE, self.nave)
- write_int(fid, FIFF.FIFF_ASPECT_KIND, self.aspect_kind)
- write_int(fid, FIFF.FIFF_NAVE, self.nave)
-
- decal = np.zeros((self.info['nchan'], self.info['nchan']))
- for k in range(self.info['nchan']): # XXX : can be improved
- decal[k, k] = 1.0 / self.info['chs'][k]['cal']
-
- write_float_matrix(fid, FIFF.FIFF_EPOCH,
- np.dot(decal, evoked))
- end_block(fid, FIFF.FIFFB_ASPECT)
- end_block(fid, FIFF.FIFFB_EVOKED)
+ decal = np.zeros((self.info['nchan'], self.info['nchan']))
+ for k in range(self.info['nchan']): # XXX : can be improved
+ decal[k, k] = 1.0 / self.info['chs'][k]['cal']
+ write_float_matrix(fid, FIFF.FIFF_EPOCH, np.dot(decal, self.data))
+ end_block(fid, FIFF.FIFFB_ASPECT)
+ end_block(fid, FIFF.FIFFB_EVOKED)
end_block(fid, FIFF.FIFFB_PROCESSED_DATA)
-
end_block(fid, FIFF.FIFFB_MEAS)
-
end_file(fid)
def __repr__(self):
@@ -400,6 +291,21 @@ class Evoked(object):
def ch_names(self):
return self.info['ch_names']
+ def crop(self, tmin=None, tmax=None):
+ """Crop data to a given time interval
+ """
+ times = self.times
+ mask = np.ones(len(times), dtype=np.bool)
+ if tmin is not None:
+ mask = mask & (times >= tmin)
+ if tmax is not None:
+ mask = mask & (times <= tmax)
+ self.times = times[mask]
+ self.first = - int(np.sum(self.times < 0))
+ self.last = int(np.sum(self.times > 0))
+ self.data = self.data[:,mask]
+
+
def read_evoked(fname, setno=0, baseline=None):
"""Read an evoked dataset
diff --git a/mne/fiff/meas_info.py b/mne/fiff/meas_info.py
index 4aaed58..61c4b29 100755
--- a/mne/fiff/meas_info.py
+++ b/mne/fiff/meas_info.py
@@ -5,13 +5,18 @@
import numpy as np
-from .tree import dir_tree_find
+from .open import fiff_open
+from .tree import dir_tree_find, copy_tree
from .constants import FIFF
from .tag import read_tag
-from .proj import read_proj
-from .ctf import read_ctf_comp
+from .proj import read_proj, write_proj
+from .ctf import read_ctf_comp, write_ctf_comp
from .channels import _read_bad_channels
+from .write import start_block, end_block, write_id, \
+ write_float, write_int, write_coord_trans, write_ch_info, \
+ write_dig_point, write_name_list
+
def read_meas_info(fid, tree):
"""Read the measurement info
@@ -60,7 +65,7 @@ def read_meas_info(fid, tree):
p = 0
for k in range(meas_info.nent):
kind = meas_info.directory[k].kind
- pos = meas_info.directory[k].pos
+ pos = meas_info.directory[k].pos
if kind == FIFF.FIFF_NCHAN:
tag = read_tag(fid, pos)
nchan = int(tag.data)
@@ -225,3 +230,73 @@ def read_meas_info(fid, tree):
return info, meas
+
+def write_meas_info(fid, info):
+ """Write measurement info in fif file."""
+
+ # Measurement info
+ start_block(fid, FIFF.FIFFB_MEAS_INFO)
+
+ # Blocks from the original
+ blocks = [FIFF.FIFFB_SUBJECT, FIFF.FIFFB_HPI_MEAS, FIFF.FIFFB_HPI_RESULT,
+ FIFF.FIFFB_ISOTRAK, FIFF.FIFFB_PROCESSING_HISTORY]
+
+ have_hpi_result = False
+ have_isotrak = False
+
+ if len(blocks) > 0 and 'filename' in info and \
+ info['filename'] is not None:
+ fid2, tree, _ = fiff_open(info['filename'])
+ for block in blocks:
+ nodes = dir_tree_find(tree, block)
+ copy_tree(fid2, tree['id'], nodes, fid)
+ if block == FIFF.FIFFB_HPI_RESULT and len(nodes) > 0:
+ have_hpi_result = True
+ if block == FIFF.FIFFB_ISOTRAK and len(nodes) > 0:
+ have_isotrak = True
+ fid2.close()
+
+ # General
+ write_float(fid, FIFF.FIFF_SFREQ, info['sfreq'])
+ write_float(fid, FIFF.FIFF_HIGHPASS, info['highpass'])
+ write_float(fid, FIFF.FIFF_LOWPASS, info['lowpass'])
+ write_int(fid, FIFF.FIFF_NCHAN, info['nchan'])
+ if info['meas_date'] is not None:
+ write_int(fid, FIFF.FIFF_MEAS_DATE, info['meas_date'])
+
+ # Coordinate transformations if the HPI result block was not there
+ if not have_hpi_result:
+ if info['dev_head_t'] is not None:
+ write_coord_trans(fid, info['dev_head_t'])
+
+ if info['ctf_head_t'] is not None:
+ write_coord_trans(fid, info['ctf_head_t'])
+
+ # Channel information
+ for k in range(info['nchan']):
+ # Scan numbers may have been messed up
+ info['chs'][k]['scanno'] = k + 1
+ write_ch_info(fid, info['chs'][k])
+
+ # Polhemus data
+ if info['dig'] is not None and not have_isotrak:
+ start_block(fid, FIFF.FIFFB_ISOTRAK)
+ for d in info['dig']:
+ write_dig_point(fid, d)
+
+ end_block(fid, FIFF.FIFFB_ISOTRAK)
+
+ # Projectors
+ write_proj(fid, info['projs'])
+
+ # CTF compensation info
+ write_ctf_comp(fid, info['comps'])
+
+ # Bad channels
+ if len(info['bads']) > 0:
+ start_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS)
+ write_name_list(fid, FIFF.FIFF_MNE_CH_NAME_LIST, info['bads'])
+ end_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS)
+
+ end_block(fid, FIFF.FIFFB_MEAS_INFO)
+
diff --git a/mne/fiff/proj.py b/mne/fiff/proj.py
index 5483346..920ef76 100755
--- a/mne/fiff/proj.py
+++ b/mne/fiff/proj.py
@@ -70,7 +70,7 @@ def read_proj(fid, node):
tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_KIND)
if tag is not None:
- kind = tag.data
+ kind = int(tag.data)
else:
raise ValueError, 'Projection item kind missing'
diff --git a/mne/fiff/tag.py b/mne/fiff/tag.py
index b7bd7ce..de91909 100755
--- a/mne/fiff/tag.py
+++ b/mne/fiff/tag.py
@@ -159,7 +159,7 @@ def read_tag(fid, pos=None):
# Find dimensions and return to the beginning of tag data
pos = fid.tell()
fid.seek(tag.size-4, 1)
- ndim = np.fromfile(fid, dtype='>i', count=1)
+ ndim = int(np.fromfile(fid, dtype='>i', count=1))
fid.seek(-(ndim+2)*4, 1)
dims = np.fromfile(fid, dtype='>i', count=ndim+1)
if ndim != 2:
@@ -222,21 +222,21 @@ def read_tag(fid, pos=None):
#
elif tag.type == FIFF.FIFFT_ID_STRUCT:
tag.data = dict()
- tag.data['version'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['version'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['version'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['version'] = int(np.fromfile(fid, dtype=">i4", count=1))
tag.data['machid'] = np.fromfile(fid, dtype=">i4", count=2)
- tag.data['secs'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['usecs'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['secs'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['usecs'] = int(np.fromfile(fid, dtype=">i4", count=1))
elif tag.type == FIFF.FIFFT_DIG_POINT_STRUCT:
tag.data = dict()
- tag.data['kind'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['ident'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['kind'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['ident'] = int(np.fromfile(fid, dtype=">i4", count=1))
tag.data['r'] = np.fromfile(fid, dtype=">i4", count=3)
tag.data['coord_frame'] = 0
elif tag.type == FIFF.FIFFT_COORD_TRANS_STRUCT:
tag.data = Bunch()
- tag.data['from_'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['to'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['from_'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['to'] = int(np.fromfile(fid, dtype=">i4", count=1))
rot = np.fromfile(fid, dtype=">f4", count=9).reshape(3, 3)
move = np.fromfile(fid, dtype=">f4", count=3)
tag.data['trans'] = np.r_[np.c_[rot, move],
@@ -248,12 +248,12 @@ def read_tag(fid, pos=None):
fid.seek(12*4, 1)
elif tag.type == FIFF.FIFFT_CH_INFO_STRUCT:
tag.data = Bunch()
- tag.data['scanno'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['logno'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['kind'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['range'] = np.fromfile(fid, dtype=">f4", count=1)
- tag.data['cal'] = np.fromfile(fid, dtype=">f4", count=1)
- tag.data['coil_type'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['scanno'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['logno'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['kind'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['range'] = float(np.fromfile(fid, dtype=">f4", count=1))
+ tag.data['cal'] = float(np.fromfile(fid, dtype=">f4", count=1))
+ tag.data['coil_type'] = int(np.fromfile(fid, dtype=">i4", count=1))
#
# Read the coil coordinate system definition
#
@@ -280,8 +280,8 @@ def read_tag(fid, pos=None):
#
# Unit and exponent
#
- tag.data['unit'] = np.fromfile(fid, dtype=">i4", count=1)
- tag.data['unit_mul'] = np.fromfile(fid, dtype=">i4", count=1)
+ tag.data['unit'] = int(np.fromfile(fid, dtype=">i4", count=1))
+ tag.data['unit_mul'] = int(np.fromfile(fid, dtype=">i4", count=1))
#
# Handle the channel name
#
@@ -293,8 +293,8 @@ def read_tag(fid, pos=None):
ch_name[:np.where(ch_name == '')[0][0]])
elif tag.type == FIFF.FIFFT_OLD_PACK:
- offset = np.fromfile(fid, dtype=">f4", count=1)
- scale = np.fromfile(fid, dtype=">f4", count=1)
+ offset = float(np.fromfile(fid, dtype=">f4", count=1))
+ scale = float(np.fromfile(fid, dtype=">f4", count=1))
tag.data = np.fromfile(fid, dtype=">h2", count=(tag.size-8)/2)
tag.data = scale*tag.data + offset
elif tag.type == FIFF.FIFFT_DIR_ENTRY_STRUCT:
diff --git a/mne/fiff/tests/test_evoked.py b/mne/fiff/tests/test_evoked.py
index ac0f833..d14bbcb 100755
--- a/mne/fiff/tests/test_evoked.py
+++ b/mne/fiff/tests/test_evoked.py
@@ -7,10 +7,12 @@ from .. import read_evoked, write_evoked
fname = op.join(op.dirname(__file__), 'data', 'test-ave.fif')
def test_io_evoked():
- """Test IO for noise covariance matrices
+ """Test IO for evoked data
"""
ave = read_evoked(fname)
+ ave.crop(tmin=0)
+
write_evoked('evoked.fif', ave)
ave2 = read_evoked('evoked.fif')
diff --git a/mne/fiff/write.py b/mne/fiff/write.py
index 254a8a3..12b24fb 100755
--- a/mne/fiff/write.py
+++ b/mne/fiff/write.py
@@ -25,100 +25,48 @@ def _write(fid, data, kind, data_size, FIFFT_TYPE, dtype):
def write_int(fid, kind, data):
- """
- %
- % fiff_write_int(fid,kind,data)
- %
- % Writes a 32-bit integer tag to a fif file
- %
- % fid An open fif file descriptor
- % kind Tag kind
- % data The integers to use as data
- %
- """
+ """Writes a 32-bit integer tag to a fif file"""
FIFFT_INT = 3
data_size = 4
+ data = np.array(data, dtype='>i4')
_write(fid, data, kind, data_size, FIFFT_INT, '>i4')
def write_double(fid, kind, data):
- """
- %
- % fiff_write_int(fid,kind,data)
- %
- % Writes a double-precision floating point tag to a fif file
- %
- % fid An open fif file descriptor
- % kind Tag kind
- % data The data
- %
- """
+ """Writes a double-precision floating point tag to a fif file"""
FIFFT_DOUBLE = 5
data_size = 8
+ data = np.array(data, dtype='>f8')
_write(fid, data, kind, data_size, FIFFT_DOUBLE, '>f8')
def write_float(fid, kind, data):
- """
- %
- % fiff_write_float(fid,kind,data)
- %
- % Writes a single-precision floating point tag to a fif file
- %
- % fid An open fif file descriptor
- % kind Tag kind
- % data The data
- %
- """
+ """Writes a single-precision floating point tag to a fif file"""
FIFFT_FLOAT = 4
data_size = 4
+ data = np.array(data, dtype='>f4')
_write(fid, data, kind, data_size, FIFFT_FLOAT, '>f4')
def write_string(fid, kind, data):
- """
- %
- % fiff_write_string(fid,kind,data)
- %
- % Writes a string tag
- %
- % fid An open fif file descriptor
- % kind The tag kind
- % data The string data to write
- %
- """
+ """Writes a string tag"""
FIFFT_STRING = 10
data_size = 1
- _write(fid, data, kind, data_size, FIFFT_STRING, '>c')
+ _write(fid, str(data), kind, data_size, FIFFT_STRING, '>c')
def write_name_list(fid, kind, data):
- """
- %
- % fiff_write_name_list(fid,kind,mat)
- %
- % Writes a colon-separated list of names
- %
- % fid An open fif file descriptor
- % kind The tag kind
- % data An array of names to create the list from
- %
+ """Writes a colon-separated list of names
+
+ Parameters
+ ----------
+ data : list of strings
"""
write_string(fid, kind, ':'.join(data))
def write_float_matrix(fid, kind, mat):
- """
- %
- % fiff_write_float_matrix(fid,kind,mat)
- %
- % Writes a single-precision floating-point matrix tag
- %
- % fid An open fif file descriptor
- % kind The tag kind
- % mat The data matrix
- %
- """
+ """Writes a single-precision floating-point matrix tag"""
FIFFT_FLOAT = 4
FIFFT_MATRIX = 1 << 30
FIFFT_MATRIX_FLOAT = FIFFT_FLOAT | FIFFT_MATRIX
@@ -139,21 +87,8 @@ def write_float_matrix(fid, kind, mat):
fid.write(np.array(dims, dtype='>i4').tostring())
-
def write_id(fid, kind, id_=None):
- """
- %
- % fiff_write_id(fid, kind, id)
- %
- % Writes fiff id
- %
- % fid An open fif file descriptor
- % kind The tag kind
- % id The id to write
- %
- % If the id argument is missing it will be generated here
- %
- """
+ """Writes fiff id"""
if id_ is None:
id_ = dict()
@@ -165,7 +100,7 @@ def write_id(fid, kind, id_=None):
FIFFT_ID_STRUCT = 31
FIFFV_NEXT_SEQ = 0
- data_size = 5*4 # The id comprises five integers
+ data_size = 5 * 4 # The id comprises five integers
fid.write(np.array(kind, dtype='>i4').tostring())
fid.write(np.array(FIFFT_ID_STRUCT, dtype='>i4').tostring())
fid.write(np.array(data_size, dtype='>i4').tostring())
@@ -182,47 +117,27 @@ def write_id(fid, kind, id_=None):
def start_block(fid, kind):
- """
- %
- % fiff_start_block(fid,kind)
- %
- % Writes a FIFF_BLOCK_START tag
- %
- % fid An open fif file descriptor
- % kind The block kind to start
- %
- """
+ """Writes a FIFF_BLOCK_START tag"""
FIFF_BLOCK_START = 104
write_int(fid, FIFF_BLOCK_START, kind)
def end_block(fid, kind):
- """
- %
- % fiff_end_block(fid,kind)
- %
- % Writes a FIFF_BLOCK_END tag
- %
- % fid An open fif file descriptor
- % kind The block kind to end
- %
- """
+ """Writes a FIFF_BLOCK_END tag"""
FIFF_BLOCK_END = 105
write_int(fid, FIFF_BLOCK_END, kind)
def start_file(name):
- """
- %
- % [fid] = fiff_start_file(name)
- %
- % Opens a fif file for writing and writes the compulsory header tags
- %
- % name The name of the file to open. It is recommended
- % that the name ends with .fif
- %
+ """Opens a fif file for writing and writes the compulsory header tags
+
+ Parameters
+ ----------
+ name : string
+ The name of the file to open. It is recommended
+ that the name ends with .fif
"""
fid = open(name, 'wb')
@@ -239,15 +154,7 @@ def start_file(name):
def end_file(fid):
- """
- %
- % fiff_end_file(fid)
- %
- % Writes the closing tags to a fif file and closes the file
- %
- % fid An open fif file descriptor
- %
- """
+ """Writes the closing tags to a fif file and closes the file"""
data_size = 0
fid.write(np.array(FIFF.FIFF_NOP, dtype='>i4').tostring())
fid.write(np.array(FIFF.FIFFT_VOID, dtype='>i4').tostring())
@@ -257,16 +164,7 @@ def end_file(fid):
def write_coord_trans(fid, trans):
- """
- #
- # fiff_write_coord_trans(fid,trans)
- #
- # Writes a coordinate transformation structure
- #
- # fid An open fif file descriptor
- # trans The coordinate transfomation structure
- #
- """
+ """Writes a coordinate transformation structure"""
FIFF_COORD_TRANS = 222
FIFFT_COORD_TRANS_STRUCT = 35
@@ -304,19 +202,7 @@ def write_coord_trans(fid, trans):
def write_ch_info(fid, ch):
- """
- %
- % fiff_write_ch_info(fid,ch)
- %
- % Writes a channel information record to a fif file
- %
- % fid An open fif file descriptor
- % ch The channel information structure to write
- %
- % The type, cal, unit, and pos members are explained in Table 9.5
- % of the MNE manual
- %
- """
+ """Writes a channel information record to a fif file"""
FIFF_CH_INFO = 203
FIFFT_CH_INFO_STRUCT = 30
@@ -343,7 +229,7 @@ def write_ch_info(fid, ch):
# fiff_char_t ch_name[16]; /*!< Descriptive name for the channel */
#} fiffChInfoRec,*fiffChInfo; /*!< Description of one channel */
- data_size = 4*13 + 4*7 + 16;
+ data_size = 4*13 + 4*7 + 16
fid.write(np.array(FIFF_CH_INFO, dtype='>i4').tostring())
fid.write(np.array(FIFFT_CH_INFO_STRUCT, dtype='>i4').tostring())
@@ -376,16 +262,7 @@ def write_ch_info(fid, ch):
def write_dig_point(fid, dig):
- """
- %
- % fiff_write_dig_point(fid,dig)
- %
- % Writes a digitizer data point into a fif file
- %
- % fid An open fif file descriptor
- % dig The point to write
- %
- """
+ """Writes a digitizer data point into a fif file"""
FIFF_DIG_POINT = 213
FIFFT_DIG_POINT_STRUCT = 33
@@ -413,31 +290,21 @@ def write_dig_point(fid, dig):
def write_named_matrix(fid, kind, mat):
- """
- %
- % fiff_write_named_matrix(fid,kind,mat)
- %
- % Writes a named single-precision floating-point matrix
- %
- % fid An open fif file descriptor
- % kind The tag kind to use for the data
- % mat The data matrix
- %
- """
- raise NotImplementedError, "CTF data processing is not supported yet"
+ """Writes a named single-precision floating-point matrix"""
+
+ raise NotImplementedError("CTF data processing is not supported yet")
# start_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
# write_int(fid, FIFF.FIFF_MNE_NROW, mat['nrow'])
# write_int(fid, FIFF.FIFF_MNE_NCOL, mat['ncol'])
- #
+ #
# if len(mat['row_names']) > 0:
# write_name_list(fid, FIFF.FIFF_MNE_ROW_NAMES, mat['row_names'])
- #
+ #
# if len(mat['col_names']) > 0:
# write_name_list(fid, FIFF.FIFF_MNE_COL_NAMES, mat['col_names'])
- #
+ #
# write_float_matrix(fid,kind, mat.data)
# end_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
- #
+ #
# return;
-
--
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