Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/fluxopt/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,30 @@ def effect_contributions(self) -> xr.Dataset:
self._result.data,
self._result.solution,
)

def summary(self) -> xr.Dataset:
"""Headline KPIs overview.

Returns a tidy dataset with the objective value, total effects,
and per-flow full load hours.
"""
import numpy as np
import xarray as xr

ds = xr.Dataset()
ds['objective'] = xr.DataArray(self._result.objective)

if self._result.effect_totals is not None and len(self._result.effect_totals) > 0:
ds['effect_totals'] = self._result.effect_totals

# Compute full load hours
combined_size = self._result.data.flows.size.copy()
if len(self._result.sizes) > 0:
combined_size = combined_size.fillna(self._result.sizes)

with xr.set_options(keep_attrs=True):
flh = self.total_flow_hours / combined_size
flh = flh.where(np.isfinite(flh))

ds['full_load_hours'] = flh
return ds
17 changes: 17 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
import xarray as xr

from fluxopt import FlowSystem, Flow, Effect

def test_stats_summary_quickstart():
time = xr.DataArray(np.arange(2), dims=['time'], coords={'time': np.arange(2)})
sys = FlowSystem(time=time, dt=1.0)
sys.build(flows=[Flow(id='f1', size=10)], effects=[Effect(id='cost')])
res = sys.solve()

summary = res.stats.summary()

assert 'objective' in summary
assert 'effect_totals' in summary
assert 'full_load_hours' in summary
assert 'f1' in summary['full_load_hours'].coords['flow'].values