Skip to content

Commit

Permalink
🐛 indicator-upgrader: team feedback (#2900)
Browse files Browse the repository at this point in the history
* 🐛 indicator-upgrader: team feedback

* fix session_state ignore fields
  • Loading branch information
lucasrodes authored Jun 26, 2024
1 parent 9f80592 commit 70b0397
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 36 deletions.
57 changes: 34 additions & 23 deletions apps/wizard/app_pages/indicator_upgrade/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,42 @@
indicator_mapping = render_indicator_mapping(search_form)
# log.info(f"INDICATORS CONFIG (2): {indicator_config}")

##########################################################################################
# 3 GET CHARTS
#
# Once the indicator mapping is done, we retrieve the affected charts (those that rely on
# the indicators in the mapping. create the revisions. We store these under what we
# call the "submission_config". This is a dataclass that holds the charts and updaters.
#
##########################################################################################
if st.session_state.submitted_datasets and st.session_state.submitted_indicators:
# log.info(f"INDICATOR CONFIG (3): {indicator_config}")
# st.write(reset_indicator_form)
if indicator_mapping is not None:
if indicator_mapping == {}:
msg_error = "No indicators selected! Please select at least one indicator."
st.error(msg_error)
else:
charts = get_affected_charts_and_preview(
indicator_mapping,
)
##########################################################################################
# 3 GET CHARTS
#
# Once the indicator mapping is done, we retrieve the affected charts (those that rely on
# the indicators in the mapping. create the revisions. We store these under what we
# call the "submission_config". This is a dataclass that holds the charts and updaters.
#
##########################################################################################
if st.session_state.submitted_indicators:
# log.info(f"INDICATOR CONFIG (3): {indicator_config}")
# st.write(reset_indicator_form)
if indicator_mapping is not None:
if indicator_mapping == {}:
msg_error = "No indicators selected! Please select at least one indicator."
st.error(msg_error)
else:
charts = get_affected_charts_and_preview(
indicator_mapping,
)

##########################################################################################
# 4 UPDATE CHARTS
#
# TODO: add description
##########################################################################################
if st.session_state.submitted_charts:
if isinstance(charts, list) and len(charts) > 0:
st.toast("Updating charts...")
push_new_charts(charts, SCHEMA_CHART_CONFIG)

##########################################################################################
# 4 UPDATE CHARTS
#
# TODO: add description
##########################################################################################
if st.session_state.submitted_datasets and st.session_state.submitted_charts and st.session_state.submitted_indicators:
if isinstance(charts, list) and len(charts) > 0:
st.toast("Updating charts...")
push_new_charts(charts, SCHEMA_CHART_CONFIG)
# if st.session_state.submitted_datasets and st.session_state.submitted_charts and st.session_state.submitted_indicators:
# if isinstance(charts, list) and len(charts) > 0:
# st.toast("Updating charts...")
# push_new_charts(charts, SCHEMA_CHART_CONFIG)
50 changes: 37 additions & 13 deletions apps/wizard/app_pages/indicator_upgrade/indicator_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def render_indicator_mapping(search_form) -> Dict[int, int]:
# 1/ PROCESSING: Get indicators, find similarities and suggestions, etc.
###########################################################################
indicator_upgrades, indicator_id_to_display, df_data = get_params(search_form)
if "indicator_upgrades_ignore" not in st.session_state:
st.session_state.indicator_upgrades_ignore = {iu.key: False for iu in indicator_upgrades}

###########################################################################
# 2/ DISPLAY: Show the indicator mapping form
Expand Down Expand Up @@ -86,15 +84,22 @@ def render_indicator_mapping(search_form) -> Dict[int, int]:
return indicator_mapping


def get_params(search_form):
"""Get all the parameters required to display the widgets.
@st.cache_data
def _get_params_cached(
dataset_old_id,
dataset_new_id,
map_identical_indicators,
similarity_function_name,
enable_bulk_explore,
):
"""Cached version of `get_params`.
It creates IndicatorUpgrade objects.
`get_params` can't be hashed because its input is a custom object.
"""
# 1/ Get indicators from old and new datasets
old_indicators, new_indicators = get_indicators_from_datasets(
search_form.dataset_old_id,
search_form.dataset_new_id,
dataset_old_id,
dataset_new_id,
show_new_not_in_old=False,
)

Expand All @@ -110,15 +115,15 @@ def get_params(search_form):
indicator_mapping_auto, missing_old, missing_new = preliminary_mapping_cached(
old_indicators=old_indicators,
new_indicators=new_indicators,
match_identical=search_form.map_identical_indicators,
match_identical=map_identical_indicators,
)

# 1.4/ Get remaining mapping suggestions
# This is for those indicators which couldn't be automatically mapped
suggestions = find_mapping_suggestions_cached(
missing_old=missing_old,
missing_new=missing_new,
similarity_name=search_form.similarity_function_name,
similarity_name=similarity_function_name,
) # type: ignore

iu = []
Expand All @@ -144,11 +149,30 @@ def get_params(search_form):
iu = iu_auto + iu_man

# Get datapoints
if search_form.enable_bulk_explore:
if enable_bulk_explore:
df_data = get_indicator_data_cached(list(set(old_indicators["id"]) | set(new_indicators["id"])))
else:
df_data = None

# Set states
st.session_state.indicator_upgrades_ignore = {i.key: False for i in iu}

return iu, indicator_id_to_display, df_data


def get_params(search_form):
"""Get all the parameters required to display the widgets.
It creates IndicatorUpgrade objects.
"""
iu, indicator_id_to_display, df_data = _get_params_cached(
search_form.dataset_old_id,
search_form.dataset_new_id,
search_form.map_identical_indicators,
search_form.similarity_function_name,
search_form.enable_bulk_explore,
)

return iu, indicator_id_to_display, df_data


Expand Down Expand Up @@ -270,9 +294,9 @@ def _get_skip_state() -> bool:
if self.key in st.session_state["indicator_upgrades_ignore"]:
return st.session_state["indicator_upgrades_ignore"][self.key]
else:
raise ValueError(
f"No key {self.key} in st.session_state['indicator_upgrades_ignore']! It should be created when creating the indicator upgrades."
)
error_msg = f"No key {self.key} in st.session_state['indicator_upgrades_ignore']! It should be created when creating the indicator upgrades."
log.error(error_msg)
raise ValueError(error_msg)
else:
raise ValueError(
"No indicator_upgrades_ignore in session state! It should be created when creating the indicator upgrades."
Expand Down

0 comments on commit 70b0397

Please sign in to comment.