Skip to content

Instantly share code, notes, and snippets.

@CYHSM
Last active August 21, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CYHSM/ed7c230cfa24b168fc69286f22b976b7 to your computer and use it in GitHub Desktop.
Save CYHSM/ed7c230cfa24b168fc69286f22b976b7 to your computer and use it in GitHub Desktop.
Register functional MRI images (fMRI) to template
def transform_to_template(fn_func, fn_template, save=False):
"""
Reads (4D)-functional images and transforms directly to template image
Inputs:
- fn_func : Path to functional images
- fn_template : Path to template brain, e.g. MNI template
- save : Whether to save transformed image to same folder as functional
Outputs:
- func_template : functional images transformed to template brain
"""
# Read functional images
func = ants.image_read(fn_func)
template = ants.image_read(fn_template)
# Register mean functional image to template brain, use SyNBold as transform as we register directly on functional images
anat_to_mni = ants.registration(fixed=template, moving=func.get_average_of_timeseries(),
type_of_transform='SyNBold')
# Now transform func to template space, use imagetype 3 for functional images
func_template = ants.apply_transforms(fixed=template, moving=func, transformlist=anat_to_mni['fwdtransforms'],
imagetype=3)
# Save transformed functional image
if save_anat:
fn_func_template = os.path.dirname(fn_func) + os.path.sep + 'template_' + os.path.basename(fn_func)
ants.image_write(func_template, fn_func_template)
return func_template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment