Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,7 @@ from gradio.themes.base import Base
|
|
| 25 |
from gradio.themes.utils import colors, fonts, sizes
|
| 26 |
from huggingface_hub import hf_hub_download
|
| 27 |
import time
|
|
|
|
| 28 |
|
| 29 |
class Seafoam(Base):
|
| 30 |
def __init__(
|
|
@@ -252,40 +253,48 @@ def retrieve_similarity_scores( table_name, target_mass,collision_energy, ms2_em
|
|
| 252 |
filtered_smiles = cur.fetchall()
|
| 253 |
similarity_scores = []
|
| 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 |
weighted_similarity_scores.sort(key=lambda x: x[1], reverse=True)
|
| 291 |
|
|
|
|
| 25 |
from gradio.themes.utils import colors, fonts, sizes
|
| 26 |
from huggingface_hub import hf_hub_download
|
| 27 |
import time
|
| 28 |
+
import concurrent.futures
|
| 29 |
|
| 30 |
class Seafoam(Base):
|
| 31 |
def __init__(
|
|
|
|
| 253 |
filtered_smiles = cur.fetchall()
|
| 254 |
similarity_scores = []
|
| 255 |
|
| 256 |
+
query = f"""
|
| 257 |
+
SELECT SMILES, low_energy_embedding, median_energy_embedding, high_energy_embedding
|
| 258 |
+
FROM {table_name}
|
| 259 |
+
WHERE SMILES IN ({','.join(['?']*len(filtered_smiles))})
|
| 260 |
+
"""
|
| 261 |
+
cur.execute(query, tuple(s[0] for s in filtered_smiles))
|
| 262 |
+
rows = cur.fetchall()
|
| 263 |
+
|
| 264 |
+
def decode_row(row):
|
| 265 |
+
return (
|
| 266 |
+
row[0], # SMILES
|
| 267 |
+
np.array(pickle.loads(row[1]), dtype=np.float32),
|
| 268 |
+
np.array(pickle.loads(row[2]), dtype=np.float32),
|
| 269 |
+
np.array(pickle.loads(row[3]), dtype=np.float32),
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 273 |
+
results = list(executor.map(decode_row, rows))
|
| 274 |
+
|
| 275 |
+
ms2_embedding_low_np = ms2_embedding_low.numpy()
|
| 276 |
+
ms2_embedding_median_np = ms2_embedding_median.numpy()
|
| 277 |
+
ms2_embedding_high_np = ms2_embedding_high.numpy()
|
| 278 |
+
|
| 279 |
+
similarity_scores = [
|
| 280 |
+
(
|
| 281 |
+
smile,
|
| 282 |
+
np.dot(ms2_embedding_low_np, low_embedding),
|
| 283 |
+
np.dot(ms2_embedding_median_np, median_embedding),
|
| 284 |
+
np.dot(ms2_embedding_high_np, high_embedding),
|
| 285 |
+
)
|
| 286 |
+
for smile, low_embedding, median_embedding, high_embedding in results
|
| 287 |
+
]
|
| 288 |
+
|
| 289 |
+
collision_weights = np.array([
|
| 290 |
+
[0.4, 0.3, 0.3] if collision_energy <= 15 else
|
| 291 |
+
[0.3, 0.4, 0.3] if collision_energy <= 25 else
|
| 292 |
+
[0.2, 0.3, 0.5]
|
| 293 |
+
])
|
| 294 |
+
|
| 295 |
+
similarity_array = np.array([[low, median, high] for _, low, median, high in similarity_scores])
|
| 296 |
+
weighted_similarities = similarity_array @ collision_weights.T
|
| 297 |
+
weighted_similarity_scores = [(smile, weighted) for (smile, _low, _med, _high), weighted in zip(similarity_scores, weighted_similarities)]
|
| 298 |
|
| 299 |
weighted_similarity_scores.sort(key=lambda x: x[1], reverse=True)
|
| 300 |
|