forked from abides-sim/abides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_agent_logs.py
60 lines (40 loc) · 1.69 KB
/
read_agent_logs.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
import os
import pandas as pd
import sys
# Auto-detect terminal width.
pd.options.display.width = None
pd.options.display.max_rows = 500000
pd.options.display.max_colwidth = 200
if len(sys.argv) < 2:
print ("Usage: python dump.py <log directory>")
sys.exit()
# read_agent_logs.py takes a log directory, reads all agent log files, and produces a summary of
# desired totals or statistics by strategy (type + parameter settings).
# If more than one directory is given, the program aggregates across all of them.
log_dirs = sys.argv[1:]
stats = []
dir_count = 0
file_count = 0
for log_dir in log_dirs:
if dir_count % 100 == 0: print ("Completed {} directories".format(dir_count))
dir_count += 1
for file in os.listdir(log_dir):
df = pd.read_pickle(os.path.join(log_dir,file), compression='bz2')
events = [ 'AGENT_TYPE', 'STARTING_CASH', 'ENDING_CASH', 'FINAL_CASH_POSITION', 'FINAL_VALUATION' ]
event = "|".join(events)
df = df[df['EventType'].str.contains(event)]
at = df.loc[df['EventType'] == 'AGENT_TYPE', 'Event'][0]
if 'Exchange' in at:
# There may be different fields to look at later on.
continue
file_count += 1
sc = df.loc[df['EventType'] == 'STARTING_CASH', 'Event'][0]
ec = df.loc[df['EventType'] == 'ENDING_CASH', 'Event'][0]
fcp = df.loc[df['EventType'] == 'FINAL_CASH_POSITION', 'Event'][0]
fv = df.loc[df['EventType'] == 'FINAL_VALUATION', 'Event'][0]
ret = ec - sc
surp = fcp - sc + fv
stats.append({ 'AgentType' : at, 'Return' : ret, 'Surplus' : surp })
df_stats = pd.DataFrame(stats)
print (df_stats.groupby('AgentType').mean())
print ("\nRead {} files in {} log directories.".format(file_count, dir_count))