Re: Nonlinear transform EPI data

Hi-

This question was originally asked on an existing thread, and is really a specific question of its own. As such, it has been split into its own new topic.

Because it contained two separate “starting” points, I have also merged those here.

Hope that isn’t confusing, but this should be clearer to follow as a single thread now, I think.

Thanks,
pt

--------------------------- original question/post --------------------------
Hi Rick,

I have a relevant question here. Actually, what I want to do is to nonlinearly-warp back the data from the standard space to individual native space. For example, I would like to obtain a set of masks (i.e., atlas) in each individual’s native space. Those masks have been registered to a template in standard space. I used auto_warp.py to warp a subject’s structural image to a template:

auto_warp.py -base template_t1.nii -input subject_t1.nii -skull_strip_input yes

Then, I consider using 3dNwarpApply with -iwarp option as follow:

3dNwarpApply -master subject_t1.nii -dxyz 2 -source atlas_masks.nii -iwarp -nwarp ‘anat.un.aff.qw_WARP.nii anat.un.aff.nii.Xaff.1D’ -ainterp NN -prefix atlas_native_space.nii

I am not sure if I understand option -iwarp correctly, but the result given by script above does not match the subject_t1 image. Actually, it is far away from the subject_t1 image. Could you help to take a look at if this script is the correct way to do this? If not, how can I achieve my goal (i.e., warp-back the masks from standard space to native space according to the nonlinear transformation of subject’s t1 to the template)? Thanks!

Best,
Qiuhai

To make my question clear: what I want to do is similar as a warp that we can do via affine transformation. For example: we can first register an individual’s structural image to a template by using:

@auto_tlrc -base template+tlrc -input t1+orig -suffix _al

and then, inverse warp a set of masks from standard space back to the native space by using:

cat_matvec -ONELINE t1_al+tlrc::WARP_DATA > tlrc.aff12.1D
3dAllineate -1Dmatrix_apply tlrc.aff12.1D -source masks+tlrc -master t1+orig -prefix masks_in_native_space -final NN

But I would like to do this (both steps) via a nonlinear transformation approach. Is this possible? and any suggestions?

Many thanks!
Qiuhai

It’s possible, my first guess is that your data is being corrected for centers that are far apart and that’s may not be included in the transform. Can you post the terminal output from the auto_warp.py command?

Also, what version of AFNI are you running?

Hi Peter,

Thank you for your response. Actually, your answer reminded me a warning information about deoblique at the beginning when I ran auto_warp.py. Then I took anther version of T1 image (skull stripped) from Freesurfer and the result of inverse warping looks reasonable now, although it is still a little off, but I think that might be due to the alignment between T1 and template.

I would still like to post the terminal output here, to see if you could help to check everything looks normal. Thanks! The AFNI version I used is AFNI_19.0.24 (Mar 8 2019)

Below are the terminal output for auto_warp.py

[Qiuhais-MacBook-Pro-2:~/jin/5006] qiuhai% auto_warp.py -base MNI_T1_ns.nii -input brain.nii -skull_strip_input no
#++ auto_warp.py version: 0.04

Output directory /Users/qiuhai/jin/5006/awpy/

#Script is running:
mkdir /Users/qiuhai/jin/5006/awpy/
cd /Users/qiuhai/jin/5006/awpy/
#Script is running:
3dcopy /Users/qiuhai/jin/5006/brain.nii /Users/qiuhai/jin/5006/awpy/anat.nii
++ 3dcopy: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
#Script is running (command trimmed):
3dUnifize -GM -input ./anat.nii -prefix ./anat.un.nii
++ 3dUnifize: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]

  • Pre-processing: ADV…UWGm
    ++ Output dataset ./anat.un.nii
    ++ ===== CPU time = 51.8 sec Elapsed = 12.1
    #Script is running:
    3dcopy /Users/qiuhai/jin/5006/MNI_T1_ns.nii /Users/qiuhai/jin/5006/awpy/base.nii
    ++ 3dcopy: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    #++ Aligning /Users/qiuhai/jin/5006/awpy/base.nii data to /Users/qiuhai/jin/5006/awpy/anat.un.nii data
    #Script is running (command trimmed):
    @auto_tlrc -base ./base.nii -input ./anat.un.nii -suffix .aff -no_ss -no_pre -init_xform CENTER
    Copying NIFTI volume to ./anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA
    ++ 3dcopy: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]

