Skip to content


Personal tools
You are here: Home » SSCC » dglen's Home » Alignment of EPI data across two sessions

Alignment of EPI data across two sessions

Document Actions
The special case of aligning EPI data across two sessions without bias toward either session

Occasionally, one wants to compare the effects between two conditions of scanning sessions - before versus after some treatment or day 1 versus day 2 for a longitudinal change. Removing the bias of either session can be accomplished using this script. This sample script  computes a mean anatomical dataset that is in-between the two anatomical datasets acquired in each of the sessions. The anatomical mean is used as the reference dataset. This script computes the EPI data aligned to that mean anatomical and to an anatomical template. Modify the first few lines for the anat and epi datasets in your sessions.


# align_across_days.csh
# script to align anatomical and epi data across two conditions
# with no bias toward a scan on either of those conditions

# remove pound signs from dataset names  (they make scripting difficult)


#Make sure you move perfusion data to a separate directory
#Do the same for bad runs

set anat1 = 'mprage_scan_5__001+orig'
set anat2 = 'mprage_scan_19__001+orig'

# ******* these need to be defined before running the script
# the example data I have is not clear as to which condition for which epi dataset
# pick one epi dataset as the main one to which to align
# and the others from the same condition as the children
# Usually one picks the dataset that is closest in time to the anatomical
# dataset but that's not very important really
set epi1 = 'epiRTwm_scan_7__001+orig'
set epi1_children = `ls epiRTwm_scan_8__001+orig.HEAD \
   epiRTwm_scan_9__001+orig.HEAD \
set epi2 = 'epiRTwm_scan_13__001+orig'
set epi2_children = `ls epiRTwm_scan_14__001+orig.HEAD \
   epiRTwm_scan_15_001+orig.HEAD \
   epiRTwm_scan_18_001+orig.HEAD `

# clean up from any previous runs
rm -f anat1+orig.* anat2+orig.*
rm -f anat1_ns*+orig.* anat2_ns*+orig.*
rm -f anat_ns_mean*+orig.* anat_ns_mean*+tlrc.*
rm -f anat*.1D

# skullstripping (add -orig_vol?)
3dSkullStrip -prefix anat1_ns -input $anat1
3dSkullStrip -prefix anat2_ns -input $anat2

# align the two anatomical datasets (pre / post)
# computes transformation (3dvolreg would work here too, and it's faster,
# but the allows for larger movement and distortion
# between anatomical scans and does not require a common grid or voxel size       -anat anat1_ns+orig \
                        -epi anat2_ns+orig \
                        -epi_base 0  -prep_off \
                        -anat_has_skull no   \
                        -epi_strip None \
                        -giant_move -cost lpa \
                        -overwrite -suffix _al

# make square matrix out of the row of elements - just reformatting here
cat_matvec anat1_ns_al_mat.aff12.1D > anat1_mat.1D
echo "0 0 0 1" >> anat1_mat.1D

# mean transformation - square root matrix
1dmatcalc "&read(anat1_mat.1D) &Sqrt &write(anat_sqrt.1D)"

# reformat mean transformation for 3dAllineate
cat_matvec -ONELINE anat_sqrt.1D > anat_sqrt.aff12.1D

# compute inverse of mean transformation too
cat_matvec -ONELINE anat_sqrt.1D -I > anat_sqrtI.aff12.1D

# apply mean transformation to anat-pre on anat1_ns grid
# note- original example data was not on same grid
# can handle that either by forcing data to be on the same grid
# with @AlignCenters or applying it here for the output data
3dAllineate -1Dmatrix_apply anat_sqrt.aff12.1D -base anat1_ns+orig \
   -source anat1_ns+orig -prefix anat1_ns_mean
# apply inverse mean transformation to anat-post, also on anat1_ns grid
3dAllineate -1Dmatrix_apply anat_sqrtI.aff12.1D -base anat1_ns+orig \
   -source anat2_ns+orig -prefix anat2_ns_mean

# take mean of two anats (pre and post)
3dMean -prefix anat_ns_mean anat1_ns_mean+orig anat2_ns_mean+orig

# align the mean anat to tlrc template
@auto_tlrc -base TT_N27+tlrc -input anat_ns_mean+orig -no_ss -suffix NONE -anat anat_ns_mean+orig                     \
   -epi $epi1 -epi_base 0 -volreg_base 2 -epi2anat -master_epi MIN_DXYZ      \
   -anat_has_skull no -overwrite -tlrc_apar anat_ns_mean+tlrc \
   -giant_move -child_epi $epi1_children -anat anat_ns_mean+orig                     \
   -epi $epi2 -epi_base 0 -volreg_base 2 -epi2anat -master_epi MIN_DXYZ      \
   -anat_has_skull no -overwrite -tlrc_apar anat_ns_mean+tlrc \
   -giant_move -child_epi $epi2_children
Created by Daniel Glen
Last modified 2011-07-14 11:42

Powered by Plone

This site conforms to the following standards: