multitask.BaseTask

multitask.BaseTask(
    config=None,
    *,
    dataframe=None,
    data_test=None,
    cache_home=None,
    log_level=logging.INFO,
    **overrides,
)

Shared base for all multi-target forecasting pipeline tasks.

Inherits the complete data-preparation pipeline (steps 1-7) and all helper methods from spotforecast2_safe.multitask.base.BaseTask. PlottingMixin overrides the three visualisation hooks with live Plotly figures.

The public API — constructor signature, attributes, method names, and the PipelineConfig protocol — is identical to the safe-package base. See spotforecast2_safe.multitask.base.BaseTask for full documentation.

Task subclasses implement run() for their specific mode: LazyTask (lazy), OptunaTask (optuna), SpotOptimTask (spotoptim), PredictTask (predict), CleanTask (clean).

Visualisation additions over the safe base

plot_with_outliers: Renders original vs. cleaned data with outlier markers via spotforecast2.plots.plotter.plot_with_outliers. _show_prediction_figure: Calls make_plot and shows the figure interactively. _show_prediction_figure_agg: Same for the aggregated prediction.

Examples

import tempfile
import numpy as np
import pandas as pd
from spotforecast2_safe.configurator.config_multi import ConfigMulti
from spotforecast2.multitask.base import BaseTask, PlottingMixin

rng = np.random.default_rng(0)
idx = pd.date_range("2023-01-01", periods=24 * 14, freq="h", tz="UTC")
df = pd.DataFrame({"load": rng.normal(100, 10, len(idx))}, index=idx)
df.index.name = "DateTime"

with tempfile.TemporaryDirectory() as tmp:
    cfg = ConfigMulti(
        predict_size=6,
        use_exogenous_features=False,
        use_outlier_detection=False,
        auto_save_models=False,
        cache_home=tmp,
    )
    task = BaseTask(cfg, dataframe=df)
    # Data-preparation pipeline (steps 1-3)
    task.prepare_data().detect_outliers().impute()

print("Pipeline shape:", task.df_pipeline.shape)
print("Targets:", task.config.targets)
# PlottingMixin is in the MRO — visualisation hooks are live Plotly calls.
print("PlottingMixin in MRO:", PlottingMixin in type(task).__mro__)
assert task.df_pipeline.shape[1] == 1
assert PlottingMixin in type(task).__mro__
Pipeline shape: (336, 1)
Targets: None
PlottingMixin in MRO: True
/tmp/ipykernel_3027/2659341657.py:22: DeprecationWarning: Derived pipeline fields (start_download, end_download, data_start, data_end, cov_start, cov_end, end_train_ts, start_train_ts) have moved to task.run_state. Reading them from the config is deprecated and will stop working in the next major release. config.targets continues to hold the user input unchanged; read the resolved list from task.run_state.targets.
  task.prepare_data().detect_outliers().impute()

Methods

Name Description
run Execute the task-specific training / tuning pipeline.

run

multitask.BaseTask.run(
    show=True,
    task=None,
    task_name=None,
    use_tuned_params=True,
    max_age_days=None,
    search_space=None,
    dry_run=False,
    cache_home=None,
    **kwargs,
)

Execute the task-specific training / tuning pipeline.

Subclasses must override this method.

Parameters

Name Type Description Default
show bool If True, display prediction figures. True
task Optional[str] Task mode override (used by MultiTask). None
task_name Optional[str] Restrict model loading to a specific source task (used by PredictTask). None
use_tuned_params bool Load cached tuning results when available (used by LazyTask). True
max_age_days Optional[float] Maximum age in days for cached results (used by LazyTask and PredictTask). None
search_space Optional[Any] Hyperparameter search-space definition (used by OptunaTask and SpotOptimTask). None
dry_run bool Report what would be deleted without removing anything (used by CleanTask). False
cache_home Optional[Path] Override the cache directory (used by CleanTask). None
**kwargs Any Additional task-specific arguments. {}

Returns

Name Type Description
Dict[str, Any] Aggregated prediction package for the task.

Raises

Name Type Description
NotImplementedError Always, unless overridden by a subclass.

Examples

BaseTask.run is abstract — it raises NotImplementedError to enforce that every concrete task subclass provides its own implementation. Use LazyTask, OptunaTask, SpotOptimTask, PredictTask, or CleanTask for live pipelines.

import tempfile
import numpy as np
import pandas as pd
from spotforecast2_safe.configurator.config_multi import ConfigMulti
from spotforecast2.multitask.base import BaseTask
from spotforecast2.multitask import LazyTask

rng = np.random.default_rng(0)
idx = pd.date_range("2023-01-01", periods=24 * 14, freq="h", tz="UTC")
df = pd.DataFrame({"load": rng.normal(100, 10, len(idx))}, index=idx)
df.index.name = "DateTime"

# BaseTask.run raises NotImplementedError — use a concrete subclass.
with tempfile.TemporaryDirectory() as tmp:
    cfg = ConfigMulti(cache_home=tmp)
    base = BaseTask(cfg)
try:
    base.run()
except NotImplementedError as exc:
    print("BaseTask.run() raised NotImplementedError (expected).")
    print(str(exc)[:60])

# LazyTask overrides run() with lazy fitting logic.
print("LazyTask.run is overridden:", LazyTask.run is not BaseTask.run)
assert LazyTask.run is not BaseTask.run
BaseTask.run() raised NotImplementedError (expected).
BaseTask must implement run(). Use LazyTask, OptunaTask, Spo
LazyTask.run is overridden: True