forked from openai/neural-mmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
figures.py
159 lines (140 loc) · 4.28 KB
/
figures.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
import numpy as np
import sys, json
from forge.blade.lib.enums import Neon, Color256
from forge.blade.lib.log import InkWell
from matplotlib import pyplot as plt
from pdb import set_trace as T
from itertools import groupby
from collections import defaultdict
import collections
import logs as loglib
import experiments
import pickle
import os.path as osp
import os
def plot(x, idxs, label, idx, path):
colors = Neon.color12()
loglib.dark()
c = colors[idx % 12]
loglib.plot(x, inds=idxs, label=str(idx), c=c.norm)
loglib.godsword()
loglib.save(path + label + '.png')
plt.close()
def plots(x, label, idx, path, split):
colors = Neon.color12()
loglib.dark()
for idx, item in enumerate(x.items()):
annID, val = item
c = colors[idx % 12]
idxs, val = compress(val, split)
loglib.plot(val, inds=idxs, label=str(annID), c=c.norm)
loglib.godsword()
loglib.save(path + label + '.png')
plt.close()
def meanfilter(x, n=1):
ret = []
for idx in range(len(x) - n):
val = np.mean(x[idx:(idx+n)])
ret.append(val)
return ret
def compress(x, split):
rets, idxs = [], []
if split == 'train':
n = 1 + len(x) // 20
else:
n = 1 + len(x) // 20
for idx in range(0, len(x) - n, n):
rets.append(np.mean(x[idx:(idx+n)]))
idxs.append(idx)
return 10*np.array(idxs), rets
def popPlots(popLogs, path, split):
idx = 0
print(path)
for key, val in popLogs.items():
print(key)
#val = meanfilter(val, 1+len(val)//100)
plots(val, str(key), idx, path, split)
idx += 1
def flip(popLogs):
ret = defaultdict(dict)
for annID, logs in popLogs.items():
for key, log in logs.items():
if annID not in ret[key]:
ret[key][annID] = []
if type(log) != list:
ret[key][annID].append(log)
else:
ret[key][annID] += log
return ret
def group(blobs, idmaps):
rets = defaultdict(list)
for blob in blobs:
groupID = idmaps[blob.annID]
rets[groupID].append(blob)
return rets
def mergePops(blobs, idMap):
#blobs = sorted(blobs, key=lambda x: x.annID)
#blobs = dict(blobs)
#idMap = {}
#for idx, accumList in enumerate(accum):
# for e in accumList:
# idMap[e] = idx
blobs = group(blobs, idMap)
pops = defaultdict(list)
for groupID, blobList in blobs.items():
pops[groupID] += list(blobList)
return pops
def individual(blobs, logDir, name, accum, split):
savedir = logDir + name + '/' + split + '/'
if not osp.exists(savedir):
os.makedirs(savedir)
blobs = mergePops(blobs, accum)
popLogs = {}
for annID, blobList in blobs.items():
logs, blobList = {}, list(blobList)
logs = {**logs, **InkWell.counts(blobList)}
logs = {**logs, **InkWell.unique(blobList)}
logs = {**logs, **InkWell.explore(blobList)}
logs = {**logs, **InkWell.lifetime(blobList)}
logs = {**logs, **InkWell.reward(blobList)}
logs = {**logs, **InkWell.value(blobList)}
popLogs[annID] = logs
popLogs = flip(popLogs)
popPlots(popLogs, savedir, split)
def makeAccum(config, form='single'):
assert form in 'pops single split'.split()
if form == 'pops':
return dict((idx, idx) for idx in range(config.NPOP))
elif form == 'single':
return dict((idx, 0) for idx in range(config.NPOP))
elif form == 'split':
pop1 = dict((idx, 0) for idx in range(config.NPOP1))
pop2 = dict((idx, 0) for idx in range(config.NPOP2))
return {**pop1, **pop2}
if __name__ == '__main__':
arg = None
if len(sys.argv) > 1:
arg = sys.argv[1]
logDir = 'resource/exps/'
logName = '/model/logs.p'
fName = 'frag.png'
name = 'newfig'
exps = []
for name, config in experiments.exps.items():
try:
with open(logDir + name + logName, 'rb') as f:
dat = []
idx = 0
while True:
idx += 1
try:
dat += pickle.load(f)
except EOFError as e:
break
print('Blob length: ', idx)
split = 'test' if config.TEST else 'train'
accum = makeAccum(config)
individual(dat, logDir, name, accum, split)
print('Log success: ', name)
except Exception as err:
print(str(err))