Spaces:
Running
Running
Download utils/state_utils.py from CGdev/CRRA_optimisation_tool: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/CGdev/CRRA_optimisation_tool/resolve/fd0b601aa4d92b294ea06f8e111c2d1e8221e02c/utils/state_utils.py
- Command line
-
hf download hf://spaces/CGdev/CRRA_optimisation_tool@fd0b601aa4d92b294ea06f8e111c2d1e8221e02c/utils/state_utils.py
-
curl -L -o state_utils.py https://huggingface.co/spaces/CGdev/CRRA_optimisation_tool/resolve/fd0b601aa4d92b294ea06f8e111c2d1e8221e02c/utils/state_utils.py
4.18 kB
| import streamlit as st | |
| def init_custom_resources(): | |
| """Initialise custom resource session state keys and coerce amounts/coefficients to float. | |
| Ensures custom_resources, enabled_methods, and auto_disabled_methods exist in | |
| session state, then sanitises every batch so numeric fields are float (not str). | |
| """ | |
| if "custom_resources" not in st.session_state: | |
| st.session_state.custom_resources = [] | |
| if "enabled_methods" not in st.session_state: | |
| st.session_state.enabled_methods = {} | |
| if "auto_disabled_methods" not in st.session_state: | |
| st.session_state.auto_disabled_methods = set() | |
| for batch in st.session_state.custom_resources: | |
| try: | |
| batch["amount"] = float(batch["amount"]) | |
| except (ValueError, TypeError): | |
| batch["amount"] = 0.0 | |
| for m, coefs in batch.get("methods", {}).items(): | |
| for key in ("min", "median", "max"): | |
| try: | |
| coefs[key] = float(coefs[key]) | |
| except (ValueError, TypeError): | |
| coefs[key] = 0.0 | |
| def clear_widget_keys(): | |
| """Delete all Streamlit widget state keys for sliders, manual inputs, and toggles. | |
| Called after loading a new file or scenario so widgets re-initialise from | |
| fresh session-state values instead of showing stale cached values. | |
| """ | |
| prefixes = ("toggle_", "cap_type_", "slider_", "manual_") | |
| for k in [k for k in st.session_state if k.startswith(prefixes)]: | |
| del st.session_state[k] | |
| def apply_auto_disable_logic(method_to_primary_resources, secondary_resources): | |
| """Synchronise auto-disabled method state based on current resource availability. | |
| Runs three steps: | |
| 1. Count active primary resources per method (custom batches also count). | |
| 2. Re-enable any method previously auto-disabled that now has resources. | |
| 3. Disable any method whose primary resource count just dropped to zero. | |
| Args: | |
| method_to_primary_resources (dict[str, list[str]]): method → list of | |
| primary resource names (excludes secondary resources). | |
| secondary_resources (set[tuple[str, str]]): set of (method, resource) | |
| pairs where the resource is optional for that method. | |
| Returns: | |
| None | |
| """ | |
| method_active_resource_count = {} | |
| for r, methods in st.session_state.enabled_methods.items(): | |
| for m, ok in methods.items(): | |
| if (m, r) in secondary_resources: | |
| continue | |
| method_active_resource_count.setdefault(m, 0) | |
| if ok: | |
| method_active_resource_count[m] += 1 | |
| for batch in st.session_state.get("custom_resources", []): | |
| if float(batch.get("amount") or 0) <= 0: | |
| continue | |
| for m_name in batch.get("methods", {}).keys(): | |
| method_active_resource_count.setdefault(m_name, 0) | |
| method_active_resource_count[m_name] += 1 | |
| methods_with_no_active_resource = [ | |
| m for m in method_to_primary_resources | |
| if method_active_resource_count.get(m, 0) == 0 | |
| ] | |
| for m in list(st.session_state.auto_disabled_methods): | |
| if method_active_resource_count.get(m, 0) > 0: | |
| if m in st.session_state.constraints: | |
| st.session_state.constraints[m]["active"] = True | |
| st.session_state.constraints[m]["cap_type"] = "percent" | |
| st.session_state.constraints[m]["cap_value"] = 100 | |
| st.session_state[f"toggle_{m}"] = True | |
| st.session_state[f"slider_{m}"] = 100 | |
| st.session_state.auto_disabled_methods.discard(m) | |
| st.session_state.setdefault("just_activated_methods", set()).add(m) | |
| for m in methods_with_no_active_resource: | |
| st.session_state.auto_disabled_methods.add(m) | |
| if st.session_state.constraints.get(m, {}).get("active", True): | |
| st.session_state.constraints.setdefault(m, {"active": True, "cap_type": "percent", "cap_value": 100}) | |
| st.session_state.constraints[m]["active"] = False | |
| toggle_key = f"toggle_{m}" | |
| if toggle_key in st.session_state: | |
| st.session_state[toggle_key] = False | |