-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
61 lines (55 loc) · 2.58 KB
/
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
'''customized logger that deals with input and output
'''
import os
from zmq import DEALER
from input_output import ProblemInput, TheoremOutput, HillclimbOutput, SimulatedAnnealOutput, GeneticOutput
class Logger:
@staticmethod
def write_log(log_dir, log_file, myinput, myoutputs):
if os.path.exists(log_dir) is False:
os.mkdir(log_dir)
with open(os.path.join(log_dir, log_file), 'a') as f:
f.write(f'{myinput}\n')
for output in myoutputs:
f.write(f'{output}\n')
f.write('\n')
f.flush()
@staticmethod
def read_log(logs):
data = []
for log in logs:
f = open(log, 'r')
while True:
line = f.readline()
if line == '':
break
myinput = ProblemInput.from_json_str(line)
output_by_method = {}
line = f.readline()
while line != '' and line != '\n':
if line.find('Theorem povm-noise') != -1:
output = TheoremOutput.from_json_str(line)
output_by_method['Theorem povm-noise'] = output
line = f.readline()
continue
if line.find('Theorem') != -1:
output = TheoremOutput.from_json_str(line)
output_by_method['Theorem'] = output
if line.find('GHZ') != -1: # GHZ reuse the TheoremOutput
output = TheoremOutput.from_json_str(line)
output_by_method['GHZ'] = output
if line.find('Non entangle') != -1:
output = TheoremOutput.from_json_str(line) # Non entangle reuse the TheoremOutput
output_by_method['Non entangle'] = output
if line.find('Hill climbing') != -1:
output = HillclimbOutput.from_json_str(line)
output_by_method['Hill climbing'] = output
if line.find('Simulated annealing') != -1:
output = SimulatedAnnealOutput.from_json_str(line)
output_by_method['Simulated annealing'] = output
if line.find('Genetic algorithm') != -1:
output = GeneticOutput.from_json_str(line)
output_by_method['Genetic algorithm'] = output
line = f.readline()
data.append((myinput, output_by_method))
return data