Skip to content

Commit

Permalink
improve bundle run logging
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <[email protected]>
  • Loading branch information
wyli committed Oct 10, 2023
1 parent 9747510 commit 732cc50
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions monai/bundle/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import os
import sys
import time
import warnings
from abc import ABC, abstractmethod
from copy import copy
from logging.config import fileConfig
Expand Down Expand Up @@ -158,7 +157,7 @@ def add_property(self, name: str, required: str, desc: str | None = None) -> Non
if self.properties is None:
self.properties = {}
if name in self.properties:
warnings.warn(f"property '{name}' already exists in the properties list, overriding it.")
logger.warn(f"property '{name}' already exists in the properties list, overriding it.")
self.properties[name] = {BundleProperty.DESC: desc, BundleProperty.REQUIRED: required}

def check_properties(self) -> list[str] | None:
Expand Down Expand Up @@ -241,7 +240,7 @@ def __init__(
for _config_file in _config_files:
_config_file = Path(_config_file)
if _config_file.parent != self.config_root_path:
warnings.warn(
logger.warn(
f"Not all config files are in {self.config_root_path}. If logging_file and meta_file are"
f"not specified, {self.config_root_path} will be used as the default config root directory."
)
Expand All @@ -254,7 +253,7 @@ def __init__(
if logging_file is not None:
if not os.path.exists(logging_file):
if logging_file == str(self.config_root_path / "logging.conf"):
warnings.warn(f"Default logging file in {logging_file} does not exist, skipping logging.")
logger.warn(f"Default logging file in {logging_file} does not exist, skipping logging.")
else:
raise FileNotFoundError(f"Cannot find the logging config file: {logging_file}.")
else:
Expand All @@ -265,7 +264,10 @@ def __init__(
self.parser.read_config(f=config_file)
meta_file = str(self.config_root_path / "metadata.json") if meta_file is None else meta_file
if isinstance(meta_file, str) and not os.path.exists(meta_file):
raise FileNotFoundError(f"Cannot find the metadata config file: {meta_file}.")
logger.error(
f"Cannot find the metadata config file: {meta_file}. "
"Please see: https://docs.monai.io/en/stable/mb_specification.html"
)
else:
self.parser.read_meta(f=meta_file)

Expand Down Expand Up @@ -323,17 +325,17 @@ def check_properties(self) -> list[str] | None:
"""
ret = super().check_properties()
if self.properties is None:
warnings.warn("No available properties had been set, skipping check.")
logger.warn("No available properties had been set, skipping check.")
return None
if ret:
warnings.warn(f"Loaded bundle does not contain the following required properties: {ret}")
logger.warn(f"Loaded bundle does not contain the following required properties: {ret}")
# also check whether the optional properties use correct ID name if existing
wrong_props = []
for n, p in self.properties.items():
if not p.get(BundleProperty.REQUIRED, False) and not self._check_optional_id(name=n, property=p):
wrong_props.append(n)
if wrong_props:
warnings.warn(f"Loaded bundle defines the following optional properties with wrong ID: {wrong_props}")
logger.warn(f"Loaded bundle defines the following optional properties with wrong ID: {wrong_props}")
if ret is not None:
ret.extend(wrong_props)
return ret
Expand Down

0 comments on commit 732cc50

Please sign in to comment.