diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index 829036af6f..a2ffeedc92 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -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}" @@ -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: """ diff --git a/monai/bundle/reference_resolver.py b/monai/bundle/reference_resolver.py index b36f2cc4a5..050cd75fa7 100644 --- a/monai/bundle/reference_resolver.py +++ b/monai/bundle/reference_resolver.py @@ -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"] @@ -202,6 +202,23 @@ def normalize_id(cls, id: str | int) -> str: """ return str(id).replace("#", cls.sep) # backward compatibility `#` is the old separator + def normalize_meta_id(self, config: Any) -> Any: + """ + Update deprecated identifiers in `config` using `DEPRECATED_ID_MAPPING`. + This will replace names that are marked as deprecated with their replacement. + + Args: + config: input config to be updated. + """ + if isinstance(config, dict): + for _id, _new_id in DEPRECATED_ID_MAPPING.items(): + if _id in config.keys(): + warnings.warn( + f"Detected deprecated name '{_id}' in configuration file, replacing with '{_new_id}'." + ) + config[_new_id] = config.pop(_id) + return config + @classmethod def split_id(cls, id: str | int, last: bool = False) -> list[str]: """ diff --git a/monai/bundle/utils.py b/monai/bundle/utils.py index 0f17422ba5..50d2608f4c 100644 --- a/monai/bundle/utils.py +++ b/monai/bundle/utils.py @@ -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", @@ -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: """ diff --git a/tests/testing_data/data_config.json b/tests/testing_data/data_config.json index 8b1d2868b7..79033dd0d6 100644 --- a/tests/testing_data/data_config.json +++ b/tests/testing_data/data_config.json @@ -162,9 +162,9 @@ }, "configs": { "test_meta_file": { - "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json", + "url": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20240725.json", "hash_type": "md5", - "hash_val": "662135097106b71067cd1fc657f8720f" + "hash_val": "06954cad2cc5d3784e72077ac76f0fc8" } } } diff --git a/tests/testing_data/metadata.json b/tests/testing_data/metadata.json index 98a17b73c5..29737e3a9d 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_20220324.json", + "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20240725.json", "version": "0.1.0", "changelog": { "0.1.0": "complete the model package", @@ -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",