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

Source Code for Module mvpa.misc.fsl.flobs

  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  """Wrapper around FSLs halfcosbasis to generate HRF kernels""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  import os 
 14  import tempfile 
 15  import shutil 
 16  import numpy as N 
 17  import math 
 18   
19 -def makeFlobs(pre=0, rise=5, fall=5, undershoot=5, undershootamp=0.3, 20 nsamples=1, resolution=0.05, nsecs=-1, nbasisfns=2):
21 """Wrapper around the FSL tool halfcosbasis. 22 23 This function uses halfcosbasis to generate samples of HRF kernels. 24 Kernel parameters can be modified analogous to the Make_flobs GUI 25 which is part of FSL. 26 27 28 ^ /-\\ 29 | / \\ 30 1 / \\ 31 | / \\ 32 | / \\ 33 | / \\ 34 -----/ \\ /----- | 35 \\--/ | undershootamp 36 | | | | | 37 | | | | | 38 39 pre rise fall undershoot 40 41 Parameters 'pre', 'rise', 'fall', 'undershoot' and 'undershootamp' 42 can be specified as 2-tuples (min-max range for sampling) and single 43 value (setting exact values -- no sampling). 44 45 If 'nsec' is negative, the length of the samples is determined 46 automatically to include the whole kernel function (until it returns 47 to baseline). 'nsec' has to be an integer value and is set to the next 48 greater integer value if it is not. 49 50 All parameters except for 'nsamples' and 'nbasisfns' are in seconds. 51 """ 52 # create tempdir and temporary parameter file 53 pfile, pfilename = tempfile.mkstemp('pyflobs') 54 wdir = tempfile.mkdtemp('pyflobs') 55 56 # halfcosbasis can only handle >1 samples 57 # so we simply compute two and later ignore the other 58 if nsamples < 2: 59 rnsamples = 2 60 else: 61 rnsamples = nsamples 62 63 # make range tuples if not supplied 64 if not isinstance(pre, tuple): 65 pre = (pre, pre) 66 if not isinstance(rise, tuple): 67 rise = (rise, rise) 68 if not isinstance(fall, tuple): 69 fall = (fall, fall) 70 if not isinstance(undershoot, tuple): 71 undershoot = (undershoot, undershoot) 72 if not isinstance(undershootamp, tuple): 73 undershootamp = (undershootamp, undershootamp) 74 75 # calc minimum length of hrf if not specified 76 # looks like it has to be an integer 77 if nsecs < 0: 78 nsecs = int( math.ceil( pre[1] \ 79 + rise[1] \ 80 + fall[1] \ 81 + undershoot[1] \ 82 + resolution ) ) 83 else: 84 nsecs = math.ceil(nsecs) 85 86 # write parameter file 87 pfile = os.fdopen( pfile, 'w' ) 88 89 pfile.write(str(pre[0]) + ' ' + str(pre[1]) + '\n') 90 pfile.write(str(rise[0]) + ' ' + str(rise[1]) + '\n') 91 pfile.write(str(fall[0]) + ' ' + str(fall[1]) + '\n') 92 pfile.write(str(undershoot[0]) + ' ' + str(undershoot[1]) + '\n') 93 pfile.write('0 0\n0 0\n') 94 pfile.write(str(undershootamp[0]) + ' ' + str(undershootamp[1]) + '\n') 95 pfile.write('0 0\n') 96 97 pfile.close() 98 99 # call halfcosbasis to generate the hrf samples 100 tochild, fromchild, childerror = os.popen3('halfcosbasis' 101 + ' --hf=' + pfilename 102 + ' --nbfs=' + str(nbasisfns) 103 + ' --ns=' + str(nsecs) 104 + ' --logdir=' + os.path.join(wdir, 'out') 105 + ' --nhs=' + str(rnsamples) 106 + ' --res=' + str(resolution) ) 107 err = childerror.readlines() 108 if len(err) > 0: 109 print err 110 raise RuntimeError, "Problem while running halfcosbasis." 111 112 # read samples from file into an array 113 hrfs = N.fromfile( os.path.join( wdir, 'out', 'hrfsamps.txt' ), 114 sep = ' ' ) 115 116 # reshape array to get one sample per row and 1d array only 117 # for one sample hrf 118 hrfs = \ 119 hrfs.reshape( len(hrfs)/rnsamples, rnsamples).T[:nsamples].squeeze() 120 121 # cleanup working dir (ignore errors) 122 shutil.rmtree( wdir, True ) 123 # remove paramter file 124 os.remove( pfilename ) 125 126 # and return an array 127 return( hrfs )
128