cmd.ppi.2.make.regs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/tcsh
  2. # - basis functions should be consistent across classes
  3. # i.e. should not mix GAM/BLOCK
  4. # - SPMG1 is okay, but no multi-component functions (AM/DM/IM are okay)
  5. # generate PPI regressors: no 3dD commands
  6. # inputs
  7. # - stim timing files/labels/durations
  8. # - PPI label (e.g. Lamy.test1)
  9. # - seed time series (afni_proc.py can generate example)
  10. # - NT per run, TR, TRnup
  11. set stim_files = ( stimuli/AV*.txt )
  12. set stim_labs = ( vis aud )
  13. set stim_dur = ( 20 20 ) # durations, in seconds
  14. set seed = ppi.seed.1D
  15. set plabel = Laud
  16. set basis = BLOCK # matches basis type in main analysis
  17. set NT = ( 150 150 150 ) # num time points per run
  18. set TR = 2.0
  19. set TRnup = 20 # oversample rate
  20. # compute some items
  21. # rcr - validate TRup (TR must be an integral multiple of TRup)
  22. set TRup = 0.1 # basically TR/TRnup
  23. set demean_psych = 0 # usually 0 (for comparison, should not matter)
  24. set nstim = $#stim_files
  25. set run_lens = ( 300 300 300 ) # run lengths, in seconds
  26. set workdir = work.$plabel
  27. set timingdir = timing.files
  28. # =================================================================
  29. # create work directory, copy inputs, enter
  30. if ( -d $workdir ) then
  31. # for convenience, but not recommended as a default
  32. # echo "** removing old work dir $workdir..."
  33. # rm -fr $workdir
  34. # these make a safer default
  35. echo "** will not overwrite PPI work directory $workdir, failing..."
  36. exit 1
  37. endif
  38. # create output directories and copy inputs there
  39. mkdir $workdir
  40. mkdir $workdir/$timingdir
  41. cp -pv $stim_files $workdir/$timingdir
  42. cp -pv $seed $workdir
  43. set seed = $seed:t
  44. set bind = 0
  45. cd $workdir
  46. # =================================================================
  47. # generate ideal IRF
  48. #
  49. # This generates the impulse response function for the deconvolution
  50. # and recovolution steps. It is the expected response to a ~zero
  51. # duration event.
  52. if ( $basis == GAM ) then
  53. # number of time points = duration / upsampled TR
  54. set dur = 12 # use a 12 second curve for GAM
  55. set nt_irf = `ccalc -i "$dur/$TRup"`
  56. set hrf_file = x.GAM.1D
  57. 3dDeconvolve -nodata $nt_irf 0.1 -polort -1 \
  58. -num_stimts 1 \
  59. -stim_times 1 1D:0 GAM \
  60. -x1D $hrf_file -x1D_stop
  61. else if ( $basis == BLOCK ) then
  62. # number of time points = duration / upsampled TR
  63. set dur = 15 # use a 15 second curve for BLOCK
  64. set nt_irf = `ccalc -i "$dur/$TRup"`
  65. set hrf_file = x.BLOCK.1D
  66. 3dDeconvolve -nodata $nt_irf 0.1 -polort -1 \
  67. -num_stimts 1 \
  68. -stim_times 1 1D:0 "BLOCK(0.1,1)" \
  69. -x1D $hrf_file -x1D_stop
  70. else
  71. echo "** invalid basis $basis, should be BLOCK or GAM (or SPMG1)"
  72. exit 1
  73. endif
  74. # =================================================================
  75. # create timing partition files
  76. @ bind ++
  77. set prefix = p$bind.$plabel
  78. set timing_prefix = $prefix
  79. foreach sind ( `count -digits 1 1 $nstim` )
  80. set sind2 = `ccalc -form '%02d' $sind`
  81. set tfile = $timingdir/$stim_files[$sind]:t
  82. set label = $stim_labs[$sind]
  83. if ( ! -f $tfile ) then
  84. echo "** missing timing file $tfile"
  85. exit 1
  86. endif
  87. timing_tool.py -timing $tfile \
  88. -tr $TRup -stim_dur $stim_dur[$sind] \
  89. -run_len $run_lens \
  90. -min_frac 0.3 \
  91. -timing_to_1D $timing_prefix.$sind2.$label \
  92. -per_run_file -show_timing
  93. # optionally replace psychological variables with de-meaned versions
  94. if ( $demean_psych ) then
  95. set mean = `cat $timing_prefix.$sind2.* | 3dTstat -prefix - 1D:stdin\'`
  96. echo "-- mean of psych '$label' files = $mean"
  97. foreach file ( $timing_prefix.$sind2.$label* )
  98. 1deval -a $file -expr "a-$mean" > rm.1D
  99. mv rm.1D $file
  100. end
  101. endif
  102. end
  103. # =================================================================
  104. # upsample seed
  105. @ bind ++
  106. set prefix = p$bind.$plabel
  107. # break into n runs
  108. @ rend = - 1
  109. foreach rind ( `count -digits 1 1 $#NT` )
  110. @ rstart = $rend + 1 # start after prior endpoint
  111. @ rend += $NT[$rind]
  112. 1dcat $seed"{$rstart..$rend}" | 1dUpsample $TRnup stdin: \
  113. > $prefix.seed.$TRnup.r$rind.1D
  114. end
  115. set seed_up = $prefix.seed.$TRnup.rall.1D
  116. cat $prefix.seed.$TRnup.r[0-9]*.1D > $seed_up
  117. # =================================================================
  118. # deconvolve
  119. set pprev = $prefix
  120. @ bind ++
  121. set prefix = p$bind.$plabel
  122. set neuro_prefix = $prefix
  123. foreach rind ( `count -digits 1 1 $#NT` )
  124. 3dTfitter -RHS $pprev.seed.$TRnup.r$rind.1D \
  125. -FALTUNG $hrf_file temp.1D 012 -2 \
  126. -l2lasso -6
  127. 1dtranspose temp.1D > $prefix.neuro.r$rind.1D
  128. end
  129. # ===========================================================================
  130. # partition neuro seeds
  131. set pprev = $prefix
  132. @ bind ++
  133. set prefix = p$bind.$plabel
  134. foreach sind ( `count -digits 1 1 $nstim` )
  135. set sind2 = `ccalc -form '%02d' $sind`
  136. set slab = $sind2.$stim_labs[$sind]
  137. foreach rind ( `count -digits 1 1 $#NT` )
  138. set neuro_seed = $neuro_prefix.neuro.r$rind.1D
  139. set rind2 = `ccalc -form '%02d' $rind`
  140. @ nt = $NT[$rind] * $TRnup
  141. # note partition files: 1 input, 2 outputs
  142. set stim_part = $timing_prefix.${slab}_r$rind2.1D
  143. set neuro_part = $prefix.a.$slab.r$rind.neuro_part.1D
  144. set recon_part = $prefix.b.$slab.r$rind.reBOLD.1D
  145. 1deval -a $neuro_seed -b $stim_part -expr 'a*b' > $neuro_part
  146. waver -FILE $TRup $hrf_file -input $neuro_part -numout $nt > $recon_part
  147. end
  148. # and generate upsampled seeds that span runs
  149. cat $prefix.b.$slab.r*.reBOLD.1D > $prefix.d.$slab.rall.reBOLD.1D
  150. end
  151. # and generate corresponding (reBOLD) seed time series
  152. foreach rind ( `count -digits 1 1 $#NT` )
  153. set neuro_seed = $neuro_prefix.neuro.r$rind.1D
  154. waver -FILE $TRup $hrf_file -input $neuro_seed -numout $nt \
  155. > $prefix.c.seed.r$rind.reBOLD.1D
  156. end
  157. # to compare with $seed_up
  158. 3dMean -sum -prefix - $prefix.d.[0-9]*.1D > $prefix.d.task.rall.reBOLD.1D
  159. cat $prefix.c.seed.r*.reBOLD.1D > $prefix.d.seed.rall.reBOLD.1D
  160. echo == can compare upsampled seeds: \
  161. $seed_up $prefix.d.{seed,task}.rall.reBOLD.1D
  162. set seed_rebold_up = $prefix.d.seed.rall.reBOLD.1D
  163. # ===========================================================================
  164. # downsample
  165. set pprev = $prefix
  166. @ bind ++
  167. set prefix = p$bind.$plabel
  168. foreach rind ( `count -digits 1 1 $#NT` )
  169. set neuro_seed = $neuro_prefix.neuro.r$rind.1D
  170. @ nt = $NT[$rind] * $TRnup
  171. foreach sind ( `count -digits 1 1 $nstim` )
  172. set sind2 = `ccalc -form '%02d' $sind`
  173. set recon_part = $pprev.b.$sind2.$stim_labs[$sind].r$rind.reBOLD.1D
  174. set recon_down = $prefix.$sind2.$stim_labs[$sind].r$rind.PPIdown.1D
  175. 1dcat $recon_part'{0..$('$TRnup')}' > $recon_down
  176. end
  177. # and downsample filtered seed time series
  178. 1dcat $seed_rebold_up'{0..$('$TRnup')}' > $seed:r.reBOLD.1D
  179. end
  180. # ===========================================================================
  181. # catentate across runs: final PPI regressors
  182. set pprev = $prefix
  183. @ bind ++
  184. set prefix = p$bind.$plabel
  185. foreach sind ( `count -digits 1 1 $nstim` )
  186. set sind2 = `ccalc -form '%02d' $sind`
  187. set slab = $sind2.$stim_labs[$sind]
  188. cat $pprev.$slab.r*.PPIdown.1D > $prefix.$slab.rall.PPI.1D
  189. end
  190. # =================================================================
  191. # make a final comparison time series
  192. set pprev = $prefix
  193. @ bind ++
  194. set prefix = p$bind.$plabel
  195. 3dMean -sum -prefix - $pprev.* > $prefix.sum.PPI.1D
  196. echo "== can compare original seed to sum of PPI regressors:"
  197. echo " 1dplot -one $seed $prefix.sum.PPI.1D"
  198. echo ""
  199. echo "== final PPI regressors: " $seed $pprev.*
  200. echo " (copy to stimuli dir)"
  201. echo ""