Skip to content

flixopt.results

Classes

CalculationResults

CalculationResults(solution: Dataset, flow_system: Dataset, name: str, summary: Dict, folder: Optional[Path] = None, model: Optional[Model] = None)

Results container for Calculation results.

This class is used to collect the results of a Calculation. It provides access to component, bus, and effect results, and includes methods for filtering, plotting, and saving results.

The recommended way to create instances is through the class methods from_file() or from_calculation(), rather than direct initialization.

Attributes:

Name Type Description
solution Dataset

Dataset containing optimization results.

flow_system Dataset

Dataset containing the flow system.

summary Dict

Information about the calculation.

name str

Name identifier for the calculation.

model Model

The optimization model (if available).

folder Path

Path to the results directory.

components Dict[str, ComponentResults]

Results for each component.

buses Dict[str, BusResults]

Results for each bus.

effects Dict[str, EffectResults]

Results for each effect.

timesteps_extra DatetimeIndex

The extended timesteps.

hours_per_timestep DataArray

Duration of each timestep in hours.

Example

Load results from saved files:

results = CalculationResults.from_file('results_dir', 'optimization_run_1') element_result = results['Boiler'] results.plot_heatmap('Boiler(Q_th)|flow_rate') results.to_file(compression=5) results.to_file(folder='new_results_dir', compression=5) # Save the results to a new folder

Parameters:

Name Type Description Default
solution Dataset

The solution of the optimization.

required
flow_system Dataset

The flow_system that was used to create the calculation as a datatset.

required
name str

The name of the calculation.

required
summary Dict

Information about the calculation,

required
folder Optional[Path]

The folder where the results are saved.

None
model Optional[Model]

The linopy model that was used to solve the calculation.

None

Attributes

storages property
storages: List[ComponentResults]

All storages in the results.

objective property
objective: float

The objective result of the optimization.

variables property
variables: Variables

The variables of the optimization. Only available if the linopy.Model is available.

constraints property
constraints: Constraints

The constraints of the optimization. Only available if the linopy.Model is available.

Functions

from_file classmethod
from_file(folder: Union[str, Path], name: str)

Create CalculationResults instance by loading from saved files.

This method loads the calculation results from previously saved files, including the solution, flow system, model (if available), and metadata.

Parameters:

Name Type Description Default
folder Union[str, Path]

Path to the directory containing the saved files.

required
name str

Base name of the saved files (without file extensions).

required

Returns:

Name Type Description
CalculationResults

A new instance containing the loaded data.

Raises:

Type Description
FileNotFoundError

If required files cannot be found.

ValueError

If files exist but cannot be properly loaded.

from_calculation classmethod
from_calculation(calculation: Calculation)

Create CalculationResults directly from a Calculation object.

This method extracts the solution, flow system, and other relevant information directly from an existing Calculation object.

Parameters:

Name Type Description Default
calculation Calculation

A Calculation object containing a solved model.

required

Returns:

Name Type Description
CalculationResults

A new instance containing the results from the provided calculation.

Raises:

Type Description
AttributeError

If the calculation doesn't have required attributes.

filter_solution
filter_solution(variable_dims: Optional[Literal['scalar', 'time']] = None, element: Optional[str] = None) -> xr.Dataset

Filter the solution to a specific variable dimension and element. If no element is specified, all elements are included.

Parameters:

Name Type Description Default
variable_dims Optional[Literal['scalar', 'time']]

The dimension of the variables to filter for.

None
element Optional[str]

The element to filter for.

None
plot_network
plot_network(controls: Union[bool, List[Literal['nodes', 'edges', 'layout', 'interaction', 'manipulation', 'physics', 'selection', 'renderer']]] = True, path: Optional[Path] = None, show: bool = False) -> pyvis.network.Network

See flixopt.flow_system.FlowSystem.plot_network

to_file
to_file(folder: Optional[Union[str, Path]] = None, name: Optional[str] = None, compression: int = 5, document_model: bool = True, save_linopy_model: bool = False)

Save the results to a file Args: folder: The folder where the results should be saved. Defaults to the folder of the calculation. name: The name of the results file. If not provided, Defaults to the name of the calculation. compression: The compression level to use when saving the solution file (0-9). 0 means no compression. document_model: Wether to document the mathematical formulations in the model. save_linopy_model: Wether to save the model to file. If True, the (linopy) model is saved as a .nc4 file. The model file size is rougly 100 times larger than the solution file.

BusResults

BusResults(calculation_results: CalculationResults, label: str, variables: List[str], constraints: List[str], inputs: List[str], outputs: List[str])

Bases: _NodeResults

Results for a Bus

Attributes

variables property
variables: Variables

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

constraints property
constraints: Constraints

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

Functions

filter_solution
filter_solution(variable_dims: Optional[Literal['scalar', 'time']] = None) -> xr.Dataset

Filter the solution of the element by dimension.

Parameters:

