-
Notifications
You must be signed in to change notification settings - Fork 6
Create a nifti of within subject change (2 timepoints, output from dbm)
Goal: Create a nifti that represents the difference between two scans (usually different timepoints). One reason you may want to do this if you want to use a multivariate technique such as non-negative matrix factorization to examine variation and don't have a longitudinal approach, but want to capture change between two time points. To create a single image per subject representing this change, the following script warps two images together and creates a single output per subject.
For this, I used files on Niagara. Create a script in the directory where you ran the 2level, for example named within_subject_difference.sh. The files used here are outputs from Gabe's two-level dbm.
Currently, the parts of the script below only relevant for absolute jacobians are commented out.
Lines to edit to make the script run for you:
- path to the 2level run on niagara
- the time point files
(note: here the script is using the first two time points, however you can use any two timepoints if you have more than 2)
- time point 1 = ./output/$subject/subject*lsq601Warp.nii.gz
- time point 2 = ./output/$subject/subject*lsq611Warp.nii.gz
- time point 3 = ./output/$subject/subject*lsq621Warp.nii.gz
- time point n = ./output/$subject/subject*lsq6n1Warp.nii.gz
this is useful is you didn't explicitly hard code time point in your mri file name - however this workaround will not work for the section of the code that uses the absolute jacobians - you'll likely need to rename the mri files.
- if you do change the time points that you will use, you should change everything that has "1_to_2" and "2_to_1" with the appropriate numbers
module load cobralab
for file in /home/m/mchakrav/lanicupo/scratch/dbm_20200807/output/subject*; do subject=$(basename $file);
if [ -s ./output/$subject/subject*lsq611Warp.nii.gz ]; then
#First create jacobian between the two timepoints
#Merge two warps to create one between the timepoints
antsApplyTransforms --verbose -d 3 -r ./output/$subject/*_template0.nii.gz \
-t ./output/$subject/subject*lsq601InverseWarp.nii.gz \
-t ./output/$subject/subject*lsq611Warp.nii.gz \
-o [ warp_2_to_1.nii.gz,1 ] --verbose
mv warp_2_to_1.nii.gz ./output/$subject/
echo "Moving warp_2_to_1.nii.gz to subject folder"
# #Create a jacobian from the merged warps
echo $subject
CreateJacobianDeterminantImage 3 ./output/$subject/warp_2_to_1.nii.gz \
./output/$subject/jacobian_1_to_2.nii.gz 1 1
#Second create jacobian of either nlin+affine (absolutes) or just nlin (relative)
#FOR ABSOLUTES
#subtract the affines of time one and two
#echo ./output/jacobians/groupwise/$subject*_mri_2_lsq6_affine.nii.gz
#ImageMath 3 ./output/$subject/affine_jacobian_1_to_2.nii.gz - \
#./output/jacobians/groupwise/$subject*_mri_2_lsq6_affine.nii.gz \
#./output/jacobians/groupwise/$subject*_mri_1_lsq6_affine.nii.gz
#FOR RELATIVES
#Get ants transform from deformation field
ANTSUseDeformationFieldToGetAffineTransform ./output/$subject/warp_2_to_1.nii.gz 0.25 \
affine ./output/$subject/delin_subject.mat \
./output/$subject/*_otsumask.nii.gz
#Create composite warp
#output from step above is transform
antsApplyTransforms -d 3 --verbose \
-t [./output/$subject/delin_subject.mat,1] \
-r ./output/$subject/${subject}_template0.nii.gz \
-o [./output/$subject/delin_subject.nii.gz,1]
#Create a jacobian determinant of composite warp (output from step above)
CreateJacobianDeterminantImage 3 ./output/$subject/delin_subject.nii.gz \
./output/$subject/delin_jacobian.nii.gz 1 1
#Third combine the two jacobians created above
#For the ABSOLUTES, combine the jacobians and the affines
#combine the affines between time 1 and 2 and the jacobians between 1 and 2
#ImageMath 3 ./output/$subject/absolute_jacobian_1_to_2.nii.gz + ./output/$subject/jacobian_1_to_2.nii.gz ./output/$subject/affine_jacobian_1_to_2.nii.gz
#for the RELATIVES, combine the jacobians and the nlin
ImageMath 3 ./output/$subject/relative_jacobian_1_to_2.nii.gz + ./output/$subject/jacobian_1_to_2.nii.gz ./output/$subject/delin_jacobian.nii.gz
#Fourth put them in common space
antsApplyTransforms -d 3 -i ./output/$subject/relative_jacobian_1_to_2.nii.gz \
-t ./output/secondlevel/secondlevel_*${subject}*_template*1Warp.nii.gz \
-t ./output/secondlevel/secondlevel_*${subject}*_template*GenericAffine.mat \
-o ./output/$subject/relative_1_to_2_jacobian_in_common_space.nii.gz -r ./output/secondlevel/secondlevel_template0.nii.gz
#Fifth blur them
SmoothImage 3 ./output/$subject/relative_1_to_2_jacobian_in_common_space.nii.gz 0.08492569002 ./output/$subject/relative_common_smooth.nii.gz 1 0
else
echo "$subject has only one scan"
fi
done