Skip to content

flixopt.comparison

Compare multiple FlowSystems side-by-side.

Attributes

Classes

Comparison

Comparison(flow_systems: list[FlowSystem], names: list[str] | None = None)

Compare multiple FlowSystems side-by-side.

Combines solutions, statistics, and inputs from multiple FlowSystems into unified xarray Datasets with a 'case' dimension. The existing plotting infrastructure automatically handles faceting by the 'case' dimension.

For comparing solutions/statistics, all FlowSystems must be optimized and have matching dimensions. For comparing inputs only, optimization is not required.

Parameters:

Name Type Description Default
flow_systems list[FlowSystem]

List of FlowSystems to compare.

required
names list[str] | None

Optional names for each case. If None, uses FlowSystem.name.

None

Raises:

Type Description
ValueError

If case names are not unique.

RuntimeError

If accessing solution/statistics without optimized FlowSystems.

Examples:

# Compare two systems (uses FlowSystem.name by default)
comp = fx.Comparison([fs_base, fs_modified])

# Or with custom names
comp = fx.Comparison([fs_base, fs_modified], names=['baseline', 'modified'])

# Side-by-side plots (auto-facets by 'case')
comp.stats.plot.balance('Heat')
comp.stats.flow_rates.plotly.line()

# Access combined data
comp.solution  # xr.Dataset with 'case' dimension
comp.stats.flow_rates  # xr.Dataset with 'case' dimension

# Compute differences relative to first case
comp.diff()  # Returns xr.Dataset of differences
comp.diff('baseline')  # Or specify reference by name

# For systems with different dimensions, align first:
fs_both = ...  # Has scenario dimension
fs_mild = fs_both.transform.sel(scenario='Mild')  # Select one scenario
fs_other = ...  # Also select to match
comp = fx.Comparison([fs_mild, fs_other])  # Now dimensions match

Attributes

flow_systems property
flow_systems: dict[str, FlowSystem]

Access underlying FlowSystems as a dict mapping name → FlowSystem.

is_optimized property
is_optimized: bool

Check if all FlowSystems have been optimized.

dims property
dims: dict[str, int]

Shared dimensions across all FlowSystems.

Returns dimensions that exist in all systems with matching sizes.

names property
names: list[str]

Case names for each FlowSystem.

solution property
solution: Dataset

Combined solution Dataset with 'case' dimension.

stats property
stats: ComparisonStatistics

Combined statistics accessor with 'case' dimension.

statistics property
statistics: ComparisonStatistics

Deprecated: Use :attr:stats instead.

inputs property
inputs: Dataset

Combined input data Dataset with 'case' dimension.

Concatenates input parameters from all FlowSystems. Each FlowSystem's .inputs Dataset is combined with a 'case' dimension.

Returns:

Type Description
Dataset

xr.Dataset with all input parameters. Variable naming follows

Dataset

the pattern {element.label_full}|{parameter_name}.

Examples:

comp = fx.Comparison([fs1, fs2], names=['Base', 'Modified'])
comp.inputs  # All inputs with 'case' dimension
comp.inputs['Boiler(Q_th)|relative_minimum']  # Specific parameter

Functions

diff
diff(reference: str | int = 0) -> xr.Dataset

Compute differences relative to a reference case.

Parameters:

Name Type Description Default
reference str | int

Reference case name or index (default: 0, first case).

0

Returns:

Type Description
Dataset

Dataset with differences (each case minus reference).

ComparisonStatistics

ComparisonStatistics(comparison: Comparison)

Combined statistics accessor for comparing FlowSystems.

Mirrors StatisticsAccessor properties, concatenating data with a 'case' dimension. Access via Comparison.stats.

Attributes

flow_rates property
flow_rates: Dataset

Combined flow rates with 'case' dimension.

flow_hours property
flow_hours: Dataset

Combined flow hours (energy) with 'case' dimension.

flow_sizes property
flow_sizes: Dataset

Combined flow investment sizes with 'case' dimension.

storage_sizes property
storage_sizes: Dataset

Combined storage capacity sizes with 'case' dimension.

sizes property
sizes: Dataset

Combined sizes (flow + storage) with 'case' dimension.

charge_states property
charge_states: Dataset

Combined storage charge states with 'case' dimension.

temporal_effects property
temporal_effects: Dataset

Combined temporal effects with 'case' dimension.

periodic_effects property
periodic_effects: Dataset

Combined periodic effects with 'case' dimension.

total_effects property
total_effects: Dataset

Combined total effects with 'case' dimension.

carrier_colors property
carrier_colors: dict[str, str]

Merged carrier colors from all cases.

component_colors property
component_colors: dict[str, str]

Merged component colors from all cases.

