Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils_logplot.py
More file actions
Latest commit
110 lines (93 loc) · 3.54 KB
/
Copy pathutils_logplot.py
File metadata and controls
110 lines (93 loc) · 3.54 KB
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
frommatplotlibimportpyplotasplt
fromdatetimeimportdatetime
importnumpyasnp
importjson, os
defgeom(data, alpha=0.995):
gdata= [data[0]]
fordindata[1:]:
gdata.append(gdata[-1]*alpha+ (1-alpha)*d)
returngdata
defplot(files=[], folder=None, filt='', geo=0, xaxis='steps', maxy={}):
# filt: a filter substring. Only keys with this substring will be plotted. For example 'V' to plot only validation values
# geo: a float between (0,1.0) that is used for smoothing the series. Values closer to 1.0 lead to more smoothing. 0.99 is a lot of smoothing
# xaxis: what to base the x-axis on. `steps` will just be a range(len(values)). `datetime` will show absolute times.
iffolderisnotNone:
files= [os.path.join(folder, fn) forfninos.listdir(folder) ifos.path.isfile(os.path.join(folder, fn))]
bad_keys= ['datetime']
full_summary= {}
forfileinfiles:
full_summ= []
withopen(file, "r") asf:
forlineinf:
iflen(line) ==0: continue
try: obj=json.loads(line)
except:
obj= {}
if'datetime'inobj:
obj['datetime'] =datetime.strptime(obj['datetime'][:19], "%Y-%m-%d %H:%M:%S")
full_summ.append(obj)
full_summary[file] =full_summ
all_keys=list(set([kforfile, full_summinfull_summary.items() forsumminfull_summforkinsumm]))
all_keys= [kforkinall_keysiffiltinkandknotinbad_keys]
xlabel=None
forkinall_keys:
plt.figure()
plt.title(k)
legend= []
forfileinfiles:
subsumm= [summforsumminfull_summary[file] ifkinsumm]
ifxaxisin ['datetime', 'seconds']:
subsumm= [summforsumminsubsummif'datetime'insumm]
ifkinmaxy:
subsumm= [summforsumminsubsummifsumm[k] <maxy[k]]
iflen(subsumm) ==0: continue
xs=list(range(len(subsumm)))
ys=geom([summ[k] forsumminsubsumm], geo)
ifxaxisin ['datetime', 'seconds']:
xs= [summ['datetime'] forsumminsubsumm]
ifxaxis=='seconds':
start=min(xs)
xs=np.array([(dt-start).total_seconds() fordtinxs])
second_span=xs[-1]
ifxlabelisNone:
ifsecond_span>3*86400:
xlabel='days'
elifsecond_span>5*3600:
xlabel='hours'
elifsecond_span>10*60:
xlabel='minutes'
ifxlabel=='days': xs/=86400.0
ifxlabel=='hours': xs/=3600.0
ifxlabel=='minutes': xs/=60.0
ifxaxis!='seconds':
xlabel=xaxis
legend.append(file.split("/")[-1])
plt.plot(xs, ys)
plt.ylabel(k)
plt.xlabel(xlabel)
plt.legend(legend)
classLogPlot():
def__init__(self, where_to):
# `where_to` the file where you want to save the summaries
self.current_cache= {}
self.where_to=where_to
defcache(self, results, prefix=''):
# Results should be a dict of keys (of things to save) and values to save {"Loss": 1.0}
# Prefix: will be added to each key string (for instance a "T" for training, an "V" for validation)
fork, valinresults.items():
nk=prefix+k
ifnknotinself.current_cache:
self.current_cache[nk] = []
self.current_cache[nk].append(float(val))
returnself
defsave(self, printing=False):
defreduce_array(vals, k):
ifk=="T_count": returnsum(vals)
else: returnfloat(np.mean(vals))
save_obj= {k: reduce_array(vals, k) fork, valsinself.current_cache.items()}
save_obj['datetime'] =str(datetime.now())
f=open(self.where_to, "a"); f.write(json.dumps(save_obj)+"\n"); f.close()
self.clear_cache()
ifprinting: print(save_obj)
defclear_cache(self):
self.current_cache= {} # Reempty the cache