From 0031f2da7fb29149ee8e0f6bb3d03030c91a5987 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:30:24 +0800 Subject: [PATCH 1/3] remove deprecated Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/bundle/workflows.py | 7 +++---- monai/handlers/__init__.py | 2 +- monai/handlers/ignite_metric.py | 24 +----------------------- monai/losses/dice.py | 17 +---------------- monai/networks/blocks/patchembedding.py | 8 +------- monai/networks/nets/unetr.py | 9 +-------- monai/networks/nets/vit.py | 8 -------- monai/networks/nets/vitautoenc.py | 9 +-------- monai/utils/enums.py | 12 ------------ 9 files changed, 9 insertions(+), 87 deletions(-) diff --git a/monai/bundle/workflows.py b/monai/bundle/workflows.py index 11c9bf0562..d728d7d930 100644 --- a/monai/bundle/workflows.py +++ b/monai/bundle/workflows.py @@ -26,7 +26,7 @@ from monai.bundle.properties import InferProperties, MetaProperties, TrainProperties from monai.bundle.utils import DEFAULT_EXP_MGMT_SETTINGS, EXPR_KEY, ID_REF_KEY, ID_SEP_KEY from monai.config import PathLike -from monai.utils import BundleProperty, BundlePropertyConfig, deprecated_arg, deprecated_arg_default, ensure_tuple +from monai.utils import BundleProperty, BundlePropertyConfig, deprecated_arg, ensure_tuple __all__ = ["BundleWorkflow", "ConfigWorkflow"] @@ -43,7 +43,7 @@ class BundleWorkflow(ABC): workflow_type: specifies the workflow type: "train" or "training" for a training workflow, or "infer", "inference", "eval", "evaluation" for a inference workflow, other unsupported string will raise a ValueError. - default to `None` for common workflow. + default to `train` for train workflow. workflow: specifies the workflow type: "train" or "training" for a training workflow, or "infer", "inference", "eval", "evaluation" for a inference workflow, other unsupported string will raise a ValueError. @@ -274,7 +274,6 @@ class ConfigWorkflow(BundleWorkflow): new_name="workflow_type", msg_suffix="please use `workflow_type` instead.", ) - @deprecated_arg_default("workflow_type", None, "train", since="1.2", replaced="1.4") def __init__( self, config_file: str | Sequence[str], @@ -284,7 +283,7 @@ def __init__( run_id: str = "run", final_id: str = "finalize", tracking: str | dict | None = None, - workflow_type: str | None = None, + workflow_type: str | None = "train", workflow: str | None = None, properties_path: PathLike | None = None, **override: Any, diff --git a/monai/handlers/__init__.py b/monai/handlers/__init__.py index fa6e158be8..c1fa448f25 100644 --- a/monai/handlers/__init__.py +++ b/monai/handlers/__init__.py @@ -20,7 +20,7 @@ from .earlystop_handler import EarlyStopHandler from .garbage_collector import GarbageCollector from .hausdorff_distance import HausdorffDistance -from .ignite_metric import IgniteMetric, IgniteMetricHandler +from .ignite_metric import IgniteMetricHandler from .logfile_handler import LogfileHandler from .lr_schedule_handler import LrScheduleHandler from .mean_dice import MeanDice diff --git a/monai/handlers/ignite_metric.py b/monai/handlers/ignite_metric.py index 021154d705..64ded4d5ea 100644 --- a/monai/handlers/ignite_metric.py +++ b/monai/handlers/ignite_metric.py @@ -20,7 +20,7 @@ from monai.config import IgniteInfo from monai.metrics import CumulativeIterationMetric, LossMetric -from monai.utils import MetricReduction, deprecated, min_version, optional_import +from monai.utils import MetricReduction, min_version, optional_import idist, _ = optional_import("ignite", IgniteInfo.OPT_IMPORT_VERSION, min_version, "distributed") @@ -153,25 +153,3 @@ def attach(self, engine: Engine, name: str) -> None: # type: ignore[override] self._name = name if self.save_details and not hasattr(engine.state, "metric_details"): engine.state.metric_details = {} # type: ignore - - -@deprecated(since="1.2", removed="1.4", msg_suffix="Use IgniteMetricHandler instead of IgniteMetric.") -class IgniteMetric(IgniteMetricHandler): - - def __init__( - self, - metric_fn: CumulativeIterationMetric | None = None, - loss_fn: _Loss | None = None, - output_transform: Callable = lambda x: x, - save_details: bool = True, - reduction: MetricReduction | str = MetricReduction.MEAN, - get_not_nans: bool = False, - ) -> None: - super().__init__( - metric_fn=metric_fn, - loss_fn=loss_fn, - output_transform=output_transform, - save_details=save_details, - reduction=reduction, - get_not_nans=get_not_nans, - ) diff --git a/monai/losses/dice.py b/monai/losses/dice.py index 44cde41e5d..3f02fae6b8 100644 --- a/monai/losses/dice.py +++ b/monai/losses/dice.py @@ -24,7 +24,7 @@ from monai.losses.focal_loss import FocalLoss from monai.losses.spatial_mask import MaskedLoss from monai.networks import one_hot -from monai.utils import DiceCEReduction, LossReduction, Weight, deprecated_arg, look_up_option, pytorch_after +from monai.utils import DiceCEReduction, LossReduction, Weight, look_up_option, pytorch_after class DiceLoss(_Loss): @@ -646,9 +646,6 @@ class DiceCELoss(_Loss): """ - @deprecated_arg( - "ce_weight", since="1.2", removed="1.4", new_name="weight", msg_suffix="please use `weight` instead." - ) def __init__( self, include_background: bool = True, @@ -662,7 +659,6 @@ def __init__( smooth_nr: float = 1e-5, smooth_dr: float = 1e-5, batch: bool = False, - ce_weight: torch.Tensor | None = None, weight: torch.Tensor | None = None, lambda_dice: float = 1.0, lambda_ce: float = 1.0, @@ -712,7 +708,6 @@ def __init__( """ super().__init__() reduction = look_up_option(reduction, DiceCEReduction).value - weight = ce_weight if ce_weight is not None else weight dice_weight: torch.Tensor | None if weight is not None and not include_background: dice_weight = weight[1:] @@ -825,9 +820,6 @@ class DiceFocalLoss(_Loss): """ - @deprecated_arg( - "focal_weight", since="1.2", removed="1.4", new_name="weight", msg_suffix="please use `weight` instead." - ) def __init__( self, include_background: bool = True, @@ -842,7 +834,6 @@ def __init__( smooth_dr: float = 1e-5, batch: bool = False, gamma: float = 2.0, - focal_weight: Sequence[float] | float | int | torch.Tensor | None = None, weight: Sequence[float] | float | int | torch.Tensor | None = None, lambda_dice: float = 1.0, lambda_focal: float = 1.0, @@ -885,7 +876,6 @@ def __init__( [0, 1]. Defaults to None. """ super().__init__() - weight = focal_weight if focal_weight is not None else weight self.dice = DiceLoss( include_background=include_background, to_onehot_y=False, @@ -994,9 +984,6 @@ class GeneralizedDiceFocalLoss(_Loss): ValueError: if either `lambda_gdl` or `lambda_focal` is less than 0. """ - @deprecated_arg( - "focal_weight", since="1.2", removed="1.4", new_name="weight", msg_suffix="please use `weight` instead." - ) def __init__( self, include_background: bool = True, @@ -1010,7 +997,6 @@ def __init__( smooth_dr: float = 1e-5, batch: bool = False, gamma: float = 2.0, - focal_weight: Sequence[float] | float | int | torch.Tensor | None = None, weight: Sequence[float] | float | int | torch.Tensor | None = None, lambda_gdl: float = 1.0, lambda_focal: float = 1.0, @@ -1028,7 +1014,6 @@ def __init__( smooth_dr=smooth_dr, batch=batch, ) - weight = focal_weight if focal_weight is not None else weight self.focal = FocalLoss( include_background=include_background, to_onehot_y=to_onehot_y, diff --git a/monai/networks/blocks/patchembedding.py b/monai/networks/blocks/patchembedding.py index 91bd73ebbb..fca566591a 100644 --- a/monai/networks/blocks/patchembedding.py +++ b/monai/networks/blocks/patchembedding.py @@ -21,7 +21,7 @@ from monai.networks.blocks.pos_embed_utils import build_sincos_position_embedding from monai.networks.layers import Conv, trunc_normal_ -from monai.utils import deprecated_arg, ensure_tuple_rep, optional_import +from monai.utils import ensure_tuple_rep, optional_import from monai.utils.module import look_up_option Rearrange, _ = optional_import("einops.layers.torch", name="Rearrange") @@ -42,9 +42,6 @@ class PatchEmbeddingBlock(nn.Module): """ - @deprecated_arg( - name="pos_embed", since="1.2", removed="1.4", new_name="proj_type", msg_suffix="please use `proj_type` instead." - ) def __init__( self, in_channels: int, @@ -52,7 +49,6 @@ def __init__( patch_size: Sequence[int] | int, hidden_size: int, num_heads: int, - pos_embed: str = "conv", proj_type: str = "conv", pos_embed_type: str = "learnable", dropout_rate: float = 0.0, @@ -69,8 +65,6 @@ def __init__( pos_embed_type: position embedding layer type. dropout_rate: fraction of the input units to drop. spatial_dims: number of spatial dimensions. - .. deprecated:: 1.4 - ``pos_embed`` is deprecated in favor of ``proj_type``. """ super().__init__() diff --git a/monai/networks/nets/unetr.py b/monai/networks/nets/unetr.py index a88e5a92fd..79ea0e23f7 100644 --- a/monai/networks/nets/unetr.py +++ b/monai/networks/nets/unetr.py @@ -18,7 +18,7 @@ from monai.networks.blocks.dynunet_block import UnetOutBlock from monai.networks.blocks.unetr_block import UnetrBasicBlock, UnetrPrUpBlock, UnetrUpBlock from monai.networks.nets.vit import ViT -from monai.utils import deprecated_arg, ensure_tuple_rep +from monai.utils import ensure_tuple_rep class UNETR(nn.Module): @@ -27,9 +27,6 @@ class UNETR(nn.Module): UNETR: Transformers for 3D Medical Image Segmentation " """ - @deprecated_arg( - name="pos_embed", since="1.2", removed="1.4", new_name="proj_type", msg_suffix="please use `proj_type` instead." - ) def __init__( self, in_channels: int, @@ -39,7 +36,6 @@ def __init__( hidden_size: int = 768, mlp_dim: int = 3072, num_heads: int = 12, - pos_embed: str = "conv", proj_type: str = "conv", norm_name: tuple | str = "instance", conv_block: bool = True, @@ -67,9 +63,6 @@ def __init__( qkv_bias: apply the bias term for the qkv linear layer in self attention block. Defaults to False. save_attn: to make accessible the attention in self attention block. Defaults to False. - .. deprecated:: 1.4 - ``pos_embed`` is deprecated in favor of ``proj_type``. - Examples:: # for single channel input 4-channel output with image size of (96,96,96), feature size of 32 and batch norm diff --git a/monai/networks/nets/vit.py b/monai/networks/nets/vit.py index 4eada6aa76..07c5147cb2 100644 --- a/monai/networks/nets/vit.py +++ b/monai/networks/nets/vit.py @@ -18,7 +18,6 @@ from monai.networks.blocks.patchembedding import PatchEmbeddingBlock from monai.networks.blocks.transformerblock import TransformerBlock -from monai.utils import deprecated_arg __all__ = ["ViT"] @@ -31,9 +30,6 @@ class ViT(nn.Module): ViT supports Torchscript but only works for Pytorch after 1.8. """ - @deprecated_arg( - name="pos_embed", since="1.2", removed="1.4", new_name="proj_type", msg_suffix="please use `proj_type` instead." - ) def __init__( self, in_channels: int, @@ -43,7 +39,6 @@ def __init__( mlp_dim: int = 3072, num_layers: int = 12, num_heads: int = 12, - pos_embed: str = "conv", proj_type: str = "conv", pos_embed_type: str = "learnable", classification: bool = False, @@ -75,9 +70,6 @@ def __init__( qkv_bias (bool, optional): apply bias to the qkv linear layer in self attention block. Defaults to False. save_attn (bool, optional): to make accessible the attention in self attention block. Defaults to False. - .. deprecated:: 1.4 - ``pos_embed`` is deprecated in favor of ``proj_type``. - Examples:: # for single channel input with image size of (96,96,96), conv position embedding and segmentation backbone diff --git a/monai/networks/nets/vitautoenc.py b/monai/networks/nets/vitautoenc.py index d69f5df4be..3c20f9a784 100644 --- a/monai/networks/nets/vitautoenc.py +++ b/monai/networks/nets/vitautoenc.py @@ -20,7 +20,7 @@ from monai.networks.blocks.patchembedding import PatchEmbeddingBlock from monai.networks.blocks.transformerblock import TransformerBlock from monai.networks.layers import Conv -from monai.utils import deprecated_arg, ensure_tuple_rep, is_sqrt +from monai.utils import ensure_tuple_rep, is_sqrt __all__ = ["ViTAutoEnc"] @@ -33,9 +33,6 @@ class ViTAutoEnc(nn.Module): Modified to also give same dimension outputs as the input size of the image """ - @deprecated_arg( - name="pos_embed", since="1.2", removed="1.4", new_name="proj_type", msg_suffix="please use `proj_type` instead." - ) def __init__( self, in_channels: int, @@ -47,7 +44,6 @@ def __init__( mlp_dim: int = 3072, num_layers: int = 12, num_heads: int = 12, - pos_embed: str = "conv", proj_type: str = "conv", dropout_rate: float = 0.0, spatial_dims: int = 3, @@ -71,9 +67,6 @@ def __init__( qkv_bias: apply bias to the qkv linear layer in self attention block. Defaults to False. save_attn: to make accessible the attention in self attention block. Defaults to False. Defaults to False. - .. deprecated:: 1.4 - ``pos_embed`` is deprecated in favor of ``proj_type``. - Examples:: # for single channel input with image size of (96,96,96), conv position embedding and segmentation backbone diff --git a/monai/utils/enums.py b/monai/utils/enums.py index eba1be18ed..441dc02891 100644 --- a/monai/utils/enums.py +++ b/monai/utils/enums.py @@ -56,7 +56,6 @@ "DataStatsKeys", "ImageStatsKeys", "LabelStatsKeys", - "AlgoEnsembleKeys", "HoVerNetMode", "HoVerNetBranch", "LazyAttr", @@ -615,17 +614,6 @@ class LabelStatsKeys(StrEnum): LABEL_NCOMP = "ncomponents" -@deprecated(since="1.2", removed="1.4", msg_suffix="please use `AlgoKeys` instead.") -class AlgoEnsembleKeys(StrEnum): - """ - Default keys for Mixed Ensemble - """ - - ID = "identifier" - ALGO = "infer_algo" - SCORE = "best_metric" - - class HoVerNetMode(StrEnum): """ Modes for HoVerNet model: From 450c217e77ca884c467d583ccfa90faa57fd4bef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 10:33:31 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/utils/enums.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/utils/enums.py b/monai/utils/enums.py index 441dc02891..7838a2e741 100644 --- a/monai/utils/enums.py +++ b/monai/utils/enums.py @@ -16,7 +16,6 @@ from typing import TYPE_CHECKING from monai.config import IgniteInfo -from monai.utils import deprecated from monai.utils.module import min_version, optional_import __all__ = [ From 7bb3f518b0087ae3c3f475497c2fd3e6b42ff167 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:43:21 +0800 Subject: [PATCH 3/3] fix mypy Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/utils/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/utils/misc.py b/monai/utils/misc.py index 40370ca2c6..6386aae713 100644 --- a/monai/utils/misc.py +++ b/monai/utils/misc.py @@ -887,7 +887,7 @@ def run_cmd(cmd_list: list[str], **kwargs: Any) -> subprocess.CompletedProcess: if kwargs.pop("run_cmd_verbose", False): import monai - monai.apps.utils.get_logger("run_cmd").info(f"{cmd_list}") + monai.apps.utils.get_logger("run_cmd").info(f"{cmd_list}") # type: ignore[attr-defined] try: return subprocess.run(cmd_list, **kwargs) except subprocess.CalledProcessError as e: