|
| 1 | +""" Data module for pytorch lightning """ |
| 2 | +from lightning.pytorch import LightningDataModule |
| 3 | +from ocf_data_sampler.numpy_sample.collate import stack_np_samples_into_batch |
| 4 | +from ocf_datapipes.batch import ( |
| 5 | + NumpyBatch, |
| 6 | + TensorBatch, |
| 7 | + batch_to_tensor, |
| 8 | +) |
| 9 | +from torch.utils.data import DataLoader, Dataset |
| 10 | + |
| 11 | + |
| 12 | +def collate_fn(samples: list[NumpyBatch]) -> TensorBatch: |
| 13 | + """Convert a list of NumpySample samples to a tensor batch""" |
| 14 | + return batch_to_tensor(stack_np_samples_into_batch(samples)) |
| 15 | + |
| 16 | + |
| 17 | +class BaseDataModule(LightningDataModule): |
| 18 | + """Base Datamodule for training pvnet and using pvnet pipeline in ocf-data-sampler.""" |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + configuration: str | None = None, |
| 23 | + sample_dir: str | None = None, |
| 24 | + batch_size: int = 16, |
| 25 | + num_workers: int = 0, |
| 26 | + prefetch_factor: int | None = None, |
| 27 | + train_period: list[str | None] = [None, None], |
| 28 | + val_period: list[str | None] = [None, None], |
| 29 | + ): |
| 30 | + """Base Datamodule for training pvnet architecture. |
| 31 | +
|
| 32 | + Can also be used with pre-made batches if `sample_dir` is set. |
| 33 | +
|
| 34 | + Args: |
| 35 | + configuration: Path to ocf-data-sampler configuration file. |
| 36 | + sample_dir: Path to the directory of pre-saved samples. Cannot be used together with |
| 37 | + `configuration` or '[train/val]_period'. |
| 38 | + batch_size: Batch size. |
| 39 | + num_workers: Number of workers to use in multiprocess batch loading. |
| 40 | + prefetch_factor: Number of data will be prefetched at the end of each worker process. |
| 41 | + train_period: Date range filter for train dataloader. |
| 42 | + val_period: Date range filter for val dataloader. |
| 43 | +
|
| 44 | + """ |
| 45 | + super().__init__() |
| 46 | + |
| 47 | + if not ((sample_dir is not None) ^ (configuration is not None)): |
| 48 | + raise ValueError("Exactly one of `sample_dir` or `configuration` must be set.") |
| 49 | + |
| 50 | + if sample_dir is not None: |
| 51 | + if any([period != [None, None] for period in [train_period, val_period]]): |
| 52 | + raise ValueError("Cannot set `(train/val)_period` with presaved samples") |
| 53 | + |
| 54 | + self.configuration = configuration |
| 55 | + self.sample_dir = sample_dir |
| 56 | + self.train_period = train_period |
| 57 | + self.val_period = val_period |
| 58 | + |
| 59 | + self._common_dataloader_kwargs = dict( |
| 60 | + batch_size=batch_size, |
| 61 | + sampler=None, |
| 62 | + batch_sampler=None, |
| 63 | + num_workers=num_workers, |
| 64 | + collate_fn=collate_fn, |
| 65 | + pin_memory=False, |
| 66 | + drop_last=False, |
| 67 | + timeout=0, |
| 68 | + worker_init_fn=None, |
| 69 | + prefetch_factor=prefetch_factor, |
| 70 | + persistent_workers=False, |
| 71 | + ) |
| 72 | + |
| 73 | + def _get_streamed_samples_dataset(self, start_time, end_time) -> Dataset: |
| 74 | + raise NotImplementedError |
| 75 | + |
| 76 | + def _get_premade_samples_dataset(self, subdir) -> Dataset: |
| 77 | + raise NotImplementedError |
| 78 | + |
| 79 | + def train_dataloader(self) -> DataLoader: |
| 80 | + """Construct train dataloader""" |
| 81 | + if self.sample_dir is not None: |
| 82 | + dataset = self._get_premade_samples_dataset("train") |
| 83 | + else: |
| 84 | + dataset = self._get_streamed_samples_dataset(*self.train_period) |
| 85 | + return DataLoader(dataset, shuffle=True, **self._common_dataloader_kwargs) |
| 86 | + |
| 87 | + def val_dataloader(self) -> DataLoader: |
| 88 | + """Construct val dataloader""" |
| 89 | + if self.sample_dir is not None: |
| 90 | + dataset = self._get_premade_samples_dataset("val") |
| 91 | + else: |
| 92 | + dataset = self._get_streamed_samples_dataset(*self.val_period) |
| 93 | + return DataLoader(dataset, shuffle=False, **self._common_dataloader_kwargs) |
0 commit comments