Name Type Description Default
variable_dims Optional[Literal['scalar', 'time']]

The dimension of the variables to filter for.

None
plot_node_balance
plot_node_balance(save: Union[bool, Path] = False, show: bool = True, colors: ColorType = 'viridis', engine: PlottingEngine = 'plotly') -> Union[plotly.graph_objs.Figure, Tuple[plt.Figure, plt.Axes]]

Plots the node balance of the Component or Bus. Args: save: Whether to save the plot or not. If a path is provided, the plot will be saved at that location. show: Whether to show the plot or not. engine: The engine to use for plotting. Can be either 'plotly' or 'matplotlib'.

plot_node_balance_pie
plot_node_balance_pie(lower_percentage_group: float = 5, colors: ColorType = 'viridis', text_info: str = 'percent+label+value', save: Union[bool, Path] = False, show: bool = True, engine: PlottingEngine = 'plotly') -> plotly.graph_objects.Figure

Plots a pie chart of the flow hours of the inputs and outputs of buses or components.

Parameters:

Name Type Description Default
colors ColorType

a colorscale or a list of colors to use for the plot

'viridis'
lower_percentage_group float

The percentage of flow_hours that is grouped in "Others" (0...100)

5
text_info str

What information to display on the pie plot

'percent+label+value'
save Union[bool, Path]

Whether to save the figure.

False
show bool

Whether to show the figure.

True
engine PlottingEngine

Plotting engine to use. Only 'plotly' is implemented atm.

'plotly'

ComponentResults

ComponentResults(calculation_results: CalculationResults, label: str, variables: List[str], constraints: List[str], inputs: List[str], outputs: List[str])

Bases: _NodeResults

Results for a Component

Attributes

charge_state property
charge_state: DataArray

Get the solution of the charge state of the Storage.

variables property
variables: Variables

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

constraints property
constraints: Constraints

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

Functions

plot_charge_state
plot_charge_state(save: Union[bool, Path] = False, show: bool = True, colors: ColorType = 'viridis', engine: PlottingEngine = 'plotly') -> plotly.graph_objs.Figure

Plots the charge state of a Storage. Args: save: Whether to save the plot or not. If a path is provided, the plot will be saved at that location. show: Whether to show the plot or not. colors: The c engine: Plotting engine to use. Only 'plotly' is implemented atm.

Raises:

Type Description
ValueError

If the Component is not a Storage.

node_balance_with_charge_state
node_balance_with_charge_state(negate_inputs: bool = True, negate_outputs: bool = False, threshold: Optional[float] = 1e-05) -> xr.Dataset

Returns a dataset with the node balance of the Storage including its charge state. Args: negate_inputs: Whether to negate the inputs of the Storage. negate_outputs: Whether to negate the outputs of the Storage. threshold: The threshold for small values.

Raises:

Type Description
ValueError

If the Component is not a Storage.

filter_solution
filter_solution(variable_dims: Optional[Literal['scalar', 'time']] = None) -> xr.Dataset

Filter the solution of the element by dimension.

Parameters:

Name Type Description Default
variable_dims Optional[Literal['scalar', 'time']]

The dimension of the variables to filter for.

None
plot_node_balance
plot_node_balance(save: Union[bool, Path] = False, show: bool = True, colors: ColorType = 'viridis', engine: PlottingEngine = 'plotly') -> Union[plotly.graph_objs.Figure, Tuple[plt.Figure, plt.Axes]]

Plots the node balance of the Component or Bus. Args: save: Whether to save the plot or not. If a path is provided, the plot will be saved at that location. show: Whether to show the plot or not. engine: The engine to use for plotting. Can be either 'plotly' or 'matplotlib'.

plot_node_balance_pie
plot_node_balance_pie(lower_percentage_group: float = 5, colors: ColorType = 'viridis', text_info: str = 'percent+label+value', save: Union[bool, Path] = False, show: bool = True, engine: PlottingEngine = 'plotly') -> plotly.graph_objects.Figure

Plots a pie chart of the flow hours of the inputs and outputs of buses or components.

Parameters:

Name Type Description Default
colors ColorType

a colorscale or a list of colors to use for the plot

'viridis'
lower_percentage_group float

The percentage of flow_hours that is grouped in "Others" (0...100)

5
text_info str

What information to display on the pie plot

'percent+label+value'
save Union[bool, Path]

Whether to save the figure.

False
show bool

Whether to show the figure.

True
engine PlottingEngine

Plotting engine to use. Only 'plotly' is implemented atm.

'plotly'

EffectResults

EffectResults(calculation_results: CalculationResults, label: str, variables: List[str], constraints: List[str])

Bases: _ElementResults

Results for an Effect

Attributes

variables property
variables: Variables

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

constraints property
constraints: Constraints

Returns the variables of the element.

Raises:

Type Description
ValueError

If the linopy model is not availlable.

Functions

get_shares_from
get_shares_from(element: str)

Get the shares from an Element (without subelements) to the Effect

