Spaces:
Sleeping
Sleeping
[REFACTOR] tab3: relocate normalize_costs, get_slider_params, is_method_enabled, is_custom_secondary to data_utils; show summary table even when no active method
Browse files- tabs/tab3_coefficients.py +12 -87
tabs/tab3_coefficients.py
CHANGED
|
@@ -5,90 +5,8 @@ from config.config import (method_groups, method_order, resource_groups,
|
|
| 5 |
resource_order, secondary_resources, resource_unit_map, CO2_UNIT,
|
| 6 |
hidden_resource_names)
|
| 7 |
from utils.ui_helpers import format_efficiency_summary, show_bar
|
| 8 |
-
from utils.data_utils import strip_unit_suffix, get_resource_to_group
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def normalize_costs(costs_dict: dict) -> dict:
|
| 12 |
-
"""Strip unit suffixes from resource keys for legacy scenario compatibility."""
|
| 13 |
-
return {strip_unit_suffix(r): v for r, v in costs_dict.items()}
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def get_slider_params(conf: dict) -> tuple:
|
| 17 |
-
"""Derive slider step size and display format from a coefficient config dict.
|
| 18 |
-
|
| 19 |
-
Format is driven by min (display precision); step is driven by range
|
| 20 |
-
(usability) and floored at 1e-06 (Streamlit's hard lower limit).
|
| 21 |
-
|
| 22 |
-
Args:
|
| 23 |
-
conf (dict): slider config with keys "min", "median", "max".
|
| 24 |
-
|
| 25 |
-
Returns:
|
| 26 |
-
tuple[float, str]: (step, fmt) where step is the slider increment and
|
| 27 |
-
fmt is a printf-style format string for display.
|
| 28 |
-
"""
|
| 29 |
-
min_coeff = conf["min"]
|
| 30 |
-
coeff_range = conf["max"] - min_coeff
|
| 31 |
-
|
| 32 |
-
# Format: enough decimals to show the minimum value without scientific notation
|
| 33 |
-
if min_coeff < 1e-10:
|
| 34 |
-
fmt = "%.11f"
|
| 35 |
-
elif min_coeff < 1e-08:
|
| 36 |
-
fmt = "%.9f"
|
| 37 |
-
elif min_coeff < 1e-05:
|
| 38 |
-
fmt = "%.6f"
|
| 39 |
-
elif min_coeff < 0.01:
|
| 40 |
-
fmt = "%.4f"
|
| 41 |
-
elif min_coeff < 1:
|
| 42 |
-
fmt = "%.3f"
|
| 43 |
-
elif min_coeff < 100:
|
| 44 |
-
fmt = "%.2f"
|
| 45 |
-
else:
|
| 46 |
-
fmt = "%.0f"
|
| 47 |
-
|
| 48 |
-
# Step: ~1/1000 of the range, floored at 1e-06 (Streamlit minimum)
|
| 49 |
-
if coeff_range < 0.001:
|
| 50 |
-
step = 1e-06
|
| 51 |
-
elif coeff_range < 0.1:
|
| 52 |
-
step = 0.0001
|
| 53 |
-
elif coeff_range < 1:
|
| 54 |
-
step = 0.001
|
| 55 |
-
elif coeff_range < 10:
|
| 56 |
-
step = 0.01
|
| 57 |
-
elif coeff_range < 100:
|
| 58 |
-
step = 0.1
|
| 59 |
-
else:
|
| 60 |
-
step = 1.0
|
| 61 |
-
|
| 62 |
-
return step, fmt
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def is_method_enabled(method, resource, enabled_methods):
|
| 66 |
-
"""Check whether a method is currently enabled for a given resource.
|
| 67 |
-
|
| 68 |
-
Args:
|
| 69 |
-
method (str): CDR method name.
|
| 70 |
-
resource (str): resource name.
|
| 71 |
-
enabled_methods (dict): {resource: {method: bool}} from session state.
|
| 72 |
-
|
| 73 |
-
Returns:
|
| 74 |
-
bool: True if the method is enabled (defaults to True if not explicitly set).
|
| 75 |
-
"""
|
| 76 |
-
return enabled_methods.get(resource, {}).get(method, True)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def is_custom_secondary(method_name, custom_r, custom_resource_to_sub_group):
|
| 80 |
-
"""Return True if the reference resource for custom_r is secondary for method_name.
|
| 81 |
-
|
| 82 |
-
Args:
|
| 83 |
-
method_name (str): CDR method name.
|
| 84 |
-
custom_r (str): custom batch resource name.
|
| 85 |
-
custom_resource_to_sub_group (dict): {custom_r: reference_resource}.
|
| 86 |
-
|
| 87 |
-
Returns:
|
| 88 |
-
bool
|
| 89 |
-
"""
|
| 90 |
-
sub_group = custom_resource_to_sub_group.get(custom_r)
|
| 91 |
-
return sub_group is not None and (method_name, sub_group) in secondary_resources
|
| 92 |
|
| 93 |
|
| 94 |
def make_sync_s_to_m(slider_key, manual_key):
|
|
@@ -191,7 +109,7 @@ def render_standard_resource_row(method, r, conf, display_label, enabled_methods
|
|
| 191 |
None
|
| 192 |
"""
|
| 193 |
if not is_method_enabled(method, r, enabled_methods):
|
| 194 |
-
st.info(f"
|
| 195 |
return
|
| 196 |
|
| 197 |
is_secondary = (method, r) in secondary_resources
|
|
@@ -283,7 +201,7 @@ def render_custom_resource_row(method, r, conf, enabled_methods, custom_resource
|
|
| 283 |
None
|
| 284 |
"""
|
| 285 |
if not is_method_enabled(method, r, enabled_methods):
|
| 286 |
-
st.info("
|
| 287 |
return
|
| 288 |
|
| 289 |
is_sec = is_custom_secondary(method, r, custom_resource_to_sub_group)
|
|
@@ -562,7 +480,14 @@ def render_tab(cost_sliders):
|
|
| 562 |
if not constraints or m in active_methods
|
| 563 |
]
|
| 564 |
if not method_choices:
|
| 565 |
-
st.warning("No active methods. Enable at least one method in the **Method Constraints** tab.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
return
|
| 567 |
method_lookup = {label: label.split(" | ", 1)[1] for label in method_choices}
|
| 568 |
selected_label = st.selectbox("Select a method:", method_choices, key="crra-select")
|
|
|
|
| 5 |
resource_order, secondary_resources, resource_unit_map, CO2_UNIT,
|
| 6 |
hidden_resource_names)
|
| 7 |
from utils.ui_helpers import format_efficiency_summary, show_bar
|
| 8 |
+
from utils.data_utils import (strip_unit_suffix, get_resource_to_group, normalize_costs,
|
| 9 |
+
get_slider_params, is_method_enabled, is_custom_secondary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def make_sync_s_to_m(slider_key, manual_key):
|
|
|
|
| 109 |
None
|
| 110 |
"""
|
| 111 |
if not is_method_enabled(method, r, enabled_methods):
|
| 112 |
+
st.info(f"Method disabled for **{r}** (see Resource Inputs tab)", icon=":material/info:")
|
| 113 |
return
|
| 114 |
|
| 115 |
is_secondary = (method, r) in secondary_resources
|
|
|
|
| 201 |
None
|
| 202 |
"""
|
| 203 |
if not is_method_enabled(method, r, enabled_methods):
|
| 204 |
+
st.info("Method disabled for this resource (see Resource Inputs tab)", icon=":material/info:")
|
| 205 |
return
|
| 206 |
|
| 207 |
is_sec = is_custom_secondary(method, r, custom_resource_to_sub_group)
|
|
|
|
| 480 |
if not constraints or m in active_methods
|
| 481 |
]
|
| 482 |
if not method_choices:
|
| 483 |
+
st.warning("No active methods. Enable at least one method in the **Method Constraints** tab.", icon=":material/warning:")
|
| 484 |
+
panel_summary_table(
|
| 485 |
+
cost_sliders_norm=cost_sliders_norm,
|
| 486 |
+
custom_sliders_by_method=custom_sliders_by_method,
|
| 487 |
+
enabled_methods=enabled_methods,
|
| 488 |
+
custom_resource_names=custom_resource_names,
|
| 489 |
+
custom_resource_to_sub_group=custom_resource_to_sub_group,
|
| 490 |
+
)
|
| 491 |
return
|
| 492 |
method_lookup = {label: label.split(" | ", 1)[1] for label in method_choices}
|
| 493 |
selected_label = st.selectbox("Select a method:", method_choices, key="crra-select")
|