[med-svn] [python-mne] 117/376: ENH : Raw objects with IO capabilities

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:22:18 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 0508abeab5679745bd2993f78ffc3cf9459d1e1f
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Mon Mar 7 17:33:09 2011 -0500

    ENH : Raw objects with IO capabilities
---
 examples/plot_estimate_covariance_matrix.py        |   6 +-
 examples/plot_read_and_write_raw_data.py           |  49 +++
 examples/plot_read_epochs.py                       |  10 +-
 examples/plot_read_raw_data.py                     |  39 --
 examples/read_write_raw.py                         |  69 ----
 examples/stats/plot_cluster_stats_evoked.py        |   4 +-
 .../stats/plot_cluster_stats_time_frequency.py     |  12 +-
 examples/stats/plot_sensor_permutation_test.py     |  18 +-
 examples/time_frequency/plot_time_frequency.py     |  14 +-
 mne/__init__.py                                    |   1 +
 mne/cov.py                                         |   6 +-
 mne/epochs.py                                      |  24 +-
 mne/fiff/__init__.py                               |   2 +-
 mne/fiff/raw.py                                    | 402 ++++++++++++---------
 mne/fiff/tests/test_raw.py                         |  50 +--
 mne/tests/test_epochs.py                           |   6 +-
 mne/time_frequency/tests/test_tfr.py               |   8 +-
 17 files changed, 349 insertions(+), 371 deletions(-)

diff --git a/examples/plot_estimate_covariance_matrix.py b/examples/plot_estimate_covariance_matrix.py
index 128db46..66a92d3 100644
--- a/examples/plot_estimate_covariance_matrix.py
+++ b/examples/plot_estimate_covariance_matrix.py
@@ -18,15 +18,15 @@ from mne.datasets import sample
 data_path = sample.data_path('.')
 fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
 
-raw = fiff.setup_read_raw(fname)
+raw = fiff.Raw(fname)
 
 # Set up pick list: MEG + STI 014 - bad channels
 want_meg = True
 want_eeg = False
 want_stim = False
 
-picks = fiff.pick_types(raw['info'], meg=want_meg, eeg=want_eeg,
-                        stim=want_stim, exclude=raw['info']['bads'])
+picks = fiff.pick_types(raw.info, meg=want_meg, eeg=want_eeg,
+                        stim=want_stim, exclude=raw.info['bads'])
 
 print "Number of picked channels : %d" % len(picks)
 
diff --git a/examples/plot_read_and_write_raw_data.py b/examples/plot_read_and_write_raw_data.py
new file mode 100644
index 0000000..aca8e04
--- /dev/null
+++ b/examples/plot_read_and_write_raw_data.py
@@ -0,0 +1,49 @@
+"""
+=============================
+Reading and writing raw files
+=============================
+
+In this example we read a raw file. Plot a segment of MEG data
+restricted to MEG channels. And save these data in a new
+raw file.
+"""
+# Author: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
+#
+# License: BSD (3-clause)
+
+print __doc__
+
+from mne import fiff
+from mne.datasets import sample
+data_path = sample.data_path('.')
+
+fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
+
+raw = fiff.Raw(fname)
+
+# Set up pick list: MEG + STI 014 - bad channels
+want_meg = True
+want_eeg = False
+want_stim = False
+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)
+
+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]
+
+raw.save('sample_audvis_meg_raw.fif', picks=picks, first_samp=start,
+            last_samp=stop-1) # save MEG data in FIF file
+
+###############################################################################
+# Show MEG data
+import pylab as pl
+pl.close('all')
+pl.plot(times, data.T)
+pl.xlabel('time (s)')
+pl.ylabel('MEG data (T)')
+pl.show()
diff --git a/examples/plot_read_epochs.py b/examples/plot_read_epochs.py
index 7dbe1e4..e2c6d94 100644
--- a/examples/plot_read_epochs.py
+++ b/examples/plot_read_epochs.py
@@ -29,29 +29,29 @@ tmin = -0.2
 tmax = 0.5
 
 #   Setup for reading the raw data
-raw = fiff.setup_read_raw(raw_fname)
+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']
-exclude = raw['info']['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
 
 # EEG
-eeg_picks = fiff.pick_types(raw['info'], meg=False, eeg=True, stim=False,
+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))
 eeg_evoked_data = eeg_epochs.average()
 
 # MEG Magnetometers
-meg_mag_picks = fiff.pick_types(raw['info'], meg='mag', eeg=False, stim=False,
+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))
 meg_mag_evoked_data = meg_mag_epochs.average()
 
 # MEG
