CarlyneB commited on
Commit
cf283fb
·
1 Parent(s): 36d3ed3

[FIX] tab6: source resource list from resource_usage (not resource_caps) to include custom batches, fix resource usage computation, remove broken CSV export

Browse files
Files changed (1) hide show
  1. tabs/tab6_scenarios.py +15 -13
tabs/tab6_scenarios.py CHANGED
@@ -58,9 +58,14 @@ def prepare_comparison_data(selected):
58
  m for name in selected
59
  for m in st.session_state.scenarios[name].get("allocations", {})
60
  })
 
 
 
 
 
61
  all_resources = sorted({
62
  r for name in selected
63
- for r in st.session_state.scenarios[name].get("resource_caps", {})
64
  if r not in hidden_resource_names
65
  })
66
 
@@ -69,7 +74,11 @@ def prepare_comparison_data(selected):
69
  for name in selected:
70
  scenario = st.session_state.scenarios[name]
71
  allocations = scenario.get("allocations", {})
72
- method_costs = scenario.get("method_costs", {}) # ← fix: was using method_constraints
 
 
 
 
73
 
74
  row = {
75
  "Scenario": name,
@@ -82,10 +91,9 @@ def prepare_comparison_data(selected):
82
  method_alloc_rows.append({"Scenario": name, "Method": method, f"{CO2_UNIT}": val})
83
 
84
  for resource in all_resources:
85
- total_used = 0
86
- for method, tco2 in allocations.items():
87
- used = method_costs.get(method, {}).get(resource, 0) * tco2
88
- total_used += used
89
  if used > 0:
90
  resource_usage_rows.append({
91
  "Scenario": name, "Method": method,
@@ -180,7 +188,7 @@ def render_tab():
180
  with col1:
181
  st.markdown(f"**{name}**")
182
  with col2:
183
- if st.button("❌", key=f"remove_{name}"):
184
  st.session_state.scenarios.pop(name, None)
185
  st.rerun()
186
 
@@ -193,12 +201,6 @@ def render_tab():
193
 
194
  st.markdown("""<p class="crra-sub-heading">Summary Table</p>""", unsafe_allow_html=True)
195
  st.dataframe(df_summary.style.format(na_rep="-", thousands=","), width='stretch')
196
- st.download_button(
197
- "Export Comparison CSV",
198
- df_summary.to_csv(index=False).encode("utf-8"),
199
- "scenario_comparison.csv", "text/csv",
200
- icon=":material/download:"
201
- )
202
 
203
  show_method_allocations(df_methods)
204
  show_resource_usage(df_resources)
 
58
  m for name in selected
59
  for m in st.session_state.scenarios[name].get("allocations", {})
60
  })
61
+ # Built from the stored resource_usage keys (not resource_caps): a custom
62
+ # batch pooled with a standard resource (e.g. "Basalt A" complementing
63
+ # "Basalt mineral") is never added to resource_caps — only resource_usage
64
+ # tracks it — so sourcing the list from resource_caps would silently drop
65
+ # every such batch from the comparison entirely.
66
  all_resources = sorted({
67
  r for name in selected
68
+ for r in st.session_state.scenarios[name].get("resource_usage", {})
69
  if r not in hidden_resource_names
70
  })
71
 
 
74
  for name in selected:
75
  scenario = st.session_state.scenarios[name]
76
  allocations = scenario.get("allocations", {})
77
+ # Use the resource_usage already computed by the LP at save time —
78
+ # NOT a recomputation from method_costs × allocations, which double-
79
+ # counts standard resources that are pooled with a custom batch (the
80
+ # LP only attributes the standard's own share to it, not the batch's).
81
+ resource_usage = scenario.get("resource_usage", {})
82
 
83
  row = {
84
  "Scenario": name,
 
91
  method_alloc_rows.append({"Scenario": name, "Method": method, f"{CO2_UNIT}": val})
92
 
93
  for resource in all_resources:
94
+ per_method = resource_usage.get(resource, {})
95
+ total_used = sum(per_method.values())
96
+ for method, used in per_method.items():
 
97
  if used > 0:
98
  resource_usage_rows.append({
99
  "Scenario": name, "Method": method,
 
188
  with col1:
189
  st.markdown(f"**{name}**")
190
  with col2:
191
+ if st.button(label="", key=f"remove_{name}", icon=":material/delete:", help=f"Remove scenario {name} from comparison"):
192
  st.session_state.scenarios.pop(name, None)
193
  st.rerun()
194
 
 
201
 
202
  st.markdown("""<p class="crra-sub-heading">Summary Table</p>""", unsafe_allow_html=True)
203
  st.dataframe(df_summary.style.format(na_rep="-", thousands=","), width='stretch')
 
 
 
 
 
 
204
 
205
  show_method_allocations(df_methods)
206
  show_resource_usage(df_resources)