-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
263 lines (217 loc) · 9.26 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import torch
import numpy as np
from torch.autograd import Variable
import logging
import matplotlib.pyplot as plt
from rdkit.Chem.rdmolfiles import MolFromPDBBlock
from rdkit.Chem import AllChem
from rdkit import DataStructs
from PIL import Image
import heapq
import cv2 as cv
def coord2contour_img(coords, img_fn=None):
fig, ax = plt.subplots()
DPI = fig.get_dpi()
fig.set_size_inches(500.0/float(DPI), 500.0/float(DPI))
ax.scatter(coords[:,0], coords[:,1], c='blue', s=400)
ax.scatter(coords[:,0], coords[:,1], c='green', s=200)
ax.scatter(coords[:,0], coords[:,1], c='yellow', s=60)
ax.scatter(coords[:,0], coords[:,1], c='red', s=10)
ax.axis('off')
ax.axis('equal')
ax.set_xticks([])
ax.set_yticks([])
fig.canvas.draw()
X = np.array(fig.canvas.renderer.buffer_rgba())
plt.close()
im = Image.fromarray(X)
if img_fn is not None:
im.save(img_fn)
print("image saved to {}".format(img_fn))
# fig2 = plt.figure()
# ax2 = fig2.add_subplot(111, frameon=False)
# ax2.axis('off')
# ax2.axis('equal')
# ax2.imshow(X)
# plt.show()
return im
def cal_distance(p1, p2):
return np.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
def coord2feature(coord, n_features = 1024, cutoff = 5):
# Step 1: Convert atom coordinates to pdb string block
pdbStart = "CRYST1 0.000 0.000 0.000 90.00 90.00 90.00 P 1 1\n"
pdbContent = "ATOM ind C GRA X ind xcoord ycoord 96.500 0.00 0.00 SHT C\n"
pdbEnd = "END"
##############
block = ''+pdbStart
count = 0
for atom in coord:
pdbline = pdbContent
count += 1
x = atom[0]
y = atom[1]
block += pdbline.replace('ind',
str(count).rjust(3)).replace('xcoord',
"{:.3f}".format(x).rjust(6)).replace('ycoord',
"{:.3f}".format(y).rjust(6))
block += pdbEnd
# Step 2: Get Mol object from PDB Block
mol = MolFromPDBBlock(block)
# Step 3: Convert Mol object to Morgan Fingerprints
fp = AllChem.GetMorganFingerprintAsBitVect(mol, cutoff, useFeatures=True, nBits = n_features)
# Step 4: Convert Morgan Fingerprints to numpy array
array = np.zeros((1,))
DataStructs.ConvertToNumpyArray(fp, array)
array = array.reshape((1, -1))
return array
def running_mean(x, window_size):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[window_size:] - cumsum[:-window_size]) / float(window_size)
def plot_episode(rew_list, save_path):
plt.figure()
plt.plot(np.arange(len(rew_list)), rew_list)
plt.xlabel('timestep')
plt.ylabel('accumulated reward')
plt.savefig(save_path, bbox_inches='tight')
# print('figure saved as {}'.format(save_path))
plt.close()
def plot_training(acc_rewards, save_path):
window_size = 10
plt.figure(figsize=(20,10))
plt.plot(np.arange(len(acc_rewards)), acc_rewards)
plt.plot(np.arange(window_size//2-1, len(acc_rewards)-window_size//2), running_mean(acc_rewards, 10), c='red')
plt.xlabel('episode')
plt.ylabel('accumulated reward')
plt.savefig(save_path, bbox_inches='tight')
# print('figure saved as {}'.format(save_path))
plt.close()
def episode_finished(target_net, policy_net, env, episode, save_dir, start_time_str, acc_rewards):
topk = max(1, int(len(acc_rewards)*0.1))
top_rew = heapq.nlargest(topk, acc_rewards)
rew_threshold = min(top_rew)
if acc_rewards[-1] >= rew_threshold or (episode+1)%100 == 0:
# plot timesteps vs. accumulated reward
fn = '_'.join((start_time_str, 'ep', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
plot_episode(env.rew_list, save_path)
# plot episode vs. accumulated reward
fn = '_'.join((start_time_str, 'rew', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
plot_training(acc_rewards, save_path)
# plot graphene structures in the episode
fn = '_'.join((start_time_str, 'atoms', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
env.visualize_seq(save_path)
# plot flux/rejection/remove atoms in the episode
fn = '_'.join((start_time_str, 'flux', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
env.plot_flux(save_path)
fn = '_'.join((start_time_str, 'ion_rej', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
env.plot_ion_rej(save_path)
fn = '_'.join((start_time_str, 'num_rm', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
env.plot_remove_number(save_path)
# save the target_net
dqn_fn = '_'.join((start_time_str, 'target', str(episode))) + '.ckpt'
dqn_path = os.path.join(save_dir, dqn_fn)
torch.save(target_net.state_dict(), dqn_path)
# save the policy_net
dqn_fn = '_'.join((start_time_str, 'policy', str(episode))) + '.ckpt'
dqn_path = os.path.join(save_dir, dqn_fn)
torch.save(policy_net.state_dict(), dqn_path)
return True
def episode_finished_dense(target_net, policy_net, env, episode, save_dir, start_time_str, acc_rewards):
sub_save_dir = os.path.join(save_dir, str(episode))
if not os.path.exists(sub_save_dir):
os.makedirs(sub_save_dir)
# plot timesteps vs. accumulated reward
fn = '_'.join((start_time_str, 'ep', str(episode))) + '.png'
save_path = os.path.join(sub_save_dir, fn)
plot_episode(env.rew_list, save_path)
# plot graphene structures in the episode
fn = '_'.join((start_time_str, 'atoms', str(episode))) + '.png'
save_path = os.path.join(sub_save_dir, fn)
env.visualize(save_path)
# plot flux/rejection/remove atoms in the episode
fn = '_'.join((start_time_str, 'flux', str(episode))) + '.png'
save_path = os.path.join(sub_save_dir, fn)
env.plot_flux(save_path)
fn = '_'.join((start_time_str, 'ion_rej', str(episode))) + '.png'
save_path = os.path.join(sub_save_dir, fn)
env.plot_ion_rej(save_path)
fn = '_'.join((start_time_str, 'num_rm', str(episode))) + '.png'
save_path = os.path.join(sub_save_dir, fn)
env.plot_remove_number(save_path)
# save to pdb file
fn = '_'.join((start_time_str, 'graphene', str(episode))) + '.pdb'
save_path = os.path.join(sub_save_dir, fn)
env.graphene.write2pdb(save_path)
if (episode+1)%100 == 0:
# plot episode vs. accumulated reward
fn = '_'.join((start_time_str, 'rew', str(episode))) + '.png'
save_path = os.path.join(save_dir, fn)
plot_training(acc_rewards, save_path)
# save the target_net
dqn_fn = '_'.join((start_time_str, 'target', str(episode))) + '.ckpt'
dqn_path = os.path.join(save_dir, dqn_fn)
torch.save(target_net.state_dict(), dqn_path)
# save the policy_net
dqn_fn = '_'.join((start_time_str, 'policy', str(episode))) + '.ckpt'
dqn_path = os.path.join(save_dir, dqn_fn)
torch.save(policy_net.state_dict(), dqn_path)
return True
def nearest_atom(atom_coord, removed_idx, num_atoms):
if len(removed_idx) == 1:
center = np.array(atom_coord[list(removed_idx)]).squeeze()
else:
center = np.array(atom_coord[list(removed_idx)]).mean(axis=0)
dist_list = np.zeros(len(atom_coord))
for i in range(len(atom_coord)):
if i in removed_idx:
dist_list[i] = 100
else:
dist_list[i] = cal_distance(center, atom_coord[i])
nearest_idx = dist_list.argsort()[:num_atoms]
return nearest_idx
def coord2area(coords):
########Coord to image##########
# https://stackoverflow.com/questions/33094509/correct-sizing-of-markers-in-scatter-plot-to-a-radius-r-in-matplotlib
figure = plt.figure(figsize=[5, 5])
ax = plt.axes([0, 0, 1, 1], xlim=(0, 40), ylim=(0, 40))
points_whole_ax = 5 *1 * 72 # 1 point = dpi / 72 pixels
radius = 3.39/2
points_radius = 2 * radius / (40) * points_whole_ax
ax.scatter(coords[:,0], coords[:,1], s=points_radius**2, color='grey', edgecolors = 'k')
ax.axis('off')
plt.savefig('./process_img/temp.png')
plt.close()
################Area from Image#############
img = cv.imread('./process_img/temp.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(gray,127,255,0)
contours,hierarchy = cv.findContours(thresh,
cv.RETR_TREE,
cv.CHAIN_APPROX_NONE)
print('Number of contours = ', str(len(contours)))
##############Get areas for all contours###########
areas = [cv.contourArea(c) for c in contours]
###########Get the largest contour##########
sort_index = np.argsort(areas)[::-1]
ind = sort_index[0]
###########Scale conversion##################
rate = (40**2)/(360**2)
###########################################
final_area = areas[ind] * rate
print('Area:', areas[ind] * rate)
##########Draw tht pore area contour(No need here)##########
# cv.drawContours(img, contours[ind], -1, (0,255,0), 3)
# cv.imshow('Image', img)
# cv.waitKey(0)
# cv.destroyAllWindows()
# plt.show()
os.remove('temp.png')
return final_area