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 import Configimport pandas as pd# Create custom configurationcustom_config = Config( api_country_code='DE', predict_size=48, refit_size=14, train_size=pd.Timedelta(days=365), random_state=42)# Use in your codeprint(custom_config.API_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 Config 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:
For users working in Jupyter Notebooks or Quarto, the entire ENTSO-E pipeline can be executed using the Python API. This approach is highly recommended for safety-critical research as it allows for precise control over time windows and hyperparameters.
import pandas as pdimport osfrom spotforecast2_safe.downloader.entsoe import download_new_datafrom spotforecast2_safe.manager.trainer import handle_training as handle_training_safefrom spotforecast2_safe.manager.predictor import get_model_prediction as get_model_prediction_safefrom spotforecast2.manager.plotter import make_plotfrom spotforecast2.tasks.task_entsoe import ForecasterRecursiveLGBM# 1. Setup Time Windows (Last 3 years until last month) and country:country_code ="ES"now = pd.Timestamp.now(tz='UTC').floor('D')current_month_start = now.replace(day=1)last_month_start = (current_month_start - pd.Timedelta(days=1)).replace(day=1)# 2. 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", country_code=country_code)# 3. Configure and Train# Explicit parameters override global configuration for reproducibilitymodel_class = ForecasterRecursiveLGBMmodel_name ="lgbm_advanced"handle_training_safe( model_class=model_class, model_name=model_name, train_size=pd.Timedelta(days=3*365), end_dev=last_month_start.strftime("%Y-%m-%d %H:%M%z"), country_code=country_code)# 4. Generate Predictions for the forecast horizon# The predictor will automatically load the model trained abovepredictions = get_model_prediction_safe( model_name=model_name, predict_size=24*31)# 5. Visualize Resultsif predictions: make_plot(predictions)
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