Package mvpa :: Package misc :: Package fsl :: Module base
[hide private]
[frames] | no frames]

Source Code for Module mvpa.misc.fsl.base

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set 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  """Tiny snippets to interface with FSL easily.""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  from mvpa.misc.io import ColumnData 
 14  from mvpa.misc.support import Event 
 15   
 16  if __debug__: 
 17      from mvpa.base import debug 
 18   
 19   
20 -class FslEV3(ColumnData):
21 """IO helper to read FSL's EV3 files. 22 23 This is a three-column textfile format that is used to specify stimulation 24 protocols for fMRI data analysis in FSL's FEAT module. 25 26 Data is always read as `float`. 27 """
28 - def __init__(self, source):
29 """Read and write FSL EV3 files. 30 31 :Parameter: 32 source: filename of an EV3 file 33 """ 34 # init data from known format 35 ColumnData.__init__(self, source, 36 header=['onsets', 'durations', 'intensities'], 37 sep=None, dtype=float)
38 39
40 - def getNEVs(self):
41 """Returns the number of EVs in the file. 42 """ 43 return self.getNRows()
44 45
46 - def getEV(self, evid):
47 """Returns a tuple of (onset time, simulus duration, intensity) for a 48 certain EV. 49 """ 50 return (self['onsets'][evid], 51 self['durations'][evid], 52 self['intensities'][evid])
53 54
55 - def tofile(self, filename):
56 """Write data to a FSL EV3 file. 57 """ 58 ColumnData.tofile(self, filename, 59 header=False, 60 header_order=['onsets', 'durations', 'intensities'], 61 sep=' ')
62 63
64 - def toEvents(self):
65 """Convert into a list of `Event` instances. 66 """ 67 return \ 68 [Event(onset=self['onsets'][i], 69 duration=self['durations'][i], 70 features=[self['intensities'][i]]) 71 for i in xrange(self.nevs)]
72 73 74 onsets = property(fget=lambda self: self['onsets']) 75 durations = property(fget=lambda self: self['durations']) 76 intensities = property(fget=lambda self: self['intensities']) 77 nevs = property(fget=getNEVs)
78 79 80
81 -class McFlirtParams(ColumnData):
82 """Read and write McFlirt's motion estimation parameters from and to text 83 files. 84 """ 85 header_def = ['rot1', 'rot2', 'rot3', 'x', 'y', 'z'] 86
87 - def __init__(self, source):
88 """ 89 :Parameter: 90 91 source: str 92 Filename of a parameter file. 93 """ 94 ColumnData.__init__(self, source, 95 header=McFlirtParams.header_def, 96 sep=None, dtype=float)
97 98
99 - def tofile(self, filename):
100 """Write motion parameters to file. 101 """ 102 ColumnData.tofile(self, filename, 103 header=False, 104 header_order=McFlirtParams.header_def, 105 sep=' ')
106 107
108 - def plot(self):
109 """Produce a simple plot of the estimated translation and rotation 110 parameters using. 111 112 You still need to can pylab.show() or pylab.savefig() if you want to 113 see/get anything. 114 """ 115 # import internally as it takes some time and might not be needed most 116 # of the time 117 import pylab as P 118 119 # translations subplot 120 P.subplot(211) 121 P.plot(self.x) 122 P.plot(self.y) 123 P.plot(self.z) 124 P.ylabel('Translations in mm') 125 P.legend(('x', 'y', 'z'), loc=0) 126 127 # rotations subplot 128 P.subplot(212) 129 P.plot(self.rot1) 130 P.plot(self.rot2) 131 P.plot(self.rot3) 132 P.ylabel('Rotations in rad') 133 P.legend(('rot1', 'rot2', 'rot3'), loc=0)
134 135
136 - def toarray(self):
137 """Returns the data as an array with six columns (same order as in file). 138 """ 139 import numpy as N 140 141 # return as array with time axis first 142 return N.array([self[i] for i in McFlirtParams.header_def], 143 dtype='float').T
144