Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -227,16 +227,23 @@ def plot_properties_dashboard(df: pd.DataFrame):
|
|
| 227 |
if df.empty:
|
| 228 |
return None, "Cannot plot: No analysis data."
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
plt.style.use('dark_background')
|
| 231 |
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
| 232 |
fig.suptitle("Molecular Properties Analysis", fontsize=16)
|
| 233 |
|
|
|
|
| 234 |
fig.patch.set_facecolor('none')
|
| 235 |
for ax_row in axes:
|
| 236 |
for ax in ax_row:
|
| 237 |
ax.set_facecolor('none')
|
| 238 |
|
| 239 |
-
|
|
|
|
|
|
|
| 240 |
axes[0,0].set_title('Molecular Weight vs LogP')
|
| 241 |
axes[0,0].set_xlabel('Molecular Weight (Da)')
|
| 242 |
axes[0,0].set_ylabel('LogP')
|
|
@@ -244,7 +251,8 @@ def plot_properties_dashboard(df: pd.DataFrame):
|
|
| 244 |
axes[0,0].axhline(5, color='r', linestyle='--', alpha=0.6, label='LogP < 5')
|
| 245 |
axes[0,0].legend()
|
| 246 |
|
| 247 |
-
|
|
|
|
| 248 |
axes[0,1].set_title('Hydrogen Bonding Properties')
|
| 249 |
axes[0,1].set_xlabel('Hydrogen Bond Donors')
|
| 250 |
axes[0,1].set_ylabel('Hydrogen Bond Acceptors')
|
|
@@ -252,11 +260,13 @@ def plot_properties_dashboard(df: pd.DataFrame):
|
|
| 252 |
axes[0,1].axhline(10, color='r', linestyle='--', alpha=0.6, label='HBA < 10')
|
| 253 |
axes[0,1].legend()
|
| 254 |
|
| 255 |
-
|
|
|
|
| 256 |
axes[1,0].set_title('TPSA vs Flexibility')
|
| 257 |
axes[1,0].set_xlabel('Topological Polar Surface Area (Ų)')
|
| 258 |
axes[1,0].set_ylabel('Rotatable Bonds')
|
| 259 |
|
|
|
|
| 260 |
drug_like_counts = df['Drug_Like'].value_counts()
|
| 261 |
labels = ['Drug-like' if i else 'Non-drug-like' for i in drug_like_counts.index]
|
| 262 |
colors = ['green' if i else 'red' for i in drug_like_counts.index]
|
|
@@ -264,8 +274,10 @@ def plot_properties_dashboard(df: pd.DataFrame):
|
|
| 264 |
axes[1,1].set_title('Drug-likeness Distribution')
|
| 265 |
|
| 266 |
plt.tight_layout(rect=[0, 0, 1, 0.96])
|
|
|
|
|
|
|
| 267 |
return fig, "✅ Generated properties dashboard."
|
| 268 |
-
|
| 269 |
# ===== Phase 2 Functions =====
|
| 270 |
def get_phase2_molecules():
|
| 271 |
return {
|
|
@@ -426,9 +438,14 @@ def simulate_rwd_analysis(adverse_event_text):
|
|
| 426 |
event_counts = pd.Series(all_events).value_counts()
|
| 427 |
log = f"✅ Analyzed {len(all_events)} simulated adverse event reports.\n"
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
plt.style.use('dark_background')
|
| 430 |
fig_bar, ax_bar = plt.subplots(figsize=(10, 6))
|
| 431 |
|
|
|
|
| 432 |
fig_bar.patch.set_facecolor('none')
|
| 433 |
ax_bar.set_facecolor('none')
|
| 434 |
|
|
@@ -440,7 +457,7 @@ def simulate_rwd_analysis(adverse_event_text):
|
|
| 440 |
plt.tight_layout()
|
| 441 |
|
| 442 |
return event_counts.reset_index().rename(columns={'index': 'Event', 0: 'Count'}), fig_bar, log
|
| 443 |
-
|
| 444 |
def get_ethical_framework():
|
| 445 |
framework = {'Pillar': ['1. Beneficence & Non-Maleficence', '2. Justice & Fairness', '3. Transparency & Explainability', '4. Accountability & Governance'],
|
| 446 |
'Description': ['AI should help patients and do no harm. Requires rigorous validation and safety monitoring.',
|
|
@@ -537,9 +554,12 @@ with tab1:
|
|
| 537 |
st.subheader("Calculated Molecular Properties")
|
| 538 |
st.dataframe(res1.get('props_df', pd.DataFrame()), use_container_width=True)
|
| 539 |
with p1_tabs[2]:
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
|
|
|
|
|
|
|
|
|
| 543 |
|
| 544 |
# ===== TAB 2: LEAD GENERATION & OPTIMIZATION =====
|
| 545 |
with tab2:
|
|
@@ -682,9 +702,12 @@ with tab4:
|
|
| 682 |
res4 = st.session_state.results_p4
|
| 683 |
p4_tabs = st.tabs(["Pharmacovigilance Analysis", "Regulatory & Ethical Frameworks"])
|
| 684 |
with p4_tabs[0]:
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
|
|
|
|
|
|
|
|
|
| 688 |
|
| 689 |
with p4_tabs[1]:
|
| 690 |
col1, col2 = st.columns(2)
|
|
|
|
| 227 |
if df.empty:
|
| 228 |
return None, "Cannot plot: No analysis data."
|
| 229 |
|
| 230 |
+
# Clear any existing plots to prevent interference
|
| 231 |
+
plt.clf()
|
| 232 |
+
plt.close('all')
|
| 233 |
+
|
| 234 |
plt.style.use('dark_background')
|
| 235 |
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
| 236 |
fig.suptitle("Molecular Properties Analysis", fontsize=16)
|
| 237 |
|
| 238 |
+
# Set transparent backgrounds
|
| 239 |
fig.patch.set_facecolor('none')
|
| 240 |
for ax_row in axes:
|
| 241 |
for ax in ax_row:
|
| 242 |
ax.set_facecolor('none')
|
| 243 |
|
| 244 |
+
# Plot 1: MW vs LogP
|
| 245 |
+
drug_like_colors = df['Drug_Like'].map({True: 'green', False: 'red'})
|
| 246 |
+
axes[0,0].scatter(df['MW'], df['LogP'], c=drug_like_colors, s=80, alpha=0.7)
|
| 247 |
axes[0,0].set_title('Molecular Weight vs LogP')
|
| 248 |
axes[0,0].set_xlabel('Molecular Weight (Da)')
|
| 249 |
axes[0,0].set_ylabel('LogP')
|
|
|
|
| 251 |
axes[0,0].axhline(5, color='r', linestyle='--', alpha=0.6, label='LogP < 5')
|
| 252 |
axes[0,0].legend()
|
| 253 |
|
| 254 |
+
# Plot 2: Hydrogen bonding
|
| 255 |
+
axes[0,1].scatter(df['HBD'], df['HBA'], c=drug_like_colors, s=80, alpha=0.7)
|
| 256 |
axes[0,1].set_title('Hydrogen Bonding Properties')
|
| 257 |
axes[0,1].set_xlabel('Hydrogen Bond Donors')
|
| 258 |
axes[0,1].set_ylabel('Hydrogen Bond Acceptors')
|
|
|
|
| 260 |
axes[0,1].axhline(10, color='r', linestyle='--', alpha=0.6, label='HBA < 10')
|
| 261 |
axes[0,1].legend()
|
| 262 |
|
| 263 |
+
# Plot 3: TPSA vs Flexibility
|
| 264 |
+
axes[1,0].scatter(df['TPSA'], df['RotBonds'], c=drug_like_colors, s=80, alpha=0.7)
|
| 265 |
axes[1,0].set_title('TPSA vs Flexibility')
|
| 266 |
axes[1,0].set_xlabel('Topological Polar Surface Area (Ų)')
|
| 267 |
axes[1,0].set_ylabel('Rotatable Bonds')
|
| 268 |
|
| 269 |
+
# Plot 4: Drug-likeness pie chart
|
| 270 |
drug_like_counts = df['Drug_Like'].value_counts()
|
| 271 |
labels = ['Drug-like' if i else 'Non-drug-like' for i in drug_like_counts.index]
|
| 272 |
colors = ['green' if i else 'red' for i in drug_like_counts.index]
|
|
|
|
| 274 |
axes[1,1].set_title('Drug-likeness Distribution')
|
| 275 |
|
| 276 |
plt.tight_layout(rect=[0, 0, 1, 0.96])
|
| 277 |
+
|
| 278 |
+
# Return the figure without calling plt.show()
|
| 279 |
return fig, "✅ Generated properties dashboard."
|
| 280 |
+
|
| 281 |
# ===== Phase 2 Functions =====
|
| 282 |
def get_phase2_molecules():
|
| 283 |
return {
|
|
|
|
| 438 |
event_counts = pd.Series(all_events).value_counts()
|
| 439 |
log = f"✅ Analyzed {len(all_events)} simulated adverse event reports.\n"
|
| 440 |
|
| 441 |
+
# Clear any existing plots
|
| 442 |
+
plt.clf()
|
| 443 |
+
plt.close('all')
|
| 444 |
+
|
| 445 |
plt.style.use('dark_background')
|
| 446 |
fig_bar, ax_bar = plt.subplots(figsize=(10, 6))
|
| 447 |
|
| 448 |
+
# Set transparent backgrounds
|
| 449 |
fig_bar.patch.set_facecolor('none')
|
| 450 |
ax_bar.set_facecolor('none')
|
| 451 |
|
|
|
|
| 457 |
plt.tight_layout()
|
| 458 |
|
| 459 |
return event_counts.reset_index().rename(columns={'index': 'Event', 0: 'Count'}), fig_bar, log
|
| 460 |
+
|
| 461 |
def get_ethical_framework():
|
| 462 |
framework = {'Pillar': ['1. Beneficence & Non-Maleficence', '2. Justice & Fairness', '3. Transparency & Explainability', '4. Accountability & Governance'],
|
| 463 |
'Description': ['AI should help patients and do no harm. Requires rigorous validation and safety monitoring.',
|
|
|
|
| 554 |
st.subheader("Calculated Molecular Properties")
|
| 555 |
st.dataframe(res1.get('props_df', pd.DataFrame()), use_container_width=True)
|
| 556 |
with p1_tabs[2]:
|
| 557 |
+
st.subheader("Molecular Properties Dashboard")
|
| 558 |
+
if res1.get('props_plot'):
|
| 559 |
+
# Display the plot and then close it to prevent memory leaks
|
| 560 |
+
st.pyplot(res1['props_plot'], clear_figure=True)
|
| 561 |
+
# Explicitly close the figure after displaying
|
| 562 |
+
plt.close(res1['props_plot'])
|
| 563 |
|
| 564 |
# ===== TAB 2: LEAD GENERATION & OPTIMIZATION =====
|
| 565 |
with tab2:
|
|
|
|
| 702 |
res4 = st.session_state.results_p4
|
| 703 |
p4_tabs = st.tabs(["Pharmacovigilance Analysis", "Regulatory & Ethical Frameworks"])
|
| 704 |
with p4_tabs[0]:
|
| 705 |
+
st.subheader("Simulated Adverse Event Analysis")
|
| 706 |
+
if 'plot_bar' in res4 and res4['plot_bar'] is not None:
|
| 707 |
+
st.pyplot(res4['plot_bar'], clear_figure=True)
|
| 708 |
+
# Explicitly close the figure after displaying
|
| 709 |
+
plt.close(res4['plot_bar'])
|
| 710 |
+
st.dataframe(res4['rwd_df'], use_container_width=True)
|
| 711 |
|
| 712 |
with p4_tabs[1]:
|
| 713 |
col1, col2 = st.columns(2)
|