API Reference#
Technical documentation for PyPSA-GB modules and functions.
Overview#
PyPSA-GB is organized into modular scripts:
Core Modules#
Module |
Purpose |
|---|---|
|
Network optimization |
|
Historical/future routing |
|
Technology definitions |
|
Logging setup |
Network Build Modules (scripts/network_build/)#
Module |
Purpose |
|---|---|
|
ETYS network assembly from preprocessed data |
|
Raw ETYS Excel → intermediate CSVs |
|
Network upgrade application (circuits, transformers, HVDC) |
|
ETYS file/sheet name mapping and constants |
|
Reduced/Zonal network builders |
Integration Modules#
Module |
Purpose |
|---|---|
|
Thermal generator integration |
|
Renewable generator integration |
|
Storage unit integration |
|
Geographic mapping utilities |
|
Demand-side flexibility orchestration |
|
Heat pump disaggregation and flexibility |
|
EV smart charging and V2G |
|
Event-based demand response |
Market Modules (scripts/market/)#
Module |
Purpose |
|---|---|
|
Stage 1 copperplate wholesale dispatch and wholesale price export |
|
Stage 2 anchored balancing redispatch with full network constraints |
|
Bid/offer prices, ELEXON price loading, redispatch and congestion utilities |
|
Historical ELEXON BMRS data retrieval and aggregation |
|
Market dashboard and summary generation |
|
CfD and ROC revenue accounting |
|
Historical ELEXON BM validation |
|
NESO constraint-cost and boundary-flow validation |
Utility Modules#
Module |
Purpose |
|---|---|
|
Time series handling |
|
Coordinate transformations |
|
Data validation |
Using the API#
Direct Import#
from scripts.scenario_detection import is_historical_scenario, auto_configure_scenario
from scripts.carrier_definitions import get_carrier_definitions
from scripts.spatial_utils import map_sites_to_buses
Within Snakemake#
Scripts are typically invoked via Snakemake rules, but can also be run standalone:
# Example: Build an ETYS network manually
import pypsa
from scripts.network_build.ETYS_network import create_network
from scripts.network_build.ETYS_upgrades import apply_etys_network_upgrades
# Or use the Snakemake rules (recommended):
# snakemake resources/network/ETYS_2024_base_network.nc -j 1
Type Hints#
Most functions include type hints:
def is_historical_scenario(scenario: dict) -> bool:
"""Check if scenario models a historical year."""
...
Docstring Format#
Functions use NumPy-style docstrings:
def map_sites_to_buses(
network: pypsa.Network,
sites_df: pd.DataFrame,
method: str = 'nearest'
) -> pd.DataFrame:
"""
Map generator sites to network buses.
Parameters
----------
network : pypsa.Network
The PyPSA network with bus coordinates
sites_df : pd.DataFrame
Sites with 'lat', 'lon' columns
method : str, optional
Mapping method: 'nearest' or 'voronoi'
Returns
-------
pd.DataFrame
Sites with 'bus' column added
Examples
--------
>>> sites = map_sites_to_buses(network, wind_sites)
>>> sites.groupby('bus').capacity_mw.sum()
"""
Error Handling#
Modules use consistent error handling:
from scripts.logging_config import setup_logging
logger = setup_logging("my_module")
try:
result = process_data(input_data)
except ValueError as e:
logger.error(f"Invalid input data: {e}")
raise
except FileNotFoundError as e:
logger.warning(f"Optional file not found: {e}")
result = default_value
Extending PyPSA-GB#
Adding a New Data Source#
Create processing script in
scripts/Add Snakemake rule in
rules/Document the module here
Update configuration options
Adding a New Technology#
Add to
carrier_definitions.pyUpdate integration module
Add test cases
Document the carrier
See Contributing for guidelines.