Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import torch
|
|
| 6 |
import torch.nn as nn
|
| 7 |
import torch.nn.functional as F
|
| 8 |
import xgboost as xgb
|
|
|
|
| 9 |
import plotly.express as px
|
| 10 |
import plotly.graph_objects as go
|
| 11 |
from rdkit import Chem, DataStructs
|
|
@@ -136,19 +137,38 @@ def generate_and_screen(num_to_gen):
|
|
| 136 |
|
| 137 |
mol = Chem.MolFromSmiles(generated.replace('*', 'C'))
|
| 138 |
if mol:
|
|
|
|
| 139 |
fp = rdFingerprintGenerator.GetMorganGenerator(radius=2).GetFingerprint(mol)
|
| 140 |
if any(DataStructs.TanimotoSimilarity(fp, prev_fp) > 0.7 for prev_fp in generated_fps): continue
|
| 141 |
|
| 142 |
feats = get_mff_features(generated)
|
| 143 |
-
s = np.expm1(strength_model.predict(feats.reshape(1, -1))[0]
|
| 144 |
d = degrad_model.predict(feats.reshape(1, -1))[0]
|
| 145 |
t = tg_model.predict(feats.reshape(1, -1))[0]
|
| 146 |
sa = sascorer.calculateScore(mol) if sascorer else 5.0
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
generated_fps.append(fp)
|
| 154 |
candidates.append({
|
|
@@ -164,12 +184,11 @@ def generate_and_screen(num_to_gen):
|
|
| 164 |
|
| 165 |
df = pd.DataFrame(candidates)
|
| 166 |
|
| 167 |
-
#
|
| 168 |
df = df.sort_values(by="_s")
|
| 169 |
-
# Simple Pareto calculation (running max of Y for sorted X)
|
| 170 |
pareto_x, pareto_y = [], []
|
| 171 |
current_max_y = -1
|
| 172 |
-
for i, row in df.
|
| 173 |
if row["_t"] > current_max_y:
|
| 174 |
pareto_x.append(row["_s"])
|
| 175 |
pareto_y.append(row["_t"])
|
|
|
|
| 6 |
import torch.nn as nn
|
| 7 |
import torch.nn.functional as F
|
| 8 |
import xgboost as xgb
|
| 9 |
+
import scipy.stats as stats
|
| 10 |
import plotly.express as px
|
| 11 |
import plotly.graph_objects as go
|
| 12 |
from rdkit import Chem, DataStructs
|
|
|
|
| 137 |
|
| 138 |
mol = Chem.MolFromSmiles(generated.replace('*', 'C'))
|
| 139 |
if mol:
|
| 140 |
+
# Diversity Check
|
| 141 |
fp = rdFingerprintGenerator.GetMorganGenerator(radius=2).GetFingerprint(mol)
|
| 142 |
if any(DataStructs.TanimotoSimilarity(fp, prev_fp) > 0.7 for prev_fp in generated_fps): continue
|
| 143 |
|
| 144 |
feats = get_mff_features(generated)
|
| 145 |
+
s = np.expm1(strength_model.predict(feats.reshape(1, -1)))[0]
|
| 146 |
d = degrad_model.predict(feats.reshape(1, -1))[0]
|
| 147 |
t = tg_model.predict(feats.reshape(1, -1))[0]
|
| 148 |
sa = sascorer.calculateScore(mol) if sascorer else 5.0
|
| 149 |
|
| 150 |
+
# --- DYNAMIC UNCERTAINTY (Points 2 & 4) ---
|
| 151 |
+
mol_wt = Descriptors.MolWt(mol)
|
| 152 |
+
noise = (mol_wt / 500.0)
|
| 153 |
+
s_err = s * (0.04 + (0.03 * noise))
|
| 154 |
+
t_err = 8.0 + (5.0 * noise)
|
| 155 |
+
d_err = 0.03 + (0.04 * noise)
|
| 156 |
+
|
| 157 |
+
# --- CONTINUOUS CONFIDENCE (Normal Distribution / Z-score) ---
|
| 158 |
+
def calc_prob(val, target, err, higher_is_better=True):
|
| 159 |
+
z = (val - target) / (err + 1e-6)
|
| 160 |
+
prob = stats.norm.cdf(z)
|
| 161 |
+
return prob if higher_is_better else (1 - prob)
|
| 162 |
+
|
| 163 |
+
p_s = calc_prob(s, T_STRENGTH, s_err)
|
| 164 |
+
p_t = calc_prob(t, T_TG, t_err)
|
| 165 |
+
p_d = calc_prob(d, T_DEG, d_err)
|
| 166 |
+
p_sa = calc_prob(sa, T_SA, 1.0, False)
|
| 167 |
+
|
| 168 |
+
conf_pct = ((p_s + p_t + p_d + p_sa) / 4) * 100
|
| 169 |
+
if conf_pct >= 80: status = "Excellent"
|
| 170 |
+
elif conf_pct >= 55: status = "Review"
|
| 171 |
+
else: status = "Bad"
|
| 172 |
|
| 173 |
generated_fps.append(fp)
|
| 174 |
candidates.append({
|
|
|
|
| 184 |
|
| 185 |
df = pd.DataFrame(candidates)
|
| 186 |
|
| 187 |
+
# Pareto Front Calculation & Fix
|
| 188 |
df = df.sort_values(by="_s")
|
|
|
|
| 189 |
pareto_x, pareto_y = [], []
|
| 190 |
current_max_y = -1
|
| 191 |
+
for i, row in df.iterrows():
|
| 192 |
if row["_t"] > current_max_y:
|
| 193 |
pareto_x.append(row["_s"])
|
| 194 |
pareto_y.append(row["_t"])
|