Mni coords to array indices missing in nilearn?


I needed the inverse function to nilearn.image.coord_transform
I didn’t find in nilearn, so I had to implement it.
I believe it should be there, already.
(maybe I should have opened this as an enhancement request in github)

What I’ve done is simply inverting the affine and multiply it by the needed coordinate, and then I get the indices…
Any comments about this pseudo code or a suggested pseudo code for implementing this inverse function?


(Edit: I’ve tested my implementation and it seems fine (extracted some known region and compared its mean with masker.fit_transform and asserted that coord_transform(myfunction((x,y,z))=(x,y,z) ))


Hi @brai,

Inverse transforms of affines sounds like something nibabel would have solved,
you can check out their affine example (but the entire page is a great read to understand coordinates/coordinate systems)


1 Like

Hi, indeed going from coordinates to voxel indices is just mulitplying by the inverse affine and taking the integer part of the result. you can pass the inverse affine to coord_transform or just use, not sure a new function is needed for that

1 Like