_download_all.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. Platform independent script to download all the
  3. `scipy.datasets` module data files.
  4. This doesn't require a full scipy build.
  5. Run: python _download_all.py <download_dir>
  6. """
  7. from scipy._lib._array_api import xp_capabilities
  8. import argparse
  9. try:
  10. import pooch
  11. except ImportError:
  12. pooch = None
  13. if __package__ is None or __package__ == '':
  14. # Running as python script, use absolute import
  15. import _registry # type: ignore
  16. else:
  17. # Running as python module, use relative import
  18. from . import _registry
  19. @xp_capabilities(out_of_scope=True)
  20. def download_all(path=None):
  21. """
  22. Utility method to download all the dataset files
  23. for `scipy.datasets` module.
  24. Parameters
  25. ----------
  26. path : str, optional
  27. Directory path to download all the dataset files.
  28. If None, default to the system cache_dir detected by pooch.
  29. Examples
  30. --------
  31. Download the datasets to the default cache location:
  32. >>> from scipy import datasets
  33. >>> datasets.download_all()
  34. Download the datasets to the current directory:
  35. >>> datasets.download_all(".")
  36. """
  37. if pooch is None:
  38. raise ImportError("Missing optional dependency 'pooch' required "
  39. "for scipy.datasets module. Please use pip or "
  40. "conda to install 'pooch'.")
  41. if path is None:
  42. path = pooch.os_cache('scipy-data')
  43. # https://github.com/scipy/scipy/issues/21879
  44. downloader = pooch.HTTPDownloader(headers={"User-Agent": "SciPy"})
  45. for dataset_name, dataset_hash in _registry.registry.items():
  46. pooch.retrieve(url=_registry.registry_urls[dataset_name],
  47. known_hash=dataset_hash,
  48. fname=dataset_name, path=path, downloader=downloader)
  49. def main():
  50. parser = argparse.ArgumentParser(description='Download SciPy data files.')
  51. parser.add_argument("path", nargs='?', type=str,
  52. default=pooch.os_cache('scipy-data'),
  53. help="Directory path to download all the data files.")
  54. args = parser.parse_args()
  55. download_all(args.path)
  56. if __name__ == "__main__":
  57. main()