|
| 1 | +""" |
| 2 | +Base class for conditions. |
| 3 | +""" |
| 4 | + |
| 5 | +from functools import partial |
| 6 | +import torch |
| 7 | +from torch_geometric.data import Batch |
| 8 | +from torch.utils.data import DataLoader |
| 9 | +from .condition_interface import ConditionInterface |
| 10 | +from ..graph import LabelBatch |
| 11 | +from ..label_tensor import LabelTensor |
| 12 | + |
| 13 | + |
| 14 | +class ConditionBase(ConditionInterface): |
| 15 | + """ |
| 16 | + Base abstract class for all conditions in PINA. |
| 17 | + This class provides common functionality for handling data storage, |
| 18 | + batching, and interaction with the associated problem. |
| 19 | + """ |
| 20 | + |
| 21 | + collate_fn_dict = { |
| 22 | + "tensor": torch.stack, |
| 23 | + "label_tensor": LabelTensor.stack, |
| 24 | + "graph": LabelBatch.from_data_list, |
| 25 | + "data": Batch.from_data_list, |
| 26 | + } |
| 27 | + |
| 28 | + def __init__(self, **kwargs): |
| 29 | + """ |
| 30 | + Initialization of the :class:`ConditionBase` class. |
| 31 | +
|
| 32 | + :param kwargs: Keyword arguments representing the data to be stored. |
| 33 | + """ |
| 34 | + super().__init__() |
| 35 | + self.data = self.store_data(**kwargs) |
| 36 | + |
| 37 | + @property |
| 38 | + def problem(self): |
| 39 | + """ |
| 40 | + Return the problem associated with this condition. |
| 41 | +
|
| 42 | + :return: Problem associated with this condition. |
| 43 | + :rtype: ~pina.problem.abstract_problem.AbstractProblem |
| 44 | + """ |
| 45 | + return self._problem |
| 46 | + |
| 47 | + @problem.setter |
| 48 | + def problem(self, value): |
| 49 | + """ |
| 50 | + Set the problem associated with this condition. |
| 51 | +
|
| 52 | + :param pina.problem.abstract_problem.AbstractProblem value: The problem |
| 53 | + to associate with this condition |
| 54 | + """ |
| 55 | + self._problem = value |
| 56 | + |
| 57 | + def __len__(self): |
| 58 | + """ |
| 59 | + Return the number of data points in the condition. |
| 60 | +
|
| 61 | + :return: Number of data points. |
| 62 | + :rtype: int |
| 63 | + """ |
| 64 | + return len(self.data) |
| 65 | + |
| 66 | + def __getitem__(self, idx): |
| 67 | + """ |
| 68 | + Return the data point(s) at the specified index. |
| 69 | +
|
| 70 | + :param idx: Index(es) of the data point(s) to retrieve. |
| 71 | + :type idx: int | list[int] |
| 72 | + :return: Data point(s) at the specified index. |
| 73 | + """ |
| 74 | + return self.data[idx] |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def automatic_batching_collate_fn(cls, batch): |
| 78 | + """ |
| 79 | + Collate function for automatic batching to be used in DataLoader. |
| 80 | + :param batch: A list of items from the dataset. |
| 81 | + :type batch: list |
| 82 | + :return: A collated batch. |
| 83 | + :rtype: dict |
| 84 | + """ |
| 85 | + if not batch: |
| 86 | + return {} |
| 87 | + instance_class = batch[0].__class__ |
| 88 | + return instance_class.create_batch(batch) |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def collate_fn(batch, condition): |
| 92 | + """ |
| 93 | + Collate function for custom batching to be used in DataLoader. |
| 94 | +
|
| 95 | + :param batch: A list of items from the dataset. |
| 96 | + :type batch: list |
| 97 | + :param condition: The condition instance. |
| 98 | + :type condition: ConditionBase |
| 99 | + :return: A collated batch. |
| 100 | + :rtype: dict |
| 101 | + """ |
| 102 | + data = condition.data[batch].to_batch() |
| 103 | + return data |
| 104 | + |
| 105 | + def create_dataloader( |
| 106 | + self, dataset, batch_size, shuffle, automatic_batching |
| 107 | + ): |
| 108 | + """ |
| 109 | + Create a DataLoader for the condition. |
| 110 | +
|
| 111 | + :param int batch_size: The batch size for the DataLoader. |
| 112 | + :param bool shuffle: Whether to shuffle the data. Default is ``False``. |
| 113 | + :return: The DataLoader for the condition. |
| 114 | + :rtype: torch.utils.data.DataLoader |
| 115 | + """ |
| 116 | + if batch_size == len(dataset): |
| 117 | + pass # will be updated in the near future |
| 118 | + return DataLoader( |
| 119 | + dataset=dataset, |
| 120 | + batch_size=batch_size, |
| 121 | + shuffle=shuffle, |
| 122 | + collate_fn=( |
| 123 | + partial(self.collate_fn, condition=self) |
| 124 | + if not automatic_batching |
| 125 | + else self.automatic_batching_collate_fn |
| 126 | + ), |
| 127 | + ) |
0 commit comments