Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output unit for supply chains comparison #152

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/layout_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@


def display_costs(
df_costs: pd.DataFrame, key: str, titlestring: str, key_suffix: str = ""
df_costs: pd.DataFrame,
key: str,
titlestring: str,
key_suffix: str = "",
output_unit: str | None = None,
):
"""Display costs as table and bar chart."""
key_suffix = key_suffix.lower().replace(" ", "_")
Expand Down Expand Up @@ -49,6 +53,7 @@ def display_costs(
fig = create_bar_chart_costs(
df_res,
current_selection=st.session_state[key],
output_unit=output_unit,
)
st.plotly_chart(fig, use_container_width=True)

Expand Down
14 changes: 10 additions & 4 deletions app/plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ def _make_costs_hoverdata(res_costs: pd.DataFrame) -> list[pd.Series]:
return [custom_hover_data]


def create_bar_chart_costs(res_costs: pd.DataFrame, current_selection: str = None):
def create_bar_chart_costs(
res_costs: pd.DataFrame,
current_selection: str = None,
output_unit: str | None = None,
):
"""Create bar plot for costs by components, and dots for total costs.

Parameters
Expand Down Expand Up @@ -404,9 +408,11 @@ def create_bar_chart_costs(res_costs: pd.DataFrame, current_selection: str = Non
ax=0,
ay=-50,
)
fig.update_layout(
yaxis_title=st.session_state["output_unit"],
)

if output_unit is None:
output_unit = st.session_state["output_unit"]

fig.update_layout(yaxis_title=output_unit)
return fig


Expand Down
49 changes: 31 additions & 18 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,33 @@ def calculate_results_list(
api: PtxboaAPI,
parameter_to_change: str,
parameter_list: list = None,
override_session_state: dict | None = None,
) -> pd.DataFrame:
"""Calculate results for source regions and one selected target country.

Parameters
----------
_api : :class:`~ptxboa.api.PtxboaAPI`
api : :class:`~ptxboa.api.PtxboaAPI`
an instance of the api class
settings : dict
settings from the streamlit app. An example can be obtained with the
return value from :func:`ptxboa_functions.create_sidebar`.
parameter_to_change : str
element of settings for which a list of values is to be used.
parameter_list : list or None
The values of ``parameter_to_change`` for which the results are calculated.
If None, all values available in the API will be used.
override_session_state : dict or None
pass a dict with custom values in order to change the calculation parameters
obtained from st.session_state. I None, all parameters are taken from the
session state. Keys of the dictionary must in "chain", "country",
"output_unit", "region", "res_gen", "scenario", "secproc_co2",
"secproc_water", "ship_own_fuel", "transport".

Returns
-------
pd.DataFrame
same format as for :meth:`~ptxboa.api.PtxboaAPI.calculate()`
wide format dataframe with index values from `parameter_to_change` and columns
containing the different cost components and a column for total costs "Total".
"""
res_list = []

if parameter_list is None:
parameter_list = api.get_dimension(parameter_to_change).index

# copy settings from session_state:
settings = {}
for key in [
setting_keys = [
"chain",
"country",
"output_unit",
Expand All @@ -77,15 +75,30 @@ def calculate_results_list(
"secproc_water",
"ship_own_fuel",
"transport",
]:
settings[key] = st.session_state[key]
]

# copy settings from session_state:
settings = {key: st.session_state[key] for key in setting_keys}

# update settings from session state with custom values
if override_session_state is not None:
if not set(override_session_state.keys()).issubset(set(setting_keys)):
msg = (
f"keys in 'override_session_state' must be in dict_keys({setting_keys})"
f" but are currently {override_session_state.keys()}"
)
raise ValueError(msg)
settings.update(override_session_state)

if parameter_list is None:
parameter_list = api.get_dimension(parameter_to_change).index

res_list = []
for parameter in parameter_list:
settings2 = settings.copy()
settings2[parameter_to_change] = parameter
settings.update({parameter_to_change: parameter})
res_single = calculate_results_single(
api,
settings2,
settings,
user_data=st.session_state["user_changes_df"],
)
res_list.append(res_single)
Expand Down
4 changes: 3 additions & 1 deletion app/tab_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ def content_dashboard(
costs_per_res_gen, "res_gen", "Costs by renewable electricity source:"
)

display_costs(costs_per_chain, "chain", "Costs by supply chain:")
display_costs(
costs_per_chain, "chain", "Costs by supply chain:", output_unit="USD/MWh"
)
1 change: 1 addition & 0 deletions ptxboa_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
api,
parameter_to_change="chain",
parameter_list=None,
override_session_state={"output_unit": "USD/MWh"},
)

# import context data:
Expand Down