Split a dataset into subsets given proportions of how much to allocate per split. If a split is 0% returns None for that split. Purpose: Useful for creating train/val/test splits Arguments: ds (Dataset or array-like): Data to be split. split (1D array-like): proporti
(ds, split=[.8,.2,.0], block_size = 10000, seed=131)
| 271 | return max(split) / sum(split) != 1. |
| 272 | |
| 273 | def split_ds(ds, split=[.8,.2,.0], block_size = 10000, seed=131): |
| 274 | """ |
| 275 | Split a dataset into subsets given proportions of how |
| 276 | much to allocate per split. If a split is 0% returns None for that split. |
| 277 | Purpose: Useful for creating train/val/test splits |
| 278 | Arguments: |
| 279 | ds (Dataset or array-like): Data to be split. |
| 280 | split (1D array-like): proportions to split `ds`. `sum(splits) != 0` |
| 281 | shuffle (boolean): Randomly split dataset. Default: True |
| 282 | """ |
| 283 | split_sum = sum(split) |
| 284 | if split_sum == 0: |
| 285 | raise Exception('Split cannot sum to 0.') |
| 286 | split = np.array(split) |
| 287 | split /= split_sum |
| 288 | |
| 289 | assert block_size <= len(ds) |
| 290 | |
| 291 | start_idx = 0 |
| 292 | residual_idx = 0 |
| 293 | rtn_ds = [None]*len(split) |
| 294 | rng = np.random.default_rng(seed) |
| 295 | indices = rng.permutation(np.array(range(block_size))) |
| 296 | for i, f in enumerate(split): |
| 297 | if f != 0: |
| 298 | proportion = block_size*split[i] |
| 299 | residual_idx += proportion % 1 |
| 300 | split_ = int(int(proportion) + residual_idx) |
| 301 | rtn_ds[i] = BlockedRandomSplitDataset(ds, indices[range(start_idx, start_idx+max(split_, 1))], block_size) |
| 302 | start_idx += split_ |
| 303 | residual_idx %= 1 |
| 304 | return rtn_ds |
| 305 | |
| 306 | class ConcatDataset(data.Dataset): |
| 307 | """ |
no test coverage detected