7676 issequenceiterable ,
7777 optional_import ,
7878)
79+ from monai .utils .deprecate_utils import deprecated_arg_default
7980from monai .utils .enums import GridPatchSort , PatchKeys , TraceKeys , TransformBackends
8081from monai .utils .misc import ImageMetaKey as Key
8182from monai .utils .module import look_up_option
@@ -557,6 +558,15 @@ class Orientation(InvertibleTransform, LazyTransform):
557558
558559 backend = [TransformBackends .NUMPY , TransformBackends .TORCH ]
559560
561+ @deprecated_arg_default (
562+ name = "labels" ,
563+ old_default = (("L" , "R" ), ("P" , "A" ), ("I" , "S" )),
564+ new_default = None ,
565+ msg_suffix = (
566+ "Default value changed to None meaning that the transform now uses the 'space' of a "
567+ "meta-tensor, if applicable, to determine appropriate axis labels."
568+ ),
569+ )
560570 def __init__ (
561571 self ,
562572 axcodes : str | None = None ,
@@ -574,9 +584,14 @@ def __init__(
574584 as_closest_canonical: if True, load the image as closest to canonical axis format.
575585 labels: optional, None or sequence of (2,) sequences
576586 (2,) sequences are labels for (beginning, end) of output axis.
577- Defaults to using the ``"space"`` attribute of a metatensor,
578- where appliable, or (('L', 'R'), ('P', 'A'), ('I', 'S'))``
579- otherwise (i.e. for plain tensors).
587+ If ``None``, an appropriate value is chosen depending on the
588+ value of the ``"space"`` metadata item of a metatensor: if
589+ ``"space"`` is ``"LPS"``, the value used is ``(('R', 'L'),
590+ ('A', 'P'), ('I', 'S'))``, if ``"space"`` is ``"RPS"`` or the
591+ input is not a meta-tensor or has no ``"space"`` item, the
592+ value ``(('L', 'R'), ('P', 'A'), ('I', 'S'))`` is used. If not
593+ ``None``, the provided value is always used and the ``"space"``
594+ metadata item (if any) of the intput is ignored.
580595 lazy: a flag to indicate whether this transform should execute lazily or not.
581596 Defaults to False
582597
@@ -628,7 +643,11 @@ def __call__(self, data_array: torch.Tensor, lazy: bool | None = None) -> torch.
628643 affine_ = to_affine_nd (sr , affine_np )
629644
630645 # Set up "labels" such that LPS tensors are handled correctly by default
631- if self .labels is None and SpaceKeys (data_array .meta ["space" ]) == SpaceKeys .LPS :
646+ if (
647+ self .labels is None
648+ and "space" in data_array .meta
649+ and SpaceKeys (data_array .meta ["space" ]) == SpaceKeys .LPS
650+ ):
632651 labels = (("R" , "L" ), ("A" , "P" ), ("I" , "S" )) # value for LPS
633652
634653 else :
@@ -665,7 +684,12 @@ def inverse(self, data: torch.Tensor) -> torch.Tensor:
665684 labels = self .labels
666685
667686 # Set up "labels" such that LPS tensors are handled correctly by default
668- if isinstance (data , MetaTensor ) and self .labels is None and SpaceKeys (data .meta ["space" ]) == SpaceKeys .LPS :
687+ if (
688+ isinstance (data , MetaTensor )
689+ and self .labels is None
690+ and "space" in data .meta
691+ and SpaceKeys (data .meta ["space" ]) == SpaceKeys .LPS
692+ ):
669693 labels = (("R" , "L" ), ("A" , "P" ), ("I" , "S" )) # value for LPS
670694
671695 orig_axcodes = nib .orientations .aff2axcodes (orig_affine , labels = labels )
0 commit comments