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
Functions¶
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
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 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 | |
get_installation() -> pd.DataFrame
¶
Extracts the installation data from the model and returns it as a DataFrame. The DataFrame will have a MultiIndex with 'Time' and 'Process'. The values are the installed capacities for each process at each time step.
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.
With 3D var_operation[p, v, t], this method can either aggregate across vintages (backward compatible) or return per-vintage data.
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. |
|
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
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 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 | |
plot_installation(df_installation=None, annotated=True)
¶
Plot a stacked bar chart for installation data.
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.
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 production capacity for each product at each time step.
Capacity is determined by counting installations in their operation phase and multiplying by their production coefficients. This includes both new installations (from var_installation) and existing (brownfield) capacity.
Note: Uses vintage-aware 4D calculation when production overrides exist, matching the optimizer's capacity constraint calculation.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with Time as index and Products as columns. Values represent maximum production capacity (not actual production). |
Source code in src/optimex/postprocessing.py
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 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 | |
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.
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 capacity (dashed line)
When detailed=True, also shows grouped bars per time step: - Left bar: Capacity changes (additions/removals stacked by process) - Right bar: Operation level (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
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.
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 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 | |