forked from scotthlee/hamlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ms_tables.py
211 lines (192 loc) · 7.56 KB
/
ms_tables.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
import pandas as pd
import numpy as np
import pickle
from hamlet.tools.generic import crosstab, vartab
# Read in the full dataset
data_dir = '~/OneDrive - CDC/Documents/projects/hamlet/'
df = pd.read_csv(data_dir + 'samp.csv')
# Making age group variables
df['age_group'] = pd.cut(df.age_years,
bins=[15, 25, 35, 45, 55, 65, 99],
labels=['15-24', '25-34', '35-44',
'45-54', '55-64', '>=65'])
df['age_group'] = df.age_group.astype(str)
# Making geographic region and subregion variables
countries = pd.read_csv(data_dir + 'countries.csv')
countries['upper_name'] = countries.name.str.upper()
region_dict = dict(zip(countries.upper_name, countries.region))
sub_dict = dict(zip(countries.upper_name, countries['sub-region']))
region_dict.update({'nan': 'NA'})
sub_dict.update({'nan': 'NA'})
exams = df.exam_country.str.upper().astype(str)
exams.replace('VIETNAM', 'VIET NAM', inplace=True)
exams.replace('RUSSIA', 'RUSSIAN FEDERATION', inplace=True)
exams.replace('BOSNIA AND HERCEGOVINA',
'BOSNIA AND HERZEGOVINA',
inplace=True)
exams.replace('UNITED KINGDOM',
'UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND',
inplace=True)
exams.replace('MOLDOVA', 'MOLDOVA, REPUBLIC OF', inplace=True)
exams.replace('WEST BANK', 'PALESTINE, STATE OF', inplace=True)
exams.replace('TAIWAN', 'TAIWAN, PROVINCE OF CHINA', inplace=True)
exams.replace('CONGO, DEMOCRATIC REP OF',
'CONGO, DEMOCRATIC REPUBLIC OF THE',
inplace=True)
exams.replace('IVORY COAST', "CÔTE D'IVOIRE", inplace=True)
exams.replace('CAPE VERDE', 'CABO VERDE', inplace=True)
exams.replace('SLOVAK REPUBLIC', 'SLOVAKIA', inplace=True)
exams.replace('KOSOVO', 'SERBIA', inplace=True)
exams.replace('TANZANIA, UNITED REP OF',
'TANZANIA, UNITED REPUBLIC OF',
inplace=True)
exams.replace('LAOS',
"LAO PEOPLE'S DEMOCRATIC REPUBLIC",
inplace=True)
exams.replace('GAMBIA, THE', 'GAMBIA', inplace=True)
exams.replace('BOLIVIA', 'BOLIVIA (PLURINATIONAL STATE OF)', inplace=True)
exams.replace('CONGO, REPUBLIC OF', 'CONGO', inplace=True)
exams = exams.values
df['exam_region'] = [region_dict[c] for c in exams]
df['exam_subregion'] = [sub_dict[c] for c in exams]
# Making summary variables for TB testing
smears = df[['smear_1', 'smear_2', 'smear_3']].sum(1) > 0
df['smear'] = smears.astype(np.uint8)
cultures = df[['culture_1', 'culture_2', 'culture_3']].sum(1) > 0
df['culture'] = cultures.astype(np.uint8)
df['tb_now'] = df.class_a.fillna(0)
df['tb_prior'] = np.array(df.smear + df.culture > 0, dtype=np.uint8)
# Adding a variable for the data source
source = np.array([''] * df.shape[0], dtype='U16')
ids = df.id.values.astype(str)
source[np.where(['iom_' in s for s in ids])[0]] = 'IOM Telerad QC'
source[np.where(['ref_' in s for s in ids])[0]] = 'MiMOSA'
source[np.where(['pan_' in s for s in ids])[0]] = 'Panels'
source[np.where(['im_' in s for s in ids])[0]] = 'eMedical'
df['source'] = source
# Dividing the data into splits for making separate tables
train = df[df.split == 'train']
val = df[df.split == 'val']
test = df[df.split == 'test']
splits = [df, train, val, test]
split_names = ['All', 'Train', 'Validate', 'Test']
# Making the tables, starting with demographics first
dem_tabs = []
tab_cols = [
'age_group', 'sex', 'source',
'exam_region', 'exam_subregion'
]
tab_names = [
'Age Group', 'Sex', 'Data Source',
'Exam Region', 'Exam Sub-Region'
]
var_levels = [np.unique(df[v]) for v in tab_cols]
for j, s in enumerate(splits):
tabs = []
for i, c in enumerate(tab_cols):
tab = vartab(df=s,
var=tab_cols[i],
varname=tab_names[i],
levels=var_levels[i],
use_empty=True)
tabs.append(tab)
tabs = pd.concat(tabs, axis=0)
dem_tabs.append(tabs)
dem_tabs = pd.concat(dem_tabs, axis=1)
dem_tabs.to_csv(data_dir + 'analysis/dem_tabs.csv')
# And now the TB-related table
tb_tabs = []
tab_cols = [
'abnormal', 'abnormal_tb', 'infiltrate',
'reticular', 'cavity', 'nodule',
'pleural_effusion', 'hilar_adenopathy', 'miliary',
'linear_opacity', 'discrete_nodule', 'volume_loss',
'pleural_reaction', 'other', 'tb_prior',
'tb_now',
]
tab_names = [
'Abnormal',
'Abnormal (Suggestive of TB)',
'Infiltrate or Consolidation',
'Reticular Findings',
'Cavitary Lesion',
'Nodule or Mass with Poorly Defined Margins',
'Pleural Effusion', 'Hilar/mediastinal Adenopathy',
'Miliary Findings', 'Discrete Linear Opacity',
'Discerete Nodule(s) Without Calcification',
'Volume Loss or Retraction', 'Irregular Thick Pleural Reaction',
'Other', 'TB at Prior Exam', 'TB at Current Exam'
]
var_levels = [np.unique(df[v]) for v in tab_cols]
for j, s in enumerate(splits):
tabs = [vartab(df=s,
var=tab_cols[i],
varname=tab_names[i],
levels=[1],
round=1)
for i, c in enumerate(tab_cols)]
tabs = pd.concat(tabs, axis=0)
tb_tabs.append(tabs)
tb_tabs = pd.concat(tb_tabs, axis=1)
tb_tabs.to_csv(data_dir + 'analysis/tb_tabs.csv')
# Loading the external datasets;
# TO DO: refactor generate_predictions so it can write to an existing
# set of labels.
nih = pd.read_csv(data_dir + 'output/other/nih.csv')
shen = pd.read_csv(data_dir + 'output/other/shen.csv')
mcu = pd.read_csv(data_dir + 'output/other/mcu.csv')
viet = pd.read_csv(data_dir + 'output/other/viet.csv')
ext_dfs = [nih, shen, mcu, viet]
ext_names = ['nih', 'shenzhen', 'mcu', 'vietnam']
# Making a table of AUCs for the external datasets
na_str = np.nan
ab_ab = [
auroc(nih.abnormal, nih.abnormal_prob),
na_str,
na_str,
auroc(viet.abnormal, viet.abnormal_prob)
]
ab_abtb = [
auroc(nih.abnormal, nih.abnormal_tb_prob),
na_str,
na_str,
auroc(viet.abnormal, viet.abnormal_tb_prob)
]
abtb_ab = [
na_str,
auroc(shen.abnormal, shen.abnormal_prob),
auroc(mcu.abnormal, mcu.abnormal_prob),
auroc(viet.abnormal_tb, viet.abnormal_prob)
]
abtb_abtb = [
na_str,
auroc(shen.abnormal, shen.abnormal_tb_prob),
auroc(mcu.abnormal, mcu.abnormal_tb_prob),
auroc(viet.abnormal_tb, viet.abnormal_tb_prob)
]
auc_tab = pd.DataFrame([ab_ab, ab_abtb, abtb_ab, abtb_abtb],
index=['ab/ab', 'ab/abtb',
'abtb/ab', 'abtb/abtb'],
columns=ext_names).transpose()
auc_tab = (auc_tab * 100).round(2)
auc_tab.to_csv(data_dir + 'analysis/tables/ext_aucs.csv')
# Making a table of specs at 70% sens for the external TB datasets
abtb_ab = pd.concat([
metrics.spec_at_sens(test.abnormal_tb, test.abnormal_prob),
metrics.spec_at_sens(shen.abnormal, shen.abnormal_prob),
metrics.spec_at_sens(mcu.abnormal, mcu.abnormal_prob),
metrics.spec_at_sens(viet.abnormal_tb, viet.abnormal_prob)
]).reset_index(drop=True)
abtb_ab.index = ['hamlet'] + ext_names[1:]
abtb_abtb = pd.concat([
metrics.spec_at_sens(test.abnormal_tb, test.abnormal_tb_prob),
metrics.spec_at_sens(shen.abnormal, shen.abnormal_tb_prob),
metrics.spec_at_sens(mcu.abnormal, mcu.abnormal_tb_prob),
metrics.spec_at_sens(viet.abnormal_tb, viet.abnormal_tb_prob)
]).reset_index(drop=True)
abtb_abtb.index = ['hamlet'] + ext_names[1:]
sens90 = pd.concat([abtb_ab, abtb_abtb], axis=1)
sens90.to_csv(data_dir + 'analysis/tables/sens90.csv')
# Tables 2 and 3 -- CIs for the binary and multilabel tasks
ham_cis = pickle.load(open(data_dir + 'ham_cis.pkl', 'rb'))
ext_cis = pickle.load(open(data_dir + 'ext_cis.pkl', 'rb'))