From 62883db8efcbc0e81e64b85c8878d2abc19c5eb4 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 22 Mar 2022 22:46:01 +0000 Subject: [PATCH 01/17] update bundle doc Signed-off-by: Wenqi Li --- docs/source/bundle_intro.rst | 10 +++++++++ docs/source/config_syntax.md | 40 +++++++++++++++++++++++++++++++++++ docs/source/index.rst | 2 +- monai/bundle/config_item.py | 2 +- monai/bundle/config_parser.py | 8 +++++-- 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 docs/source/bundle_intro.rst create mode 100644 docs/source/config_syntax.md diff --git a/docs/source/bundle_intro.rst b/docs/source/bundle_intro.rst new file mode 100644 index 0000000000..3d71de24b6 --- /dev/null +++ b/docs/source/bundle_intro.rst @@ -0,0 +1,10 @@ +:github_url: https://github.com/Project-MONAI/MONAI + +Bundle +====== + +.. toctree:: + :maxdepth: 1 + + mb_specification + config_syntax.md diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md new file mode 100644 index 0000000000..55a5d9c539 --- /dev/null +++ b/docs/source/config_syntax.md @@ -0,0 +1,40 @@ +# MONAI Bundle Configuration + +The `monai.bundle` module supports building Python-based workflows via structured configurations. + +The main benefits are threefold: + - it provides good readability and usability by separating system parameter settings from the Python code. + - it describes workflow at a relatively high level and allows for different low-level implementations. + - learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. + +Components as part of a workflow can be specified using `JSON` or `YAML` syntax, for example, a network architecture +definition could be stored in a `demo_config.json` file with the following content: +```json +{ + "demo_net": { + "_target_": "monai.networks.nets.BasicUNet", + "spatial_dims": 3, + "in_channels": 1, + "out_channels": 2, + "features": [16, 16, 32, 32, 64, 64] + } +} +``` + +The configuration parser can instantiate the components as a Python object: +```py +>>> from monai.bundle import ConfigParser +>>> config = ConfigParser() +>>> config.read_config("demo_config.json") +>>> net = config.get_parsed_content("demo_net", instantiate=True) +BasicUNet features: (16, 16, 32, 32, 64, 64). +>>> print(type(net)) + +``` +or additionally tune the input parameters then : +```py +>>> config["demo_net"]["features"] = [32, 32, 32, 64, 64, 64] +>>> net = config.get_parsed_content("demo_net", instantiate=True) +BasicUNet features: (32, 32, 32, 64, 64, 64). +``` + diff --git a/docs/source/index.rst b/docs/source/index.rst index 1a4263db0d..6c99feac11 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -70,7 +70,7 @@ Technical documentation is available at `docs.monai.io `_ :maxdepth: 1 :caption: Specifications - mb_specification + bundle_intro Links ----- diff --git a/monai/bundle/config_item.py b/monai/bundle/config_item.py index 0531c6f14e..4509a2de05 100644 --- a/monai/bundle/config_item.py +++ b/monai/bundle/config_item.py @@ -302,7 +302,7 @@ class ConfigExpression(ConfigItem): config = "$monai.__version__" expression = ConfigExpression(config, id="test", globals={"monai": monai}) - print(expression.execute()) + print(expression.evaluate()) Args: config: content of a config item. diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index 23d4ac7c55..33b5e68d14 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -209,12 +209,16 @@ def get_parsed_content(self, id: str = "", **kwargs): Use digits indexing from "0" for list or other strings for dict. For example: ``"xform#5"``, ``"net#channels"``. ``""`` indicates the entire ``self.config``. kwargs: additional keyword arguments to be passed to ``_resolve_one_item``. - Currently support ``reset`` (for parse), ``instantiate`` and ``eval_expr``. All defaulting to True. + Currently support ``lazy`` (whether to retain the current config cache, default to `False`), + ``instantiate`` (whether to instantiate the `ConfigComponent`, default to `True`) and + ``eval_expr`` (whether to evaluate the `ConfigExpression`, default to `True`). """ if not self.ref_resolver.is_resolved(): # not parsed the config source yet, parse it - self.parse(kwargs.get("reset", True)) + self.parse(reset=True) + elif not kwargs.get("lazy", False): + self.parse(reset=not kwargs.get("lazy", False)) return self.ref_resolver.get_resolved_content(id=id, **kwargs) def read_meta(self, f: Union[PathLike, Sequence[PathLike], Dict], **kwargs): From 1d45d68752d10089d997e132eaa07014c4d75b1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Mar 2022 22:47:09 +0000 Subject: [PATCH 02/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/config_syntax.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 55a5d9c539..95c41a6955 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -37,4 +37,3 @@ or additionally tune the input parameters then : >>> net = config.get_parsed_content("demo_net", instantiate=True) BasicUNet features: (32, 32, 32, 64, 64, 64). ``` - From 552164ce334fed44900c17be18266540c4d7c505 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 24 Mar 2022 16:28:15 +0000 Subject: [PATCH 03/17] update meta Signed-off-by: Wenqi Li --- tests/testing_data/metadata.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testing_data/metadata.json b/tests/testing_data/metadata.json index 42a55b114c..c12ae411f1 100644 --- a/tests/testing_data/metadata.json +++ b/tests/testing_data/metadata.json @@ -1,5 +1,5 @@ { - "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_202203171008.json", + "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json", "version": "0.1.0", "changelog": { "0.1.0": "complete the model package", @@ -13,7 +13,7 @@ }, "task": "Decathlon spleen segmentation", "description": "A pre-trained model for volumetric (3D) segmentation of the spleen from CT image", - "authorship": "MONAI team", + "authors": "MONAI team", "copyright": "Copyright (c) MONAI Consortium", "data_source": "Task09_Spleen.tar from http://medicaldecathlon.com/", "data_type": "dicom", From 0461f167a25612bcc0cb77c58120641fb03f114d Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 24 Mar 2022 20:14:03 +0000 Subject: [PATCH 04/17] update intro Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 95c41a6955..9a0d4fb597 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -3,12 +3,14 @@ The `monai.bundle` module supports building Python-based workflows via structured configurations. The main benefits are threefold: - - it provides good readability and usability by separating system parameter settings from the Python code. - - it describes workflow at a relatively high level and allows for different low-level implementations. - - learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. + +- it provides good readability and usability by separating system parameter settings from the Python code. +- it describes workflow at a relatively high level and allows for different low-level implementations. +- learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. Components as part of a workflow can be specified using `JSON` or `YAML` syntax, for example, a network architecture definition could be stored in a `demo_config.json` file with the following content: + ```json { "demo_net": { @@ -21,7 +23,8 @@ definition could be stored in a `demo_config.json` file with the following conte } ``` -The configuration parser can instantiate the components as a Python object: +The configuration parser can instantiate the component as a Python object: + ```py >>> from monai.bundle import ConfigParser >>> config = ConfigParser() @@ -31,7 +34,9 @@ BasicUNet features: (16, 16, 32, 32, 64, 64). >>> print(type(net)) ``` -or additionally tune the input parameters then : + +or additionally, tune the input parameters then instantiate the component: + ```py >>> config["demo_net"]["features"] = [32, 32, 32, 64, 64, 64] >>> net = config.get_parsed_content("demo_net", instantiate=True) From 20d35c6010dbe3231686f5082aada2099f79a850 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 24 Mar 2022 23:18:21 +0000 Subject: [PATCH 05/17] update examples Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 71 +++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 9a0d4fb597..33193aa419 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -8,6 +8,8 @@ The main benefits are threefold: - it describes workflow at a relatively high level and allows for different low-level implementations. - learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. +## A basic example + Components as part of a workflow can be specified using `JSON` or `YAML` syntax, for example, a network architecture definition could be stored in a `demo_config.json` file with the following content: @@ -27,7 +29,7 @@ The configuration parser can instantiate the component as a Python object: ```py >>> from monai.bundle import ConfigParser ->>> config = ConfigParser() +>>> config = monai.bundle.ConfigParser() >>> config.read_config("demo_config.json") >>> net = config.get_parsed_content("demo_net", instantiate=True) BasicUNet features: (16, 16, 32, 32, 64, 64). @@ -42,3 +44,70 @@ or additionally, tune the input parameters then instantiate the component: >>> net = config.get_parsed_content("demo_net", instantiate=True) BasicUNet features: (32, 32, 32, 64, 64, 64). ``` + +## Syntax examples explained + +A few characters and keywords are interpreted beyond the plain texts, here are examples of the syntax: + +```json +"@preprocessing#transforms#keys" +``` + +_Description:_ A reference to another configuration value defined at `preprocessing#transforms#keys`. +where `#` indicates a sub-structure of this configuration file. + +```json +"@preprocessing#1" +``` + +_Description:_ `1` is interpreted as an integer, which is used to index the `preprocessing` sub-structure. + +```json +"$print(42)" +``` + +_Description:_ `$` is a special character to indicate evaluating `print(42)` at runtime. + +```json +"$[i for i in @datalist]" +``` + +_Description:_ Create a list at runtime using the values in `datalist` as input. + +```json +{ + "demo_name":{ + "_target_": "my.python.module.Class", + "args1": "string", + "args2": 42} +} +``` + +_Description:_ This dictionary defines an object with a reference name `demo_name`, with an instantiable type +specified at `_target_` and with input arguments `args1` and `args2`. +This dictionary will be instantiated as a Pytorch object at runtime. +`_target_` is a required key by monai bundle syntax for the Python object name. +`args1` and `args2` should be compatible with the Python object to instantiate. + +```json +"%demo_config.json#demo_net#in_channels" +``` + +_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the +`demo_config.json` file. + +```json +{ + "component_name": { + "_target_": "my.module.Class", + "_requires_": "@cudnn_opt", + "_disabled_": "true"} +} +``` + +_Description:_ `_requires_` and `_disabled_` are optional keys. +`_requires_` specifies references (string starts with `@`) or +Python expression that will be evaluated/instantiated before `_target_` object is instantiated. +It is useful when the component doesn’t explicitly depends on the other ConfigItems via +its arguments, but requires the dependencies to be instantiated/evaluated beforehand. +`_disabled_` specifies a flag to indicate whether to skip the instantiation. From 43ded13dcb94fb3963166067a526d844920a4db1 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 29 Mar 2022 17:00:38 +0100 Subject: [PATCH 06/17] adds yaml demo Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 33193aa419..629cb91add 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -25,11 +25,28 @@ definition could be stored in a `demo_config.json` file with the following conte } ``` +or alternatively, in `YAML` format (`demo_config.yaml`): + +```yaml +demo_net: + _target_: monai.networks.nets.BasicUNet + spatial_dims: 3 + in_channels: 1 + out_channels: 2 + features: + - 16 + - 16 + - 32 + - 32 + - 64 + - 64 +``` + The configuration parser can instantiate the component as a Python object: ```py >>> from monai.bundle import ConfigParser ->>> config = monai.bundle.ConfigParser() +>>> config = ConfigParser() >>> config.read_config("demo_config.json") >>> net = config.get_parsed_content("demo_net", instantiate=True) BasicUNet features: (16, 16, 32, 32, 64, 64). From 99f83ad31610aa1afd58eac4341165752353085d Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 29 Mar 2022 17:33:30 +0100 Subject: [PATCH 07/17] update schema Signed-off-by: Wenqi Li --- tests/testing_data/data_config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testing_data/data_config.json b/tests/testing_data/data_config.json index 8bcdcd244e..e337a2d2e7 100644 --- a/tests/testing_data/data_config.json +++ b/tests/testing_data/data_config.json @@ -70,9 +70,9 @@ }, "configs": { "test_meta_file": { - "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_202203171008.json", + "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json", "hash_type": "md5", - "hash_val": "e3a7e23d1113a1f3e6c69f09b6f9ce2c" + "hash_val": "e12813de2c15672a8c8fa8466b3dfc95" } } } From e0ea5921e8ecdffba43ecd6b267e82bae7c0ff85 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 29 Mar 2022 17:48:59 +0100 Subject: [PATCH 08/17] update toc Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 629cb91add..c870c18b1a 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -8,6 +8,15 @@ The main benefits are threefold: - it describes workflow at a relatively high level and allows for different low-level implementations. - learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. +Content: + +- [A basic example](#a-basic-example) +- [Syntax examples explained](#syntax-examples-explained) + - `@` to interpolate with Python objects + - `$` to evaluate as Python expressions + - `_target_` to instantiate a Python object + - `%` to interpolate with plain texts + ## A basic example Components as part of a workflow can be specified using `JSON` or `YAML` syntax, for example, a network architecture @@ -62,10 +71,14 @@ or additionally, tune the input parameters then instantiate the component: BasicUNet features: (32, 32, 32, 64, 64, 64). ``` +For more details on the `ConfigParser` API, please see https://docs.monai.io/en/latest/bundle.html#config-parser. + ## Syntax examples explained A few characters and keywords are interpreted beyond the plain texts, here are examples of the syntax: +### `@` to interpolate with Python objects + ```json "@preprocessing#transforms#keys" ``` @@ -79,6 +92,8 @@ where `#` indicates a sub-structure of this configuration file. _Description:_ `1` is interpreted as an integer, which is used to index the `preprocessing` sub-structure. +### `$` to evaluate as Python expressions + ```json "$print(42)" ``` @@ -91,6 +106,8 @@ _Description:_ `$` is a special character to indicate evaluating `print(42)` at _Description:_ Create a list at runtime using the values in `datalist` as input. +### `_target_` to instantiate a Python object + ```json { "demo_name":{ @@ -106,13 +123,6 @@ This dictionary will be instantiated as a Pytorch object at runtime. `_target_` is a required key by monai bundle syntax for the Python object name. `args1` and `args2` should be compatible with the Python object to instantiate. -```json -"%demo_config.json#demo_net#in_channels" -``` - -_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the -`demo_config.json` file. - ```json { "component_name": { @@ -128,3 +138,12 @@ Python expression that will be evaluated/instantiated before `_target_` object i It is useful when the component doesn’t explicitly depends on the other ConfigItems via its arguments, but requires the dependencies to be instantiated/evaluated beforehand. `_disabled_` specifies a flag to indicate whether to skip the instantiation. + +### `%` to interpolate with plain texts + +```json +"%demo_config.json#demo_net#in_channels" +``` + +_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the +`demo_config.json` file. \ No newline at end of file From 291478786082143bb37cbccb3965fd6d86ecf74a Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 29 Mar 2022 19:59:20 +0100 Subject: [PATCH 09/17] update doc page Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 43 ++++++++++++++++++++++++-------- docs/source/mb_specification.rst | 2 +- monai/bundle/config_item.py | 4 +-- monai/bundle/scripts.py | 2 +- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index c870c18b1a..4b5df3390a 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -12,10 +12,11 @@ Content: - [A basic example](#a-basic-example) - [Syntax examples explained](#syntax-examples-explained) - - `@` to interpolate with Python objects - - `$` to evaluate as Python expressions - - `_target_` to instantiate a Python object - - `%` to interpolate with plain texts + - [`@` to interpolate with Python objects](#-to-interpolate-with-python-objects) + - [`$` to evaluate as Python expressions](#-to-evaluate-as-python-expressions) + - [`%` to interpolate with plain texts](#-to-interpolate-with-plain-texts) + - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#instantiate-a-python-object) +- [The command line interface](#the-command-line-interface) ## A basic example @@ -106,7 +107,16 @@ _Description:_ `$` is a special character to indicate evaluating `print(42)` at _Description:_ Create a list at runtime using the values in `datalist` as input. -### `_target_` to instantiate a Python object +### `%` to interpolate with plain texts + +```json +"%demo_config.json#demo_net#in_channels" +``` + +_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the +`demo_config.json` file. + +### instantiate a Python object ```json { @@ -135,15 +145,26 @@ This dictionary will be instantiated as a Pytorch object at runtime. _Description:_ `_requires_` and `_disabled_` are optional keys. `_requires_` specifies references (string starts with `@`) or Python expression that will be evaluated/instantiated before `_target_` object is instantiated. -It is useful when the component doesn’t explicitly depends on the other ConfigItems via +It is useful when the component does not explicitly depend on the other ConfigItems via its arguments, but requires the dependencies to be instantiated/evaluated beforehand. `_disabled_` specifies a flag to indicate whether to skip the instantiation. -### `%` to interpolate with plain texts +## The command line interface -```json -"%demo_config.json#demo_net#in_channels" +In addition to the Pythonic APIs, a few command line interfaces are provided to interact with the bundle. +The primary API is: +```bash +python -m monai.bundle COMMANDS ``` -_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the -`demo_config.json` file. \ No newline at end of file +where `COMMANDS` is one of the following: `run`, `verify_metadata`, `ckpt_export`, ... +(please see `python -m monai.bundle --help` for a list of available commands). + +To display a usage page for a command, for example `run`: +```bash +python -m monai.bundle run -- --help +``` + +The support is provided by [Python Fire](https://github.com/google/python-fire), please +make sure the optional dependency is installed, for example, +using `pip install monai[fire]` or `pip install fire`. \ No newline at end of file diff --git a/docs/source/mb_specification.rst b/docs/source/mb_specification.rst index cbd374ae11..5bdfa148e2 100644 --- a/docs/source/mb_specification.rst +++ b/docs/source/mb_specification.rst @@ -13,7 +13,7 @@ This specification defines the directory structure a bundle must have and the ne Directory Structure =================== -A MONAI Bundle is defined primarily as a directory with a set of specifically named subdirectories containing the model and metadata files. The root directory should be named for the model, given as "ModelName" in this exmaple, and should contain the following structure: +A MONAI Bundle is defined primarily as a directory with a set of specifically named subdirectories containing the model and metadata files. The root directory should be named for the model, given as "ModelName" in this example, and should contain the following structure: :: diff --git a/monai/bundle/config_item.py b/monai/bundle/config_item.py index 4509a2de05..3300fe91ff 100644 --- a/monai/bundle/config_item.py +++ b/monai/bundle/config_item.py @@ -21,7 +21,7 @@ from monai.bundle.utils import EXPR_KEY from monai.utils import ensure_tuple, first, instantiate, optional_import -__all__ = ["ComponentLocator", "ConfigItem", "ConfigExpression", "ConfigComponent"] +__all__ = ["ComponentLocator", "ConfigItem", "ConfigExpression", "ConfigComponent", "Instantiable"] class Instantiable(ABC): @@ -173,7 +173,7 @@ class ConfigComponent(ConfigItem, Instantiable): - ``"_requires_"`` (optional): specifies reference IDs (string starts with ``"@"``) or ``ConfigExpression`` of the dependencies for this ``ConfigComponent`` object. These dependencies will be evaluated/instantiated before this object is instantiated. It is useful when the - component doesn't explicitly depends on the other `ConfigItems` via its arguments, + component doesn't explicitly depend on the other `ConfigItems` via its arguments, but requires the dependencies to be instantiated/evaluated beforehand. - ``"_disabled_"`` (optional): a flag to indicate whether to skip the instantiation. diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 894fbd6c25..64172c4541 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -207,7 +207,7 @@ def verify_metadata( ): """ Verify the provided `metadata` file based on the predefined `schema`. - `metadata` content must contain the `schema` field for the URL of shcema file to download. + `metadata` content must contain the `schema` field for the URL of schema file to download. The schema standard follows: http://json-schema.org/. Args: From 17190e5cad069520e9219ec89827b333294a433c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Mar 2022 19:00:01 +0000 Subject: [PATCH 10/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/config_syntax.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 4b5df3390a..b3edfe1541 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -166,5 +166,5 @@ python -m monai.bundle run -- --help ``` The support is provided by [Python Fire](https://github.com/google/python-fire), please -make sure the optional dependency is installed, for example, -using `pip install monai[fire]` or `pip install fire`. \ No newline at end of file +make sure the optional dependency is installed, for example, +using `pip install monai[fire]` or `pip install fire`. From 41e01edcbb696f9ca7ed212892922db6dada761e Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 30 Mar 2022 10:00:47 +0100 Subject: [PATCH 11/17] $import Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index b3edfe1541..db64356feb 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -12,10 +12,10 @@ Content: - [A basic example](#a-basic-example) - [Syntax examples explained](#syntax-examples-explained) - - [`@` to interpolate with Python objects](#-to-interpolate-with-python-objects) - - [`$` to evaluate as Python expressions](#-to-evaluate-as-python-expressions) - - [`%` to interpolate with plain texts](#-to-interpolate-with-plain-texts) - - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#instantiate-a-python-object) + - [`@` to interpolate with Python objects](#1--to-interpolate-with-python-objects) + - [`$` to evaluate as Python expressions](#2--to-evaluate-as-python-expressions) + - [`%` to interpolate with plain texts](#3--to-interpolate-with-plain-texts) + - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#4-instantiate-a-python-object) - [The command line interface](#the-command-line-interface) ## A basic example @@ -78,7 +78,7 @@ For more details on the `ConfigParser` API, please see https://docs.monai.io/en/ A few characters and keywords are interpreted beyond the plain texts, here are examples of the syntax: -### `@` to interpolate with Python objects +### 1. `@` to interpolate with Python objects ```json "@preprocessing#transforms#keys" @@ -93,7 +93,7 @@ where `#` indicates a sub-structure of this configuration file. _Description:_ `1` is interpreted as an integer, which is used to index the `preprocessing` sub-structure. -### `$` to evaluate as Python expressions +### 2. `$` to evaluate as Python expressions ```json "$print(42)" @@ -107,7 +107,15 @@ _Description:_ `$` is a special character to indicate evaluating `print(42)` at _Description:_ Create a list at runtime using the values in `datalist` as input. -### `%` to interpolate with plain texts +```json +"$from torchvision.models import resnet18" +``` + +_Description:_ `$` followed by an import statement is handled slightly differently from the +Python expressions. The imported module `resnet18` will be available as a global variable +to the other configuration sections. This is to simplify the use of external modules in the configuration. + +### 3. `%` to interpolate with plain texts ```json "%demo_config.json#demo_net#in_channels" @@ -116,7 +124,7 @@ _Description:_ Create a list at runtime using the values in `datalist` as input. _Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the `demo_config.json` file. -### instantiate a Python object +### 4. instantiate a Python object ```json { @@ -151,14 +159,14 @@ its arguments, but requires the dependencies to be instantiated/evaluated before ## The command line interface -In addition to the Pythonic APIs, a few command line interfaces are provided to interact with the bundle. -The primary API is: +In addition to the Pythonic APIs, a few command line interfaces (CLI) are provided to interact with the bundle. +The primary usage is: ```bash python -m monai.bundle COMMANDS ``` where `COMMANDS` is one of the following: `run`, `verify_metadata`, `ckpt_export`, ... -(please see `python -m monai.bundle --help` for a list of available commands). +(please see `python -m monai.bundle --help` for a list of available options). To display a usage page for a command, for example `run`: ```bash From d927a28a0112335980571a3dbd24f8d15f5ca73b Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 30 Mar 2022 10:57:10 +0100 Subject: [PATCH 12/17] adds recommendations Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index db64356feb..49f1b3f0d4 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -17,6 +17,7 @@ Content: - [`%` to interpolate with plain texts](#3--to-interpolate-with-plain-texts) - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#4-instantiate-a-python-object) - [The command line interface](#the-command-line-interface) +- [Recommendations](#recommendations) ## A basic example @@ -176,3 +177,11 @@ python -m monai.bundle run -- --help The support is provided by [Python Fire](https://github.com/google/python-fire), please make sure the optional dependency is installed, for example, using `pip install monai[fire]` or `pip install fire`. + +## Recommendations +- Both `YAML` and `JSON` are supported, but the advanced features of these formats are not supported. +- Using meaningful names for the configuration elements can improve the readability. +- While it is possible to build complex configurations with the bundle syntax, + simple structures with sparse uses of expressions or references are preferred. +- For `$import ` in the configuration, please make sure there are instructions for the users to install + the `` if it is not a (optional) dependency of MONAI. \ No newline at end of file From 69a89b3b1246607e1ce1a5f0dc4b1ad5d68f5f6e Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 30 Mar 2022 12:06:03 +0100 Subject: [PATCH 13/17] update config Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 49f1b3f0d4..2109197a94 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -44,13 +44,7 @@ demo_net: spatial_dims: 3 in_channels: 1 out_channels: 2 - features: - - 16 - - 16 - - 32 - - 32 - - 64 - - 64 + features: [16, 16, 32, 32, 64, 64] ``` The configuration parser can instantiate the component as a Python object: @@ -123,7 +117,7 @@ to the other configuration sections. This is to simplify the use of external mod ``` _Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the -`demo_config.json` file. +`demo_config.json` file. The replacement is done before instantiating or evaluating the components. ### 4. instantiate a Python object From 5083874109b671637c075e137f12e908c194e862 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 30 Mar 2022 13:00:13 +0100 Subject: [PATCH 14/17] update based on comments Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 2109197a94..869ad6f89d 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -14,7 +14,7 @@ Content: - [Syntax examples explained](#syntax-examples-explained) - [`@` to interpolate with Python objects](#1--to-interpolate-with-python-objects) - [`$` to evaluate as Python expressions](#2--to-evaluate-as-python-expressions) - - [`%` to interpolate with plain texts](#3--to-interpolate-with-plain-texts) + - [`%` to textually replace configuration elements](#3-to-textually-replace-configuration-elements) - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#4-instantiate-a-python-object) - [The command line interface](#the-command-line-interface) - [Recommendations](#recommendations) @@ -86,7 +86,7 @@ where `#` indicates a sub-structure of this configuration file. "@preprocessing#1" ``` -_Description:_ `1` is interpreted as an integer, which is used to index the `preprocessing` sub-structure. +_Description:_ `1` is interpreted as an integer, which is used to index (zero-based indexing) the `preprocessing` sub-structure. ### 2. `$` to evaluate as Python expressions @@ -110,13 +110,13 @@ _Description:_ `$` followed by an import statement is handled slightly different Python expressions. The imported module `resnet18` will be available as a global variable to the other configuration sections. This is to simplify the use of external modules in the configuration. -### 3. `%` to interpolate with plain texts +### 3. `%` to textually replace configuration elements ```json "%demo_config.json#demo_net#in_channels" ``` -_Description:_ A macro to replace the current value with the texts at `demo_net#in_channels` in the +_Description:_ A macro to replace the current configuration element with the texts at `demo_net#in_channels` in the `demo_config.json` file. The replacement is done before instantiating or evaluating the components. ### 4. instantiate a Python object @@ -133,6 +133,7 @@ _Description:_ A macro to replace the current value with the texts at `demo_net# _Description:_ This dictionary defines an object with a reference name `demo_name`, with an instantiable type specified at `_target_` and with input arguments `args1` and `args2`. This dictionary will be instantiated as a Pytorch object at runtime. + `_target_` is a required key by monai bundle syntax for the Python object name. `args1` and `args2` should be compatible with the Python object to instantiate. From 7a639a72e9f92315668ce13591750c580c53da2d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Mar 2022 12:00:46 +0000 Subject: [PATCH 15/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/config_syntax.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 869ad6f89d..f3d99613e5 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -176,7 +176,7 @@ using `pip install monai[fire]` or `pip install fire`. ## Recommendations - Both `YAML` and `JSON` are supported, but the advanced features of these formats are not supported. - Using meaningful names for the configuration elements can improve the readability. -- While it is possible to build complex configurations with the bundle syntax, +- While it is possible to build complex configurations with the bundle syntax, simple structures with sparse uses of expressions or references are preferred. - For `$import ` in the configuration, please make sure there are instructions for the users to install - the `` if it is not a (optional) dependency of MONAI. \ No newline at end of file + the `` if it is not a (optional) dependency of MONAI. From 76b1a8d9614bcb5258ea951fce9741dac9b0558b Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 30 Mar 2022 13:05:20 +0100 Subject: [PATCH 16/17] adds a link on CLI parsing Signed-off-by: Wenqi Li --- docs/source/config_syntax.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index f3d99613e5..06c69991e3 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -14,7 +14,7 @@ Content: - [Syntax examples explained](#syntax-examples-explained) - [`@` to interpolate with Python objects](#1--to-interpolate-with-python-objects) - [`$` to evaluate as Python expressions](#2--to-evaluate-as-python-expressions) - - [`%` to textually replace configuration elements](#3-to-textually-replace-configuration-elements) + - [`%` to textually replace configuration elements](#3--to-textually-replace-configuration-elements) - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#4-instantiate-a-python-object) - [The command line interface](#the-command-line-interface) - [Recommendations](#recommendations) @@ -172,6 +172,8 @@ python -m monai.bundle run -- --help The support is provided by [Python Fire](https://github.com/google/python-fire), please make sure the optional dependency is installed, for example, using `pip install monai[fire]` or `pip install fire`. +Details on the CLI argument parsing is provided in the +[Python Fire Guide](https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing). ## Recommendations - Both `YAML` and `JSON` are supported, but the advanced features of these formats are not supported. From 874135ba69f550e5b8b1b5b4688550af3d3a03dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Mar 2022 12:20:45 +0000 Subject: [PATCH 17/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/config_syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/config_syntax.md b/docs/source/config_syntax.md index 06c69991e3..bcd849cd09 100644 --- a/docs/source/config_syntax.md +++ b/docs/source/config_syntax.md @@ -172,7 +172,7 @@ python -m monai.bundle run -- --help The support is provided by [Python Fire](https://github.com/google/python-fire), please make sure the optional dependency is installed, for example, using `pip install monai[fire]` or `pip install fire`. -Details on the CLI argument parsing is provided in the +Details on the CLI argument parsing is provided in the [Python Fire Guide](https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing). ## Recommendations