filter_solution
filter_solution(variable_dims: Optional[Literal['scalar', 'time']] = None) -> xr.Dataset

Filter the solution of the element by dimension.

Parameters:

Name Type Description Default
variable_dims Optional[Literal['scalar', 'time']]

The dimension of the variables to filter for.

None

SegmentedCalculationResults

SegmentedCalculationResults(segment_results: List[CalculationResults], all_timesteps: DatetimeIndex, timesteps_per_segment: int, overlap_timesteps: int, name: str, folder: Optional[Path] = None)

Class to store the results of a SegmentedCalculation.

Functions

from_file classmethod
from_file(folder: Union[str, Path], name: str)

Create SegmentedCalculationResults directly from file

solution_without_overlap
solution_without_overlap(variable_name: str) -> xr.DataArray

Returns the solution of a variable without overlapping timesteps

plot_heatmap
plot_heatmap(variable_name: str, heatmap_timeframes: Literal['YS', 'MS', 'W', 'D', 'h', '15min', 'min'] = 'D', heatmap_timesteps_per_frame: Literal['W', 'D', 'h', '15min', 'min'] = 'h', color_map: str = 'portland', save: Union[bool, Path] = False, show: bool = True, engine: PlottingEngine = 'plotly') -> Union[plotly.graph_objs.Figure, Tuple[plt.Figure, plt.Axes]]

Plots a heatmap of the solution of a variable.

Parameters:

Name Type Description Default
variable_name str

The name of the variable to plot.

required
heatmap_timeframes Literal['YS', 'MS', 'W', 'D', 'h', '15min', 'min']

The timeframes to use for the heatmap.

'D'
heatmap_timesteps_per_frame Literal['W', 'D', 'h', '15min', 'min']

The timesteps per frame to use for the heatmap.

'h'
color_map str

The color map to use for the heatmap.

'portland'
save Union[bool, Path]

Whether to save the plot or not. If a path is provided, the plot will be saved at that location.

False
show bool

Whether to show the plot or not.

True
engine PlottingEngine

The engine to use for plotting. Can be either 'plotly' or 'matplotlib'.

'plotly'
to_file
to_file(folder: Optional[Union[str, Path]] = None, name: Optional[str] = None, compression: int = 5)

Save the results to a file

Functions

plot_heatmap

plot_heatmap(dataarray: DataArray, name: str, folder: Path, heatmap_timeframes: Literal['YS', 'MS', 'W', 'D', 'h', '15min', 'min'] = 'D', heatmap_timesteps_per_frame: Literal['W', 'D', 'h', '15min', 'min'] = 'h', color_map: str = 'portland', save: Union[bool, Path] = False, show: bool = True, engine: PlottingEngine = 'plotly')

Plots a heatmap of the solution of a variable.

Parameters:

Name Type Description Default
dataarray DataArray

The dataarray to plot.

required
name str

The name of the variable to plot.

required
folder Path

The folder to save the plot to.

required
heatmap_timeframes Literal['YS', 'MS', 'W', 'D', 'h', '15min', 'min']

The timeframes to use for the heatmap.

'D'
heatmap_timesteps_per_frame Literal['W', 'D', 'h', '15min', 'min']

The timesteps per frame to use for the heatmap.

'h'
color_map str

The color map to use for the heatmap.

'portland'
save Union[bool, Path]

Whether to save the plot or not. If a path is provided, the plot will be saved at that location.

False
show bool

Whether to show the plot or not.

True
engine PlottingEngine

The engine to use for plotting. Can be either 'plotly' or 'matplotlib'.

'plotly'

sanitize_dataset

sanitize_dataset(ds: Dataset, timesteps: Optional[DatetimeIndex] = None, threshold: Optional[float] = 1e-05, negate: Optional[List[str]] = None, drop_small_vars: bool = True, zero_small_values: bool = False) -> xr.Dataset

Sanitizes a dataset by handling small values (dropping or zeroing) and optionally reindexing the time axis.

Parameters:

Name Type Description Default
ds Dataset

The dataset to sanitize.

required
timesteps Optional[DatetimeIndex]

The timesteps to reindex the dataset to. If None, the original timesteps are kept.

None
threshold Optional[float]

The threshold for small values processing. If None, no processing is done.

1e-05
negate Optional[List[str]]

The variables to negate. If None, no variables are negated.

None
drop_small_vars bool

If True, drops variables where all values are below threshold.

True
zero_small_values bool

If True, sets values below threshold to zero.

False

Returns:

Type Description
Dataset

xr.Dataset: The sanitized dataset.

filter_dataset

filter_dataset(ds: Dataset, variable_dims: Optional[Literal['scalar', 'time']] = None) -> xr.Dataset

Filters a dataset by its dimensions.

Parameters:

Name Type Description Default
ds Dataset

The dataset to filter.

required
variable_dims Optional[Literal['scalar', 'time']]

The dimension of the variables to filter for.

None