forked from sangjoon-park/Medical_X-VL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_shot_error.py
222 lines (176 loc) · 8.01 KB
/
zero_shot_error.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
217
218
219
220
221
222
import os
import numpy as np
import pandas as pd
from pathlib import Path
from typing import List, Tuple, Optional
import sys
sys.path.append('../../')
from eval_zero import evaluate, bootstrap
from zero_error_mimic import make, make_true_labels, run_softmax_eval
from sklearn.metrics import roc_auc_score
from sklearn.utils import resample
def compute_cis(data, confidence_level=0.01):
"""
FUNCTION: compute_cis
------------------------------------------------------
Given a Pandas dataframe of (n, labels), return another
Pandas dataframe that is (3, labels).
Each row is lower bound, mean, upper bound of a confidence
interval with `confidence`.
Args:
* data - Pandas Dataframe, of shape (num_bootstrap_samples, num_labels)
* confidence_level (optional) - confidence level of interval
Returns:
* Pandas Dataframe, of shape (3, labels), representing mean, lower, upper
"""
data_columns = list(data)
intervals = []
for i in data_columns:
series = data[i]
sorted_perfs = series.sort_values()
lower_index = int(confidence_level / 2 * len(sorted_perfs)) - 1
upper_index = int((1 - confidence_level / 2) * len(sorted_perfs)) - 1
lower = sorted_perfs.iloc[lower_index].round(4)
upper = sorted_perfs.iloc[upper_index].round(4)
mean = round(sorted_perfs.mean(), 4)
interval = pd.DataFrame({i: [mean, lower, upper]})
intervals.append(interval)
intervals_df = pd.concat(intervals, axis=1)
intervals_df.index = ['mean', 'lower', 'upper']
return intervals_df
# ----- DIRECTORIES ------ #
report_filepath: str = './data/openi/Test_selected.jsonl'
cxr_true_labels_path: Optional[
str] = '/home/depecher/PycharmProjects/CheXzero/data/groundtruth.csv' # (optional for evaluation) if labels are provided, provide path
model_dir: str = '/COVID_8TB/sangjoon/chexzero_checkpoint/FINAL_no_sim/best_4/' # where pretrained models are saved (.pt)
predictions_dir: Path = Path('/home/depecher/PycharmProjects/CheXzero/predictions') # where to save predictions
cache_dir: str = predictions_dir / "cached" # where to cache ensembled predictions
context_length: int = 120
# ------- LABELS ------ #
# Define labels to query each image | will return a prediction for each label
# cxr_labels: List[str] = ['Atelectasis', 'Cardiomegaly',
# 'Consolidation', 'Edema', 'Enlarged Cardiomediastinum', 'Fracture', 'Lung Lesion',
# 'Lung Opacity', 'No Finding', 'Pleural Effusion', 'Pleural Other', 'Pneumonia',
# 'Pneumothorax', 'Support Devices']
# cxr_labels: List[str] = ['Atelectasis', 'Cardiomegaly', 'Edema', 'Fracture', 'Pleural Effusion', 'Pneumonia', 'Pneumothorax']
# ---- TEMPLATES ----- #
# Define set of templates | see Figure 1 for more details
# cxr_pair_template: Tuple[str] = ('{}', 'no {}')
# pos_query = ['Findings consistent with pneumonia', 'Findings suggesting pneumonia',
# 'This opacity can represent pneumonia', 'Findings are most compatible with pneumonia']
# neg_query = ['There is no pneumonia', 'No evidence of pneumonia',
# 'No evidence of acute pneumonia', 'No signs of pneumonia']
# ----- MODEL PATHS ------ #
# If using ensemble, collect all model paths
model_paths = []
for subdir, dirs, files in os.walk(model_dir):
for file in files:
full_dir = os.path.join(subdir, file)
model_paths.append(full_dir)
print(model_paths)
# %% md
## Run Inference
# %%
## Run the model on the data set using ensembled models
def ensemble_models(
model_paths: List[str],
report_filepath: str,
) -> Tuple[List[np.ndarray], np.ndarray]:
"""
Given a list of `model_paths`, ensemble model and return
predictions. Caches predictions at `cache_dir` if location provided.
Returns a list of each model's predictions and the averaged
set of predictions.
"""
predictions = []
model_paths = sorted(model_paths) # ensure consistency of
for path in model_paths: # for each model
model_name = Path(path).stem
# load in model and `torch.DataLoader`
model, loader = make(
model_path=path,
report_filepath=report_filepath
)
# # path to the cached prediction
# if cache_dir is not None:
# if save_name is not None:
# cache_path = Path(cache_dir) / f"{save_name}_{model_name}.npy"
# else:
# cache_path = Path(cache_dir) / f"{model_name}.npy"
# # if prediction already cached, don't recompute prediction
# if cache_dir is not None and os.path.exists(cache_path):
# print("Loading cached prediction for {}".format(model_name))
# y_pred = np.load(cache_path)
# else: # cached prediction not found, compute preds
print("Inferring model {}".format(path))
# test_true = make_true_labels(cxr_true_labels_path=cxr_true_labels_path, cxr_labels=cxr_labels)
y_pred, y_true = run_softmax_eval(model, loader)
# if cache_dir is not None:
# Path(cache_dir).mkdir(exist_ok=True, parents=True)
# np.save(file=cache_path, arr=y_pred)
predictions.append(y_pred)
# compute average predictions
y_pred_avg = np.mean(predictions, axis=0)
return predictions, y_pred_avg, y_true
# %%
predictions, y_pred_avg, test_true = ensemble_models(
model_paths=model_paths,
report_filepath=report_filepath,
)
# %%
# save averaged preds
pred_name = "chexpert_preds.npy" # add name of preds
predictions_dir = predictions_dir / pred_name
np.save(file=predictions_dir, arr=y_pred_avg)
# cxr_labels: List[str] = ['Atelectasis', 'Cardiomegaly', 'Edema', 'Fracture', 'Pleural Effusion', 'Pneumonia', 'Pneumothorax']
cxr_labels: List[str] = ['Error']
# make test_true
test_pred = y_pred_avg
# test_true = make_true_labels(cxr_true_labels_path=cxr_true_labels_path, cxr_labels=cxr_labels)
# evaluate model
# cxr_results = roc_auc_score()
auc = roc_auc_score(test_true, test_pred)
print(auc)
def bootstrap(y_pred, y_true, n_samples=1000, label_idx_map=None):
'''
This function will randomly sample with replacement
from y_pred and y_true then evaluate `n` times
and obtain AUROC scores for each.
You can specify the number of samples that should be
used with the `n_samples` parameter.
Confidence intervals will be generated from each
of the samples.
Note:
* n_total_labels >= n_cxr_labels
`n_total_labels` is greater iff alternative labels are being tested
'''
np.random.seed(97)
y_pred # (500, n_total_labels)
y_true # (500, n_cxr_labels)
idx = np.arange(len(y_true))
boot_stats = []
for i in range(n_samples):
sample = resample(idx, replace=True, random_state=i)
y_pred_sample = y_pred[sample]
y_true_sample = y_true[sample]
sample_stats = evaluate(y_pred_sample, y_true_sample, cxr_labels)
boot_stats.append(sample_stats)
boot_stats = pd.concat(boot_stats) # pandas array of evaluations for each sample
return boot_stats, compute_cis(boot_stats)
# boostrap evaluations for 95% confidence intervals
bootstrap_results = bootstrap(test_pred, test_true)
Error_auc = bootstrap_results[1]['Error_auc']
# %%
# display AUC with confidence intervals
# Atelectasis_auc = bootstrap_results[1]['Atelectasis_auc']['mean']
# Cardiomegaly_auc = bootstrap_results[1]['Cardiomegaly_auc']['mean']
# # Consolidation_auc = bootstrap_results[1]['Consolidation_auc']['mean']
# Edema_auc = bootstrap_results[1]['Edema_auc']['mean']
# Effusion_auc = bootstrap_results[1]['Pleural Effusion_auc']['mean']
# Fracture_auc = bootstrap_results[1]['Fracture_auc']['mean']
# Pneumonia_auc = bootstrap_results[1]['Pneumonia_auc']['mean']
# Pneumothorax_auc = bootstrap_results[1]['Pneumothorax_auc']['mean']
# Mean_auc = (Atelectasis_auc + Cardiomegaly_auc + Edema_auc + Effusion_auc + Fracture_auc + Pneumonia_auc + Pneumothorax_auc) / 7.
# print('Model name: {} / Mean AUC: {}'.format(model_dir, Mean_auc))
bootstrap_results[1]
# %%