| 43 | |
| 44 | |
| 45 | class MyTestCase(unittest.TestCase): |
| 46 | |
| 47 | @staticmethod |
| 48 | def _create_dataset(total_samples, seq_len, dim, val=None): |
| 49 | """ |
| 50 | Return a pytorch dataset instance, which contains (total_samples, dim). |
| 51 | :param val: constant fill in value. None: random array |
| 52 | """ |
| 53 | if val is None: |
| 54 | arr = np.random.randn(total_samples, seq_len, dim) |
| 55 | else: |
| 56 | arr = np.ones((total_samples, seq_len, dim)) * val |
| 57 | arr = arr.astype(dtype='int32') |
| 58 | return TMPDataset(arr) |
| 59 | |
| 60 | |
| 61 | def _test(self, |
| 62 | datasets: T.List[TMPDataset], |
| 63 | dataset_ratios: T.List[float], |
| 64 | dataset_max_samples: T.List[int], |
| 65 | batch_size=10, |
| 66 | num_workers=0, |
| 67 | batch_wrt_length=True, |
| 68 | max_total_samples_per_epoch=-1, |
| 69 | max_batch_combined_size=-1, |
| 70 | batch_sampler_type='sort', |
| 71 | ): |
| 72 | """ |
| 73 | test plan: |
| 74 | - given created random datasets of different sizes |
| 75 | - create batch_preparer of different dataset ratios |
| 76 | - check the sum of the batches returned by the batch_preparer and the sum of the datasets (possibly weighted) |
| 77 | """ |
| 78 | |
| 79 | # create batch_preparer |
| 80 | batch_preparer = BatchPreparer( |
| 81 | batch_size=batch_size, |
| 82 | collate_fn=TMPCollate(), |
| 83 | num_workers=num_workers, |
| 84 | batch_wrt_length=batch_wrt_length, |
| 85 | shuffle=True, |
| 86 | max_total_samples_per_epoch=max_total_samples_per_epoch, |
| 87 | max_batch_combined_size=max_batch_combined_size, |
| 88 | batch_sampler_type=batch_sampler_type, |
| 89 | ) |
| 90 | # add datasets into batch_preparer |
| 91 | for i in range(len(datasets)): |
| 92 | batch_preparer.add_dataset(datasets[i], |
| 93 | ratio=dataset_ratios[i], |
| 94 | max_samples_per_epoch=dataset_max_samples[i]) |
| 95 | # test |
| 96 | # collect the batches |
| 97 | batches = [] |
| 98 | for i, batch in enumerate(batch_preparer): |
| 99 | batches.append(batch) |
| 100 | |
| 101 | # if all ratios are 1.0, we check if the sum is correct |
| 102 | if np.allclose(dataset_ratios, 1.0): |
nothing calls this directly
no outgoing calls
no test coverage detected