forked from robinlingwood/BIMODAL
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathevaluation.py
219 lines (177 loc) · 8.16 KB
/
evaluation.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
"""
Implementation of the model evaluation
"""
import pandas as pd
import numpy as np
import configparser
import matplotlib
import sys
sys.path.insert(1, '../model/')
from helper import clean_molecule, check_valid
matplotlib.use('TkAgg')
matplotlib.rcParams.update({'font.size': 14})
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import os
class Evaluator():
def __init__(self, experiment_name):
# Read parameter used during training
self._config = configparser.ConfigParser()
self._config.read('../experiments/' + experiment_name + '.ini')
self._model_type = self._config['MODEL']['model']
self._experiment_name = experiment_name
self._file_name = self._config['DATA']['data']
self._encoding_size = int(self._config['DATA']['encoding_size'])
self._molecular_size = int(self._config['DATA']['molecular_size'])
self._epochs = int(self._config['TRAINING']['epochs'])
self._n_folds = int(self._config['TRAINING']['n_folds'])
self._learning_rate = float(self._config['TRAINING']['learning_rate'])
self._batch_size = int(self._config['TRAINING']['batch_size'])
self._samples = int(self._config['EVALUATION']['samples'])
self._T = float(self._config['EVALUATION']['temp'])
self._starting_token = self._config['EVALUATION']['starting_token']
if os.path.isfile('../data/' + self._file_name + '.csv'):
self._data = pd.read_csv('../data/' + self._file_name + '.csv', header=None).values[:, 0]
elif os.path.isfile('../data/' + self._file_name + '.tar.xz'):
# Skip first line since empty and last line since nan
self._data = pd.read_csv('../data/' + self._file_name + '.tar.xz', compression='xz', header=None).values[
1:-1, 0]
# Clean data from start, end and padding token
for i, mol_dat in enumerate(self._data):
self._data[i] = clean_molecule(mol_dat, self._model_type)
def eval_training_validation(self, stor_dir='.'):
'''
Plot training and validation loss within one figure
:return:
'''
stat = np.zeros((self._n_folds, self._epochs))
val = np.zeros((self._n_folds, self._epochs))
plt.figure()
plt.set_cmap('tab10')
plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}'))
for i in range(self._n_folds):
tmp = pd.read_csv(stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(i + 1) + '.csv',
header=None).values[:,
1:]
stat[i, :] = np.mean(tmp, axis=-1)
plt.plot(np.arange(self._epochs) + 1, stat[i, :], '-', label='Fold ' + str(i + 1) + ' Training',
color='C' + str(i))
for i in range(self._n_folds):
val[i, :] = pd.read_csv(stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(i + 1) + '.csv',
header=None).values[:, 1]
plt.plot(np.arange(self._epochs) + 1, val[i, :], ':', label='Fold ' + str(i + 1) + ' Validation',
color='C' + str(i))
plt.legend()
plt.title('Statistic Training')
plt.ylabel('Loss Per Token')
plt.xlabel('Epoch')
plt.savefig(self._experiment_name + '/statistic/all_statistic.png')
plt.close()
def eval_training(self, stor_dir='.'):
'''Plot training loss
:return:
'''
stat = np.zeros((self._n_folds, self._epochs))
plt.figure(0)
for i in range(self._n_folds):
tmp = pd.read_csv(stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(i + 1) + '.csv',
header=None).values[:,
1:]
stat[i, :] = np.mean(tmp, axis=-1)
plt.plot(np.arange(1, self._epochs + 1), stat[i, :], label='Fold ' + str(i + 1))
plt.legend()
plt.title('Training Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.savefig(self._experiment_name + '/statistic/statistic.png')
plt.close()
def eval_validation(self, stor_dir='.'):
'''Plot validation loss
:return:
'''
val = np.zeros((self._n_folds, self._epochs))
plt.figure(1)
for i in range(self._n_folds):
val[i, :] = pd.read_csv(stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(i + 1) + '.csv',
header=None).values[:, 1]
plt.plot(np.arange(1, self._epochs + 1), val[i, :], label='Fold ' + str(i + 1))
plt.legend()
plt.title('Validation Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.savefig(self._experiment_name + '/validation/validation.png')
plt.close()
def check_with_training_data(self, mol):
'''Remove molecules that are within the training set and return number
:return mol: SMILES not contained in the training
'''
to_delete = []
for i, m in enumerate(mol):
if m in self._data:
to_delete.append(i)
mol = np.delete(mol, to_delete)
return mol
def eval_molecule(self, stor_dir='.'):
'''Plot percentage of novel, valid and unique SMILES
:return:
'''
valid = np.zeros((self._n_folds, self._epochs))
unique = np.zeros((self._n_folds, self._epochs))
novel = np.zeros((self._n_folds, self._epochs))
for i in range(self._n_folds):
for j in range(self._epochs):
mol = pd.read_csv(
stor_dir + '/' + self._experiment_name + '/molecules/molecule_fold_' + str(
i + 1) + '_epochs_' + str(j) + '.csv',
header=None).values[:, 1].astype(str)
# Remove padding
for k, m in enumerate(mol):
mol[k] = clean_molecule(m, self._model_type)
# Compute unique molecules
unique[i, j] = len(set(mol)) / self._samples
# Remove duplicates
mol = np.array(list(set(mol)))
# Check validity and remove non-valid molecules
to_delete = []
for k, m in enumerate(mol):
if not check_valid(m):
to_delete.append(k)
valid_mol = np.delete(mol, to_delete)
valid[i, j] = len(valid_mol) / self._samples
# Compute molecules unequal to training data
if valid_mol.size != 0:
new_m = self.check_with_training_data(list(valid_mol))
novel[i, j] = len(new_m) / self._samples
# Get percentage
unique *= 100
novel *= 100
valid *= 100
# Get mean values
mean_unique = np.mean(unique, axis=0)
mean_valid = np.mean(valid, axis=0)
mean_novel = np.mean(novel, axis=0)
# Get standard deviation
std_unique = np.std(unique, axis=0)
std_valid = np.std(valid, axis=0)
std_novel = np.std(novel, axis=0)
print(mean_unique)
print(mean_valid)
print(mean_novel)
# PLot
plt.figure(1)
plt.errorbar(np.arange(1, self._epochs + 1), mean_unique, yerr=std_unique, capsize=3, label='unique')
plt.errorbar(np.arange(1, self._epochs + 1), mean_valid, yerr=std_valid, capsize=3,
label='valid & unique')
plt.errorbar(np.arange(1, self._epochs + 1), mean_novel, yerr=std_novel, capsize=3,
label='novel, valid & unique', linestyle=':')
plt.yticks(np.arange(0, 110, step=10))
plt.legend()
plt.ylim(0, 105)
plt.title('SMILES T=' + str(self._T))
plt.ylabel('% SMILES')
plt.xlabel('Epoch')
plt.savefig(stor_dir + '/' + self._experiment_name + '/molecules/novel_valid_unique_molecules.png')
# Store data
data = np.vstack((mean_unique, std_unique, mean_valid, std_valid, mean_novel, std_novel))
pd.DataFrame(data).to_csv(self._experiment_name + '/molecules/' + self._experiment_name + '_data.csv')
plt.show()