-
Notifications
You must be signed in to change notification settings - Fork 27
/
3_3-Shapley Values Calculation.py
216 lines (173 loc) · 9.22 KB
/
3_3-Shapley Values Calculation.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
###########################################################################################################
# Shapley Values Calculation
###########################################################################################################
#
# Licensed under the Apache License, Version 2.0**
# You may not use this file except in compliance with the License. You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing permissions and limitations under the License.
#-> Authors:
# Luis R Soenksen (<[email protected]>),
# Yu Ma (<[email protected]>),
# Cynthia Zeng (<[email protected]>),
# Leonard David Jean Boussioux (<[email protected]>),
# Kimberly M Villalobos Carballo (<[email protected]>),
# Liangyuan Na (<[email protected]>),
# Holly Mika Wiberg (<[email protected]>),
# Michael Lingzhi Li (<[email protected]>),
# Ignacio Fuentes (<[email protected]>),
# Dimitris J Bertsimas (<[email protected]>),
# -> Last Update: Dec 30th, 2021
import pandas as pd
import ast
from itertools import combinations
from math import comb
los_auc = pd.read_csv("AUC_All_los_pred.csv")
los_auc.txt = [1 if 'n_rad' in x else txt for x,txt in zip(los_auc.Sources,los_auc.txt)]
source_auc_dict = {}
for source, auc in zip(los_auc.Sources, los_auc.AUC_mean):
source_sorted = tuple(sorted(ast.literal_eval(source)))
source_auc_dict[source_sorted] = auc
source_auc_dict[()] = 0.5
sources = ['de', 'vd', 'vp', 'vmd', 'vmp', 'ts_ce', 'ts_le', 'ts_pe', 'n_ecg', 'n_ech', 'n_rad']
def powerset(iterable,missing_el):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
xs = list(iterable)
return [tuple(sorted(list(subset))) for i in range(0, len(xs) + 1) for subset in combinations(xs, i) if missing_el not in subset]
source_shap_dict = {}
for source in sources:
source_shap = 0
total_sets = powerset(sources, source)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(source)
source_shap += 1 / (len(sources) * comb(len(sources)-1, len(single_set))) * (source_auc_dict[tuple(sorted(single_list))]-source_auc_dict[single_set])
source_shap_dict[source] = source_shap
shap_values_sources = pd.DataFrame(source_shap_dict.items(), columns=['Source', 'Gain'])
shap_values_sources.to_csv("shap_vals_sources_los.csv")
los_auc_types = los_auc[['AUC_mean', 'viz','txt','tab','ts']].groupby(['viz','txt','tab','ts']).mean().reset_index()
type_auc_dict = {}
colnames = list(los_auc_types.columns.values)
for row_count, row in los_auc_types.iterrows():
set_comb = []
for i in range(4):
if row[i] == 1:
set_comb.append(colnames[i])
type_auc_dict[tuple(sorted(set_comb))] = row[4]
type_auc_dict[()] = 0.5
types = ['viz','txt','tab','ts']
types_shap_dict = {}
for tpe in types:
type_shap = 0
total_sets = powerset(types, tpe)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(tpe)
type_shap += 1 / (len(types) * comb(len(types)-1, len(single_set))) * (type_auc_dict[tuple(sorted(single_list))]-type_auc_dict[single_set])
types_shap_dict[tpe] = type_shap
shap_values_types = pd.DataFrame(types_shap_dict.items(), columns=['Type', 'Gain'])
shap_values_types.to_csv("shap_vals_types_los.csv")
mortality_auc = pd.read_csv("AUC_All_mortality_pred.csv")
mortality_auc.txt = [1 if 'n_rad' in x else txt for x,txt in zip(mortality_auc.Sources,mortality_auc.txt)]
source_auc_dict = {}
for source, auc in zip(mortality_auc.Sources, mortality_auc.AUC_mean):
source_sorted = tuple(sorted(ast.literal_eval(source)))
source_auc_dict[source_sorted] = auc
source_auc_dict[()] = 0.5
sources = ['demo', 'vd', 'vp', 'vmd', 'vmp', 'ts_ce', 'ts_le', 'ts_pe', 'n_ecg', 'n_ech', 'n_rad']
def powerset(iterable,missing_el):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
xs = list(iterable)
return [tuple(sorted(list(subset))) for i in range(0, len(xs) + 1) for subset in combinations(xs, i) if missing_el not in subset]
source_shap_dict = {}
for source in sources:
source_shap = 0
total_sets = powerset(sources, source)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(source)
source_shap += 1 / (len(sources) * comb(len(sources)-1, len(single_set))) * (source_auc_dict[tuple(sorted(single_list))]-source_auc_dict[single_set])
source_shap_dict[source] = source_shap
shap_values_sources = pd.DataFrame(source_shap_dict.items(), columns=['Source', 'Gain'])
shap_values_sources.to_csv("shap_vals_sources_mortality.csv")
mortality_auc_types = mortality_auc[['AUC_mean', 'viz','txt','tab','ts']].groupby(['viz','txt','tab','ts']).mean().reset_index()
type_auc_dict = {}
colnames = list(mortality_auc_types.columns.values)
for row_count, row in mortality_auc_types.iterrows():
set_comb = []
for i in range(4):
if row[i] == 1:
set_comb.append(colnames[i])
type_auc_dict[tuple(sorted(set_comb))] = row[4]
type_auc_dict[()] = 0.5
types = ['viz','txt','tab','ts']
types_shap_dict = {}
for tpe in types:
type_shap = 0
total_sets = powerset(types, tpe)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(tpe)
type_shap += 1 / (len(types) * comb(len(types)-1, len(single_set))) * (type_auc_dict[tuple(sorted(single_list))]-type_auc_dict[single_set])
types_shap_dict[tpe] = type_shap
shap_values_types = pd.DataFrame(types_shap_dict.items(), columns=['Type', 'Gain'])
shap_values_types.to_csv("shap_vals_types_mortality.csv")
all_modality_auc = pd.read_csv("AUC_All_Modality_Sources.csv")
diseases = ['Atelectasis','Cardiomegaly','Consolidation','Edema','Enlarged Cardiomediastinum','Fracture','Lung Lesion', 'Lung Opacity', 'Pneumonia','Pneumothorax']
shap_values_sources_diseases = []
shap_values_types_diseases = []
for disease in diseases:
source_auc_dict = {}
for source, auc in zip(all_modality_auc.Sources, all_modality_auc[disease]):
source_sorted = tuple(sorted(ast.literal_eval(source)))
source_auc_dict[source_sorted] = auc
source_auc_dict[()] = 0.5
sources = ['de', 'vd', 'vp', 'vmd', 'vmp', 'ts_ce', 'ts_le', 'ts_pe', 'n_ecg', 'n_ech']
def powerset(iterable,missing_el):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
xs = list(iterable)
return [tuple(sorted(list(subset))) for i in range(0, len(xs) + 1) for subset in combinations(xs, i) if missing_el not in subset]
source_shap_dict = {}
for source in sources:
source_shap = 0
total_sets = powerset(sources, source)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(source)
source_shap += 1 / (len(sources) * comb(len(sources)-1, len(single_set))) * (source_auc_dict[tuple(sorted(single_list))]-source_auc_dict[single_set])
source_shap_dict[source] = source_shap
column_naming = 'Gain_' + disease
shap_values_sources = pd.DataFrame(source_shap_dict.items(), columns=['Source', column_naming])
shap_values_sources = shap_values_sources.set_index('Source')
shap_values_sources_diseases.append(shap_values_sources)
selected_columns = ['viz','txt','tab','ts']
selected_columns.append(disease)
mortality_auc_types = all_modality_auc[selected_columns].groupby(['viz','txt','tab','ts']).mean().reset_index()
type_auc_dict = {}
colnames = list(mortality_auc_types.columns.values)
for row_count, row in mortality_auc_types.iterrows():
set_comb = []
for i in range(4):
if row[i] == 1:
set_comb.append(colnames[i])
type_auc_dict[tuple(sorted(set_comb))] = row[4]
type_auc_dict[()] = 0.5
types = ['viz','txt','tab','ts']
types_shap_dict = {}
for tpe in types:
type_shap = 0
total_sets = powerset(types, tpe)
for single_set in total_sets:
single_list = list(single_set)
single_list.append(tpe)
type_shap += 1 / (len(types) * comb(len(types)-1, len(single_set))) * (type_auc_dict[tuple(sorted(single_list))]-type_auc_dict[single_set])
types_shap_dict[tpe] = type_shap
shap_values_types = pd.DataFrame(types_shap_dict.items(), columns=['Type', column_naming])
shap_values_types = shap_values_types.set_index('Type')
shap_values_types_diseases.append(shap_values_types)
shap_values_sources_diseases_output = pd.concat(shap_values_sources_diseases, axis=1)
shap_values_types_diseases_output = pd.concat(shap_values_types_diseases, axis=1)
shap_values_sources_diseases_output.to_csv("shap_vals_sources_diseases.csv")
shap_values_types_diseases_output.to_csv("shap_vals_types_diseases.csv")