Performing center alignment with @Align_Centers

++ 3dcopy: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: RW Cox
++ Processing AFNI dataset ./anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+orig

  • deoblique
    ++ 3drefit processed 1 datasets
    ++ 3drename: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ THD_rename_dataset_files: rename anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+orig.HEAD → __ats_tmp__anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+orig.HEAD
    ++ THD_rename_dataset_files: rename anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+orig.BRIK.gz → __ats_tmp__anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+orig.BRIK.gz
    ** THD_rename_dataset_files: old header anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+acpc.HEAD doesn’t exist!
    ** THD_rename_dataset_files: old header anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA_shft+tlrc.HEAD doesn’t exist!
    ++ 3dWarp: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    Center distance of 0.000015 mm
    Padding …
    ++ 3dZeropad: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ output dataset: ./__ats_tmp__ref_base.nii_40pad+tlrc.BRIK
    Resampling …
    ++ 3dcalc: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: A cast of thousands
    ++ 3dcalc: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: A cast of thousands
    Clipping -0.000100 1566.890100 …
    ++ 3dcalc: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: A cast of thousands
    ++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    ++ Processing AFNI dataset __ats_tmp___rs_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+tlrc.HEAD
  • changing dataset view code
  • Changed dataset view type and filenames.
    ++ 3drefit processed 1 datasets
    ++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    ++ Processing AFNI dataset __ats_tmp__resamp_NN+tlrc.HEAD
  • changing dataset view code
  • Changed dataset view type and filenames.
    ++ 3drefit processed 1 datasets
    ++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    ++ Processing AFNI dataset __ats_tmp__resamp_edge_art+tlrc.HEAD
  • changing dataset view code
  • Changed dataset view type and filenames.
    ++ 3drefit processed 1 datasets
    Registration (linear final interpolation) …
    ++ 3dWarpDrive: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    RMS[0] = 1884.17 851.388 ITER = 14/137
    1884.17

Warping has converged.

