Skip to content

Commit

Permalink
add logger tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <[email protected]>
  • Loading branch information
wyli committed Jun 27, 2024
1 parent 0595537 commit 72d1de1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/premerge-py-min.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
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
3 changes: 2 additions & 1 deletion 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
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 set_determinism, optional_import
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 72d1de1

Please sign in to comment.