-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_utils.py
executable file
·143 lines (127 loc) · 4.89 KB
/
plot_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import numpy as np
import matplotlib.pyplot as plt
from rdkit import Chem
from rdkit.Chem import Draw
def create_figs(results):
#Create figs
figs = []
for i,r in enumerate(results):
figs_ = []
for j,rr in enumerate(r):
smile = rr["smile"]
weights = rr["weights"]
f = draw_chem_activations(smile=smile, weights=weights, colorbar=False,
radius=0.03, colormap="Blues",
vmax=1.0, vmin=0.0)
figs_.append(f)
figs.append(figs_)
return figs
def create_im_arrs(figs):
#Create image arrays from figures
ims_arr = []
for i,f in enumerate(figs):
ims_arr_ = []
for j,ff in enumerate(f):
X = np.array(ff.canvas.renderer._renderer)
ims_arr_.append(X)
ims_arr.append(ims_arr_)
return ims_arr
def draw_atom_disks(mol, weights, radius=0.05, step=0.001):
"""
Draw disks of fixed radius around each atom's coordinate.
`weights` controls the color of each disk.
"""
x = np.arange(0, 1, step)
y = np.arange(0, 1, step)
X, Y = np.meshgrid(x, y)
Z = np.zeros(X.shape)
for i, (c_x, c_y) in mol._atomPs.items():
base = (X - c_x) ** 2 + (Y - c_y) ** 2
circle_mask = (base < radius**2)
circle = circle_mask.astype('float') * weights[i]
Z += circle
return X, Y, Z
def draw_chem_activations(smile, weights, colorbar=False, colormap = 'RdBu',
step=0.001, size=(250, 250), radius=0.025,
coord_scale=1.5, title=None, vmax=1.0, vmin=-1.0):
"""
Draw scalar activations on each
"""
mol = Chem.MolFromSmiles(smile)
cmap = plt.cm.get_cmap(colormap)
fig = Draw.MolToMPL(mol, coordScale=coord_scale, size=size, **{}, )
ax = fig.axes[0]
x, y, z = draw_atom_disks(mol, radius=radius, weights=weights, step=step)
ax.imshow(z, cmap=cmap, interpolation='bilinear', origin='lower',
extent=(0, 1, 0, 1), vmin=vmin, vmax=vmax)
ax.set_axis_off()
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax))
sm._A = []
if colorbar:
plt.colorbar(sm, fraction=0.035, pad=0.04)
if title:
plt.title(title)
return fig
def plot_image_grid(grid,
row_labels_left,
row_labels_right,
col_labels,
super_col_labels=None,
file_name=None,
dpi=224,
c=10,
fontsize=24,
col_rotation=22.5):
"""
Forked from https://github.com/albermax/innvestigate/blob/master/examples/utils.py
"""
n_rows = len(grid)
n_cols = len(grid[0])
plt.clf()
plt.rc("font", family="sans-serif", size=fontsize)
f = plt.figure(figsize = (c*n_cols, c*n_rows))
for r in range(n_rows):
for c in range(n_cols):
ax = plt.subplot2grid(shape=[n_rows, n_cols], loc=[r,c])
ax.imshow(grid[r][c], interpolation='none')
ax.set_xticks([])
ax.set_yticks([])
if not r: #column labels
if col_labels != []:
ax.set_title(col_labels[c],
rotation=col_rotation,
horizontalalignment='left',
verticalalignment='bottom')
if super_col_labels != [] and c % 2 == 0:
label = super_col_labels[c // 2]
x_adjust = len(label) / 100
ax.text(x = 1 - x_adjust,
y = 1.2,
s = label,
transform=ax.transAxes,
fontdict={"fontsize": 50, "weight": 10})
if not c: #row labels
if row_labels_left != []:
txt_left = [l+'\n' for l in row_labels_left[r]]
ax.set_ylabel(''.join(txt_left),
rotation=0,
verticalalignment='center',
horizontalalignment='right',
)
if c == n_cols-1:
if row_labels_right != []:
txt_right = [l+'\n' for l in row_labels_right[r]]
ax2 = ax.twinx()
ax2.set_xticks([])
ax2.set_yticks([])
ax2.set_ylabel(''.join(txt_right),
rotation=0,
verticalalignment='center',
horizontalalignment='left'
)
if not file_name:
plt.show()
else:
print ('saving figure to {}'.format(file_name))
plt.savefig(file_name, orientation='landscape', dpi=dpi)