Skip to content

Commit ad2d41f

Browse files
committed
[DLMED] update id
Signed-off-by: Nic Ma <nma@nvidia.com>
1 parent b6e8005 commit ad2d41f

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

monai/apps/manifest/config_item.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ class ConfigItem:
120120
Args:
121121
config: content of a config item, can be objects of any types,
122122
a configuration resolver may interpret the content to generate a configuration object.
123-
id: optional name of the current config item, defaults to `None`.
123+
id: name of the current config item, defaults to empty string.
124124
125125
"""
126126

127-
def __init__(self, config: Any, id: Optional[str] = None) -> None:
127+
def __init__(self, config: Any, id: str = "") -> None:
128128
self.config = config
129129
self.id = id
130130

@@ -183,7 +183,7 @@ class ConfigComponent(ConfigItem, Instantiable):
183183
184184
Args:
185185
config: content of a config item.
186-
id: optional name of the current config item, defaults to `None`.
186+
id: name of the current config item, defaults to empty string.
187187
locator: a ``ComponentLocator`` to convert a module name string into the actual python module.
188188
if `None`, a ``ComponentLocator(excludes=excludes)`` will be used.
189189
excludes: if ``locator`` is None, create a new ``ComponentLocator`` with ``excludes``.
@@ -194,7 +194,7 @@ class ConfigComponent(ConfigItem, Instantiable):
194194
def __init__(
195195
self,
196196
config: Any,
197-
id: Optional[str] = None,
197+
id: str = "",
198198
locator: Optional[ComponentLocator] = None,
199199
excludes: Optional[Union[Sequence[str], str]] = None,
200200
) -> None:
@@ -299,12 +299,12 @@ class ConfigExpression(ConfigItem):
299299
300300
Args:
301301
config: content of a config item.
302-
id: optional name of current config item, defaults to `None`.
302+
id: name of current config item, defaults to empty string.
303303
globals: additional global context to evaluate the string.
304304
305305
"""
306306

307-
def __init__(self, config: Any, id: Optional[str] = None, globals: Optional[Dict] = None) -> None:
307+
def __init__(self, config: Any, id: str = "", globals: Optional[Dict] = None) -> None:
308308
super().__init__(config=config, id=id)
309309
self.globals = globals
310310

monai/apps/manifest/reference_resolver.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ def _resolve_one_item(self, id: str, waiting_list: Optional[Set[str]] = None):
124124
else:
125125
self.resolved_content[id] = new_config
126126

127-
def resolve_all(self):
128-
"""
129-
Resolve the references for all the config items.
130-
131-
"""
132-
for k in self.items:
133-
self._resolve_one_item(id=k)
134-
135127
def get_resolved_content(self, id: str):
136128
"""
137129
Get the resolved content with specified id name.
@@ -194,9 +186,7 @@ def update_refs_pattern(value: str, refs: Dict) -> str:
194186
return value
195187

196188
@staticmethod
197-
def find_refs_in_config(
198-
config: Union[Dict, List, str], id: Optional[str] = None, refs: Optional[List[str]] = None
199-
) -> List[str]:
189+
def find_refs_in_config(config: Union[Dict, List, str], id: str, refs: Optional[List[str]] = None) -> List[str]:
200190
"""
201191
Recursively search all the content of input config item to get the ids of references.
202192
References mean: the IDs of other config items used as "@XXX" in this config item, or the
@@ -205,7 +195,7 @@ def find_refs_in_config(
205195
206196
Args:
207197
config: input config content to search.
208-
id: ID name for the input config item, default to `None`.
198+
id: ID name for the input config item.
209199
refs: list of the ID name of found references, default to `None`.
210200
211201
"""
@@ -216,21 +206,21 @@ def find_refs_in_config(
216206
if isinstance(config, (list, dict)):
217207
subs = enumerate(config) if isinstance(config, list) else config.items()
218208
for k, v in subs:
219-
sub_id = f"{id}#{k}" if id is not None else f"{k}"
209+
sub_id = f"{id}#{k}" if len(id) > 0 else f"{k}"
220210
if ConfigComponent.is_instantiable(v) or ConfigExpression.is_expression(v):
221211
refs_.append(sub_id)
222212
refs_ = ReferenceResolver.find_refs_in_config(v, sub_id, refs_)
223213
return refs_
224214

225215
@staticmethod
226-
def update_config_with_refs(config: Union[Dict, List, str], id: Optional[str] = None, refs: Optional[Dict] = None):
216+
def update_config_with_refs(config: Union[Dict, List, str], id: str, refs: Optional[Dict] = None):
227217
"""
228218
With all the references in `refs`, update the input config content with references
229219
and return the new config.
230220
231221
Args:
232222
config: input config content to update.
233-
id: ID name for the input config, default to `None`.
223+
id: ID name for the input config.
234224
refs: all the referring content with ids, default to `None`.
235225
236226
"""
@@ -241,7 +231,7 @@ def update_config_with_refs(config: Union[Dict, List, str], id: Optional[str] =
241231
# all the items in the list should be replaced with the references
242232
ret_list: List = []
243233
for i, v in enumerate(config):
244-
sub_id = f"{id}#{i}" if id is not None else f"{i}"
234+
sub_id = f"{id}#{i}" if len(id) > 0 else f"{i}"
245235
if ConfigComponent.is_instantiable(v) or ConfigExpression.is_expression(v):
246236
ret_list.append(refs_[sub_id])
247237
else:
@@ -251,7 +241,7 @@ def update_config_with_refs(config: Union[Dict, List, str], id: Optional[str] =
251241
# all the items in the dict should be replaced with the references
252242
ret_dict: Dict = {}
253243
for k, v in config.items():
254-
sub_id = f"{id}#{k}" if id is not None else f"{k}"
244+
sub_id = f"{id}#{k}" if len(id) > 0 else f"{k}"
255245
if ConfigComponent.is_instantiable(v) or ConfigExpression.is_expression(v):
256246
ret_dict[k] = refs_[sub_id]
257247
else:

tests/test_reference_resolver.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ def test_resolve(self, configs, expected_id, output_type):
9191
else:
9292
resolver.add_item(ConfigItem(config=v, id=k))
9393

94-
result = resolver.get_resolved_content(expected_id)
95-
self.assertTrue(isinstance(result, output_type))
96-
97-
# test resolve all
98-
resolver.resolved_content = {} # clear content
99-
resolver.resolve_all()
100-
result = resolver.get_resolved_content(expected_id)
94+
result = resolver.get_resolved_content(expected_id) # the root id is `expected_id` here
10195
self.assertTrue(isinstance(result, output_type))
10296

10397
# test lazy instantiation

0 commit comments

Comments
 (0)