Skip to content

Commit 81fabd3

Browse files
committed
[DLMED] fix path_id match
Signed-off-by: Nic Ma <nma@nvidia.com>
1 parent d8beeca commit 81fabd3

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

monai/bundle/config_reader.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pathlib import Path
1515
from typing import Dict, Sequence, Tuple, Union
1616

17-
from monai.bundle.reference_resolver import ReferenceResolver
1817
from monai.config import PathLike
1918
from monai.utils import ensure_tuple, look_up_option, optional_import
2019

@@ -37,8 +36,10 @@ class ConfigReader:
3736
"""
3837

3938
suffixes = ("json", "yaml", "yml")
40-
path_match = re.compile(rf"(.*\.({'|'.join(suffixes)})$)", re.IGNORECASE)
39+
suffix_match = rf"\.({'|'.join(suffixes)})"
40+
path_match = rf"(.*{suffix_match}$)"
4141
meta_key = "<meta>" # field key to save metadata
42+
sep = "#" # separator for file path and the id of content in the file
4243

4344
def __init__(self):
4445
self.config: Dict = {self.meta_key: {}}
@@ -54,7 +55,7 @@ def load_config_file(cls, filepath: PathLike, **kwargs):
5455
5556
"""
5657
_filepath: str = str(Path(filepath))
57-
if not cls.path_match.findall(_filepath):
58+
if not re.compile(cls.path_match, re.IGNORECASE).findall(_filepath):
5859
raise ValueError(f'unknown file input: "{filepath}"')
5960
with open(_filepath) as f:
6061
if _filepath.lower().endswith(cls.suffixes[0]):
@@ -105,17 +106,18 @@ def split_path_id(cls, src: str) -> Tuple[str, str]:
105106
"""
106107
Split `src` string into two parts: a config file path and component id.
107108
The file path should end with `(json|yaml|yml)`. The component id should be separated by `#` if it exists.
109+
If no path or no id, return "".
108110
109111
Args:
110112
src: source string to split.
111113
112114
"""
113-
path, *ids = src.rsplit(ReferenceResolver.sep, 1)
114-
path_name = cls.path_match.findall(path)
115-
if not path_name:
115+
result = re.compile(cls.suffix_match, re.IGNORECASE).findall(src)
116+
if len(result) != 1:
116117
return "", src # the src is a pure id
117-
ids_string: str = ids[0] if ids else ""
118-
return path_name[0][0], ids_string
118+
items = src.split(result[0])
119+
# return file path and the
120+
return items[0] + result[0], items[1][len(cls.sep) :] if items[1] != "" else ""
119121

120122
def read_meta(self, f: Union[PathLike, Sequence[PathLike], Dict], **kwargs):
121123
"""

monai/bundle/scripts.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
from typing import Dict, Optional, Sequence, Union
12+
from typing import Optional, Sequence, Union
1313

1414
from monai.bundle.config_parser import ConfigParser
1515
from monai.bundle.config_reader import ConfigReader
@@ -31,20 +31,20 @@ def run(
3131
.. code-block:: bash
3232
3333
# Execute this module as a CLI entry:
34-
python -m monai.bundle run --meta_file=<meta path> --config_file=<config path> --target_id=trainer
34+
python -m monai.bundle run --meta_file <meta path> --config_file <config path> --target_id trainer
3535
3636
# Override config values at runtime by specifying the component id and its new value:
37-
python -m monai.bundle run --"'net#<args>#input_chns'"=1 ...
37+
python -m monai.bundle run --"net#<args>#input_chns" 1 ...
3838
3939
# Override config values with another config file `/path/to/another.json`:
40-
python -m monai.bundle run --"'net#<args>'"="'%/path/to/another.json'" ...
40+
python -m monai.bundle run --"net#<args>" "%/path/to/another.json" ...
4141
4242
# Override config values with part content of another config file:
43-
python -m monai.bundle run --"'net#<args>'"="'%/data/other.json#net_arg'" ...
43+
python -m monai.bundle run --"net#<args>" "%/data/other.json#net_arg" ...
4444
4545
# Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
4646
# Other args still can override the default args at runtime:
47-
python -m monai.bundle run --args_file="'/workspace/data/args.json'" --config_file=<config path>
47+
python -m monai.bundle run --args_file "/workspace/data/args.json" --config_file <config path>
4848
4949
Args:
5050
meta_file: filepath of the metadata file, if `None`, must be provided in `args_file`.

0 commit comments

Comments
 (0)