|
| 1 | +import os |
| 2 | +import re |
| 3 | +import numpy as np |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from download import download |
| 6 | +from pathlib import Path |
| 7 | +from scipy import sparse |
| 8 | + |
| 9 | + |
| 10 | +def _get_data_home(subdir_name=""): |
| 11 | + """ |
| 12 | + Defines the data home folder. The top priority is the environment |
| 13 | + variable $LIBSVMDATA_HOME which is specific to this package. Otherwise, we |
| 14 | + seek for the variable $XDG_DATA_HOME. Finally, the fallback is $HOME/data. |
| 15 | + """ |
| 16 | + data_home = os.environ.get("LIBSVMDATA_HOME", None) |
| 17 | + if data_home is None: |
| 18 | + data_home = os.environ.get("XDG_DATA_HOME", None) |
| 19 | + if data_home is None: |
| 20 | + data_home = Path.home() / "data" |
| 21 | + return data_home / subdir_name |
| 22 | + |
| 23 | + |
| 24 | +class AbstractDataset(ABC): |
| 25 | + """Base class defining a dataset along with its fetching methods.""" |
| 26 | + |
| 27 | + # In the derived class, __init__() must set the following attributes : |
| 28 | + dataset_name = None # dataset name |
| 29 | + dataset_file = None # dataset file (with potential extensions) |
| 30 | + dataset_dir = None # subdirectory name (see _get_data_home()) |
| 31 | + dataset_url = None # dataset download url |
| 32 | + |
| 33 | + @abstractmethod |
| 34 | + def __init__(self): |
| 35 | + """ |
| 36 | + In the derived class, this function must define the class attributes. |
| 37 | + It can also be used to pass additional information required in the |
| 38 | + function _load_file_and_save_data() of the derived class. |
| 39 | + """ |
| 40 | + pass |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def _load_file_and_save_data(self, raw_dataset_path, ext_dataset_path): |
| 44 | + """ |
| 45 | + In the derived class, this function is responsible of the |
| 46 | + transformation of the raw dataset file into two .npy/.npz files |
| 47 | + containing the feature matrix X and the response vector/matrix y. These |
| 48 | + files must be named <self.dataset_name>_X.<npz/npy> and |
| 49 | + <self.dataset_name>_y.<npz/npy>. This function is also responsible for |
| 50 | + removing the raw dataset file when needed. |
| 51 | + """ |
| 52 | + pass |
| 53 | + |
| 54 | + def _load_data(self, ext_dataset_path): |
| 55 | + """Load data from the extracted .npz/.npy files.""" |
| 56 | + |
| 57 | + try: |
| 58 | + X = sparse.load_npz(str(ext_dataset_path) + "_X.npz") |
| 59 | + except FileNotFoundError: |
| 60 | + X = np.load(str(ext_dataset_path) + "_X.npy") |
| 61 | + |
| 62 | + try: |
| 63 | + y = sparse.load_npz(str(ext_dataset_path) + "_y.npz") |
| 64 | + except FileNotFoundError: |
| 65 | + y = np.load(str(ext_dataset_path) + "_y.npy") |
| 66 | + |
| 67 | + return X, y |
| 68 | + |
| 69 | + def get_X_y(self, replace=False, verbose=False): |
| 70 | + """ |
| 71 | + Load a dataset as matrix X and vector y. If X and y already exist as |
| 72 | + .npz and/or .npy files, they are not redownloaded, unless replace=True. |
| 73 | + """ |
| 74 | + |
| 75 | + raw_dataset_path = self.dataset_dir / self.dataset_file |
| 76 | + ext_dataset_path = self.dataset_dir / self.dataset_name |
| 77 | + |
| 78 | + # Check if the dataset already exists |
| 79 | + if self.dataset_dir.exists(): |
| 80 | + regex = re.compile(f"{self.dataset_name}_(X|y).(npz|npy)") |
| 81 | + files = os.listdir(self.dataset_dir) |
| 82 | + found = [f for f in files if re.search(regex, f)] |
| 83 | + exists = len(found) == 2 |
| 84 | + else: |
| 85 | + found = [] |
| 86 | + exists = False |
| 87 | + |
| 88 | + if replace or not exists: |
| 89 | + |
| 90 | + # Remove existing dataset files if there are any |
| 91 | + if raw_dataset_path.exists(): |
| 92 | + raw_dataset_path.unlink() |
| 93 | + for file in found: |
| 94 | + Path(self.dataset_dir / file).unlink() |
| 95 | + |
| 96 | + # Path of the raw dataset file |
| 97 | + if verbose: |
| 98 | + print("Downloading...") |
| 99 | + download( |
| 100 | + self.dataset_url, |
| 101 | + raw_dataset_path, |
| 102 | + progressbar=verbose, |
| 103 | + replace=replace, |
| 104 | + verbose=verbose, |
| 105 | + ) |
| 106 | + |
| 107 | + if verbose: |
| 108 | + print("Loading file and saving data...") |
| 109 | + X, y = self._load_file_and_save_data( |
| 110 | + raw_dataset_path, |
| 111 | + ext_dataset_path |
| 112 | + ) |
| 113 | + |
| 114 | + else: |
| 115 | + if verbose: |
| 116 | + print("Loading data...") |
| 117 | + X, y = self._load_data(ext_dataset_path) |
| 118 | + |
| 119 | + return X, y |
0 commit comments