This guide provides comprehensive examples for using spotforecast2 with ENTSO-E energy data. Examples are organized from beginner to advanced, with each code snippet backed by automated tests.
An ENTSO-E API key (optional for training examples)
Tip “API Key Management”
Store your ENTSO-E API key in the ENTSOE_API_KEY environment variable to avoid passing it on every command:
exportENTSOE_API_KEY="your-api-key-here"echo$ENTSOE_API_KEYuv run spotforecast2-entsoe download 202301010000
Or set up a script with the following content:
#!/bin/zshexportENTSOE_API_KEY=your_api_key
Subcommands
# Download data from ENTSO-Euv run spotforecast2-entsoe download --api-key YOUR_API_KEY 202301010000# Train a model (lgbm or xgb)uv run spotforecast2-entsoe train lgbm --force# Generate predictions and plot (defaults to lgbm)uv run spotforecast2-entsoe predict --plot# Generate predictions with explicit model selectionuv run spotforecast2-entsoe predict lgbm --plotuv run spotforecast2-entsoe predict xgb --plot# Merge raw data filesuv run spotforecast2-entsoe merge
Download arguments and time format
The positional argument 202301010000 is a UTC timestamp in the format YYYYMMDDHHMM. It represents the start of the download window. You can provide either one timestamp (start only) or two timestamps (start and end).
# Start only (end defaults to now, UTC)uv run spotforecast2-entsoe download 202301010000# Start and end (UTC)uv run spotforecast2-entsoe download 202301010000 202312312300
Hidden arguments and defaults for download:
–api-key or ENTSOE_API_KEY environment variable
–force to re-download even if files already exist
data home controlled by SPOTFORECAST2_DATA (default is ~/spotforecast2_data)
Configuration
The ENTSO-E task uses a configuration class that can be customized programmatically. All configuration parameters have sensible defaults but can be overridden when needed.
from spotforecast2_safe.configurator import ConfigEntsoeimport pandas as pd# Create custom configurationcustom_config = ConfigEntsoe( country_code='DE', predict_size=48, refit_size=14, train_size=pd.Timedelta(days=365), random_state=42)# Use in your codeprint(custom_config.country_code) # 'DE'print(custom_config.predict_size) # 48
Time intervals for download, training, prediction, validation, and testing
Download interval is defined by the start/end timestamps passed to the download command.
Training, prediction, validation, and testing intervals are configured via the ConfigEntsoe class. The CLI uses default configuration values which can be modified programmatically:
training end time: config.end_train_default (defaults to “2025-12-31 00:00+00:00”)
training window size: config.train_size (defaults to 3 years)
Validation and testing are derived from the prediction window:
validation metrics use the first 24 hours of the prediction window
testing metrics use the full prediction window
Visualize Results
The prediction plot shows the following graphs:
Total system load (actual): The real-time electricity demand (consumption) within the bidding zone. This includes network losses but excludes consumption for pumped storage and generating auxiliaries.
Total system load (model prediction): The demand forecast generated by the spotforecast2 machine learning model (e.g., LightGBM or XGBoost) based on historical data and exogenous features.
Benchmark Forecast (e.g. ENTSOE): The reference forecast provided by the Transmission System Operators (TSOs) via the ENTSO-E Transparency Platform.
Actual (last week): The actual system load from exactly one week ago at the same time, which serves as a seasonal baseline comparison.
The prediction plot is saved as an HTML file named index.html in the data home directory. By default this is ~/spotforecast2_data/index.html or the path defined by SPOTFORECAST2_DATA.
# Default location on macOS/Linuxopen ~/spotforecast2_data/index.html# If you use a custom data homeopen"$SPOTFORECAST2_DATA/index.html"
Check the CLI logs for the exact path (look for “Plot saved to …”).
Feature Engineering
Period Dataclass
Periods define cyclical time features using radial basis functions:
from spotforecast2_safe.configurator import ConfigEntsoefrom spotforecast2_safe.preprocessing import ExogBuilderimport pandas as pdconfig = ConfigEntsoe()builder = ExogBuilder( periods=config.periods, country_code=config.country_code)X = builder.build( pd.Timestamp('2025-12-31', tz='UTC'), pd.Timestamp('2026-01-01', tz='UTC'))print(f"Generated {X.shape[1]} features for {X.shape[0]} hours")
Data Preprocessing
Linear Interpolation
Handle missing values in time series data:
from spotforecast2_safe.preprocessing import LinearlyInterpolateTSimport pandas as pdimport numpy as npts = pd.Series( [1.0, np.nan, 3.0, np.nan, 5.0], index=pd.date_range('2025-01-01', periods=5, freq='h'))interpolator = LinearlyInterpolateTS()ts_clean = interpolator.fit_transform(ts)print(ts_clean.values) # [1.0, 2.0, 3.0, 4.0, 5.0]
Forecaster Models
The pipeline builds its forecasters through factory functions that take a ConfigEntsoe and return a ready-to-fit spotforecast2_safe.forecaster.recursive.ForecasterRecursive.
LightGBM Forecaster
The stock LightGBM factory lives in the safe package:
Both factories honour config.random_state, config.lags_consider, and config.window_size; supply your own factory through config.forecaster_factory to customise further (see the Multitask tutorial).
Using the Python API (Notebooks & Quarto)
Full Prediction Pipeline
For users working in Jupyter Notebooks or Quarto, the entire ENTSO-E pipeline can be executed through MultiTask — the same path the CLI’s train and predict subcommands take. This approach is recommended for research as it gives precise control over time windows and hyperparameters.
import loggingimport osfrom spotforecast2_safe.configurator import ConfigEntsoefrom spotforecast2_safe.data.entsoe_loader import entsoe_data_loaderfrom spotforecast2_safe.data.fetch_data import get_cache_homefrom spotforecast2_safe.downloader.entsoe import download_new_datafrom spotforecast2_safe.multitask.factories import default_lgbm_forecaster_factoryfrom spotforecast2.multitask import MultiTask# 1. Download data (optional, requires ENTSOE_API_KEY)api_key = os.environ.get("ENTSOE_API_KEY")if api_key: download_new_data(api_key=api_key, start="202301010000")# 2. Wire the loader and factory into the configconfig = ConfigEntsoe()config.targets = ["Actual Load"]config.agg_weights = [1.0]config.bounds = [(-1e9, 1e9)]config.data_loader = entsoe_data_loaderconfig.forecaster_factory = default_lgbm_forecaster_factoryconfig.data_frame_name ="entsoe-lgbm"# 3. Run the five-step pipeline (task="defaults" trains; "predict" reuses# the saved model)mt = MultiTask( config, task="defaults", cache_home=get_cache_home(config.cache_home), log_level=logging.ERROR,)mt.prepare_data()mt.detect_outliers()mt.impute()mt.build_exogenous_features()mt.run(show=True)
File Paths
Data Home Directory
Access the data storage location:
from spotforecast2_safe.data import get_data_homedata_home = get_data_home()print(data_home) # ~/spotforecast2_data or SPOTFORECAST2_DATA
CLI Commands
Download Data
# Download with API keyuv run spotforecast2-entsoe download --api-key YOUR_API_KEY 202301010000# Download with date rangeuv run spotforecast2-entsoe download 202301010000 202312312300# Force re-downloaduv run spotforecast2-entsoe download --force 202301010000
Train Models
# Train LightGBM modeluv run spotforecast2-entsoe train lgbm# Train XGBoost modeluv run spotforecast2-entsoe train xgb# Force retraininguv run spotforecast2-entsoe train lgbm --force
Generate Predictions
# Predict with default model (lgbm)uv run spotforecast2-entsoe predict# Predict with specific modeluv run spotforecast2-entsoe predict lgbmuv run spotforecast2-entsoe predict xgb# Predict and generate plotuv run spotforecast2-entsoe predict --plot
Merge Data Files
uv run spotforecast2-entsoe merge
Environment Variables
Variable
Description
ENTSOE_API_KEY
ENTSO-E API key for data downloads
SPOTFORECAST2_DATA
Custom data directory (default: ~/spotforecast2_data)
Testing
All examples in this guide are validated by automated tests:
# Run documentation example testsuv run pytest tests/test_docs_entsoe_examples.py -v# Run all ENTSO-E testsuv run pytest tests/test_tasks_entsoe.py -v