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, **kwargs):
65 """Convert into a list of `Event` instances. 66 67 :Parameters: 68 kwargs 69 Any keyword arugment provided would be replicated, through all 70 the entries. Useful to specify label or even a chunk 71 """ 72 return \ 73 [Event(onset=self['onsets'][i], 74 duration=self['durations'][i], 75 features=[self['intensities'][i]], 76 **kwargs) 77 for i in xrange(self.nevs)]
78 79 80 onsets = property(fget=lambda self: self['onsets']) 81 durations = property(fget=lambda self: self['durations']) 82 intensities = property(fget=lambda self: self['intensities']) 83 nevs = property(fget=getNEVs)
84 85 86
87 -class McFlirtParams(ColumnData):
88 """Read and write McFlirt's motion estimation parameters from and to text 89 files. 90 """ 91 header_def = ['rot1', 'rot2', 'rot3', 'x', 'y', 'z'] 92
93 - def __init__(self, source):
94 """ 95 :Parameter: 96 97 source: str 98 Filename of a parameter file. 99 """ 100 ColumnData.__init__(self, source, 101 header=McFlirtParams.header_def, 102 sep=None, dtype=float)
103 104
105 - def tofile(self, filename):
106 """Write motion parameters to file. 107 """ 108 ColumnData.tofile(self, filename, 109 header=False, 110 header_order=McFlirtParams.header_def, 111 sep=' ')
112 113
114 - def plot(self):
115 """Produce a simple plot of the estimated translation and rotation 116 parameters using. 117 118 You still need to can pylab.show() or pylab.savefig() if you want to 119 see/get anything. 120 """ 121 # import internally as it takes some time and might not be needed most 122 # of the time 123 import pylab as P 124 125 # translations subplot 126 P.subplot(211) 127 P.plot(self.x) 128 P.plot(self.y) 129 P.plot(self.z) 130 P.ylabel('Translations in mm') 131 P.legend(('x', 'y', 'z'), loc=0) 132 133 # rotations subplot 134 P.subplot(212) 135 P.plot(self.rot1) 136 P.plot(self.rot2) 137 P.plot(self.rot3) 138 P.ylabel('Rotations in rad') 139 P.legend(('rot1', 'rot2', 'rot3'), loc=0)
140 141
142 - def toarray(self):
143 """Returns the data as an array with six columns (same order as in file). 144 """ 145 import numpy as N 146 147 # return as array with time axis first 148 return N.array([self[i] for i in McFlirtParams.header_def], 149 dtype='float').T
150