From 05e4d375aa96073c916734a023b17b6bf1626261 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:31:03 +0800 Subject: [PATCH 01/16] add packaging Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/data/image_reader.py | 2 +- monai/handlers/mlflow_handler.py | 2 ++ setup.cfg | 1 + tests/min_tests.py | 1 + tests/test_gdsdataset.py | 7 ++++--- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index aab1e03898..821cab0f43 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -34,7 +34,7 @@ ) from monai.utils import MetaKeys, SpaceKeys, TraceKeys, ensure_tuple, optional_import, require_pkg -# workaround for https://github.com/Project-MONAI/MONAI/issues/8061 +# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 warnings.filterwarnings("ignore", category=DeprecationWarning, module="nptyping") if TYPE_CHECKING: diff --git a/monai/handlers/mlflow_handler.py b/monai/handlers/mlflow_handler.py index 6d19579d9e..f3865e45ae 100644 --- a/monai/handlers/mlflow_handler.py +++ b/monai/handlers/mlflow_handler.py @@ -25,6 +25,8 @@ from monai.config import IgniteInfo from monai.utils import CommonKeys, ensure_tuple, min_version, optional_import +# workaround for https://github.com/Project-MONAI/MONAI/issues/8061 +warnings.filterwarnings("ignore", category=DeprecationWarning, module="mlflow") Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events") mlflow, _ = optional_import("mlflow", descriptor="Please install mlflow before using MLFlowHandler.") mlflow.entities, _ = optional_import( diff --git a/setup.cfg b/setup.cfg index c97118d43a..694dc969d9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,6 +40,7 @@ python_requires = >= 3.9 setup_requires = torch ninja + packaging install_requires = torch>=1.9 numpy>=1.24,<2.0 diff --git a/tests/min_tests.py b/tests/min_tests.py index 632355b5c6..642273acd7 100644 --- a/tests/min_tests.py +++ b/tests/min_tests.py @@ -212,6 +212,7 @@ def run_testsuit(): "test_ultrasound_confidence_map_transform", "test_vista3d_utils", "test_vista3d_transforms", + "test_matshow3d" ] assert sorted(exclude_cases) == sorted(set(exclude_cases)), f"Duplicated items in {exclude_cases}" diff --git a/tests/test_gdsdataset.py b/tests/test_gdsdataset.py index f0a419dcf5..5d2e2aa013 100644 --- a/tests/test_gdsdataset.py +++ b/tests/test_gdsdataset.py @@ -23,7 +23,7 @@ from monai.data import GDSDataset, json_hashing from monai.transforms import Compose, Flip, Identity, LoadImaged, SimulateDelayd, Transform from monai.utils import optional_import -from tests.utils import TEST_NDARRAYS, assert_allclose +from tests.utils import TEST_NDARRAYS, assert_allclose, skip_if_no_cuda _, has_cp = optional_import("cupy") nib, has_nib = optional_import("nibabel") @@ -70,9 +70,9 @@ def __call__(self, data): return data +@skip_if_no_cuda @unittest.skipUnless(has_cp, "Requires CuPy library.") -@unittest.skipUnless(has_nib, "Requires nibabel package.") -@unittest.skipUnless(has_kvikio_numpy, "Requires scikit-image library.") +@unittest.skipUnless(has_cp and has_kvikio_numpy, "Requires CuPy and kvikio library.") class TestDataset(unittest.TestCase): def test_cache(self): @@ -131,6 +131,7 @@ def test_dtype(self): self.assertEqual(ds[0].dtype, DTYPES[_dtype]) self.assertEqual(ds1[0].dtype, DTYPES[_dtype]) + @unittest.skipUnless(has_nib, "Requires nibabel package.") @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) def test_shape(self, transform, expected_shape): test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4)) From a05c5c04b2062b2be2d40ec7de48176c484ed8a4 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:37:18 +0800 Subject: [PATCH 02/16] add packaging in pyproject.toml Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 53ca608d20..c2ab92a43d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ requires = [ "setuptools", "torch>=1.9", "ninja", + "packaging" ] [tool.black] From 73f176747d51a320789282cda24e3c0410aa2f87 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 21:52:32 +0800 Subject: [PATCH 03/16] add warning filter Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 12 +++++++++++- monai/networks/utils.py | 3 +++ monai/utils/module.py | 3 +++ tests/min_tests.py | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index cb0ccd36f8..0897bf0f3a 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -13,9 +13,19 @@ import os import sys - +import warnings from ._version import get_versions + +def custom_warning_handler(message, category, filename, lineno, file=None, line=None): + message1 = "`TorchScript` support for functional optimizers is deprecated and will be removed in a future PyTorch release. Consider using the `torch.compile` optimizer instead." + message2 = "`torch.library.impl_abstract` was renamed to `torch.library.register_fake`. Please use that instead; we will remove `torch.library.impl_abstract` in a future version of PyTorch." + if message1 or message2 in message: + return + warnings.showwarning(message, category, filename, lineno, file, line) + + +warnings.showwarning = custom_warning_handler PY_REQUIRED_MAJOR = 3 PY_REQUIRED_MINOR = 9 diff --git a/monai/networks/utils.py b/monai/networks/utils.py index d0150b4e5b..fd32fe526e 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -37,6 +37,9 @@ onnxreference, _ = optional_import("onnx.reference") onnxruntime, _ = optional_import("onnxruntime") polygraphy, polygraphy_imported = optional_import("polygraphy") + +# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 +warnings.filterwarnings("ignore", category=DeprecationWarning, module="torch_tensorrt") torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0") __all__ = [ diff --git a/monai/utils/module.py b/monai/utils/module.py index 78087aef84..50eb25789f 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -399,6 +399,9 @@ def optional_import( else: actual_cmd = f"import {module}" try: + # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 + warnings.filterwarnings("ignore", module="pydantic") + warnings.filterwarnings("ignore", category=ResourceWarning) pkg = __import__(module) # top level module the_module = import_module(module) if not allow_namespace_pkg: diff --git a/tests/min_tests.py b/tests/min_tests.py index 642273acd7..f39d3f9843 100644 --- a/tests/min_tests.py +++ b/tests/min_tests.py @@ -212,7 +212,7 @@ def run_testsuit(): "test_ultrasound_confidence_map_transform", "test_vista3d_utils", "test_vista3d_transforms", - "test_matshow3d" + "test_matshow3d", ] assert sorted(exclude_cases) == sorted(set(exclude_cases)), f"Duplicated items in {exclude_cases}" From 6e36ebeefa255b4330185a137e470331c9bd13a8 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 21:56:49 +0800 Subject: [PATCH 04/16] workaround for #8060 Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/handlers/mlflow_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/handlers/mlflow_handler.py b/monai/handlers/mlflow_handler.py index f3865e45ae..eb93c5b49b 100644 --- a/monai/handlers/mlflow_handler.py +++ b/monai/handlers/mlflow_handler.py @@ -25,7 +25,7 @@ from monai.config import IgniteInfo from monai.utils import CommonKeys, ensure_tuple, min_version, optional_import -# workaround for https://github.com/Project-MONAI/MONAI/issues/8061 +# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 warnings.filterwarnings("ignore", category=DeprecationWarning, module="mlflow") Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events") mlflow, _ = optional_import("mlflow", descriptor="Please install mlflow before using MLFlowHandler.") From f719f4cec5de3c73a456a77e015aced8ea2be4be Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:25:07 +0800 Subject: [PATCH 05/16] minor fix Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index 0897bf0f3a..2be56dfb6a 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -18,9 +18,10 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line=None): - message1 = "`TorchScript` support for functional optimizers is deprecated and will be removed in a future PyTorch release. Consider using the `torch.compile` optimizer instead." - message2 = "`torch.library.impl_abstract` was renamed to `torch.library.register_fake`. Please use that instead; we will remove `torch.library.impl_abstract` in a future version of PyTorch." - if message1 or message2 in message: + message1 = "`TorchScript` support for functional optimizers is deprecated and will be removed" + message2 = "`torch.library.impl_abstract` was renamed to `torch.library.register_fake`." + + if message1 in str(message) or message2 in str(message): return warnings.showwarning(message, category, filename, lineno, file, line) From 5cca0251c399050a26b1959f1f7f4b09a640c577 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:37:53 +0800 Subject: [PATCH 06/16] use filename instead of message Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index 2be56dfb6a..be0be71c79 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -18,10 +18,9 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line=None): - message1 = "`TorchScript` support for functional optimizers is deprecated and will be removed" - message2 = "`torch.library.impl_abstract` was renamed to `torch.library.register_fake`." - - if message1 in str(message) or message2 in str(message): + filename1 = "ignite/handlers/checkpoint.py" + filename2 = "modelopt/torch/quantization/tensor_quant.py" + if filename1 in filename or filename2 in filename: return warnings.showwarning(message, category, filename, lineno, file, line) From 8ef5c0617d02605a2cc29d9c1122b7497a1bbc5f Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 23:04:27 +0800 Subject: [PATCH 07/16] Update monai/__init__.py Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monai/__init__.py b/monai/__init__.py index be0be71c79..c2b28f4d78 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -17,12 +17,13 @@ from ._version import get_versions +old_showwarning = warnings.showwarning def custom_warning_handler(message, category, filename, lineno, file=None, line=None): filename1 = "ignite/handlers/checkpoint.py" filename2 = "modelopt/torch/quantization/tensor_quant.py" if filename1 in filename or filename2 in filename: return - warnings.showwarning(message, category, filename, lineno, file, line) + old_showwarning(message, category, filename, lineno, file, line) warnings.showwarning = custom_warning_handler From 998af8fc327c500725c5ea55324ed697561135b5 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 5 Sep 2024 23:23:05 +0800 Subject: [PATCH 08/16] fix format Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/monai/__init__.py b/monai/__init__.py index c2b28f4d78..efab9c52e6 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -18,6 +18,8 @@ old_showwarning = warnings.showwarning + + def custom_warning_handler(message, category, filename, lineno, file=None, line=None): filename1 = "ignite/handlers/checkpoint.py" filename2 = "modelopt/torch/quantization/tensor_quant.py" From 5577b85352e9e550d7a4860a669828015194e6bf Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:49:53 +0800 Subject: [PATCH 09/16] add rest warning Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 12 +++++++++--- monai/data/image_reader.py | 3 --- monai/networks/utils.py | 5 ++--- monai/utils/module.py | 9 ++++++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index efab9c52e6..536c9b0047 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -21,13 +21,19 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line=None): - filename1 = "ignite/handlers/checkpoint.py" - filename2 = "modelopt/torch/quantization/tensor_quant.py" - if filename1 in filename or filename2 in filename: + ignore_files = [ + "ignite/handlers/checkpoint", + "modelopt/torch/quantization/tensor_quant", + "nptyping/typing_", + "pydantic/_internal/_config", + "tempfile", + ] + if any(ignore in filename for ignore in ignore_files): return old_showwarning(message, category, filename, lineno, file, line) +# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 warnings.showwarning = custom_warning_handler PY_REQUIRED_MAJOR = 3 PY_REQUIRED_MINOR = 9 diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 821cab0f43..f5e199e2a3 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -34,9 +34,6 @@ ) from monai.utils import MetaKeys, SpaceKeys, TraceKeys, ensure_tuple, optional_import, require_pkg -# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 -warnings.filterwarnings("ignore", category=DeprecationWarning, module="nptyping") - if TYPE_CHECKING: import itk import nibabel as nib diff --git a/monai/networks/utils.py b/monai/networks/utils.py index fd32fe526e..a7beba3ace 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -38,9 +38,8 @@ onnxruntime, _ = optional_import("onnxruntime") polygraphy, polygraphy_imported = optional_import("polygraphy") -# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 -warnings.filterwarnings("ignore", category=DeprecationWarning, module="torch_tensorrt") -torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0") +torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0", reset_warning=True) + __all__ = [ "one_hot", diff --git a/monai/utils/module.py b/monai/utils/module.py index 50eb25789f..13a18cb91f 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -345,6 +345,7 @@ def optional_import( version_args: Any = None, allow_namespace_pkg: bool = False, as_type: str = "default", + reset_warning: bool = False, ) -> tuple[Any, bool]: """ Imports an optional module specified by `module` string. @@ -363,6 +364,7 @@ def optional_import( a base class, or a decorator, the exceptions should raise accordingly. The current supported values are "default" (call once to raise), "decorator" (call the constructor and the second call to raise), and anything else will return a lazy class that can be used as a base class (call the constructor to raise). + reset_warning: whether to reset the warnings.showwarning to the original function during the import. Returns: The imported module and a boolean flag indicating whether the import is successful. @@ -399,10 +401,11 @@ def optional_import( else: actual_cmd = f"import {module}" try: - # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 - warnings.filterwarnings("ignore", module="pydantic") - warnings.filterwarnings("ignore", category=ResourceWarning) + if reset_warning: + old_showwarning = warnings.showwarning pkg = __import__(module) # top level module + if reset_warning: + warnings.showwarning = old_showwarning the_module = import_module(module) if not allow_namespace_pkg: is_namespace = getattr(the_module, "__file__", None) is None and hasattr(the_module, "__path__") From ec5e743a5448c5d97754d55f25eba65f99dfafa5 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 14:03:55 +0800 Subject: [PATCH 10/16] add reset_warning Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 2 ++ monai/data/image_reader.py | 2 +- monai/data/image_writer.py | 2 +- monai/data/itk_torch_bridge.py | 2 +- monai/handlers/mlflow_handler.py | 2 -- monai/utils/module.py | 5 ++++- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index 536c9b0047..937bf82bc5 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -27,6 +27,8 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line= "nptyping/typing_", "pydantic/_internal/_config", "tempfile", + "mlflow/gateway", + "mlflow/utils", ] if any(ignore in filename for ignore in ignore_files): return diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index f5e199e2a3..555738ecd9 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -44,7 +44,7 @@ has_nrrd = has_itk = has_nib = has_pil = has_pydicom = True else: - itk, has_itk = optional_import("itk", allow_namespace_pkg=True) + itk, has_itk = optional_import("itk", allow_namespace_pkg=True, reset_warning=True) nib, has_nib = optional_import("nibabel") Nifti1Image, _ = optional_import("nibabel.nifti1", name="Nifti1Image") PILImage, has_pil = optional_import("PIL.Image") diff --git a/monai/data/image_writer.py b/monai/data/image_writer.py index b9e8b9e68e..2cf094264f 100644 --- a/monai/data/image_writer.py +++ b/monai/data/image_writer.py @@ -46,7 +46,7 @@ import nibabel as nib from PIL import Image as PILImage else: - itk, _ = optional_import("itk", allow_namespace_pkg=True) + itk, _ = optional_import("itk", allow_namespace_pkg=True, reset_warning=True) nib, _ = optional_import("nibabel") PILImage, _ = optional_import("PIL.Image") diff --git a/monai/data/itk_torch_bridge.py b/monai/data/itk_torch_bridge.py index 4671cd112b..47e52a9c68 100644 --- a/monai/data/itk_torch_bridge.py +++ b/monai/data/itk_torch_bridge.py @@ -28,7 +28,7 @@ has_itk = True else: - itk, has_itk = optional_import("itk") + itk, has_itk = optional_import("itk", reset_warning=True) __all__ = [ "itk_image_to_metatensor", diff --git a/monai/handlers/mlflow_handler.py b/monai/handlers/mlflow_handler.py index eb93c5b49b..6d19579d9e 100644 --- a/monai/handlers/mlflow_handler.py +++ b/monai/handlers/mlflow_handler.py @@ -25,8 +25,6 @@ from monai.config import IgniteInfo from monai.utils import CommonKeys, ensure_tuple, min_version, optional_import -# workaround for https://github.com/Project-MONAI/MONAI/issues/8060 -warnings.filterwarnings("ignore", category=DeprecationWarning, module="mlflow") Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events") mlflow, _ = optional_import("mlflow", descriptor="Please install mlflow before using MLFlowHandler.") mlflow.entities, _ = optional_import( diff --git a/monai/utils/module.py b/monai/utils/module.py index 13a18cb91f..808143f2db 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -364,7 +364,8 @@ def optional_import( a base class, or a decorator, the exceptions should raise accordingly. The current supported values are "default" (call once to raise), "decorator" (call the constructor and the second call to raise), and anything else will return a lazy class that can be used as a base class (call the constructor to raise). - reset_warning: whether to reset the warnings.showwarning to the original function during the import. + reset_warning: whether to reset the warnings.showwarning and warnings.formatwarning to + the original function during the import. Returns: The imported module and a boolean flag indicating whether the import is successful. @@ -403,9 +404,11 @@ def optional_import( try: if reset_warning: old_showwarning = warnings.showwarning + old_formatwarning = warnings.formatwarning pkg = __import__(module) # top level module if reset_warning: warnings.showwarning = old_showwarning + warnings.formatwarning = old_formatwarning the_module = import_module(module) if not allow_namespace_pkg: is_namespace = getattr(the_module, "__file__", None) is None and hasattr(the_module, "__path__") From 449b88c6a4a83b0cd8e2f10cf426d1a575f9adce Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 14:10:04 +0800 Subject: [PATCH 11/16] minor fix Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/networks/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/networks/utils.py b/monai/networks/utils.py index a7beba3ace..ce53ff9755 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -37,7 +37,6 @@ onnxreference, _ = optional_import("onnx.reference") onnxruntime, _ = optional_import("onnxruntime") polygraphy, polygraphy_imported = optional_import("polygraphy") - torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0", reset_warning=True) From 0faf233a63fe4929a2b2ecbe65e6be4bc4df1858 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:36:59 +0800 Subject: [PATCH 12/16] add log warning filter Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 34 +++++++++++++++++++++++++++++----- monai/data/image_reader.py | 2 +- monai/data/image_writer.py | 2 +- monai/data/itk_torch_bridge.py | 2 +- monai/networks/utils.py | 2 +- monai/utils/module.py | 8 -------- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index 937bf82bc5..c2c73077e5 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -13,6 +13,7 @@ import os import sys +import logging import warnings from ._version import get_versions @@ -24,19 +25,42 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line= ignore_files = [ "ignite/handlers/checkpoint", "modelopt/torch/quantization/tensor_quant", - "nptyping/typing_", - "pydantic/_internal/_config", - "tempfile", - "mlflow/gateway", - "mlflow/utils", ] if any(ignore in filename for ignore in ignore_files): return old_showwarning(message, category, filename, lineno, file, line) +class DeprecatedTypesWarningFilter(logging.Filter): + def filter(self, record): + deprecated_aliases = [ + "np.bool8", + "np.object0", + "np.int0", + "np.uint0", + "np.void0", + "np.str0", + "np.bytes0", + "@validator", + "@root_validator", + "class-based `config`", + "pkg_resources", + "Implicitly cleaning up" + ] + for alias in deprecated_aliases: + if alias in record.getMessage(): + return False + return True # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 +# Set the custom warning handler to filter warning warnings.showwarning = custom_warning_handler +# Get the logger for warnings +logger = logging.getLogger("py.warnings") +# Create and add the filter to the logger +filter = DeprecatedTypesWarningFilter() +logger.addFilter(filter) + + PY_REQUIRED_MAJOR = 3 PY_REQUIRED_MINOR = 9 diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 555738ecd9..f5e199e2a3 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -44,7 +44,7 @@ has_nrrd = has_itk = has_nib = has_pil = has_pydicom = True else: - itk, has_itk = optional_import("itk", allow_namespace_pkg=True, reset_warning=True) + itk, has_itk = optional_import("itk", allow_namespace_pkg=True) nib, has_nib = optional_import("nibabel") Nifti1Image, _ = optional_import("nibabel.nifti1", name="Nifti1Image") PILImage, has_pil = optional_import("PIL.Image") diff --git a/monai/data/image_writer.py b/monai/data/image_writer.py index 2cf094264f..b9e8b9e68e 100644 --- a/monai/data/image_writer.py +++ b/monai/data/image_writer.py @@ -46,7 +46,7 @@ import nibabel as nib from PIL import Image as PILImage else: - itk, _ = optional_import("itk", allow_namespace_pkg=True, reset_warning=True) + itk, _ = optional_import("itk", allow_namespace_pkg=True) nib, _ = optional_import("nibabel") PILImage, _ = optional_import("PIL.Image") diff --git a/monai/data/itk_torch_bridge.py b/monai/data/itk_torch_bridge.py index 47e52a9c68..4671cd112b 100644 --- a/monai/data/itk_torch_bridge.py +++ b/monai/data/itk_torch_bridge.py @@ -28,7 +28,7 @@ has_itk = True else: - itk, has_itk = optional_import("itk", reset_warning=True) + itk, has_itk = optional_import("itk") __all__ = [ "itk_image_to_metatensor", diff --git a/monai/networks/utils.py b/monai/networks/utils.py index ce53ff9755..1826e9ae1f 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -37,7 +37,7 @@ onnxreference, _ = optional_import("onnx.reference") onnxruntime, _ = optional_import("onnxruntime") polygraphy, polygraphy_imported = optional_import("polygraphy") -torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0", reset_warning=True) +torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0") __all__ = [ diff --git a/monai/utils/module.py b/monai/utils/module.py index 808143f2db..59fe906bdd 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -364,8 +364,6 @@ def optional_import( a base class, or a decorator, the exceptions should raise accordingly. The current supported values are "default" (call once to raise), "decorator" (call the constructor and the second call to raise), and anything else will return a lazy class that can be used as a base class (call the constructor to raise). - reset_warning: whether to reset the warnings.showwarning and warnings.formatwarning to - the original function during the import. Returns: The imported module and a boolean flag indicating whether the import is successful. @@ -402,13 +400,7 @@ def optional_import( else: actual_cmd = f"import {module}" try: - if reset_warning: - old_showwarning = warnings.showwarning - old_formatwarning = warnings.formatwarning pkg = __import__(module) # top level module - if reset_warning: - warnings.showwarning = old_showwarning - warnings.formatwarning = old_formatwarning the_module = import_module(module) if not allow_namespace_pkg: is_namespace = getattr(the_module, "__file__", None) is None and hasattr(the_module, "__path__") From b65ad148a80326d772775f7a46cdcbb04f69a855 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:37:40 +0800 Subject: [PATCH 13/16] fix format Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index c2c73077e5..a7910b6618 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -22,14 +22,12 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line=None): - ignore_files = [ - "ignite/handlers/checkpoint", - "modelopt/torch/quantization/tensor_quant", - ] + ignore_files = ["ignite/handlers/checkpoint", "modelopt/torch/quantization/tensor_quant"] if any(ignore in filename for ignore in ignore_files): return old_showwarning(message, category, filename, lineno, file, line) + class DeprecatedTypesWarningFilter(logging.Filter): def filter(self, record): deprecated_aliases = [ @@ -44,13 +42,14 @@ def filter(self, record): "@root_validator", "class-based `config`", "pkg_resources", - "Implicitly cleaning up" - ] + "Implicitly cleaning up", + ] for alias in deprecated_aliases: if alias in record.getMessage(): return False return True + # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 # Set the custom warning handler to filter warning warnings.showwarning = custom_warning_handler From 7955e5a7c824e66d98c8b90974196411e9b16581 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:39:43 +0800 Subject: [PATCH 14/16] minor fix Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/networks/utils.py | 1 - monai/utils/module.py | 1 - 2 files changed, 2 deletions(-) diff --git a/monai/networks/utils.py b/monai/networks/utils.py index 1826e9ae1f..d0150b4e5b 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -39,7 +39,6 @@ polygraphy, polygraphy_imported = optional_import("polygraphy") torch_tensorrt, _ = optional_import("torch_tensorrt", "1.4.0") - __all__ = [ "one_hot", "predict_segmentation", diff --git a/monai/utils/module.py b/monai/utils/module.py index 59fe906bdd..78087aef84 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -345,7 +345,6 @@ def optional_import( version_args: Any = None, allow_namespace_pkg: bool = False, as_type: str = "default", - reset_warning: bool = False, ) -> tuple[Any, bool]: """ Imports an optional module specified by `module` string. From e46040142f59b4ffa64085ca2ef9c9e18b95d4f4 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:13:43 +0800 Subject: [PATCH 15/16] address comments Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index a7910b6618..200acc72e4 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -30,7 +30,7 @@ def custom_warning_handler(message, category, filename, lineno, file=None, line= class DeprecatedTypesWarningFilter(logging.Filter): def filter(self, record): - deprecated_aliases = [ + message_bodies_to_ignore = [ "np.bool8", "np.object0", "np.int0", @@ -44,20 +44,18 @@ def filter(self, record): "pkg_resources", "Implicitly cleaning up", ] - for alias in deprecated_aliases: - if alias in record.getMessage(): + for message in message_bodies_to_ignore: + if message in record.getMessage(): return False return True # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 +#TODO: remove this workaround after upstream fixed the warning # Set the custom warning handler to filter warning warnings.showwarning = custom_warning_handler -# Get the logger for warnings -logger = logging.getLogger("py.warnings") -# Create and add the filter to the logger -filter = DeprecatedTypesWarningFilter() -logger.addFilter(filter) +# Get the logger for warnings and add the filter to the logger +logging.getLogger("py.warnings").addFilter(DeprecatedTypesWarningFilter()) PY_REQUIRED_MAJOR = 3 From 5ad5eca078bbea1239ff1f8191e8e092b6bff9bd Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 18:33:49 +0800 Subject: [PATCH 16/16] fix format Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/__init__.py b/monai/__init__.py index 200acc72e4..3f6b12c407 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -51,7 +51,7 @@ def filter(self, record): # workaround for https://github.com/Project-MONAI/MONAI/issues/8060 -#TODO: remove this workaround after upstream fixed the warning +# TODO: remove this workaround after upstream fixed the warning # Set the custom warning handler to filter warning warnings.showwarning = custom_warning_handler # Get the logger for warnings and add the filter to the logger