From 87e74091213826b8599e7dd25734c7787d2c5949 Mon Sep 17 00:00:00 2001 From: Wenqi Li <831580+wyli@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:18:41 +0100 Subject: [PATCH] add logger tests (#23) add logger config tests @heyufan1995 ### 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. - [ ] In-line docstrings updated. --------- Signed-off-by: Wenqi Li Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/premerge-py-min.yml | 4 ++-- data/README.md | 2 +- scripts/infer.py | 5 ++-- scripts/train.py | 7 +++--- tests/__init__.py | 10 ++++++++ tests/test_logger.py | 33 +++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_logger.py diff --git a/.github/workflows/premerge-py-min.yml b/.github/workflows/premerge-py-min.yml index 3dcc1c9..d7e6da8 100644 --- a/.github/workflows/premerge-py-min.yml +++ b/.github/workflows/premerge-py-min.yml @@ -46,11 +46,11 @@ jobs: - name: Install the dependencies run: | python -m pip install torch --extra-index-url https://download.pytorch.org/whl/cpu - python -m pip install monai pyyaml + python -m pip install "monai[all]" python -m pip list shell: bash - name: Run quick tests (CPU ${{ runner.os }}) run: | python -c 'import torch; print(torch.__version__); print(torch.rand(5,3))' python -c "import monai; monai.config.print_config()" - python tests/test_config.py + python -m unittest -v diff --git a/data/README.md b/data/README.md index 4b8c5fc..f9e2a4d 100644 --- a/data/README.md +++ b/data/README.md @@ -20,7 +20,7 @@ The JSON file has the following structure: ], "training_transform": [ # a set of monai transform configuration for dataset-specific loading - ] + ], "original_label_dict": {"1": "liver", ...}, "label_dict": {"1": "liver", ...} } diff --git a/scripts/infer.py b/scripts/infer.py index 6a5a21f..2673c2d 100644 --- a/scripts/infer.py +++ b/scripts/infer.py @@ -33,6 +33,7 @@ from .utils.trans_utils import get_largest_connected_component_point rearrange, _ = optional_import("einops", name="rearrange") +RankFilter, _ = optional_import("monai.utils", name="RankFilter") sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) CONFIG = { "version": 1, @@ -45,7 +46,7 @@ "propagate": False, } }, - "filters": {"rank_filter": {"{}": "__main__.RankFilter"}}, + "filters": {"rank_filter": {"{}": RankFilter}}, "handlers": { "file": { "class": "logging.FileHandler", @@ -352,7 +353,5 @@ def batch_infer_everything(self, datalist=str, basedir=str): if __name__ == "__main__": - from monai.utils import optional_import - fire, _ = optional_import("fire") fire.Fire(InferClass) diff --git a/scripts/train.py b/scripts/train.py index eccf2d0..e7c4ef5 100644 --- a/scripts/train.py +++ b/scripts/train.py @@ -35,7 +35,7 @@ from monai.data import DataLoader, DistributedSampler, DistributedWeightedRandomSampler from monai.metrics import compute_dice from monai.networks.utils import copy_model_state -from monai.utils import set_determinism +from monai.utils import optional_import, set_determinism from torch.nn.parallel import DistributedDataParallel from torch.utils.data.sampler import RandomSampler, WeightedRandomSampler from torch.utils.tensorboard import SummaryWriter @@ -62,6 +62,7 @@ ) nib.imageglobals.logger.setLevel(40) +RankFilter, _ = optional_import("monai.utils", name="RankFilter") CONFIG = { "version": 1, "disable_existing_loggers": False, @@ -83,7 +84,7 @@ "propagate": False, }, }, - "filters": {"rank_filter": {"()": "__main__.RankFilter"}}, + "filters": {"rank_filter": {"()": RankFilter}}, "handlers": { "file": { "class": "logging.FileHandler", @@ -1023,7 +1024,5 @@ def run(config_file: Optional[Union[str, Sequence[str]]] = None, **override): if __name__ == "__main__": - from monai.utils import optional_import - fire, _ = optional_import("fire") fire.Fire() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..1e97f89 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,10 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/tests/test_logger.py b/tests/test_logger.py new file mode 100644 index 0000000..b5f713c --- /dev/null +++ b/tests/test_logger.py @@ -0,0 +1,33 @@ +# Copyright (c) MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import unittest + +from monai.apps.auto3dseg.auto_runner import logger + + +class TestLogger(unittest.TestCase): + def test_vista3d_logger(self): + from scripts.train import CONFIG + + logging.config.dictConfig(CONFIG) + logger.warning("check train logging format") + + def test_vista3d_logger_infer(self): + from scripts.infer import CONFIG + + logging.config.dictConfig(CONFIG) + logger.warning("check infer logging format") + + +if __name__ == "__main__": + unittest.main()