| 27 | |
| 28 | |
| 29 | class MyTestCase(unittest.TestCase): |
| 30 | |
| 31 | @classmethod |
| 32 | def setUpClass(cls): |
| 33 | if os.path.exists('tmp_test_chunked_memmap'): |
| 34 | raise RuntimeError('make sure tmp_test_chunked_memmap is empty to run the test') |
| 35 | |
| 36 | def test_write(self): |
| 37 | |
| 38 | # create random array |
| 39 | samples_shape = [20, 10, 4] |
| 40 | working_dir = 'tmp_test_chunked_memmap' |
| 41 | samples = np.random.randn(*samples_shape).astype('float32') |
| 42 | |
| 43 | # determine chunk_idxs |
| 44 | total_chunks = 3 |
| 45 | chunk_idxs = np.random.randint(total_chunks, size=samples_shape[0]) |
| 46 | |
| 47 | # create chunked_memmap |
| 48 | chunked_memmap = ChunkedMemmap(working_dir, remove_exist=True) |
| 49 | chunked_memmap.add_all_samples(samples, chunk_idxs, dtype='float32') |
| 50 | |
| 51 | # check if the same |
| 52 | arr = np.zeros(samples_shape) |
| 53 | for i in range(samples_shape[0]): |
| 54 | arr[i] = chunked_memmap[i] |
| 55 | |
| 56 | assert np.allclose(samples, arr) |
| 57 | |
| 58 | |
| 59 | def test_multithread_write(self): |
| 60 | |
| 61 | # create random array |
| 62 | samples_shape = [2000, 10, 4] |
| 63 | working_dir = 'tmp_test_chunked_memmap' |
| 64 | samples = np.random.randn(*samples_shape).astype('float32') |
| 65 | num_workers = 5 |
| 66 | |
| 67 | # determine chunk_idxs |
| 68 | total_chunks = 3 |
| 69 | chunk_idxs = np.random.randint(total_chunks, size=samples_shape[0]) |
| 70 | |
| 71 | # create chunked_memmap |
| 72 | chunked_memmap = ChunkedMemmap(working_dir, remove_exist=True) |
| 73 | chunked_memmap.add_all_samples(samples, chunk_idxs, dtype='float32', num_workers=num_workers) |
| 74 | |
| 75 | # check if the same |
| 76 | arr = np.zeros(samples_shape) |
| 77 | for i in range(samples_shape[0]): |
| 78 | arr[i] = chunked_memmap[i] |
| 79 | |
| 80 | assert np.allclose(samples, arr) |
| 81 | |
| 82 | |
| 83 | def test_load_existing(self): |
| 84 | |
| 85 | # create random array |
| 86 | samples_shape = [1000, 10, 4] |
nothing calls this directly
no outgoing calls
no test coverage detected