Skip to content

Serialization

Save and load pipeline configurations for reproducibility and version control.

Save

from epftoolbox2.pipelines import ModelPipeline
from epftoolbox2.models import OLSModel, LassoCVModel
from epftoolbox2.evaluators import MAEEvaluator
from epftoolbox2.exporters import TerminalExporter, ExcelExporter
predictors = [
"load_actual",
"is_monday_d+{horizon}",
"price_d-1",
"warsaw_temperature_2m_d+{horizon}",
]
pipeline = (
ModelPipeline()
.add_model(OLSModel(predictors=predictors, training_window=365, name="OLS"))
.add_model(LassoCVModel(predictors=predictors, training_window=365, cv=5, name="LassoCV"))
.add_evaluator(MAEEvaluator())
.add_exporter(TerminalExporter())
.add_exporter(ExcelExporter("results.xlsx"))
)
pipeline.save("model_pipeline.yaml")

Load

pipeline = ModelPipeline.load("model_pipeline.yaml")
report = pipeline.run(data=df, test_start="2024-04-01", test_end="2024-06-01")

YAML Format

If the pipeline has an information_set (see Information Set), it is saved as a top-level information_set key:

information_set:
issue_time: "11:45"
issue_timezone: Europe/Brussels
models:
- class: OLSModel
params:
predictors: &id001
- load_actual
- is_monday_d+{horizon}
- price_d-1
- warsaw_temperature_2m_d+{horizon}
training_window: 365
name: OLS
scaler: standard
- class: LassoCVModel
params:
predictors: *id001
training_window: 365
cv: 5
name: LassoCV
scaler: standard
evaluators:
- class: MAEEvaluator
params: {}
exporters:
- class: TerminalExporter
params:
show: [summary, horizon]
- class: ExcelExporter
params:
path: results.xlsx

Serialization Rules

TypeBehavior
str, int, float, bool, list, dict, NoneSerialized as-is
PathConverted to str
Callable predictorsRaises ValueError. Use "{horizon}" string templates instead
Model scalerSaved as a name (standard) or a {name, params} mapping. A scaler instance is converted when the model is built. A model without scaler loads with standard
Private attrs (_name)Excluded

Callable Predictors

Callable predictors cannot be serialized to YAML. Replace them with "{horizon}" string templates before saving:

# Instead of:
lambda h: f"warsaw_temperature_2m_d+{h}"
# Use:
"warsaw_temperature_2m_d+{horizon}"

YAML Aliases

If the same predictor list object is passed to multiple models, PyYAML emits a YAML anchor (&id001) and alias (*id001). This is valid YAML: the aliased list is reconstructed independently for each model on load.

Registering Custom Models/Evaluators/Exporters

Add to COMPONENT_REGISTRY in model_pipeline.py:

COMPONENT_REGISTRY["models"]["MyModel"] = "mypackage.models.my_model"
COMPONENT_REGISTRY["evaluators"]["MyEvaluator"] = "mypackage.evaluators.my_evaluator"
COMPONENT_REGISTRY["exporters"]["MyExporter"] = "mypackage.exporters.my_exporter"