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

Function map_spatial_axes

monai/transforms/utils.py:1684–1716  ·  view source on GitHub ↗

Utility to map the spatial axes to real axes in channel first/last shape. For example: If `channel_first` is True, and `img` has 3 spatial dims, map spatial axes to real axes as below: None -> [1, 2, 3] [0, 1] -> [1, 2] [0, -1] -> [1, -1] If `channel_first` is False, and

(
    img_ndim: int, spatial_axes: Sequence[int] | int | None = None, channel_first: bool = True
)

Source from the content-addressed store, hash-verified

1682
1683
1684def map_spatial_axes(
1685 img_ndim: int, spatial_axes: Sequence[int] | int | None = None, channel_first: bool = True
1686) -> list[int]:
1687 """
1688 Utility to map the spatial axes to real axes in channel first/last shape.
1689 For example:
1690 If `channel_first` is True, and `img` has 3 spatial dims, map spatial axes to real axes as below:
1691 None -> [1, 2, 3]
1692 [0, 1] -> [1, 2]
1693 [0, -1] -> [1, -1]
1694 If `channel_first` is False, and `img` has 3 spatial dims, map spatial axes to real axes as below:
1695 None -> [0, 1, 2]
1696 [0, 1] -> [0, 1]
1697 [0, -1] -> [0, -2]
1698
1699 Args:
1700 img_ndim: dimension number of the target image.
1701 spatial_axes: spatial axes to be converted, default is None.
1702 The default `None` will convert to all the spatial axes of the image.
1703 If axis is negative it counts from the last to the first axis.
1704 If axis is a tuple of ints.
1705 channel_first: the image data is channel first or channel last, default to channel first.
1706
1707 """
1708 if spatial_axes is None:
1709 return list(range(1, img_ndim) if channel_first else range(img_ndim - 1))
1710 spatial_axes_ = []
1711 for a in ensure_tuple(spatial_axes):
1712 if channel_first:
1713 spatial_axes_.append(a % img_ndim if a < 0 else a + 1)
1714 else:
1715 spatial_axes_.append((a - 1) % (img_ndim - 1) if a < 0 else a)
1716 return spatial_axes_
1717
1718
1719@contextmanager

Callers 1

__call__Method · 0.90

Calls 2

ensure_tupleFunction · 0.90
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…