Spaces:
Sleeping
Sleeping
fix: replace SimilarityMaps with manual atom highlight visualization
Browse files- explain/visualizer.py +20 -11
explain/visualizer.py
CHANGED
|
@@ -2,7 +2,7 @@ import io
|
|
| 2 |
import base64
|
| 3 |
import numpy as np
|
| 4 |
from rdkit import Chem
|
| 5 |
-
from rdkit.Chem
|
| 6 |
import matplotlib
|
| 7 |
matplotlib.use("Agg")
|
| 8 |
import matplotlib.pyplot as plt
|
|
@@ -19,20 +19,29 @@ def generate_similarity_map(smiles: str, attributions: list, target_assay: str)
|
|
| 19 |
f"but {len(attributions)} attributions provided"
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
mol,
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
)
|
| 30 |
-
ax.set_title(f"SHAP Attributions — {target_assay}", fontsize=14, fontweight="bold")
|
| 31 |
-
plt.tight_layout()
|
| 32 |
|
| 33 |
buf = io.BytesIO()
|
| 34 |
-
|
| 35 |
-
plt.close(fig)
|
| 36 |
buf.seek(0)
|
| 37 |
|
| 38 |
return base64.b64encode(buf.read()).decode("utf-8")
|
|
|
|
| 2 |
import base64
|
| 3 |
import numpy as np
|
| 4 |
from rdkit import Chem
|
| 5 |
+
from rdkit.Chem import Draw, AllChem
|
| 6 |
import matplotlib
|
| 7 |
matplotlib.use("Agg")
|
| 8 |
import matplotlib.pyplot as plt
|
|
|
|
| 19 |
f"but {len(attributions)} attributions provided"
|
| 20 |
)
|
| 21 |
|
| 22 |
+
AllChem.Compute2DCoords(mol)
|
| 23 |
+
weights = np.array(attributions)
|
| 24 |
+
vmax = max(abs(weights).max(), 0.01)
|
| 25 |
+
|
| 26 |
+
highlight_colors = {}
|
| 27 |
+
for i, w in enumerate(weights):
|
| 28 |
+
if w > 0:
|
| 29 |
+
intensity = min(abs(w) / vmax, 1.0)
|
| 30 |
+
highlight_colors[i] = (1, 0, 0, intensity * 0.7)
|
| 31 |
+
else:
|
| 32 |
+
intensity = min(abs(w) / vmax, 1.0)
|
| 33 |
+
highlight_colors[i] = (0, 0, 1, intensity * 0.7)
|
| 34 |
+
|
| 35 |
+
img = Draw.MolToImage(
|
| 36 |
mol,
|
| 37 |
+
size=(600, 500),
|
| 38 |
+
highlightAtoms=list(range(mol.GetNumAtoms())),
|
| 39 |
+
highlightColors=highlight_colors,
|
| 40 |
+
kekulize=True,
|
| 41 |
)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
buf = io.BytesIO()
|
| 44 |
+
img.save(buf, format="png")
|
|
|
|
| 45 |
buf.seek(0)
|
| 46 |
|
| 47 |
return base64.b64encode(buf.read()).decode("utf-8")
|