2024-11-27 11:22:07 -03:00
|
|
|
"""Basic utilities for structuring experiment configurations based on Pydantic schemas."""
|
2024-11-28 15:15:05 -03:00
|
|
|
import os
|
2024-11-27 11:22:07 -03:00
|
|
|
from abc import abstractmethod
|
2024-11-28 15:15:05 -03:00
|
|
|
from io import TextIOBase
|
2024-12-11 13:52:55 -03:00
|
|
|
from typing import Type, Dict, TextIO, Callable, cast
|
2024-11-27 11:22:07 -03:00
|
|
|
|
2024-11-28 15:15:05 -03:00
|
|
|
import yaml
|
|
|
|
from typing_extensions import Generic, overload
|
2024-11-27 11:22:07 -03:00
|
|
|
|
2024-12-11 13:52:55 -03:00
|
|
|
from benchmarks.core.experiments.experiments import TExperiment
|
|
|
|
from benchmarks.core.pydantic import SnakeCaseModel
|
2024-11-27 11:22:07 -03:00
|
|
|
|
|
|
|
|
2024-12-11 13:52:55 -03:00
|
|
|
class ExperimentBuilder(SnakeCaseModel, Generic[TExperiment]):
|
2024-11-27 11:22:07 -03:00
|
|
|
""":class:`ExperimentBuilders` can build real :class:`Experiment`s out of :class:`ConfigModel`s. """
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def build(self) -> TExperiment:
|
|
|
|
pass
|
2024-11-28 15:15:05 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigParser:
|
|
|
|
"""
|
|
|
|
:class:`ConfigParser` is a utility class to parse configuration files into :class:`ExperimentBuilder`s.
|
|
|
|
Currently, each :class:`ExperimentBuilder` can appear at most once in the config file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2024-12-10 13:55:13 -03:00
|
|
|
self.experiment_types = {}
|
2024-11-28 15:15:05 -03:00
|
|
|
|
2024-11-28 15:52:38 -03:00
|
|
|
def register(self, root: Type[ExperimentBuilder[TExperiment]]):
|
2024-12-11 13:52:55 -03:00
|
|
|
self.experiment_types[root.alias()] = root
|
2024-11-28 15:15:05 -03:00
|
|
|
|
|
|
|
@overload
|
2024-11-28 15:52:38 -03:00
|
|
|
def parse(self, data: dict) -> Dict[str, ExperimentBuilder[TExperiment]]:
|
2024-11-28 15:15:05 -03:00
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
2024-11-28 15:52:38 -03:00
|
|
|
def parse(self, data: TextIO) -> Dict[str, ExperimentBuilder[TExperiment]]:
|
2024-11-28 15:15:05 -03:00
|
|
|
...
|
|
|
|
|
2024-11-28 15:52:38 -03:00
|
|
|
def parse(self, data: dict | TextIO) -> Dict[str, ExperimentBuilder[TExperiment]]:
|
2024-11-28 15:15:05 -03:00
|
|
|
if isinstance(data, TextIOBase):
|
|
|
|
entries = yaml.safe_load(os.path.expandvars(data.read()))
|
|
|
|
else:
|
|
|
|
entries = data
|
|
|
|
|
|
|
|
return {
|
2024-12-10 13:55:13 -03:00
|
|
|
tag: self.experiment_types[tag].model_validate(config)
|
2024-11-28 15:15:05 -03:00
|
|
|
for tag, config in entries.items()
|
|
|
|
}
|