Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: write template output such that things sort together #1771

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 77 additions & 49 deletions Scripts/antsMultivariateTemplateConstruction2.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

shopt -s extglob

VERSION="0.0.0"
Expand Down Expand Up @@ -389,19 +392,22 @@ function shapeupdatetotemplate() {
#echo " ImageSetStatistics $dim ${whichtemplate}WarpedToTemplateList.txt ${template} 0"
echo "--------------------------------------------------------------------------------------"

imagelist=(`ls ${outputname}template-modality${whichtemplate}-*WarpedToTemplate.nii.gz`)
imagelist=(`ls ${outputname}*-modality${whichtemplate}-*-WarpedToTemplate.nii.gz`)
if [[ ${#imagelist[@]} -ne ${IMAGESPERMODALITY} ]]
then
echo "ERROR shapeupdatedtotemplate - imagelist length is ${#imagelist[@]}, expected ${IMAGESPERMODALITY}"
echo "ERROR shapeupdatetotemplate - imagelist length is ${#imagelist[@]}, expected ${IMAGESPERMODALITY}"
exit 1
fi

summarizeimageset $dim $template $statsmethod $sharpenmethod ${imagelist[@]}

WARPLIST=( `ls ${outputname}*[0-9]Warp.nii.gz 2> /dev/null` )
NWARPS=${#WARPLIST[*]}
WARPLIST=( `ls ${outputname}input*-[0-9]Warp.nii.gz 2> /dev/null` ) || true
NWARPS=${#WARPLIST[@]}
echo "number of warps = $NWARPS"
echo "$WARPLIST"
if [[ $NWARPS -ne 0 ]];
then
echo "${WARPLIST[@]}"
fi

if [[ $whichtemplate -eq 0 ]];
then
Expand All @@ -411,10 +417,10 @@ function shapeupdatetotemplate() {
echo
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---voxel-wise averaging of the inverse warp fields (from subject to template)"
echo " AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 `ls ${outputname}*Warp.nii.gz | grep -v "InverseWarp"`"
echo " AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 ${WARPLIST[@]}"
date
echo "--------------------------------------------------------------------------------------"
AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 `ls ${outputname}*Warp.nii.gz | grep -v "InverseWarp"`
AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 ${WARPLIST[@]}

echo
echo "--------------------------------------------------------------------------------------"
Expand All @@ -429,11 +435,11 @@ function shapeupdatetotemplate() {
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---average the affine transforms (template <-> subject)"
echo " ---transform the inverse field by the resulting average affine transform"
echo " ${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}*GenericAffine.mat"
echo " ${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}*-input*GenericAffine.mat"
echo " ${WARP} -d ${dim} -e vector -i ${templatename}0warp.nii.gz -o ${templatename}0warp.nii.gz -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template} --verbose 1"
echo "--------------------------------------------------------------------------------------"

${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}*GenericAffine.mat
${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}input*GenericAffine.mat

if [[ $NWARPS -ne 0 ]];
then
Expand All @@ -442,15 +448,19 @@ function shapeupdatetotemplate() {
fi
fi

echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---warp each template by the resulting transforms"
echo " ${WARP} -d ${dim} --float $USEFLOAT --verbose 1 -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -r ${template}"
echo "--------------------------------------------------------------------------------------"

if [[ -f "${templatename}0warp.nii.gz" ]];
then
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---warp each template by the resulting transforms"
echo " ${WARP} -d ${dim} --float $USEFLOAT --verbose 1 -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -r ${template}"
echo "--------------------------------------------------------------------------------------"
${WARP} -d ${dim} --float $USEFLOAT --verbose 1 -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -r ${template}
else
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---warp each template by the resulting transform"
echo " ${WARP} -d ${dim} --float $USEFLOAT --verbose 1 -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template}"
echo "--------------------------------------------------------------------------------------"
${WARP} -d ${dim} --float $USEFLOAT --verbose 1 -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template}
fi

Expand Down Expand Up @@ -897,7 +907,7 @@ elif [[ ${NINFILES} -eq 1 ]];
IMAGECOUNT=0
while read line
do
files=(`echo $line | tr ',' ' '`)
files=(`echo $line | tr "," "\n"`)
if [[ ${#files[@]} -ne $NUMBEROFMODALITIES ]];
then
echo "The number of files in the csv file does not match the specified number of modalities."
Expand Down Expand Up @@ -965,10 +975,10 @@ elif [[ ${NINFILES} -eq 1 ]];
number=$RANDOM
if [[ $i -lt 15 ]];
then
let "number %= $BINrange" # Scales $number down within $range.
number=$((number %= $BINrange)) # Scales $number down within $range.
elif [[ $i -eq 15 ]];
then
let "number %= $range" # Scales $number down within $range.
number=$((number %= $range)) # Scales $number down within $range.
fi
done
#debug only
Expand All @@ -989,15 +999,15 @@ elif [[ ${NINFILES} -eq 1 ]];
ImageMath $TDIM selection/vol0${number}.nii.gz ExtractSlice ${IMAGESETVARIABLE} ${number}
# cp vol0${number}.nii.gz selection/
fi
let j++
((j++))
done
fi
elif [[ ${range} -gt ${nfmribins} && ${range} -lt 32 ]];
then
for ((i = 0; i < ${nfmribins} ; i++))
do
number=$RANDOM
let "number %= $range"
number=$((number %= $range))
if [[ ${number} -lt 10 ]];
then
ImageMath $TDIM selection/vol0.nii.gz ExtractSlice ${IMAGESETVARIABLE} ${number}
Expand Down Expand Up @@ -1043,7 +1053,7 @@ if [[ $NUMBEROFMODALITIES -gt 1 ]];
for (( j = 0; j < $NUMBEROFMODALITIES; j++ ))
do
k=0
let k=$i+$j
k=$((i+j))
IMAGEMETRICSET="$IMAGEMETRICSET ${IMAGESETARRAY[$k]}"
done
echo $IMAGEMETRICSET
Expand All @@ -1059,7 +1069,7 @@ for (( i = 0; i < $NUMBEROFMODALITIES; i++ ))
do
setCurrentImageSet $i

if [[ -n "${REGTEMPLATES[$i]}" ]];
if [[ ${#REGTEMPLATES[@]} -gt 0 && -n "${REGTEMPLATES[$i]}" ]];
then
if [[ ! -r "${REGTEMPLATES[$i]}" ]];
then
Expand Down Expand Up @@ -1140,7 +1150,7 @@ if [[ "$RIGID" -eq 1 ]];
for (( j = 0; j < $NUMBEROFMODALITIES; j++ ))
do
k=0
let k=$i+$j
k=$((i+j))
IMAGEMETRICSET="$IMAGEMETRICSET -m MI[ ${TEMPLATES[$j]},${IMAGESETARRAY[$k]},${MODALITYWEIGHTS[$j]},32,Regular,0.25 ]"
done

Expand Down Expand Up @@ -1171,7 +1181,7 @@ if [[ "$RIGID" -eq 1 ]];
for (( j = 0; j < $NUMBEROFMODALITIES; j++ ))
do
k=0
let k=$i+$j
k=$((i+j))
IMGbase=`basename ${IMAGESETARRAY[$k]}`
BASENAME=` echo ${IMGbase} | cut -d '.' -f 1 `
RIGID="${outdir}/rigid${i}_${j}_${IMGbase}"
Expand Down Expand Up @@ -1299,7 +1309,7 @@ if [[ "$RIGID" -eq 1 ]];
for (( i = $j; i < ${#IMAGESETARRAY[@]}; i+=$NUMBEROFMODALITIES ))
do
k=0
let k=$i-$j
k=$((i-j))
IMGbase=`basename ${IMAGESETARRAY[$i]}`
BASENAME=` echo ${IMGbase} | cut -d '.' -f 1 `
RIGID="${outdir}/rigid${k}_${j}_${IMGbase}"
Expand Down Expand Up @@ -1359,13 +1369,13 @@ fi # endif RIGID
#
##########################################################################

ITERATLEVEL=( $(echo $MAXITERATIONS | tr 'x' ' ') )
ITERATLEVEL=( $(echo $MAXITERATIONS | tr 'x' '\n') )
NUMLEVELS=${#ITERATLEVEL[@]}

SHRINKLEVEL=( $(echo $SHRINKFACTORS | tr 'x' ' ') )
SHRINKLEVEL=( $(echo $SHRINKFACTORS | tr 'x' '\n') )
NUMSHRINK=${#SHRINKLEVEL[@]}

SMOOTHLEVEL=( $(echo $SMOOTHINGFACTORS | tr 'x' ' ') )
SMOOTHLEVEL=( $(echo $SMOOTHINGFACTORS | tr 'x' '\n') )
NUMSMOOTH=${#SMOOTHLEVEL[@]}

if [[ $NUMLEVELS -ne $NUMSHRINK ]]
Expand Down Expand Up @@ -1399,7 +1409,9 @@ i=0
while [[ $i -lt ${ITERATIONLIMIT} ]];
do
itdisplay=$((i+1))
rm -f ${OUTPUTNAME}*WarpedToTemplate.nii.gz
rm -f ${OUTPUTNAME}*Warp.nii*
rm -f ${OUTPUTNAME}*warp.nii*
rm -f ${OUTPUTNAME}*GenericAffine.mat
rm -f ${outdir}/job*.sh
# Used to save time by only running coarse registration for the first couple of iterations
Expand Down Expand Up @@ -1439,7 +1451,7 @@ while [[ $i -lt ${ITERATIONLIMIT} ]];
for (( k = 0; k < $NUMBEROFMODALITIES; k++ ))
do
l=0
let l=$j+$k
l=$((j+k))

if [[ "${METRICTYPE[$k]}" == "DEMONS" ]];
then
Expand Down Expand Up @@ -1477,15 +1489,13 @@ while [[ $i -lt ${ITERATIONLIMIT} ]];
indir=`pwd`
fi
IMGbase=`basename ${IMAGESETARRAY[$l]}`
OUTFN=${OUTPUTNAME}template-modality${k}-${IMGbase/%?(.nii.gz|.nii)}
OUTFN=${OUTPUTNAME}input$(printf "%04d" $l)-modality${k}-${IMGbase/%?(.nii.gz|.nii)}
OUTFN=`basename ${OUTFN}`
OUTFN="${OUTFN}${l}"
DEFORMED="${outdir}/${OUTFN}${l}WarpedToTemplate.nii.gz"
DEFORMED="${outdir}/${OUTFN}-WarpedToTemplate.nii.gz"

IMGbase=`basename ${IMAGESETARRAY[$j]}`
OUTWARPFN=${OUTPUTNAME}${IMGbase/%?(.nii.gz|.nii)}
OUTWARPFN=${OUTPUTNAME}input$(printf "%04d" $j)-${IMGbase/%?(.nii.gz|.nii)}-
OUTWARPFN=`basename ${OUTWARPFN}`
OUTWARPFN="${OUTWARPFN}${j}"

if [[ $NOWARP -eq 0 ]];
then
Expand Down Expand Up @@ -1516,9 +1526,11 @@ while [[ $i -lt ${ITERATIONLIMIT} ]];

done

IMGbase=`basename ${IMAGESETARRAY[$j]}`
OUTWARPFN=${OUTPUTNAME}${IMGbase/%?(.nii.gz|.nii)}
OUTWARPFN=`basename ${OUTWARPFN}${j}`

# Already defined above
# IMGbase=`basename ${IMAGESETARRAY[$j]}`
# OUTWARPFN=${OUTPUTNAME}${IMGbase/%?(.nii.gz|.nii)}
# OUTWARPFN=`basename ${OUTWARPFN}${j}`

stage0="-r [ ${TEMPLATES[0]},${IMAGESETARRAY[$j]},1 ]"
stage1="-t Rigid[ 0.1 ] ${IMAGEMETRICLINEARSET} -c [ 1000x500x250x0,1e-6,10 ] -f 6x4x2x1 -s 4x2x1x0"
Expand Down Expand Up @@ -1674,22 +1686,32 @@ while [[ $i -lt ${ITERATIONLIMIT} ]];
fi
fi

WARPFILES=`ls ${OUTPUTNAME}*Warp.nii.gz | grep -v "InverseWarp"`
AFFINEFILES=`ls ${OUTPUTNAME}*GenericAffine.mat`

if [[ ${#WARPFILES[@]} -eq 0 || ${#AFFINEFILES[@]} -eq 0 ]];
then
echo "The registrations did not terminate properly. There are no warp files"
echo "or affine files."
exit 1
fi
NUM_AFFINEFILES=0
NUM_AFFINEFILES=$(ls ${OUTPUTNAME}*GenericAffine.mat | wc -l) || true

if [[ ${#WARPFILES[@]} -ne ${#AFFINEFILES[@]} ]];
if [[ $NOWARP -eq 1 ]];
then
echo "The registrations did not terminate properly. The number of warp files"
echo "does not match the number of affine files."
exit 1
fi
if [[ ${NUM_AFFINEFILES} -eq 0 ]];
then
echo "The registrations did not terminate properly. There are no warp files or affine files."
exit 1
fi
if [[ ${NUM_AFFINEFILES} -ne $IMAGESPERMODALITY ]];
then
echo "The registrations did not terminate properly. The number of affine transform files"
echo "does not match the number of input images."
exit 1
fi
else
NUM_WARPFILES=0
NUM_WARPFILES=$(ls ${OUTPUTNAME}*Warp.nii.gz | grep -v InverseWarp | wc -l) || true
if [[ ${NUM_WARPFILES} -ne $IMAGESPERMODALITY ]];
then
echo "The registrations did not terminate properly. The number of warp files"
echo "does not match the number of input images."
exit 1
fi
fi

for (( j = 0; j < $NUMBEROFMODALITIES; j++ ))
do
Expand Down Expand Up @@ -1733,6 +1755,7 @@ while [[ $i -lt ${ITERATIONLIMIT} ]];
else
rm -f ${outdir}/job*.txt ${outdir}/slurm-*.out
fi
echo "Iteration $itdisplay completed"
((i++))
done

Expand All @@ -1753,6 +1776,11 @@ echo "--------------------------------------------------------------------------
echo " Done creating: ${TEMPLATES[@]}"
echo " Script executed in $time_elapsed seconds"
echo " $(( time_elapsed / 3600 ))h $(( time_elapsed %3600 / 60 ))m $(( time_elapsed % 60 ))s"
echo " Intermediate templates and warps stored in: ${intermediateTemplateDir}/"
echo "--------------------------------------------------------------------------------------"
echo " Note: The template has been updated after the final iteration."
echo " Pairwise warps from the final iteration are left in ${outdir}."
echo " They register input to the penultimate template(s), stored in ${intermediateTemplateDir}/"
echo "--------------------------------------------------------------------------------------"

exit 0