Help with diffeomorphic warping script

I am trying to do a diffeomorphic warp for an analysis. We are trying to use the non-linear warp to more accurately map out an area of hypersignal. The script I used was to take an aligned T2 set (pic attached) and output a non-linear warp. What resulted is the second pic, which doesn’t look quite right. What am I doing wrong? The script is as follows:

if ( ! -f ${dset_pre}_WARP.nii.gz ) then
		echo "running nonlinear alignment with auto_warp for $subj" -base $template -affine_input_xmat ID -qworkhard 0 2 \
		-input ${dset_pre}_aff${templateview} -overwrite \
		-output_dir awpy_${dset_pre} -qw_opts -iwarp
		cp awpy_${dset_pre}/anat.un.qw_WARP.nii ${dset_pre}_WARP.nii
		cp awpy_${dset_pre}/anat.un.qw_WARPINV.nii ${dset_pre}_WARPINV.nii
		gzip -f ${dset_pre}_WARP.nii ${dset_pre}_WARPINV.nii
		echo "nonlinear alignment done for $subj"
	# combine nonlinear and affine warps for dataset warped to standard template space
	if ( ! -f ${dset_pre}_warp2std.nii.gz ) then
		echo "combining nonlinear and affine warps to warp original dataset to NMT space for $subj"
		3dNwarpApply -prefix ${dset_pre}_warp2std.nii.gz \
		-nwarp "${dset_pre}_WARP.nii.gz ${dset_pre}_al2std_mat.aff12.1D" \
		-source ${dset_pre}${dset_view} -master $template
		echo "nonlinear and affine warps applied to original dataset for $subj"

Thank you for your help!



Can you please describe a bit more what datasets you are inputting into the initial warping program? Basically, there is probably a “source” and a “base” dataset: what you might think of as warping “from” and “to”, respectively. Important considerations are:

  • relative tissue contrast of the datasets (do they both have bright CSF/ventricles and dark GM? or opposite contrasts?)
  • spatial resolution: do they have similar voxel size?
  • is this the same subjects’ data, or different subjects’ data?
  • initial overlap: are they “far apart” from each other, in terms of relative shifts and rotations?
  • are both dsets whole brain coverage, or something else?
  • does one/both have their skull on?
    These factors determine initial parameter settings for warping, with perhaps the most important one being cost function (highly dependent on relative contrast of dsets). Knowing the above kind of information is vital for every alignment procedure (one also generally wants to consider the purpose of alignment, for determining how much warping to allow-- you have already stated that nonlinear alignment suits your purpose).

In part of the AFNI Academy set of videos online, there is a playlist about alignment, and I think it would probably be useful to watch most of these, certainly the opening one about a lot of these concepts, as well as the nonlinear one:


Thank you for your feedback. I’ll check out the AFNI Academy videos in order to better understand what I’m trying to do and how to do it. I think I don’t understand enough about what is happening to ask the right questions. I’ll watch the videos and then come back if I can’t figure out what is wrong.

Hi, JR-

OK, that sounds like a plan. For discussing this more, knowing what your $template and $dset_pre variables/datasets are will be key. For example, most templates are T1w volumes, and you said you are looking at using a T2w input (is that your ${dset_pre}?). If that is the case, those dsets have opposite tissue contrast, and you would want to use a cost function appropriate for that-- such as “lpc” or some variant thereof.

I think after watching the alignment lectures, these kinds of things will be easier to discuss, so please ping back.


If you are aligning macaques or other nonhuman subjects, you might want to try @animal_warper. Aligning to one of the NMT or D99 templates would require an lpa cost function. Take a look at the @Install_MACAQUE_DEMO for an example of alignment and processing.

It is a macaque brain. I did not know about @animal_warper. I will look at that and the demo you mentioned.
Thank you!

Oooh, I should probably update the MACAQUE_DEMO…

I will try to do this soon, but the existing demo should have any features.


Thank you all for your help. @animal_warper worked well for my T1 scans. However, 3/4 of my scans are T2. The template I am using is the NMT template, which is a T1 scan. Is there an option that I can use to make the command work for a T1 template with a T2 scan? I couldn’t find anything in the command documentation for what to do.
Another problem I am having is that the skulls are not being stripped off, even when I use the -skullstrip option with a template skull stripped. Is there something else I am missing?
Thank you!

You should have datasets that end in _warp2std_ns.nii.gz and _warp2std_nsu.nii.gz in your animal warper output directory that correspond to the skull stripped (no skull) and a unifized version of that.

For T2 images to align to a T1 template (or vice versa), you need a cost function that looks for the reverse contrast. The lpc method works well for this. Add the option “-cost lpc” or “-cost lpc+ZZ”.

… and just to add to Daniel’s post, if you would like to read about more of these programs (e.g., @animal_warper) and aspects of alignment like cost functions (esp. lpc and lpa), some papers describing them are listed here: