[med-svn] [python-mne] 193/376: API : change access to raw data to hide the first_samp mess API : rename cov estimation methods FIX : write of raw data FIX : some tests
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 e193348ea42d0701b430d3a108468ba4d5a0d147
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Tue Apr 12 15:28:46 2011 -0400
API : change access to raw data to hide the first_samp mess
API : rename cov estimation methods
FIX : write of raw data
FIX : some tests
---
examples/plot_estimate_covariance_matrix.py | 2 +-
examples/plot_read_and_write_raw_data.py | 12 ++---
examples/plot_whitened_evoked_data.py | 6 +--
mne/__init__.py | 2 +-
mne/cov.py | 15 ++++---
mne/epochs.py | 8 ++--
mne/event.py | 2 +
mne/fiff/raw.py | 70 +++++++++++++----------------
mne/fiff/tests/test_raw.py | 20 +++++++--
mne/fiff/write.py | 6 +--
mne/tests/test_cov.py | 8 ++--
11 files changed, 80 insertions(+), 71 deletions(-)
diff --git a/examples/plot_estimate_covariance_matrix.py b/examples/plot_estimate_covariance_matrix.py
index 7f595db..6b0c6f9 100755
--- a/examples/plot_estimate_covariance_matrix.py
+++ b/examples/plot_estimate_covariance_matrix.py
@@ -20,7 +20,7 @@ fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
raw = fiff.Raw(fname)
# Set up pick list: MEG + STI 014 - bad channels
-cov = mne.noise_covariance_segment(raw, reject=dict(eeg=40e-6, eog=150e-6))
+cov = mne.compute_raw_data_covariance(raw, reject=dict(eeg=40e-6, eog=150e-6))
print cov
###############################################################################
diff --git a/examples/plot_read_and_write_raw_data.py b/examples/plot_read_and_write_raw_data.py
index aca8e04..6a6b9e5 100755
--- a/examples/plot_read_and_write_raw_data.py
+++ b/examples/plot_read_and_write_raw_data.py
@@ -29,15 +29,15 @@ include = ['STI 014']
exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bad channels + 2 more
picks = fiff.pick_types(raw.info, meg=want_meg, eeg=want_eeg,
- stim=want_stim, include=include,
- exclude=exclude)
+ stim=want_stim, include=include,
+ exclude=exclude)
some_picks = picks[:5] # take 5 first
-start, stop = raw.time_to_index(100, 115) # 100 s to 115 s data segment
-data, times = raw[some_picks, start:stop]
+start, stop = raw.time_to_index(0, 15) # read the first 15s of data
+data, times = raw[some_picks, start:(stop+1)]
-raw.save('sample_audvis_meg_raw.fif', picks=picks, first_samp=start,
- last_samp=stop-1) # save MEG data in FIF file
+# save 150s of MEG data in FIF file
+raw.save('sample_audvis_meg_raw.fif', tmin=0, tmax=150, picks=picks)
###############################################################################
# Show MEG data
diff --git a/examples/plot_whitened_evoked_data.py b/examples/plot_whitened_evoked_data.py
index c1e5db4..cd88035 100755
--- a/examples/plot_whitened_evoked_data.py
+++ b/examples/plot_whitened_evoked_data.py
@@ -44,9 +44,9 @@ evoked = epochs.average() # average epochs and get an Evoked dataset.
cov = mne.Covariance(cov_fname)
# Whiten data
-W, ch_names = cov.whitener(evoked.info, pca=False) # get whitening matrix
-sel = mne.fiff.pick_channels(evoked.ch_names, include=ch_names) # channels id
-whitened_data = np.dot(W, evoked.data[sel]) # apply whitening
+whitener = cov.get_whitener(evoked.info, pca=False) # get whitening matrix
+sel = mne.fiff.pick_channels(evoked.ch_names, include=whitener.ch_names)
+whitened_data = np.dot(whitener.W, evoked.data[sel]) # apply whitening
###############################################################################
# Show result
diff --git a/mne/__init__.py b/mne/__init__.py
index c471729..47aba8d 100755
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -1,7 +1,7 @@
__version__ = '0.1.git'
from .cov import read_cov, write_cov, write_cov_file, Covariance, \
- noise_covariance_segment, noise_covariance
+ compute_raw_data_covariance, compute_covariance
from .event import read_events, write_events, find_events
from .forward import read_forward_solution
from .stc import read_stc, write_stc
diff --git a/mne/cov.py b/mne/cov.py
index b7c345c..a3b7da4 100755
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -338,7 +338,7 @@ def read_cov(fid, node, cov_kind):
###############################################################################
# Estimate from data
-def _estimate_noise_covariance_from_epochs(epochs, bmin, bmax, reject, flat,
+def _estimate_compute_covariance_from_epochs(epochs, bmin, bmax, reject, flat,
keep_sample_mean):
"""Estimate noise covariance matrix from epochs
"""
@@ -374,7 +374,7 @@ def _estimate_noise_covariance_from_epochs(epochs, bmin, bmax, reject, flat,
return data, n_samples, ch_names
-def noise_covariance_segment(raw, tmin=None, tmax=None, tstep=0.2,
+def compute_raw_data_covariance(raw, tmin=None, tmax=None, tstep=0.2,
reject=None, flat=None, keep_sample_mean=True):
"""Estimate noise covariance matrix from a continuous segment of raw data
@@ -418,8 +418,11 @@ def noise_covariance_segment(raw, tmin=None, tmax=None, tstep=0.2,
sfreq = raw.info['sfreq']
# Convert to samples
- start = raw.first_samp if tmin is None else int(floor(tmin * sfreq))
- stop = raw.last_samp if tmax is None else int(ceil(tmax * sfreq))
+ start = 0 if tmin is None else int(floor(tmin * sfreq))
+ if tmax is None:
+ stop = raw.last_samp - raw.first_samp
+ else:
+ stop = int(ceil(tmax * sfreq))
step = int(ceil(tstep * raw.info['sfreq']))
picks = pick_types(raw.info, meg=True, eeg=True, eog=True)
@@ -461,7 +464,7 @@ def noise_covariance_segment(raw, tmin=None, tmax=None, tstep=0.2,
return cov
-def noise_covariance(raw, events, event_ids, tmin, tmax,
+def compute_covariance(raw, events, event_ids, tmin, tmax,
bmin=None, bmax=None, reject=None, flat=None,
keep_sample_mean=True):
"""Estimate noise covariance matrix from raw file and events.
@@ -521,7 +524,7 @@ def noise_covariance(raw, events, event_ids, tmin, tmax,
print "Processing event : %d" % event_id
epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0))
- d, n, ch_names = _estimate_noise_covariance_from_epochs(epochs,
+ d, n, ch_names = _estimate_compute_covariance_from_epochs(epochs,
bmin=bmin, bmax=bmax, reject=reject, flat=flat,
keep_sample_mean=keep_sample_mean)
data += d
diff --git a/mne/epochs.py b/mne/epochs.py
index 982ca6a..1b0d629 100755
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -178,7 +178,6 @@ class Epochs(object):
self.picks = [self.picks[k] for k in idx]
# XXX : could maybe be factorized
- n_channels = self.info['nchan']
self.info['chs'] = [self.info['chs'][k] for k in idx]
self.info['ch_names'] = [self.info['ch_names'][k] for k in idx]
self.info['nchan'] = len(idx)
@@ -207,7 +206,8 @@ class Epochs(object):
event_samp = self.events[idx, 0]
# Read a data segment
- start = int(round(event_samp + self.tmin*sfreq))
+ first_samp = self.raw.first_samp
+ start = int(round(event_samp + self.tmin*sfreq)) - first_samp
stop = start + len(self.times)
epoch, _ = self.raw[self.picks, start:stop]
@@ -332,8 +332,8 @@ class Epochs(object):
evoked.comment = self.name
evoked.aspect_kind = np.array([100]) # for standard average file
evoked.nave = n_events
- evoked.first = - np.sum(self.times < 0)
- evoked.last = np.sum(self.times > 0)
+ evoked.first = - int(np.sum(self.times < 0))
+ evoked.last = int(np.sum(self.times > 0))
# dropping EOG, ECG and STIM channels. Keeping only data
data_picks = pick_types(evoked.info, meg=True, eeg=True,
diff --git a/mne/event.py b/mne/event.py
index fc5abd3..68cbb1c 100755
--- a/mne/event.py
+++ b/mne/event.py
@@ -97,6 +97,8 @@ def find_events(raw, stim_channel='STI 014'):
pick = pick_channels(raw.info['ch_names'], include=['STI 014'],
exclude=[])
+ if len(pick) == 0:
+ raise ValueError('No stim channel found to extract event triggers.')
data, times = raw[pick,:]
data = data.ravel()
idx = np.where(np.diff(data.ravel()) > 0)[0]
diff --git a/mne/fiff/raw.py b/mne/fiff/raw.py
index 4bc609f..4ee74b9 100755
--- a/mne/fiff/raw.py
+++ b/mne/fiff/raw.py
@@ -55,10 +55,10 @@ class Raw(dict):
if allow_maxshield:
raw_node = dir_tree_find(meas, FIFF.FIFFB_SMSH_RAW_DATA)
if len(raw_node) == 0:
- raise ValueError, 'No raw data in %s' % fname
+ raise ValueError('No raw data in %s' % fname)
else:
if len(raw_node) == 0:
- raise ValueError, 'No raw data in %s' % fname
+ raise ValueError('No raw data in %s' % fname)
if len(raw_node) == 1:
raw_node = raw_node[0]
@@ -82,7 +82,7 @@ class Raw(dict):
# Omit initial skip
if directory[first].kind == FIFF.FIFF_DATA_SKIP:
- # This first skip can be applied only after we know the buffer size
+ # This first skip can be applied only after we know the buffer size
tag = read_tag(fid, directory[first].pos)
first_skip = int(tag.data)
first += 1
@@ -109,8 +109,8 @@ class Raw(dict):
nsamp = ent.size / (4*nchan)
else:
fid.close()
- raise ValueError, 'Cannot handle data buffers of type %d' % (
- ent.type)
+ raise ValueError('Cannot handle data buffers of type %d' %
+ ent.type)
# Do we have an initial skip pending?
if first_skip > 0:
@@ -154,7 +154,6 @@ class Raw(dict):
self.fid = fid
self.info = info
-
def __getitem__(self, item):
"""getting raw data content with python slicing"""
if isinstance(item, tuple): # slicing required
@@ -173,15 +172,15 @@ class Raw(dict):
sel = None
start, stop, step = time_slice.start, time_slice.stop, \
time_slice.step
+ if start is None:
+ start = 0
if step is not None:
raise ValueError('step needs to be 1 : %d given' % step)
return read_raw_segment(self, start=start, stop=stop, sel=sel)
else:
return super(Raw, self).__getitem__(item)
-
- def save(self, fname, picks=None, quantum_sec=10, first_samp=None,
- last_samp=None):
+ def save(self, fname, picks=None, tmin=0, tmax=None, quantum_sec=10):
"""Save raw data to file
Parameters
@@ -192,33 +191,34 @@ class Raw(dict):
picks : list of int
Indices of channels to include
+ tmin : float
+ Time in seconds of first sample to save
+
+ tmax : int
+ Time in seconds of last sample to save
+
quantum_sec : float
Size of data chuncks in seconds.
- first_samp : int
- Index of first sample to save
-
- last_samp : int
- Index of last sample to save
"""
outfid, cals = start_writing_raw(fname, self.info, picks)
#
# Set up the reading parameters
#
- if first_samp is None:
- start = self.first_samp
- else:
- start = first_samp
- if last_samp is None:
- stop = self.last_samp + 1
+ # Convert to samples
+ start = int(floor(tmin * self.info['sfreq']))
+
+ if tmax is None:
+ stop = self.last_samp + 1 - self.first_samp
else:
- stop = last_samp + 1
+ stop = int(floor(tmax * self.info['sfreq']))
quantum = int(ceil(quantum_sec * self.info['sfreq']))
#
# Read and write all the data
#
+ write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, start)
for first in range(start, stop, quantum):
last = first + quantum
if last >= stop:
@@ -231,8 +231,6 @@ class Raw(dict):
print '[done]'
finish_writing_raw(outfid)
- self.close()
-
def time_to_index(self, *args):
indices = []
@@ -241,18 +239,16 @@ class Raw(dict):
indices.append(ind)
return indices
-
def close(self):
self.fid.close()
-
def __repr__(self):
s = "n_channels x n_times : %s x %s" % (len(self.info['ch_names']),
- self.last_samp - self.first_samp + 1)
+ self.last_samp - self.first_samp + 1)
return "Raw (%s)" % s
-def read_raw_segment(raw, start=None, stop=None, sel=None):
+def read_raw_segment(raw, start=0, stop=None, sel=None):
"""Read a chunck of raw data
Parameters
@@ -286,20 +282,16 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
if stop is None:
stop = raw.last_samp + 1
- if start is None:
- start = raw.first_samp
# Initial checks
- start = int(start)
- stop = int(stop)
- if start < raw.first_samp:
- start = raw.first_samp
+ start = int(start + raw.first_samp)
+ stop = int(stop + raw.first_samp)
if stop >= raw.last_samp:
stop = raw.last_samp + 1
if start >= stop:
- raise ValueError, 'No data in this range'
+ raise ValueError('No data in this range')
print 'Reading %d ... %d = %9.3f ... %9.3f secs...' % (
start, stop - 1, start / float(raw.info['sfreq']),
@@ -345,8 +337,7 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
from scipy import sparse
mult = sparse.csr_matrix(mult)
- for k in range(len(raw.rawdir)):
- this = raw.rawdir[k]
+ for this in raw.rawdir:
# Do we need this buffer
if this['last'] >= start:
@@ -413,7 +404,7 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
print ' [done]'
break
- times = np.arange(start, stop) / raw.info['sfreq']
+ times = (np.arange(start, stop) - raw.first_samp) / raw.info['sfreq']
raw.fid.seek(0, 0) # Go back to beginning of the file
@@ -618,9 +609,9 @@ def write_raw_buffer(fid, buf, cals):
Calibration factors
"""
if buf.shape[0] != len(cals):
- raise ValueError, 'buffer and calibration sizes do not match'
+ raise ValueError('buffer and calibration sizes do not match')
- write_float(fid, FIFF.FIFF_DATA_BUFFER,
+ write_float(fid, FIFF.FIFF_DATA_BUFFER, # XXX can do better
np.dot(np.diag(1.0 / np.ravel(cals)), buf))
@@ -635,4 +626,3 @@ def finish_writing_raw(fid):
end_block(fid, FIFF.FIFFB_RAW_DATA)
end_block(fid, FIFF.FIFFB_MEAS)
end_file(fid)
-
diff --git a/mne/fiff/tests/test_raw.py b/mne/fiff/tests/test_raw.py
index 9cf74b3..3ecbf15 100755
--- a/mne/fiff/tests/test_raw.py
+++ b/mne/fiff/tests/test_raw.py
@@ -1,11 +1,12 @@
import os.path as op
-# from numpy.testing import assert_array_almost_equal, assert_equal
+from numpy.testing import assert_array_almost_equal
from .. import Raw, pick_types
fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')
+
def test_io_raw():
"""Test IO for raw data
"""
@@ -16,8 +17,8 @@ def test_io_raw():
meg_channels_idx = [k for k in range(nchan) if ch_names[k][:3] == 'MEG']
meg_channels_idx = meg_channels_idx[:5]
- start, stop = raw.time_to_index(45, 50) # 100 s to 115 s data segment
- data, times = raw[meg_channels_idx, start:stop]
+ start, stop = raw.time_to_index(0, 5)
+ data, times = raw[meg_channels_idx, start:(stop+1)]
# Set up pick list: MEG + STI 014 - bad channels
want_meg = True
@@ -28,8 +29,19 @@ def test_io_raw():
picks = pick_types(raw.info, meg=want_meg, eeg=want_eeg,
stim=want_stim, include=include,
exclude=raw.info['bads'])
+ picks = picks[:5] # take 5 first
print "Number of picked channels : %d" % len(picks)
# Writing
- raw.save('raw.fif', picks)
+ raw.save('raw.fif', picks, tmin=0, tmax=5)
+
+ raw2 = Raw('raw.fif')
+
+ data2, times2 = raw2[:,:]
+
+ assert_array_almost_equal(data, data2)
+ assert_array_almost_equal(times, times2)
+ assert_array_almost_equal(raw.info['dev_head_t']['trans'],
+ raw2.info['dev_head_t']['trans'])
+ assert_array_almost_equal(raw.info['sfreq'], raw2.info['sfreq'])
diff --git a/mne/fiff/write.py b/mne/fiff/write.py
index 12b24fb..d96b6ff 100755
--- a/mne/fiff/write.py
+++ b/mne/fiff/write.py
@@ -28,7 +28,7 @@ def write_int(fid, kind, data):
"""Writes a 32-bit integer tag to a fif file"""
FIFFT_INT = 3
data_size = 4
- data = np.array(data, dtype='>i4')
+ data = np.array(data, dtype='>i4').T
_write(fid, data, kind, data_size, FIFFT_INT, '>i4')
@@ -36,7 +36,7 @@ def write_double(fid, kind, data):
"""Writes a double-precision floating point tag to a fif file"""
FIFFT_DOUBLE = 5
data_size = 8
- data = np.array(data, dtype='>f8')
+ data = np.array(data, dtype='>f8').T
_write(fid, data, kind, data_size, FIFFT_DOUBLE, '>f8')
@@ -44,7 +44,7 @@ def write_float(fid, kind, data):
"""Writes a single-precision floating point tag to a fif file"""
FIFFT_FLOAT = 4
data_size = 4
- data = np.array(data, dtype='>f4')
+ data = np.array(data, dtype='>f4').T
_write(fid, data, kind, data_size, FIFFT_FLOAT, '>f4')
diff --git a/mne/tests/test_cov.py b/mne/tests/test_cov.py
index a833db8..fe425bf 100755
--- a/mne/tests/test_cov.py
+++ b/mne/tests/test_cov.py
@@ -36,9 +36,11 @@ def test_cov_estimation_on_raw_segment():
"""Estimate raw on continuous recordings (typically empty room)
"""
raw = Raw(raw_fname)
- cov = mne.noise_covariance_segment(raw)
+ cov = mne.compute_raw_data_covariance(raw)
cov_mne = mne.Covariance(erm_cov_fname)
assert cov_mne.ch_names == cov.ch_names
+ print (linalg.norm(cov.data - cov_mne.data, ord='fro')
+ / linalg.norm(cov.data, ord='fro'))
assert (linalg.norm(cov.data - cov_mne.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 1e-6
@@ -49,7 +51,7 @@ def test_cov_estimation_with_triggers():
raw = Raw(raw_fname)
events = mne.find_events(raw)
event_ids = [1, 2, 3, 4]
- cov = mne.noise_covariance(raw, events, event_ids, tmin=-0.2, tmax=0,
+ cov = mne.compute_covariance(raw, events, event_ids, tmin=-0.2, tmax=0,
reject=dict(grad=10000e-13, mag=4e-12,
eeg=80e-6, eog=150e-6),
keep_sample_mean=True)
@@ -72,6 +74,6 @@ def test_whitening_cov():
evoked = read_evoked(ave_fname, setno=0, baseline=(None, 0))
cov = mne.Covariance(cov_fname)
- cov.whitener(evoked.info)
+ cov.get_whitener(evoked.info)
# XXX : test something
--
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