-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPytorchDatasets.py
242 lines (173 loc) · 7.58 KB
/
PytorchDatasets.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import torch
from torch.utils.data.dataset import Dataset
import h5py
import numpy as np
class DatasetFromHDF5(Dataset):
"""Implementation of the Pytorch HDF5 Dataset.
Parameters
----------
filename: str
Path to the hdf5 file
dataset_x: str
Name of the hdf5 dataframe containing input features
dataset_y: str
Name of the hdf5 dataframe containing target variables
norm_params: dict
Dictionary of normalization parameters
feat_cols: list, optional (default=None)
List of input column names. Use all columns if None
load_all: boolean, optional (default=False)
Whether to load all data from HDF5 at once. It will speed up processing, but
the data will also take up a lot of memory.
Returns
-------
x_norm: torch.DoubleTensor
Tensor of normalized input features
y_norm: torch.DoubleTensor
Tensor of normalized target variables
"""
def __init__(self, filename, dataset_x, dataset_y, norm_params, feat_cols=None, load_all=False):
h5f = h5py.File(filename, 'r')
if not feat_cols:
self.feat_cols = norm_params['input_features']
else:
self.feat_cols = feat_cols
self.n_features = len(self.feat_cols)
if not load_all:
self.x = h5f[dataset_x]['table']
self.y = h5f[dataset_y]['table']
else:
self.x = h5f[dataset_x]['table'][:]
self.y = h5f[dataset_y]['table'][:]
# Extract normalization parameters
self.x_min = np.array([norm_params[col]['min'] for col in norm_params['input_features']])
self.x_max = np.array([norm_params[col]['max'] for col in norm_params['input_features']])
self.y_min = np.array([norm_params[col]['min'] for col in norm_params['target']])
self.y_max = np.array([norm_params[col]['max'] for col in norm_params['target']])
self.cols_idx = [idx for idx, col in enumerate(norm_params['input_features']) if col in self.feat_cols]
def __len__(self):
return self.x.shape[0]
def __getitem__(self, index):
x_norm = (self.x[index][1] - self.x_min) / (self.x_max - self.x_min)
y_norm = (self.y[index][1] - self.y_min) / (self.y_max - self.y_min)
if self.feat_cols:
x_norm = x_norm[self.cols_idx]
return torch.DoubleTensor(x_norm), torch.DoubleTensor(y_norm)
class PandasDataset(Dataset):
"""Implementation of the Pytorch Dataset for Pandas dataframe.
Parameters
---------
df: pandas.DataFrame
Pandas dataframe of input and target variables
norm_params: dict
Dictionary of normalization parameters
feat_cols: list, optional (default=None)
List of input column names. Use all columns if None
Returns
------
x_norm: torch.DoubleTensor
Tensor of normalized input features
y_norm: torch.DoubleTensor
Tensor of normalized target variables
"""
def __init__(self, df, norm_params, feat_cols=None):
if feat_cols:
x_cols = feat_cols
else:
x_cols = norm_params['input_features']
self.n_features = len(x_cols)
y_cols = norm_params['target']
# Extract normalization parameters
x_min = np.array([norm_params[col]['min'] for col in x_cols])
x_max = np.array([norm_params[col]['max'] for col in x_cols])
y_min = np.array([norm_params[col]['min'] for col in y_cols])
y_max = np.array([norm_params[col]['max'] for col in y_cols])
self.x = (np.array(df[x_cols]) - x_min) / (x_max - x_min)
self.y = (np.array(df[y_cols]) - y_min) / (y_max - y_min)
def __len__(self):
return self.x.shape[0]
def __getitem__(self, idx):
return torch.DoubleTensor(self.x[idx]), torch.DoubleTensor(self.y[idx])
class MariaDB_chunk_indices:
"""Generates indices of database rows that form chunk of MySQL/MariaDB
database parameterized by chunk_size (chunking is used to diminish memory usage while parsing).
Parameters
---------
cursor: mariadb.connection.cursor
MariaDB/MySQL cursor object used to execute SQL statements
table: str
Name of the SQL table to read from
chunk_size: int
Size of the chunk of data to be read from database
Returns
-------
chunk_indices[idx]: list
List of chunk indices
"""
def __init__(self, cursor, table, chunk_size):
# Extract number of the rows in the database
cursor.execute("SELECT COUNT(ID) FROM {};".format(table))
db_length = cursor.fetchone()[0]
# Calculate number of chunks
num_chunks = db_length // chunk_size
# Generate the indices list
indices = np.arange(0, num_chunks * chunk_size)
self.chunk_indices = np.array_split(indices, num_chunks)
self.chunk_indices.append(np.arange(num_chunks * chunk_size, db_length))
def __len__(self):
return len(self.chunk_indices)
def __getitem__(self, idx):
return self.chunk_indices[idx]
class MariaDB_dataset(Dataset):
"""Implementation of the Pytorch dataset that reads rows specified by the indices parameter
from the MariaDB/MySQL database.
Parameters
---------
indices: np.array
Array of indices
cursor: mariadb.connection.cursor
MariaDB/MySQL cursor object used to execute SQL statements
table: str
Name of the SQL table to read from
norm_params: dict
Dictionary of normalization parameters
feat_cols: list, optional (default=None)
List of input column names. Use all columns if None
Returns
------
x_norm: torch.DoubleTensor
Tensor of normalized input features
y_norm: torch.DoubleTensor
Tensor of normalized target variables
"""
def __init__(self, indices, cursor, table, norm_params, feat_cols=None):
super(MariaDB_dataset, self).__init__()
indices = tuple(indices)
if not feat_cols:
feat_cols = norm_params['input_features']
else:
feat_cols = feat_cols
y_cols = norm_params['target']
self.n_features = len(feat_cols)
# Extract normalization parameters
x_min = np.array([norm_params[col]['min'] for col in feat_cols])
x_max = np.array([norm_params[col]['max'] for col in feat_cols])
y_min = np.array([norm_params[col]['min'] for col in y_cols])
y_max = np.array([norm_params[col]['max'] for col in y_cols])
feat_cols = ", ".join(feat_cols)
y_cols = ", ".join(y_cols)
# Fetch independent variables from database
cursor.execute("SELECT {} FROM {} WHERE ID IN {};"\
.format(feat_cols, table, indices))
self.x = torch.DoubleTensor(cursor.fetchall())
# Fetch target variables from database
cursor.execute("SELECT {} FROM {} WHERE ID IN {};"\
.format(y_cols, table, indices))
self.y = torch.DoubleTensor(cursor.fetchall())
# Perform normalization
self.x = (self.x - x_min) / (x_max - x_min)
self.y = (self.y - y_min) / (y_max - y_min)
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.y[idx]