-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
148 lines (144 loc) · 6.49 KB
/
Copy pathtest.py
File metadata and controls
148 lines (144 loc) · 6.49 KB
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
import pandas as pd
import numpy as np
import os
from statsmodels.formula.api import ols
from src.utils import get_rdd_robust_results_reduced
def main():
res = pd.DataFrame()
target_coverages = [0.1, 0.2, 0.3, 0.4, 0.5, 0.60, 0.70, 0.80, 0.90]
epochs_dict = {
"cifar10h": 150,
"galaxyzoo": 50,
"chestxray2": 3,
"hatespeech": 100,
"synth": 50,
}
# collect all results
for data in ["cifar10h", "galaxyzoo", "chestxray2", "hatespeech", "synth"]:
# for each method compute effects of interest
for method in ["RS", "CC", "DT", "LCE", "SP", "OVA", "ASM"]:
if data == "synth":
filename_cal = "resultsRAW/{}/GCresultsRAW_cal_{}_{}_42_0.1_0.4_0.1_ep{}.csv".format(
data, data, method, epochs_dict[data]
)
filename_test = "resultsRAW/{}/GCresultsRAW_test_{}_{}_42_0.1_0.4_0.1_ep{}.csv".format(
data, data, method, epochs_dict[data]
)
else:
filename_cal = (
"resultsRAW/{}/GCresultsRAW_cal_{}_{}_42_ep{}.csv".format(
data, data, method, epochs_dict[data]
)
)
filename_test = (
"resultsRAW/{}/GCresultsRAW_test_{}_{}_42_ep{}.csv".format(
data, data, method, epochs_dict[data]
)
)
if os.path.exists(filename_cal) and os.path.exists(filename_test):
df_cal = pd.read_csv(filename_cal)
df_test = pd.read_csv(filename_test)
else:
raise FileNotFoundError(
"Run train.py for the method {} and data {} first!".format(
method, data
)
)
# compute correct predictions by the human
human_correct = np.where(
df_test["hum_preds"] == df_test["labels"], 1, 0
).astype(float)
# compute correct predictions by the ML model
ML_correct = np.where(df_test["preds"] == df_test["labels"], 1, 0).astype(
float
)
for (
c
) in (
target_coverages
): # for each target coverage compute the tau_ATT and tau_RDD
cutoff = np.quantile(df_cal["rej_score"], c)
defer = np.where(df_test["rej_score"] >= cutoff, 1, 0).astype(float)
final_pred = np.where(
df_test["rej_score"] >= cutoff,
df_test["hum_preds"],
df_test["preds"],
)
correct = np.where(final_pred == df_test["labels"], 1, 0).astype(float)
ATT = np.mean(human_correct[defer == 1]) - np.mean(
ML_correct[defer == 1]
)
check = human_correct - ML_correct
tmp = pd.DataFrame()
tmp["check"] = check
tmp["defer"] = np.where(defer == 1, 1, 0)
# run a regression to obtain the significance of ATT (the intercept coefficient is also the ATT)
dd = ols("check ~ 1", data=tmp[tmp["defer"] == 1]).fit(cov_type="HC1")
acc_hum = np.mean(human_correct[defer == 1])
acc_ML = np.mean(ML_correct[defer == 0])
acc_system = np.mean(correct)
tmp_res = pd.DataFrame()
tmp_res["data"] = [data if data != "chestxray2" else "xray-airspace"]
tmp_res["method"] = [method]
tmp_res["target_coverage"] = [c]
tmp_res["ATT"] = [ATT]
tmp_res["ci_l_ATT"] = [dd.conf_int().iloc[:, 0].values[0]]
tmp_res["ci_u_ATT"] = [dd.conf_int().iloc[:, 1].values[0]]
tmp_res["pv_rob_ATT"] = [dd.pvalues[0]]
tmp_res["acc_hum"] = [acc_hum]
tmp_res["acc_ML"] = [acc_ML]
tmp_res["acc_system"] = [acc_system]
try:
df_rdd, _ = get_rdd_robust_results_reduced(
correct, df_test["rej_score"], cutoff=cutoff
)
tmp_res = pd.concat([tmp_res, df_rdd], axis=1)
except:
df_rdd = pd.DataFrame()
value_input = 0
df_rdd["coef_rob"] = value_input
df_rdd["se_rob"] = value_input
df_rdd["pv_rob"] = value_input
df_rdd["ci_rob_l"] = value_input
df_rdd["ci_rob_l"] = value_input
df_rdd["ci_rob_u"] = value_input
tmp_res = pd.concat([tmp_res, df_rdd], axis=1)
res = pd.concat([res, tmp_res])
# compute measures for target coverage 1 and 0
tmp_res = pd.DataFrame()
tmp_res["data"] = [
data if data != "chestxray2" else "xray-airspace"
] # name the dataset xray-airspace for consistency
tmp_res["method"] = [method]
tmp_res["target_coverage"] = [1]
tmp_res["ATT"] = [0]
tmp_res["ci_l_ATT"] = [0]
tmp_res["ci_u_ATT"] = [0]
tmp_res["pv_rob_ATT"] = [0]
tmp_res["acc_hum"] = [0]
tmp_res["acc_ML"] = np.mean(ML_correct)
tmp_res["acc_system"] = np.mean(ML_correct)
tmp_res["obs"] = tmp.shape[0]
res = pd.concat([res, tmp_res])
tmp_res = pd.DataFrame()
tmp_res["data"] = [
data if data != "chestxray2" else "xray-airspace"
] # name the dataset xray-airspace for consistency
tmp_res["method"] = [method]
tmp_res["target_coverage"] = [0]
tmp = pd.DataFrame()
check = human_correct - ML_correct
tmp["check"] = check
tmp["defer"] = 1
dd = ols("check ~ 1", data=tmp).fit(cov_type="HC1")
tmp_res["ATT"] = [dd.params[0]]
tmp_res["ci_l_ATT"] = [dd.conf_int().iloc[:, 0].values[0]]
tmp_res["ci_u_ATT"] = [dd.conf_int().iloc[:, 1].values[0]]
tmp_res["pv_rob_ATT"] = [dd.pvalues[0]]
tmp_res["acc_hum"] = [np.mean(human_correct)]
tmp_res["acc_ML"] = 0
tmp_res["acc_system"] = np.mean(human_correct)
res = pd.concat([res, tmp_res])
res.to_csv("results/all_results.csv", index=False) # save results
if __name__ == "__main__":
main()