Skip to content

Commit

Permalink
Suppress deprecated warning when import monai (#8067)
Browse files Browse the repository at this point in the history
- 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
<!--- 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>
  • Loading branch information
KumoLiu and ericspod authored Sep 6, 2024
1 parent 19cc6f0 commit b539cbb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
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"]
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

0 comments on commit b539cbb

Please sign in to comment.