-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simplify the configparser usage to get parsed attributes easily #5804
Comments
Hi, I would like to work on this issue. |
While searching through the files, I think I found a typo: https://github.com/Project-MONAI/research-contributions/blob/07da7ad36a0b218556a427f3bc5292a772c30f60/auto3dseg/algorithm_templates/dints/scripts/infer.py#L48 |
I think I got the issue wrong, |
My first intuition was to implement it the following way: def __getattr__(self, key):
"""
Get the config by id.
Args:
key: id to specify the expected position. See also :py:meth:`__getitem__`.
"""
return self[key] Now the implementation just forwards the call to So the method won't be called if a defined method/property is tried to be accessed (e.g. One problem with this implementation is, that it only works one layer deep, so this doesn't work: from monai.bundle.config_parser import ConfigParser
x = ConfigParser({'a':{'b':"c"}, 1:2})
x.a.b # AttributeError: 'dict' object has no attribute 'b' |
sounds great, I'm assigning this to you , it's ok if the first PR only works for the shallow structure...I haven't thought in detail about the nested case but we can always follow up and improve it. (also thanks for reporting the typo!) cc @Nic-Ma |
Sounds good for the initial PR idea, I think we can think about the nested case together later. Thanks. |
…ect-MONAI#5804) Signed-off-by: Felix Schnabel <[email protected]>
…5813) part of #5804. ### Description This adds a simple attribute access for ConfigParser so ```python from monai.bundle import ConfigParser parser = ConfigParser({"a":"b"}) a = parser.get_parsed_content("a") ``` can be rewritten to ```python from monai.bundle import ConfigParser parser = ConfigParser({"a":"b"}) a = parser.a ``` This only works for variables/methods that are not included in ConfigParser so e.g. `parser.config` would still get the whole config. This PR only supports shallow attribute accesses, since the returned value will be a `dict` where you need to use `["key"]` to get the content. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [x] New tests added to cover the changes. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. Signed-off-by: Felix Schnabel <[email protected]>
Is your feature request related to a problem? Please describe.
the current usage looks tedious (https://github.com/Project-MONAI/research-contributions/blob/07da7ad36a0b218556a427f3bc5292a772c30f60/auto3dseg/algorithm_templates/segresnet2d/scripts/infer.py#L34-L50)
this is a feature request of simplifying the APIs to support more user-friendly calls
to get the parsed config as attributes directly:
cc @Project-MONAI/core-reviewers
The text was updated successfully, but these errors were encountered: