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,
)
| 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 | |
| 1041 | class RandCropByPosNegLabel(Randomizable, TraceableTransform, LazyTransform, MultiSampleTrait): |
nothing calls this directly
no test coverage detected