[6ac965]: / src / iterpretability / utils.py

Download this file

187 lines (162 with data), 5.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# stdlib
import random
from typing import Optional
# third party
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import torch
from matplotlib.lines import Line2D
from sklearn.metrics import mean_squared_error
abbrev_dict = {
"shapley_value_sampling": "SVS",
"integrated_gradients": "IG",
"kernel_shap": "SHAP",
"gradient_shap": "GSHAP",
"feature_permutation": "FP",
"feature_ablation": "FA",
"deeplift": "DL",
"lime": "LIME",
}
explainer_symbols = {
"shapley_value_sampling": "D",
"integrated_gradients": "8",
"kernel_shap": "s",
"feature_permutation": "<",
"feature_ablation": "x",
"deeplift": "H",
"lime": ">",
}
cblind_palete = sns.color_palette("colorblind", as_cmap=True)
learner_colors = {
"SLearner": cblind_palete[0],
"TLearner": cblind_palete[1],
"TARNet": cblind_palete[3],
"CFRNet_0.01": cblind_palete[4],
"CFRNet_0.001": cblind_palete[6],
"CFRNet_0.0001": cblind_palete[7],
"DRLearner": cblind_palete[8],
"XLearner": cblind_palete[5],
"Truth": cblind_palete[9],
}
def enable_reproducible_results(seed: int = 42) -> None:
"""
Set a fixed seed for all the used libraries
Args:
seed: int
The seed to use
"""
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
def dataframe_line_plot(
df: pd.DataFrame,
x_axis: str,
y_axis: str,
explainers: list,
learners: list,
x_logscale: bool = True,
aggregate: bool = False,
aggregate_type: str = "mean",
) -> plt.Figure:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
sns.set_style("white")
for learner_name in learners:
for explainer_name in explainers:
sub_df = df.loc[
(df["Learner"] == learner_name) & (df["Explainer"] == explainer_name)
]
if aggregate:
sub_df = sub_df.groupby(x_axis).agg(aggregate_type).reset_index()
x_values = sub_df.loc[:, x_axis].values
y_values = sub_df.loc[:, y_axis].values
ax.plot(
x_values,
y_values,
color=learner_colors[learner_name],
marker=explainer_symbols[explainer_name],
)
learner_lines = [
Line2D([0], [0], color=learner_colors[learner_name], lw=2)
for learner_name in learners
]
explainer_lines = [
Line2D([0], [0], color="black", marker=explainer_symbols[explainer_name])
for explainer_name in explainers
]
legend_learners = plt.legend(
learner_lines, learners, loc="lower left", bbox_to_anchor=(1.04, 0.7)
)
legend_explainers = plt.legend(
explainer_lines,
[abbrev_dict[explainer_name] for explainer_name in explainers],
loc="lower left",
bbox_to_anchor=(1.04, 0),
)
plt.subplots_adjust(right=0.75)
ax.add_artist(legend_learners)
ax.add_artist(legend_explainers)
if x_logscale:
ax.set_xscale("log")
ax.set_xlabel(x_axis)
ax.set_ylabel(y_axis)
return fig
def compute_pehe(
cate_true: np.ndarray,
cate_pred: torch.Tensor,
) -> tuple:
pehe = np.sqrt(mean_squared_error(cate_true, cate_pred.detach().cpu().numpy()))
return pehe
def compute_cate_metrics(
cate_true: np.ndarray,
y_true: np.ndarray,
w_true: np.ndarray,
mu0_pred: torch.Tensor,
mu1_pred: torch.Tensor,
) -> tuple:
mu0_pred = mu0_pred.detach().cpu().numpy()
mu1_pred = mu1_pred.detach().cpu().numpy()
cate_pred = mu1_pred - mu0_pred
pehe = np.sqrt(mean_squared_error(cate_true, cate_pred))
y_pred = w_true.reshape(len(cate_true),) * mu1_pred.reshape(len(cate_true),) + (
1
- w_true.reshape(
len(cate_true),
)
) * mu0_pred.reshape(
len(cate_true),
)
factual_rmse = np.sqrt(
mean_squared_error(
y_true.reshape(
len(cate_true),
),
y_pred,
)
)
return pehe, factual_rmse
def attribution_accuracy(
target_features: list, feature_attributions: np.ndarray
) -> float:
"""
Computes the fraction of the most important features that are truly important
Args:
target_features: list of truly important feature indices
feature_attributions: feature attribution outputted by a feature importance method
Returns:
Fraction of the most important features that are truly important
"""
if target_features is None:
return -1
n_important = len(target_features) # Number of features that are important
largest_attribution_idx = torch.topk(
torch.from_numpy(feature_attributions), n_important
)[
1
] # Features with largest attribution
accuracy = 0 # Attribution accuracy
for k in range(len(largest_attribution_idx)):
accuracy += len(np.intersect1d(largest_attribution_idx[k], target_features))
return accuracy / (len(feature_attributions) * n_important)