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
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}")
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)}")
def online_test_file_dummy_paths() -> Dict[str, str]: """Return a :class:`dict` of dummy paths to the downloadable test files. Returns ------- dict A dict of dummy paths to the test files available via download. """ filenames = list(get_url_map().keys()) test_files_root = os.path.join(DATA_ROOT, 'test_files') dummy_path_map = { os.path.join(test_files_root, filename): filename for filename in filenames } return dummy_path_map
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