Repository: Project-MONAI/MONAI
File: monai/bundle/scripts.py (Python)

Code before:
        resp.raise_for_status()
    else:
        raise ValueError("NGC API requires requests package. Please install it.")
    model_info = json.loads(resp.text)
    return model_info["model"]["latestVersionIdStr"]


def _examine_monai_version(monai_version: str) -> tuple[bool, str]:
    """Examine if the package version is compatible with the MONAI version in the metadata."""
    version_dict = get_versions()
    package_version = version_dict.get("version", None)
    if package_version is None:
        return False, "Package version is not available. Skipping version check."
    # treat rc versions as the same as the release version
    package_version = re.sub(r"rc\d.*", "", package_version)
    monai_version = re.sub(r"rc\d.*", "", monai_version)
    if package_version < monai_version:
        return (
            False,
            f"Your MONAI version is {package_version}, but the bundle is built on MONAI version {monai_version}.",
        )
    return True, ""


def _check_monai_version(bundle_dir: PathLike, name: str) -> None:
    """Get the `monai_versions` from the metadata.json and compare if it is smaller than the package version"""
    metadata_file = Path(bundle_dir) / name / "configs" / "metadata.json"
    if not metadata_file.exists():
        logger.warning(f"metadata file not found in {metadata_file}.")
        return
    with open(metadata_file) as f:
        metadata = json.load(f)
    is_compatible, msg = _examine_monai_version(metadata["monai_version"])
    if not is_compatible:
        logger.warning(msg)


def _list_latest_versions(data: dict, max_versions: int = 3) -> list[str]:
    """
    Extract the latest versions from the data dictionary.

    Args:
        data: the data dictionary.
        max_versions: the maximum number of versions to return.

    Returns:
        versions of the latest models in the reverse order of creation date, e.g. ['1.0.0', '0.9.0', '0.8.0'].
    """
    # Check if the data is a dictionary and it has the key 'modelVersions'
    if not isinstance(data, dict) or "modelVersions" not in data:
        raise ValueError("The data is not a dictionary or it does not have the key 'modelVersions'.")

Review comment (suggestion):
```suggestion
    """Get the `monai_version` from the metadata.json and compare if it is smaller than the installed `monai` package version"""
```

Code after:


def _examine_monai_version(monai_version: str) -> tuple[bool, str]:
    """Examine if the package version is compatible with the MONAI version in the metadata."""
    version_dict = get_versions()
    package_version = version_dict.get("version", "0+unknown")
    if package_version == "0+unknown":
        return False, "Package version is not available. Skipping version check."
    if monai_version == "0+unknown":
        return False, "MONAI version is not specified in the bundle. Skipping version check."
    # treat rc versions as the same as the release version
    package_version = re.sub(r"rc\d.*", "", package_version)
    monai_version = re.sub(r"rc\d.*", "", monai_version)
    if package_version < monai_version:
        return (
            False,
            f"Your MONAI version is {package_version}, but the bundle is built on MONAI version {monai_version}.",
        )
    return True, ""


def _check_monai_version(bundle_dir: PathLike, name: str) -> None:
    """Get the `monai_version` from the metadata.json and compare if it is smaller than the installed `monai` package version"""
    metadata_file = Path(bundle_dir) / name / "configs" / "metadata.json"
    if not metadata_file.exists():
        logger.warning(f"metadata file not found in {metadata_file}.")
        return
    with open(metadata_file) as f:
        metadata = json.load(f)
    is_compatible, msg = _examine_monai_version(metadata.get("monai_version", "0+unknown"))
    if not is_compatible:
        logger.warning(msg)


def _list_latest_versions(data: dict, max_versions: int = 3) -> list[str]:
    """
    Extract the latest versions from the data dictionary.

    Args:
        data: the data dictionary.
        max_versions: the maximum number of versions to return.

    Returns:
        versions of the latest models in the reverse order of creation date, e.g. ['1.0.0', '0.9.0', '0.8.0'].
    """
    # Check if the data is a dictionary and it has the key 'modelVersions'
    if not isinstance(data, dict) or "modelVersions" not in data:
        raise ValueError("The data is not a dictionary or it does not have the key 'modelVersions'.")

    # Extract the list of model versions
    model_versions = data["modelVersions"]