dorianps / LINDA

Lesion Identification with Neighborhood Data Analysis
Apache License 2.0
20 stars 4 forks source link

Applying transformation matrix output by LINDA #25

Closed yqliu9240 closed 4 years ago

yqliu9240 commented 4 years ago

Hello all,

I originally asked a question in Google group and now I am following up on that. I have successfully transformed a lesioned brain to the template using LINDA. Now I want to apply the affine.mat and warp.nii.gz files to some statistical maps, which are also in the lesioned brain's T1 space, so that I can look at stats in the template space. Before transforming the stats maps, I tried to apply transformation to the original brain-extracted T1 just to check. Here are my codes:

antsApplyTransforms -i T1.nii.gz -o T1_in MNI.nii.gz -r Subject_in_MNI.nii.gz -t [Reg3_template_to_sub_affine.mat,1] -t Reg3_sub_to_template_warp.nii.gz

However, the output image doesn't match the size and position of the template space. Can anyone provide any insights as to why this didn't work? Thank you!

Yuqi

dorianps commented 4 years ago

Now that I checked better I realize where was the problem. The transforms saved in the linda folder are just subject -> penn template. To go in MNI space you also need to concatenate penn template -> MNI transforms. The R code that does this in LINDA is roughly here: https://github.com/dorianps/LINDA/blob/master/R/linda_predict_function.R#L445-L479

Here is an example how that might work directly in R. If you want to use ANTs in command line, you need to apply the same transforms using a command similar to what you have.

outdir = '/path/to/linda/folder'

# these below are the penn->MNI transforms, obtained from the location where LINDA is installed
 warpmni = system.file("extdata", "pennTemplate",
                          "templateToCh2_1Warp.nii.gz",
                          package = "LINDA",
                          mustWork = FALSE)

    affmni = system.file("extdata",
                         "pennTemplate",
                         "templateToCh2_0GenericAffine.mat",
                         package = "LINDA",
                         mustWork = FALSE)

# these below are the subject->penn transforms created for this specific subject
reg_to_temp_warp = file.path(outdir , 'Reg3_sub_to_template_warp.nii.gz')
warppenn = reg_to_temp_warp
reg_to_temp_aff = file.path(outdir , 'Reg3_sub_to_template_affine.mat')
affpenn = reg_to_temp_aff

# here we create the list of four transformation files in the correct order to project subject->MNI
matrices = c(warpmni, affmni, affpenn, warppenn)

# this is the example of applying these transforms, note that we invert the third one (affpenn)
   submni = antsApplyTransforms(
        moving = simg,
        fixed = mni,
        transformlist = matrices,
        interpolator = 'Linear',
        whichtoinvert = c(0, 0, 1, 0),
        verbose = TRUE
      )
yqliu9240 commented 4 years ago

It worked! Thank you so much! I have one followup question on the order of transformation. From the matrices variable c(warpmni, affmni, affpenn, warppenn), how should I interpret the order of transformation? And why is the order of affine and warp switched between mni and penn transformation?

Thank you! Yuqi

On Mon, Sep 9, 2019 at 5:22 PM dorianps notifications@github.com wrote:

Now that I checked better I realize where was the problem. The transforms saved in the linda folder are just subject -> penn template. To go in MNI space you also need to concatenate penn template -> MNI transforms. The R code that does this in LINDA is roughly here:

https://github.com/dorianps/LINDA/blob/master/R/linda_predict_function.R#L445-L479

Here is an example how that might work directly in R, if you want to just use ANTs in command line, you need to apply the same transforms using a command similar to what you have.

outdir = '/path/to/linda/folder'

these below are the penn->MNI transforms, obtained from the location where LINDA is installed

warpmni = system.file("extdata", "pennTemplate", "templateToCh2_1Warp.nii.gz", package = "LINDA", mustWork = FALSE)

affmni = system.file("extdata",
                     "pennTemplate",
                     "templateToCh2_0GenericAffine.mat",
                     package = "LINDA",
                     mustWork = FALSE)

these below are the subject->penn transforms created for this specific subjectreg_to_temp_warp = file.path(outdir , 'Reg3_sub_to_template_warp.nii.gz')warppenn = reg_to_temp_warpreg_to_temp_aff = file.path(outdir , 'Reg3_sub_to_template_affine.mat')affpenn = reg_to_temp_aff

here we create the list of four transformation files in the correct order to project subject->MNImatrices = c(warpmni, affmni, affpenn, warppenn)

this is the example of applying these transforms, note that we invert the third one (affpenn)

submni = antsApplyTransforms( moving = simg, fixed = mni, transformlist = matrices, interpolator = 'Linear', whichtoinvert = c(0, 0, 1, 0), verbose = verbose > 1 )

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dorianps/LINDA/issues/25?email_source=notifications&email_token=ANEOOMKMNAXDZ63IW7ERUXTQI247VA5CNFSM4IU7O6GKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6JCQOQ#issuecomment-529672250, or mute the thread https://github.com/notifications/unsubscribe-auth/ANEOOMIZ45EDD7RMVSTS5TTQI247VANCNFSM4IU7O6GA .

dorianps commented 4 years ago

The order of transformations is a bit complicated to understand but the rule of thumb works this way. When you register A to B, to bring someting from A space to B space you need to place (1)warp,(2)affine. To bring something from B to A you need to place (1)inverse affine, (2) inverse warp.

The order above is switched because for MNI we are moving in the same direction the two templates were registered initally (penn tempate -> MNI ch2 template), while for the subject we are moving in the inverse direction of the registration (penn template <- subject). It is also counterintuitive but to move A -> B -> C you need to place first the B -> C transforms and then the A -> B transforms.

dorianps commented 4 years ago

Reopen if necessary.