Package mvpa :: Package misc :: Package io :: Module meg

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.misc.io.meg

      1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
      2  # vi: set ft=python sts=4 ts=4 sw=4 et:
      3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
      4  #
      5  #   See COPYING file distributed along with the PyMVPA package for the
      6  #   copyright and license terms.
      7  #
      8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
      9  """IO helper for MEG datasets."""
     10
     11  __docformat__ = 'restructuredtext'
     12
     13  import numpy as N
     14
     15  from mvpa.base import externals
     16
     17 -class TuebingenMEG(object):
     18      """Reader for MEG data from line-based textfile format.
     19
     20      This class reads segmented MEG data from a textfile, which is created by
     21      converting the proprietary binary output files of a MEG device in
     22      Tuebingen (Germany) with an unkown tool.
     23
     24      The file format is line-based, i.e. all timepoints for all samples/trials
     25      are written in a single line. Each line is prefixed with an identifier
     26      (using a colon as the delimiter between identifier and data). Two lines
     27      have a special purpose. The first 'Sample Number' is a list of timepoint
     28      ids, similar to `range(ntimepoints)` for each sample/trial (all
     29      concatenated into one line. The second 'Time' contains the timing
     30      information for each timepoint (relative to stimulus onset), again for all
     31      trials concatenated into a single line.
     32
     33      All other lines contain various information (channels) recorded during
     34      the experiment. The meaning of some channels is unknown. Known ones are:
     35
     36        M*: MEG channels
     37        EEG*: EEG channels
     38        ADC*: Analog to digital converter output
     39
     40      Dataset properties are available from various class attributes. The `data`
     41      member provides all data from all channels (except for 'Sample Number' and
     42      'Time') in a NumPy array (nsamples x nchannels x ntimepoints).
     43
     44      The reader supports uncompressed as well as gzipped input files (or other
     45      file-like objects).
     46      """
     47
     48 -    def __init__(self, source):
     49          """Reader MEG data from texfiles or file-like objects.
     50
     51          :Parameters:
     52            source: str | file-like
     53              Strings are assumed to be filenames (with `.gz` suffix
     54              compressed), while all other object types are treated as file-like
     55              objects.
     56          """
     57          self.ntimepoints = None
     58          self.timepoints = None
     59          self.nsamples = None
     60          self.channelids = []
     61          self.data = []
     62          self.samplingrate = None
     63
     64          # open textfiles
     65          if isinstance(source, str):
     66              if source.endswith('.gz'):
     67                  externals.exists('gzip', raiseException=True)
     68                  import gzip
     69                  source = gzip.open(source, 'r')
     70              else:
     71                  source = open(source, 'r')
     72
     73          # read file
     74          for line in source:
     75              # split ID
     76              colon = line.find(':')
     77
     78              # ignore lines without id
     79              if colon == -1:
     80                  continue
     81
     82              id = line[:colon]
     83              data = line[colon+1:].strip()
     84              if id == 'Sample Number':
     85                  timepoints = N.fromstring(data, dtype=int, sep='\t')
     86                  # one more as it starts with zero
     87                  self.ntimepoints = int(timepoints.max()) + 1
     88                  self.nsamples = int(len(timepoints) / self.ntimepoints)
     89              elif id == 'Time':
     90                  self.timepoints = N.fromstring(data,
     91                                                 dtype=float,
     92                                                 count=self.ntimepoints,
     93                                                 sep='\t')
     94                  self.samplingrate = self.ntimepoints \
     95                      / (self.timepoints[-1] - self.timepoints[0])
     96              else:
     97                  # load data
     98                  self.data.append(
     99                      N.fromstring(data, dtype=float, sep='\t').reshape(
    100                          self.nsamples, self.ntimepoints))
    101                  # store id
    102                  self.channelids.append(id)
    103
    104          # reshape data from (channels x samples x timepoints) to
    105          # (samples x chanels x timepoints)
    106          self.data = N.swapaxes(N.array(self.data), 0, 1)
    107
    108
    109 -    def __str__(self):
    110          """Give a short summary.
    111          """
    112          return '<TuebingenMEG: %i samples, %i timepoints, %i channels>' \
    113                    % (self.nsamples, self.ntimepoints, len(self.channelids))
    114