multitask.predict.execute_predict

multitask.predict.execute_predict(
    task,
    show=False,
    task_name=None,
    max_age_days=None,
)

Execute prediction-only mode using previously saved models.

Loads the most recent fitted forecaster for every configured target from the cache directory. No training is performed. If no saved models can be found the function raises RuntimeError.

Parameters

Name Type Description Default
task BaseTask A BaseTask (or subclass) instance with prepared data. required
show bool If True, invoke the visualisation hooks. False
task_name Optional[str] Restrict model loading to a specific source task ("lazy", "defaults", "optuna", or "spotoptim"). None loads the most recent model regardless of source. None
max_age_days Optional[float] Maximum age in days for saved models. Models older than this are ignored. None accepts any age. None

Returns

Name Type Description
Dict[str, Any] Aggregated prediction package (weighted combination of all targets).
Dict[str, Any] Per-target packages are stored on task.results["predict"].

Raises

Name Type Description
RuntimeError If no saved models are found in the cache directory, or if a target has no matching saved model.

Examples

import tempfile
import warnings
from pathlib import Path

import numpy as np
import pandas as pd

from spotforecast2_safe.configurator.config_multi import ConfigMulti
from spotforecast2_safe.multitask.lazy import LazyTask
from spotforecast2_safe.multitask.predict import PredictTask, execute_predict

warnings.filterwarnings("ignore")

rng = np.random.default_rng(0)
idx = pd.date_range("2023-01-01", periods=300, freq="h", tz="UTC")
df = pd.DataFrame(
    {"total_load_actual": rng.normal(10_000, 500, 300).clip(8_000, 12_000)},
    index=idx,
)
df.index.name = "DateTime"

with tempfile.TemporaryDirectory() as tmp:
    cfg = ConfigMulti(
        data_frame_name="demo",
        predict_size=12,
        auto_save_models=True,
        cache_home=Path(tmp),
        targets=["total_load_actual"],
        verbose=False,
        use_outlier_detection=False,
        use_exogenous_features=False,
        number_folds=2,
    )
    # Train and save a model with LazyTask first.
    lazy = LazyTask(cfg, dataframe=df)
    lazy.prepare_data()
    lazy.run()

    # Load the saved model and generate predictions.
    predict_task = PredictTask(cfg, dataframe=df)
    predict_task.prepare_data()
    result = execute_predict(predict_task)

    print(f"Future predictions shape: {result['future_pred'].shape}")
    assert result["future_pred"].shape == (12,)
Future predictions shape: (12,)