++ 3dcopy: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ 3dWarp: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: RW Cox
Applying brain mask
++ 3dcalc: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: A cast of thousands
++ 3dWarp: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: RW Cox
++ 3drename: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ THD_rename_dataset_files: rename __ats_tmp___pad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+orig.HEAD → __ats_tmp___pad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff.skl+orig.HEAD
++ THD_rename_dataset_files: rename __ats_tmp___pad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+orig.BRIK.gz → __ats_tmp___pad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff.skl+orig.BRIK.gz
++ 3dcalc: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: A cast of thousands
Unpadding …
++ 3dZeropad: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ output dataset: ./__ats_tmp___upad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+orig.BRIK
++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: RW Cox
++ Processing AFNI dataset __ats_tmp___upad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+orig
*+ WARNING: Changing the space of an ORIG view dataset may cause confusion!
*+ WARNING: NIFTI copies will be interpreted as TLRC view (not TLRC space).
*+ WARNING: Consider changing the view of the dataset to TLRC view also
++ 3drefit processed 1 datasets
Changing view of transformed anatomy
++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: RW Cox
++ Processing AFNI dataset __ats_tmp___upad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+orig.HEAD

  • changing dataset view code
  • Changed dataset view type and filenames.
    ++ 3drefit processed 1 datasets
    Setting parent with 3drefit -wset ./anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+orig.HEAD __ats_tmp___upad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+tlrc
    ++ 3drefit: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: RW Cox
    ++ Processing AFNI dataset __ats_tmp___upad40_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA.aff+tlrc
  • setting Warp parent
    ++ 3drefit processed 1 datasets
    ++ 3dAFNItoNIFTI: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    Cleanup …
    ++ 3drename: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Warning: ignoring +orig on new_prefix.
    ++ THD_rename_dataset_files: rename __ats_tmp___rs_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+orig.HEAD → __ats_tmp___rs_pre.anat.un+orig.HEAD
    ++ THD_rename_dataset_files: rename __ats_tmp___rs_pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+orig.BRIK.gz → __ats_tmp___rs_pre.anat.un+orig.BRIK.gz
    ++ 3drename: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Warning: ignoring +orig on new_prefix.
    ++ THD_rename_dataset_files: rename pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+orig.HEAD → pre.anat.un+orig.HEAD
    ++ THD_rename_dataset_files: rename pre.anat.un_AFN_OYmeqfZ3JqF7n9LNC7gDAA+orig.BRIK.gz → pre.anat.un+orig.BRIK.gz
    #Script is running:
    3dAttribute DELTA /Users/qiuhai/jin/5006/awpy/anat.un.aff.nii
    #Script is running:
    3dAttribute DELTA /Users/qiuhai/jin/5006/awpy/base.nii
    2.000000 2.000000
    #Script is running (command trimmed):
    3dinfo -same_grid ./anat.un.aff.nii ./base.nii
    #++ Aligning /Users/qiuhai/jin/5006/awpy/base.nii data to /Users/qiuhai/jin/5006/awpy/anat.un.aff.nii data
    #Script is running (command trimmed):
    3dQwarp -prefix ./anat.un.aff.qw.nii -blur -3 -3 -workhard:0:1 -base ./base.nii -source ./anat.un.aff.nii
    ++ OpenMP thread count = 12
    ++ 3dQwarp: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: Zhark the (Hermite) Cubically Warped
    ++ Dataset zero-pad: xbot=1 xtop=2 ybot=2 ytop=5 zbot=5 ztop=0 voxels
    ++ Weightizing the base image
    ++ +++++++++++ Begin warp optimization: base=./base.nii source=./anat.un.aff.nii
    ++ AFNI warpomatic: 94 x 116 x 96 volume ; autobbox = 5…87 7…107 5…89 [clock= 502ms]
    lev=0 0…93 0…115 0…95: [first cost=-0.81092] … done [cost:-0.81092==>-0.89414]
    lev=1 patch=71x87x71 [clock= 11s 133ms]…:[cost=-0.94629]:… done [cost:-0.89414==>-0.95986 ; 16 patches optimized, 0 skipped]
    lev=2 patch=53x65x53 [clock= 16s 830ms]… done [cost:-0.95986==>-1.04325 ; 27 patches optimized, 0 skipped]
    lev=3 patch=39x49x41 [clock= 22s 45ms]… done [cost:-1.04325==>-1.11984 ; 50 patches optimized, 14 skipped]
    lev=4 patch=29x37x31 [clock= 31s 648ms]… done [cost:-1.11984==>-1.17822 ; 105 patches optimized, 45 skipped]
    lev=5 patch=25x25x25 [clock= 40s 856ms]… done [cost:-1.17822==>-1.24541 ; 241 patches optimized, 151 skipped]
    ++ ========== total number of parameters ‘optimized’ = 5427
    ++ Output dataset ./anat.un.aff.qw.nii
    ++ Output dataset ./anat.un.aff.qw_WARP.nii
    ++ ===== CPU time = 333.8 sec clock time = 53s 854ms
    #++ Applying warps to /Users/qiuhai/jin/5006/brain.nii
    #Script is running (command trimmed):
    3dNwarpApply -nwarp “./anat.un.aff.qw_WARP.nii anat.un.aff.Xat.1D” -master ./base.nii -source /Users/qiuhai/jin/5006/brain.nii -prefix ./brain.aw.nii
    ++ 3dNwarpApply: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
    ++ Authored by: Zhark the Warped
    ++ -master dataset is ‘./base.nii’
    ++ opened source dataset ‘/Users/qiuhai/jin/5006/brain.nii’
    ++ Processing -nwarp
    ++ Warping:.Z
    ++ Output dataset ./brain.aw.nii
    ++ total CPU time = 3.3 sec Elapsed = 0.5
    #++ Saving history
    #Script is running (command trimmed):
    3dNotes -h “auto_warp.py -base MNI_T1_ns.nii -input brain.nii
    -skull_strip_input no” ./brain.aw.nii

And below is the terminal output for 3dNwarpApply that I re-ran with -iwarp option:

