Package mvpa :: Package misc :: Package io :: Module eepbin

[hide private]
[frames] | no frames]

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

      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  #   Derived from the EEP binary reader of the pybsig toolbox
      9  #   (C) by Ingo Fruend
     10  #
     11  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
     12  """Reader for binary EEP files."""
     13
     14  __docformat__ = 'restructuredtext'
     15
     16  import numpy as N
     17  from mvpa.misc.io import DataReader
     18
     19
     20 -class EEPBin(DataReader):
     21      """Read-access to binary EEP files.
     22
     23      EEP files are used by *eeprobe* a software for analysing even-related
     24      potentials (ERP), which was developed at the Max-Planck Institute for
     25      Cognitive Neuroscience in Leipzig, Germany.
     26
     27        http://www.ant-neuro.com/products/eeprobe
     28
     29      EEP files consist of a plain text header and a binary data block in a
     30      single file. The header starts with a line of the form
     31
     32      ';%d %d %d %g %g' % (Nchannels, Nsamples, Ntrials, t0, dt)
     33
     34      where Nchannels, Nsamples, Ntrials are the numbers of channels, samples
     35      per trial and trials respectively. t0 is the time of the first sample
     36      of a trial relative to the stimulus onset and dt is the sampling interval.
     37
     38      The binary data block consists of single precision floats arranged in the
     39      following way::
     40
     41          <trial1,channel1,sample1>,<trial1,channel1,sample2>,...
     42          <trial1,channel2,sample1>,<trial1,channel2,sample2>,...
     43          .
     44          <trial2,channel1,sample1>,<trial2,channel1,sample2>,...
     45          <trial2,channel2,sample1>,<trial2,channel2,sample2>,...
     46      """
     47 -    def __init__(self, source):
     48          """Read EEP file and store header and data.
     49
     50          :Parameter:
     51            source : str
     52              Filename.
     53          """
     54          # init base class
     55          DataReader.__init__(self)
     56          # temp storage of number of samples
     57          nsamples = None
     58          # non-critical header components stored in temp dict
     59          hdr = {}
     60
     61          infile = open(source, "r")
     62
     63          # read file the end of header of EOF
     64          while True:
     65              # one line at a time
     66              line = infile.readline()
     67
     68              # stop if EOH or EOF
     69              if not line or line.startswith(';EOH;'):
     70                  break
     71
     72              # no crap!
     73              line = line.strip()
     74
     75              # all but first line as colon
     76              if not line.count(':'):
     77                  # top header
     78                  l = line.split()
     79                  # extract critical information
     80                  self._props['nchannels'] = int(l[0][1:])
     81                  self._props['ntimepoints'] = int(l[1])
     82                  self._props['t0'] = float(l[3])
     83                  self._props['dt'] = float(l[4])
     84                  nsamples = int(l[2])
     85              else:
     86                  # simply store non-critical extras
     87                  l = line.split(':')
     88                  key = l[0].lstrip(';')
     89                  value = ':'.join(l[1:])
     90                  hdr[key] = value
     91
     92          # post process channel name info -> list
     93          if hdr.has_key('channels'):
     94              self._props['channels'] = hdr['channels'].split()
     95
     96          self._data = \
     97              N.reshape(N.fromfile(infile, dtype='f'), \
     98                  (nsamples,
     99                   self._props['nchannels'],
    100                   self._props['ntimepoints']))
    101
    102          # cleanup
    103          infile.close()
    104
    105
    106      nchannels = property(fget=lambda self: self._props['nchannels'],
    107                           doc="Number of channels")
    108      ntimepoints  = property(fget=lambda self: self._props['ntimepoints'],
    109                           doc="Number of data timepoints")
    110      nsamples   = property(fget=lambda self: self._data.shape[0],
    111                           doc="Number of trials/samples")
    112      t0        = property(fget=lambda self: self._props['t0'],
    113                           doc="Relative start time of sampling interval")
    114      dt        = property(fget=lambda self: self._props['dt'],
    115                           doc="Time difference between two adjacent samples")
    116      channels  = property(fget=lambda self: self._props['channels'],
    117                           doc="List of channel names")
    118