Args: img: input data to crop samples from based on the pos/neg ratio of `label` and `image`. Assumes `img` is a channel-first array. label: the label image that is used for finding foreground/background, if None, use `self.label`. image:
(
self,
img: torch.Tensor,
label: torch.Tensor | None = None,
image: torch.Tensor | None = None,
fg_indices: NdarrayOrTensor | None = None,
bg_indices: NdarrayOrTensor | None = None,
randomize: bool = True,
lazy: bool | None = None,
)
| 1169 | return False |
| 1170 | |
| 1171 | def __call__( |
| 1172 | self, |
| 1173 | img: torch.Tensor, |
| 1174 | label: torch.Tensor | None = None, |
| 1175 | image: torch.Tensor | None = None, |
| 1176 | fg_indices: NdarrayOrTensor | None = None, |
| 1177 | bg_indices: NdarrayOrTensor | None = None, |
| 1178 | randomize: bool = True, |
| 1179 | lazy: bool | None = None, |
| 1180 | ) -> list[torch.Tensor]: |
| 1181 | """ |
| 1182 | Args: |
| 1183 | img: input data to crop samples from based on the pos/neg ratio of `label` and `image`. |
| 1184 | Assumes `img` is a channel-first array. |
| 1185 | label: the label image that is used for finding foreground/background, if None, use `self.label`. |
| 1186 | image: optional image data to help select valid area, can be same as `img` or another image array. |
| 1187 | use ``label == 0 & image > image_threshold`` to select the negative sample(background) center. |
| 1188 | so the crop center will only exist on valid image area. if None, use `self.image`. |
| 1189 | fg_indices: foreground indices to randomly select crop centers, |
| 1190 | need to provide `fg_indices` and `bg_indices` together. |
| 1191 | bg_indices: background indices to randomly select crop centers, |
| 1192 | need to provide `fg_indices` and `bg_indices` together. |
| 1193 | randomize: whether to execute the random operations, default to `True`. |
| 1194 | lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None. |
| 1195 | |
| 1196 | """ |
| 1197 | if image is None: |
| 1198 | image = self.image |
| 1199 | if randomize: |
| 1200 | if label is None: |
| 1201 | label = self.label |
| 1202 | self.randomize(label, fg_indices, bg_indices, image) |
| 1203 | results: list[torch.Tensor] = [] |
| 1204 | if self.centers is not None: |
| 1205 | img_shape = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:] |
| 1206 | roi_size = fall_back_tuple(self.spatial_size, default=img_shape) |
| 1207 | lazy_ = self.lazy if lazy is None else lazy |
| 1208 | for i, center in enumerate(self.centers): |
| 1209 | cropper = SpatialCrop(roi_center=center, roi_size=roi_size, lazy=lazy_) |
| 1210 | cropped = cropper(img) |
| 1211 | if get_track_meta(): |
| 1212 | ret_: MetaTensor = cropped # type: ignore |
| 1213 | ret_.meta[Key.PATCH_INDEX] = i |
| 1214 | ret_.meta["crop_center"] = center |
| 1215 | self.push_transform(ret_, replace=True, lazy=lazy_) |
| 1216 | results.append(cropped) |
| 1217 | return results |
| 1218 | |
| 1219 | |
| 1220 | class RandCropByLabelClasses(Randomizable, TraceableTransform, LazyTransform, MultiSampleTrait): |
nothing calls this directly
no test coverage detected