-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathDataProvider.py
287 lines (206 loc) · 9.61 KB
/
DataProvider.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from Logger import log
import numpy as np
import pandas as pd
class ChunkDoubleSourceSlider(object):
def __init__(self, filename, batchsize, chunksize, shuffle, offset, crop=None, header=0, ram_threshold=5*10**5):
self.filename = filename
self.batchsize = batchsize
self.chunksize = chunksize
self.shuffle = shuffle
self.offset = offset
self.header = header
self.crop = crop
self.ram = ram_threshold
def check_lenght(self):
# check the csv size
check_cvs = pd.read_csv(self.filename,
nrows=self.crop,
chunksize=10 ** 3,
header=self.header
)
t_size = 0
for chunk in check_cvs:
size = chunk.shape[0]
t_size += size
del chunk
log('Size of the dataset is {:.3f} M rows.'.format(t_size/10 ** 6))
if t_size > self.ram: # IF dataset is too large for memory
log('It is too large to fit in memory so it will be loaded in chunkes of size {:}.'.format(self.chunksize))
else:
log('This size can fit the memory so it will load entirely')
return t_size
def feed_chunk(self):
try:
total_size
except NameError:
#global total_size
total_size = ChunkDoubleSourceSlider.check_lenght(self)
if total_size > self.ram: # IF dataset is too large for memory
# LOAD data from csv
data_frame = pd.read_csv(self.filename,
nrows=self.crop,
chunksize=self.chunksize,
header=self.header
)
# iterations over csv file
for chunk in data_frame:
np_array = np.array(chunk)
inputs, targets = np_array[:, 0], np_array[:, 1]
"""
if len(inputs) < self.batchsize:
while len(inputs) == self.batchsize:
inputs = np.append(inputs, 0)
targets = np.append(targets, 0)
"""
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
# define indices and shuffle them if necessary
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
# providing sliding windows:
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
inp = np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt])
tar = targets[excerpt + self.offset].reshape(-1, 1)
yield inp, tar
else: # IF dataset can fit the memory
# LOAD data from csv
data_frame = pd.read_csv(self.filename,
nrows=self.crop,
header=self.header
)
np_array = np.array(data_frame)
inputs, targets = np_array[:, 0], np_array[:, 1]
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
# define indices and shuffle them if necessary
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
# providing sliding windows:
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
inp = np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt])
tar = targets[excerpt + self.offset].reshape(-1, 1)
yield inp, tar
class ChunkDoubleSourceSlider2(object):
def __init__(self, filename, batchsize, chunksize, shuffle, offset, crop=None, header=0, ram_threshold=5 * 10 ** 5):
self.filename = filename
self.batchsize = batchsize
self.chunksize = chunksize
self.shuffle = shuffle
self.offset = offset
self.header = header
self.crop = crop
self.ram = ram_threshold
self.total_size = 0
def check_length(self):
# check the csv size
check_cvs = pd.read_csv(self.filename,
nrows=self.crop,
chunksize=10 ** 3,
header=self.header
)
for chunk in check_cvs:
size = chunk.shape[0]
self.total_size += size
del chunk
log('Size of the dataset is {:.3f} M rows.'.format(self.total_size / 10 ** 6))
if self.total_size > self.ram: # IF dataset is too large for memory
log('It is too large to fit in memory so it will be loaded in chunkes of size {:}.'.format(self.chunksize))
else:
log('This size can fit the memory so it will load entirely')
def feed_chunk(self):
if self.total_size == 0:
ChunkDoubleSourceSlider2.check_length(self)
if self.total_size > self.ram: # IF dataset is too large for memory
# LOAD data from csv
data_frame = pd.read_csv(self.filename,
nrows=self.crop,
chunksize=self.chunksize,
header=self.header
)
skip_idx = np.arange(self.total_size/self.chunksize)
if self.shuffle:
np.random.shuffle(skip_idx)
log(str(skip_idx), 'debug')
for i in skip_idx:
log('index: ' + str(i), 'debug')
# Read the data
data = pd.read_csv(self.filename,
nrows=self.chunksize,
skiprows=int(i)*self.chunksize,
header=self.header)
np_array = np.array(data)
inputs, targets = np_array[:, 0], np_array[:, 1]
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
# define indices and shuffle them if necessary
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
# providing sliding windows:
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
inp = np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt])
tar = targets[excerpt + self.offset].reshape(-1, 1)
yield inp, tar
else: # IF dataset can fit the memory
# LOAD data from csv
data_frame = pd.read_csv(self.filename,
nrows=self.crop,
header=self.header
)
np_array = np.array(data_frame)
inputs, targets = np_array[:, 0], np_array[:, 1]
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
# define indices and shuffle them if necessary
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
# providing sliding windows:
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
inp = np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt])
tar = targets[excerpt + self.offset].reshape(-1, 1)
yield inp, tar
class DoubleSourceProvider2(object):
def __init__(self, batchsize, shuffle, offset):
self.batchsize = batchsize
self.shuffle = shuffle
self.offset = offset
def feed(self, inputs, targets):
assert len(inputs) == len(targets)
inputs = inputs.flatten()
targets = targets.flatten()
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize == -1:
self.batchsize = len(inputs)
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]),\
targets[excerpt + self.offset].reshape(-1, 1)
class DoubleSourceProvider3(object):
def __init__(self, nofWindows, offset):
self.nofWindows = nofWindows
self.offset = offset
def feed(self, inputs):
inputs = inputs.flatten()
max_nofw = inputs.size - 2 * self.offset
if self.nofWindows < 0:
self.nofWindows = max_nofw
indices = np.arange(max_nofw, dtype=int)
# providing sliding windows:
for start_idx in range(0, max_nofw, self.nofWindows):
excerpt = indices[start_idx:start_idx + self.nofWindows]
inp = np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt])
yield inp