1#!/bin/tcsh -f
2@global_parse `basename $0` "$*" ; if ($status) exit 0
3set stat = 0
4set stdir = $PWD
5
6goto PARSE
7RET_PARSE:
8
9LOCALSTATS:
10   cd $stdir
11   if ( ! -d $odir) mkdir -p $odir
12
13   set sopt = ''
14   if ($#statvector == 3 && \
15         $statvector[1] == "median" && \
16         $statvector[2] == "MAD" && \
17         $statvector[3] == "P2skew" ) then
18      set sopt = ('-stat mMP2s')
19   else if ($#statvector == 4 && \
20         $statvector[1] == "mean" && \
21         $statvector[2] == "median" && \
22         $statvector[2] == "MAD" && \
23         $statvector[3] == "P2skew" ) then
24      set sopt = ('-stat mMP2s')
25   else
26      foreach lstat ($statvector)
27         set sopt = ($sopt '-stat' $lstat)
28      end
29   endif
30
31   set mopt = ''
32   if ($inmask != '') set mopt = "-mask $inmask"
33   set inpref = `@GetAfniPrefix $involume | sed 's/.nii$//g'`
34   if ( ! -f $odir/${inpref}+orig.HEAD) then
35      3dcalc -a "${involume}" -expr 'a' -datum short -prefix $odir/${inpref}+orig
36   endif
37
38   foreach neigh ($localvector)
39      set oneigh = `printf '%02d' $neigh`
40      set sublopt = ''
41      set cnt = 1
42      foreach lstat ($statvector)
43         set ss = $statlabelvector[$cnt].${oneigh}_mm
44         set sublopt = ($sublopt -sublabel `ccalc -i $cnt -1` $ss)
45         @ cnt ++
46      end
47      if ( ! -f $odir/${inpref}.${olabel}.${oneigh}+orig.HEAD ) then
48         echo "Running 3dLocalstat $neigh on $involume...`date`"
49         3dLocalstat   $mopt \
50                       -nbhd "RECT($neigh,$neigh,$neigh)" \
51                       $sopt -label_ext ${oneigh}_mm \
52                       -datum short -reduce_max_vox $max_vox \
53                       -prefix $odir/${inpref}.${olabel}.${oneigh} \
54                       "${involume}"
55         #3drefit $sublopt $odir/${inpref}.${olabel}.${oneigh}+orig
56      else
57         echo "$odir/${inpref}.${olabel}.${oneigh} exists already"
58      endif
59   end
60   if ( $Scale == 9) then
61      if ( ! -f $odir/${inpref}.sc${fatScale}.perc+orig.HEAD ) then
62         echo "Running 3dLocalstat $fatScale on $involume for scaling...`date`"
63         3dLocalstat  \
64                 -nbhd "SPHERE($fatScale)" \
65                 -stat  perc:65:95:5 \
66                 -datum short -reduce_max_vox $max_vox \
67                 -prefix $odir/${inpref}.sc${fatScale}.perc \
68                 "${involume}"
69      else
70         echo "$odir/${inpref}.sc${fatScale}.perc exists already"
71      endif
72   endif
73   if ( "$Mname" != "" ) then
74      if ("$Mname" == "AUTO") then
75               cd $odir
76         if ( ! -f T1.uni+orig.HEAD) then
77            if ( $Scale == 9) then
78               cd $stdir
79               @T1mask -T1 $involume -hullmask 1.0 -odir $odir
80               cd $odir
81               set sbscale = \
82                  `3dinfo -label2index "perc:${fatPerc}.00"   \
83                              ${inpref}.sc${fatScale}.perc+orig`
84               if ("$sbscale" == '') then
85                  echo "Could not find sub-brick 'perc:${fatPerc}.00'"
86                  echo " in ${inpref}.sc${fatScale}.perc+orig"
87                  goto BEND
88               endif
89               3dcalc -a T1+orig \
90                      -b ${inpref}.sc${fatScale}.perc+orig"[$sbscale]"   \
91                      -c T1.hull.mask.m+orig \
92                      -expr 'step(c-1)*step(b)*a/b'   \
93                      -prefix T1.uni
94            else
95               cd $stdir
96               @T1scale -T1 $involume -brainhull 1.0 -odir $odir
97               cd $odir
98            endif
99         else
100            echo "Reusing T1.uni+orig"
101         endif
102         if ( ! -f T1.uni.sn+tlrc.HEAD) then
103            #Run 3dSpatNorm to get rid of basal voxels, and auto_tlrc
104            3dSpatNorm  -overwrite -orig_space -prefix T1.uni.sn \
105                        -bottom_cuts ALRP T1.uni+orig
106            @auto_tlrc  -base MNI_sk_avg152T1+tlrc  -init_xform AUTO_CENTER \
107                        -no_ss -input T1.uni.sn+orig
108            if ( ! -f MNI_sk152T1+tlrc.HEAD ) then
109               @auto_tlrc  -base MNI_sk_avg152T1+tlrc -base_copy MNI_sk152T1
110            endif
111         else
112            echo "Reusing T1.uni.sn+tlrc.HEAD"
113         endif
114         if ( ! -f T1.uni.sn.Xat.1D) then
115            echo "Failed to run @auto_tlrc"
116            goto BEND
117         endif
118         cp -p T1.uni.sn.Xat.1D ${inpref}.Xat.1D
119            cd $stdir
120      else
121         cp -p $Mname $odir/${inpref}.Xat.1D
122      endif
123      set Mname = ${inpref}.Xat.1D
124   endif
125
126REGROUP:
127   cd $stdir
128
129   cd $odir
130   set cnt = 1
131   set AddedAnat = 0
132   foreach lstat ($statvector)
133      if ( ! -f ${inpref}.us.${lstat}+orig.HEAD) then
134         echo "Creating ${inpref}.us.${lstat} ..."
135
136         set sbl = ()
137         foreach neigh ($localvector)
138            set oneigh = `printf '%02d' $neigh`
139            set statlabel = $statlabelvector[$cnt].${oneigh}_mm
140            set sbl = ("$sbl" ${inpref}.${olabel}.${oneigh}+orig"[$statlabel]")
141         end
142         if ($prepend_anat && $AddedAnat == 0 && \
143             ($lstat =~ med* || $lstat =~ mean*)) then
144            set AddAnatOpt = "${inpref}+orig"
145         else
146            set AddAnatOpt = ''
147         endif
148         set noglob
149         3dTcat -prefix ${inpref}.us.${lstat} $AddAnatOpt $sbl
150         if ( "$AddAnatOpt" != '') then
151            if ($lstat =~ med*) then
152               3drefit -sublabel 0 MEDIAN.00_mm ${inpref}.us.${lstat}+orig
153            else
154               3drefit -sublabel 0 MEAN.00_mm ${inpref}.us.${lstat}+orig
155            endif
156            set AddedAnat = 1
157         endif
158         unset noglob
159      else
160         echo "Reusing ${inpref}.us.${lstat} ..."
161      endif
162      @ cnt ++
163   end
164   cd $stdir
165
166SCALE:
167   cd $stdir
168
169   cd $odir
170   set cnt = 1
171   foreach lstat ($statvector)
172      set sss = "${inpref}.us.${lstat}"
173      set ttt = "${inpref}.sc${Scale}.${lstat}"
174      if ( -f ${inpref}.${lstat}+orig.HEAD) then
175         rm -f ${inpref}.${lstat}+orig.*
176      endif
177      if ($Scale == 1) then
178         echo "Scaling by each sb median (overwriting) ${sss} to ${ttt} ..."
179         @ScaleVolume -scale_by_median -input ${sss}+orig -prefix ${ttt}
180      else if ($Scale == 2) then
181         echo "Scaling by $ScaleVal (overwriting) ${sss} to ${ttt} ..."
182         3dcalc -a ${sss}+orig -prefix ${ttt} -expr "a/$ScaleVal"
183      else if ($Scale == 3) then
184         echo "normalizing (overwriting) ${sss} to ${ttt} ..."
185         @ScaleVolume -norm -input ${sss}+orig -prefix ${ttt}
186      else if ($Scale == 4) then
187         #do nothing, deal with speciality below
188      else if ($Scale == 5) then
189         #do nothing, deal with speciality below
190      else if ($Scale == 6) then
191         #do nothing, deal with speciality below
192      else if ($Scale == 7) then
193         #do nothing, deal with speciality below
194      else if ($Scale == 8) then
195         #do nothing, deal with speciality below
196      else if ($Scale == 9) then
197         #do nothing, deal with speciality below
198      else if ($Scale == 0) then
199         echo "Copying unscaled signatures"
200         3dcopy $sss+orig $ttt
201      else
202         echo "Bad scale value of $Scale"
203         goto END
204      endif
205      @ cnt ++
206   end
207   if ($Scale == 4) then
208      #Only scale median signature per the equations derived
209      foreach lstat ($statvector)
210         set sss = "${inpref}.us.${lstat}"
211         set ttt = "${inpref}.sc${Scale}.${lstat}"
212         if ($lstat =~ med*) then
213            3dDetrend -overwrite -prefix ___dt -polort 0 ${sss}+orig
214            3dcalc -overwrite -a ___dt+orig -b ${inpref}.us.MAD+orig \
215                     -expr '(step(b)*a/b)' -prefix $ttt
216            rm -f ___dt+orig*
217         else
218            #Just copy for ease on other scripts, nothing to do
219            3dcopy $sss+orig $ttt
220         endif
221      end
222   else if ($Scale == 5 || $Scale == 6) then
223      #Only scale median signature per the equations derived
224      #But only use last MAD sub-brick.
225      #Also, scale MAD by last sub-brick
226      set ttal = ()
227      foreach lstat ($statvector)
228         set sss = "${inpref}.us.${lstat}"
229         set ttt = "${inpref}.sc${Scale}.${lstat}"
230         if ( ! -f ${inpref}.sc${Scale}.${lstat}+orig.HEAD) then
231            echo "Creating $ttt"
232            if ($lstat =~ med*) then
233               if ($Scale == 5) then
234                  3dDetrend -overwrite -prefix ___dt -polort 0 ${sss}+orig
235               else if ($Scale == 6) then
236                  #prepend anatomy
237                  3dbucket -prefix ___adt+orig "$stdir/${involume}" ${sss}+orig
238                  3drefit -sublabel 0 MEDIAN.00_mm ___adt+orig
239                  3dDetrend -overwrite -prefix ___dt -polort 0 ___adt+orig
240               endif
241               3dcalc -overwrite -a ___dt+orig -b ${inpref}.us.MAD+orig'[$]' \
242                        -expr '(step(b)*a/b)' -prefix $ttt
243               rm -f ___dt+orig* ___adt+orig*
244            else if ($lstat =~ MAD*) then
245               set mm = `3dnvals ${inpref}.us.MAD+orig`
246               @ mm --
247               @ mm --
248               3dcalc -overwrite -a ${inpref}.us.MAD+orig"[0..$mm]" \
249                                 -b ${inpref}.us.MAD+orig'[$]' \
250                        -expr '(step(b)*a/b)' -prefix $ttt
251            else
252               #Just copy for ease on other scripts, nothing to do
253               3dcopy $sss+orig $ttt
254            endif
255         else
256            echo "Reusing $ttt"
257         endif
258         set ttal = ($ttal $ttt+orig)
259      end
260      if ( ! -f ${inpref}.sc${Scale}.${sigset}+orig.HEAD) then
261         3dTcat -prefix ${inpref}.sc${Scale}.${sigset} $ttal
262      else
263         echo "Reusing ${inpref}.sc${Scale}.${sigset}"
264      endif
265   else if ($Scale == 7) then
266      set ttal = ()
267      foreach lstat ($statvector)
268         set sss = "${inpref}.us.${lstat}"
269         set ttt = "${inpref}.sc${Scale}.${lstat}"
270         if ( ! -f ${inpref}.sc${Scale}.${lstat}+orig.HEAD) then
271            echo "Creating $ttt"
272            if ($lstat =~ med*) then
273               set mm = `3dinfo -nvi ${sss}+orig`
274               @ mm --
275               3dcalc -overwrite -a ${sss}+orig"[0-$mm]" \
276                        -b ${inpref}.us.mean+orig'[$]' \
277                        -expr '(step(b)*a/b)' -prefix $ttt
278            else if ($lstat =~ MAD*) then
279               3dcalc -overwrite -a ${inpref}.us.MAD+orig \
280                                 -b ${inpref}.us.mean+orig'[$]' \
281                        -expr '(step(b)*a/b)' -prefix $ttt
282            else if ($lstat =~ mea*) then
283               set mm = `3dinfo -nvi ${sss}+orig`
284               @ mm --
285               3dcalc -overwrite -a ${sss}+orig"[0-$mm]" \
286                        -b ${inpref}.us.mean+orig'[$]' \
287                        -expr '(step(b)*a/b)' -prefix $ttt
288            else
289               #Just copy for ease on other scripts, nothing to do
290               3dcopy $sss+orig $ttt
291            endif
292         else
293            echo "Reusing $ttt"
294         endif
295         set ttal = ($ttal $ttt+orig)
296      end
297      if ( ! -f ${inpref}.sc${Scale}.${sigset}+orig.HEAD) then
298         3dTcat -prefix ${inpref}.sc${Scale}.${sigset} $ttal
299      else
300         echo "Reusing ${inpref}.sc${Scale}.${sigset}"
301      endif
302   else if ($Scale == 8) then
303      set ttal = ()
304      foreach lstat ($statvector)
305         set sss = "${inpref}.us.${lstat}"
306         set ttt = "${inpref}.sc${Scale}.${lstat}"
307         if ( ! -f ${inpref}.sc${Scale}.${lstat}+orig.HEAD) then
308            echo "Creating $ttt"
309            if ($lstat =~ med* || $lstat =~ mean*) then
310               3dcalc -overwrite -a ${sss}+orig -b ${inpref}.us.MAD+orig'[$]' \
311                        -expr '(step(b)*a/b)' -prefix $ttt
312            else if ($lstat =~ MAD*) then
313               set mm = `3dnvals ${inpref}.us.MAD+orig`
314               @ mm --
315               @ mm --
316               3dcalc -overwrite -a ${inpref}.us.MAD+orig"[0..$mm]" \
317                                 -b ${inpref}.us.MAD+orig'[$]' \
318                        -expr '(step(b)*a/b)' -prefix $ttt
319            else
320               #Just copy for ease on other scripts, nothing to do
321               3dcopy $sss+orig $ttt
322            endif
323            3dNotes -h "`basename $0` $*" $ttt+orig
324         else
325            echo "Reusing $ttt"
326         endif
327         set ttal = ($ttal $ttt+orig)
328      end
329      if ( ! -f ${inpref}.sc${Scale}.${sigset}+orig.HEAD) then
330         3dTcat -prefix ${inpref}.sc${Scale}.${sigset} $ttal
331      else
332         echo "Reusing ${inpref}.sc${Scale}.${sigset}"
333      endif
334   else if ($Scale == 9) then
335      if ( ! -f ${inpref}.sc${fatScale}.perc+orig.HEAD) then
336         echo "Error: Missing scaling volume ${inpref}.sc${fatScale}.perc"
337         goto BEND
338      else
339         set sbscale = \
340            `3dinfo -label2index "perc:${fatPerc}.00"   \
341                           ${inpref}.sc${fatScale}.perc+orig`
342         if ("$sbscale" == '') then
343            echo "Could not find sub-brick 'perc:${fatPerc}.00'"
344            echo " in ${inpref}.sc${fatScale}.perc+orig"
345            goto BEND
346         endif
347      endif
348
349      set ttal = ()
350      foreach lstat ($statvector)
351         set sss = "${inpref}.us.${lstat}"
352         set ttt = "${inpref}.sc${Scale}.${lstat}"
353         if ( ! -f ${inpref}.sc${Scale}.${lstat}+orig.HEAD) then
354            echo "Creating $ttt"
355            if ($lstat =~ med* || $lstat =~ mean* || $lstat =~ MAD*) then
356               3dcalc -overwrite -a ${sss}+orig \
357                      -b ${inpref}.sc${fatScale}.perc+orig"[$sbscale]" \
358                      -expr '(step(b)*a/b)' -prefix $ttt
359            else
360               #Just copy for ease on other scripts, nothing to do
361               3dcopy $sss+orig $ttt
362            endif
363            3dNotes -h "`basename $0` $*" $ttt+orig
364         else
365            echo "Reusing $ttt"
366         endif
367         set ttal = ($ttal $ttt+orig)
368      end
369      if ( ! -f ${inpref}.sc${Scale}.${sigset}+orig.HEAD) then
370         3dTcat -prefix ${inpref}.sc${Scale}.${sigset} $ttal
371         3drefit -sublabel_suffix .sc${Scale} ${inpref}.sc${Scale}.${sigset}+orig
372      else
373         echo "Reusing ${inpref}.sc${Scale}.${sigset}"
374      endif
375   endif
376
377
378   if ("$extrascl" == '-feat_znorm') then
379      if ( $featmask == '' ) then
380         if ( ! -f ${inpref}.am+orig.HEAD) then
381            3dAutomask -prefix ${inpref}.am ${inpref}+orig
382         else
383            echo "Reusing ${inpref}.am+orig.HEAD"
384         endif
385         set featmask = ${inpref}.am+orig
386      endif
387      if ( ! -f ${inpref}.sc${Scale}z.${sigset}+orig.HEAD) then
388         @ScaleVolume   -feat_znorm  -mask $featmask \
389                        -prefix  ${inpref}.sc${Scale}z.${sigset} \
390                        -input ${inpref}.sc${Scale}.${sigset}+orig
391      else
392         echo "Reusing ${inpref}.sc${Scale}z.${sigset}+orig.HEAD"
393      endif
394   endif
395
396   #Add XYZ features
397   if ( "$Mname" != "") then
398      #The matrix coming from @auto_tlrc is such that M Xt = Xo
399      #I want Xt = Mt Xo, so Mt = inv(M)
400      #So, Xt = Mt Xo and Xz = Mz Mt Xo
401      #Mz is obtained from @CreateMz, which creates Mnorm.1D
402      #
403      #Mall = Mz Mt, which is computed next
404      # Mz is manually inserted here from the output of @CreateMz
405      # with something like: set ll = ( `cat_matvec Mnorm.1D`)
406      #                      set ll = ( `echo $ll | sed 's/ /,/g'` )
407      # and put the content of $ll in MATRIX() below
408      set Mall = (`cat_matvec $Mname -I   'MATRIX(0.024764,0,0,0.012501,0,0.020628,0,-0.379561,0,0,0.0231,-0.006564)'`)
409
410      #Now I will use 3dcalc to compute Xz = Mall Xo, one coord at a time
411      #
412      set M = ($Mall) #notation convenience
413      if (! -f ${inpref}.Xz+orig.HEAD) then
414         3dcalc -RAI -overwrite -a ${inpref}+orig \
415                -expr "step(a)*(x*$M[1] + y*$M[2] + z*$M[3] + $M[4])"   \
416                -prefix ${inpref}.Xz
417         3drefit -sublabel 0 Xz ${inpref}.Xz+orig
418      else
419         echo "Reusing ${inpref}.Xz+orig.HEAD"
420      endif
421
422      if (! -f ${inpref}.Yz+orig.HEAD) then
423         3dcalc -RAI -overwrite -a ${inpref}+orig \
424                -expr "step(a)*(x*$M[5] + y*$M[6] + z*$M[7] + $M[8])"   \
425                -prefix ${inpref}.Yz
426         3drefit -sublabel 0 Yz ${inpref}.Yz+orig
427      else
428         echo "Reusing ${inpref}.Yz+orig.HEAD"
429      endif
430
431
432      if (! -f ${inpref}.Zz+orig.HEAD) then
433         3dcalc -RAI -overwrite -a ${inpref}+orig \
434                -expr "step(a)*(x*$M[9] + y*$M[10] + z*$M[11] + $M[12])"   \
435                -prefix ${inpref}.Zz
436         3drefit -sublabel 0 Zz ${inpref}.Zz+orig
437      else
438         echo "Reusing ${inpref}.Zz+orig.HEAD"
439      endif
440
441      foreach gg (${inpref}.sc${Scale}.${sigset}+orig.HEAD  \
442                  ${inpref}.sc${Scale}z.${sigset}+orig.HEAD )
443         if ( -f $gg ) then
444            set ok = (`3dinfo -label $gg | grep -w Xz | grep -w Yz | grep -w Zz`)
445            if ($status == 0) then
446             echo "It looks like $gg has Xz Yz and Zz in it, not adding anything"
447            else
448               3dTcat -glueto  $gg \
449                         ${inpref}.Xz+orig   \
450                         ${inpref}.Yz+orig   \
451                         ${inpref}.Zz+orig
452            endif
453         endif
454      end
455   endif
456
457   #make sure we have the history note
458   foreach gg (${inpref}.sc${Scale}.${sigset}+orig.HEAD  \
459                  ${inpref}.sc${Scale}z.${sigset}+orig.HEAD )
460      if ( -f $gg ) then
461         3dNotes -h "`basename $0` $*" $gg
462      endif
463   end
464
465   cd $stdir
466goto END
467
468
469PARSE:
470set Narg = $#
471set cnt = 1
472set starttime=`date`
473set resample = 1
474set statvector = ""
475set localvector = ""
476set cleanafter = 1
477set inmask = ""
478set odir = './'
479set olabel = 'loc_st'
480set sigset = 'sigset'
481set statopen = 0
482set localopen = 0
483set Scale = 0
484set ScaleVal = 0
485set max_vox = 0
486set prepend_anat = 0
487set extrascl = ''
488set Mname = ''
489set featmask = ''
490if ("$1" == '') goto HELP
491while ($cnt <= $Narg)
492   set donext = 1
493
494   if ($donext && "$argv[$cnt]" == "-help" || "$argv[$cnt]" == "-h") then
495      goto HELP
496   endif
497
498   if ($donext && ("$argv[$cnt]" == "-d" || "$argv[$cnt]" == "-echo")) then
499      set echo
500      set donext = 0
501   endif
502
503   if ($donext && "$argv[$cnt]" == "-feat_znorm") then
504      set extrascl = -feat_znorm
505      set donext = 0
506   endif
507
508   if ($donext && "$argv[$cnt]" == "-prepend_anat") then
509      set prepend_anat = 1
510      set donext = 0
511   endif
512
513   if ($donext && "$argv[$cnt]" == "-no_scale") then
514      set Scale = 0
515      set donext = 0
516   endif
517
518   if ($donext && "$argv[$cnt]" == "-median_scale") then
519      set Scale = 1
520      set donext = 0
521   endif
522
523   if ($donext && "$argv[$cnt]" == "-norm") then
524      set Scale = 3
525      set donext = 0
526   endif
527
528   if ($donext && "$argv[$cnt]" == "-spnorm") then
529      set Scale = 4
530      set donext = 0
531   endif
532
533   if ($donext && "$argv[$cnt]" == "-spnorm2") then
534      set Scale = 5
535      set donext = 0
536   endif
537
538   if ($donext && "$argv[$cnt]" == "-aspnorm2") then
539      set Scale = 6
540      set donext = 0
541   endif
542
543   if ($donext && "$argv[$cnt]" == "-mscl") then
544      set Scale = 7
545      set donext = 0
546   endif
547
548   if ($donext && "$argv[$cnt]" == "-Mscl") then
549      set Scale = 8
550      set donext = 0
551   endif
552
553   if ($donext && "$argv[$cnt]" == "-FATscl") then
554      if ($cnt == `ccalc -i $Narg - 1`) then
555         echo "Need two parameters after -FATscl such as -FATscl 25 75"
556         goto END
557      endif
558      set Scale = 9
559      @ cnt ++
560      set fatScale = $argv[$cnt]
561         #Sanity check, scale limits can be increased if needed
562      if ($fatScale > 110 || $fatScale < 5) then
563         echo "Looks like you -FATscl paramater 1 is bad."
564         echo "Acceptable values should be <110 or >5, have $fatScale"
565         goto END
566      endif
567      @ cnt ++
568      set fatPerc = $argv[$cnt]
569         #Sanity check, scale limits can be increased if needed
570      if ($fatPerc > 100 || $fatPerc < 5) then
571         echo "Looks like you -FATscl paramater 2 is bad."
572         echo "Acceptable values should be <110 or >5, have $fatPerc "
573         goto END
574      endif
575      set donext = 0
576   endif
577
578
579
580
581   if ($donext && "$argv[$cnt]" == "-TT_Xform") then
582      if ($cnt == $Narg) then
583         echo "Need Affine xform in 1D file after -TT_Xform"
584         goto END
585      else
586         @ cnt ++
587         set Mname = $argv[$cnt]
588         if ("$Mname" != 'AUTO') then
589            echo "Option -TT_Xform not ready for AUTO"
590            goto BEND
591         endif
592         if ( ! -f $Mname && "$Mname" != 'AUTO') then
593            echo "File $Mname not found"
594            goto END
595         endif
596         set donext = 0
597      endif
598   endif
599
600   if ($donext && "$argv[$cnt]" == "-scale_by_dset_median") then
601      if ($cnt == $Narg) then
602         echo "Need label after -scale_by_dset_median"
603         goto END
604      else
605         @ cnt ++
606         set Scale = 2
607         set ScaleVal = `3dBrickStat -median $argv[$cnt]`
608         echo $ScaleVal
609         set ScaleVal = $ScaleVal[2]
610         set donext = 0
611      endif
612
613   endif
614
615   if ($donext && "$argv[$cnt]" == "-statlabel") then
616      if ($cnt == $Narg) then
617         echo "Need label after -statlabel"
618         goto END
619      else
620         @ cnt ++
621         set olabel = $argv[$cnt]
622         set donext = 0
623      endif
624   endif
625
626   if ($donext && "$argv[$cnt]" == "-siglabel") then
627      if ($cnt == $Narg) then
628         echo "Need label after -siglabel"
629         goto END
630      else
631         @ cnt ++
632         set sigset = $argv[$cnt]
633         set donext = 0
634      endif
635   endif
636
637   if ($donext && "$argv[$cnt]" == "-max_vox") then
638      if ($cnt == $Narg) then
639         echo "Need label after -max_vox"
640         goto END
641      else
642         @ cnt ++
643         set max_vox = $argv[$cnt]
644         set donext = 0
645      endif
646   endif
647
648   if ($donext && "$argv[$cnt]" == "-mask") then
649      if ($cnt == $Narg) then
650         echo "Need mask volume after -mask"
651         goto END
652      else
653         @ cnt ++
654         set localopen = 0
655         set statopen = 0
656         set inmask = $argv[$cnt]
657         set donext = 0
658         #@ cnt ++
659         #echo "MASK SET" $cnt $argv[$cnt]
660      endif
661   endif
662
663   if ($donext && "$argv[$cnt]" == "-feat_znorm_mask") then
664      if ($cnt == $Narg) then
665         echo "Need mask volume after -feat_znorm_mask"
666         goto END
667      else
668         @ cnt ++
669         set localopen = 0
670         set statopen = 0
671         set featmask = $argv[$cnt]
672         set donext = 0
673         #@ cnt ++
674         #echo "FEAT MASK SET" $cnt $argv[$cnt]
675      endif
676   endif
677
678   if ($donext && "$argv[$cnt]" == "-input") then
679      if ($cnt == $Narg) then
680         echo "Need volume for segmentation after -input"
681         goto END
682      else
683         @ cnt ++
684         set localopen = 0
685         set statopen = 0
686         set involume = "$argv[$cnt]"
687         set donext = 0
688         #echo "INVOLUME SET" $cnt $argv[$cnt]
689      endif
690   endif
691
692   if ($donext && "$argv[$cnt]" == "-odir") then
693      if ($cnt == $Narg) then
694         echo "Need directory after -odir"
695         goto END
696      else
697         @ cnt ++
698         set odir = "$argv[$cnt]"
699         if (! -d $odir) mkdir -p $odir
700         if (! -d $odir) then
701            echo "Failed to create $odir"
702            goto END
703         endif
704         set donext = 0
705      endif
706   endif
707
708   if ($donext && "$argv[$cnt]" == "-stat") then
709      if ($cnt == $Narg) then
710         echo "Need local stat names after -stat"
711         goto END
712      else
713         @ cnt ++
714         set localopen = 0
715         set statvector = ($statvector $argv[$cnt])
716         set donext = 0
717         set statopen = 1
718      endif
719   endif
720
721   if ($donext && "$argv[$cnt]" == "-local") then
722      if ($cnt == $Narg) then
723         echo "Need neighborhood sizes after -local"
724         goto END
725      else
726         @ cnt ++
727         set statopen = 0
728         set localvector = ($localvector $argv[$cnt])
729         set donext = 0
730         set localopen = 1
731      endif
732   endif
733
734  if ($donext) then
735      if ($statopen == 1) then
736         set statvector = ($statvector $argv[$cnt])
737         @ cnt ++
738      endif
739      if ($localopen == 1) then
740         set localvector = ($localvector $argv[$cnt])
741         @ cnt ++
742      endif
743      if ($statopen == 0 && $localopen == 0) then
744         echo "Parameter $argv[$cnt] not understood"
745         apsearch -popt `basename $0` -word $argv[$cnt]
746         goto END
747      endif
748   else
749      @ cnt ++
750   endif
751
752end
753
754if ($Scale == 6 && $prepend_anat == 1) then
755   echo "Cannot use -aspnorm2 with -prepend_anat"
756   goto BEND
757endif
758
759set statlabelvector = ()
760foreach lstat ($statvector)
761   if ($lstat =~ med*) then
762      set statlabelvector = ($statlabelvector MEDIAN)
763   else if ($lstat =~ mea*)  then
764      set statlabelvector = ($statlabelvector mean)
765   else if ($lstat =~ MAD)  then
766      set statlabelvector = ($statlabelvector MAD)
767   else if ($lstat =~ *skew*) then
768      set statlabelvector = ($statlabelvector P2skew)
769   else if ($lstat =~ *kurt*) then
770      set statlabelvector = ($statlabelvector KURT)
771   else if ($lstat =~ *FWHM*) then
772      set statlabelvector = ($statlabelvector FWHMx FWHMy FWHMz)
773   else if ($lstat =~ *diffs*) then
774      set statlabelvector = ($statlabelvector AvgDif MinDif MaxDif)
775   else if ($lstat =~ *perc*) then
776      set i0 = `echo $lstat | cut -d ':' -f 2`
777      set i1 = `echo $lstat | cut -d ':' -f 3`
778      set ip = `echo $lstat | cut -d ':' -f 4`
779      while ($i0 <= $i1)
780         set statlabelvector = ($statlabelvector perc:$i0)
781         @ i0 += $ip
782      end
783   else
784      echo "Don't have label for stat $lstat"
785      set stat = 1
786      goto END
787   endif
788end
789
790goto RET_PARSE
791
792HELP:
793   echo ""
794   echo "Usage: `basename $0` <-input DSET> <-stat STAT1 [STAT2...]> "
795   echo "                     <-local R1 [R2 ...]> "
796   echo "Calculate a set of local statisctics to form a signature (feature) set."
797   echo "-input DSET: input dset"
798   echo "-stat STAT: Type of localstat to compute. See 3dLocalstat -help"
799   echo "            e.g. -stat mean median MAD P2skew kurt"
800   echo "-local R: Radius of sphere which envelopes voxels for which STAT "
801   echo "          STAT is derived."
802   echo "            e.g. -local 1 2 3 4 6 8 10 13 16 19 "
803   echo "-mask MASK: Mask volume restricting computation region"
804   echo "-odir DIR: Output directory"
805   echo "-prepend_anat: Add copy of DSET in feature vector. DSET is considered"
806   echo "               to be a MEDIAN stat over a radius of 0. This option"
807   echo "               will only work if you use at least one of 'mean' or "
808   echo "               'median' as STAT."
809   echo "               This option is useless with -aspnorm2 (see below)"
810   echo ""
811   echo "Scaling options:"
812   echo "   -no_scale: Do not scale features. (scale 0)"
813   echo "   -median_scale: Divide each sub-brick with its median which is"
814   echo "               computed with @ScaleVolume -scale_by_median. "
815   echo "               (scale 1)"
816   echo "   -scale_by_dset_median: Divide by median over whole dset. (scale 2)"
817   echo "   -norm: Scale dset with @ScaleVolume -norm.    (scale 3)"
818   echo "   -spnorm: Scale median stats only."
819   echo "         The set of median stats is demeaned, then divided by"
820   echo "         the set of MAD values. (scale 4)"
821   echo "   -spnorm2: Scale median and MAD stats"
822   echo "         For the set of median stats, demean then divide by"
823   echo "         the last MAD feature."
824   echo "         For the MAD stats, divide by the last MAD feature. (scale 5)"
825   echo "   -aspnorm2: Lile -spnorm2, but add anatomical image to the "
826   echo "           median feature set before scaling. (scale 6)"
827   echo "   -mscl: Scale the median stats by the last mean feature"
828   echo "       Scale the MAD stats by the last mean feature. (scale 7)"
829   echo "   -Mscl: Like spnorm2, BUT the mean is not removed from median stats"
830   echo "       before scaling, AND mean stats are also subject to MAD scaling."
831   echo "   -FATscl S P: Scale by P percentil value in sphere of radius S"
832   echo "                Crude recommendation is 25 75"
833   echo "                For now, P must be one of: 70, 75, 80, 85, 90, or 95"
834   echo "   -TT_Xform XMAT.1D: You can also create X Y Z coordinate features"
835   echo "                      that are Z normalized. To do so you need to "
836   echo "                      provide an affine transform that can be used"
837   echo "                      to go between original and standard space."
838   echo "                      The XMAT.1D expected here is the one generated"
839   echo "                      by @auto_tlrc, but it can come from other sources"
840   echo "                      as long as the following relationship holds:"
841   echo "                          Xo = Mt Xt, where Xo and Xt are 4x1 "
842   echo "                      coordinate vectors in original, and standard"
843   echo "                      space, respectively."
844   echo "               Note:  The transform need not be exact for these "
845   echo "                      features to be of use."
846   echo ""
847   echo "Extra Scaling:"
848   echo "   -feat_znorm: After you're done applying the scaling options, "
849   echo "            normalize the entire feature vector using "
850   echo "            @ScaleVolume -feat_znorm"
851   echo "   -feat_znorm_mask FSCMASK: Mask volume used for -feat_znorm"
852   echo "                        If nothing is specified, 3dAutomask is used"
853   echo ""
854   echo "Example:"
855   set ss = 12
856   echo "   @CalculateSignatures -input AfniDataCropped/IBSR_${ss}_ana+orig. \\"
857   echo "                     -stat median MAD P2skew kurt \\"
858   echo "                     -statlabel medMADskewkurt   \\"
859   echo "                     -local 1 2 3 4 6 8 10 13 16 19 \\"
860   echo "                     -mask AfniDataCropped/IBSR_${ss}_ana+orig. \\"
861   echo "                     -aspnorm2   \\"
862   echo "                     -odir SegResults/s${ss}"
863   echo ""
864   @global_parse -gopts_help
865
866goto END
867
868BEND:
869set stat = 1
870goto END
871
872END:
873exit $stat
874
875
876
877
878