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

Method __call__

monai/transforms/croppad/array.py:995–1038  ·  view source on GitHub ↗

Args: img: input image to sample patches from. assuming `img` is a channel-first array. weight_map: weight map used to generate patch samples. The weights must be non-negative. Each element denotes a sampling weight of the spatial location. 0 indicate

(
        self,
        img: torch.Tensor,
        weight_map: NdarrayOrTensor | None = None,
        randomize: bool = True,
        lazy: bool | None = None,
    )

Source from the content-addressed store, hash-verified

993 self._lazy = _val
994
995 def __call__(
996 self,
997 img: torch.Tensor,
998 weight_map: NdarrayOrTensor | None = None,
999 randomize: bool = True,
1000 lazy: bool | None = None,
1001 ) -> list[torch.Tensor]:
1002 """
1003 Args:
1004 img: input image to sample patches from. assuming `img` is a channel-first array.
1005 weight_map: weight map used to generate patch samples. The weights must be non-negative.
1006 Each element denotes a sampling weight of the spatial location. 0 indicates no sampling.
1007 It should be a single-channel array in shape, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`
1008 randomize: whether to execute random operations, default to `True`.
1009 lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.
1010
1011 Returns:
1012 A list of image patches
1013 """
1014 img_shape = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]
1015
1016 if randomize:
1017 if weight_map is None:
1018 weight_map = self.weight_map
1019 if weight_map is None:
1020 raise ValueError("weight map must be provided for weighted patch sampling.")
1021 w_shape = weight_map.peek_pending_shape() if isinstance(weight_map, MetaTensor) else weight_map.shape[1:]
1022 if img_shape != w_shape:
1023 warnings.warn(f"image and weight map spatial shape mismatch: {img_shape} vs {w_shape}.")
1024 self.randomize(weight_map)
1025
1026 _spatial_size = fall_back_tuple(self.spatial_size, img_shape)
1027 results: list[torch.Tensor] = []
1028 lazy_ = self.lazy if lazy is None else lazy
1029 for i, center in enumerate(self.centers):
1030 cropper = SpatialCrop(roi_center=center, roi_size=_spatial_size, lazy=lazy_)
1031 cropped = cropper(img)
1032 if get_track_meta():
1033 ret_: MetaTensor = cropped # type: ignore
1034 ret_.meta[Key.PATCH_INDEX] = i
1035 ret_.meta["crop_center"] = center
1036 self.push_transform(ret_, replace=True, lazy=lazy_)
1037 results.append(cropped)
1038 return results
1039
1040
1041class RandCropByPosNegLabel(Randomizable, TraceableTransform, LazyTransform, MultiSampleTrait):

Callers

nothing calls this directly

Calls 7

randomizeMethod · 0.95
fall_back_tupleFunction · 0.90
get_track_metaFunction · 0.90
SpatialCropClass · 0.85
peek_pending_shapeMethod · 0.80
push_transformMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected