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

Method __call__

monai/transforms/spatial/array.py:3202–3238  ·  view source on GitHub ↗
(
        self, image: NdarrayOrTensor, size: int | tuple[int, int] | np.ndarray | None = None
    )

Source from the content-addressed store, hash-verified

3200 self.size = None if size is None else ensure_tuple_rep(size, len(self.grid))
3201
3202 def __call__(
3203 self, image: NdarrayOrTensor, size: int | tuple[int, int] | np.ndarray | None = None
3204 ) -> list[NdarrayOrTensor]:
3205 input_size = self.size if size is None else ensure_tuple_rep(size, len(self.grid))
3206
3207 if self.grid == (1, 1) and input_size is None:
3208 return [image]
3209 if isinstance(image, MetaTensor) and image.pending_operations:
3210 warnings.warn("MetaTensor img has pending operations, transform may return incorrect results.")
3211 split_size, steps = self._get_params(image.shape[1:], input_size)
3212 patches: list[NdarrayOrTensor]
3213 as_strided_func: Callable
3214 if isinstance(image, torch.Tensor):
3215 as_strided_func = torch.as_strided
3216 c_stride, x_stride, y_stride = image.stride()
3217 elif isinstance(image, np.ndarray):
3218 as_strided_func = np.lib.stride_tricks.as_strided
3219 c_stride, x_stride, y_stride = image.strides
3220 else:
3221 raise ValueError(f"Input type [{type(image)}] is not supported.")
3222
3223 x_step, y_step = steps
3224 n_channels = image.shape[0]
3225 strided_image = as_strided_func(
3226 image,
3227 (*self.grid, n_channels, split_size[0], split_size[1]),
3228 (x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),
3229 )
3230 # Flatten the first two dimensions
3231 strided_image = strided_image.reshape(-1, *strided_image.shape[2:])
3232 # Make a list of contiguous patches
3233 if isinstance(image, torch.Tensor):
3234 patches = [p.contiguous() for p in strided_image]
3235 elif isinstance(image, np.ndarray):
3236 patches = [np.ascontiguousarray(p) for p in strided_image]
3237
3238 return patches
3239
3240 def _get_params(self, image_size: Sequence[int] | np.ndarray, size: Sequence[int] | np.ndarray | None = None):
3241 """

Callers

nothing calls this directly

Calls 2

_get_paramsMethod · 0.95
ensure_tuple_repFunction · 0.90

Tested by

no test coverage detected