-meg_grad_picks = fiff.pick_types(raw['info'], meg='grad', eeg=False,
+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))
diff --git a/examples/plot_read_raw_data.py b/examples/plot_read_raw_data.py
deleted file mode 100644
index fee5580..0000000
--- a/examples/plot_read_raw_data.py
+++ /dev/null
@@ -1,39 +0,0 @@
-"""
-==========================
-Reading a raw file segment
-==========================
-"""
-# Author: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
-#
-# License: BSD (3-clause)
-
-print __doc__
-
-import os
-from mne import fiff
-from mne.datasets import sample
-data_path = sample.data_path('.')
-
-fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
-
-raw = fiff.setup_read_raw(fname)
-
-exclude = ['MEG 2443', 'EEG 053'] # bad channels
-
-meg_channels_idx = fiff.pick_types(raw['info'], meg=True, exclude=exclude)
-meg_channels_idx = meg_channels_idx[:5] # take 5 first
-
-start, stop = raw.time_to_index(100, 115) # 100 s to 115 s data segment
-data, times = raw[meg_channels_idx, start:stop]
-# data, times = raw[:, start:stop] # read all channels
-
-raw.close()
-
-###############################################################################
-# Show MEG data
-import pylab as pl
-pl.close('all')
-pl.plot(times, data.T)
-pl.xlabel('time (s)')
-pl.ylabel('MEG data (T)')
-pl.show()
diff --git a/examples/read_write_raw.py b/examples/read_write_raw.py
deleted file mode 100644
index 6d23be4..0000000
--- a/examples/read_write_raw.py
+++ /dev/null
@@ -1,69 +0,0 @@
-"""
-=======================
-Read and write raw data
-=======================
-
-Read and write raw data in 60-sec blocks
-"""
-# Author: Alexandre Gramfort <gramfort at nmr.mgh.harvard.edu>
-#
-# License: BSD (3-clause)
-
-print __doc__
-
-import os
-from math import ceil
-from mne import fiff
-from mne.datasets import sample
-
-data_path = sample.data_path('.')
-infile = data_path
-infile += '/MEG/sample/sample_audvis_raw.fif'
-outfile = 'sample_audvis_small_raw.fif'
-
-raw = fiff.setup_read_raw(infile)
-
-# Set up pick list: MEG + STI 014 - bad channels
-want_meg = True
-want_eeg = False
-want_stim = False
-include = ['STI 014']
-# include = []
-# include = ['STI101', 'STI201', 'STI301']
-
-picks = fiff.pick_types(raw['info'], meg=want_meg, eeg=want_eeg,
-                        stim=want_stim, include=include,
-                        exclude=raw['info']['bads'])
-
-print "Number of picked channels : %d" % len(picks)
-
-outfid, cals = fiff.start_writing_raw(outfile, raw['info'], picks)
-#
-#   Set up the reading parameters
-#
-start = raw['first_samp']
-stop = raw['last_samp'] + 1
-quantum_sec = 10
-quantum = int(ceil(quantum_sec * raw['info']['sfreq']))
-#
-#   To read the whole file at once set
-#
-# quantum     = stop - start
-#
-#
-#   Read and write all the data
-#
-first_buffer = True
-for first in range(start, stop, quantum):
-    last = first + quantum
-    if last >= stop:
-        last = stop+1
-
-    data, times = raw[picks, first:last]
-
-    print 'Writing ... ',
-    fiff.write_raw_buffer(outfid, data, cals)
-    print '[done]'
-
-fiff.finish_writing_raw(outfid)
-raw['fid'].close()
diff --git a/examples/stats/plot_cluster_stats_evoked.py b/examples/stats/plot_cluster_stats_evoked.py
index ff8e390..e4c5cd4 100644
--- a/examples/stats/plot_cluster_stats_evoked.py
+++ b/examples/stats/plot_cluster_stats_evoked.py
@@ -30,7 +30,7 @@ tmin = -0.2
 tmax = 0.5
 
 #   Setup for reading the raw data
-raw = fiff.setup_read_raw(raw_fname)
+raw = fiff.Raw(raw_fname)
 events = mne.read_events(event_fname)
 
 channel = 'MEG 1332'
@@ -38,7 +38,7 @@ include = [channel]
 
 ###############################################################################
 # Read epochs for the channel of interest
-picks = fiff.pick_types(raw['info'], meg=False, include=include)
+picks = fiff.pick_types(raw.info, meg=False, include=include)
 event_id = 1
 epochs1 = mne.Epochs(raw, events, event_id,
                             tmin, tmax, picks=picks, baseline=(None, 0))
diff --git a/examples/stats/plot_cluster_stats_time_frequency.py b/examples/stats/plot_cluster_stats_time_frequency.py
index a5793b0..11e81ad 100644
--- a/examples/stats/plot_cluster_stats_time_frequency.py
+++ b/examples/stats/plot_cluster_stats_time_frequency.py
@@ -19,7 +19,7 @@ import numpy as np
 
 import mne
 from mne import fiff
-from mne.tfr import single_trial_power
+from mne.time_frequency import single_trial_power
 from mne.stats import permutation_cluster_test
 from mne.datasets import sample
 
@@ -33,18 +33,18 @@ tmin = -0.2
 tmax = 0.5
 
 # Setup for reading the raw data
-raw = fiff.setup_read_raw(raw_fname)
+raw = fiff.Raw(raw_fname)
 events = mne.read_events(event_fname)
 
 include = []
