Skip to content

Commit b539cbb

Browse files
KumoLiuericspod
andauthored
Suppress deprecated warning when import monai (Project-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 Project-MONAI#8060) ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [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>
1 parent 19cc6f0 commit b539cbb

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

monai/__init__.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,51 @@
1313

1414
import os
1515
import sys
16-
16+
import logging
17+
import warnings
1718
from ._version import get_versions
1819

20+
21+
old_showwarning = warnings.showwarning
22+
23+
24+
def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
25+
ignore_files = ["ignite/handlers/checkpoint", "modelopt/torch/quantization/tensor_quant"]
26+
if any(ignore in filename for ignore in ignore_files):
27+
return
28+
old_showwarning(message, category, filename, lineno, file, line)
29+
30+
31+
class DeprecatedTypesWarningFilter(logging.Filter):
32+
def filter(self, record):
33+
message_bodies_to_ignore = [
34+
"np.bool8",
35+
"np.object0",
36+
"np.int0",
37+
"np.uint0",
38+
"np.void0",
39+
"np.str0",
40+
"np.bytes0",
41+
"@validator",
42+
"@root_validator",
43+
"class-based `config`",
44+
"pkg_resources",
45+
"Implicitly cleaning up",
46+
]
47+
for message in message_bodies_to_ignore:
48+
if message in record.getMessage():
49+
return False
50+
return True
51+
52+
53+
# workaround for https://github.com/Project-MONAI/MONAI/issues/8060
54+
# TODO: remove this workaround after upstream fixed the warning
55+
# Set the custom warning handler to filter warning
56+
warnings.showwarning = custom_warning_handler
57+
# Get the logger for warnings and add the filter to the logger
58+
logging.getLogger("py.warnings").addFilter(DeprecatedTypesWarningFilter())
59+
60+
1961
PY_REQUIRED_MAJOR = 3
2062
PY_REQUIRED_MINOR = 9
2163

monai/data/image_reader.py

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
)
3535
from monai.utils import MetaKeys, SpaceKeys, TraceKeys, ensure_tuple, optional_import, require_pkg
3636

37-
# workaround for https://github.com/Project-MONAI/MONAI/issues/8061
38-
warnings.filterwarnings("ignore", category=DeprecationWarning, module="nptyping")
39-
4037
if TYPE_CHECKING:
4138
import itk
4239
import nibabel as nib

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ requires = [
44
"setuptools",
55
"torch>=1.9",
66
"ninja",
7+
"packaging"
78
]
89

910
[tool.black]

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ python_requires = >= 3.9
4040
setup_requires =
4141
torch
4242
ninja
43+
packaging
4344
install_requires =
4445
torch>=1.9
4546
numpy>=1.24,<2.0

tests/min_tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def run_testsuit():
212212
"test_ultrasound_confidence_map_transform",
213213
"test_vista3d_utils",
214214
"test_vista3d_transforms",
215+
"test_matshow3d",
215216
]
216217
assert sorted(exclude_cases) == sorted(set(exclude_cases)), f"Duplicated items in {exclude_cases}"
217218

tests/test_gdsdataset.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from monai.data import GDSDataset, json_hashing
2424
from monai.transforms import Compose, Flip, Identity, LoadImaged, SimulateDelayd, Transform
2525
from monai.utils import optional_import
26-
from tests.utils import TEST_NDARRAYS, assert_allclose
26+
from tests.utils import TEST_NDARRAYS, assert_allclose, skip_if_no_cuda
2727

2828
_, has_cp = optional_import("cupy")
2929
nib, has_nib = optional_import("nibabel")
@@ -70,9 +70,9 @@ def __call__(self, data):
7070
return data
7171

7272

73+
@skip_if_no_cuda
7374
@unittest.skipUnless(has_cp, "Requires CuPy library.")
74-
@unittest.skipUnless(has_nib, "Requires nibabel package.")
75-
@unittest.skipUnless(has_kvikio_numpy, "Requires scikit-image library.")
75+
@unittest.skipUnless(has_cp and has_kvikio_numpy, "Requires CuPy and kvikio library.")
7676
class TestDataset(unittest.TestCase):
7777

7878
def test_cache(self):
@@ -131,6 +131,7 @@ def test_dtype(self):
131131
self.assertEqual(ds[0].dtype, DTYPES[_dtype])
132132
self.assertEqual(ds1[0].dtype, DTYPES[_dtype])
133133

134+
@unittest.skipUnless(has_nib, "Requires nibabel package.")
134135
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3])
135136
def test_shape(self, transform, expected_shape):
136137
test_image = nib.Nifti1Image(np.random.randint(0, 2, size=[128, 128, 128]).astype(float), np.eye(4))

0 commit comments

Comments
 (0)