MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / __call__

Method __call__

monai/transforms/spatial/array.py:615–680  ·  view source on GitHub ↗

If input type is `MetaTensor`, original affine is extracted with `data_array.affine`. If input type is `torch.Tensor`, original affine is assumed to be identity. Args: data_array: in shape (num_channels, H[, W, ...]). lazy: a flag to indicate whether

(self, data_array: torch.Tensor, lazy: bool | None = None)

Source from the content-addressed store, hash-verified

613 self.labels = labels
614
615 def __call__(self, data_array: torch.Tensor, lazy: bool | None = None) -> torch.Tensor:
616 """
617 If input type is `MetaTensor`, original affine is extracted with `data_array.affine`.
618 If input type is `torch.Tensor`, original affine is assumed to be identity.
619
620 Args:
621 data_array: in shape (num_channels, H[, W, ...]).
622 lazy: a flag to indicate whether this transform should execute lazily or not
623 during this call. Setting this to False or True overrides the ``lazy`` flag set
624 during initialization for this call. Defaults to None.
625
626 Raises:
627 ValueError: When ``data_array`` has no spatial dimensions.
628 ValueError: When ``axcodes`` spatiality differs from ``data_array``.
629
630 Returns:
631 data_array [reoriented in `self.axcodes`]. Output type will be `MetaTensor`
632 unless `get_track_meta() == False`, in which case it will be
633 `torch.Tensor`.
634
635 """
636 spatial_shape = data_array.peek_pending_shape() if isinstance(data_array, MetaTensor) else data_array.shape[1:]
637 sr = len(spatial_shape)
638 if sr <= 0:
639 raise ValueError(f"data_array must have at least one spatial dimension, got {spatial_shape}.")
640 affine_: np.ndarray
641 affine_np: np.ndarray
642 labels = self.labels
643 if isinstance(data_array, MetaTensor):
644 affine_np, *_ = convert_data_type(data_array.peek_pending_affine(), np.ndarray)
645 affine_ = to_affine_nd(sr, affine_np)
646
647 # Set up "labels" such that LPS tensors are handled correctly by default
648 if (
649 self.labels is None
650 and "space" in data_array.meta
651 and SpaceKeys(data_array.meta["space"]) == SpaceKeys.LPS
652 ):
653 labels = (("R", "L"), ("A", "P"), ("I", "S")) # value for LPS
654
655 else:
656 warnings.warn("`data_array` is not of type `MetaTensor, assuming affine to be identity.")
657 # default to identity
658 affine_np = np.eye(sr + 1, dtype=np.float64)
659 affine_ = np.eye(sr + 1, dtype=np.float64)
660
661 src = nib.io_orientation(affine_)
662 if self.as_closest_canonical:
663 spatial_ornt = src
664 else:
665 if self.axcodes is None:
666 raise ValueError("Incompatible values: axcodes=None and as_closest_canonical=True.")
667 if sr < len(self.axcodes):
668 warnings.warn(
669 f"axcodes ('{self.axcodes}') length is smaller than number of input spatial dimensions D={sr}.\n"
670 f"{self.__class__.__name__}: spatial shape = {spatial_shape}, channels = {data_array.shape[0]},"
671 "please make sure the input is in the channel-first format."
672 )

Callers

nothing calls this directly

Calls 7

convert_data_typeFunction · 0.90
to_affine_ndFunction · 0.90
SpaceKeysClass · 0.90
orientationFunction · 0.90
peek_pending_shapeMethod · 0.80
peek_pending_affineMethod · 0.80
get_transform_infoMethod · 0.80

Tested by

no test coverage detected