flow_colors property
flow_colors: dict[str, str]

Merged flow colors from all cases (derived from parent components).

bus_colors property
bus_colors: dict[str, str]

Merged bus colors from all cases.

carrier_units property
carrier_units: dict[str, str]

Merged carrier units from all cases.

effect_units property
effect_units: dict[str, str]

Merged effect units from all cases.

plot property
plot: ComparisonStatisticsPlot

Access plot methods for comparison statistics.

ComparisonStatisticsPlot

ComparisonStatisticsPlot(statistics: ComparisonStatistics)

Plot accessor for comparison statistics.

Wraps StatisticsPlotAccessor methods, combining data from all FlowSystems with a 'case' dimension for faceting.

Functions

balance
balance(node: str, *, select: SelectType | None = None, include: str | list[str] | None = None, exclude: str | list[str] | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot node balance comparison across cases.

Parameters:

Name Type Description Default
node str

Bus or component label to plot balance for.

required
select SelectType | None

xarray-style selection.

None
include str | list[str] | None

Filter to include only matching flow labels.

None
exclude str | list[str] | None

Filter to exclude matching flow labels.

None
unit Literal['flow_rate', 'flow_hours']

'flow_rate' or 'flow_hours'.

'flow_rate'
colors ColorType | None

Color specification (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined balance data and figure.

carrier_balance
carrier_balance(carrier: str, *, select: SelectType | None = None, include: str | list[str] | None = None, exclude: str | list[str] | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot carrier balance comparison across cases.

Parameters:

Name Type Description Default
carrier str

Carrier name to plot balance for.

required
select SelectType | None

xarray-style selection.

None
include str | list[str] | None

Filter to include only matching flow labels.

None
exclude str | list[str] | None

Filter to exclude matching flow labels.

None
unit Literal['flow_rate', 'flow_hours']

'flow_rate' or 'flow_hours'.

'flow_rate'
colors ColorType | None

Color specification (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined carrier balance 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, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot flows comparison across cases.

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 (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined flows data and figure.

storage
storage(storage: str, *, select: SelectType | None = None, unit: Literal['flow_rate', 'flow_hours'] = 'flow_rate', colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot storage operation comparison across cases.

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' or 'flow_hours'.

'flow_rate'
colors ColorType | None

Color specification for flow bars.

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined storage operation data and figure.

charge_states
charge_states(storages: str | list[str] | None = None, *, select: SelectType | None = None, colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot charge states comparison across cases.

Parameters:

Name Type Description Default
storages str | list[str] | None

Storage label(s) to plot. If None, plots all.

None
select SelectType | None

xarray-style selection.

None
colors ColorType | None

Color specification (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined charge state data and figure.

duration_curve
duration_curve(variables: str | list[str], *, select: SelectType | None = None, normalize: bool = False, colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot duration curves comparison across cases.

Parameters:

Name Type Description Default
variables str | list[str]

Flow label(s) or variable name(s) to plot.

required
select SelectType | None

xarray-style selection.

None
normalize bool

If True, normalize x-axis to 0-100%.

False
colors ColorType | None

Color specification (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined duration curve data and figure.

sizes
sizes(*, max_size: float | None = 1000000.0, select: SelectType | None = None, colors: ColorType | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot investment sizes comparison across cases.

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 (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined sizes data and figure.

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, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot effects comparison across cases.

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. If None, plots all.

None
by Literal['component', 'contributor', 'time'] | None

Group by 'component', 'contributor', or 'time'.

None
select SelectType | None

xarray-style selection.

None
colors ColorType | None

Color specification (dict, list, or colorscale name).

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined effects data and figure.

heatmap
heatmap(variables: str | list[str], *, select: SelectType | None = None, reshape: tuple[str, str] | Literal['auto'] | None = 'auto', colors: str | list[str] | None = None, threshold: float | None = 1e-05, show: bool | None = None, data_only: bool = False, **plotly_kwargs: Any) -> PlotResult

Plot heatmap comparison across cases.

Parameters:

Name Type Description Default
variables str | list[str]

Flow label(s) or variable name(s) to plot.

required
select SelectType | None

xarray-style selection.

None
reshape tuple[str, str] | Literal['auto'] | None

Time reshape frequencies, 'auto', or None.

'auto'
colors str | list[str] | None

Colorscale name or list of colors.

None
threshold float | None

Filter out variables where max absolute value is below this.

1e-05
show bool | None

Whether to display the figure.

None
data_only bool

If True, skip figure creation and return only data.

False
**plotly_kwargs Any

Additional arguments passed to plotly.

{}

Returns:

Type Description
PlotResult

PlotResult with combined heatmap data and figure.