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
Merged

Conversation

KumoLiu
Copy link
Contributor

@KumoLiu KumoLiu commented Sep 5, 2024

Types of changes

  • 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>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 5, 2024

I aim to make the suppression of warnings as specific as possible to avoid impacting other parts of the code. For warnings that I can trace to specific imports, like nptyping, I apply filtering at the point of import. For warnings where the source is harder to locate, I suppress them based on the warning message in the init file. For those I can't suppress certain warnings directly in init, I handle them by adding the suppression logic to optional imports.
cc @ericspod @Nic-Ma @mingxin-zheng, please help take a look to see if you have any concern or better suggestions, thanks.

monai/__init__.py Outdated Show resolved Hide resolved
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@ericspod
Copy link
Member

ericspod commented Sep 5, 2024

It's a little worrying doing a blanket "ignore" on warnings from a library, but I know they're annoying to deal with in any other way. Even though some of the suppression is for optional packages, could we still use your filter function to ignore them? Eg. whether we import mlflow or not we can filter the warnings that it may create.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 5, 2024

could we still use your filter function to ignore them? Eg. whether we import mlflow or not we can filter the warnings that it may create.

Looks like I can't use this custom filter handler due to this RecursionError.

  File "/Users/runner/work/MONAI/MONAI/monai/__init__.py", line 25, in custom_warning_handler
BasicUNet features: (8, 16, 32, 64, 128, 8).
    warnings.showwarning(message, category, filename, lineno, file, line)
  [Previous line repeated 975 more times]
RecursionError: maximum recursion depth exceeded

@ericspod
Copy link
Member

ericspod commented Sep 5, 2024

Looks like I can't use this custom filter handler due to this RecursionError.

This is possibly because you're calling showwarning within it which is itself, you can save the older function as a separate variable and call it, or call formatwarning https://docs.python.org/3/library/warnings.html#warnings.showwarning

monai/__init__.py Outdated Show resolved Hide resolved
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 5, 2024

could we still use your filter function to ignore them? Eg. whether we import mlflow or not we can filter the warnings that it may create.

Hi @ericspod, I tried this but seems the warning message from optional_import will not shown in this init. One way we can do it is add this handler in the init file where will show the warning, but the issue is that we don't know the import order, so for each submodule init, we may need to add this handler. Do you think that make sense?

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@ericspod
Copy link
Member

ericspod commented Sep 5, 2024

I don't think it would be better to have this handler added in multiple places. Alternatively can we put the filterwarnings calls into __init__.py in one place?

@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 5, 2024

I don't think it would be better to have this handler added in multiple places. Alternatively can we put the filterwarnings calls into __init__.py in one place?

Actually I didn't understand why some of the warning message can be captured or shown by warnings.showwarning from the __init__.py I add, but some of them may not shown. So I'm not sure where we can put this handler, to my best of the knowledge, project-level init.py file should be be best place, but it can not capture all warning.

@ericspod
Copy link
Member

ericspod commented Sep 5, 2024

Actually I didn't understand why some of the warning message can be captured or shown by warnings.showwarning from the __init__.py I add, but some of them may not shown. So I'm not sure where we can put this handler, to my best of the knowledge, project-level init.py file should be be best place, but it can not capture all warning.

It's possible other libraries are using other functions in warnings so bypasses the override you're using, such as warn_explicit. I'm trying to read through the source of the warnings module and there's a few things in there like that which could be used to raise warnings, but filtering should work with that.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 6, 2024

Hi @ericspod, I discovered that the showwarning function has been modified, which is why some warnings are no longer displayed. I attempted to add a reset warning to the optional import, and set True in tensorrt and itk. Could you please help take another look at it. This may resolve your concern you mentioned before.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@mingxin-zheng
Copy link
Contributor

Hi @KumoLiu , I think the reason that you can't disable some warnings is partially because they're sent by logging.warning module.

This seems to be working to cancel out some of the warnings:

import logging

# Create a custom filter
class DeprecatedTypesWarningFilter(logging.Filter):
    def filter(self, record):
        deprecated_aliases = ["np.bool8", "np.object0", "np.int0", "np.uint0", "np.void0", "np.str0", "np.bytes0"]
        for alias in deprecated_aliases:
            if alias in record.getMessage():
                return False
        return True

# Get the logger for warnings
logger = logging.getLogger("py.warnings")

# Create and add the filter to the logger
filter = DeprecatedTypesWarningFilter()
logger.addFilter(filter)

import monai

print(monai.__version__)

@mingxin-zheng
Copy link
Contributor

Another example why the filter doesn't work is because the library forces it:

https://github.com/pytorch/pytorch/blob/758d515d982a7fe00afe78060fecec3e55b72fe8/torch/distributed/optim/__init__.py#L30

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 6, 2024

Thanks @mingxin-zheng,I added it in the latest commit. A good catch then I don't need to locate where shown the warning. Thanks!

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
monai/__init__.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mingxin-zheng mingxin-zheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good to me. Thanks!

monai/__init__.py Outdated Show resolved Hide resolved
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@ericspod
Copy link
Member

ericspod commented Sep 6, 2024

Another example why the filter doesn't work is because the library forces it:

https://github.com/pytorch/pytorch/blob/758d515d982a7fe00afe78060fecec3e55b72fe8/torch/distributed/optim/__init__.py#L30

This strikes me as just being really bad behaviour. The point of filtering things is so that one can suppress warnings after they've seen them so they don't continue to annoy, people will abuse that to sweep warnings under the rug but this is the same thing in mirror.

Copy link
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried that we're having to do all this to shut up other warnings. Pytorch and others abuse the API of warnings I feel to force their messages to show, this is bad design but then we're doing the same thing here to counteract this. I hope this is all temporary until other dependencies update.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Sep 6, 2024

/build

@KumoLiu KumoLiu enabled auto-merge (squash) September 6, 2024 11:10
@KumoLiu KumoLiu merged commit b539cbb into Project-MONAI:dev Sep 6, 2024
28 checks passed
@KumoLiu KumoLiu deleted the setup branch September 8, 2024 14:24
hjmjohnson pushed a commit to hjmjohnson/MONAI that referenced this pull request Sep 8, 2024
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants