Postprocessing¶
Post-processing and visualization of optimization results.
This module provides tools to extract, process, and visualize results from solved optimization models. The PostProcessor class handles denormalization of scaled results, data extraction into DataFrames, and creation of publication-quality plots.
Key Classes¶
PostProcessor: Extract and visualize optimization results
Available Methods¶
Data Extraction¶
get_impacts(): Extract impact results as DataFrameget_installation(): Extract installation schedulesget_operation(): Extract operation profilesget_production(): Extract production quantitiesget_demand(): Extract demand values
Visualization¶
plot_impacts(): Stacked bar chart of impacts over timeplot_installation(): Installation schedule visualizationplot_operation(): Operation levels over timeplot_capacity_balance(): Production vs capacity comparisonplot_utilization_heatmap(): Capacity utilization heatmap by process
Module Reference¶
Post-processing and visualization of optimization results.
This module provides tools to extract, process, and visualize results from solved optimization models. The PostProcessor class handles denormalization of scaled results, data extraction into DataFrames, and creation of publication-quality plots for impacts, installation schedules, production, and operation profiles.
Key classes: - PostProcessor: Extract and visualize optimization results
Classes¶
PostProcessor(solved_model: pyo.ConcreteModel, plot_config: dict = None)
¶
A class for post-processing and visualizing results from a solved Pyomo model.
This class provides plotting utilities with configurable styles for generating visualizations such as stacked bar charts, line plots, etc., from model outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solved_model
|
ConcreteModel
|
A solved Pyomo model instance containing the data to be processed and visualized. |
required |
plot_config
|
dict
|
A dictionary of plot styling options to override default settings. Recognized keys include: - "figsize" : tuple of (width, height) in inches - "fontsize" : int, font size for labels and titles - "grid_alpha" : float, transparency of grid lines - "grid_linestyle" : str, line style for grid (e.g., "--", ":", "-.") - "rotation" : int, angle of x-axis tick label rotation - "bar_width" : float, width of bars in bar charts - "colormap" : list of colors used for plotting - "line_color" : str, color of lines in line plots - "line_marker" : str, marker style for line plots - "line_width" : float, width of lines in line plots - "max_xticks" : int, maximum number of x-axis ticks to display Unrecognized keys are ignored. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
m |
ConcreteModel
|
The solved Pyomo model. |
_plot_config |
dict
|
The finalized configuration dictionary used for plotting. |
Source code in src/optimex/postprocessing.py
Methods:¶
get_impacts() -> pd.DataFrame
¶
Extract environmental impacts by category, process, and time.
Returns denormalized impact values from the solved optimization model, organized as a pivoted DataFrame with time as rows and (category, process) as column MultiIndex.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Pivoted DataFrame with 'Time' as index and MultiIndex columns for (Category, Process) combinations. Values represent environmental impacts in the units of the characterization method. |
Source code in src/optimex/postprocessing.py
get_dynamic_inventory(biosphere_database: str = 'ecoinvent-3.12-biosphere') -> pd.DataFrame
¶
Extract the dynamic inventory from the solved model.
Returns a DataFrame with elementary flows over time, formatted for use with dynamic_characterization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biosphere_database
|
str
|
Name of the biosphere database to look up flow IDs. |
"ecoinvent-3.12-biosphere"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: activity, flow, date, amount. - activity: process code (str) - flow: biosphere flow ID (int) - date: datetime of emission - amount: flow amount (float) |
Source code in src/optimex/postprocessing.py
get_characterized_dynamic_inventory(base_lcia_method: tuple, metric: str = 'radiative_forcing', time_horizon: int = 100, fixed_time_horizon: bool = True, biosphere_database: str = 'ecoinvent-3.12-biosphere', df_inventory: pd.DataFrame = None) -> pd.DataFrame
¶
Characterize the dynamic inventory using dynamic_characterization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_lcia_method
|
tuple
|
The LCIA method tuple for characterization (e.g., ('IPCC', 'GWP100')). |
required |
metric
|
str
|
Characterization metric. Options: "radiative_forcing", "GWP". |
"radiative_forcing"
|
time_horizon
|
int
|
Time horizon for characterization in years. |
100
|
fixed_time_horizon
|
bool
|
If True, use fixed time horizon; if False, use dynamic time horizon. |
True
|
biosphere_database
|
str
|
Name of the biosphere database (used if df_inventory not provided). |
"ecoinvent-3.12-biosphere"
|
df_inventory
|
DataFrame
|
Pre-computed inventory DataFrame. If not provided, calls get_dynamic_inventory(). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Characterized inventory DataFrame with columns: date, amount. |
Source code in src/optimex/postprocessing.py
plot_characterized_dynamic_inventory(base_lcia_method: tuple = None, metric: str = 'radiative_forcing', time_horizon: int = 100, fixed_time_horizon: bool = True, biosphere_database: str = 'ecoinvent-3.12-biosphere', df_characterized: pd.DataFrame = None)
¶
Plot the characterized dynamic inventory aggregated by year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_lcia_method
|
tuple
|
The LCIA method tuple for characterization. Required if df_characterized is not provided. |
None
|
metric
|
str
|
Characterization metric (used if df_characterized not provided). |
"radiative_forcing"
|
time_horizon
|
int
|
Time horizon for characterization (used if df_characterized not provided). |
100
|
fixed_time_horizon
|
bool
|
If True, use fixed time horizon (used if df_characterized not provided). |
True
|
biosphere_database
|
str
|
Name of the biosphere database (used if df_characterized not provided). |
"ecoinvent-3.12-biosphere"
|
df_characterized
|
DataFrame
|
Pre-computed characterized inventory. If not provided, calls get_characterized_dynamic_inventory(). |
None
|
Source code in src/optimex/postprocessing.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
get_installation() -> pd.DataFrame
¶
Extracts the installation data from the model and returns it as a DataFrame.
Values are the decision variable var_installation[p, v] itself, unchanged:
the number of process UNITS built in vintage year v. One unit delivers its full
lifetime production (the sum of its production temporal distribution) spread
over its operation window.
Two things follow, and both matter when reading plots:
- The index is the year of INSTALLATION, not a year of production. A unit installed in 2030 with an operation window of tau 1-20 produces in 2031-2050.
- The values are a lifetime quantity, so they are not an annual capacity and
must not be compared with the per-year values from
get_production()orget_demand(). Useget_production_capacity()for the annual capacity that installed units and existing stock make available in each year.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Time (vintage year) as index, Process as columns, units as values. |
Source code in src/optimex/postprocessing.py
get_operation(aggregate_vintages: bool = True) -> pd.DataFrame
¶
Extracts the operation data from the model and returns it as a DataFrame.
Values are the decision variable var_operation[p, v, t] itself: the number of
UNITS of vintage v that run in year t, summed over vintages by default. Unlike
get_installation(), the index is the year of OPERATION.
Units running are not a production volume: multiply by the output per unit and
year (the production entry at the vintage's lifecycle stage) to get production,
or simply use get_production(). Units running can be compared directly with
get_installation() only per vintage, since operation of a vintage is bounded
by the units installed in that vintage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregate_vintages
|
bool
|
If True (default), sum operation across vintages for each (process, time) to provide backward-compatible 2D output. If False, return full 3D data with (Process, Vintage) as MultiIndex columns. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame
|
If aggregate_vintages=True: DataFrame with Time as index, Process as columns. If aggregate_vintages=False: DataFrame with Time as index, (Process, Vintage) MultiIndex columns. Values are counts of running units in both cases. |
|
Note |
var_operation is not scaled because when both demand and
|
|
foreground_production are scaled by the same factor, the scaling
|
|
|
cancels out in the constraint: demand = production * operation.
|
|
Source code in src/optimex/postprocessing.py
get_production() -> pd.DataFrame
¶
Extracts the production data from the model and returns it as a DataFrame. The DataFrame will have a MultiIndex with 'Process', 'Product', and 'Time'. The values are the total production for each process and product at each time step.
With 3D var_operation[p, v, t], production is summed across all active vintages at each time step.
Source code in src/optimex/postprocessing.py
get_demand() -> pd.DataFrame
¶
Extracts the demand data from the model and returns it as a DataFrame. The DataFrame will have a MultiIndex with 'Product' and 'Time'. The values are the demand for each Product at each time step.
Source code in src/optimex/postprocessing.py
plot_impacts(df_impacts=None, annotated=True)
¶
Plot a stacked bar chart for impacts by category and process over time.
Creates a figure with one subplot per impact category, showing process contributions as stacked bars. Automatically denormalizes scaled values and optionally displays human-readable process names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_impacts
|
DataFrame
|
DataFrame with Time as index, Categories and Processes as columns. Columns must be a MultiIndex: (Category, Process). If not provided, automatically extracted via get_impacts(). |
None
|
annotated
|
bool
|
If True, show human-readable names from Brightway database instead of process codes. |
True
|
Source code in src/optimex/postprocessing.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
plot_installation(df_installation=None, annotated=True)
¶
Plot a stacked bar chart for installation data.
Bars show var_installation: the number of process units built in each vintage
year, a LIFETIME quantity plotted at the year of installation. This is not an
annual capacity and does not line up with production in the same year - use
plot_capacity_balance() to compare production against the annual capacity
those units make available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_installation
|
DataFrame
|
DataFrame with Time as index, Processes as columns |
None
|
annotated
|
bool
|
If True, show human-readable names instead of codes |
True
|
Source code in src/optimex/postprocessing.py
plot_operation(df_operation=None, annotated=True)
¶
Plot a stacked bar chart for operation data.
Bars show var_operation summed over vintages: how many units run in each year.
Note the different meaning of the x-axis compared to plot_installation(),
which is indexed by the year of installation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_operation
|
DataFrame
|
DataFrame with Time as index, Processes as columns |
None
|
annotated
|
bool
|
If True, show human-readable names instead of codes |
True
|
Source code in src/optimex/postprocessing.py
get_existing_capacity() -> pd.DataFrame
¶
Extract existing (brownfield) capacity data from the model.
Returns a DataFrame showing which processes have existing capacity, when they were installed, and their operational status at each time step.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with Time as index and (Process, Type) as MultiIndex columns. Type can be 'existing_capacity' (total existing) or 'existing_operating' (existing capacity in operation phase at that time). |
Source code in src/optimex/postprocessing.py
get_production_capacity() -> pd.DataFrame
¶
Calculate maximum available ANNUAL production capacity for each product at each time step.
Installed units (get_installation()) are counted in process units, and one
unit yields its full lifetime production over the whole operation window. This
method converts those units into the output they can deliver in a given year:
for every vintage active at time t, its unit count is multiplied by the
production coefficient at its current lifecycle stage tau = t - v. This is the
quantity to compare against actual production (get_production()), which is
also per year. Includes both new installations (from var_installation) and
existing (brownfield) capacity.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with Time as index and Products as columns. Values represent maximum annual production capacity (not actual production). |
Source code in src/optimex/postprocessing.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 | |
plot_capacity_balance(product=None, prod_df=None, capacity_df=None, demand_df=None, annotated=True, detailed=False)
¶
Plot actual production vs maximum available capacity.
Everything in this plot is in PRODUCT UNITS PER YEAR, which is what makes
production and capacity comparable. The capacity shown is therefore not
var_installation: it is get_production_capacity(), i.e. the units of every
vintage that is in its operation phase multiplied by the output that vintage
yields per unit and year, and it is indexed by the year the capacity is
available rather than the year it was installed.
When a specific product is given, plots a single chart. When product is None, auto-detects all products with non-zero demand or production and plots a grid of subplots.
Shows two lines per product: - Production (demand is assumed equal and overlaid) - Maximum available annual capacity (dashed line)
When detailed=True, also shows grouped bars per time step: - Left bar: Annual capacity entering/leaving operation, stacked by process. A cohort installed in year v appears here in the year it starts operating (v + operation start), again converted to output per year. - Right bar: Production of the running units, stacked by process
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
product
|
str
|
Product to plot. If None, plots all products with non-zero demand or production in a grid layout. |
None
|
prod_df
|
DataFrame
|
Production DataFrame from get_production() |
None
|
capacity_df
|
DataFrame
|
Capacity DataFrame from get_production_capacity() |
None
|
demand_df
|
DataFrame
|
Demand DataFrame from get_demand() |
None
|
annotated
|
bool
|
If True, show human-readable names instead of codes |
True
|
detailed
|
bool
|
If True, show grouped bars for capacity changes and operation by process |
False
|
Source code in src/optimex/postprocessing.py
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 | |
plot_utilization_heatmap(product=None, annotated=True, show_values=True)
¶
Plot a heatmap showing capacity utilization by process over time.
This provides a clean, dedicated view of which processes are being operated vs sitting idle at each time step.
Utilization is computed per year as actual production divided by the annual
capacity of the vintages in their operation phase - both in product units per
year. It is not var_operation / var_installation: those are unit counts
indexed by different years (operation year vs vintage year), and a unit only
counts towards capacity while it is inside its operation window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
product
|
str
|
Product to analyze. If None, uses the first product with non-zero demand. |
None
|
annotated
|
bool
|
If True, show human-readable process names instead of codes. |
True
|
show_values
|
bool
|
If True, show utilization percentages in cells. |
True
|
Note
|
|
required |
Source code in src/optimex/postprocessing.py
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 | |