From b11647a8ec06e27ec4b6a87ed5d3b37cdfa04a02 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 26 Jan 2023 09:42:35 +0000 Subject: [PATCH 1/3] will change to image_only=True in LoadImage Signed-off-by: Wenqi Li --- monai/transforms/io/array.py | 10 +++++++++- monai/transforms/io/dictionary.py | 2 ++ monai/utils/deprecate_utils.py | 10 +++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/monai/transforms/io/array.py b/monai/transforms/io/array.py index ef8ca31510..5e7e3cff88 100644 --- a/monai/transforms/io/array.py +++ b/monai/transforms/io/array.py @@ -44,7 +44,14 @@ from monai.transforms.utility.array import EnsureChannelFirst from monai.utils import GridSamplePadMode from monai.utils import ImageMetaKey as Key -from monai.utils import OptionalImportError, convert_to_dst_type, ensure_tuple, look_up_option, optional_import +from monai.utils import ( + OptionalImportError, + convert_to_dst_type, + deprecated_arg_default, + ensure_tuple, + look_up_option, + optional_import, +) nib, _ = optional_import("nibabel") Image, _ = optional_import("PIL.Image") @@ -119,6 +126,7 @@ class LoadImage(Transform): """ + @deprecated_arg_default("image_only", False, True, since="1.1", replaced="1.3") def __init__( self, reader=None, diff --git a/monai/transforms/io/dictionary.py b/monai/transforms/io/dictionary.py index 6d649c6a24..407ca24716 100644 --- a/monai/transforms/io/dictionary.py +++ b/monai/transforms/io/dictionary.py @@ -27,6 +27,7 @@ from monai.transforms.io.array import LoadImage, SaveImage from monai.transforms.transform import MapTransform from monai.utils import GridSamplePadMode, ensure_tuple, ensure_tuple_rep +from monai.utils.deprecate_utils import deprecated_arg_default from monai.utils.enums import PostFix __all__ = ["LoadImaged", "LoadImageD", "LoadImageDict", "SaveImaged", "SaveImageD", "SaveImageDict"] @@ -69,6 +70,7 @@ class LoadImaged(MapTransform): """ + @deprecated_arg_default("image_only", False, True, since="1.1", replaced="1.3") def __init__( self, keys: KeysCollection, diff --git a/monai/utils/deprecate_utils.py b/monai/utils/deprecate_utils.py index ef53ce444c..63a3fb9429 100644 --- a/monai/utils/deprecate_utils.py +++ b/monai/utils/deprecate_utils.py @@ -289,16 +289,16 @@ def deprecated_arg_default( def _decorator(func): argname = f"{func.__module__} {func.__qualname__}:{name}" - msg_prefix = f"Default of argument `{name}`" + msg_prefix = f" Current default value of argument `{name}={old_default}`" if is_replaced: - msg_infix = f"was replaced in version {replaced} from `{old_default}` to `{new_default}`." + msg_infix = f"was changed in version {replaced} from `{name}={old_default}` to `{name}={new_default}`." elif is_deprecated: - msg_infix = f"has been deprecated since version {since} from `{old_default}` to `{new_default}`." + msg_infix = f"has been deprecated since {since}." if replaced is not None: - msg_infix += f" It will be replaced in version {replaced}." + msg_infix += f" It will be changed to `{name}={new_default}` in version {replaced}." else: - msg_infix = f"has been deprecated from `{old_default}` to `{new_default}`." + msg_infix = f"has been deprecated from `{name}={old_default}` to `{name}={new_default}`." msg = f"{msg_prefix} {msg_infix} {msg_suffix}".strip() From 364344aabe5383e8e4aad117bf9d9ebf01b88bca Mon Sep 17 00:00:00 2001 From: Wenqi Li <831580+wyli@users.noreply.github.com> Date: Thu, 26 Jan 2023 20:44:17 +0000 Subject: [PATCH 2/3] Update monai/utils/deprecate_utils.py Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Signed-off-by: Wenqi Li <831580+wyli@users.noreply.github.com> --- monai/utils/deprecate_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/utils/deprecate_utils.py b/monai/utils/deprecate_utils.py index 63a3fb9429..ed58111504 100644 --- a/monai/utils/deprecate_utils.py +++ b/monai/utils/deprecate_utils.py @@ -294,7 +294,7 @@ def _decorator(func): if is_replaced: msg_infix = f"was changed in version {replaced} from `{name}={old_default}` to `{name}={new_default}`." elif is_deprecated: - msg_infix = f"has been deprecated since {since}." + msg_infix = f"has been deprecated since version {since}." if replaced is not None: msg_infix += f" It will be changed to `{name}={new_default}` in version {replaced}." else: From e688b6542a1ab6feb03d9ca1663466e81ef655ae Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 26 Jan 2023 22:15:55 +0000 Subject: [PATCH 3/3] remove more deprecated logs Signed-off-by: Wenqi Li --- monai/data/grid_dataset.py | 4 +--- monai/data/image_writer.py | 4 +++- monai/data/iterable_dataset.py | 4 +--- monai/transforms/spatial/array.py | 28 +++----------------------- monai/transforms/spatial/dictionary.py | 17 ---------------- monai/transforms/utility/array.py | 4 ---- monai/transforms/utility/dictionary.py | 3 --- 7 files changed, 8 insertions(+), 56 deletions(-) diff --git a/monai/data/grid_dataset.py b/monai/data/grid_dataset.py index 6c6ee293e2..fc8175f630 100644 --- a/monai/data/grid_dataset.py +++ b/monai/data/grid_dataset.py @@ -21,7 +21,7 @@ from monai.data.iterable_dataset import IterableDataset from monai.data.utils import iter_patch from monai.transforms import apply_transform -from monai.utils import NumpyPadMode, deprecated_arg, ensure_tuple, first, look_up_option +from monai.utils import NumpyPadMode, ensure_tuple, first, look_up_option __all__ = ["PatchDataset", "GridPatchDataset", "PatchIter", "PatchIterd"] @@ -175,7 +175,6 @@ class GridPatchDataset(IterableDataset): """ - @deprecated_arg(name="dataset", new_name="data", since="0.8", msg_suffix="please use `data` instead.") def __init__( self, data: Iterable | Sequence, @@ -244,7 +243,6 @@ class PatchDataset(Dataset): """ - @deprecated_arg(name="dataset", new_name="data", since="0.8", msg_suffix="please use `data` instead.") def __init__( self, data: Sequence, patch_func: Callable, samples_per_image: int = 1, transform: Callable | None = None ) -> None: diff --git a/monai/data/image_writer.py b/monai/data/image_writer.py index 20e9e08c6a..0274c44900 100644 --- a/monai/data/image_writer.py +++ b/monai/data/image_writer.py @@ -270,7 +270,9 @@ def resample_if_needed( if affine is not None: data_array.affine = convert_to_tensor(affine, track_meta=False) # type: ignore resampler = SpatialResample(mode=mode, padding_mode=padding_mode, align_corners=align_corners, dtype=dtype) - output_array = resampler(data_array[None], dst_affine=target_affine, spatial_size=output_spatial_shape) + output_array = resampler( + data_array[None], dst_affine=target_affine, spatial_size=output_spatial_shape # type: ignore + ) # convert back at the end if isinstance(output_array, MetaTensor): output_array.applied_operations = [] diff --git a/monai/data/iterable_dataset.py b/monai/data/iterable_dataset.py index d191a7c812..4c476b2f9d 100644 --- a/monai/data/iterable_dataset.py +++ b/monai/data/iterable_dataset.py @@ -20,7 +20,7 @@ from monai.data.utils import convert_tables_to_dicts from monai.transforms import apply_transform from monai.transforms.transform import Randomizable -from monai.utils import deprecated_arg, optional_import +from monai.utils import optional_import pd, _ = optional_import("pandas") @@ -200,7 +200,6 @@ class CSVIterableDataset(IterableDataset): """ - @deprecated_arg(name="filename", new_name="src", since="0.8", msg_suffix="please use `src` instead.") def __init__( self, src: str | Sequence[str] | Iterable | Sequence[Iterable], @@ -231,7 +230,6 @@ def __init__( self.iters: list[Iterable] = self.reset() super().__init__(data=None, transform=transform) # type: ignore - @deprecated_arg(name="filename", new_name="src", since="0.8", msg_suffix="please use `src` instead.") def reset(self, src: str | Sequence[str] | Iterable | Sequence[Iterable] | None = None): """ Reset the pandas `TextFileReader` iterable object to read data. For more details, please check: diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 7688bcae9d..c99b068c1f 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -189,13 +189,9 @@ def _post_process( def update_meta(self, img, dst_affine): img.affine = dst_affine - @deprecated_arg( - name="src_affine", since="0.9", msg_suffix="img should be `MetaTensor`, so affine can be extracted directly." - ) def __call__( self, img: torch.Tensor, - src_affine: NdarrayOrTensor | None = None, dst_affine: torch.Tensor | None = None, spatial_size: Sequence[int] | torch.Tensor | int | None = None, mode: str | int | None = None, @@ -354,18 +350,10 @@ def update_meta(self, img: torch.Tensor, dst_affine=None, img_dst=None): img.meta = deepcopy(img_dst.meta) img.meta[Key.FILENAME_OR_OBJ] = original_fname # keep the original name, the others are overwritten - @deprecated_arg( - name="src_meta", since="0.9", msg_suffix="img should be `MetaTensor`, so affine can be extracted directly." - ) - @deprecated_arg( - name="dst_meta", since="0.9", msg_suffix="img_dst should be `MetaTensor`, so affine can be extracted directly." - ) - def __call__( + def __call__( # type: ignore self, img: torch.Tensor, img_dst: torch.Tensor, - src_meta: dict | None = None, - dst_meta: dict | None = None, mode: str | int | None = None, padding_mode: str | None = None, align_corners: bool | None = None, @@ -373,16 +361,8 @@ def __call__( ) -> torch.Tensor: """ Args: - img: input image to be resampled to match ``dst_meta``. It currently supports channel-first arrays with + img: input image to be resampled to match ``img_dst``. It currently supports channel-first arrays with at most three spatial dimensions. - src_meta: Dictionary containing the source affine matrix in the form ``{'affine':src_affine}``. - If ``affine`` is not specified, an identity matrix is assumed. Defaults to ``None``. - See also: https://docs.monai.io/en/stable/transforms.html#spatialresample - dst_meta: Dictionary containing the target affine matrix and target spatial shape in the form - ``{'affine':src_affine, 'spatial_shape':spatial_size}``. If ``affine`` is not - specified, ``src_affine`` is assumed. If ``spatial_shape`` is not specified, spatial size is - automatically computed, containing the previous field of view. Defaults to ``None``. - See also: https://docs.monai.io/en/stable/transforms.html#spatialresample mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers). Interpolation mode to calculate output values. Defaults to ``"bilinear"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html @@ -402,8 +382,6 @@ def __call__( ``np.float64`` (for best precision). If ``None``, use the data type of input data. To be compatible with other modules, the output data type is always `float32`. Raises: - RuntimeError: When ``src_meta`` is missing. - RuntimeError: When ``dst_meta`` is missing. ValueError: When the affine matrix of the source image is not invertible. Returns: Resampled input tensor or MetaTensor. @@ -608,7 +586,7 @@ def __call__( data_array = self.sp_resample( data_array, dst_affine=torch.as_tensor(new_affine), - spatial_size=actual_shape, + spatial_size=actual_shape, # type: ignore mode=mode, padding_mode=padding_mode, align_corners=align_corners, diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index 554fddbe2d..1c3920138e 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -65,7 +65,6 @@ ensure_tuple_rep, fall_back_tuple, ) -from monai.utils.deprecate_utils import deprecated_arg from monai.utils.enums import PytorchPadMode, TraceKeys from monai.utils.module import optional_import @@ -159,9 +158,6 @@ class SpatialResampled(MapTransform, InvertibleTransform): backend = SpatialResample.backend - @deprecated_arg(name="meta_keys", since="0.9") - @deprecated_arg(name="meta_key_postfix", since="0.9") - @deprecated_arg(name="meta_src_keys", since="0.9") def __init__( self, keys: KeysCollection, @@ -169,9 +165,6 @@ def __init__( padding_mode: SequenceStr = GridSamplePadMode.BORDER, align_corners: Sequence[bool] | bool = False, dtype: Sequence[DtypeLike] | DtypeLike = np.float64, - meta_keys: KeysCollection | None = None, - meta_key_postfix: str = "meta_dict", - meta_src_keys: KeysCollection | None = "src_affine", dst_keys: KeysCollection | None = "dst_affine", allow_missing_keys: bool = False, ) -> None: @@ -238,12 +231,10 @@ class ResampleToMatchd(MapTransform, InvertibleTransform): backend = ResampleToMatch.backend - @deprecated_arg(name="template_key", since="0.9") def __init__( self, keys: KeysCollection, key_dst: str, - template_key: str | None = None, mode: SequenceStr = GridSampleMode.BILINEAR, padding_mode: SequenceStr = GridSamplePadMode.BORDER, align_corners: Sequence[bool] | bool = False, @@ -323,8 +314,6 @@ class Spacingd(MapTransform, InvertibleTransform): backend = Spacing.backend - @deprecated_arg(name="meta_keys", since="0.9") - @deprecated_arg(name="meta_key_postfix", since="0.9") def __init__( self, keys: KeysCollection, @@ -336,8 +325,6 @@ def __init__( dtype: Sequence[DtypeLike] | DtypeLike = np.float64, scale_extent: bool = False, recompute_affine: bool = False, - meta_keys: KeysCollection | None = None, - meta_key_postfix: str = "meta_dict", min_pixdim: Sequence[float] | float | None = None, max_pixdim: Sequence[float] | float | None = None, allow_missing_keys: bool = False, @@ -444,16 +431,12 @@ class Orientationd(MapTransform, InvertibleTransform): backend = Orientation.backend - @deprecated_arg(name="meta_keys", since="0.9") - @deprecated_arg(name="meta_key_postfix", since="0.9") def __init__( self, keys: KeysCollection, axcodes: str | None = None, as_closest_canonical: bool = False, labels: Sequence[tuple[str, str]] | None = (("L", "R"), ("P", "A"), ("I", "S")), - meta_keys: KeysCollection | None = None, - meta_key_postfix: str = "meta_dict", allow_missing_keys: bool = False, ) -> None: """ diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index fd667d0c3f..9175c37740 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -59,7 +59,6 @@ convert_to_numpy, convert_to_tensor, deprecated, - deprecated_arg, ensure_tuple, look_up_option, min_version, @@ -1421,9 +1420,6 @@ class AddCoordinateChannels(Transform): backend = [TransformBackends.NUMPY] - @deprecated_arg( - name="spatial_channels", new_name="spatial_dims", since="0.8", msg_suffix="please use `spatial_dims` instead." - ) def __init__(self, spatial_dims: Sequence[int]) -> None: self.spatial_dims = spatial_dims diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py index 0ec652bb03..2e0bde21b4 100644 --- a/monai/transforms/utility/dictionary.py +++ b/monai/transforms/utility/dictionary.py @@ -1728,9 +1728,6 @@ class AddCoordinateChannelsd(MapTransform): backend = AddCoordinateChannels.backend - @deprecated_arg( - name="spatial_channels", new_name="spatial_dims", since="0.8", msg_suffix="please use `spatial_dims` instead." - ) def __init__(self, keys: KeysCollection, spatial_dims: Sequence[int], allow_missing_keys: bool = False) -> None: super().__init__(keys, allow_missing_keys) self.add_coordinate_channels = AddCoordinateChannels(spatial_dims=spatial_dims)