Debian Codegraph @

tests/unit/utils_test/test_core_DataChunkIterator.py

raw
1 import numpy as np
2
3 from hdmf.data_utils import DataChunkIterator, DataChunk
4 from hdmf.testing import TestCase
5
6
7 class DataChunkIteratorTests(TestCase):
8
9 def setUp(self):
10 pass
11
12 def tearDown(self):
13 pass
14
15 def test_none_iter(self):
16 """Test that DataChunkIterator __init__ sets defaults correctly and all chunks and recommended shapes are None.
17 """
18 dci = DataChunkIterator(dtype=np.dtype('int'))
19 self.assertIsNone(dci.maxshape)
20 self.assertEqual(dci.dtype, np.dtype('int'))
21 self.assertEqual(dci.buffer_size, 1)
22 self.assertEqual(dci.iter_axis, 0)
23 count = 0
24 for chunk in dci:
25 pass
26 self.assertEqual(count, 0)
27 self.assertIsNone(dci.recommended_data_shape())
28 self.assertIsNone(dci.recommended_chunk_shape())
29
30 def test_list_none(self):
31 """Test that DataChunkIterator has no dtype or chunks when given a list of None.
32 """
33 a = [None, None, None]
34 with self.assertRaisesWith(Exception, 'Data type could not be determined. Please specify dtype in '
35 'DataChunkIterator init.'):
36 DataChunkIterator(a)
37
38 def test_list_none_dtype(self):
39 """Test that DataChunkIterator has the passed-in dtype and no chunks when given a list of None.
40 """
41 a = [None, None, None]
42 dci = DataChunkIterator(a, dtype=np.dtype('int'))
43 self.assertTupleEqual(dci.maxshape, (3,))
44 self.assertEqual(dci.dtype, np.dtype('int'))
45 count = 0
46 for chunk in dci:
47 pass
48 self.assertEqual(count, 0)
49 self.assertTupleEqual(dci.recommended_data_shape(), (3,))
50 self.assertIsNone(dci.recommended_chunk_shape())
51
52 def test_numpy_iter_unbuffered_first_axis(self):
53 """Test DataChunkIterator with numpy data, no buffering, and iterating on the first dimension.
54 """
55 a = np.arange(30).reshape(5, 2, 3)
56 dci = DataChunkIterator(data=a, buffer_size=1)
57 count = 0
58 for chunk in dci:
59 self.assertTupleEqual(chunk.shape, (1, 2, 3))
60 count += 1
61 self.assertEqual(count, 5)
62 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
63 self.assertIsNone(dci.recommended_chunk_shape())
64
65 def test_numpy_iter_unbuffered_middle_axis(self):
66 """Test DataChunkIterator with numpy data, no buffering, and iterating on a middle dimension.
67 """
68 a = np.arange(30).reshape(5, 2, 3)
69 dci = DataChunkIterator(data=a, buffer_size=1, iter_axis=1)
70 count = 0
71 for chunk in dci:
72 self.assertTupleEqual(chunk.shape, (5, 1, 3))
73 count += 1
74 self.assertEqual(count, 2)
75 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
76 self.assertIsNone(dci.recommended_chunk_shape())
77
78 def test_numpy_iter_unbuffered_last_axis(self):
79 """Test DataChunkIterator with numpy data, no buffering, and iterating on the last dimension.
80 """
81 a = np.arange(30).reshape(5, 2, 3)
82 dci = DataChunkIterator(data=a, buffer_size=1, iter_axis=2)
83 count = 0
84 for chunk in dci:
85 self.assertTupleEqual(chunk.shape, (5, 2, 1))
86 count += 1
87 self.assertEqual(count, 3)
88 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
89 self.assertIsNone(dci.recommended_chunk_shape())
90
91 def test_numpy_iter_buffered_first_axis(self):
92 """Test DataChunkIterator with numpy data, buffering, and iterating on the first dimension.
93 """
94 a = np.arange(30).reshape(5, 2, 3)
95 dci = DataChunkIterator(data=a, buffer_size=2)
96 count = 0
97 for chunk in dci:
98 if count < 2:
99 self.assertTupleEqual(chunk.shape, (2, 2, 3))
100 else:
101 self.assertTupleEqual(chunk.shape, (1, 2, 3))
102 count += 1
103 self.assertEqual(count, 3)
104 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
105 self.assertIsNone(dci.recommended_chunk_shape())
106
107 def test_numpy_iter_buffered_middle_axis(self):
108 """Test DataChunkIterator with numpy data, buffering, and iterating on a middle dimension.
109 """
110 a = np.arange(45).reshape(5, 3, 3)
111 dci = DataChunkIterator(data=a, buffer_size=2, iter_axis=1)
112 count = 0
113 for chunk in dci:
114 if count < 1:
115 self.assertTupleEqual(chunk.shape, (5, 2, 3))
116 else:
117 self.assertTupleEqual(chunk.shape, (5, 1, 3))
118 count += 1
119 self.assertEqual(count, 2)
120 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
121 self.assertIsNone(dci.recommended_chunk_shape())
122
123 def test_numpy_iter_buffered_last_axis(self):
124 """Test DataChunkIterator with numpy data, buffering, and iterating on the last dimension.
125 """
126 a = np.arange(30).reshape(5, 2, 3)
127 dci = DataChunkIterator(data=a, buffer_size=2, iter_axis=2)
128 count = 0
129 for chunk in dci:
130 if count < 1:
131 self.assertTupleEqual(chunk.shape, (5, 2, 2))
132 else:
133 self.assertTupleEqual(chunk.shape, (5, 2, 1))
134 count += 1
135 self.assertEqual(count, 2)
136 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
137 self.assertIsNone(dci.recommended_chunk_shape())
138
139 def test_numpy_iter_unmatched_buffer_size(self):
140 a = np.arange(10)
141 dci = DataChunkIterator(data=a, buffer_size=3)
142 self.assertTupleEqual(dci.maxshape, a.shape)
143 self.assertEqual(dci.dtype, a.dtype)
144 count = 0
145 for chunk in dci:
146 if count < 3:
147 self.assertTupleEqual(chunk.data.shape, (3,))
148 else:
149 self.assertTupleEqual(chunk.data.shape, (1,))
150 count += 1
151 self.assertEqual(count, 4)
152 self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
153 self.assertIsNone(dci.recommended_chunk_shape())
154
155 def test_standard_iterator_unbuffered(self):
156 dci = DataChunkIterator(data=range(10), buffer_size=1)
157 self.assertEqual(dci.dtype, np.dtype(int))
158 self.assertTupleEqual(dci.maxshape, (10,))
159 self.assertTupleEqual(dci.recommended_data_shape(), (10,)) # Test before and after iteration
160 count = 0
161 for chunk in dci:
162 self.assertTupleEqual(chunk.data.shape, (1,))
163 count += 1
164 self.assertEqual(count, 10)
165 self.assertTupleEqual(dci.recommended_data_shape(), (10,)) # Test before and after iteration
166 self.assertIsNone(dci.recommended_chunk_shape())
167
168 def test_standard_iterator_unmatched_buffersized(self):
169 dci = DataChunkIterator(data=range(10), buffer_size=3)
170 self.assertEqual(dci.dtype, np.dtype(int))
171 self.assertTupleEqual(dci.maxshape, (10,))
172 self.assertIsNone(dci.recommended_chunk_shape())
173 self.assertTupleEqual(dci.recommended_data_shape(), (10,)) # Test before and after iteration
174 count = 0
175 for chunk in dci:
176 if count < 3:
177 self.assertTupleEqual(chunk.data.shape, (3,))
178 else:
179 self.assertTupleEqual(chunk.data.shape, (1,))
180 count += 1
181 self.assertEqual(count, 4)
182 self.assertTupleEqual(dci.recommended_data_shape(), (10,)) # Test before and after iteration
183
184 def test_multidimensional_list_first_axis(self):
185 """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on the first dimension.
186 """
187 a = np.arange(30).reshape(5, 2, 3).tolist()
188 dci = DataChunkIterator(a)
189 self.assertTupleEqual(dci.maxshape, (5, 2, 3))
190 self.assertEqual(dci.dtype, np.dtype(int))
191 count = 0
192 for chunk in dci:
193 self.assertTupleEqual(chunk.data.shape, (1, 2, 3))
194 count += 1
195 self.assertEqual(count, 5)
196 self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
197 self.assertIsNone(dci.recommended_chunk_shape())
198
199 def test_multidimensional_list_middle_axis(self):
200 """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on a middle dimension.
201 """
202 a = np.arange(30).reshape(5, 2, 3).tolist()
203 warn_msg = ('Iterating over an axis other than the first dimension of list or tuple data '
204 'involves converting the data object to a numpy ndarray, which may incur a computational '
205 'cost.')
206 with self.assertWarnsWith(UserWarning, warn_msg):
207 dci = DataChunkIterator(a, iter_axis=1)
208 self.assertTupleEqual(dci.maxshape, (5, 2, 3))
209 self.assertEqual(dci.dtype, np.dtype(int))
210 count = 0
211 for chunk in dci:
212 self.assertTupleEqual(chunk.data.shape, (5, 1, 3))
213 count += 1
214 self.assertEqual(count, 2)
215 self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
216 self.assertIsNone(dci.recommended_chunk_shape())
217
218 def test_multidimensional_list_last_axis(self):
219 """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on the last dimension.
220 """
221 a = np.arange(30).reshape(5, 2, 3).tolist()
222 warn_msg = ('Iterating over an axis other than the first dimension of list or tuple data '
223 'involves converting the data object to a numpy ndarray, which may incur a computational '
224 'cost.')
225 with self.assertWarnsWith(UserWarning, warn_msg):
226 dci = DataChunkIterator(a, iter_axis=2)
227 self.assertTupleEqual(dci.maxshape, (5, 2, 3))
228 self.assertEqual(dci.dtype, np.dtype(int))
229 count = 0
230 for chunk in dci:
231 self.assertTupleEqual(chunk.data.shape, (5, 2, 1))
232 count += 1
233 self.assertEqual(count, 3)
234 self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
235 self.assertIsNone(dci.recommended_chunk_shape())
236
237 def test_maxshape(self):
238 a = np.arange(30).reshape(5, 2, 3)
239 aiter = iter(a)
240 daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
241 self.assertEqual(daiter.maxshape, (None, 2, 3))
242
243 def test_dtype(self):
244 a = np.arange(30, dtype='int32').reshape(5, 2, 3)
245 aiter = iter(a)
246 daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
247 self.assertEqual(daiter.dtype, a.dtype)
248
249 def test_sparse_data_buffer_aligned(self):
250 a = [1, 2, 3, 4, None, None, 7, 8, None, None]
251 dci = DataChunkIterator(a, buffer_size=2)
252 self.assertTupleEqual(dci.maxshape, (10,))
253 self.assertEqual(dci.dtype, np.dtype(int))
254 count = 0
255 for chunk in dci:
256 self.assertTupleEqual(chunk.data.shape, (2,))
257 self.assertEqual(len(chunk.selection), 1)
258 self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
259 count += 1
260 self.assertEqual(count, 3)
261 self.assertTupleEqual(dci.recommended_data_shape(), (10,))
262 self.assertIsNone(dci.recommended_chunk_shape())
263
264 def test_sparse_data_buffer_notaligned(self):
265 a = [1, 2, 3, None, None, None, None, 8, 9, 10]
266 dci = DataChunkIterator(a, buffer_size=2)
267 self.assertTupleEqual(dci.maxshape, (10,))
268 self.assertEqual(dci.dtype, np.dtype(int))
269 count = 0
270 for chunk in dci:
271 self.assertEqual(len(chunk.selection), 1)
272 if count == 0: # [1, 2]
273 self.assertListEqual(chunk.data.tolist(), [1, 2])
274 self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
275 elif count == 1: # [3, None]
276 self.assertListEqual(chunk.data.tolist(), [3, ])
277 self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[0]))
278 elif count == 2: # [8, 9]
279 self.assertListEqual(chunk.data.tolist(), [8, 9])
280 self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
281 else: # count == 3, [10]
282 self.assertListEqual(chunk.data.tolist(), [10, ])
283 self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[0]))
284 count += 1
285 self.assertEqual(count, 4)
286 self.assertTupleEqual(dci.recommended_data_shape(), (10,))
287 self.assertIsNone(dci.recommended_chunk_shape())
288
289 def test_start_with_none(self):
290 a = [None, None, 3]
291 dci = DataChunkIterator(a, buffer_size=2)
292 self.assertTupleEqual(dci.maxshape, (3,))
293 self.assertEqual(dci.dtype, np.dtype(int))
294 count = 0
295 for chunk in dci:
296 self.assertListEqual(chunk.data.tolist(), [3])
297 self.assertEqual(len(chunk.selection), 1)
298 self.assertEqual(chunk.selection[0], slice(2, 3))
299 count += 1
300 self.assertEqual(count, 1)
301 self.assertTupleEqual(dci.recommended_data_shape(), (3,))
302 self.assertIsNone(dci.recommended_chunk_shape())
303
304 def test_list_scalar(self):
305 a = [3]
306 dci = DataChunkIterator(a, buffer_size=2)
307 self.assertTupleEqual(dci.maxshape, (1,))
308 self.assertEqual(dci.dtype, np.dtype(int))
309 count = 0
310 for chunk in dci:
311 self.assertListEqual(chunk.data.tolist(), [3])
312 self.assertEqual(len(chunk.selection), 1)
313 self.assertEqual(chunk.selection[0], slice(0, 1))
314 count += 1
315 self.assertEqual(count, 1)
316 self.assertTupleEqual(dci.recommended_data_shape(), (1,))
317 self.assertIsNone(dci.recommended_chunk_shape())
318
319 def test_list_numpy_scalar(self):
320 a = np.array([3])
321 dci = DataChunkIterator(a, buffer_size=2)
322 self.assertTupleEqual(dci.maxshape, (1,))
323 self.assertEqual(dci.dtype, np.dtype(int))
324 count = 0
325 for chunk in dci:
326 self.assertListEqual(chunk.data.tolist(), [3])
327 self.assertEqual(len(chunk.selection), 1)
328 self.assertEqual(chunk.selection[0], slice(0, 1))
329 count += 1
330 self.assertEqual(count, 1)
331 self.assertTupleEqual(dci.recommended_data_shape(), (1,))
332 self.assertIsNone(dci.recommended_chunk_shape())
333
334 def test_set_maxshape(self):
335 a = np.array([3])
336 dci = DataChunkIterator(a, maxshape=(5, 2, 3), buffer_size=2)
337 self.assertTupleEqual(dci.maxshape, (5, 2, 3))
338 self.assertEqual(dci.dtype, np.dtype(int))
339 count = 0
340 for chunk in dci:
341 self.assertListEqual(chunk.data.tolist(), [3])
342 self.assertTupleEqual(chunk.selection, (slice(0, 1), slice(None), slice(None)))
343 count += 1
344 self.assertEqual(count, 1)
345 self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
346 self.assertIsNone(dci.recommended_chunk_shape())
347
348 def test_custom_iter_first_axis(self):
349 def my_iter():
350 count = 0
351 a = np.arange(30).reshape(5, 2, 3)
352 while count < a.shape[0]:
353 val = a[count, :, :]
354 count = count + 1
355 yield val
356 return
357 dci = DataChunkIterator(data=my_iter(), buffer_size=2)
358 count = 0
359 for chunk in dci:
360 if count < 2:
361 self.assertTupleEqual(chunk.shape, (2, 2, 3))
362 else:
363 self.assertTupleEqual(chunk.shape, (1, 2, 3))
364 count += 1
365 self.assertEqual(count, 3)
366 # self.assertTupleEqual(dci.recommended_data_shape(), (2, 2, 3))
367 self.assertIsNone(dci.recommended_chunk_shape())
368
369 def test_custom_iter_middle_axis(self):
370 def my_iter():
371 count = 0
372 a = np.arange(45).reshape(5, 3, 3)
373 while count < a.shape[1]:
374 val = a[:, count, :]
375 count = count + 1
376 yield val
377 return
378 dci = DataChunkIterator(data=my_iter(), buffer_size=2, iter_axis=1)
379 count = 0
380 for chunk in dci:
381 if count < 1:
382 self.assertTupleEqual(chunk.shape, (5, 2, 3))
383 else:
384 self.assertTupleEqual(chunk.shape, (5, 1, 3))
385 count += 1
386 self.assertEqual(count, 2)
387 # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
388 self.assertIsNone(dci.recommended_chunk_shape())
389
390 def test_custom_iter_last_axis(self):
391 def my_iter():
392 count = 0
393 a = np.arange(30).reshape(5, 2, 3)
394 while count < a.shape[2]:
395 val = a[:, :, count]
396 count = count + 1
397 yield val
398 return
399 dci = DataChunkIterator(data=my_iter(), buffer_size=2, iter_axis=2)
400 count = 0
401 for chunk in dci:
402 if count < 1:
403 self.assertTupleEqual(chunk.shape, (5, 2, 2))
404 else:
405 self.assertTupleEqual(chunk.shape, (5, 2, 1))
406 count += 1
407 self.assertEqual(count, 2)
408 # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 2))
409 self.assertIsNone(dci.recommended_chunk_shape())
410
411 def test_custom_iter_mismatched_axis(self):
412 def my_iter():
413 count = 0
414 a = np.arange(30).reshape(5, 2, 3)
415 while count < a.shape[2]:
416 val = a[:, :, count]
417 count = count + 1
418 yield val
419 return
420 # iterator returns slices of size (5, 2)
421 # because iter_axis is by default 0, these chunks will be placed along the first dimension
422 dci = DataChunkIterator(data=my_iter(), buffer_size=2)
423 count = 0
424 for chunk in dci:
425 if count < 1:
426 self.assertTupleEqual(chunk.shape, (2, 5, 2))
427 else:
428 self.assertTupleEqual(chunk.shape, (1, 5, 2))
429 count += 1
430 self.assertEqual(count, 2)
431 # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 2))
432 self.assertIsNone(dci.recommended_chunk_shape())
433
434
435 class DataChunkTests(TestCase):
436
437 def setUp(self):
438 pass
439
440 def tearDown(self):
441 pass
442
443 def test_len_operator_no_data(self):
444 temp = DataChunk()
445 self.assertEqual(len(temp), 0)
446
447 def test_len_operator_with_data(self):
448 temp = DataChunk(np.arange(10).reshape(5, 2))
449 self.assertEqual(len(temp), 5)
450
451 def test_dtype(self):
452 temp = DataChunk(np.arange(10).astype('int'))
453 temp_dtype = temp.dtype
454 self.assertEqual(temp_dtype, np.dtype('int'))
455
456 def test_astype(self):
457 temp1 = DataChunk(np.arange(10).reshape(5, 2))
458 temp2 = temp1.astype('float32')
459 self.assertEqual(temp2.dtype, np.dtype('float32'))