forked from huawei-noah/HEBO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_logger.py
62 lines (43 loc) · 2.27 KB
/
data_logger.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
import os
import numpy as np
import pandas as pd
class DataLogger:
def __init__(self, size):
self.size = size
self._idx = 0
self.df = self.res = pd.DataFrame(np.nan, index=np.arange(1, self.size + 1),
columns=['Num BB Evals', 'Last Binding Energy', 'Best Binding Energy',
'Suggest Time', 'Last Protein', 'Best Protein'])
def append(self, protein, binding_energy, suggest_time, num_bb_evals):
if self._idx == 0:
best_binding_energy = binding_energy
best_protein = protein
else:
best_idx = self.df.iloc[:self._idx]['Last Binding Energy'].argmin()
best_binding_energy = self.df.iloc[best_idx]['Last Binding Energy']
best_protein = self.df.iloc[best_idx]['Last Protein']
if best_binding_energy > binding_energy:
best_binding_energy = binding_energy
best_protein = protein
self.df.iloc[self._idx] = [int(num_bb_evals), binding_energy, best_binding_energy, suggest_time, protein,
best_protein]
self._idx += 1
def append_batch(self, proteins, binding_energies, suggest_times, num_bb_evals):
for protein_, binding_energy_, suggest_time_, num_bb_evals_ in zip(proteins, binding_energies, suggest_times,
num_bb_evals):
self.append(protein_, binding_energy_, suggest_time_, num_bb_evals_)
def save_results(self, save_dir):
self.df.to_csv(os.path.join(save_dir, 'results.csv'))
def reset_logger(self):
self._idx = 0
self.df = self.res = pd.DataFrame(np.nan, index=np.arange(1, self.size + 1),
columns=['Num BB Evals', 'Last Binding Energy', 'Best Binding Energy',
'Suggest Time', 'Last Protein', 'Best Protein'])
if __name__ == '__main__':
import random
import string
n = 20
logger = DataLogger(n)
for i in range(n):
logger.append(''.join(random.choice(string.ascii_uppercase) for _ in range(10)), np.random.randn(),
np.random.random(), i + 1)