diff --git a/monai/transforms/__init__.py b/monai/transforms/__init__.py index cb17f69c54..bc29e61a21 100644 --- a/monai/transforms/__init__.py +++ b/monai/transforms/__init__.py @@ -354,6 +354,8 @@ RepeatChannelD, RepeatChannelDict, SelectItemsd, + SelectItemsD, + SelectItemsDict, SimulateDelayd, SimulateDelayD, SimulateDelayDict, @@ -395,6 +397,7 @@ img_bounds, in_bounds, is_empty, + is_positive, map_binary_to_indices, map_spatial_axes, rand_choice, diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index dd6b5e3193..5b52673816 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -26,6 +26,7 @@ compute_divisible_spatial_size, generate_pos_neg_label_crop_centers, generate_spatial_bounding_box, + is_positive, map_binary_to_indices, weighted_patch_samples, ) @@ -400,7 +401,14 @@ class CropForeground(Transform): [0, 1, 3, 2, 0], [0, 1, 2, 1, 0], [0, 0, 0, 0, 0]]]) # 1x5x5, single channel 5x5 image - cropper = CropForeground(select_fn=lambda x: x > 1, margin=0) + + + def threshold_at_one(x): + # threshold at 1 + return x > 1 + + + cropper = CropForeground(select_fn=threshold_at_one, margin=0) print(cropper(image)) [[[2, 1], [3, 2], @@ -410,7 +418,7 @@ class CropForeground(Transform): def __init__( self, - select_fn: Callable = lambda x: x > 0, + select_fn: Callable = is_positive, channel_indices: Optional[IndexSelection] = None, margin: Union[Sequence[int], int] = 0, return_coords: bool = False, @@ -725,7 +733,7 @@ class BoundingRect(Transform): select_fn: function to select expected foreground, default is to select values > 0. """ - def __init__(self, select_fn: Callable = lambda x: x > 0) -> None: + def __init__(self, select_fn: Callable = is_positive) -> None: self.select_fn = select_fn def __call__(self, img: np.ndarray) -> np.ndarray: diff --git a/monai/transforms/croppad/dictionary.py b/monai/transforms/croppad/dictionary.py index 90ba2d601a..45d7768072 100644 --- a/monai/transforms/croppad/dictionary.py +++ b/monai/transforms/croppad/dictionary.py @@ -37,7 +37,12 @@ ) from monai.transforms.inverse import InvertibleTransform from monai.transforms.transform import MapTransform, Randomizable -from monai.transforms.utils import generate_pos_neg_label_crop_centers, map_binary_to_indices, weighted_patch_samples +from monai.transforms.utils import ( + generate_pos_neg_label_crop_centers, + is_positive, + map_binary_to_indices, + weighted_patch_samples, +) from monai.utils import ImageMetaKey as Key from monai.utils import Method, NumpyPadMode, ensure_tuple, ensure_tuple_rep, fall_back_tuple from monai.utils.enums import InverseKeys @@ -572,7 +577,7 @@ def __init__( self, keys: KeysCollection, source_key: str, - select_fn: Callable = lambda x: x > 0, + select_fn: Callable = is_positive, channel_indices: Optional[IndexSelection] = None, margin: int = 0, k_divisible: Union[Sequence[int], int] = 1, @@ -948,7 +953,7 @@ def __init__( self, keys: KeysCollection, bbox_key_postfix: str = "bbox", - select_fn: Callable = lambda x: x > 0, + select_fn: Callable = is_positive, allow_missing_keys: bool = False, ): super().__init__(keys, allow_missing_keys) diff --git a/monai/transforms/intensity/dictionary.py b/monai/transforms/intensity/dictionary.py index 1c393b9650..b06b85f316 100644 --- a/monai/transforms/intensity/dictionary.py +++ b/monai/transforms/intensity/dictionary.py @@ -102,6 +102,8 @@ "RandGaussianSharpenDict", "RandHistogramShiftD", "RandHistogramShiftDict", + "RandRicianNoiseD", + "RandRicianNoiseDict", ] diff --git a/monai/transforms/post/array.py b/monai/transforms/post/array.py index 7ac0e6799c..dd4f7afd9d 100644 --- a/monai/transforms/post/array.py +++ b/monai/transforms/post/array.py @@ -33,6 +33,7 @@ "LabelToContour", "MeanEnsemble", "VoteEnsemble", + "ProbNMS", ] @@ -74,7 +75,7 @@ def __call__( softmax: whether to execute softmax function on model output before transform. Defaults to ``self.softmax``. other: callable function to execute other activation layers, for example: - `other = lambda x: torch.tanh(x)`. Defaults to ``self.other``. + `other = torch.tanh`. Defaults to ``self.other``. Raises: ValueError: When ``sigmoid=True`` and ``softmax=True``. Incompatible values. diff --git a/monai/transforms/post/dictionary.py b/monai/transforms/post/dictionary.py index 52bde4ab79..13b709e284 100644 --- a/monai/transforms/post/dictionary.py +++ b/monai/transforms/post/dictionary.py @@ -57,6 +57,9 @@ "DecollateD", "DecollateDict", "Decollated", + "ProbNMSd", + "ProbNMSD", + "ProbNMSDict", ] @@ -83,7 +86,7 @@ def __init__( softmax: whether to execute softmax function on model output before transform. it also can be a sequence of bool, each element corresponds to a key in ``keys``. other: callable function to execute other activation layers, - for example: `other = lambda x: torch.tanh(x)`. it also can be a sequence of Callable, each + for example: `other = torch.tanh`. it also can be a sequence of Callable, each element corresponds to a key in ``keys``. allow_missing_keys: don't raise exception if key is missing. diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index f5c4461b99..dcf6416648 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -41,6 +41,7 @@ "CastToType", "ToTensor", "ToNumpy", + "ToPIL", "Transpose", "SqueezeDim", "DataStats", diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py index e4774be971..0409697ea1 100644 --- a/monai/transforms/utility/dictionary.py +++ b/monai/transforms/utility/dictionary.py @@ -53,84 +53,90 @@ from monai.utils import ensure_tuple, ensure_tuple_rep __all__ = [ - "Identityd", - "AsChannelFirstd", - "AsChannelLastd", + "AddChannelD", + "AddChannelDict", "AddChanneld", - "EnsureChannelFirstd", - "RepeatChanneld", - "RemoveRepeatedChanneld", - "SplitChanneld", - "CastToTyped", - "ToTensord", - "ToNumpyd", - "ToPILd", - "DeleteItemsd", - "SelectItemsd", - "SqueezeDimd", - "DataStatsd", - "SimulateDelayd", - "CopyItemsd", - "ConcatItemsd", - "Lambdad", - "RandLambdad", - "LabelToMaskd", - "FgBgToIndicesd", - "ConvertToMultiChannelBasedOnBratsClassesd", + "AddExtremePointsChannelD", + "AddExtremePointsChannelDict", "AddExtremePointsChanneld", - "TorchVisiond", - "RandTorchVisiond", - "MapLabelValued", - "IdentityD", - "IdentityDict", "AsChannelFirstD", "AsChannelFirstDict", + "AsChannelFirstd", "AsChannelLastD", "AsChannelLastDict", - "AddChannelD", - "AddChannelDict", + "AsChannelLastd", + "CastToTypeD", + "CastToTypeDict", + "CastToTyped", + "ConcatItemsD", + "ConcatItemsDict", + "ConcatItemsd", + "ConvertToMultiChannelBasedOnBratsClassesD", + "ConvertToMultiChannelBasedOnBratsClassesDict", + "ConvertToMultiChannelBasedOnBratsClassesd", + "CopyItemsD", + "CopyItemsDict", + "CopyItemsd", + "DataStatsD", + "DataStatsDict", + "DataStatsd", + "DeleteItemsD", + "DeleteItemsDict", + "DeleteItemsd", "EnsureChannelFirstD", "EnsureChannelFirstDict", + "EnsureChannelFirstd", + "FgBgToIndicesD", + "FgBgToIndicesDict", + "FgBgToIndicesd", + "IdentityD", + "IdentityDict", + "Identityd", + "LabelToMaskD", + "LabelToMaskDict", + "LabelToMaskd", + "LambdaD", + "LambdaDict", + "Lambdad", + "MapLabelValueD", + "MapLabelValueDict", + "MapLabelValued", "RandLambdaD", "RandLambdaDict", - "RepeatChannelD", - "RepeatChannelDict", + "RandLambdad", + "RandTorchVisionD", + "RandTorchVisionDict", + "RandTorchVisiond", "RemoveRepeatedChannelD", "RemoveRepeatedChannelDict", + "RemoveRepeatedChanneld", + "RepeatChannelD", + "RepeatChannelDict", + "RepeatChanneld", + "SelectItemsD", + "SelectItemsDict", + "SelectItemsd", + "SimulateDelayD", + "SimulateDelayDict", + "SimulateDelayd", "SplitChannelD", "SplitChannelDict", - "CastToTypeD", - "CastToTypeDict", - "ToTensorD", - "ToTensorDict", - "DeleteItemsD", - "DeleteItemsDict", + "SplitChanneld", "SqueezeDimD", "SqueezeDimDict", - "DataStatsD", - "DataStatsDict", - "SimulateDelayD", - "SimulateDelayDict", - "CopyItemsD", - "CopyItemsDict", - "ConcatItemsD", - "ConcatItemsDict", - "LambdaD", - "LambdaDict", - "LabelToMaskD", - "LabelToMaskDict", - "FgBgToIndicesD", - "FgBgToIndicesDict", - "ConvertToMultiChannelBasedOnBratsClassesD", - "ConvertToMultiChannelBasedOnBratsClassesDict", - "AddExtremePointsChannelD", - "AddExtremePointsChannelDict", + "SqueezeDimd", + "ToNumpyD", + "ToNumpyDict", + "ToNumpyd", + "ToPILD", + "ToPILDict", + "ToPILd", + "ToTensorD", + "ToTensorDict", + "ToTensord", "TorchVisionD", "TorchVisionDict", - "RandTorchVisionD", - "RandTorchVisionDict", - "MapLabelValueD", - "MapLabelValueDict", + "TorchVisiond", ] @@ -1062,6 +1068,7 @@ def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.nda ToNumpyD = ToNumpyDict = ToNumpyd ToPILD = ToPILDict = ToPILd DeleteItemsD = DeleteItemsDict = DeleteItemsd +SelectItemsD = SelectItemsDict = SelectItemsd SqueezeDimD = SqueezeDimDict = SqueezeDimd DataStatsD = DataStatsDict = DataStatsd SimulateDelayD = SimulateDelayDict = SimulateDelayd diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 87fc4365e4..911270b3fd 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -42,11 +42,13 @@ "img_bounds", "in_bounds", "is_empty", + "is_positive", "zero_margins", "rescale_array", "rescale_instance_array", "rescale_array_int_max", "copypaste_arrays", + "compute_divisible_spatial_size", "resize_center", "map_binary_to_indices", "weighted_patch_samples", @@ -97,6 +99,13 @@ def is_empty(img: Union[np.ndarray, torch.Tensor]) -> bool: return not (img.max() > img.min()) # use > instead of <= so that an image full of NaNs will result in True +def is_positive(img): + """ + Returns a boolean version of `img` where the positive values are converted into True, the other values are False. + """ + return img > 0 + + def zero_margins(img: np.ndarray, margin: int) -> bool: """ Returns True if the values within `margin` indices of the edges of `img` in dimensions 1 and 2 are 0. @@ -526,7 +535,7 @@ def create_translate(spatial_dims: int, shift: Union[Sequence[float], float]) -> def generate_spatial_bounding_box( img: np.ndarray, - select_fn: Callable = lambda x: x > 0, + select_fn: Callable = is_positive, channel_indices: Optional[IndexSelection] = None, margin: Union[Sequence[int], int] = 0, ) -> Tuple[List[int], List[int]]: diff --git a/monai/utils/enums.py b/monai/utils/enums.py index 9f753ca700..1b9e4f615d 100644 --- a/monai/utils/enums.py +++ b/monai/utils/enums.py @@ -30,6 +30,7 @@ "Method", "InverseKeys", "CommonKeys", + "ForwardMode", ]