Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -491,48 +491,57 @@ def check_constraints(mol, motifs, limit_aromatic, max_mw):
|
|
| 491 |
return True
|
| 492 |
|
| 493 |
def run_sensitivity_analysis(raw_df, t_s, t_t, t_d):
|
| 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 |
def generate_exports(df):
|
| 538 |
if df is None or df.empty:
|
|
|
|
| 491 |
return True
|
| 492 |
|
| 493 |
def run_sensitivity_analysis(raw_df, t_s, t_t, t_d):
|
| 494 |
+
# 1. Handle empty or None state safely
|
| 495 |
+
if raw_df is None or (isinstance(raw_df, pd.DataFrame) and raw_df.empty):
|
| 496 |
+
return None, "No data to analyze. Run the 'Design Pipeline' first.", "### <span style='color:grey'>0% Pass Rate</span>"
|
| 497 |
|
| 498 |
+
try:
|
| 499 |
+
processed_df = raw_df.copy()
|
| 500 |
+
|
| 501 |
+
# 2. Verify raw numeric columns exist (preventing KeyErrors)
|
| 502 |
+
required_cols = ["_s", "_t", "Degradability_raw"]
|
| 503 |
+
if not all(col in processed_df.columns for col in required_cols):
|
| 504 |
+
return None, "Required numeric data missing for analysis.", ""
|
| 505 |
+
|
| 506 |
+
# 3. Re-classification logic based on NEW thresholds
|
| 507 |
+
def reclassify(row):
|
| 508 |
+
s, t, d = row["_s"], row["_t"], row["Degradability_raw"]
|
| 509 |
+
failed = []
|
| 510 |
+
if s < t_s: failed.append("Strength")
|
| 511 |
+
if t < t_t: failed.append("Tg")
|
| 512 |
+
if d < t_d: failed.append("Degradability")
|
| 513 |
+
|
| 514 |
+
if not failed: return "Excellent β
"
|
| 515 |
+
if len(failed) <= 2: return f"Review (Failed {', '.join(failed)}) β οΈ"
|
| 516 |
+
return "Bad β"
|
| 517 |
+
|
| 518 |
+
processed_df["Classification"] = processed_df.apply(reclassify, axis=1)
|
| 519 |
+
|
| 520 |
+
# 4. Calculate Market Readiness
|
| 521 |
+
total = len(processed_df)
|
| 522 |
+
passed = len(processed_df[processed_df["Classification"] == "Excellent β
"])
|
| 523 |
+
pass_pct = (passed / total) * 100 if total > 0 else 0
|
| 524 |
+
|
| 525 |
+
# 5. Update icons for the display view
|
| 526 |
+
def update_icons(row):
|
| 527 |
+
row["Strength (GPa)"] = f"{'π’' if row['_s'] >= t_s else 'π΄'} {row['_s']:.3f}"
|
| 528 |
+
row["Tg (Β°C)"] = f"{'π’' if row['_t'] >= t_t else 'π΄'} {row['_t']:.1f}"
|
| 529 |
+
row["Degradability"] = f"{'π’' if row['Degradability_raw'] >= t_d else 'π΄'} {row['Degradability_raw']:.2f}"
|
| 530 |
+
return row
|
| 531 |
+
|
| 532 |
+
display_df = processed_df.apply(update_icons, axis=1)
|
| 533 |
+
|
| 534 |
+
# 6. Formatting messages
|
| 535 |
+
badge_color = "green" if pass_pct > 25 else "orange" if pass_pct > 5 else "red"
|
| 536 |
+
annotated_data = f"### <span style='color:{badge_color}'>{pass_pct:.0f}% Pass Rate</span>"
|
| 537 |
+
status_msg = f"**{pass_pct:.1f}%** of candidates ({passed}/{total}) meet these adjusted industrial benchmarks."
|
| 538 |
+
|
| 539 |
+
# 7. Strictly filter out hidden columns (starting with _) for the UI
|
| 540 |
+
final_cols = [c for c in display_df.columns if not str(c).startswith('_') and c != "Degradability_raw"]
|
| 541 |
+
return display_df[final_cols], status_msg, annotated_data
|
| 542 |
+
|
| 543 |
+
except Exception as e:
|
| 544 |
+
return None, f"Analysis Error: {str(e)}", ""
|
| 545 |
|
| 546 |
def generate_exports(df):
|
| 547 |
if df is None or df.empty:
|