CarlyneB commited on
Commit
b98d5cb
·
1 Parent(s): 03b9c64

[FIX] tab5: switch to run_expanded_optimization, filter hidden resources, expose Other method variants in method selector

Browse files
Files changed (1) hide show
  1. tabs/tab5_sensitivity.py +35 -14
tabs/tab5_sensitivity.py CHANGED
@@ -2,9 +2,10 @@ import copy
2
  import pandas as pd
3
  import streamlit as st
4
  import plotly.express as px
5
- from config.config import CO2_UNIT, method_order, resource_order
6
- from optimization.optimization import run_optimization
7
- from utils.data_utils import get_resource_to_unit
 
8
  from utils.ui_helpers import register_chart_data, show_bar
9
 
10
 
@@ -17,16 +18,17 @@ def compute_baseline():
17
  all_resources – standard resources + custom batch names.
18
  resource_to_unit – {resource: unit} including custom batches.
19
  """
20
- baseline_success, baseline_result = run_optimization(
21
  resource_caps=st.session_state.resource_caps,
22
  method_constraints=st.session_state.constraints,
23
- method_costs=st.session_state.costs,
24
  custom_resources=st.session_state.get("custom_resources", []),
 
25
  )
26
  baseline_removed = baseline_result["total_removed"] if baseline_success else 0
27
 
28
  custom_resource_names = [b["name"] for b in st.session_state.get("custom_resources", [])]
29
- all_resources = resource_order + custom_resource_names
30
 
31
  resource_to_unit = get_resource_to_unit()
32
  for b in st.session_state.get("custom_resources", []):
@@ -79,11 +81,12 @@ def panel_resource_sensitivity(baseline_removed, all_resources, resource_to_unit
79
  actual_amount = base_cap * pct / 100
80
  perturbed_caps[resource] = actual_amount
81
 
82
- success, result = run_optimization(
83
  resource_caps=perturbed_caps,
84
  method_constraints=st.session_state.constraints,
85
- method_costs=st.session_state.costs,
86
  custom_resources=st.session_state.get("custom_resources", []),
 
87
  )
88
  all_data.append({
89
  "Input": resource,
@@ -130,8 +133,23 @@ def panel_method_sensitivity(baseline_removed):
130
  '<p class="crra-sub-heading">Impact of max allocation per method</p>',
131
  unsafe_allow_html=True,
132
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  selected_methods = st.multiselect(
134
- "Select one or more methods:", method_order, key="crra-select-method"
135
  )
136
 
137
  if not selected_methods:
@@ -143,8 +161,9 @@ def panel_method_sensitivity(baseline_removed):
143
  skipped = []
144
 
145
  for method in selected_methods:
146
- cap_type = st.session_state.constraints[method].get("cap_type", "percent")
147
- current_cap = st.session_state.constraints[method].get(
 
148
  "cap_value", 100 if cap_type == "percent" else 0.0
149
  )
150
 
@@ -154,6 +173,7 @@ def panel_method_sensitivity(baseline_removed):
154
 
155
  for pct in percentages:
156
  perturbed_constraints = copy.deepcopy(st.session_state.constraints)
 
157
  if cap_type == "percent":
158
  actual_cap = min(100, current_cap * pct / 100)
159
  perturbed_constraints[method]["cap_value"] = actual_cap
@@ -163,11 +183,12 @@ def panel_method_sensitivity(baseline_removed):
163
  perturbed_constraints[method]["cap_value"] = actual_cap
164
  cap_label = f"{actual_cap:.2f} {CO2_UNIT}/yr"
165
 
166
- success, result = run_optimization(
167
  resource_caps=st.session_state.resource_caps,
168
  method_constraints=perturbed_constraints,
169
- method_costs=st.session_state.costs,
170
- custom_resources=st.session_state.get("custom_resources", []),
 
171
  )
172
  all_data.append({
173
  "Method": method,
 
2
  import pandas as pd
3
  import streamlit as st
4
  import plotly.express as px
5
+ from config.config import CO2_UNIT, hidden_resource_names, method_order, resource_order
6
+ from utils.data_utils import (get_resource_to_unit, run_expanded_optimization,
7
+ is_other_method, get_other_method_variants, make_variant_name,
8
+ default_constraint)
9
  from utils.ui_helpers import register_chart_data, show_bar
10
 
11
 
 
18
  all_resources – standard resources + custom batch names.
19
  resource_to_unit – {resource: unit} including custom batches.
20
  """
