@@ -42,6 +42,44 @@ def _spatial_dims_last(ngff_image: NgffImage) -> NgffImage:
4242 return result
4343
4444
45+ def _spatial_dims_last_zyx (ngff_image : NgffImage ) -> NgffImage :
46+ dims = list (ngff_image .dims )
47+ spatial_dims = [dim for dim in dims if dim in _spatial_dims ]
48+
49+ # If spatial dimensions are already zyx, return the original image
50+ if spatial_dims == ["z" , "y" , "x" ] or spatial_dims == ["y" , "x" ]:
51+ dims_spatial_channel = len (spatial_dims )
52+ if dims [- 1 ] == "c" :
53+ dims_spatial_channel += 1
54+
55+ # If spatial dimensions are already last (and 'c' can be last), return the original image
56+ if all (dim in dims [- dims_spatial_channel :] for dim in spatial_dims + ["c" ]):
57+ return ngff_image
58+
59+ # Move spatial dimensions to the end, keeping 'c' as the last pre-spatial dimension if present
60+ non_spatial_dims = [dim for dim in dims if dim not in _spatial_dims ]
61+ new_spatial_dims = ["z" , "y" , "x" ][- len (spatial_dims ) :]
62+ if "c" in non_spatial_dims :
63+ non_spatial_dims .remove ("c" )
64+ new_dims = non_spatial_dims + ["c" ] + new_spatial_dims
65+ else :
66+ new_dims = non_spatial_dims + new_spatial_dims
67+
68+ new_order = [dims .index (dim ) for dim in new_dims ]
69+
70+ if tuple (new_dims ) == tuple (ngff_image .dims ):
71+ return ngff_image
72+
73+ # Reorder the data array
74+ reordered_data = ngff_image .data .transpose (new_order )
75+
76+ result = copy .copy (ngff_image )
77+ result .data = reordered_data
78+ result .dims = tuple (new_dims )
79+
80+ return result
81+
82+
4583def _channel_dim_last (ngff_image : NgffImage ) -> NgffImage :
4684 if "c" not in ngff_image .dims or ngff_image .dims [- 1 ] == "c" :
4785 return ngff_image
0 commit comments