Exemplo n.º 1
0
def _get_testdata_file(name: str, download: bool = True) -> Optional[str]:
    # Check pydicom local
    data_path = Path(DATA_ROOT) / 'test_files'
    matches = [m for m in data_path.rglob(name)]
    if matches:
        return os.fspath(matches[0])

    # Check external data sources
    fpath: Optional[str]
    for lib, source in external_data_sources().items():
        try:
            fpath = source.get_path(name, dtype=DataTypes.DATASET)
        except ValueError:
            fpath = None

        # For pydicom-data, check the hash against hashes.json
        if lib == "pydicom-data":
            if fpath and _check_data_hash(fpath):
                return fpath
        elif fpath:
            return fpath

    # Try online
    if download:
        for filename in get_url_map().keys():
            if filename != name:
                continue
            try:
                return os.fspath(data_path_with_download(filename))
            except Exception:
                warnings.warn(
                    f"A download failure occurred while attempting to "
                    f"retrieve {name}")

    return None
Exemplo n.º 2
0
def fetch_data_files():
    """Download missing test files to the local cache."""
    cache = get_data_dir()
    paths = {cache / fname: fname for fname in list(get_url_map().keys())}

    error = []
    for p in paths:
        # Download missing files or files that don't match the hash
        try:
            data_path_with_download(p.name)
        except Exception:
            error.append(p.name)

    if error:
        raise RuntimeError(
            "An error occurred downloading the following files: "
            f"{', '.join(error)}")
Exemplo n.º 3
0
def get_testdata_file(name: str) -> str:
    """Return an absolute path to the first matching dataset with filename
    `name`.

    .. versionadded:: 1.4

    First searches the local *pydicom* data store, then any locally available
    external sources, and finally the files available in the
    pydicom/pydicom-data repository.

    .. versionchanged:: 2.1

        Modified to search locally available external data sources and the
        pydicom/pydicom-data repository

    Parameters
    ----------
    name : str
        The full file name (without path)

    Returns
    -------
    str or None
        The absolute path of the file if found, or ``None``.
    """
    # Check pydicom local
    data_path = Path(DATA_ROOT) / 'test_files'
    matches = [m for m in data_path.rglob(name)]
    if matches:
        return os.fspath(matches[0])

    # Check external data sources
    for lib, source in external_data_sources().items():
        try:
            fpath = source.get_path(name, dtype=DataTypes.DATASET)
        except ValueError:
            fpath = None

        # For pydicom-data, check the hash against hashes.json
        if lib == "pydicom-data":
            if fpath and _check_data_hash(fpath):
                return fpath
        elif fpath:
            return fpath

    # Try online
    for filename in get_url_map().keys():
        if filename == name:
            try:
                return os.fspath(data_path_with_download(filename))
            except Exception:
                warnings.warn(
                    f"A download failure occurred while attempting to "
                    f"retrieve {name}")
Exemplo n.º 4
0
def get_testdata_file(name: str,
                      read: bool = False) -> Union[str, "Dataset", None]:
    """Return an absolute path to the first matching dataset with filename
    `name`.

    .. versionadded:: 1.4

    First searches the local *pydicom* data store, then any locally available
    external sources, and finally the files available in the
    pydicom/pydicom-data repository.

    .. versionchanged:: 2.1

        Modified to search locally available external data sources and the
        pydicom/pydicom-data repository

    .. versionchanged:: 2.2

        Added the `read` keyword parameter.

    Parameters
    ----------
    name : str
        The full file name (without path)
    read : bool, optional
        If ``True`` then use :func:`~pydicom.filereader.dcmread` to read the
        file and return the corresponding
        :class:`~pydicom.dataset.FileDataset`. Default ``False``.

    Returns
    -------
    str, pydicom.dataset.Dataset or None
        The absolute path of the file if found, the dataset itself if `read` is
        ``True``, or ``None`` if the file is not found.
    """
    from pydicom.filereader import dcmread

    # Check pydicom local
    data_path = Path(DATA_ROOT) / 'test_files'
    matches = [m for m in data_path.rglob(name)]
    if matches:
        path = os.fspath(matches[0])
        return dcmread(path, force=True) if read else path

    # Check external data sources
    for lib, source in external_data_sources().items():
        try:
            fpath = source.get_path(name, dtype=DataTypes.DATASET)
        except ValueError:
            fpath = None

        # For pydicom-data, check the hash against hashes.json
        if lib == "pydicom-data":
            if fpath and _check_data_hash(fpath):
                return dcmread(fpath, force=True) if read else fpath
        elif fpath:
            return dcmread(fpath, force=True) if read else fpath

    # Try online
    for filename in get_url_map().keys():
        if filename == name:
            try:
                path = os.fspath(data_path_with_download(filename))
                return dcmread(path, force=True) if read else path
            except Exception:
                warnings.warn(
                    f"A download failure occurred while attempting to "
                    f"retrieve {name}")

    return None
Exemplo n.º 5
0
def get_files(base: Union[str, os.PathLike],
              pattern: str = "**/*",
              dtype: int = DataTypes.DATASET) -> List[str]:
    """Return all matching file paths from the available data sources.

    First searches the local *pydicom* data store, then any locally available
    external sources, and finally the files available in the
    pydicom/pydicom-data repository.

    .. versionchanged: 2.1

        Added the `dtype` keyword parameter, modified to search locally
        available external data sources and the pydicom/pydicom-data repository

    Parameters
    ----------
    base : str or os.PathLike
        Base directory to recursively search.
    pattern : str, optional
        The pattern to pass to :meth:`~pathlib.Path.glob`, default
        (``'**/*'``).
    dtype : int, optional
        The type of data to search for when using an external source, one of:

        * ``0`` - DICOM dataset
        * ``1`` - Character set file
        * ``2`` - Palette file
        * ``3`` - DICOMDIR file
        * ``4`` - JPEG file

    Returns
    -------
    list of str
        A list of absolute paths to matching files.
    """
    base = Path(base)

    # Search locally
    files = [os.fspath(m) for m in base.glob(pattern)]

    # Search external sources
    for lib, source in external_data_sources().items():
        fpaths = source.get_paths(pattern, dtype)
        if lib == "pydicom-data":
            # For pydicom-data, check the hash against hashes.json
            fpaths = [p for p in fpaths if _check_data_hash(p)]

        files.extend(fpaths)

    # Search http://github.com/pydicom/pydicom-data or local cache
    # To preserve backwards compatibility filter the downloaded files
    # as if they are stored within DATA_ROOT/test_files/*.dcm
    dummy_online_file_path_map = online_test_file_dummy_paths()
    dummy_online_file_path_filtered = fnmatch.filter(
        dummy_online_file_path_map.keys(), os.path.join(base, pattern))
    download_names = [
        os.fspath(dummy_online_file_path_map[dummy_path])
        for dummy_path in dummy_online_file_path_filtered
    ]

    real_online_file_paths = []
    download_error = False
    for filename in download_names:
        try:
            real_online_file_paths.append(
                os.fspath(data_path_with_download(filename)))
        except Exception:
            download_error = True

    files += real_online_file_paths

    if download_error:
        warnings.warn(
            "One or more download failures occurred, the list of matching "
            "file paths may be incomplete")

    return files