Skip to content

Commit 28fc3af

Browse files
committed
Fix issues from review
Signed-off-by: Chris Bridge <[email protected]>
1 parent 1d389a2 commit 28fc3af

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

monai/transforms/spatial/array.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
issequenceiterable,
7777
optional_import,
7878
)
79+
from monai.utils.deprecate_utils import deprecated_arg_default
7980
from monai.utils.enums import GridPatchSort, PatchKeys, TraceKeys, TransformBackends
8081
from monai.utils.misc import ImageMetaKey as Key
8182
from 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)

monai/transforms/spatial/dictionary.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
ensure_tuple_rep,
7272
fall_back_tuple,
7373
)
74+
from monai.utils.deprecate_utils import deprecated_arg_default
7475
from monai.utils.enums import TraceKeys
7576
from monai.utils.module import optional_import
7677

@@ -545,6 +546,15 @@ class Orientationd(MapTransform, InvertibleTransform, LazyTransform):
545546

546547
backend = Orientation.backend
547548

549+
@deprecated_arg_default(
550+
name="labels",
551+
old_default=(("L", "R"), ("P", "A"), ("I", "S")),
552+
new_default=None,
553+
msg_suffix=(
554+
"Default value changed to None meaning that the transform now uses the 'space' of a "
555+
"meta-tensor, if applicable, to determine appropriate axis labels."
556+
),
557+
)
548558
def __init__(
549559
self,
550560
keys: KeysCollection,
@@ -564,9 +574,14 @@ def __init__(
564574
as_closest_canonical: if True, load the image as closest to canonical axis format.
565575
labels: optional, None or sequence of (2,) sequences
566576
(2,) sequences are labels for (beginning, end) of output axis.
567-
Defaults to using the ``"space"`` attribute of a metatensor,
568-
where appliable, or (('L', 'R'), ('P', 'A'), ('I', 'S'))``
569-
otherwise (i.e. for plain tensors).
577+
If ``None``, an appropriate value is chosen depending on the
578+
value of the ``"space"`` metadata item of a metatensor: if
579+
``"space"`` is ``"LPS"``, the value used is ``(('R', 'L'),
580+
('A', 'P'), ('I', 'S'))``, if ``"space"`` is ``"RPS"`` or the
581+
input is not a meta-tensor or has no ``"space"`` item, the
582+
value ``(('L', 'R'), ('P', 'A'), ('I', 'S'))`` is used. If not
583+
``None``, the provided value is always used and the ``"space"``
584+
metadata item (if any) of the intput is ignored.
570585
allow_missing_keys: don't raise exception if key is missing.
571586
lazy: a flag to indicate whether this transform should execute lazily or not.
572587
Defaults to False

0 commit comments

Comments
 (0)