Skip to content

3482 3829 Add ConfigParser to recursively parse config content #3822

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

Merged
merged 32 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
42a45e0
Merge pull request #19 from Project-MONAI/master
Nic-Ma Feb 1, 2021
cd16a13
Merge pull request #32 from Project-MONAI/master
Nic-Ma Feb 24, 2021
6f87afd
Merge pull request #180 from Project-MONAI/dev
Nic-Ma Jul 22, 2021
f398298
Merge pull request #214 from Project-MONAI/dev
Nic-Ma Sep 8, 2021
85604cf
Merge pull request #373 from Project-MONAI/dev
Nic-Ma Feb 19, 2022
a3193ba
[DLMED] add config parser
Nic-Ma Feb 19, 2022
04e3ee7
[DLMED] simplify parse logic
Nic-Ma Feb 20, 2022
9abb571
[DLMED] add more function tests
Nic-Ma Feb 21, 2022
32aac56
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 21, 2022
fe3f7c2
[DLMED] fix torchvision tests
Nic-Ma Feb 21, 2022
d4cb4b0
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 21, 2022
886b607
[DLMED] enhance update_config to support multiple items
Nic-Ma Feb 21, 2022
b90b732
[DLMED] enhance doc and unit tests
Nic-Ma Feb 22, 2022
9056a9f
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 22, 2022
9292450
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 22, 2022
bfc74fc
[DLMED] clear API
Nic-Ma Feb 22, 2022
ac3e893
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 23, 2022
0551374
Merge branch 'dev' into 3482-add-config-parser
Nic-Ma Feb 23, 2022
362e336
[DLMED] simplify usage APIs
Nic-Ma Feb 23, 2022
e7559c5
[DLMED] support root config
Nic-Ma Feb 23, 2022
420346b
[DLMED] fix update reference typo
Nic-Ma Feb 23, 2022
3c1205c
revise APIs and docstrings
wyli Feb 23, 2022
43ce0e5
remove->discard
wyli Feb 23, 2022
b86f897
[DLMED] update code
Nic-Ma Feb 24, 2022
28c5c07
[DLMED] update doc
Nic-Ma Feb 24, 2022
7d2903f
update usage
wyli Feb 24, 2022
337c51d
update regex for <args>
wyli Feb 24, 2022
5b693bb
revise docstring
wyli Feb 24, 2022
4aadb09
refactor to not hard code special chars
wyli Feb 25, 2022
eabfff0
flag to switch eval expr
wyli Feb 25, 2022
5b1fae6
[DLMED] change apps.manifest to bundle
Nic-Ma Mar 1, 2022
b6020d9
[DLMED] add missing component
Nic-Ma Mar 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ API Reference
:maxdepth: 1

apps
bundle
transforms
losses
networks
Expand Down
23 changes: 2 additions & 21 deletions docs/source/apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Applications
:members:


Clara MMARs
-----------
`Clara MMARs`
-------------
.. autofunction:: download_mmar

.. autofunction:: load_from_mmar
Expand All @@ -29,25 +29,6 @@ Clara MMARs
:annotation:


Model Manifest
--------------

.. autoclass:: ComponentLocator
:members:

.. autoclass:: ConfigComponent
:members:

.. autoclass:: ConfigExpression
:members:

.. autoclass:: ConfigItem
:members:

.. autoclass:: ReferenceResolver
:members:


`Utilities`
-----------

Expand Down
34 changes: 34 additions & 0 deletions docs/source/bundle.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
:github_url: https://github.com/Project-MONAI/MONAI

.. _bundle:

Model Bundle
============
.. currentmodule:: monai.bundle

`Config Item`
-------------
.. autoclass:: Instantiable
:members:

.. autoclass:: ComponentLocator
:members:

.. autoclass:: ConfigComponent
:members:

.. autoclass:: ConfigExpression
:members:

.. autoclass:: ConfigItem
:members:

`Reference Resolver`
--------------------
.. autoclass:: ReferenceResolver
:members:

