-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_metrics.py
135 lines (112 loc) · 5.06 KB
/
compute_metrics.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
import os
import logging
import argparse
import numpy as np
import pandas as pd
from models.data import IHDP, JOBS, TWINS, NEWS
from models.estimators import SEvaluator, TEvaluator, XEvaluator, DREvaluator, DMLEvaluator, IPSWEvaluator, CausalForestEvaluator
from models.estimators import TSEvaluator, DRSEvaluator, DMLSEvaluator, IPSWSEvaluator
from helpers.utils import init_logger, get_model_name
def get_parser():
parser = argparse.ArgumentParser()
# General
parser.add_argument('--data_path', type=str)
parser.add_argument('--dtype', type=str, choices=['ihdp', 'jobs', 'news', 'twins'])
parser.add_argument('--results_path', type=str)
parser.add_argument('--sf', dest='splits_file', type=str)
parser.add_argument('--iters', type=int, default=-1)
parser.add_argument('-o', type=str, dest='output_path', default='./')
parser.add_argument('--scaler', type=str, choices=['minmax', 'std'], default='std')
parser.add_argument('--scale_y', action='store_true')
# Estimation
parser.add_argument('--em', dest='estimation_model', type=str, choices=['sl', 'tl', 'tls', 'xl', 'dr', 'drs', 'dml', 'dmls', 'ipsw', 'ipsws', 'two-head', 'cf'], default='sl')
parser.add_argument('--bm', dest='base_model', type=str, choices=['l1', 'l2', 'tr', 'dt', 'rf', 'et', 'kr', 'cb', 'lgbm', 'mlp'], default='lr')
return parser
def get_evaluator(opt):
if opt.estimation_model in ('sl', 'two-head'):
return SEvaluator(opt)
elif opt.estimation_model == 'tl':
return TEvaluator(opt)
elif opt.estimation_model == 'tls':
return TSEvaluator(opt)
elif opt.estimation_model == 'xl':
return XEvaluator(opt)
elif opt.estimation_model == 'dr':
return DREvaluator(opt)
elif opt.estimation_model == 'drs':
return DRSEvaluator(opt)
elif opt.estimation_model == 'dml':
return DMLEvaluator(opt)
elif opt.estimation_model == 'dmls':
return DMLSEvaluator(opt)
elif opt.estimation_model == 'ipsw':
return IPSWEvaluator(opt)
elif opt.estimation_model == 'ipsws':
return IPSWSEvaluator(opt)
elif opt.estimation_model == 'cf':
return CausalForestEvaluator(opt)
else:
raise ValueError("Unrecognised 'get_evaluator' key.")
def get_dataset(name, path, iters):
result = None
if name == 'ihdp':
result = IHDP(path, iters)
elif name == 'jobs':
result = JOBS(path, iters)
elif name == 'twins':
result = TWINS(path, iters, static_splits=True)
elif name == 'news':
result = NEWS(path, iters, static_splits=True)
else:
raise ValueError('Unknown dataset type selected.')
return result
if __name__ == "__main__":
parser = get_parser()
options = parser.parse_args()
# Check if output folder exists and create if necessary.
if not os.path.isdir(options.output_path):
os.mkdir(options.output_path)
# Initialise the logger (writes simultaneously to a file and the console).
init_logger(options)
logging.debug(options)
# (iters, folds, idx)
splits = np.load(options.splits_file, allow_pickle=True)
n_iters = options.iters if options.iters > 0 else splits.shape[0]
dataset = get_dataset(options.dtype, options.data_path, n_iters)
# iter id, fold id, param id, mse, ate, pehe
# iter id, param id, mse, ate, pehe
df_val = None
df_test = None
evaluator = get_evaluator(options)
# Data iterations
for i in range(n_iters):
train, test = dataset._get_train_test(i)
X_tr, t_tr, y_tr = dataset.get_xty(train)
X_test, t_test, y_test = dataset.get_xty(test)
eval_test = dataset.get_eval(test)
# *** Test set metrics ***
df_iter = evaluator.run(i+1, -1, y_tr, t_test, y_test, eval_test)
df_test = pd.concat([df_test, df_iter], ignore_index=True)
# ***
# CV iterations
for k, (train_idx, valid_idx) in enumerate(zip(splits['train'][i], splits['valid'][i])):
logging.info(f'Iter {i+1}, Fold {k+1}')
train_idx = train_idx.astype(int)
valid_idx = valid_idx.astype(int)
y_tr_fold = y_tr[train_idx]
t_val_fold = t_tr[valid_idx]
y_val_fold = y_tr[valid_idx]
eval_valid = dataset.get_eval_idx(train, valid_idx)
# *** CV metrics ***
df_fold = evaluator.run(i+1, k+1, y_tr_fold, t_val_fold, y_val_fold, eval_valid)
df_val = pd.concat([df_val, df_fold], ignore_index=True)
# ***
model_name = get_model_name(options)
df_test.to_csv(os.path.join(options.output_path, f'{model_name}_test_metrics.csv'), index=False)
#print(df_test[['ate', 'pehe']].min())
#df_ate = df_test.groupby(['iter_id'], as_index=False).apply(lambda x: x.loc[x['ate'].idxmin(), ['ate']])
#df_pehe = df_test.groupby(['iter_id'], as_index=False).apply(lambda x: x.loc[x['pehe'].idxmin(), ['pehe']])
#df_all = df_ate.merge(df_pehe, on=['iter_id'])
#print(df_all)
#print(df_all.mean())
df_val.to_csv(os.path.join(options.output_path, f'{model_name}_val_metrics.csv'), index=False)