-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmechanism_feedback.py
More file actions
678 lines (550 loc) · 29.1 KB
/
Copy pathmechanism_feedback.py
File metadata and controls
678 lines (550 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
"""
Streamlit App for Blind Rating of Kinetic Fitting Results (Round 1 vs Round 2)
Features:
1. Blind evaluation of 12 candidate runs (6 pairs of before/after human feedback).
2. Deterministic shuffling per user to prevent bias without losing position on refresh.
3. Completely hidden metadata/directory names to avoid info leaks.
4. Aggregated Admin View for Round 1 vs Round 2 head-to-head comparison.
5. Includes both Concentration and Rate fitting plots.
"""
import streamlit as st
import regex as re
import glob
import json
import base64
import warnings
import hashlib
import random
from datetime import datetime
from pathlib import Path
from typing import Optional, Tuple, List, Dict, Any
from dataclasses import dataclass, field
import pandas as pd
import numpy as np
import requests
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from scipy.integrate import odeint
import h5py
# =============================================================================
# Configuration & Hardcoded Mapping
# =============================================================================
PAIRS =[
{
"type": "No Feedback (Claude Sonnet 4.5)",
"r1_path": "round1/kinetic_fitting_no_fb_claudesonnet45_3",
"r2_path": "round2/kinetic_fitting_no_fb_claudesonnet45_hf1_3",
},
{
"type": "No Feedback (GPT-4o)",
"r1_path": "round1/kinetic_fitting_no_fb_gpt4o_feedback_4",
"r2_path": "round2/kinetic_fitting_no_fb_gpt4o_feedback_hf1_4",
},
{
"type": "Text+Chemistry Feedback every round (Claude Sonnet 4.5)",
"r1_path": "round1/kinetic_fitting_tasks_with_visionfb_and_chemistryopus_no_text_3",
"r2_path": "round2/kinetic_fitting_tasks_with_visionfb_and_chemistryopus_no_text_hf1_3",
},
{
"type": "Text Feedback (Claude Sonnet 4.5)",
"r1_path": "round1/kinetic_fitting_with_fb_claudesonnet45_3",
"r2_path": "round2/kinetic_fitting_with_fb_claudesonnet45_hf1_3",
},
{
"type": "Text+Vision Feedback (Claude Sonnet 4.5)",
"r1_path": "round1/kinetic_fitting_with_visionfb_claudesonnet45_5",
"r2_path": "round2/kinetic_fitting_with_visionfb_claudesonnet45_hf1_5",
},
{
"type": "Text+Vision+Chemistry Feedback (Claude Sonnet 4.5)",
"r1_path": "round1/kinetic_fitting_with_visionfb_claudesonnet45_with_opus_fb_3",
"r2_path": "round2/kinetic_fitting_with_visionfb_claudesonnet45_with_chem_fb_hf1_3",
}
]
DATA_PATH = "data/experimental_data.h5"
# GitHub configuration
GITHUB_API = "https://api.github.com"
RATINGS_ROOT = "feedback_ratings"
# Performance settings
MAX_POINTS_PER_TRACE = 200
USE_WEBGL = True
# Color scheme for species
SPECIES_COLORS = {
"O2": "#2ecc71", "H2O2": "#3498db", "Ru_Dimer": "#27ae60",
"Ru2_dim": "#27ae60", "Inactive": "#9b59b6", "Ru_inactive": "#9b59b6",
"Ru2_inactive": "#9b59b6", "RuII": "#e67e22", "RuIII": "#e74c3c",
"RuII_ex": "#f1c40f", "S2O8": "#8e44ad", "SO4": "#d35400",
}
PARAM_COLORS =[
"#440154", "#482878", "#3e4989", "#31688e", "#26838f",
"#1f9e89", "#35b779", "#6ece58", "#b5de2b", "#fde725"
]
# =============================================================================
# Helper Utilities
# =============================================================================
def _slugify(text: str) -> str:
text = text.lower().strip()
return re.sub(r"[^a-z0-9]+", "_", text).strip("_")
def downsample_data(x: np.ndarray, y: np.ndarray, max_points: int = MAX_POINTS_PER_TRACE) -> Tuple[np.ndarray, np.ndarray]:
if len(x) <= max_points:
return x, y
indices = np.linspace(0, len(x) - 1, max_points, dtype=int)
return x[indices], y[indices]
def get_shuffled_runs(username: str) -> List[dict]:
"""Deterministically shuffles the 12 candidate runs based on the username."""
runs =[]
for pair in PAIRS:
runs.append({"path": pair["r1_path"], "round": "Round 1 (Before FB)", "type": pair["type"]})
runs.append({"path": pair["r2_path"], "round": "Round 2 (After FB)", "type": pair["type"]})
# Create a stable seed based on the username
seed = int(hashlib.md5(username.encode()).hexdigest(), 16) % (2**32)
rng = random.Random(seed)
rng.shuffle(runs)
for i, run in enumerate(runs):
run["candidate_id"] = f"Candidate {i+1}"
return runs
# =============================================================================
# GitHub Storage
# =============================================================================
def _github_headers() -> dict:
token = st.secrets.get("GITHUB_TOKEN", "")
if not token:
st.error("⚠️ `GITHUB_TOKEN` is not set in Streamlit secrets.")
st.stop()
return {"Authorization": f"token {token}", "Accept": "application/vnd.github+json"}
def _github_branch() -> str:
return st.secrets.get("GITHUB_BRANCH", "main")
def _github_repo() -> str:
return st.secrets.get("GITHUB_REPO", "")
def _github_contents_url(repo_path: str) -> str:
repo = _github_repo()
return f"{GITHUB_API}/repos/{repo}/contents/{repo_path}"
def _load_json_from_github(repo_path: str) -> Tuple[dict, Optional[str]]:
url = _github_contents_url(repo_path)
params = {"ref": _github_branch()}
resp = requests.get(url, headers=_github_headers(), params=params)
if resp.status_code == 200:
payload = resp.json()
content = base64.b64decode(payload["content"]).decode("utf-8")
return json.loads(content), payload["sha"]
return {}, None
def _save_json_to_github(repo_path: str, data: dict, sha: Optional[str] = None) -> bool:
url = _github_contents_url(repo_path)
content_bytes = json.dumps(data, indent=2, default=str).encode("utf-8")
encoded = base64.b64encode(content_bytes).decode("utf-8")
body = {
"message": f"rating update: {repo_path} @ {datetime.now().isoformat()}",
"content": encoded,
"branch": _github_branch(),
}
if sha is not None:
body["sha"] = sha
resp = requests.put(url, headers=_github_headers(), json=body)
if resp.status_code not in (200, 201):
st.error(f"Failed to save ratings ({resp.status_code}): {resp.text}")
return False
return True
def load_user_ratings(username: str) -> Tuple[dict, Optional[str]]:
path = f"{RATINGS_ROOT}/{_slugify(username)}.json"
return _load_json_from_github(path)
def save_user_ratings(username: str, data: dict, sha: Optional[str]) -> bool:
path = f"{RATINGS_ROOT}/{_slugify(username)}.json"
return _save_json_to_github(path, data, sha)
# =============================================================================
# Math / ODE / Data
# =============================================================================
@dataclass
class Reaction:
equation: str
type: str
reactants: List[str]
products: List[str]
stoichiometry: Dict[str, float]
quantum_yield: Optional[Tuple[float, float]] = None
fitted_k: Optional[float] = None
fitted_quantum_yield: Optional[float] = None
@classmethod
def from_dict(cls, rxn_dict: dict) -> "Reaction":
equation = rxn_dict["equation"].replace("<->", "->")
left, right = equation.split("->")
reactants =[s.strip() for s in left.split("+") if s.strip()]
products =[s.strip() for s in right.split("+") if s.strip()]
ignored = {"hv", "H2O", "OH", "products", "H", "H+", ""}
stoich = {}
def parse(species_str):
if not species_str: return 1, ""
parts = species_str.split(" ", 1)
if len(parts) == 2 and parts[0].isdigit():
return int(parts[0]), parts[1]
return 1, species_str
for r in reactants:
c, s = parse(r)
if s and s not in ignored: stoich[s] = stoich.get(s, 0) - c
for p in products:
c, s = parse(p)
if s and s not in ignored: stoich[s] = stoich.get(s, 0) + c
return cls(
equation=rxn_dict["equation"], type=rxn_dict["type"],
reactants=reactants, products=products, stoichiometry=stoich,
quantum_yield=rxn_dict.get("quantum_yield"),
fitted_k=rxn_dict.get("fitted_k"),
fitted_quantum_yield=rxn_dict.get("fitted_quantum_yield"),
)
def create_ode_func(reactions: List[Reaction], species_idx: Dict[str, int]):
PATHLENGTH, EPSILON_RU_II, EPSILON_RU_III, AVOGADRO_NUMBER = 2.25, 8500.0, 540.0, 6.022e23
VOLUME_L = PATHLENGTH * 1e-3
def ode_func(y: np.ndarray, t: float, params: dict, conditions: dict) -> np.ndarray:
dydt = np.zeros_like(y)
c_ru_ii_M = y[species_idx["RuII"]] * 1e-6 if "RuII" in species_idx else 0
c_ru_iii_M = y[species_idx["RuIII"]] * 1e-6 if "RuIII" in species_idx else 0
absorbance_tot = ((c_ru_ii_M * EPSILON_RU_II) + (c_ru_iii_M * EPSILON_RU_III)) * PATHLENGTH
absorptance_factor = fraction_ru_ii = fraction_ru_iii = 0
if absorbance_tot >= 1e-9:
absorptance_factor = 1 - 10 ** (-absorbance_tot)
fraction_ru_ii = (c_ru_ii_M * EPSILON_RU_II * PATHLENGTH) / absorbance_tot
fraction_ru_iii = (c_ru_iii_M * EPSILON_RU_III * PATHLENGTH) / absorbance_tot
if conditions.get("photon_flux"):
incident_flux = (conditions["photon_flux"] / AVOGADRO_NUMBER / VOLUME_L) * 1e6
else:
irr = conditions.get("irradiance", 1000) * 1e-4 / 4.41e-19
incident_flux = (irr / PATHLENGTH / AVOGADRO_NUMBER * 1000) * 1e6
for i, rxn in enumerate(reactions):
rate = 0.0
if rxn.type == "light":
flux = 0.0
if "RuII" in rxn.reactants: flux = incident_flux * absorptance_factor * fraction_ru_ii
elif "RuIII" in rxn.reactants: flux = incident_flux * absorptance_factor * fraction_ru_iii
rate = flux * params.get(f"qy_{i}", rxn.quantum_yield[0] if rxn.quantum_yield else 0.1)
else:
rate = params.get(f"k_{i}", 1.0)
for r in rxn.reactants:
if r in species_idx: rate *= y[species_idx[r]] ** (2 if rxn.equation.startswith(f"2 {r}") else 1)
for s, coeff in rxn.stoichiometry.items():
if s in species_idx: dydt[species_idx[s]] += coeff * rate
return dydt
return ode_func
@st.cache_data(ttl=3600, show_spinner=False)
def simulate_species_evolution_cached(
network_json: str, params_json: str, conditions_json: str, t_min: float, t_max: float, n_points: int = 200
):
network, params, conditions = json.loads(network_json), json.loads(params_json), json.loads(conditions_json)
reactions =[Reaction.from_dict(r) for r in network.get("reactions", [])]
all_species = set()
for rxn in reactions: all_species.update(rxn.stoichiometry.keys())
species_list = sorted({s for s in all_species if s and s.strip()})
species_idx = {sp: i for i, sp in enumerate(species_list)}
ode_func = create_ode_func(reactions, species_idx)
time_points = np.linspace(t_min, t_max, n_points)
y0 = np.zeros(len(species_list))
if "RuII" in species_idx: y0[species_idx["RuII"]] = conditions.get("c_Ru", 10)
if "S2O8" in species_idx: y0[species_idx["S2O8"]] = conditions.get("c_S2O8", 6000)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
y_solution = odeint(ode_func, y0, time_points, args=(params, conditions), rtol=1e-6, atol=1e-8)
return y_solution.tolist(), species_list, species_idx, time_points.tolist()
def create_synthetic_dataset() -> Dict[str, dict]:
"""Create a synthetic dataset with multiple experiments for rate plot visualization."""
dataset = {}
# Vary Ru concentration
for c_ru in[5, 10, 20, 50, 100]:
name = f"exp_Ru_{c_ru}"
time = np.linspace(0, 600, 100)
scale = c_ru / 50.0
oxygen = 50 * scale * (1 - np.exp(-time / (100 / scale))) + np.random.normal(0, 0.5, len(time))
oxygen = np.maximum(oxygen, 0)
dataset[name] = {
"time": time.tolist(), "oxygen": oxygen.tolist(),
"metadata": {"c_Ru": float(c_ru), "c_S2O8": 6000.0, "irradiance": 1000.0, "pH": 7.0, "photon_flux": None}
}
# Vary S2O8 concentration
for c_s2o8 in[1000, 3000, 6000, 10000]:
name = f"exp_S2O8_{c_s2o8}"
time = np.linspace(0, 600, 100)
scale = c_s2o8 / 6000.0
oxygen = 50 * (1 - np.exp(-time / 100)) * min(scale, 1.5) + np.random.normal(0, 0.5, len(time))
oxygen = np.maximum(oxygen, 0)
dataset[name] = {
"time": time.tolist(), "oxygen": oxygen.tolist(),
"metadata": {"c_Ru": 50.0, "c_S2O8": float(c_s2o8), "irradiance": 1000.0, "pH": 7.0, "photon_flux": None}
}
# Vary irradiance
for irr in[500, 1000, 1500, 2000, 3000]:
name = f"exp_irr_{irr}"
time = np.linspace(0, 600, 100)
scale = irr / 1000.0
oxygen = 50 * (1 - np.exp(-time / (100 / scale))) + np.random.normal(0, 0.5, len(time))
oxygen = np.maximum(oxygen, 0)
dataset[name] = {
"time": time.tolist(), "oxygen": oxygen.tolist(),
"metadata": {"c_Ru": 50.0, "c_S2O8": 6000.0, "irradiance": float(irr), "pH": 7.0, "photon_flux": None}
}
return dataset
# =============================================================================
# Plotting Functions
# =============================================================================
def create_interactive_concentration_plot(network, params, exp_data, exp_name="Rep Exp"):
time_exp, oxygen_exp = np.array(exp_data["time"]), np.array(exp_data["oxygen"])
time_ds, oxygen_ds = downsample_data(time_exp, oxygen_exp)
y_sol, s_list, s_idx, t_sim = simulate_species_evolution_cached(
json.dumps(network, default=str), json.dumps(params, default=str),
json.dumps(exp_data["metadata"], default=str), float(time_exp.min()), float(time_exp.max())
)
y_sol, t_sim = np.array(y_sol), np.array(t_sim)
bulk = {"S2O8", "SO4"}
cats = {"RuII", "RuIII", "Ru_Dimer", "Ru2_dim", "Inactive", "Ru_inactive", "Ru2_inactive", "RuII_ex"}
fig = make_subplots(rows=1, cols=2, subplot_titles=("A: Products & Intermediates", "B: Catalyst Species"))
ScatterClass = go.Scattergl if USE_WEBGL else go.Scatter
fig.add_trace(ScatterClass(x=time_ds, y=oxygen_ds, mode="markers", name="[O₂] Exp", marker=dict(color="gray", size=5), legend="legend1"), row=1, col=1)
for sp in[s for s in s_list if s not in bulk]:
if sp in s_idx: fig.add_trace(go.Scatter(x=t_sim, y=y_sol[:, s_idx[sp]], mode="lines", name=f"[{sp}]", line=dict(color=SPECIES_COLORS.get(sp, "#666")), legend="legend1"), row=1, col=1)
for sp in[s for s in s_list if s in cats]:
if sp in s_idx: fig.add_trace(go.Scatter(x=t_sim, y=y_sol[:, s_idx[sp]], mode="lines", name=f"[{sp}]", line=dict(color=SPECIES_COLORS.get(sp, "#666")), legend="legend2"), row=1, col=2)
fig.update_layout(title=dict(text=f"Species Evolution: {exp_name}", font=dict(size=14)), height=450, template="plotly_white", margin=dict(l=50, r=20, t=50, b=50))
return fig
def create_interactive_rate_plots_optimized(network: dict, params: dict, all_exp_data: Dict[str, dict], selected_curves: Optional[Dict[str, List[str]]] = None) -> go.Figure:
groups = {"c_Ru": {}, "c_S2O8": {}, "irradiance": {}}
for exp_name, exp_data in all_exp_data.items():
meta = exp_data["metadata"]
groups["c_Ru"].setdefault(meta.get("c_Ru", 0), []).append((exp_name, exp_data))
groups["c_S2O8"].setdefault(meta.get("c_S2O8", 0),[]).append((exp_name, exp_data))
groups["irradiance"].setdefault(meta.get("irradiance", 1000),[]).append((exp_name, exp_data))
fig = make_subplots(rows=2, cols=2, subplot_titles=("A: [Ru(bpy)₃]Cl₂ Variation", "B: Na₂S₂O₈ Variation", "C: Irradiance Variation", ""), horizontal_spacing=0.10, vertical_spacing=0.12)
panel_configs =[("c_Ru", "µM", (1, 1), "legend1"), ("c_S2O8", "µM", (1, 2), "legend2"), ("irradiance", "Wm⁻²", (2, 1), "legend3")]
ScatterClass = go.Scattergl if USE_WEBGL else go.Scatter
network_json, params_json = json.dumps(network, default=str), json.dumps(params, default=str)
for param_key, unit, (row, col), legend_name in panel_configs:
param_groups = groups[param_key]
if not param_groups: continue
sorted_values = sorted(param_groups.keys())
n_colors = max(len(sorted_values), 2)
colors = [PARAM_COLORS[int(i * (len(PARAM_COLORS) - 1) / (n_colors - 1))] for i in range(n_colors)]
for i, param_val in enumerate(sorted_values):
exp_list = param_groups[param_val]
color = colors[i]
label = f"{param_val:.0f} {unit}"
visible = True
if selected_curves and param_key in selected_curves:
visible = label in selected_curves[param_key]
all_time_exp, all_rate_exp, all_time_model, all_rate_model = [], [], [],[]
for exp_name, exp_data in exp_list:
time_exp, oxygen_exp = np.array(exp_data["time"]), np.array(exp_data["oxygen"])
time_ds, oxygen_ds = downsample_data(time_exp, oxygen_exp, max_points=50)
rate_exp = np.gradient(oxygen_ds, time_ds)
all_time_exp.extend(time_ds.tolist() + [None])
all_rate_exp.extend(rate_exp.tolist() + [None])
y_sol, s_list, s_idx, t_sim = simulate_species_evolution_cached(
network_json, params_json, json.dumps(exp_data["metadata"], default=str),
float(time_exp.min()), float(time_exp.max()), n_points=50
)
y_sol, t_sim = np.array(y_sol), np.array(t_sim)
o2_pred = y_sol[:, s_idx["O2"]] if "O2" in s_idx else np.zeros_like(t_sim)
rate_pred = np.gradient(o2_pred, t_sim)
all_time_model.extend(t_sim.tolist() + [None])
all_rate_model.extend(rate_pred.tolist() + [None])
fig.add_trace(ScatterClass(x=all_time_exp, y=all_rate_exp, mode="markers", name=f"{label}", marker=dict(color=color, size=4, opacity=0.6), legendgroup=f"{param_key}_{param_val}", showlegend=True, visible=visible, legend=legend_name), row=row, col=col)
fig.add_trace(go.Scatter(x=all_time_model, y=all_rate_model, mode="lines", name=f"{label} (fit)", line=dict(color=color, width=2), legendgroup=f"{param_key}_{param_val}", showlegend=False, visible=visible, connectgaps=False, legend=legend_name), row=row, col=col)
for r, c in[(1, 1), (1, 2), (2, 1)]: fig.add_hline(y=0, line_dash="dash", line_color="gray", opacity=0.5, row=r, col=c)
fig.update_layout(height=700, template="plotly_white", margin=dict(l=70, r=30, t=60, b=60))
return fig
# =============================================================================
# Reading Results
# =============================================================================
def _find_best_phenomenological_result(output_dir: str):
base = Path(output_dir)
result_files = glob.glob(f"{base.as_posix()}/phenomenologic_result.json.*")
best_score, best_data, best_image = -1.0, None, None
for filepath in result_files:
ts = re.search(r"phenomenologic_result\.json\.(\d+)$", filepath)
if ts:
try:
with open(filepath, "r") as f: data = json.load(f)
score = data.get("phenomenological_trends", {}).get("overall_score", -1)
if score > best_score:
best_score, best_data = score, data
cand_img = base / f"phenomenological_trends_{ts.group(1)}.png"
if cand_img.exists(): best_image = cand_img
except: pass
return best_data, best_image
def format_reaction_for_display(reaction: dict) -> dict:
formatted = {
"Equation": reaction.get("equation", "N/A"),
"Type": reaction.get("type", "N/A"),
"Description": reaction.get("description", "N/A"),
}
if reaction.get("type") == "light":
formatted["Fitted Parameter"] = f"QY = {reaction.get('fitted_quantum_yield', 0):.4f}"
else:
formatted["Fitted Parameter"] = f"k = {reaction.get('fitted_k', 0):.4e}"
return formatted
# =============================================================================
# UI
# =============================================================================
def render_login_screen():
st.markdown("<div style='text-align:center; padding: 2rem;'><h2>👤 Who are you?</h2><p style='color:#888;'>Enter your name to start the blind evaluation.</p></div>", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
name = st.text_input("Your name", placeholder="e.g., Dr. Smith").strip()
if st.button("🔓 Start Evaluating", use_container_width=True, type="primary"):
if name: return name
st.error("Please enter your name.")
return None
def render_curve_selector(all_exp_data: Dict[str, dict], run_id: str) -> Dict[str, List[str]]:
st.markdown("##### 🎛️ Curve Selection")
groups = {"c_Ru": set(), "c_S2O8": set(), "irradiance": set()}
for exp_data in all_exp_data.values():
meta = exp_data["metadata"]
groups["c_Ru"].add(f"{meta.get('c_Ru', 0):.0f} µM")
groups["c_S2O8"].add(f"{meta.get('c_S2O8', 0):.0f} µM")
groups["irradiance"].add(f"{meta.get('irradiance', 1000):.0f} Wm⁻²")
selected = {}
col1, col2, col3 = st.columns(3)
with col1:
st.caption("**[Ru(bpy)₃]Cl₂**")
ru_options = sorted(groups["c_Ru"], key=lambda x: float(x.split()[0]))
selected["c_Ru"] = st.multiselect("Ru", options=ru_options, default=ru_options, label_visibility="collapsed", key=f"ru_{run_id}")
with col2:
st.caption("**Na₂S₂O₈**")
s2o8_options = sorted(groups["c_S2O8"], key=lambda x: float(x.split()[0]))
selected["c_S2O8"] = st.multiselect("S2O8", options=s2o8_options, default=s2o8_options, label_visibility="collapsed", key=f"s2o8_{run_id}")
with col3:
st.caption("**Irradiance**")
irr_options = sorted(groups["irradiance"], key=lambda x: float(x.split()[0]))
selected["irradiance"] = st.multiselect("Irr", options=irr_options, default=irr_options, label_visibility="collapsed", key=f"irr_{run_id}")
return selected
def render_candidate_view(run_info: dict, rating_data: dict, all_exp_data: dict, username: str, sha: Optional[str]):
candidate_id = run_info["candidate_id"]
st.header(f"🔍 Inspecting: {candidate_id}")
st.caption("Review the reaction network parameters and fits below, then submit your rating.")
best_data, best_image = _find_best_phenomenological_result(run_info["path"])
if not best_data:
st.warning(f"⚠️ Simulation data missing for {candidate_id}. It might not have converged.")
else:
# 1. Reactions Table
reactions = best_data.get("network", {}).get("reactions",[])
if reactions:
df = pd.DataFrame([{**format_reaction_for_display(rxn), "#": i+1} for i, rxn in enumerate(reactions)])
df = df[["#", "Equation", "Type", "Fitted Parameter", "Description"]]
st.dataframe(df.style.map(lambda v: "background-color: #fff3cd" if v == "light" else "", subset=["Type"]), hide_index=True, use_container_width=True)
# 2. Image & Plots
col_plot, col_img = st.columns([3, 2])
with col_img:
if best_image:
st.image(str(best_image), use_container_width=True)
else:
st.info("No phenomenological trend image available.")
with col_plot:
params = best_data.get("phenomenological_trends", {}).get("global_params", {})
for i, rxn in enumerate(reactions):
if rxn.get("type") == "light" and "fitted_quantum_yield" in rxn: params[f"qy_{i}"] = rxn["fitted_quantum_yield"]
elif "fitted_k" in rxn: params[f"k_{i}"] = rxn["fitted_k"]
# Curve selector for rate plots
selected_curves = render_curve_selector(all_exp_data, candidate_id)
plot_tabs = st.tabs(["📈 Concentration vs Time", "⚡ Rate vs Time"])
with plot_tabs[0]:
rep_exp_data = list(all_exp_data.values())[0] if all_exp_data else create_synthetic_dataset()["exp_Ru_50"]
try:
fig_conc = create_interactive_concentration_plot(best_data["network"], params, rep_exp_data, "Representative Data")
st.plotly_chart(fig_conc, use_container_width=True)
except Exception as e:
st.error(f"Concentration plot error: {e}")
with plot_tabs[1]:
try:
fig_rate = create_interactive_rate_plots_optimized(best_data["network"], params, all_exp_data, selected_curves)
st.plotly_chart(fig_rate, use_container_width=True)
except Exception as e:
st.error(f"Rate plot error: {e}")
# Rating Widget
st.markdown("---")
st.subheader("📝 Your Rating")
current_rating = rating_data.get(candidate_id, {})
with st.form(key=f"form_{candidate_id}"):
score = st.slider("Rate the chemical validity and fit quality (1 = Poor, 10 = Excellent)", 1, 10, current_rating.get("score", 5))
comments = st.text_area("Observations (Optional)", value=current_rating.get("comments", ""), height=100)
if st.form_submit_button("💾 Save Rating", type="primary"):
rating_data[candidate_id] = {
"score": score,
"comments": comments,
"true_round": run_info["round"],
"true_type": run_info["type"],
"true_path": run_info["path"],
"timestamp": datetime.now().isoformat()
}
if save_user_ratings(username, rating_data, sha):
st.success("✅ Saved!")
st.rerun()
def render_admin_view():
st.title("📊 Admin Panel: Round 1 vs Round 2 Comparison")
url = f"{GITHUB_API}/repos/{_github_repo()}/contents/{RATINGS_ROOT}"
resp = requests.get(url, headers=_github_headers(), params={"ref": _github_branch()})
if resp.status_code != 200:
st.info("No rating files found or GitHub misconfigured.")
return
all_ratings = []
for item in resp.json():
if item["name"].endswith(".json"):
data, _ = _load_json_from_github(item["path"])
for cand_id, r_info in data.items():
r_info["User"] = item["name"].replace(".json", "")
all_ratings.append(r_info)
if not all_ratings:
st.info("No ratings collected yet.")
return
df = pd.DataFrame(all_ratings)
st.subheader("Aggregate Scores Head-to-Head")
try:
pivot_df = df.pivot_table(
index="true_type",
columns="true_round",
values="score",
aggfunc=["mean", "count"]
)
pivot_df.columns = [f"{col[1]} ({col[0]})" for col in pivot_df.columns]
r1_col = "Round 1 (Before FB) (mean)"
r2_col = "Round 2 (After FB) (mean)"
if r1_col in pivot_df.columns and r2_col in pivot_df.columns:
pivot_df["Improvement (Avg)"] = pivot_df[r2_col] - pivot_df[r1_col]
st.dataframe(pivot_df.style.format(precision=2), use_container_width=True)
except Exception as e:
st.warning(f"Not enough data to construct comparison table: {e}")
st.subheader("Raw Data")
st.dataframe(df, use_container_width=True)
st.download_button("📥 Download CSV", df.to_csv(index=False).encode('utf-8'), "blind_ratings_export.csv")
# =============================================================================
# Main Layout
# =============================================================================
def main():
st.set_page_config(page_title="Blind Kinetic Rating", page_icon="⚖️", layout="wide")
query_params = st.query_params
if "admin" in query_params:
render_admin_view()
return
if "current_user" not in st.session_state:
chosen = render_login_screen()
if chosen:
st.session_state["current_user"] = chosen
st.rerun()
st.stop()
username = st.session_state["current_user"]
shuffled_runs = get_shuffled_runs(username)
rating_data, sha = load_user_ratings(username)
st.sidebar.markdown(f"### 👤 {username}")
if st.sidebar.button("🔄 Switch User"):
del st.session_state["current_user"]
st.rerun()
st.sidebar.markdown("---")
st.sidebar.subheader("Candidates")
completed = len(rating_data.keys())
st.sidebar.progress(completed / 12.0, text=f"{completed}/12 Evaluated")
options =[]
for run in shuffled_runs:
cid = run["candidate_id"]
icon = "✅" if cid in rating_data else "⭕"
options.append(f"{icon} {cid}")
selected_option = st.sidebar.radio("Select Candidate to Evaluate", options)
selected_idx = options.index(selected_option)
selected_run = shuffled_runs[selected_idx]
# Load full synthetic dataset for rate plots
all_exp_data = create_synthetic_dataset()
render_candidate_view(selected_run, rating_data, all_exp_data, username, sha)
if __name__ == "__main__":
main()