Skip to content

Commit 65bbe5a

Browse files
committed
Updates CLI conversion with explicit spatial axis labels
Configures the conversion utility to explicitly label axes as "spatial_x", "spatial_y", and "spatial_z" for 2D and 3D images. This ensures that MLArray correctly interprets the XYZ axis convention provided by MedVol, preventing the default ZYX permutation and ensuring the affine matrix is applied correctly to the underlying array.
1 parent 469fe00 commit 65bbe5a

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

mlarray/cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,22 @@ def convert_to_mlarray(load_filepath: Union[str, Path], save_filepath: Union[str
130130
# Let MedVol auto-detect the backend (nibabel for NIfTI, pynrrd for NRRD).
131131
# The default canonicalize=True reorients the array+affine to RAS+.
132132
image_medvol = MedVol(load_filepath)
133+
# MedVol canonical output uses nibabel's XYZ axis convention: axis 0 ≈ X, axis 1 ≈ Y,
134+
# axis 2 ≈ Z, with affine column i = direction of array axis i.
135+
# SlicerMLArray assumes ZYX order when axis_labels is None. Supply explicit XYZ labels so
136+
# Slicer uses an identity permutation and the stored affine is used as-is.
137+
ndim = image_medvol.array.ndim
138+
if ndim == 3:
139+
axis_labels = ["spatial_x", "spatial_y", "spatial_z"]
140+
elif ndim == 2:
141+
axis_labels = ["spatial_x", "spatial_y"]
142+
else:
143+
axis_labels = None # 4D: non-spatial axis ambiguous; leave unlabeled
133144
image_mlarray = MLArray(
134145
image_medvol.array,
135146
affine=image_medvol.affine,
136147
meta=_header_to_source_meta(image_medvol.header),
148+
axis_labels=axis_labels,
137149
)
138150
coord_system = _coord_system_alias(image_medvol.coordinate_system)
139151
if coord_system is not None:

tests/test_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def test_convert_to_mlarray_from_nifti_copies_medvol_header_and_spatial(self):
134134
)
135135
self.assertEqual(loaded.meta._image_meta_format.to_plain(), "nifti")
136136
self.assertEqual(loaded.meta.spatial.coord_system, "RAS+")
137+
# XYZ axis labels are set so Slicer uses the affine as-is (identity permutation).
138+
self.assertEqual(loaded.meta.spatial.axis_labels, ["spatial_x", "spatial_y", "spatial_z"])
137139

138140
def test_convert_to_mlarray_from_nrrd_uses_medvol_coord_system(self):
139141
with tempfile.TemporaryDirectory() as tmpdir:

0 commit comments

Comments
 (0)