andreamena commited on
Commit
545d20d
Β·
verified Β·
1 Parent(s): 621a31b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -41
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
- if raw_df is None or raw_df.empty:
495
- return None, "No data to analyze", "" # Changed [] to ""
 
496
 
497
- processed_df = raw_df.copy()
498
-
499
- # Re-classification logic based on NEW thresholds
500
- def reclassify(row):
501
- s, t, d = row["_s"], row["_t"], row["Degradability_raw"]
502
- failed = []
503
- if s < t_s: failed.append("Strength")
504
- if t < t_t: failed.append("Tg")
505
- if d < t_d: failed.append("Degradability")
506
-
507
- if not failed: return "Excellent βœ…"
508
- if len(failed) <= 2: return f"Review (Failed {', '.join(failed)}) ⚠️"
509
- return "Bad ❌"
510
-
511
- processed_df["Classification"] = processed_df.apply(reclassify, axis=1)
512
-
513
- # Calculate Market Readiness
514
- total = len(processed_df)
515
- passed = len(processed_df[processed_df["Classification"] == "Excellent βœ…"])
516
- pass_pct = (passed / total) * 100 if total > 0 else 0
517
-
518
- # Update icons in the visible columns for the user
519
- def update_icons(row):
520
- row["Strength (GPa)"] = f"{'🟒' if row['_s'] >= t_s else 'πŸ”΄'} {row['_s']:.3f}"
521
- row["Tg (Β°C)"] = f"{'🟒' if row['_t'] >= t_t else 'πŸ”΄'} {row['_t']:.1f}"
522
- row["Degradability"] = f"{'🟒' if row['Degradability_raw'] >= t_d else 'πŸ”΄'} {row['Degradability_raw']:.2f}"
523
- return row
524
-
525
- display_df = processed_df.apply(update_icons, axis=1)
526
-
527
- status_msg = f"**{pass_pct:.1f}%** of candidates ({passed}/{total}) meet these industrial benchmarks."
528
- # Color-coded badge for Gradio AnnotatedText
529
- badge_color = "green" if pass_pct > 25 else "orange" if pass_pct > 5 else "red"
530
- annotated_data = f"### <span style='color:{badge_color}'>{pass_pct:.0f}% Pass Rate</span>"
531
- status_msg = f"**{pass_pct:.1f}%** of candidates ({passed}/{total}) meet these industrial benchmarks."
532
-
533
- # Drop hidden columns before returning to the UI
534
- final_cols = [c for c in display_df.columns if not c.startswith('_') and c != "Degradability_raw"]
535
- return display_df[final_cols], status_msg, annotated_data
 
 
 
 
 
 
 
 
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: