Advice on keeping attributes up to date after slicing #8897
Replies: 3 comments 3 replies
-
Could be worth taking a page out of FreeSurfer's book and decompose the affine into the cosines, zooms and translations. The cosine could be static metadata on the axis, while the zooms and translations could be incorporated into the axis ticks, such that I say this without a definite model in my head of xarray's capabilities. |
Beta Was this translation helpful? Give feedback.
-
Sorry - to add - Chris' suggestion above is a workaround; using that workaround, we can work out what slicing the user has done to the object, and adjust the object accordingly - but that still means that, after the user has sliced the object, the object is in an unresolved state, where the metadata no longer matches the image, until we update it. So - returning to my original question - is there any way of putting hooks into the slicing such that we can update the metadata for the image when the user does the slicing? |
Beta Was this translation helpful? Give feedback.
-
A custom index may allow you to do this in future. You would attach the metadata to the custom index object, so that it is updated after indexing. |
Beta Was this translation helpful? Give feedback.
-
Could I ask for advice about attributes and array slicing?
My particular case is that I'm representing functional and structural MRI images as Xarrays - see e.g. this notebook.
This has many advantages, but one complexity is that each 3D brain volume has an associated [affine])https://nipy.org/nibabel/coordinate_systems.html) - a 4x4 array that maps coordinates in voxel space to coordinates in the real world - for example in terms of millimeters from the center of the scanner.
The transformation is conceptually fairly simple, but for now, lets just note that it includes the real-world coordinate of the voxel 0, 0, 0 in the array. Call this the origin part of the affine.
At the moment I store the affine as a 4x4 array attribute.
The problem is that, when I slice the 3D array - e.g.
img[:, :, 10:20]
, the corresponding affine needs to change to reflect the fact that the voxel at current coordinate 0, 0, 0 is the voxel that used to be at coordinate 10, 0, 0, and therefore its real-world coordinate in origin has to change accordingly.I'm not sure how to achieve that with Xarray. I could try and divert users to do all slicing operations via an accessor, and wrap them accordingly to fix up the affine, but that seems very error-prone - I guess many people will nevertheless use standard indexing, and evade my attempts to keep track of the affine. Is there a better way to add hooks to the indexing interfaces to apply the logic I need to keep the affine up to date?
Beta Was this translation helpful? Give feedback.
All reactions