File: test_core_DataChunkIterator.py

package info (click to toggle)
hdmf 3.14.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,372 kB
  • sloc: python: 34,738; makefile: 303; sh: 35
file content (459 lines) | stat: -rw-r--r-- 18,929 bytes parent folder | download | duplicates (4)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import numpy as np

from hdmf.data_utils import DataChunkIterator, DataChunk
from hdmf.testing import TestCase


class DataChunkIteratorTests(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_none_iter(self):
        """Test that DataChunkIterator __init__ sets defaults correctly and all chunks and recommended shapes are None.
        """
        dci = DataChunkIterator(dtype=np.dtype('int'))
        self.assertIsNone(dci.maxshape)
        self.assertEqual(dci.dtype, np.dtype('int'))
        self.assertEqual(dci.buffer_size, 1)
        self.assertEqual(dci.iter_axis, 0)
        count = 0
        for chunk in dci:
            pass
        self.assertEqual(count, 0)
        self.assertIsNone(dci.recommended_data_shape())
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_list_none(self):
        """Test that DataChunkIterator has no dtype or chunks when given a list of None.
        """
        a = [None, None, None]
        with self.assertRaisesWith(Exception, 'Data type could not be determined. Please specify dtype in '
                                              'DataChunkIterator init.'):
            DataChunkIterator(a)

    def test_list_none_dtype(self):
        """Test that DataChunkIterator has the passed-in dtype and no chunks when given a list of None.
        """
        a = [None, None, None]
        dci = DataChunkIterator(a, dtype=np.dtype('int'))
        self.assertTupleEqual(dci.maxshape, (3,))
        self.assertEqual(dci.dtype, np.dtype('int'))
        count = 0
        for chunk in dci:
            pass
        self.assertEqual(count, 0)
        self.assertTupleEqual(dci.recommended_data_shape(), (3,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_unbuffered_first_axis(self):
        """Test DataChunkIterator with numpy data, no buffering, and iterating on the first dimension.
        """
        a = np.arange(30).reshape(5, 2, 3)
        dci = DataChunkIterator(data=a, buffer_size=1)
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.shape, (1, 2, 3))
            count += 1
        self.assertEqual(count, 5)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_unbuffered_middle_axis(self):
        """Test DataChunkIterator with numpy data, no buffering, and iterating on a middle dimension.
        """
        a = np.arange(30).reshape(5, 2, 3)
        dci = DataChunkIterator(data=a, buffer_size=1, iter_axis=1)
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.shape, (5, 1, 3))
            count += 1
        self.assertEqual(count, 2)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_unbuffered_last_axis(self):
        """Test DataChunkIterator with numpy data, no buffering, and iterating on the last dimension.
        """
        a = np.arange(30).reshape(5, 2, 3)
        dci = DataChunkIterator(data=a, buffer_size=1, iter_axis=2)
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.shape, (5, 2, 1))
            count += 1
        self.assertEqual(count, 3)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_buffered_first_axis(self):
        """Test DataChunkIterator with numpy data, buffering, and iterating on the first dimension.
        """
        a = np.arange(30).reshape(5, 2, 3)
        dci = DataChunkIterator(data=a, buffer_size=2)
        count = 0
        for chunk in dci:
            if count < 2:
                self.assertTupleEqual(chunk.shape, (2, 2, 3))
            else:
                self.assertTupleEqual(chunk.shape, (1, 2, 3))
            count += 1
        self.assertEqual(count, 3)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_buffered_middle_axis(self):
        """Test DataChunkIterator with numpy data, buffering, and iterating on a middle dimension.
        """
        a = np.arange(45).reshape(5, 3, 3)
        dci = DataChunkIterator(data=a, buffer_size=2, iter_axis=1)
        count = 0
        for chunk in dci:
            if count < 1:
                self.assertTupleEqual(chunk.shape, (5, 2, 3))
            else:
                self.assertTupleEqual(chunk.shape, (5, 1, 3))
            count += 1
        self.assertEqual(count, 2)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_buffered_last_axis(self):
        """Test DataChunkIterator with numpy data, buffering, and iterating on the last dimension.
        """
        a = np.arange(30).reshape(5, 2, 3)
        dci = DataChunkIterator(data=a, buffer_size=2, iter_axis=2)
        count = 0
        for chunk in dci:
            if count < 1:
                self.assertTupleEqual(chunk.shape, (5, 2, 2))
            else:
                self.assertTupleEqual(chunk.shape, (5, 2, 1))
            count += 1
        self.assertEqual(count, 2)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_numpy_iter_unmatched_buffer_size(self):
        a = np.arange(10)
        dci = DataChunkIterator(data=a, buffer_size=3)
        self.assertTupleEqual(dci.maxshape, a.shape)
        self.assertEqual(dci.dtype, a.dtype)
        count = 0
        for chunk in dci:
            if count < 3:
                self.assertTupleEqual(chunk.data.shape, (3,))
            else:
                self.assertTupleEqual(chunk.data.shape, (1,))
            count += 1
        self.assertEqual(count, 4)
        self.assertTupleEqual(dci.recommended_data_shape(), a.shape)
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_standard_iterator_unbuffered(self):
        dci = DataChunkIterator(data=range(10), buffer_size=1)
        self.assertEqual(dci.dtype, np.dtype(int))
        self.assertTupleEqual(dci.maxshape, (10,))
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))  # Test before and after iteration
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.data.shape, (1,))
            count += 1
        self.assertEqual(count, 10)
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))  # Test before and after iteration
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_standard_iterator_unmatched_buffersized(self):
        dci = DataChunkIterator(data=range(10), buffer_size=3)
        self.assertEqual(dci.dtype, np.dtype(int))
        self.assertTupleEqual(dci.maxshape, (10,))
        self.assertIsNone(dci.recommended_chunk_shape())
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))  # Test before and after iteration
        count = 0
        for chunk in dci:
            if count < 3:
                self.assertTupleEqual(chunk.data.shape, (3,))
            else:
                self.assertTupleEqual(chunk.data.shape, (1,))
            count += 1
        self.assertEqual(count, 4)
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))  # Test before and after iteration

    def test_multidimensional_list_first_axis(self):
        """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on the first dimension.
        """
        a = np.arange(30).reshape(5, 2, 3).tolist()
        dci = DataChunkIterator(a)
        self.assertTupleEqual(dci.maxshape, (5, 2, 3))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.data.shape, (1, 2, 3))
            count += 1
        self.assertEqual(count, 5)
        self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_multidimensional_list_middle_axis(self):
        """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on a middle dimension.
        """
        a = np.arange(30).reshape(5, 2, 3).tolist()
        warn_msg = ('Iterating over an axis other than the first dimension of list or tuple data '
                    'involves converting the data object to a numpy ndarray, which may incur a computational '
                    'cost.')
        with self.assertWarnsWith(UserWarning, warn_msg):
            dci = DataChunkIterator(a, iter_axis=1)
        self.assertTupleEqual(dci.maxshape, (5, 2, 3))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.data.shape, (5, 1, 3))
            count += 1
        self.assertEqual(count, 2)
        self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_multidimensional_list_last_axis(self):
        """Test DataChunkIterator with multidimensional list data, no buffering, and iterating on the last dimension.
        """
        a = np.arange(30).reshape(5, 2, 3).tolist()
        warn_msg = ('Iterating over an axis other than the first dimension of list or tuple data '
                    'involves converting the data object to a numpy ndarray, which may incur a computational '
                    'cost.')
        with self.assertWarnsWith(UserWarning, warn_msg):
            dci = DataChunkIterator(a, iter_axis=2)
        self.assertTupleEqual(dci.maxshape, (5, 2, 3))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.data.shape, (5, 2, 1))
            count += 1
        self.assertEqual(count, 3)
        self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_maxshape(self):
        a = np.arange(30).reshape(5, 2, 3)
        aiter = iter(a)
        daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
        self.assertEqual(daiter.maxshape, (None, 2, 3))

    def test_dtype(self):
        a = np.arange(30, dtype='int32').reshape(5, 2, 3)
        aiter = iter(a)
        daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
        self.assertEqual(daiter.dtype, a.dtype)

    def test_sparse_data_buffer_aligned(self):
        a = [1, 2, 3, 4, None, None, 7, 8, None, None]
        dci = DataChunkIterator(a, buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (10,))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertTupleEqual(chunk.data.shape, (2,))
            self.assertEqual(len(chunk.selection), 1)
            self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
            count += 1
        self.assertEqual(count, 3)
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_sparse_data_buffer_notaligned(self):
        a = [1, 2, 3, None, None, None, None, 8, 9, 10]
        dci = DataChunkIterator(a, buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (10,))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertEqual(len(chunk.selection), 1)
            if count == 0:  # [1, 2]
                self.assertListEqual(chunk.data.tolist(), [1, 2])
                self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
            elif count == 1:  # [3, None]
                self.assertListEqual(chunk.data.tolist(), [3, ])
                self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[0]))
            elif count == 2:  # [8, 9]
                self.assertListEqual(chunk.data.tolist(), [8, 9])
                self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[1]))
            else:  # count == 3, [10]
                self.assertListEqual(chunk.data.tolist(), [10, ])
                self.assertEqual(chunk.selection[0], slice(chunk.data[0] - 1, chunk.data[0]))
            count += 1
        self.assertEqual(count, 4)
        self.assertTupleEqual(dci.recommended_data_shape(), (10,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_start_with_none(self):
        a = [None, None, 3]
        dci = DataChunkIterator(a, buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (3,))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertListEqual(chunk.data.tolist(), [3])
            self.assertEqual(len(chunk.selection), 1)
            self.assertEqual(chunk.selection[0], slice(2, 3))
            count += 1
        self.assertEqual(count, 1)
        self.assertTupleEqual(dci.recommended_data_shape(), (3,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_list_scalar(self):
        a = [3]
        dci = DataChunkIterator(a, buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (1,))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertListEqual(chunk.data.tolist(), [3])
            self.assertEqual(len(chunk.selection), 1)
            self.assertEqual(chunk.selection[0], slice(0, 1))
            count += 1
        self.assertEqual(count, 1)
        self.assertTupleEqual(dci.recommended_data_shape(), (1,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_list_numpy_scalar(self):
        a = np.array([3])
        dci = DataChunkIterator(a, buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (1,))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertListEqual(chunk.data.tolist(), [3])
            self.assertEqual(len(chunk.selection), 1)
            self.assertEqual(chunk.selection[0], slice(0, 1))
            count += 1
        self.assertEqual(count, 1)
        self.assertTupleEqual(dci.recommended_data_shape(), (1,))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_set_maxshape(self):
        a = np.array([3])
        dci = DataChunkIterator(a, maxshape=(5, 2, 3), buffer_size=2)
        self.assertTupleEqual(dci.maxshape, (5, 2, 3))
        self.assertEqual(dci.dtype, np.dtype(int))
        count = 0
        for chunk in dci:
            self.assertListEqual(chunk.data.tolist(), [3])
            self.assertTupleEqual(chunk.selection, (slice(0, 1), slice(None), slice(None)))
            count += 1
        self.assertEqual(count, 1)
        self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_custom_iter_first_axis(self):
        def my_iter():
            count = 0
            a = np.arange(30).reshape(5, 2, 3)
            while count < a.shape[0]:
                val = a[count, :, :]
                count = count + 1
                yield val
            return
        dci = DataChunkIterator(data=my_iter(), buffer_size=2)
        count = 0
        for chunk in dci:
            if count < 2:
                self.assertTupleEqual(chunk.shape, (2, 2, 3))
            else:
                self.assertTupleEqual(chunk.shape, (1, 2, 3))
            count += 1
        self.assertEqual(count, 3)
        # self.assertTupleEqual(dci.recommended_data_shape(), (2, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_custom_iter_middle_axis(self):
        def my_iter():
            count = 0
            a = np.arange(45).reshape(5, 3, 3)
            while count < a.shape[1]:
                val = a[:, count, :]
                count = count + 1
                yield val
            return
        dci = DataChunkIterator(data=my_iter(), buffer_size=2, iter_axis=1)
        count = 0
        for chunk in dci:
            if count < 1:
                self.assertTupleEqual(chunk.shape, (5, 2, 3))
            else:
                self.assertTupleEqual(chunk.shape, (5, 1, 3))
            count += 1
        self.assertEqual(count, 2)
        # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 3))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_custom_iter_last_axis(self):
        def my_iter():
            count = 0
            a = np.arange(30).reshape(5, 2, 3)
            while count < a.shape[2]:
                val = a[:, :, count]
                count = count + 1
                yield val
            return
        dci = DataChunkIterator(data=my_iter(), buffer_size=2, iter_axis=2)
        count = 0
        for chunk in dci:
            if count < 1:
                self.assertTupleEqual(chunk.shape, (5, 2, 2))
            else:
                self.assertTupleEqual(chunk.shape, (5, 2, 1))
            count += 1
        self.assertEqual(count, 2)
        # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 2))
        self.assertIsNone(dci.recommended_chunk_shape())

    def test_custom_iter_mismatched_axis(self):
        def my_iter():
            count = 0
            a = np.arange(30).reshape(5, 2, 3)
            while count < a.shape[2]:
                val = a[:, :, count]
                count = count + 1
                yield val
            return
        # iterator returns slices of size (5, 2)
        # because iter_axis is by default 0, these chunks will be placed along the first dimension
        dci = DataChunkIterator(data=my_iter(), buffer_size=2)
        count = 0
        for chunk in dci:
            if count < 1:
                self.assertTupleEqual(chunk.shape, (2, 5, 2))
            else:
                self.assertTupleEqual(chunk.shape, (1, 5, 2))
            count += 1
        self.assertEqual(count, 2)
        # self.assertTupleEqual(dci.recommended_data_shape(), (5, 2, 2))
        self.assertIsNone(dci.recommended_chunk_shape())


class DataChunkTests(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_len_operator_no_data(self):
        temp = DataChunk()
        self.assertEqual(len(temp), 0)

    def test_len_operator_with_data(self):
        temp = DataChunk(np.arange(10).reshape(5, 2))
        self.assertEqual(len(temp), 5)

    def test_dtype(self):
        temp = DataChunk(np.arange(10).astype('int'))
        temp_dtype = temp.dtype
        self.assertEqual(temp_dtype, np.dtype('int'))

    def test_astype(self):
        temp1 = DataChunk(np.arange(10).reshape(5, 2))
        temp2 = temp1.astype('float32')
        self.assertEqual(temp2.dtype, np.dtype('float32'))