From b539cbb99b263e4b69a8fb2cbc21ef020ff669b2 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Fri, 6 Sep 2024 20:06:11 +0800 Subject: [PATCH] Suppress deprecated warning when import monai (#8067) - add packaging in setup.cfg - fix test_gdsdataset.py issue - add test_matshow3d to the skip list for min test - suppress deprecated warning when import monai (workaround for #8060) ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> --- monai/__init__.py | 44 +++++++++++++++++++++++++++++++++++++- monai/data/image_reader.py | 3 --- pyproject.toml | 1 + setup.cfg | 1 + tests/min_tests.py | 1 + tests/test_gdsdataset.py | 7 +++--- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/monai/__init__.py b/monai/__init__.py index cb0ccd36f8..3f6b12c407 100644 --- a/monai/__init__.py +++ b/monai/__init__.py @@ -13,9 +13,51 @@ import os import sys - +import logging +import warnings from ._version import get_versions + +old_showwarning = warnings.showwarning + + +def custom_warning_handler(message, category, filename, lineno, file=None, line=None): + 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): + message_bodies_to_ignore = [ + "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 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 and add the filter to the logger +logging.getLogger("py.warnings").addFilter(DeprecatedTypesWarningFilter()) + + PY_REQUIRED_MAJOR = 3 PY_REQUIRED_MINOR = 9 diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index aab1e03898..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/8061 -warnings.filterwarnings("ignore", category=DeprecationWarning, module="nptyping") - if TYPE_CHECKING: import itk import nibabel as nib 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] 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..f39d3f9843 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))