Skip to content

Datamodules

Reference information for the Datamodule classes and functions.

eva.data.DataModule

Bases: LightningDataModule

DataModule encapsulates all the steps needed to process data.

It will initialize and create the mapping between dataloaders and datasets. During the prepare_data, setup and teardown, the datamodule will call the respective methods from all datasets, given that they are defined.

Parameters:

Name Type Description Default
datasets DatasetsSchema | None

The desired datasets.

None
dataloaders DataloadersSchema | None

The desired dataloaders.

None
Source code in src/eva/core/data/datamodules/datamodule.py
def __init__(
    self,
    datasets: schemas.DatasetsSchema | None = None,
    dataloaders: schemas.DataloadersSchema | None = None,
) -> None:
    """Initializes the datamodule.

    Args:
        datasets: The desired datasets.
        dataloaders: The desired dataloaders.
    """
    super().__init__()

    self.datasets = datasets or self.default_datasets
    self.dataloaders = dataloaders or self.default_dataloaders

default_datasets: schemas.DatasetsSchema property

Returns the default datasets.

default_dataloaders: schemas.DataloadersSchema property

Returns the default dataloader schema.

eva.data.datamodules.call.call_method_if_exists

Calls a desired method from the datasets if exists.

Parameters:

Name Type Description Default
objects Iterable[Any]

An iterable of objects.

required
method str

The dataset method name to call if exists.

required
Source code in src/eva/core/data/datamodules/call.py
def call_method_if_exists(objects: Iterable[Any], /, method: str) -> None:
    """Calls a desired `method` from the datasets if exists.

    Args:
        objects: An iterable of objects.
        method: The dataset method name to call if exists.
    """
    for _object in _recursive_iter(objects):
        if hasattr(_object, method):
            fn = getattr(_object, method)
            fn()

eva.data.datamodules.schemas.DatasetsSchema dataclass

Datasets schema used in DataModule.

train: TRAIN_DATASET = None class-attribute instance-attribute

Train dataset.

val: EVAL_DATASET = None class-attribute instance-attribute

Validation dataset.

test: EVAL_DATASET = None class-attribute instance-attribute

Test dataset.

predict: EVAL_DATASET = None class-attribute instance-attribute

Predict dataset.

tolist

Returns the dataclass as a list and optionally filters it given the stage.

Source code in src/eva/core/data/datamodules/schemas.py
def tolist(self, stage: str | None = None) -> List[EVAL_DATASET]:
    """Returns the dataclass as a list and optionally filters it given the stage."""
    match stage:
        case "fit":
            return [self.train, self.val]
        case "validate":
            return [self.val]
        case "test":
            return [self.test]
        case "predict":
            return [self.predict]
        case None:
            return [self.train, self.val, self.test, self.predict]
        case _:
            raise ValueError(f"Invalid stage `{stage}`.")