From e06338b0ac10f6cb57b850619e620a149627721e Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Thu, 5 Jan 2023 17:20:10 +0100 Subject: [PATCH 1/6] simplify the configparser usage to get parsed attributes easily (#5804) Signed-off-by: Felix Schnabel --- monai/bundle/config_parser.py | 10 ++++++++++ tests/test_config_parser.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index d57238cfaa..d17cf91ef4 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -116,6 +116,16 @@ def __init__( def __repr__(self): return f"{self.config}" + def __getattr__(self, id): + """ + Get the config by id. + + Args: + id: id to specify the expected position. See also :py:meth:`__getitem__`. + + """ + return self[id] + def __getitem__(self, id: Union[str, int]): """ Get the config by id. diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index d02a05c914..90beeed2d4 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -256,6 +256,11 @@ def test_pdb(self): with self.assertRaisesRegex(RuntimeError, ".*bdb.BdbQuit.*"): case_pdb() + def test_get_via_attributes(self): + config = {"A": {"B": {"C": 1}}} + parser = ConfigParser(config=config) + self.assertEqual(parser.A, {"B": {"C": 1}}) + if __name__ == "__main__": unittest.main() From c364fe88afbf95c0496fc675bd9d0300d7c22e2b Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Fri, 6 Jan 2023 13:21:30 +0100 Subject: [PATCH 2/6] Return the parsed content when accessing ConfigParser config via attribute (not nested). Signed-off-by: Felix Schnabel --- monai/bundle/config_parser.py | 2 +- tests/test_config_parser.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index d17cf91ef4..158c03a2ca 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -124,7 +124,7 @@ def __getattr__(self, id): id: id to specify the expected position. See also :py:meth:`__getitem__`. """ - return self[id] + return self.get_parsed_content(id) def __getitem__(self, id: Union[str, int]): """ diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index 90beeed2d4..305dae13e2 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -257,9 +257,20 @@ def test_pdb(self): case_pdb() def test_get_via_attributes(self): - config = {"A": {"B": {"C": 1}}} + config = { + "A": {"B": {"C": 1}}, + "my_dims": 2, + "dims_1": "$@my_dims + 1", + "patch_size": [8, 8], + "transform": {"_target_": "Lambda", "func": "$lambda x: x.reshape((1, *@patch_size))"}, + } parser = ConfigParser(config=config) self.assertEqual(parser.A, {"B": {"C": 1}}) + self.assertEqual(parser.dims_1, 3) + + trans = parser.transform + result = trans(np.ones(64)) + self.assertTupleEqual(result.shape, (1, 8, 8)) if __name__ == "__main__": From 098bb1b35ba70368278a0147c907980672aff1fb Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 6 Jan 2023 13:03:25 +0000 Subject: [PATCH 3/6] fixes integration typo Signed-off-by: Wenqi Li --- tests/test_auto3dseg_ensemble.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_auto3dseg_ensemble.py b/tests/test_auto3dseg_ensemble.py index c1ffbd254a..d33e20b07d 100644 --- a/tests/test_auto3dseg_ensemble.py +++ b/tests/test_auto3dseg_ensemble.py @@ -139,7 +139,7 @@ def test_ensemble(self) -> None: builder = AlgoEnsembleBuilder(history, data_src_cfg) builder.set_ensemble_method(AlgoEnsembleBestN(n_best=1)) ensemble = builder.get_ensemble() - pred_param["network#init_filter"] = 8 # segresnet + pred_param["network#init_filters"] = 8 # segresnet preds = ensemble(pred_param) self.assertTupleEqual(preds[0].shape, (2, 24, 24, 24)) From 7e0c53b1230b1db6d991006506b7c96711dd8234 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Fri, 6 Jan 2023 15:06:11 +0100 Subject: [PATCH 4/6] Fix doc-string. Signed-off-by: Felix Schnabel --- monai/bundle/config_parser.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index 158c03a2ca..6b8ab8d810 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -118,11 +118,14 @@ def __repr__(self): def __getattr__(self, id): """ - Get the config by id. + Get the parsed result of ``ConfigItem`` with the specified ``id`` + with default arguments (e.g. ``lazy=True``, ``instantiate=True`` and ``eval_expr=True``). Args: - id: id to specify the expected position. See also :py:meth:`__getitem__`. - + id: id of the ``ConfigItem``. + + See also: + :py:meth:`get_parsed_content` """ return self.get_parsed_content(id) From abc1ae8dbb05ddc9c345a48e3bfb82072030e3be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:06:55 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/bundle/config_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index 6b8ab8d810..f1970b3758 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -123,7 +123,7 @@ def __getattr__(self, id): Args: id: id of the ``ConfigItem``. - + See also: :py:meth:`get_parsed_content` """ From c6192a742c05cda36bae11bf2033da78b7a8dd89 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 6 Jan 2023 14:11:53 +0000 Subject: [PATCH 6/6] display docstring Signed-off-by: Wenqi Li --- docs/source/bundle.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/bundle.rst b/docs/source/bundle.rst index 0977788ded..a429f446e9 100644 --- a/docs/source/bundle.rst +++ b/docs/source/bundle.rst @@ -32,6 +32,7 @@ Model Bundle --------------- .. autoclass:: ConfigParser :members: + :special-members: `Scripts` ---------