[Qiuhais-MacBook-Pro-2:~/jin/5006] qiuhai% 3dNwarpApply -master brain.nii -dxyz 2 -source aal_MNI_V4.nii -iwarp -nwarp “awpy/anat.un.aff.qw_WARP.nii awpy/anat.un.aff.nii.Xaff12.1D” -ainterp NN -prefix aal_MNI_V4_NL_native.nii
++ 3dNwarpApply: AFNI version=AFNI_19.0.24 (Mar 8 2019) [64-bit]
++ Authored by: Zhark the Warped
++ -master dataset is ‘brain.nii’
++ output grid size = 2 mm
++ opened source dataset ‘aal_MNI_V4.nii’
++ Processing -nwarp

  • – invert max|AA|=40.394058
    • init nstep=7 qq=1/2^7=0.007812
    • start iterations: normAA=28.084309 inewtfac=0.273993
    • iterate 1 nrat=0.029783
    • iterate 2 nrat=0.021639
    • iterate 3 nrat=0.019353
    • iterate 4 nrat=0.012896
    • iterate 5 nrat=0.010630
    • iterate 6 nrat=0.006168
    • iterate 7 nrat=0.004342
    • iterate 8 nrat=0.002062
    • iterate 9 nrat=0.001304
    • iterate 10 nrat=0.000478
    • switching from linear interp
    • iterate 11 nrat=0.000187
    • iterate 12 nrat=0.000063
  • – iteration converged
    ++ Warping:.Z
    ++ Output dataset ./aal_MNI_V4_NL_native.nii
    ++ total CPU time = 61.4 sec Elapsed = 7.0

Hi Peter,

I carefully checked the results, but the mask file is still far away from the anatomical image. Please see pictures attached for details. Underlay shows the anatomical image in native space, and Overlay shows the atlas file given by the script that I posted in my last message. The shape looks okay, but I am not sure why it is superior to the anatomical image. All terminal output of auto_warp.py and 3dNwarpApply are posted in my last message.

Qiuhai

2.jpg

3.jpg

What I can update here is that, if I inverse-apply the anat.un_shft.1D (given by auto_warp.py) to the output (i.e., inverse-warped atlas), it gave a somewhat good match between the inverse-warped atlas and the individual’s anatomical image in the native space. However, a few top slices of the inverse-warped atlas were missing (see the sagittal view of my previous post), probably due to the previous displacement. So I just added the anat.un_shft.1D (firstly converted to anat.un_shft.aff12.1D) at the end of -nwarp option in 3dNwarpApply shown as below:

3dNwarpApply -master subject_t1.nii -dxyz 2 -source atlas_masks.nii -iwarp -nwarp ‘anat.un.aff.qw_WARP.nii anat.un.aff.nii.Xaff.1D anat.un_shft.aff12.1D’ -ainterp NN -prefix atlas_native_space.nii

Then, it gave a near-perfect match between the inverse-warped atlas and the anatomical image in the native space. I am not sure if this is the correct way to achieve my goal, although the outcome looks good. It is probably because at the very beginning of auto_warp.py, the program made such a shift to the image, but have not included it when considering the -iwarp option. Any thoughts?

Best,
Qiuhai

Hi Qiuhai-

That should be correct, you can input multiple warps to 3dNwarpApply and it will concatenate them on the fly. You could also use cat_matvec to concatenate the two linear warps (_shft + 12.1D) and then pass that as one warp to 3dNwarpApply.

-Peter

Oh Peter, I just find your name on the poster of the Haskins pediatric template and atlas. Is there any paper that we can cite if we use these template and atlas? Actually, the anatomical data that I do the nonlinear warping comes from a child, so we also consider using a pediatric template. Thanks!

Qiuhai

The paper is a couple days from submission with final edits in the hands of coauthors. In the meantime, you can cite the poster:

Molfese, Glen, Mesite, Pugh, & Cox (2015, June). The Haskins Pediatric Brain Atlas.
Poster session presented at the Organization for Human Brain Mapping, Honolulu, HI

And you’ve probably already found our template included in AFNI (HaskinsPeds_NL_template1.0+tlrc) and one that can also be used with @SSwarper (HaskinsPeds_NL_template1.0_SSW.nii.gz)