Skip to content

Commit

Permalink
Merge pull request #103 from agoenergy/feat/use_user_data_in_calculat…
Browse files Browse the repository at this point in the history
…ions

User data in calculations
  • Loading branch information
markushal authored Nov 14, 2023
2 parents e78931b + 35b93cf commit dcd0db8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
31 changes: 24 additions & 7 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@


@st.cache_data()
def calculate_results_single(_api: PtxboaAPI, settings: dict) -> pd.DataFrame:
def calculate_results_single(
_api: PtxboaAPI,
settings: dict,
user_data: pd.DataFrame | None = None,
) -> pd.DataFrame:
"""Calculate results for a single set of settings.
Parameters
Expand All @@ -28,7 +32,7 @@ def calculate_results_single(_api: PtxboaAPI, settings: dict) -> pd.DataFrame:
pd.DataFrame
same format as for :meth:`~ptxboa.api.PtxboaAPI.calculate()`
"""
res = _api.calculate(**settings)
res = _api.calculate(user_data=user_data, **settings)

return res

Expand Down Expand Up @@ -75,6 +79,7 @@ def calculate_results_list(
settings: dict,
parameter_to_change: str,
parameter_list: list = None,
user_data: pd.DataFrame | None = None,
) -> pd.DataFrame:
"""Calculate results for source regions and one selected target country.
Expand Down Expand Up @@ -104,7 +109,7 @@ def calculate_results_list(
for parameter in parameter_list:
settings2 = settings.copy()
settings2[parameter_to_change] = parameter
res_single = calculate_results_single(api, settings2)
res_single = calculate_results_single(api, settings2, user_data=user_data)
res_list.append(res_single)
res_details = pd.concat(res_list)

Expand Down Expand Up @@ -308,7 +313,7 @@ def create_sidebar(api: PtxboaAPI):
return settings


def create_world_map(settings: dict, res_costs: pd.DataFrame):
def create_world_map(api: PtxboaAPI, settings: dict, res_costs: pd.DataFrame):
"""Create world map."""
parameter_to_show_on_map = "Total"

Expand All @@ -324,6 +329,9 @@ def create_world_map(settings: dict, res_costs: pd.DataFrame):
(1, st.session_state["colors"][9]), # Ending color at the maximum data value
]

# remove subregions from deep dive countries (otherwise colorscale is not correct)
res_costs = remove_subregions(api, res_costs, settings)

# Create custom hover text:
custom_hover_data = res_costs.apply(
lambda x: f"<b>{x.name}</b><br><br>"
Expand Down Expand Up @@ -513,7 +521,7 @@ def content_dashboard(api, res_costs: dict, context_data: dict, settings: pd.Dat
create_infobox(context_data, settings)

with c_1:
create_world_map(settings, res_costs)
create_world_map(api, settings, res_costs)

st.divider()

Expand All @@ -528,6 +536,9 @@ def content_dashboard(api, res_costs: dict, context_data: dict, settings: pd.Dat
st.write("Chosen settings:")
st.write(settings)

st.write("res_cost")
st.write(res_costs)


def content_market_scanning(
api: PtxboaAPI, res_costs: pd.DataFrame, settings: dict
Expand Down Expand Up @@ -714,15 +725,21 @@ def display_costs(
display_costs(res_costs_without_subregions, "region", "Costs by region:", settings)

# Display costs by scenario:
res_scenario = calculate_results_list(api, settings, "scenario")
res_scenario = calculate_results_list(
api, settings, "scenario", user_data=st.session_state["user_changes_df"]
)
display_costs(res_scenario, "scenario", "Costs by data scenario:", settings)

# Display costs by RE generation:
# TODO: remove PV tracking manually, this needs to be fixed in data
list_res_gen = api.get_dimension("res_gen").index.to_list()
list_res_gen.remove("PV tracking")
res_res_gen = calculate_results_list(
api, settings, "res_gen", parameter_list=list_res_gen
api,
settings,
"res_gen",
parameter_list=list_res_gen,
user_data=st.session_state["user_changes_df"],
)
display_costs(
res_res_gen, "res_gen", "Costs by renewable electricity source:", settings
Expand Down
12 changes: 7 additions & 5 deletions ptxboa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_input_data(
self,
scenario: ScenarioCode,
long_names: bool = True,
user_data: dict = None,
user_data: pd.DataFrame | None = None,
) -> pd.DataFrame:
"""Return scenario data.
Expand All @@ -78,7 +78,8 @@ def get_input_data(
user_data : pd.DataFrame | None, optional
user data that overrides scenario data
contains only rows of scenario_data that have been modified.
ids are expected to come as long names
ids are expected to come as long names. Needs to have the columns
["source_region_code", "process_code", "parameter_code", "value"].
Returns
-------
Expand All @@ -102,7 +103,7 @@ def calculate(
transport: str,
ship_own_fuel: bool = False, # TODO: no correctly passed by app
output_unit="USD/MWh",
user_data: dict = None,
user_data: pd.DataFrame | None = None,
) -> pd.DataFrame:
"""Calculate results based on user selection.
Expand All @@ -128,10 +129,11 @@ def calculate(
`True` if ship uses product as fuel
output_unit : str, optional
output unit
user_data: pd.DataFrame
user_data : pd.DataFrame | None, optional
user data that overrides scenario data
contains only rows of scenario_data that have been modified.
ids are expected to come as long names
ids are expected to come as long names. Needs to have the columns
["source_region_code", "process_code", "parameter_code", "value"].
Returns
Expand Down
4 changes: 3 additions & 1 deletion ptxboa_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
settings = pf.create_sidebar(api)

# calculate results:
res_costs = pf.calculate_results_list(api, settings, "region")
res_costs = pf.calculate_results_list(
api, settings, "region", user_data=st.session_state["user_changes_df"]
)

# import context data:
cd = st.cache_resource(pf.import_context_data)()
Expand Down

0 comments on commit dcd0db8

Please sign in to comment.