-
Notifications
You must be signed in to change notification settings - Fork 239
/
_base.py
39 lines (25 loc) · 939 Bytes
/
_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Base tools to handle datasets.
Note: deeply inspired by sklearn.
"""
import logging
import os
from collections import namedtuple
from urllib.request import urlretrieve
DEFAULT_DATA_DIR = os.path.expanduser(os.path.join("~", ".geomstats_data"))
RemoteFileMetadata = namedtuple("RemoteFileMetadata", ["filename", "url"])
def _get_data_home(data_home=None):
if data_home is None:
data_home = DEFAULT_DATA_DIR
os.makedirs(data_home, exist_ok=True)
return data_home
def _fetch_remote(url, filename, dirname=None):
dirname = _get_data_home(data_home=dirname)
file_path = os.path.join(dirname, filename)
if os.path.exists(file_path):
logging.info(
f"Data has already been downloaded... using cached file ('{file_path}')."
)
else:
logging.info(f"Downloading '{file_path}' from {url} to '{dirname}'.")
urlretrieve(url, file_path)
return file_path