| 9 | |
| 10 | |
| 11 | class BaseDataset: |
| 12 | |
| 13 | def __init__(self, |
| 14 | reader_cfg: Optional[Dict] = {}, |
| 15 | k: Union[int, List[int]] = 1, |
| 16 | n: int = 1, |
| 17 | **kwargs): |
| 18 | abbr = kwargs.pop('abbr', 'dataset') |
| 19 | dataset = self.load(**kwargs) |
| 20 | # maybe duplicate |
| 21 | assert (max(k) if isinstance(k, List) else |
| 22 | k) <= n, 'Maximum value of `k` must less than or equal to `n`' |
| 23 | if isinstance(dataset, Dataset): |
| 24 | dataset = dataset.map(lambda x, idx: { |
| 25 | 'subdivision': abbr, |
| 26 | 'idx': idx |
| 27 | }, |
| 28 | with_indices=True, |
| 29 | writer_batch_size=16, |
| 30 | load_from_cache_file=False) |
| 31 | dataset = concatenate_datasets([dataset] * n) |
| 32 | self.dataset = dataset |
| 33 | else: |
| 34 | self.dataset = DatasetDict() |
| 35 | for key in dataset: |
| 36 | dataset[key] = dataset[key].map(lambda x, idx: { |
| 37 | 'subdivision': f'{abbr}_{key}', |
| 38 | 'idx': idx |
| 39 | }, |
| 40 | with_indices=True, |
| 41 | writer_batch_size=16, |
| 42 | load_from_cache_file=False) |
| 43 | dataset[key] = concatenate_datasets([dataset[key]] * n) |
| 44 | self.dataset[key] = dataset[key] |
| 45 | self._init_reader(**reader_cfg) |
| 46 | |
| 47 | def _init_reader(self, **kwargs): |
| 48 | self.reader = DatasetReader(self.dataset, **kwargs) |
| 49 | |
| 50 | @property |
| 51 | def train(self): |
| 52 | return self.reader.dataset['train'] |
| 53 | |
| 54 | @property |
| 55 | def test(self): |
| 56 | return self.reader.dataset['test'] |
| 57 | |
| 58 | @staticmethod |
| 59 | def load(**kwargs) -> Union[Dataset, DatasetDict]: |
| 60 | pass |
nothing calls this directly
no outgoing calls
no test coverage detected