-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompute_averages.py
49 lines (41 loc) · 1.41 KB
/
compute_averages.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
from argparse import ArgumentParser
import numpy as np
import pandas as pd
from glob import glob
import os
from pdb import set_trace
parser = ArgumentParser()
parser.add_argument('inputdir')
args = parser.parse_args()
def save(df, fname):
dname = os.path.dirname(fname)
if not os.path.isdir(dname):
os.makedirs(dname)
records = df.to_records(index=False)
records.dtype.names = [str(i) for i in records.dtype.names]
np.save(fname, records)
histories = glob('%s/*/history.npy' % args.inputdir)
histories = [pd.DataFrame(np.load(i)) for i in histories]
columns = list(histories[0].columns)
out = pd.DataFrame()
for column in columns:
vals = pd.concat([i[column] for i in histories], axis=1)
out['%s_mean' % column] = vals.mean(axis=1)
out['%s_std' % column] = vals.std(axis=1)
save(out, '%s/history.npy' % args.inputdir)
predictions = glob('%s/*/predictions.npy' % args.inputdir)
predictions = [pd.DataFrame(np.load(i)) for i in predictions]
#check that MC thruts are the same
assert(
all(
((predictions[0][['isB', 'isMC']] == i[['isB', 'isMC']]).all()).all()
for i in predictions
)
)
out = predictions[0][['isB', 'isMC']]
vals = pd.concat([i['prediction'] for i in predictions], axis=1)
out['prediction_mean'] = vals.mean(axis=1)
out['prediction_std'] = vals.std(axis=1)
for idx, df in enumerate(predictions):
out['prediction_%d' % idx] = df['prediction']
save(out, '%s/predictions.npy' % args.inputdir)