-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataLog.py
82 lines (64 loc) · 2.2 KB
/
DataLog.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
import os
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
class DataLog:
"""
Class to collect data from simulation and store it somewhere
"""
def __init__(self):
self.sim = None
self.f = None
self.ToFile = False
self.ToHeatMap = False
now = str(datetime.now().time())
self.folder = "My Sim "+now
def set(self,sim,path,ToFile= False,ToHeatMap= False):
"""
Set the simulation argument
Parameters
----------
sim : Simulator
Simulator argument
path : str
Path where to save the log file
"""
self.sim = sim
self.f = open(path,"w")
self.ToFile = ToFile
self.ToHeatMap = ToHeatMap
def storeToFile(self,label,data):
"""
Store the log to the file
"""
i = 0
for d in data[0]:
self.f.write(label+":"+str(i)+":"+str(d)+"\n")
i = i + 1
def storeToHeatMap(self,data,t):
if not os.path.exists(self.folder):
os.makedirs(self.folder)
i = 0
for d in data[0]:
if not os.path.exists(self.folder+"/"+str(i)):
os.makedirs(self.folder+"/"+str(i))
plt.matshow(np.reshape(d,(28,28)),cmap="Reds")
plt.title(str(i)+str(t))
plt.savefig(self.folder+"/"+str(i)+"/"+str(i)+":"+str(i)+str(t)+".png")
plt.cla()
i = i+1
def closeLog(self):
"""
Close log file
"""
self.f.close()
def __call__(self, t):
if self.sim is not None:
assert len(self.sim.model.probes) != 0 , "No Probes to store"
for probe in self.sim.model.probes:
if len(self.sim._sim_data[probe]) != 0:
self.sim._sim_data[probe] = [self.sim._sim_data[probe][-1]]
if self.ToFile:
self.storeToFile(str(t)+probe.label,self.sim._sim_data[probe])
if self.ToHeatMap:
self.storeToHeatMap(self.sim._sim_data[probe],t)