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

Method __call__

monai/transforms/spatial/array.py:1765–1827  ·  view source on GitHub ↗

The grid can be initialized with a `spatial_size` parameter, or provided directly as `grid`. Therefore, either `spatial_size` or `grid` must be provided. When initialising from `spatial_size`, the backend "torch" will be used. Args: spatial_size: output

(
        self, spatial_size: Sequence[int] | None = None, grid: torch.Tensor | None = None, lazy: bool | None = None
    )

Source from the content-addressed store, hash-verified

1763 self.affine = affine
1764
1765 def __call__(
1766 self, spatial_size: Sequence[int] | None = None, grid: torch.Tensor | None = None, lazy: bool | None = None
1767 ) -> tuple[torch.Tensor | None, torch.Tensor]:
1768 """
1769 The grid can be initialized with a `spatial_size` parameter, or provided directly as `grid`.
1770 Therefore, either `spatial_size` or `grid` must be provided.
1771 When initialising from `spatial_size`, the backend "torch" will be used.
1772
1773 Args:
1774 spatial_size: output grid size.
1775 grid: grid to be transformed. Shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.
1776 lazy: a flag to indicate whether this transform should execute lazily or not
1777 during this call. Setting this to False or True overrides the ``lazy`` flag set
1778 during initialization for this call. Defaults to None.
1779 Raises:
1780 ValueError: When ``grid=None`` and ``spatial_size=None``. Incompatible values.
1781
1782 """
1783 lazy_ = self.lazy if lazy is None else lazy
1784 _device: torch.device | None
1785
1786 if not lazy_:
1787 if grid is None: # create grid from spatial_size
1788 if spatial_size is None:
1789 raise ValueError("Incompatible values: grid=None and spatial_size=None.")
1790 grid_ = create_grid(spatial_size, device=self.device, backend="torch", dtype=self.dtype)
1791 else:
1792 grid_ = grid
1793 _dtype = self.dtype or grid_.dtype
1794 grid_: torch.Tensor = convert_to_tensor(grid_, dtype=_dtype, track_meta=get_track_meta()) # type: ignore
1795 _device = torch.device(grid_.device) # type: ignore
1796 spatial_dims = len(grid_.shape) - 1
1797 else:
1798 _device = self.device # type: ignore[assignment]
1799 spatial_dims = len(spatial_size) # type: ignore
1800 _b = TransformBackends.TORCH
1801 affine: torch.Tensor
1802 if self.affine is None:
1803 affine = torch.eye(spatial_dims + 1, device=_device)
1804 if self.rotate_params:
1805 affine @= create_rotate(spatial_dims, self.rotate_params, device=_device, backend=_b) # type: ignore[assignment]
1806 if self.shear_params:
1807 affine @= create_shear(spatial_dims, self.shear_params, device=_device, backend=_b) # type: ignore[assignment]
1808 if self.translate_params:
1809 affine @= create_translate(spatial_dims, self.translate_params, device=_device, backend=_b) # type: ignore[assignment]
1810 if self.scale_params:
1811 affine @= create_scale(spatial_dims, self.scale_params, device=_device, backend=_b) # type: ignore[assignment]
1812 else:
1813 affine = self.affine # type: ignore
1814 affine = to_affine_nd(spatial_dims, affine)
1815 if lazy_:
1816 return None, affine
1817
1818 affine = convert_to_tensor(affine, device=grid_.device, dtype=grid_.dtype, track_meta=False) # type: ignore
1819 if self.align_corners:
1820 sc = create_scale(
1821 spatial_dims, [max(d, 2) / (max(d, 2) - 1) for d in grid_.shape[1:]], device=_device, backend=_b
1822 )

Callers

nothing calls this directly

Calls 10

create_gridFunction · 0.90
convert_to_tensorFunction · 0.90
get_track_metaFunction · 0.90
create_rotateFunction · 0.90
create_shearFunction · 0.90
create_translateFunction · 0.90
create_scaleFunction · 0.90
to_affine_ndFunction · 0.90
convert_to_dst_typeFunction · 0.90
maxFunction · 0.85

Tested by

no test coverage detected