21
+ baseline_success, baseline_result = run_expanded_optimization(
22
  resource_caps=st.session_state.resource_caps,
23
  method_constraints=st.session_state.constraints,
24
+ costs=st.session_state.costs,
25
  custom_resources=st.session_state.get("custom_resources", []),
26
+ enabled_methods=st.session_state.get("enabled_methods", {}),
27
  )
28
  baseline_removed = baseline_result["total_removed"] if baseline_success else 0
29
 
30
  custom_resource_names = [b["name"] for b in st.session_state.get("custom_resources", [])]
31
+ all_resources = [r for r in resource_order if r not in hidden_resource_names] + custom_resource_names
32
 
33
  resource_to_unit = get_resource_to_unit()
34
  for b in st.session_state.get("custom_resources", []):
 
81
  actual_amount = base_cap * pct / 100
82
  perturbed_caps[resource] = actual_amount
83
 
84
+ success, result = run_expanded_optimization(
85
  resource_caps=perturbed_caps,
86
  method_constraints=st.session_state.constraints,
87
+ costs=st.session_state.costs,
88
  custom_resources=st.session_state.get("custom_resources", []),
89
+ enabled_methods=st.session_state.get("enabled_methods", {}),
90
  )
91
  all_data.append({
92
  "Input": resource,
 
133
  '<p class="crra-sub-heading">Impact of max allocation per method</p>',
134
  unsafe_allow_html=True,
135
  )
136
+
137
+ # "Other"-tagged methods with custom batches attached are expanded into
138
+ # independent variants (e.g. "Enhanced weathering - Other mineral: Ganite")
139
+ # by run_expanded_optimization — offer those variants as selectable
140
+ # entries too, alongside the standard method names, so their own cap can
141
+ # be swept just like any other method.
142
+ custom_resources = st.session_state.get("custom_resources", [])
143
+ method_options = []
144
+ for m in method_order:
145
+ variants = get_other_method_variants(m, custom_resources)
146
+ if variants:
147
+ method_options.extend(make_variant_name(m, b["name"]) for b in variants)
148
+ else:
149
+ method_options.append(m)
150
+
151
  selected_methods = st.multiselect(
152
+ "Select one or more methods:", method_options, key="crra-select-method"
153
  )
154
 
155
  if not selected_methods:
 
161
  skipped = []
162
 
163
  for method in selected_methods:
164
+ constraint = st.session_state.constraints.get(method) or default_constraint()
165
+ cap_type = constraint.get("cap_type", "percent")
166
+ current_cap = constraint.get(
167
  "cap_value", 100 if cap_type == "percent" else 0.0
168
  )
169
 
 
173
 
174
  for pct in percentages:
175
  perturbed_constraints = copy.deepcopy(st.session_state.constraints)
176
+ perturbed_constraints.setdefault(method, default_constraint())
177
  if cap_type == "percent":
178
  actual_cap = min(100, current_cap * pct / 100)
179
  perturbed_constraints[method]["cap_value"] = actual_cap
 
183
  perturbed_constraints[method]["cap_value"] = actual_cap
184
  cap_label = f"{actual_cap:.2f} {CO2_UNIT}/yr"
185
 
186
+ success, result = run_expanded_optimization(
187
  resource_caps=st.session_state.resource_caps,
188
  method_constraints=perturbed_constraints,
189
+ costs=st.session_state.costs,
190
+ custom_resources=custom_resources,
191
+ enabled_methods=st.session_state.get("enabled_methods", {}),
192
  )
193
  all_data.append({
194
  "Method": method,