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

Optionally consider user data in optimization #382

Merged
merged 7 commits into from
May 16, 2024
15 changes: 13 additions & 2 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from ptxboa.utils import is_test


@st.cache_data
def calculate_results_single(
_api: PtxboaAPI,
settings: dict,
user_data: pd.DataFrame | None = None,
optimize_flh: bool = True,
use_user_data_for_optimize_flh: bool = False,
) -> pd.DataFrame:
"""Calculate results for a single set of settings.

Expand All @@ -32,7 +34,10 @@ def calculate_results_single(
same format as for :meth:`~ptxboa.api.PtxboaAPI.calculate()`
"""
res, _metadata = _api.calculate(
user_data=user_data, **settings, optimize_flh=optimize_flh
user_data=user_data,
**settings,
optimize_flh=optimize_flh,
use_user_data_for_optimize_flh=use_user_data_for_optimize_flh,
)

return res
Expand Down Expand Up @@ -113,13 +118,19 @@ def calculate_results_list(
res_list = []
for parameter in parameter_list:
settings.update({parameter_to_change: parameter})
# only optimize when using actual chosen parameter set:

# consider user data in optimization only for parameter set in session state
if st.session_state[parameter_to_change] == parameter:
use_user_data_for_optimize_flh = True
else:
use_user_data_for_optimize_flh = False

res_single = calculate_results_single(
api,
settings,
user_data=st.session_state["user_changes_df"] if apply_user_data else None,
optimize_flh=optimize_flh,
use_user_data_for_optimize_flh=use_user_data_for_optimize_flh,
)
res_list.append(res_single)
res_details = pd.concat(res_list)
Expand Down
4 changes: 4 additions & 0 deletions ptxboa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def calculate(
output_unit: OutputUnitType = "USD/MWh",
user_data: pd.DataFrame | None = None,
optimize_flh: bool = True,
use_user_data_for_optimize_flh: bool = False,
) -> Tuple[pd.DataFrame, object]:
"""Calculate results based on user selection.

Expand Down Expand Up @@ -152,6 +153,8 @@ def calculate(
contains only rows of scenario_data that have been modified.
ids are expected to come as long names. Needs to have the columns
["source_region_code", "process_code", "parameter_code", "value"].
use_user_data_for_optimize_flh: bool
If True: use user data as input for flh optimization as well.

Returns
-------
Expand Down Expand Up @@ -201,6 +204,7 @@ def calculate(
use_ship=(transport == "Ship"),
ship_own_fuel=ship_own_fuel,
optimize_flh=optimize_flh,
use_user_data_for_optimize_flh=use_user_data_for_optimize_flh,
)

result_df = PtxCalc.calculate(data)
Expand Down
Loading
Loading