From 80acccf9292a3affd41355ee063d3e902f48857e Mon Sep 17 00:00:00 2001 From: Mark Jenkinson <mark@fmrib.ox.ac.uk> Date: Mon, 13 Aug 2012 10:44:06 +0000 Subject: [PATCH] Changes to avoid unnecessary processing --- fslreorient2std | 18 ++++++++++++++---- fslswapdim | 33 ++++++++++++++++++++++++++------- fslswapdim.cc | 14 +++++++++++++- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/fslreorient2std b/fslreorient2std index f8453a3..2bcffd2 100755 --- a/fslreorient2std +++ b/fslreorient2std @@ -24,8 +24,8 @@ if [ `$FSLDIR/bin/imtest $img` = 0 ] ; then exit 2 fi -scode=`$FSLDIR/bin/fslorient -getsformcode $img`; -qcode=`$FSLDIR/bin/fslorient -getqformcode $img`; +scode=`$FSLDIR/bin/fslval $img sform_code`; +qcode=`$FSLDIR/bin/fslval $img qform_code`; if [ $scode = 0 -a $qcode = 0 ] ; then echo "ERROR: Orientation information not stored in ${1}!" echo "Cannot reslice without orientation information (i.e. need valid labels in FSLView)" @@ -34,9 +34,19 @@ if [ $scode = 0 -a $qcode = 0 ] ; then exit 3 fi -hand=`$FSLDIR/bin/fslorient -getorient $img`; -if [ $hand = RADIOLOGICAL ] ; then +pattern=sto_xyz +if [ $scode = 0 ] ; then + pattern=qto_xyz +fi +$FSLDIR/bin/fslhd $img | grep $pattern | awk '{ print $2 " " $3 " " $4 " " $5 }' > ${outimg}_nii.mat +det=`$FSLDIR/bin/avscale ${outimg}_nii.mat | grep Determinant | awk '{ print $3 }'`; +if [ X`echo "if ( $det < 0 ) { 1 }" | bc -l` = X1 ] ; then + # radiological case (negative determinant) $FSLDIR/bin/fslswapdim $img RL PA IS $outimg else $FSLDIR/bin/fslswapdim $img LR PA IS $outimg fi + +# clean up +rm -f ${outimg}_nii.mat + diff --git a/fslswapdim b/fslswapdim index 4333a70..4b173f0 100755 --- a/fslswapdim +++ b/fslswapdim @@ -1,4 +1,5 @@ #!/bin/sh +set -e # Extended syntax for fslswapdim # @@ -27,7 +28,6 @@ get_axis(){ echo $ax; } - if [ $# -lt 4 ] ; then echo "Usage: fslswapdim <input> <a> <b> <c> [output] @@ -41,6 +41,7 @@ if [ $# -lt 4 ] ; then exit 1; fi + filetype=`$FSLDIR/bin/fslval $1 file_type`; if [ $filetype = "ANALYZE-7.5" ] ; then nifti=no; @@ -62,7 +63,9 @@ fi uselabels=no -comm="$FSLDIR/bin/fslswapdim_exe $1" +comm1="$FSLDIR/bin/fslswapdim_exe $1" +comm2="" +swaplabs="" for fn in $2 $3 $4 ; do cc=`echo Q$fn | sed 's/[RLAPIS]//g'`; if [ $cc = "Q" ] ; then @@ -79,14 +82,30 @@ for fn in $2 $3 $4 ; do else cc=$fn; fi - comm="$comm $cc"; + comm2="$comm2 $cc"; + swaplabs="$swaplabs $cc" done +# check if just a simple copy is good enough (only if an output filename is specified) +if [ "$swaplabs" = " x y z" ] ; then + if [ X$5 != X ] ; then + if [ `$FSLDIR/bin/remove_ext $1` != `$FSLDIR/bin/remove_ext $5` ] ; then + comm="$FSLDIR/bin/imcp $1 $5" + $comm + exit 0 + fi + else + cat $FSLDIR/etc/flirtsch/ident.mat + exit 0 + fi +fi + + # Check if the labels are used *and* it is doing a left-right inversion if [ $uselabels = yes ] ; then - tmpf=`$FSLDIR/bin/tmpnam`_sd - $comm > $tmpf 2>&1 - wl=`grep WARNING $tmpf | wc -l`; + # the line below just checks the swapping syntax - it does not require the true image, as any will do since the arguments are now reduced to x/y/z format + check="`$FSLDIR/bin/fslswapdim_exe $FSLDIR/data/standard/MNI152_T1_2mm_brain $comm2 --checkLR`" + wl=`echo $check | grep -i changed | wc -l`; if [ $wl -gt 0 ] ; then echo "Cannot perform requested swap (NEUROLOGICAL/RADIOLOGICAL storage inverted)" echo "Try the following command instead:" @@ -96,7 +115,7 @@ if [ $uselabels = yes ] ; then fi fi -comm="$comm $5" +comm="$comm1 $comm2 $5" # verbose? #echo $comm diff --git a/fslswapdim.cc b/fslswapdim.cc index 7601d54..8b504a0 100644 --- a/fslswapdim.cc +++ b/fslswapdim.cc @@ -13,10 +13,11 @@ using namespace NEWIMAGE; void print_usage(const string& progname) { - cout << "Usage: " << progname << " <input> <a> <b> <c> [output]" << endl; + cout << "Usage: " << progname << " <input> <a> <b> <c> [output] [--checkLR]" << endl; cout << endl; cout << " where a,b,c represent the new x,y,z axes in terms of the" << endl; cout << " old axes. They can take values of -x,x,y,-y,z,-z" << endl; + cout << " --checkLR is an option that checks if the specified arguments lead to a Left-Right swap or not - it cannot be used with an output name" << endl; cout << " e.g. " << progname << " invol y x -z outvol" << endl; } @@ -28,8 +29,13 @@ int fmrib_main(int argc,char *argv[]) string inname=argv[1]; string outname=""; bool showmat=false; + bool checkonly=false; if (argc==6) { outname=argv[5]; + if (outname=="--checkLR") { + outname=""; + checkonly=true; + } showmat=false; } else { showmat=true; @@ -45,6 +51,12 @@ int fmrib_main(int argc,char *argv[]) cout << affmat << endl; } + if (checkonly) { + if (affmat.Determinant()<0.0) cout << "LR orientation changed" << endl; + else cout << "LR orientation preserved" << endl; + return 0; + } + if (affmat.Determinant()<0.0) { cout << "WARNING:: Flipping Left/Right orientation (as det < 0)" << endl; } -- GitLab