Skip to content

Commit

Permalink
add logger tests (#23)
Browse files Browse the repository at this point in the history
add logger config tests @heyufan1995 



### 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.
- [ ] In-line docstrings updated.

---------

Signed-off-by: Wenqi Li <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
wyli and pre-commit-ci[bot] authored Jun 27, 2024
1 parent 0595537 commit 87e7409
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/premerge-py-min.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", ...}
}
Expand Down
5 changes: 2 additions & 3 deletions scripts/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -45,7 +46,7 @@
"propagate": False,
}
},
"filters": {"rank_filter": {"{}": "__main__.RankFilter"}},
"filters": {"rank_filter": {"{}": RankFilter}},
"handlers": {
"file": {
"class": "logging.FileHandler",
Expand Down Expand Up @@ -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)
7 changes: 3 additions & 4 deletions scripts/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,6 +62,7 @@
)

nib.imageglobals.logger.setLevel(40)
RankFilter, _ = optional_import("monai.utils", name="RankFilter")
CONFIG = {
"version": 1,
"disable_existing_loggers": False,
Expand All @@ -83,7 +84,7 @@
"propagate": False,
},
},
"filters": {"rank_filter": {"()": "__main__.RankFilter"}},
"filters": {"rank_filter": {"()": RankFilter}},
"handlers": {
"file": {
"class": "logging.FileHandler",
Expand Down Expand Up @@ -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()
10 changes: 10 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 87e7409

Please sign in to comment.