Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress deprecated warning when import monai #8067

Merged
merged 16 commits into from
Sep 6, 2024
44 changes: 43 additions & 1 deletion monai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
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

Expand Down
3 changes: 0 additions & 3 deletions monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ requires = [
"setuptools",
"torch>=1.9",
"ninja",
"packaging"
]

[tool.black]
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ python_requires = >= 3.9
setup_requires =
torch
ninja
packaging
install_requires =
torch>=1.9
numpy>=1.24,<2.0
Expand Down
1 change: 1 addition & 0 deletions tests/min_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
7 changes: 4 additions & 3 deletions tests/test_gdsdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
Loading