Skip to content

Python Quickstart

Install Cobre and run a study in a few steps.

Terminal window
pip install cobre-python

Requires Python 3.12, 3.13, or 3.14.

import cobre
result = cobre.run.run("path/to/case")

The cobre.run.run() function loads the case, trains an SDDP policy, optionally runs simulation, and writes output files. It returns a dictionary with the following keys:

KeyTypeDescription
convergedboolWhether training converged
iterationsintNumber of training iterations completed
lower_boundfloatFinal lower bound
upper_boundfloat or NoneFinal upper bound (None if no simulation)
gap_percentfloat or NoneOptimality gap percentage (None if unavailable)
total_time_msintTotal wall-clock time in milliseconds
output_dirstrPath to the output directory
simulationdict or NoneSimulation summary (if enabled)
stochasticdict or NoneStochastic preprocessing summary
hydro_modelsdict or NoneHydro model summary
provenancedictBuild version and environment metadata
print(f"Converged: {result['converged']}")
print(f"Iterations: {result['iterations']}")
print(f"Lower bound: {result['lower_bound']:.2f}")
if result['gap_percent'] is not None:
print(f"Gap: {result['gap_percent']:.2f}%")
print(f"Output dir: {result['output_dir']}")
result = cobre.run.run(
"path/to/case",
output_dir="path/to/output", # default: case_dir/output
threads=4, # default: 1
skip_simulation=True, # default: False
)

Cobre writes results as Parquet files, which can be loaded directly with Polars or any Arrow-compatible library:

import polars as pl
# Convergence trajectory
convergence = pl.read_parquet("output/training/convergence.parquet")
print(convergence.head())
# Simulation costs (if simulation was enabled) — Hive-partitioned
costs = pl.read_parquet("output/simulation/costs/")
print(costs.describe())

For larger datasets, use the built-in Arrow loaders that avoid serialization overhead:

# Returns a pyarrow.Table (zero-copy)
convergence_table = cobre.results.load_convergence_arrow("output/")
simulation_tables = cobre.results.load_simulation_arrow("output/")
# Convert to Polars without copying
import polars as pl
df = pl.from_arrow(convergence_table)
  • See the case directory format for input file specifications.
  • Explore the examples for ready-to-run cases.
  • Read the Jupyter quickstart notebook for a complete end-to-end workflow with visualization.