`Config Parser`
---------------
.. autoclass:: ConfigParser
:members:
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"engines",
"data",
"apps",
"bundle",
"config",
"handlers",
"losses",
Expand Down
2 changes: 2 additions & 0 deletions monai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* **apps**: high level medical domain specific deep learning applications.

* **bundle**: components to build the portable self-descriptive model bundle.

* **config**: for system configuration and diagnostic output.

* **csrc**: for C++/CUDA extensions.
Expand Down
1 change: 1 addition & 0 deletions monai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

__all__ = [
"apps",
"bundle",
"config",
"data",
"engines",
Expand Down
1 change: 0 additions & 1 deletion monai/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
# limitations under the License.

from .datasets import CrossValidation, DecathlonDataset, MedNISTDataset
from .manifest import ComponentLocator, ConfigComponent, ConfigExpression, ConfigItem, ReferenceResolver
from .mmars import MODEL_DESC, RemoteMMARKeys, download_mmar, get_model_spec, load_from_mmar
from .utils import SUPPORTED_HASH_TYPES, check_hash, download_and_extract, download_url, extractall, get_logger, logger
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .config_item import ComponentLocator, ConfigComponent, ConfigExpression, ConfigItem
from .config_item import ComponentLocator, ConfigComponent, ConfigExpression, ConfigItem, Instantiable
from .config_parser import ConfigParser
from .reference_resolver import ReferenceResolver
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# limitations under the License.

import inspect
import os
import sys
import warnings
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -128,7 +129,7 @@ def __init__(self, config: Any, id: str = "") -> None:
self.config = config
self.id = id

def get_id(self) -> Optional[str]:
def get_id(self) -> str:
"""
Get the ID name of current config item, useful to identify config items during parsing.

Expand All @@ -153,6 +154,9 @@ def get_config(self):
"""
return self.config

def __repr__(self) -> str:
return str(self.config)


class ConfigComponent(ConfigItem, Instantiable):
"""
Expand Down Expand Up @@ -187,7 +191,7 @@ class ConfigComponent(ConfigItem, Instantiable):
locator: a ``ComponentLocator`` to convert a module name string into the actual python module.
if `None`, a ``ComponentLocator(excludes=excludes)`` will be used.
excludes: if ``locator`` is None, create a new ``ComponentLocator`` with ``excludes``.
See also: :py:class:`monai.apps.manifest.ComponentLocator`.
See also: :py:class:`monai.bundle.ComponentLocator`.

"""

Expand Down Expand Up @@ -291,7 +295,7 @@ class ConfigExpression(ConfigItem):
.. code-block:: python

import monai
from monai.apps.manifest import ConfigExpression
from monai.bundle import ConfigExpression

config = "$monai.__version__"
expression = ConfigExpression(config, id="test", globals={"monai": monai})
Expand All @@ -304,6 +308,9 @@ class ConfigExpression(ConfigItem):

"""

prefix = "$"
run_eval = False if os.environ.get("MONAI_EVAL_EXPR", "1") == "0" else True

def __init__(self, config: Any, id: str = "", globals: Optional[Dict] = None) -> None:
super().__init__(config=config, id=id)
self.globals = globals
Expand All @@ -320,10 +327,12 @@ def evaluate(self, locals: Optional[Dict] = None):
value = self.get_config()
if not ConfigExpression.is_expression(value):
return None
return eval(value[1:], self.globals, locals)
if not self.run_eval:
return f"{value[len(self.prefix) :]}"
return eval(value[len(self.prefix) :], self.globals, locals)

@staticmethod
def is_expression(config: Union[Dict, List, str]) -> bool:
@classmethod
def is_expression(cls, config: Union[Dict, List, str]) -> bool:
"""
Check whether the config is an executable expression string.
Currently, a string starts with ``"$"`` character is interpreted as an expression.
Expand All @@ -332,4 +341,4 @@ def is_expression(config: Union[Dict, List, str]) -> bool:
config: input config content to check.

"""
return isinstance(config, str) and config.startswith("$")
return isinstance(config, str) and config.startswith(cls.prefix)
Loading