From f68eb5a60d8632e7d1cc30dddb3b6c92f72add7c Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 3 Feb 2023 16:47:46 +0800 Subject: [PATCH 1/2] [DLMED] fix subclass reference Signed-off-by: Nic Ma --- monai/bundle/reference_resolver.py | 3 +++ tests/test_config_parser.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/monai/bundle/reference_resolver.py b/monai/bundle/reference_resolver.py index 710bb399ae..c87f474476 100644 --- a/monai/bundle/reference_resolver.py +++ b/monai/bundle/reference_resolver.py @@ -222,6 +222,9 @@ def update_refs_pattern(cls, value: str, refs: dict) -> str: """ # regular expression pattern to match "@XXX" or "@XXX#YYY" result = cls.id_matcher.findall(value) + # reversely sort the matched references by length + # and handle the longer first in case some reference item is a substring of another longer item + result.sort(key=len, reverse=True) value_is_expr = ConfigExpression.is_expression(value) for item in result: # only update reference when string starts with "$" or the whole content is "@XXX" diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index 9c7701e9fa..c4b50daed1 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -107,6 +107,8 @@ def __call__(self, a, b): TEST_CASE_4 = [{"A": 1, "B": "@A", "C": "@D", "E": "$'test' + '@F'"}] +TEST_CASE_5 = [{"training": {"A": 1, "A_B": 2}, "total": "$@training#A + @training#A_B + 1"}, 4] + class TestConfigParser(unittest.TestCase): def test_config_content(self): @@ -296,6 +298,11 @@ def test_builtin(self): config = {"import statements": "$import math", "calc": {"_target_": "math.isclose", "a": 0.001, "b": 0.001}} self.assertEqual(ConfigParser(config).calc, True) + @parameterized.expand([TEST_CASE_5]) + def test_substring_reference(self, config, expected): + parser = ConfigParser(config=config) + self.assertEqual(parser.get_parsed_content("total"), expected) + if __name__ == "__main__": unittest.main() From e9f05ad71f12840115fe9a1d39d4b298e37cfc1b Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 3 Feb 2023 16:54:36 +0800 Subject: [PATCH 2/2] [DLMED] fix typo Signed-off-by: Nic Ma --- monai/bundle/reference_resolver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/bundle/reference_resolver.py b/monai/bundle/reference_resolver.py index c87f474476..7b74263d65 100644 --- a/monai/bundle/reference_resolver.py +++ b/monai/bundle/reference_resolver.py @@ -223,7 +223,7 @@ def update_refs_pattern(cls, value: str, refs: dict) -> str: # regular expression pattern to match "@XXX" or "@XXX#YYY" result = cls.id_matcher.findall(value) # reversely sort the matched references by length - # and handle the longer first in case some reference item is a substring of another longer item + # and handle the longer first in case a reference item is substring of another longer item result.sort(key=len, reverse=True) value_is_expr = ConfigExpression.is_expression(value) for item in result: