Skip to content

Commit f793e80

Browse files
resolve comment of meta_file
Signed-off-by: Yiheng Wang <vennw@nvidia.com>
1 parent ccc2364 commit f793e80

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

monai/bundle/workflows.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ def __init__(
9191

9292
if meta_file is not None:
9393
if isinstance(meta_file, str) and not os.path.isfile(meta_file):
94-
raise FileNotFoundError(f"Cannot find the metadata config file: {meta_file}.")
94+
logger.error(
95+
f"Cannot find the metadata config file: {meta_file}. "
96+
"Please see: https://docs.monai.io/en/stable/mb_specification.html"
97+
)
98+
meta_file = None
9599
if isinstance(meta_file, list):
96100
for f in meta_file:
97101
if not os.path.isfile(f):
98-
raise FileNotFoundError(f"Cannot find the metadata config file: {f}.")
102+
logger.error(
103+
f"Cannot find the metadata config file: {f}. "
104+
"Please see: https://docs.monai.io/en/stable/mb_specification.html"
105+
)
106+
meta_file = None
99107

100108
self.meta_file = meta_file
101109

@@ -258,22 +266,23 @@ def __init__(
258266
**override: Any,
259267
) -> None:
260268
workflow_type = workflow if workflow is not None else workflow_type
261-
super().__init__(workflow_type=workflow_type)
262269
if config_file is not None:
263270
_config_files = ensure_tuple(config_file)
264-
self.config_root_path = Path(_config_files[0]).parent
271+
config_root_path = Path(_config_files[0]).parent
265272
for _config_file in _config_files:
266273
_config_file = Path(_config_file)
267-
if _config_file.parent != self.config_root_path:
274+
if _config_file.parent != config_root_path:
268275
logger.warn(
269-
f"Not all config files are in {self.config_root_path}. If logging_file and meta_file are"
270-
f"not specified, {self.config_root_path} will be used as the default config root directory."
276+
f"Not all config files are in {config_root_path}. If logging_file and meta_file are"
277+
f"not specified, {config_root_path} will be used as the default config root directory."
271278
)
272279
if not _config_file.is_file():
273280
raise FileNotFoundError(f"Cannot find the config file: {_config_file}.")
274281
else:
275-
self.config_root_path = Path("configs")
276-
282+
config_root_path = Path("configs")
283+
meta_file = str(config_root_path / "metadata.json") if meta_file is None else meta_file
284+
super().__init__(workflow_type=workflow_type, meta_file=meta_file)
285+
self.config_root_path = config_root_path
277286
logging_file = str(self.config_root_path / "logging.conf") if logging_file is None else logging_file
278287
if logging_file is not None:
279288
if not os.path.isfile(logging_file):
@@ -287,21 +296,8 @@ def __init__(
287296

288297
self.parser = ConfigParser()
289298
self.parser.read_config(f=config_file)
290-
meta_file = str(self.config_root_path / "metadata.json") if meta_file is None else meta_file
291-
if isinstance(meta_file, str) and not os.path.isfile(meta_file):
292-
logger.error(
293-
f"Cannot find the metadata config file: {meta_file}. "
294-
"Please see: https://docs.monai.io/en/stable/mb_specification.html"
295-
)
296-
elif isinstance(meta_file, list):
297-
for f in meta_file:
298-
if not os.path.isfile(f):
299-
logger.error(
300-
f"Cannot find the metadata config file: {f}. "
301-
"Please see: https://docs.monai.io/en/stable/mb_specification.html"
302-
)
303-
else:
304-
self.parser.read_meta(f=meta_file)
299+
if self.meta_file is not None:
300+
self.parser.read_meta(f=self.meta_file)
305301

306302
# the rest key-values in the _args are to override config content
307303
self.parser.update(pairs=override)

tests/test_bundle_workflow.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from __future__ import annotations
1313

14+
import logging
1415
import os
1516
import shutil
1617
import tempfile
@@ -37,10 +38,6 @@
3738

3839
TEST_CASE_NON_CONFIG_WRONG_LOG = [None, "logging.conf", "Cannot find the logging config file: logging.conf."]
3940

40-
TEST_CASE_NON_CONFIG_WRONG_META = ["meta.json", None, "Cannot find the metadata config file: meta.json."]
41-
42-
TEST_CASE_NON_CONFIG_WRONG_META_LIST = [["meta.json"], None, "Cannot find the metadata config file: meta.json."]
43-
4441

4542
class TestBundleWorkflow(unittest.TestCase):
4643

@@ -153,10 +150,8 @@ def test_non_config(self):
153150
self.assertEqual(inferer.meta_file, None)
154151
self._test_inferer(inferer)
155152

156-
@parameterized.expand(
157-
[TEST_CASE_NON_CONFIG_WRONG_META, TEST_CASE_NON_CONFIG_WRONG_META_LIST, TEST_CASE_NON_CONFIG_WRONG_LOG]
158-
)
159-
def test_non_config_wrong_cases(self, meta_file, logging_file, expected_error):
153+
@parameterized.expand([TEST_CASE_NON_CONFIG_WRONG_LOG])
154+
def test_non_config_wrong_log_cases(self, meta_file, logging_file, expected_error):
160155
with self.assertRaisesRegex(FileNotFoundError, expected_error):
161156
NonConfigWorkflow(self.filename, self.data_dir, meta_file, logging_file)
162157

0 commit comments

Comments
 (0)