Skip to content
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

Rename optional_packages_version to required_packages_version #7253

Merged
merged 16 commits into from
Jul 30, 2024
4 changes: 2 additions & 2 deletions docs/source/mb_specification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ This file contains the metadata information relating to the model, including wha
* **monai_version**: version of MONAI the bundle was generated on, later versions expected to work.
* **pytorch_version**: version of Pytorch the bundle was generated on, later versions expected to work.
* **numpy_version**: version of Numpy the bundle was generated on, later versions expected to work.
* **optional_packages_version**: dictionary relating optional package names to their versions, these packages are not needed but are recommended to be installed with this stated minimum version.
* **required_packages_version**: dictionary relating required package names to their versions, these packages are needed to be installed with this stated minimum version.
* **task**: plain-language description of what the model is meant to do.
* **description**: longer form plain-language description of what the model is, what it does, etc.
* **authors**: state author(s) of the model.
Expand Down Expand Up @@ -124,7 +124,7 @@ An example JSON metadata file:
"monai_version": "0.9.0",
"pytorch_version": "1.10.0",
"numpy_version": "1.21.2",
"optional_packages_version": {"nibabel": "3.2.1"},
"required_packages_version": {"nibabel": "3.2.1"},
"task": "Decathlon spleen segmentation",
"description": "A pre-trained model for volumetric (3D) segmentation of the spleen from CT image",
"authors": "MONAI team",
Expand Down
4 changes: 2 additions & 2 deletions monai/bundle/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(
self.ref_resolver = ReferenceResolver()
if config is None:
config = {self.meta_key: {}}
self.set(config=config)
self.set(config=self.ref_resolver.normalize_meta_id(config))

def __repr__(self):
return f"{self.config}"
Expand Down Expand Up @@ -221,7 +221,7 @@ def set(self, config: Any, id: str = "", recursive: bool = True) -> None:
if isinstance(conf_, dict) and k not in conf_:
conf_[k] = {}
conf_ = conf_[k if isinstance(conf_, dict) else int(k)]
self[ReferenceResolver.normalize_id(id)] = config
self[ReferenceResolver.normalize_id(id)] = self.ref_resolver.normalize_meta_id(config)

def update(self, pairs: dict[str, Any]) -> None:
"""
Expand Down
17 changes: 16 additions & 1 deletion monai/bundle/reference_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any, Iterator

from monai.bundle.config_item import ConfigComponent, ConfigExpression, ConfigItem
from monai.bundle.utils import ID_REF_KEY, ID_SEP_KEY
from monai.bundle.utils import DEPRECATED_ID_MAPPING, ID_REF_KEY, ID_SEP_KEY
from monai.utils import allow_missing_reference, look_up_option

__all__ = ["ReferenceResolver"]
Expand Down Expand Up @@ -202,6 +202,21 @@ def normalize_id(cls, id: str | int) -> str:
"""
return str(id).replace("#", cls.sep) # backward compatibility `#` is the old separator

@classmethod
def normalize_meta_id(cls, config: Any) -> Any:
"""
Update deprecated id use `DEPRECATED_ID_MAPPING`.

Args:
config: input config to be updated.
"""
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(config, dict):
for _id, _new_id in DEPRECATED_ID_MAPPING.items():
if _id in config.keys():
warnings.warn(f"Detect deprecated id: {_id} in config, replacing with {_new_id}.")
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
config[_new_id] = config.pop(_id)
return config

@classmethod
def split_id(cls, id: str | int, last: bool = False) -> list[str]:
"""
Expand Down
4 changes: 3 additions & 1 deletion monai/bundle/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"monai_version": _conf_values["MONAI"],
"pytorch_version": str(_conf_values["Pytorch"]).split("+")[0].split("a")[0], # 1.9.0a0+df837d0 or 1.13.0+cu117
"numpy_version": _conf_values["Numpy"],
"optional_packages_version": {},
"required_packages_version": {},
"task": "Describe what the network predicts",
"description": "A longer description of what the network does, use context, inputs, outputs, etc.",
"authors": "Your Name Here",
Expand Down Expand Up @@ -157,6 +157,8 @@

DEFAULT_EXP_MGMT_SETTINGS = {"mlflow": DEFAULT_MLFLOW_SETTINGS} # default experiment management settings

DEPRECATED_ID_MAPPING = {"optional_packages_version": "required_packages_version"}


def load_bundle_config(bundle_path: str, *config_names: str, **load_kw_args: Any) -> Any:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/testing_data/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"monai_version": "0.9.0",
"pytorch_version": "1.10.0",
"numpy_version": "1.21.2",
"optional_packages_version": {
"required_packages_version": {
"nibabel": "3.2.1"
},
"task": "Decathlon spleen segmentation",
Expand Down
Loading