-exclude = raw['info']['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
 
 # picks MEG gradiometers
-picks = fiff.pick_types(raw['info'], meg='grad', eeg=False,
+picks = fiff.pick_types(raw.info, meg='grad', eeg=False,
                                 stim=False, include=include, exclude=exclude)
 
 picks = [picks[97]]
-ch_name = raw['info']['ch_names'][picks[0]]
+ch_name = raw.info['ch_names'][picks[0]]
 
 # Load condition 1
 event_id = 1
@@ -64,7 +64,7 @@ data_condition_2 *= 1e13 # change unit to fT / cm
 times = 1e3 * epochs_condition_1.times # change unit to ms
 
 frequencies = np.arange(7, 30, 3) # define frequencies of interest
-Fs = raw['info']['sfreq'] # sampling in Hz
+Fs = raw.info['sfreq'] # sampling in Hz
 epochs_coefs_1 = single_trial_power(data_condition_1, Fs=Fs,
                                    frequencies=frequencies,
                                    n_cycles=2, use_fft=False)
diff --git a/examples/stats/plot_sensor_permutation_test.py b/examples/stats/plot_sensor_permutation_test.py
index 557b50d..c0a0386 100644
--- a/examples/stats/plot_sensor_permutation_test.py
+++ b/examples/stats/plot_sensor_permutation_test.py
@@ -15,7 +15,6 @@ is performed on MNE sample dataset between 40 and 60 ms.
 
 print __doc__
 
-import os
 import numpy as np
 
 import mne
@@ -33,29 +32,30 @@ tmin = -0.2
 tmax = 0.5
 
 #   Setup for reading the raw data
-raw = fiff.setup_read_raw(raw_fname)
+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 channel ['STI 014']
-exclude = raw['info']['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
 
 # pick MEG Magnetometers
-picks = fiff.pick_types(raw['info'], meg='grad', eeg=False, stim=False,
+picks = fiff.pick_types(raw.info, meg='grad', eeg=False, stim=False,
                                             include=include, exclude=exclude)
-data, times, channel_names = mne.read_epochs(raw, events, event_id,
+epochs = mne.Epochs(raw, events, event_id,
                             tmin, tmax, picks=picks, baseline=(None, 0))
-epochs = np.array([d['epoch'] for d in data]) # as 3D matrix
-evoked_data = np.mean(epochs, axis=0) # compute evoked fields
+data = epochs.get_data()
+times = epochs.times
+evoked_data = data.mean(axis=0)
 
 temporal_mask = np.logical_and(0.04 <= times, times <= 0.06)
-data = np.squeeze(np.mean(epochs[:, :, temporal_mask], axis=2))
+data = np.squeeze(np.mean(data[:, :, temporal_mask], axis=2))
 
 n_permutations = 50000
 p_values, T0, H0 = permutation_t_test(data, n_permutations)
 
 significant_sensors = picks[p_values <= 0.05]
-significant_sensors_names = [raw['info']['ch_names'][k]
+significant_sensors_names = [raw.info['ch_names'][k]
                               for k in significant_sensors]
 
 print "Number of significant sensors : %d" % len(significant_sensors)
diff --git a/examples/time_frequency/plot_time_frequency.py b/examples/time_frequency/plot_time_frequency.py
index f7cea4a..2725aa4 100644
--- a/examples/time_frequency/plot_time_frequency.py
+++ b/examples/time_frequency/plot_time_frequency.py
@@ -31,14 +31,14 @@ tmin = -0.2
 tmax = 0.5
 
 # Setup for reading the raw data
-raw = fiff.setup_read_raw(raw_fname)
+raw = fiff.Raw(raw_fname)
 events = mne.read_events(event_fname)
 
 include = []
-exclude = raw['info']['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
 
 # picks MEG gradiometers
-picks = fiff.pick_types(raw['info'], meg='grad', eeg=False,
+picks = fiff.pick_types(raw.info, meg='grad', eeg=False,
                                 stim=False, include=include, exclude=exclude)
 
 picks = [picks[97]]
@@ -51,7 +51,7 @@ times = 1e3 * epochs.times # change unit to ms
 evoked *= 1e13 # change unit to fT / cm
 
 frequencies = np.arange(7, 30, 3) # define frequencies of interest
-Fs = raw['info']['sfreq'] # sampling in Hz
+Fs = raw.info['sfreq'] # sampling in Hz
 power, phase_lock = induced_power(data, Fs=Fs, frequencies=frequencies,
                                    n_cycles=2, n_jobs=1, use_fft=False)
 
@@ -62,7 +62,7 @@ pl.clf()
 pl.subplots_adjust(0.1, 0.08, 0.96, 0.94, 0.2, 0.63)
 pl.subplot(3, 1, 1)
 pl.plot(times, evoked.T)
-pl.title('Evoked response (%s)' % raw['info']['ch_names'][picks[0]])
+pl.title('Evoked response (%s)' % raw.info['ch_names'][picks[0]])
 pl.xlabel('time (ms)')
 pl.ylabel('Magnetic Field (fT/cm)')
 pl.xlim(times[0], times[-1])
@@ -74,7 +74,7 @@ pl.imshow(20*np.log10(power[0]), extent=[times[0], times[-1],
           aspect='auto', origin='lower')
 pl.xlabel('Time (s)')
 pl.ylabel('Frequency (Hz)')
-pl.title('Induced power (%s)' % raw['info']['ch_names'][picks[0]])
+pl.title('Induced power (%s)' % raw.info['ch_names'][picks[0]])
 pl.colorbar()
 
 pl.subplot(3, 1, 3)
@@ -83,6 +83,6 @@ pl.imshow(phase_lock[0], extent=[times[0], times[-1],
           aspect='auto', origin='lower')
 pl.xlabel('Time (s)')
 pl.ylabel('PLF')
-pl.title('Phase-lock (%s)' % raw['info']['ch_names'][picks[0]])
+pl.title('Phase-lock (%s)' % raw.info['ch_names'][picks[0]])
 pl.colorbar()
 pl.show()
diff --git a/mne/__init__.py b/mne/__init__.py
index 5b0ed2d..526774a 100644
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -5,6 +5,7 @@ from .event import read_events, write_events
 from .forward import read_forward_solution
 from .stc import read_stc, write_stc
 from .bem_surfaces import read_bem_surfaces
+from .source_space import read_source_spaces
 from .inverse import read_inverse_operator, compute_inverse
 from .epochs import Epochs
 from .label import label_time_courses, read_label
diff --git a/mne/cov.py b/mne/cov.py
index 1771648..5fc4826 100644
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -55,9 +55,9 @@ class Covariance(object):
         """Estimate noise covariance matrix from a raw FIF file
         """
         #   Set up the reading parameters
-        start = raw['first_samp']
-        stop = raw['last_samp'] + 1
-        quantum = int(quantum_sec * raw['info']['sfreq'])
+        start = raw.first_samp
+        stop = raw.last_samp + 1
+        quantum = int(quantum_sec * raw.info['sfreq'])
 
         cov = 0
         n_samples = 0
diff --git a/mne/epochs.py b/mne/epochs.py
index 8da14db..364ad8c 100644
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -13,7 +13,7 @@ class Epochs(object):
     Parameters
     ----------
     raw : Raw object
-        Returned by the setup_read_raw function
+        A instance of Raw
 
     events : array, of shape [n_events, 3]
         Returned by the read_events function
@@ -72,24 +72,24 @@ class Epochs(object):
         self.preload = preload
 
         if picks is None:
-            picks = range(len(raw['info']['ch_names']))
-            self.ch_names = raw['info']['ch_names']
+            picks = range(len(raw.info['ch_names']))
+            self.ch_names = raw.info['ch_names']
         else:
-            self.ch_names = [raw['info']['ch_names'][k] for k in picks]
+            self.ch_names = [raw.info['ch_names'][k] for k in picks]
 
         #   Set up projection
-        if raw['info']['projs'] is None:
+        if raw.info['projs'] is None:
             print 'No projector specified for these data'
             raw['proj'] = []
         else:
             #   Activate the projection items
-            for proj in raw['info']['projs']:
+            for proj in raw.info['projs']:
                 proj['active'] = True
 
-            print '%d projection items activated' % len(raw['info']['projs'])
+            print '%d projection items activated' % len(raw.info['projs'])
 
             #   Create the projector
-            proj, nproj = fiff.proj.make_projector_info(raw['info'])
+            proj, nproj = fiff.proj.make_projector_info(raw.info)
             if nproj == 0:
                 print 'The projection vectors do not apply to these channels'
                 raw['proj'] = None
@@ -99,7 +99,7 @@ class Epochs(object):
                 raw['proj'] = proj
 
         #   Set up the CTF compensator
-        current_comp = fiff.get_current_comp(raw['info'])
+        current_comp = fiff.get_current_comp(raw.info)
         if current_comp > 0:
             print 'Current compensation grade : %d' % current_comp
 
@@ -107,7 +107,7 @@ class Epochs(object):
             dest_comp = current_comp
 
         if current_comp != dest_comp:
-            raw.comp = fiff.raw.make_compensator(raw['info'], current_comp,
+            raw.comp = fiff.raw.make_compensator(raw.info, current_comp,
                                                  dest_comp)
             print 'Appropriate compensator added to change to grade %d.' % (
                                                                     dest_comp)
@@ -123,7 +123,7 @@ class Epochs(object):
             raise ValueError, 'No desired events found.'
 
         # Handle times
-        sfreq = raw['info']['sfreq']
+        sfreq = raw.info['sfreq']
         self.times = np.arange(int(tmin*sfreq), int(tmax*sfreq),
                           dtype=np.float) / sfreq
 
@@ -135,7 +135,7 @@ class Epochs(object):
 
     def get_epoch(self, idx):
         """Load one epoch from disk"""
-        sfreq = self.raw['info']['sfreq']
+        sfreq = self.raw.info['sfreq']
         event_samp = self.events[idx, 0]
 
         # Read a data segment
diff --git a/mne/fiff/__init__.py b/mne/fiff/__init__.py
index e490312..08096c5 100644
--- a/mne/fiff/__init__.py
+++ b/mne/fiff/__init__.py
@@ -8,7 +8,7 @@ __version__ = '0.1.git'
 from .constants import FIFF
 from .open import fiff_open
 from .evoked import read_evoked, write_evoked
-from .raw import setup_read_raw, read_raw_segment, read_raw_segment_times, \
+from .raw import Raw, read_raw_segment, read_raw_segment_times, \
                  start_writing_raw, write_raw_buffer, finish_writing_raw
 from .pick import pick_types
 from .compensator import get_current_comp
diff --git a/mne/fiff/raw.py b/mne/fiff/raw.py
index f90cfd2..33cefc1 100644
--- a/mne/fiff/raw.py
+++ b/mne/fiff/raw.py
@@ -14,7 +14,147 @@ from .tag import read_tag
 
 
 class Raw(dict):
-    """Raw data set"""
+    """Raw data set
+
+    Parameters
+    ----------
+    fname: string
+        The name of the raw file
+
+    allow_maxshield: bool, (default False)
+        allow_maxshield if True XXX ???
+
+    info: dict
+        Infos about raw data
+
+    """
+
+    def __init__(self, fname, allow_maxshield=False):
+        """
+        Parameters
+        ----------
+        fname: string
+            The name of the raw file
+
+        allow_maxshield: bool, (default False)
+            allow_maxshield if True XXX ???
+
+        """
+
+        #   Open the file
+        print 'Opening raw data file %s...' % fname
+        fid, tree, _ = fiff_open(fname)
+
+        #   Read the measurement info
+        info, meas = read_meas_info(fid, tree)
+
+        #   Locate the data of interest
+        raw_node = dir_tree_find(meas, FIFF.FIFFB_RAW_DATA)
+        if len(raw_node) == 0:
+            raw_node = dir_tree_find(meas, FIFF.FIFFB_CONTINUOUS_DATA)
+            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
+            else:
+                if len(raw_node) == 0:
+                    raise ValueError, 'No raw data in %s' % fname
+
+        if len(raw_node) == 1:
+            raw_node = raw_node[0]
+
+        #   Set up the output structure
+        info['filename'] = fname
+
+        #   Process the directory
+        directory = raw_node['directory']
+        nent = raw_node['nent']
+        nchan = int(info['nchan'])
+        first = 0
+        first_samp = 0
+        first_skip = 0
+
+        #   Get first sample tag if it is there
+        if directory[first].kind == FIFF.FIFF_FIRST_SAMPLE:
+            tag = read_tag(fid, directory[first].pos)
+            first_samp = int(tag.data)
+            first += 1
+
+        #   Omit initial skip
+        if directory[first].kind == FIFF.FIFF_DATA_SKIP:
+            #  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
+
+        self.first_samp = first_samp
+
+        #   Go through the remaining tags in the directory
+        rawdir = list()
+        nskip = 0
+        for k in range(first, nent):
+            ent = directory[k]
+            if ent.kind == FIFF.FIFF_DATA_SKIP:
+                tag = read_tag(fid, ent.pos)
+                nskip = int(tag.data)
+            elif ent.kind == FIFF.FIFF_DATA_BUFFER:
+                #   Figure out the number of samples in this buffer
+                if ent.type == FIFF.FIFFT_DAU_PACK16:
+                    nsamp = ent.size / (2*nchan)
+                elif ent.type == FIFF.FIFFT_SHORT:
+                    nsamp = ent.size / (2*nchan)
+                elif ent.type == FIFF.FIFFT_FLOAT:
+                    nsamp = ent.size / (4*nchan)
+                elif ent.type == FIFF.FIFFT_INT:
+                    nsamp = ent.size / (4*nchan)
+                else:
+                    fid.close()
+                    raise ValueError, 'Cannot handle data buffers of type %d' % (
+                                                                        ent.type)
+
+                #  Do we have an initial skip pending?
+                if first_skip > 0:
+                    first_samp += nsamp * first_skip
+                    self.first_samp = first_samp
+                    first_skip = 0
+
+                #  Do we have a skip pending?
+                if nskip > 0:
+                    import pdb; pdb.set_trace()
+                    rawdir.append(dict(ent=None, first=first_samp,
+                                       last=first_samp + nskip*nsamp - 1,
+                                       nsamp=nskip*nsamp))
+                    first_samp += nskip*nsamp
+                    nskip = 0
+
+                #  Add a data buffer
+                rawdir.append(dict(ent=ent, first=first_samp,
+                                   last=first_samp + nsamp - 1,
+                                   nsamp=nsamp))
+                first_samp += nsamp
+
+        self.last_samp = first_samp - 1
+
+        #   Add the calibration factors
+        cals = np.zeros(info['nchan'])
+        for k in range(info['nchan']):
+            cals[k] = info['chs'][k]['range'] * \
+                      info['chs'][k]['cal']
+
+        self.cals = cals
+        self.rawdir = rawdir
+        self.proj = None
+        self.comp = None
+        print '\tRange : %d ... %d =  %9.3f ... %9.3f secs' % (
+                   self.first_samp, self.last_samp,
+                   float(self.first_samp) / info['sfreq'],
+                   float(self.last_samp) / info['sfreq'])
+        print 'Ready.'
+
+        self.fid = fid
+        self.info = info
+
+
     def __getitem__(self, item):
         """getting raw data content with python slicing"""
         if isinstance(item, tuple): # slicing required
@@ -22,7 +162,7 @@ class Raw(dict):
                 time_slice = item[1]
                 if isinstance(item[0], slice):
                     start = item[0].start if item[0].start is not None else 0
-                    nchan = self['info']['nchan']
+                    nchan = self.info['nchan']
                     stop = item[0].stop if item[0].stop is not None else nchan
                     step = item[0].step if item[0].step is not None else 1
                     sel = range(start, stop, step)
@@ -39,147 +179,71 @@ class Raw(dict):
         else:
             return super(Raw, self).__getitem__(item)
 
-    def time_to_index(self, *args):
-        indices = []
-        for time in args:
-            ind = int(time * self['info']['sfreq'])
-            indices.append(ind)
-        return indices
 
-    def close(self):
-        self['fid'].close()
+    def save(self, fname, picks=None, quantum_sec=10, first_samp=None,
+             last_samp=None):
+        """Save raw data to file
 
+        Parameters
+        ----------
+        fname : string
+            File name of the new dataset.
 
-def setup_read_raw(fname, allow_maxshield=False):
-    """Read information about raw data file
+        picks : list of int
+            Indices of channels to include
 
-    Parameters
-    ----------
-    fname: string
-        The name of the raw file
+        quantum_sec : float
+            Size of data chuncks in seconds.
 
-    allow_maxshield: bool, (default False)
-        allow_maxshield if True XXX ???
+        first_samp : int
+            Index of first sample to save
 
-    Returns
-    -------
-    data: dict
-        Infos about raw data
-    """
+        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
 
-    #   Open the file
-    print 'Opening raw data file %s...' % fname
-    fid, tree, _ = fiff_open(fname)
-
-    #   Read the measurement info
-    info, meas = read_meas_info(fid, tree)
-
-    #   Locate the data of interest
-    raw = dir_tree_find(meas, FIFF.FIFFB_RAW_DATA)
-    if len(raw) == 0:
-        raw = dir_tree_find(meas, FIFF.FIFFB_CONTINUOUS_DATA)
-        if allow_maxshield:
-            raw = dir_tree_find(meas, FIFF.FIFFB_SMSH_RAW_DATA)
-            if len(raw) == 0:
-                raise ValueError, 'No raw data in %s' % fname
+        if last_samp is None:
+            stop = self.last_samp + 1
         else:
-            if len(raw) == 0:
-                raise ValueError, 'No raw data in %s' % fname
-
-    if len(raw) == 1:
-        raw = raw[0]
-
-    #   Set up the output structure
-    info['filename'] = fname
-
-    data = Raw(fid=fid, info=info, first_samp=0, last_samp=0)
-
-    #   Process the directory
-    directory = raw['directory']
-    nent = raw['nent']
-    nchan = int(info['nchan'])
-    first = 0
-    first_samp = 0
-    first_skip = 0
-
-    #  Get first sample tag if it is there
-    if directory[first].kind == FIFF.FIFF_FIRST_SAMPLE:
-        tag = read_tag(fid, directory[first].pos)
-        first_samp = int(tag.data)
-        first += 1
-
-    #  Omit initial skip
-    if directory[first].kind == FIFF.FIFF_DATA_SKIP:
-        #  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
-
-    data['first_samp'] = first_samp
-
-    #   Go through the remaining tags in the directory
-    rawdir = list()
-    nskip = 0
-    for k in range(first, nent):
-        ent = directory[k]
-        if ent.kind == FIFF.FIFF_DATA_SKIP:
-            tag = read_tag(fid, ent.pos)
-            nskip = int(tag.data)
-        elif ent.kind == FIFF.FIFF_DATA_BUFFER:
-            #   Figure out the number of samples in this buffer
-            if ent.type == FIFF.FIFFT_DAU_PACK16:
-                nsamp = ent.size / (2*nchan)
-            elif ent.type == FIFF.FIFFT_SHORT:
-                nsamp = ent.size / (2*nchan)
-            elif ent.type == FIFF.FIFFT_FLOAT:
-                nsamp = ent.size / (4*nchan)
-            elif ent.type == FIFF.FIFFT_INT:
-                nsamp = ent.size / (4*nchan)
-            else:
-                fid.close()
-                raise ValueError, 'Cannot handle data buffers of type %d' % (
-                                                                    ent.type)
-
-            #  Do we have an initial skip pending?
-            if first_skip > 0:
-                first_samp += nsamp * first_skip
-                data['first_samp'] = first_samp
-                first_skip = 0
-
-            #  Do we have a skip pending?
-            if nskip > 0:
-                import pdb; pdb.set_trace()
-                rawdir.append(dict(ent=None, first=first_samp,
-                                   last=first_samp + nskip*nsamp - 1,
-                                   nsamp=nskip*nsamp))
-                first_samp += nskip*nsamp
-                nskip = 0
-
-            #  Add a data buffer
-            rawdir.append(dict(ent=ent, first=first_samp,
-                               last=first_samp + nsamp - 1,
-                               nsamp=nsamp))
-            first_samp += nsamp
-
-    data['last_samp'] = first_samp - 1
-
-    #   Add the calibration factors
-    cals = np.zeros(data['info']['nchan'])
-    for k in range(data['info']['nchan']):
-        cals[k] = data['info']['chs'][k]['range'] * \
-                  data['info']['chs'][k]['cal']
-
-    data['cals'] = cals
-    data['rawdir'] = rawdir
-    data['proj'] = None
-    data['comp'] = None
-    print '\tRange : %d ... %d =  %9.3f ... %9.3f secs' % (
-               data['first_samp'], data['last_samp'],
-               float(data['first_samp']) / data['info']['sfreq'],
-               float(data['last_samp']) / data['info']['sfreq'])
-    print 'Ready.'
-
-    return data
+            stop = last_samp + 1
+
+        quantum = int(ceil(quantum_sec * self.info['sfreq']))
+        #
+        #   Read and write all the data
+        #
+        for first in range(start, stop, quantum):
+            last = first + quantum
+            if last >= stop:
+                last = stop + 1
+
+            data, times = self[picks, first:last]
+
+            print 'Writing ... ',
+            write_raw_buffer(outfid, data, cals)
+            print '[done]'
+
+        finish_writing_raw(outfid)
+        self.close()
+
+
+    def time_to_index(self, *args):
+        indices = []
+        for time in args:
+            ind = int(time * self.info['sfreq'])
+            indices.append(ind)
+        return indices
+
+
+    def close(self):
+        self.fid.close()
 
 
 def read_raw_segment(raw, start=None, stop=None, sel=None):
@@ -187,8 +251,8 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
 
     Parameters
     ----------
-    raw: dict
-        The dict returned by setup_read_raw
+    raw: Raw object
+        An instance of Raw
 
     start: int, (optional)
         first sample to include (first is 0). If omitted, defaults to the first
@@ -215,55 +279,55 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
     """
 
     if stop is None:
-        stop = raw['last_samp'] + 1
+        stop = raw.last_samp + 1
     if start is None:
-        start = raw['first_samp']
+        start = raw.first_samp
 
     #  Initial checks
     start = int(start)
     stop = int(stop)
-    if start < raw['first_samp']:
-        start = raw['first_samp']
+    if start < raw.first_samp:
+        start = raw.first_samp
 
-    if stop >= raw['last_samp']:
-        stop = raw['last_samp'] + 1
+    if stop >= raw.last_samp:
+        stop = raw.last_samp + 1
 
     if start >= stop:
         raise ValueError, 'No data in this range'
 
     print 'Reading %d ... %d  =  %9.3f ... %9.3f secs...' % (
-                       start, stop - 1, start / float(raw['info']['sfreq']),
-                       (stop - 1) / float(raw['info']['sfreq'])),
+                       start, stop - 1, start / float(raw.info['sfreq']),
+                       (stop - 1) / float(raw.info['sfreq'])),
 
     #  Initialize the data and calibration vector
-    nchan = raw['info']['nchan']
+    nchan = raw.info['nchan']
     dest = 0
-    cal = np.diag(raw['cals'].ravel())
+    cal = np.diag(raw.cals.ravel())
 
     if sel is None:
         data = np.empty((nchan, stop - start))
-        if raw['proj'] is None and raw['comp'] is None:
+        if raw.proj is None and raw.comp is None:
             mult = None
         else:
-            if raw['proj'] is None:
-                mult = raw['comp'] * cal
-            elif raw['comp'] is None:
-                mult = raw['proj'] * cal
+            if raw.proj is None:
+                mult = raw.comp * cal
+            elif raw.comp is None:
+                mult = raw.proj * cal
             else:
-                mult = raw['proj'] * raw['comp'] * cal
+                mult = raw.proj * raw.comp * cal
 
     else:
         data = np.empty((len(sel), stop - start))
-        if raw['proj'] is None and raw['comp'] is None:
+        if raw.proj is None and raw.comp is None:
             mult = None
-            cal = np.diag(raw['cals'][sel].ravel())
+            cal = np.diag(raw.cals[sel].ravel())
         else:
-            if raw['proj'] is None:
-                mult = raw['comp'][sel,:] * cal
-            elif raw['comp'] is None:
-                mult = raw['proj'][sel,:] * cal
+            if raw.proj is None:
+                mult = raw.comp[sel,:] * cal
+            elif raw.comp is None:
+                mult = raw.proj[sel,:] * cal
             else:
-                mult = raw['proj'][sel,:] * raw['comp'] * cal
+                mult = raw.proj[sel,:] * raw.comp * cal
 
     do_debug = False
     # do_debug = True
@@ -275,8 +339,8 @@ 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 k in range(len(raw.rawdir)):
+        this = raw.rawdir[k]
 
         #  Do we need this buffer
         if this['last'] >= start:
@@ -289,7 +353,7 @@ def read_raw_segment(raw, start=None, stop=None, sel=None):
                 else:
                     one = np.zeros((len(sel), this['nsamp']))
             else:
-                tag = read_tag(raw['fid'], this['ent'].pos)
+                tag = read_tag(raw.fid, this['ent'].pos)
 
                 #   Depending on the state of the projection and selection
                 #   we proceed a little bit differently
@@ -343,9 +407,9 @@ 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.info['sfreq']
 
-    raw['fid'].seek(0, 0) # Go back to beginning of the file
+    raw.fid.seek(0, 0) # Go back to beginning of the file
 
     return data, times
 
@@ -355,8 +419,8 @@ def read_raw_segment_times(raw, start, stop, sel=None):
 
     Parameters
     ----------
-    raw: dict
-        The dict returned by setup_read_raw
+    raw: Raw object
+        An instance of Raw
 
     start: float
         Starting time of the segment in seconds
@@ -379,8 +443,8 @@ def read_raw_segment_times(raw, start, stop, sel=None):
         returns the time values corresponding to the samples
     """
     #   Convert to samples
-    start = floor(start * raw['info']['sfreq'])
-    stop = ceil(stop * raw['info']['sfreq'])
+    start = floor(start * raw.info['sfreq'])
+    stop = ceil(stop * raw.info['sfreq'])
 
     #   Read it
     return read_raw_segment(raw, start, stop, sel)
diff --git a/mne/fiff/tests/test_raw.py b/mne/fiff/tests/test_raw.py
index 6629183..9f5851c 100644
--- a/mne/fiff/tests/test_raw.py
+++ b/mne/fiff/tests/test_raw.py
@@ -4,7 +4,7 @@ import os.path as op
 from numpy.testing import assert_array_almost_equal, assert_equal
 
 from math import ceil
-from .. import setup_read_raw, read_raw_segment_times, pick_types, \
+from .. import Raw, read_raw_segment_times, pick_types, \
                start_writing_raw, write_raw_buffer, finish_writing_raw
 
 
@@ -13,17 +13,15 @@ fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')
 def test_io_raw():
     """Test IO for raw data
     """
-    raw = setup_read_raw(fname)
+    raw = Raw(fname)
 
-    nchan = raw['info']['nchan']
-    ch_names = raw['info']['ch_names']
-    meg_channels_idx = [k for k in range(nchan) if ch_names[k][:3]=='MEG']
+    nchan = raw.info['nchan']
+    ch_names = raw.info['ch_names']
+    meg_channels_idx = [k for k in range(nchan) if ch_names[k][:3] == 'MEG']
     meg_channels_idx = meg_channels_idx[:5]
 
-    data, times = read_raw_segment_times(raw, start=45, stop=50,
-                                              sel=meg_channels_idx)
-
-    # Writing
+    start, stop = raw.time_to_index(45, 50) # 100 s to 115 s data segment
+    data, times = raw[meg_channels_idx, start:stop]
 
     # Set up pick list: MEG + STI 014 - bad channels
     want_meg = True
@@ -31,37 +29,11 @@ def test_io_raw():
     want_stim = False
     include = ['STI 014']
 
-    picks = pick_types(raw['info'], meg=want_meg, eeg=want_eeg,
+    picks = pick_types(raw.info, meg=want_meg, eeg=want_eeg,
                             stim=want_stim, include=include,
-                            exclude=raw['info']['bads'])
+                            exclude=raw.info['bads'])
 
     print "Number of picked channels : %d" % len(picks)
 
-    outfid, cals = start_writing_raw('raw.fif', raw['info'], picks)
-    #
-    #   Set up the reading parameters
-    #
-    start = raw['first_samp']
-    stop = raw['last_samp']
-    quantum_sec = 10
-    quantum = int(ceil(quantum_sec * raw['info']['sfreq']))
-    #
-    #   Read and write all the data
-    #
-    for first in range(start, stop, quantum):
-        last = first + quantum
-        if last >= stop:
-            last = stop
-        try:
-            data, times = raw[picks, first:last]
-        except Exception as inst:
-            raw['fid'].close()
-            outfid.close()
-            print inst
-
-        write_raw_buffer(outfid, data, cals)
-        print '[done]'
-
-    finish_writing_raw(outfid)
-    raw['fid'].close()
-
+    # Writing
+    raw.save('raw.fif', picks)
diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py
index 874a30d..a335fe6 100644
--- a/mne/tests/test_epochs.py
+++ b/mne/tests/test_epochs.py
@@ -20,7 +20,7 @@ def test_read_epochs():
     tmax = 0.5
 
     #   Setup for reading the raw data
-    raw = fiff.setup_read_raw(raw_fname)
+    raw = fiff.Raw(raw_fname)
     events = mne.read_events(event_name)
 
     # Set up pick list: MEG + STI 014 - bad channels (modify to your needs)
@@ -28,8 +28,8 @@ def test_read_epochs():
     want_meg = True
     want_eeg = False
     want_stim = False
-    picks = fiff.pick_types(raw['info'], want_meg, want_eeg, want_stim,
-                            include, raw['info']['bads'])
+    picks = fiff.pick_types(raw.info, want_meg, want_eeg, want_stim,
+                            include, raw.info['bads'])
 
     epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
                         baseline=(None, 0))
diff --git a/mne/time_frequency/tests/test_tfr.py b/mne/time_frequency/tests/test_tfr.py
index e701117..5df188d 100644
--- a/mne/time_frequency/tests/test_tfr.py
+++ b/mne/time_frequency/tests/test_tfr.py
@@ -20,14 +20,14 @@ def test_time_frequency():
     tmax = 0.5
 
     # Setup for reading the raw data
-    raw = fiff.setup_read_raw(raw_fname)
+    raw = fiff.Raw(raw_fname)
     events = mne.read_events(event_fname)
 
     include = []
-    exclude = raw['info']['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
+    exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more
 
     # picks MEG gradiometers
-    picks = fiff.pick_types(raw['info'], meg='grad', eeg=False,
+    picks = fiff.pick_types(raw.info, meg='grad', eeg=False,
                                     stim=False, include=include, exclude=exclude)
 
     picks = picks[:2]
@@ -37,7 +37,7 @@ def test_time_frequency():
     times = epochs.times
 
     frequencies = np.arange(6, 20, 5) # define frequencies of interest
-    Fs = raw['info']['sfreq'] # sampling in Hz
+    Fs = raw.info['sfreq'] # sampling in Hz
     power, phase_lock = induced_power(data, Fs=Fs, frequencies=frequencies,
                                        n_cycles=2, use_fft=True)
 

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