-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathviz.py
76 lines (58 loc) · 1.87 KB
/
viz.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
import argparse, json
USAGE = "python viz.py data.json"
RED = '\x1b[6;30;48;2;255;0;0m'
DARKRED = '\x1b[6;30;48;2;124m'
GREEN = '\x1b[6;30;48;5;120m'
DARKGREEN = '\x1b[6;30;48;5;28m'
def main(data):
traces = []
plans = []
for trace in data:
traces.append([])
plans.append([])
for step in trace:
print(step)
traces[-1].append(step['state'])
if step['actions']:
plans[-1].append(', '.join(step['actions']))
else:
plans[-1].append('')
for trace, plan in zip(traces, plans):
print_trace(trace, plan)
def print_trace(trace, plan):
fluents = list(sorted(trace[0].keys()))
maxfluentlen = max(len(f) for f in fluents)+2
print("\nActions:")
for i in range(len(plan)):
print(str(i).rjust(2) + ': ' + str(plan[i]))
print("\nTrace:\n")
# Print the step numbers
print(' '*maxfluentlen, end=' ')
for i in range(len(trace)):
print(str(i).rjust(2), end=' ')
print()
for f in fluents:
# print the fluent right aligned with maxfluentlen characters
print(f.rjust(maxfluentlen), end=' ')
lastf = "init"
for t in trace:
if t[f] != lastf:
lastf = t[f]
if t[f]:
print(GREEN + ' ' + '\x1b[0m', end=' ')
else:
print(RED + ' ' + '\x1b[0m', end=' ')
else:
if t[f]:
print(DARKGREEN + ' ' + '\x1b[0m', end=' ')
else:
print(DARKRED + ' ' + '\x1b[0m', end=' ')
print()
print()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('data', help='Path to data file')
args = parser.parse_args()
with open(args.data) as f:
data = json.load(f)
main(data)