_utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. import shutil
  3. from ._registry import method_files_map
  4. try:
  5. import appdirs
  6. except ImportError:
  7. appdirs = None
  8. def _clear_cache(datasets, cache_dir=None, method_map=None):
  9. if method_map is None:
  10. # Use SciPy Datasets method map
  11. method_map = method_files_map
  12. if cache_dir is None:
  13. # Use default cache_dir path
  14. if appdirs is None:
  15. # appdirs is pooch dependency
  16. raise ImportError("Missing optional dependency 'pooch' required "
  17. "for scipy.datasets module. Please use pip or "
  18. "conda to install 'pooch'.")
  19. cache_dir = appdirs.user_cache_dir("scipy-data")
  20. if not os.path.exists(cache_dir):
  21. print(f"Cache Directory {cache_dir} doesn't exist. Nothing to clear.")
  22. return
  23. if datasets is None:
  24. print(f"Cleaning the cache directory {cache_dir}!")
  25. shutil.rmtree(cache_dir)
  26. else:
  27. if not isinstance(datasets, (list, tuple)):
  28. # single dataset method passed should be converted to list
  29. datasets = [datasets, ]
  30. for dataset in datasets:
  31. assert callable(dataset)
  32. dataset_name = dataset.__name__ # Name of the dataset method
  33. if dataset_name not in method_map:
  34. raise ValueError(f"Dataset method {dataset_name} doesn't "
  35. "exist. Please check if the passed dataset "
  36. "is a subset of the following dataset "
  37. f"methods: {list(method_map.keys())}")
  38. data_files = method_map[dataset_name]
  39. data_filepaths = [os.path.join(cache_dir, file)
  40. for file in data_files]
  41. for data_filepath in data_filepaths:
  42. if os.path.exists(data_filepath):
  43. print("Cleaning the file "
  44. f"{os.path.split(data_filepath)[1]} "
  45. f"for dataset {dataset_name}")
  46. os.remove(data_filepath)
  47. else:
  48. print(f"Path {data_filepath} doesn't exist. "
  49. "Nothing to clear.")
  50. def clear_cache(datasets=None):
  51. """
  52. Cleans the scipy datasets cache directory.
  53. If a scipy.datasets method or a list/tuple of the same is
  54. provided, then clear_cache removes all the data files
  55. associated to the passed dataset method callable(s).
  56. By default, it removes all the cached data files.
  57. Parameters
  58. ----------
  59. datasets : callable or list/tuple of callable or None
  60. Examples
  61. --------
  62. >>> from scipy import datasets
  63. >>> ascent_array = datasets.ascent()
  64. >>> ascent_array.shape
  65. (512, 512)
  66. >>> datasets.clear_cache([datasets.ascent])
  67. Cleaning the file ascent.dat for dataset ascent
  68. """
  69. _clear_cache(datasets)