-
Notifications
You must be signed in to change notification settings - Fork 2
/
multitables_test_v1.py
363 lines (286 loc) · 13 KB
/
multitables_test_v1.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Copyright (C) 2016 G. H. Collin (ghcollin)
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
import unittest
import numpy as np
import tempfile
import os
import shutil
import tables
import threading
import multitables
__author__ = "G. H. Collin"
__version__ = "2.0.0"
def lcm(a,b):
import fractions
return abs(a * b) // fractions.gcd(a, b) if a and b else 0
test_table_col_A_shape = (100,200)
test_table_col_B_shape = (7,49)
class TestTableRow(tables.IsDescription):
col_A = tables.UInt8Col(shape=test_table_col_A_shape)
col_B = tables.Float64Col(shape=test_table_col_B_shape)
def get_batches(array, size):
return [ array[i:i+size] for i in range(0, len(array), size)]
def assert_items_equal(self, a, b, key):
self.assertEqual(len(a), len(b))
if key is not None:
a_sorted, b_sorted = sorted(a, key=key), sorted(b, key=key)
else:
a_sorted, b_sorted = a, b
for i in range(len(a)):
#self.assertTrue(np.all(a_sorted[i] == b_sorted[i]),
# msg=str(i) + "/" + str(len(a)) + "): LHS: \n" + str(a_sorted[i]) + "\n RHS: \n" + str(b_sorted[i]))
np.testing.assert_array_equal(a_sorted[i], b_sorted[i])
class MultiTablesTest(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.test_filename = os.path.join(self.test_dir, 'test.h5')
test_file = tables.open_file(self.test_filename, 'w')
self.test_array = np.arange(100*1000).reshape((1000, 10, 10))
self.test_array_path = '/test_array'
array = test_file.create_array(test_file.root, self.test_array_path[1:], self.test_array)
self.test_table_ary = np.array([ (
np.random.randint(256, size=np.prod(test_table_col_A_shape)).reshape(test_table_col_A_shape),
np.random.rand(*test_table_col_B_shape)) for _ in range(100) ],
dtype=tables.dtype_from_descr(TestTableRow))
self.test_table_path = '/test_table'
table = test_file.create_table(test_file.root, self.test_table_path[1:], TestTableRow)
table.append(self.test_table_ary)
test_file.close()
def tearDown(self):
import errno
import time
# There can be some trouble with deleting the test HDF5 file on Windows. If the file is deleted too quickly
# then one of the reader background processes may not have started yet, and will raise an exception when
# it cannot find the (now deleted) test HDF5 file.
time.sleep(0.1)
# In addition, if the reader does not wait for background processes/threads to shutdown when it is closed,
# then deleting the test HDF5 file can raise a permission error, as one of the background processes still
# has the HDF5 open, and has not shut down yet. In this case, the cleanup procedure waits a fraction of a
# second and tries again, until the test file is correctly deleted.
while True:
try:
shutil.rmtree(self.test_dir)
break
except (IOError, OSError) as e:
if (e.errno == errno.EPERM or e.errno == errno.EACCES):
# If the raised error has to do with permissions.
time.sleep(0.1)
continue
else:
raise
def test_get_batches(self):
array = np.arange(8)
batches = get_batches(array, 3)
assert_items_equal(self, batches,
[np.arange(3), np.arange(3, 6), np.arange(6, 8)],
key=lambda x: x[0])
def test_generator(self):
reader = multitables.Streamer(filename=self.test_filename)
ary_gen = reader.get_generator(path=self.test_array_path)
assert_items_equal(self,
list(ary_gen),
list(self.test_array),
key=lambda x: x[0, 0])
ary_gen.close()
table_gen = reader.get_generator(path=self.test_table_path)
assert_items_equal(self,
list(table_gen),
list(self.test_table_ary),
key=lambda x: x['col_B'][0][0])
table_gen.close()
def test_ordered(self):
reader = multitables.Streamer(filename=self.test_filename)
ary_gen = reader.get_generator(path=self.test_array_path, ordered=True)
assert_items_equal(self,
list(ary_gen),
list(self.test_array),
key=None)
ary_gen.close()
table_gen = reader.get_generator(path=self.test_table_path, ordered=True)
assert_items_equal(self,
list(table_gen),
list(self.test_table_ary),
key=None)
table_gen.close()
def test_direct(self):
block_size = None
reader = multitables.Streamer(filename=self.test_filename)
queue = reader.get_queue(path=self.test_array_path, block_size=block_size)
result = []
while True:
guard = queue.get()
if guard is multitables.QueueClosed:
break
else:
with guard as batch:
result.append(batch.copy())
result.append(reader.get_remainder(path=self.test_array_path, block_size=queue.block_size))
assert_items_equal(self,
result,
get_batches(self.test_array, queue.block_size),
key=lambda x: x[0, 0, 0])
queue.close()
block_size = 16
queue = reader.get_queue(path=self.test_array_path, block_size=block_size)
result = []
for guard in queue.iter():
with guard as batch:
result.append(batch.copy())
result.append(reader.get_remainder(path=self.test_array_path, block_size=queue.block_size))
assert_items_equal(self,
result,
get_batches(self.test_array, queue.block_size),
key=lambda x: x[0, 0, 0])
queue.close()
def test_cycle(self):
block_size = 45
num_cycles = lcm(block_size, len(self.test_array))//len(self.test_array)
#if num_cycles < 3:
# num_cycles = 4
#elif num_cycles == 3:
# num_cycles = 6
num_cycles = max(num_cycles, num_cycles*(int(100/num_cycles)+1))
reader = multitables.Streamer(filename=self.test_filename)
ary = reader.get_generator(path=self.test_array_path, cyclic=True, block_size=block_size)
result = []
for i, row in enumerate(ary):
if i >= num_cycles*len(self.test_array):
#print("Terminating at " + str(row[0,0]))
break
#print(row[0, 0])
result.append(row)
assert_items_equal(self,
result,
list(self.test_array)*num_cycles,
key=lambda x: x[0, 0])
#self.assertEqual(len(result), 4*len(self.test_array))
ary.close()
return
queue = reader.get_queue(path=self.test_array_path, cyclic=True, block_size=block_size)
result = []
for i in range(num_cycles*len(self.test_array)//block_size):
guard = queue.get()
self.assertIsNot(guard, multitables.QueueClosed)
with guard as batch:
result.append(batch.copy())
assert_items_equal(self,
np.concatenate(result, axis=0),
list(self.test_array) * num_cycles,
key=lambda x: x[0, 0])
queue.close()
def test_cycle_ordered(self):
return
block_size = 45
num_cycles = lcm(block_size, len(self.test_array))//len(self.test_array)
if num_cycles < 3:
num_cycles = 4
elif num_cycles == 3:
num_cycles = 6
reader = multitables.Streamer(filename=self.test_filename)
ary = reader.get_generator(path=self.test_array_path, cyclic=True, block_size=block_size, ordered=True)
result = []
for i, row in enumerate(ary):
if i >= num_cycles*len(self.test_array):
#print("Terminating at " + str(row[0,0]))
break
#print(row[0, 0])
result.append(row)
#print(np.bincount(np.array(result)[:,0,0]/100))
assert_items_equal(self,
result,
list(self.test_array)*num_cycles,
key=None)
ary.close()
def test_threaded(self):
return
block_size = len(self.test_array)//100
reader = multitables.Streamer(filename=self.test_filename)
queue = reader.get_queue(path=self.test_array_path, n_procs=4, block_size=block_size)
lock = threading.Lock()
result = []
def read():
while True:
guard = queue.get()
if guard is multitables.QueueClosed:
break
with guard as batch:
batch_copy = batch.copy()
with lock:
result.append(batch_copy)
threads = []
for i in range(100):
threads.append(threading.Thread(target=read))
for t in threads:
t.start()
last_batch = reader.get_remainder(path=self.test_array_path, block_size=queue.block_size)
if 100*block_size == len(self.test_array):
self.assertEqual(len(last_batch), 0)
else:
result.append(last_batch)
for t in threads:
t.join()
assert_items_equal(self,
result,
get_batches(self.test_array, block_size),
key=lambda x: x[0, 0, 0])
queue.close()
def test_quickstart(self):
return
do_something = lambda x: x
stream = multitables.Streamer(filename=self.test_filename)
for row in stream.get_generator(path=self.test_array_path):
do_something(row)
def test_howto(self):
return
kw_args = {}
stream = multitables.Streamer(filename=self.test_filename, **kw_args)
do_something = lambda x: x
do_something_else = lambda x: x
queue = stream.get_queue(
path=self.test_array_path, # Path to dataset within the H5file.
n_procs=4, # Number of processes to launch for parallel reads. Defaults to 4.
read_ahead=5, # Size of internal buffer in no. of blocks. Defaults to 2*n_proc+1.
cyclic=False, # A cyclic reader wraps at the end of the dataset. Defaults to False.
block_size=32, # Size (along the outer dimension) of the blocks that will be read.
# Defaults to a multiple of the dataset chunk size, or a 128KB block.
# Should be left to the default or carefully chosen for chunked arrays,
# else performance degradation can occur.
ordered=False # Force the stream to return blocks in on-disk order. Useful if two
# datasets need to be read synchronously. This option may have a
# performance penalty.
)
while True:
guard = queue.get() # Get the guard object, will block until data is ready.
if guard is multitables.QueueClosed:
break # Terminate the loop once the dataset is finished.
with guard as block: # The guard returns the next block of data in the buffer.
do_something(block) # Perform actions on the data
while True:
guard = queue.get() # Get the guard object, will block until data is ready.
if guard is multitables.QueueClosed:
break # Terminate the loop once the dataset is finished.
with guard as block: # The guard returns the next block of data in the buffer.
do_something(block) # Perform actions on the data
for guard in queue.iter():
with guard as block:
do_something(block)
last_block = stream.get_remainder(self.test_array_path, queue.block_size)
queue.close()
queue = stream.get_queue(
path=self.test_array_path, # Path to dataset within the H5file.
n_procs=2, # Number of processes to launch for parallel reads. Defaults to 2.
read_ahead=5, # Size of internal buffer in no. of blocks. Defaults to 2*n_proc+1.
cyclic=True, # A cyclic reader wraps at the end of the dataset. Defaults to False.
)
while True:
with queue.get() as block:
do_something(block)
break
gen = stream.get_generator(self.test_array_path, n_procs=4, read_ahead=9, cyclic=False, block_size=32)
for row in gen:
do_something_else(row)
gen.close()
if __name__ == '__main__':
unittest.main()