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

fix key error for missing offshore profiles #404

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 21 additions & 8 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Utility functions for streamlit app."""
import logging
from pathlib import Path
from typing import Literal

Expand Down Expand Up @@ -125,14 +126,21 @@ def calculate_results_list(
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)
# catch all api errors so that the tool is stable
try:
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)
except Exception as exc:
logging.info(f"could not get data: {exc}")

res_details = pd.concat(res_list)

return aggregate_costs(res_details, parameter_to_change)
Expand Down Expand Up @@ -460,6 +468,11 @@ def remove_subregions(api: PtxboaAPI, df: pd.DataFrame, country_name: str):
if country_name in region_list_without_subregions:
region_list_without_subregions.remove(country_name)

# sometimes, not all regions exist
region_list_without_subregions = [
r for r in region_list_without_subregions if r in df.index
]

df = df.loc[region_list_without_subregions]

return df
Expand Down
6 changes: 5 additions & 1 deletion ptxboa/api_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ def _get_hashsum(self, data, opt_input_data):
break

key = (src_reg, res)
profiles_filehash_md5 = self.profiles_hashes.data[key]["filehash_md5"]
try:
profiles_filehash_md5 = self.profiles_hashes.data[key]["filehash_md5"]
except KeyError:
# raise more descriptive error
raise KeyError("No profiles data exists for region=%s, RES=%s." % key)

hash_data = {
"opt_input_data": opt_input_data,
Expand Down
Loading