Beispiel #1
0
def _download_from_github(repo: str, download_path: Path, filename: str, progress: bool = True):
    if len(repo.split("/")) != 3:
        raise ValueError("if source is `github`, repo should be in the form of `repo_owner/repo_name/release_tag`.")
    repo_owner, repo_name, tag_name = repo.split("/")
    if ".zip" not in filename:
        filename += ".zip"
    url = _get_git_release_url(repo_owner, repo_name, tag_name=tag_name, filename=filename)
    filepath = download_path / f"{filename}"
    download_url(url=url, filepath=filepath, hash_val=None, progress=progress)
    extractall(filepath=filepath, output_dir=download_path, has_base=True)
Beispiel #2
0
def download(
    name: Optional[str] = None,
    bundle_dir: Optional[PathLike] = None,
    source: str = "github",
    repo: Optional[str] = None,
    url: Optional[str] = None,
    progress: bool = True,
    args_file: Optional[str] = None,
):
    """
    download bundle from the specified source or url. The bundle should be a zip file and it
    will be extracted after downloading.
    This function refers to:
    https://pytorch.org/docs/stable/_modules/torch/hub.html

    Typical usage examples:

    .. code-block:: bash

        # Execute this module as a CLI entry, and download bundle:
        python -m monai.bundle download --name "bundle_name" --source "github" --repo "repo_owner/repo_name/release_tag"

        # Execute this module as a CLI entry, and download bundle via URL:
        python -m monai.bundle download --name "bundle_name" --url <url>

        # Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
        # Other args still can override the default args at runtime.
        # The content of the JSON / YAML file is a dictionary. For example:
        # {"name": "spleen", "bundle_dir": "download", "source": ""}
        # then do the following command for downloading:
        python -m monai.bundle download --args_file "args.json" --source "github"

    Args:
        name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`.
        bundle_dir: target directory to store the downloaded data.
            Default is `bundle` subfolder under`torch.hub get_dir()`.
        source: place that saved the bundle.
            If `source` is `github`, the bundle should be within the releases.
        repo: repo name. If `None` and `url` is `None`, it must be provided in `args_file`.
            If `source` is `github`, it should be in the form of `repo_owner/repo_name/release_tag`.
            For example: `Project-MONAI/MONAI-extra-test-data/0.8.1`.
        url: url to download the data. If not `None`, data will be downloaded directly
            and `source` will not be checked.
            If `name` is `None`, filename is determined by `monai.apps.utils._basename(url)`.
        progress: whether to display a progress bar.
        args_file: a JSON or YAML file to provide default values for all the args in this function.
            so that the command line inputs can be simplified.

    """
    _args = _update_args(args=args_file,
                         name=name,
                         bundle_dir=bundle_dir,
                         source=source,
                         repo=repo,
                         url=url,
                         progress=progress)

    _log_input_summary(tag="download", args=_args)
    name_, bundle_dir_, source_, repo_, url_, progress_ = _pop_args(
        _args,
        name=None,
        bundle_dir=None,
        source="github",
        repo=None,
        url=None,
        progress=True)

    bundle_dir_ = _process_bundle_dir(bundle_dir_)

    if url_ is not None:
        if name is not None:
            filepath = bundle_dir_ / f"{name}.zip"
        else:
            filepath = bundle_dir_ / f"{_basename(url_)}"
        download_url(url=url_,
                     filepath=filepath,
                     hash_val=None,
                     progress=progress_)
        extractall(filepath=filepath, output_dir=bundle_dir_, has_base=True)
    elif source_ == "github":
        if name_ is None or repo_ is None:
            raise ValueError(
                f"To download from source: Github, `name` and `repo` must be provided, got {name_} and {repo_}."
            )
        _download_from_github(repo=repo_,
                              download_path=bundle_dir_,
                              filename=name_,
                              progress=progress_)
    else:
        raise NotImplementedError(
            f"Currently only download from provided URL in `url` or Github is implemented, got source: {source_}."
        )