NBCLab / power-replication

A replication and extension of Power et al. (2018)
https://www.overleaf.com/read/swgjxcjqytxg
Apache License 2.0
2 stars 0 forks source link

Apply xfms to get T1w-space masks into scanner (BOLD reference) space #11

Closed tsalo closed 3 years ago

tsalo commented 3 years ago

Is anyone familiar with the xfm.txt files produced by fMRIPrep? Specifically, is there a Python function (e.g., in nitransforms) to apply an xfm.txt-stored transform to an image? Tagging @emdupre, in case you might know.

emdupre commented 3 years ago

I'd think to use apply off-hand ! You should be able to directly load the xfm.txt file, too, with nitransforms.linear.load.

tsalo commented 3 years ago

I did think apply would work, but:

import nibabel as nib
import nitransforms as nit

scanner_to_t1w = "sub-04570_task-rest_from-scanner_to-T1w_mode-image_xfm.txt"
scanner_img = nib.load("sub-04570_task-rest_echo-2_space-scanner_desc-partialPreproc_bold.nii.gz")
t1w_img = nib.load("sub-04570_task-rest_space-T1w_boldref.nii.gz")

xfm1 = nit.linear.load(scanner_to_t1w, fmt="itk")
xfm1.apply(spatialimage=scanner_img, reference=t1w_img)

got me:

RuntimeError                              Traceback (most recent call last)
<ipython-input-21-35d477a5eb1d> in <module>
----> 1 xfm1.apply(spatialimage=scanner_img, reference=t1w_img)

/opt/miniconda3/lib/python3.8/site-packages/nitransforms/base.py in apply(self, spatialimage, reference, order, mode, cval, prefilter, output_dtype)
    277         )
    278 
--> 279         resampled = ndi.map_coordinates(
    280             data,
    281             targets.T,

/opt/miniconda3/lib/python3.8/site-packages/scipy/ndimage/interpolation.py in map_coordinates(input, coordinates, output, order, mode, cval, prefilter)
    381         raise RuntimeError('input and output rank must be > 0')
    382     if coordinates.shape[0] != input.ndim:
--> 383         raise RuntimeError('invalid shape for coordinate array')
    384     mode = _ni_support._extend_mode_to_code(mode)
    385     if prefilter and order > 1:

RuntimeError: invalid shape for coordinate array
tsalo commented 3 years ago

Oh, wait. It works on the first volume of the 4D img when I select with nilearn.image.index_img, so maybe I need to split it from 4D to 3D and then merge back after transforming? That seems like a lot of work...

tsalo commented 3 years ago

I should be able to just do everything in scanner space instead of T1w space by transforming the masks and dseg files instead of the BOLD data though. I think that was the goal to begin with.

emdupre commented 3 years ago

I think that makes sense, too ! From a quick read through of their issue board, it seems like they expect that with eg BOLD data you'd be taking a composite of xfms such that it includes one with a time dimension, like HMC. So, if you have a constant transform to apply across all images, applying directly to each frame is probably the way to go. Maybe you could use nilearn.image.iter_img to that effect ?

tsalo commented 3 years ago

I'll have to do that when I transform the MEDN and other outputs to MNI space for the motion analyses (unless I transform the Power coordinates to scanner space I guess?), but for most analyses scanner space is perfect.