Repository navigation
BUG: Fix axis order in image_from_xarray origin/spacing - #6937
hjmjohnson merged 1 commit into
Conversation
image_from_xarray() iterated the alphabetically-sorted image_dims list to assign per-axis origin/spacing values, while ordered_dims holds the correct canonical (t, z, y, x) order already computed earlier in the function. The two orderings coincide for dims without "t", masking the bug, but diverge once a "t" dimension is present, scrambling origin/spacing onto the wrong axes for 4D images. Iterate ordered_dims instead, and add a 4D coordinate test to catch this.
|
|
@SID-6921 Thanks for your PR! Honestly I'm not familiar with xarray, but it sounds like a serious bug. Do you also think so? Or is it rarely a problem, in practice? As far as I can see, the specific |
|
Yes, it's at least that old. I checked: the On severity: it only misfires when all four dims (t, z, y, x) are present. For 2D/3D subsets, reverse-alphabetical order happens to match the canonical order by coincidence, which is exactly why nobody caught it. The 3D tests all pass. So the practical impact is: build a true 4D image from xarray (e.g. a time-resolved volume) through this path, and you get correct pixel data but origin/spacing silently assigned to the wrong axes, no error raised. That's a real bug when you hit it, but the surface is narrow. It only bites 4D-with-named-time-axis usage of a function whose own docstring already calls it experimental. |
|
Thanks @SID-6921, this is a good partial step in the right direction. The fix is correct, and on the ITK 5.4.7 wheel the new asserts fail before your change and pass after it; a 4D round trip through What was checked locallyIsolated venv, ITK 5.4.7 wheel (wraps 4D
|
Fix
image_from_xarray()assigning origin/spacing to the wrong axes for 4D (t, z, y, x) images; the per-axis loop iterated an incorrectly-ordered variable instead of the already-computed canonical order. Adds a 4D coordinate test to catch this.Root cause
image_dimsis built by alphabetically sorting a set, whileordered_dims(computed just above it) already holds the correct canonical
(t, z, y, x)order used elsewhere in the same function for the valuestranspose. The origin/spacing loop iterated
image_dimsinstead ofordered_dims. The two orderings coincide whenever notdimension ispresent (3D/2D), which is why the existing tests passed, but diverge for
4D data, silently assigning each axis's origin/spacing to the wrong
dimension.
AI assistance
Wrapping/Generators/Python/itk/supportimage_dimsvsordered_dimsdivergence bytracing the 4D axis-ordering logic, proposed the one-line fix, and
added the regression test
Test results
Reproduced the ordering bug and verified the fix in isolation (no local
ITK Python build available) by extracting the axis-ordering logic from
image_from_xarrayinto a standalone script with mock coordinate data:(t, z, y, x)case: before fix,SetOrigin/SetSpacingorder wasscrambled (e.g. origin
(100.0, 30.0, 20.0, 10.0)instead of theexpected
(30.0, 20.0, 10.0, 100.0)); after the fix, output matchesexpected
(x, y, z, t)order exactly.(z, y, x)case: output unchanged before/after, confirming noregression for the common case.
test_xarray.pycoverage assertingGetOrigin()/GetSpacing()on a round-tripped 4D array with distinct per-axis coordinates, which
the existing 4D test did not check.