flixopt.statistics_accessor ¶
Statistics accessor for FlowSystem.
This module provides a user-friendly API for analyzing optimization results directly from a FlowSystem.
Structure
.statistics- Data/metrics access (cached xarray Datasets).statistics.plot- Plotting methods using the statistics data
Example
flow_system.optimize(solver)
Data access¶
flow_system.statistics.flow_rates flow_system.statistics.flow_hours
Plotting¶
flow_system.statistics.plot.balance('ElectricityBus') flow_system.statistics.plot.heatmap('Boiler|on')
Attributes¶
SelectType module-attribute ¶
xarray-style selection dict: {'time': slice(...), 'scenario': 'base'}
FilterType module-attribute ¶
For include/exclude filtering: 'Boiler' or ['Boiler', 'CHP']
FlowSankeySelect module-attribute ¶
FlowSankeySelect = dict[Literal['flow', 'bus', 'component', 'carrier', 'time', 'period', 'scenario'], Any]
Select options for flow-based sankey: flow, bus, component, carrier, time, period, scenario.
EffectsSankeySelect module-attribute ¶
EffectsSankeySelect = dict[Literal['effect', 'component', 'contributor', 'period', 'scenario'], Any]
Select options for effects sankey: effect, component, contributor, period, scenario.
Classes¶
StatisticsAccessor ¶
Statistics accessor for FlowSystem. Access via flow_system.statistics.
This accessor provides cached data properties for optimization results. Use .plot for visualization methods.
Data Properties
flow_rates : xr.Dataset Flow rates for all flows. flow_hours : xr.Dataset Flow hours (energy) for all flows. sizes : xr.Dataset Sizes for all flows. charge_states : xr.Dataset Charge states for all storage components. temporal_effects : xr.Dataset Temporal effects per contributor per timestep. periodic_effects : xr.Dataset Periodic (investment) effects per contributor. total_effects : xr.Dataset Total effects (temporal + periodic) per contributor. effect_share_factors : dict Conversion factors between effects.
Examples:
>>> flow_system.optimize(solver)
>>> flow_system.statistics.flow_rates # Get data
>>> flow_system.statistics.plot.balance('Bus') # Plot
Attributes¶
carrier_colors property ¶
component_colors property ¶
bus_colors property ¶
carrier_units property ¶
effect_units property ¶
plot property ¶
Access plotting methods for statistics.
Returns:
| Type | Description |
|---|---|
StatisticsPlotAccessor | A StatisticsPlotAccessor instance. |
Examples:
flow_rates property ¶
All flow rates as a Dataset with flow labels as variable names.
Each variable has attributes
- 'carrier': carrier type (e.g., 'heat', 'electricity', 'gas')
- 'unit': carrier unit (e.g., 'kW')
flow_hours property ¶
All flow hours (energy) as a Dataset with flow labels as variable names.
Each variable has attributes
- 'carrier': carrier type (e.g., 'heat', 'electricity', 'gas')
- 'unit': energy unit (e.g., 'kWh', 'm3/s*h')
flow_sizes property ¶
Flow sizes as a Dataset with flow labels as variable names.
storage_sizes property ¶
Storage capacity sizes as a Dataset with storage labels as variable names.
charge_states property ¶
All storage charge states as a Dataset with storage labels as variable names.
effect_share_factors property ¶
temporal_effects property ¶
Temporal effects per contributor per timestep.
Returns a Dataset where each effect is a data variable with dimensions [time, contributor] (plus period/scenario if present).
Coordinates
- contributor: Individual contributor labels
- component: Parent component label for groupby operations
- component_type: Component type (e.g., 'Boiler', 'Source', 'Sink')
Examples:
>>> # Get costs per contributor per timestep
>>> statistics.temporal_effects['costs']
>>> # Sum over all contributors to get total costs per timestep
>>> statistics.temporal_effects['costs'].sum('contributor')
>>> # Group by component
>>> statistics.temporal_effects['costs'].groupby('component').sum()
Returns:
| Type | Description |
|---|---|
Dataset | xr.Dataset with effects as variables and contributor dimension. |
periodic_effects property ¶
Periodic (investment) effects per contributor.
Returns a Dataset where each effect is a data variable with dimensions [contributor] (plus period/scenario if present).
Coordinates
- contributor: Individual contributor labels
- component: Parent component label for groupby operations
- component_type: Component type (e.g., 'Boiler', 'Source', 'Sink')
Examples:
>>> # Get investment costs per contributor
>>> statistics.periodic_effects['costs']
>>> # Sum over all contributors to get total investment costs
>>> statistics.periodic_effects['costs'].sum('contributor')
>>> # Group by component
>>> statistics.periodic_effects['costs'].groupby('component').sum()
Returns:
| Type | Description |
|---|---|
Dataset | xr.Dataset with effects as variables and contributor dimension. |
total_effects property ¶
Total effects (temporal + periodic) per contributor.
Returns a Dataset where each effect is a data variable with dimensions [contributor] (plus period/scenario if present).
Coordinates
- contributor: Individual contributor labels
- component: Parent component label for groupby operations
- component_type: Component type (e.g., 'Boiler', 'Source', 'Sink')
Examples:
>>> # Get total costs per contributor
>>> statistics.total_effects['costs']
>>> # Sum over all contributors to get total system costs
>>> statistics.total_effects['costs'].sum('contributor')
>>> # Group by component
>>> statistics.total_effects['costs'].groupby('component').sum()
>>> # Group by component type
>>> statistics.total_effects['costs'].groupby('component_type').sum()
Returns:
| Type | Description |
|---|---|
Dataset | xr.Dataset with effects as variables and contributor dimension. |
Functions¶
get_effect_shares ¶
get_effect_shares(element: str, effect: str, mode: Literal['temporal', 'periodic'] | None = None, include_flows: bool = False) -> xr.Dataset
Retrieve individual effect shares for a specific element and effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element | str | The element identifier (component or flow label). | required |
effect | str | The effect identifier. | required |
mode | Literal['temporal', 'periodic'] | None | 'temporal', 'periodic', or None for both. | None |
include_flows | bool | Whether to include effects from flows connected to this element. | False |
Returns:
| Type | Description |
|---|---|
Dataset | xr.Dataset containing the requested effect shares. |
Raises:
| Type | Description |
|---|---|
ValueError | If the effect is not available or mode is invalid. |
SankeyPlotAccessor ¶
Sankey diagram accessor. Access via flow_system.statistics.plot.sankey.
Provides typed methods for different sankey diagram types.
Examples:
>>> fs.statistics.plot.sankey.flows(select={'bus': 'HeatBus'})
>>> fs.statistics.plot.sankey.effects(select={'effect': 'costs'})
>>> fs.statistics.plot.sankey.sizes(select={'component': 'Boiler'})
Functions¶
flows ¶
flows(*, aggregate: Literal['sum', 'mean'] = 'sum', select: FlowSankeySelect | None = None, colors: ColorType | None = None, show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot Sankey diagram of energy/material flow amounts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregate | Literal['sum', 'mean'] | How to aggregate over time ('sum' or 'mean'). | 'sum' |
select | FlowSankeySelect | None | Filter options: - flow: filter by flow label (e.g., 'Boiler|Q_th') - bus: filter by bus label (e.g., 'HeatBus') - component: filter by component label (e.g., 'Boiler') - time: select specific time (e.g., 100 or '2023-01-01') - period, scenario: xarray dimension selection | None |
colors | ColorType | None | Color specification for nodes. | None |
show | bool | None | Whether to display the figure. | None |
**plotly_kwargs | Any | Additional arguments passed to Plotly layout. | {} |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with Sankey flow data and figure. |
sizes ¶
sizes(*, select: FlowSankeySelect | None = None, max_size: float | None = None, colors: ColorType | None = None, show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot Sankey diagram of investment sizes/capacities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select | FlowSankeySelect | None | Filter options: - flow: filter by flow label (e.g., 'Boiler|Q_th') - bus: filter by bus label (e.g., 'HeatBus') - component: filter by component label (e.g., 'Boiler') - period, scenario: xarray dimension selection | None |
max_size | float | None | Filter flows with sizes exceeding this value. | None |
colors | ColorType | None | Color specification for nodes. | None |
show | bool | None | Whether to display the figure. | None |
**plotly_kwargs | Any | Additional arguments passed to Plotly layout. | {} |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with Sankey size data and figure. |
peak_flow ¶
peak_flow(*, select: FlowSankeySelect | None = None, colors: ColorType | None = None, show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot Sankey diagram of peak (maximum) flow rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select | FlowSankeySelect | None | Filter options: - flow: filter by flow label (e.g., 'Boiler|Q_th') - bus: filter by bus label (e.g., 'HeatBus') - component: filter by component label (e.g., 'Boiler') - time, period, scenario: xarray dimension selection | None |
colors | ColorType | None | Color specification for nodes. | None |
show | bool | None | Whether to display the figure. | None |
**plotly_kwargs | Any | Additional arguments passed to Plotly layout. | {} |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with Sankey peak flow data and figure. |
effects ¶
effects(*, select: EffectsSankeySelect | None = None, colors: ColorType | None = None, show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot Sankey diagram of component contributions to effects.
Shows how each component contributes to costs, CO2, and other effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select | EffectsSankeySelect | None | Filter options: - effect: filter which effects are shown (e.g., 'costs', ['costs', 'CO2']) - component: filter by component label (e.g., 'Boiler') - contributor: filter by contributor label (e.g., 'Boiler|Q_th') - period, scenario: xarray dimension selection | None |
colors | ColorType | None | Color specification for nodes. | None |
show | bool | None | Whether to display the figure. | None |
**plotly_kwargs | Any | Additional arguments passed to Plotly layout. | {} |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with Sankey effects data and figure. |
StatisticsPlotAccessor ¶
Plot accessor for statistics. Access via flow_system.statistics.plot.
All methods return PlotResult with both data and figure.
Attributes¶
sankey property ¶
Access sankey diagram methods with typed select options.
Returns:
| Type | Description |
|---|---|
SankeyPlotAccessor | SankeyPlotAccessor with methods: flows(), sizes(), peak_flow(), effects() |
Examples:
Functions¶
balance ¶
balance(node: str, *, select: SelectType | None = None, include: FilterType | None = None, exclude: FilterType | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot node balance (inputs vs outputs) for a Bus or Component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node | str | Label of the Bus or Component to plot. | required |
select | SelectType | None | xarray-style selection dict. | None |
include | FilterType | None | Only include flows containing these substrings. | None |
exclude | FilterType | None | Exclude flows containing these substrings. | None |
unit | Literal['flow_rate', 'flow_hours'] | 'flow_rate' (power) or 'flow_hours' (energy). | 'flow_rate' |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display the plot. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with .data and .figure. |
carrier_balance ¶
carrier_balance(carrier: str, *, select: SelectType | None = None, include: FilterType | None = None, exclude: FilterType | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot carrier-level balance showing all flows of a carrier type.
Shows production (positive) and consumption (negative) of a carrier across all buses of that carrier type in the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier | str | Carrier name (e.g., 'heat', 'electricity', 'gas'). | required |
select | SelectType | None | xarray-style selection dict. | None |
include | FilterType | None | Only include flows containing these substrings. | None |
exclude | FilterType | None | Exclude flows containing these substrings. | None |
unit | Literal['flow_rate', 'flow_hours'] | 'flow_rate' (power) or 'flow_hours' (energy). | 'flow_rate' |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display the plot. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with .data and .figure. |
Examples:
>>> fs.statistics.plot.carrier_balance('heat')
>>> fs.statistics.plot.carrier_balance('electricity', unit='flow_hours')
Notes
- Inputs to carrier buses (from sources/converters) are shown as positive
- Outputs from carrier buses (to sinks/converters) are shown as negative
- Internal transfers between buses of the same carrier appear on both sides
heatmap ¶
heatmap(variables: str | list[str], *, select: SelectType | None = None, reshape: tuple[str, str] | None = ('D', 'h'), colors: str | list[str] | None = None, facet_col: str | None = 'period', animation_frame: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot heatmap of time series data.
Time is reshaped into 2D (e.g., days × hours) when possible. Multiple variables are shown as facets. If too many dimensions exist to display without data loss, reshaping is skipped and variables are shown on the y-axis with time on x-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables | str | list[str] | Flow label(s) or variable name(s). Flow labels like 'Boiler(Q_th)' are automatically resolved to 'Boiler(Q_th)|flow_rate'. Full variable names like 'Storage|charge_state' are used as-is. | required |
select | SelectType | None | xarray-style selection, e.g. {'scenario': 'Base Case'}. | None |
reshape | tuple[str, str] | None | Time reshape frequencies as (outer, inner), e.g. ('D', 'h') for days × hours. Set to None to disable reshaping. | ('D', 'h') |
colors | str | list[str] | None | Colorscale name (str) or list of colors for heatmap coloring. Dicts are not supported for heatmaps (use str or list[str]). | None |
facet_col | str | None | Dimension for subplot columns (default: 'period'). With multiple variables, 'variable' is used instead. | 'period' |
animation_frame | str | None | Dimension for animation slider (default: 'scenario'). | 'scenario' |
show | bool | None | Whether to display the figure. | None |
**plotly_kwargs | Any | Additional arguments passed to px.imshow. | {} |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with processed data and figure. |
flows ¶
flows(*, start: str | list[str] | None = None, end: str | list[str] | None = None, component: str | list[str] | None = None, select: SelectType | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot flow rates filtered by start/end nodes or component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start | str | list[str] | None | Filter by source node(s). | None |
end | str | list[str] | None | Filter by destination node(s). | None |
component | str | list[str] | None | Filter by parent component(s). | None |
select | SelectType | None | xarray-style selection. | None |
unit | Literal['flow_rate', 'flow_hours'] | 'flow_rate' or 'flow_hours'. | 'flow_rate' |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with flow data. |
sizes ¶
sizes(*, max_size: float | None = 1000000.0, select: SelectType | None = None, colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot investment sizes (capacities) of flows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | float | None | Maximum size to include (filters defaults). | 1000000.0 |
select | SelectType | None | xarray-style selection. | None |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with size data. |
duration_curve ¶
duration_curve(variables: str | list[str], *, select: SelectType | None = None, normalize: bool = False, colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot load duration curves (sorted time series).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables | str | list[str] | Flow label(s) or variable name(s). Flow labels like 'Boiler(Q_th)' are looked up in flow_rates. Full variable names like 'Boiler(Q_th)|flow_rate' are stripped to their flow label. Other variables (e.g., 'Storage|charge_state') are looked up in the solution directly. | required |
select | SelectType | None | xarray-style selection. | None |
normalize | bool | If True, normalize x-axis to 0-100%. | False |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with sorted duration curve data. |
effects ¶
effects(aspect: Literal['total', 'temporal', 'periodic'] = 'total', *, effect: str | None = None, by: Literal['component', 'contributor', 'time'] | None = None, select: SelectType | None = None, colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot effect (cost, emissions, etc.) breakdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aspect | Literal['total', 'temporal', 'periodic'] | Which aspect to plot - 'total', 'temporal', or 'periodic'. | 'total' |
effect | str | None | Specific effect name to plot (e.g., 'costs', 'CO2'). If None, plots all effects. | None |
by | Literal['component', 'contributor', 'time'] | None | Group by 'component', 'contributor' (individual flows), 'time', or None to show aggregated totals per effect. | None |
select | SelectType | None | xarray-style selection. | None |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets (ignored if not in data). | 'period' |
facet_row | str | None | Dimension for row facets (ignored if not in data). | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with effect breakdown data. |
Examples:
>>> flow_system.statistics.plot.effects() # Aggregated totals per effect
>>> flow_system.statistics.plot.effects(effect='costs') # Just costs
>>> flow_system.statistics.plot.effects(by='component') # Breakdown by component
>>> flow_system.statistics.plot.effects(by='contributor') # By individual flows
>>> flow_system.statistics.plot.effects(aspect='temporal', by='time') # Over time
charge_states ¶
charge_states(storages: str | list[str] | None = None, *, select: SelectType | None = None, colors: ColorType | None = None, facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot storage charge states over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storages | str | list[str] | None | Storage label(s) to plot. If None, plots all storages. | None |
select | SelectType | None | xarray-style selection. | None |
colors | ColorType | None | Color specification (colorscale name, color list, or label-to-color dict). | None |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with charge state data. |
storage ¶
storage(storage: str, *, select: SelectType | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, charge_state_color: str = 'black', facet_col: str | None = 'period', facet_row: str | None = 'scenario', show: bool | None = None, **plotly_kwargs: Any) -> PlotResult
Plot storage operation: balance and charge state in vertically stacked subplots.
Creates two subplots sharing the x-axis: - Top: Charging/discharging flows as stacked bars (inputs negative, outputs positive) - Bottom: Charge state over time as a line
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage | str | Storage component label. | required |
select | SelectType | None | xarray-style selection. | None |
unit | Literal['flow_rate', 'flow_hours'] | 'flow_rate' (power) or 'flow_hours' (energy). | 'flow_rate' |
colors | ColorType | None | Color specification for flow bars. | None |
charge_state_color | str | Color for the charge state line overlay. | 'black' |
facet_col | str | None | Dimension for column facets. | 'period' |
facet_row | str | None | Dimension for row facets. | 'scenario' |
show | bool | None | Whether to display. | None |
Returns:
| Type | Description |
|---|---|
PlotResult | PlotResult with combined balance and charge state data. |
Raises:
| Type | Description |
|---|---|
KeyError | If storage component not found. |
ValueError | If component is not a storage. |