File size: 9,758 Bytes
9e9cca9 |
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import logging
import math
import pickle
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import torch
from datasets.utils.logging import disable_progress_bar, enable_progress_bar
from sklearn import preprocessing
from sklearn.metrics import (
ConfusionMatrixDisplay,
accuracy_score,
auc,
confusion_matrix,
f1_score,
roc_curve,
)
from tqdm.auto import trange
from .emb_extractor import make_colorbar
from .tokenizer import TOKEN_DICTIONARY_FILE
logger = logging.getLogger(__name__)
# load token dictionary (Ensembl IDs:token)
with open(TOKEN_DICTIONARY_FILE, "rb") as f:
gene_token_dict = pickle.load(f)
def preprocess_classifier_batch(cell_batch, max_len, label_name):
if max_len is None:
max_len = max([len(i) for i in cell_batch["input_ids"]])
def pad_label_example(example):
example[label_name] = np.pad(
example[label_name],
(0, max_len - len(example["input_ids"])),
mode="constant",
constant_values=-100,
)
example["input_ids"] = np.pad(
example["input_ids"],
(0, max_len - len(example["input_ids"])),
mode="constant",
constant_values=gene_token_dict.get("<pad>"),
)
example["attention_mask"] = (
example["input_ids"] != gene_token_dict.get("<pad>")
).astype(int)
return example
padded_batch = cell_batch.map(pad_label_example)
return padded_batch
# Function to find the largest number smaller
# than or equal to N that is divisible by k
def find_largest_div(N, K):
rem = N % K
if rem == 0:
return N
else:
return N - rem
def vote(logit_list):
m = max(logit_list)
logit_list.index(m)
indices = [i for i, x in enumerate(logit_list) if x == m]
if len(indices) > 1:
return "tie"
else:
return indices[0]
def py_softmax(vector):
e = np.exp(vector)
return e / e.sum()
def classifier_predict(model, classifier_type, evalset, forward_batch_size):
if classifier_type == "gene":
label_name = "labels"
elif classifier_type == "cell":
label_name = "label"
predict_logits = []
predict_labels = []
model.eval()
# ensure there is at least 2 examples in each batch to avoid incorrect tensor dims
evalset_len = len(evalset)
max_divisible = find_largest_div(evalset_len, forward_batch_size)
if len(evalset) - max_divisible == 1:
evalset_len = max_divisible
max_evalset_len = max(evalset.select([i for i in range(evalset_len)])["length"])
disable_progress_bar() # disable progress bar for preprocess_classifier_batch mapping
for i in trange(0, evalset_len, forward_batch_size):
max_range = min(i + forward_batch_size, evalset_len)
batch_evalset = evalset.select([i for i in range(i, max_range)])
padded_batch = preprocess_classifier_batch(
batch_evalset, max_evalset_len, label_name
)
padded_batch.set_format(type="torch")
input_data_batch = padded_batch["input_ids"]
attn_msk_batch = padded_batch["attention_mask"]
label_batch = padded_batch[label_name]
with torch.no_grad():
outputs = model(
input_ids=input_data_batch.to("cuda"),
attention_mask=attn_msk_batch.to("cuda"),
labels=label_batch.to("cuda"),
)
predict_logits += [torch.squeeze(outputs.logits.to("cpu"))]
predict_labels += [torch.squeeze(label_batch.to("cpu"))]
enable_progress_bar()
logits_by_cell = torch.cat(predict_logits)
last_dim = len(logits_by_cell.shape) - 1
all_logits = logits_by_cell.reshape(-1, logits_by_cell.shape[last_dim])
labels_by_cell = torch.cat(predict_labels)
all_labels = torch.flatten(labels_by_cell)
logit_label_paired = [
item
for item in list(zip(all_logits.tolist(), all_labels.tolist()))
if item[1] != -100
]
y_pred = [vote(item[0]) for item in logit_label_paired]
y_true = [item[1] for item in logit_label_paired]
logits_list = [item[0] for item in logit_label_paired]
return y_pred, y_true, logits_list
def get_metrics(y_pred, y_true, logits_list, num_classes, labels):
conf_mat = confusion_matrix(y_true, y_pred, labels=list(labels))
macro_f1 = f1_score(y_true, y_pred, average="macro")
acc = accuracy_score(y_true, y_pred)
roc_metrics = None # roc metrics not reported for multiclass
if num_classes == 2:
y_score = [py_softmax(item)[1] for item in logits_list]
fpr, tpr, _ = roc_curve(y_true, y_score)
mean_fpr = np.linspace(0, 1, 100)
interp_tpr = np.interp(mean_fpr, fpr, tpr)
interp_tpr[0] = 0.0
tpr_wt = len(tpr)
roc_auc = auc(fpr, tpr)
roc_metrics = {
"fpr": fpr,
"tpr": tpr,
"interp_tpr": interp_tpr,
"auc": roc_auc,
"tpr_wt": tpr_wt,
}
return conf_mat, macro_f1, acc, roc_metrics
# get cross-validated mean and sd metrics
def get_cross_valid_roc_metrics(all_tpr, all_roc_auc, all_tpr_wt):
wts = [count / sum(all_tpr_wt) for count in all_tpr_wt]
all_weighted_tpr = [a * b for a, b in zip(all_tpr, wts)]
mean_tpr = np.sum(all_weighted_tpr, axis=0)
mean_tpr[-1] = 1.0
all_weighted_roc_auc = [a * b for a, b in zip(all_roc_auc, wts)]
roc_auc = np.sum(all_weighted_roc_auc)
roc_auc_sd = math.sqrt(np.average((all_roc_auc - roc_auc) ** 2, weights=wts))
return mean_tpr, roc_auc, roc_auc_sd
# plot ROC curve
def plot_ROC(roc_metric_dict, model_style_dict, title, output_dir, output_prefix):
fig = plt.figure()
fig.set_size_inches(10, 8)
sns.set(font_scale=2)
sns.set_style("white")
lw = 3
for model_name in roc_metric_dict.keys():
mean_fpr = roc_metric_dict[model_name]["mean_fpr"]
mean_tpr = roc_metric_dict[model_name]["mean_tpr"]
roc_auc = roc_metric_dict[model_name]["roc_auc"]
roc_auc_sd = roc_metric_dict[model_name]["roc_auc_sd"]
color = model_style_dict[model_name]["color"]
linestyle = model_style_dict[model_name]["linestyle"]
if len(roc_metric_dict[model_name]["all_roc_auc"]) > 1:
label = f"{model_name} (AUC {roc_auc:0.2f} $\pm$ {roc_auc_sd:0.2f})"
else:
label = f"{model_name} (AUC {roc_auc:0.2f})"
plt.plot(
mean_fpr, mean_tpr, color=color, linestyle=linestyle, lw=lw, label=label
)
plt.plot([0, 1], [0, 1], color="black", lw=lw, linestyle="--")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title(title)
plt.legend(loc="lower right")
plt.show()
output_file = (Path(output_dir) / f"{output_prefix}_roc").with_suffix(".pdf")
plt.savefig(output_file, bbox_inches="tight")
# plot confusion matrix
def plot_confusion_matrix(
conf_mat_df, title, output_dir, output_prefix, custom_class_order
):
fig = plt.figure()
fig.set_size_inches(10, 10)
sns.set(font_scale=1)
sns.set_style("whitegrid", {"axes.grid": False})
if custom_class_order is not None:
conf_mat_df = conf_mat_df.reindex(
index=custom_class_order, columns=custom_class_order
)
display_labels = generate_display_labels(conf_mat_df)
conf_mat = preprocessing.normalize(conf_mat_df.to_numpy(), norm="l1")
display = ConfusionMatrixDisplay(
confusion_matrix=conf_mat, display_labels=display_labels
)
display.plot(cmap="Blues", values_format=".2g")
plt.title(title)
plt.show()
output_file = (Path(output_dir) / f"{output_prefix}_conf_mat").with_suffix(".pdf")
display.figure_.savefig(output_file, bbox_inches="tight")
def generate_display_labels(conf_mat_df):
display_labels = []
i = 0
for label in conf_mat_df.index:
display_labels += [f"{label}\nn={conf_mat_df.iloc[i,:].sum():.0f}"]
i = i + 1
return display_labels
def plot_predictions(predictions_df, title, output_dir, output_prefix, kwargs_dict):
sns.set(font_scale=2)
plt.figure(figsize=(10, 10), dpi=150)
label_colors, label_color_dict = make_colorbar(predictions_df, "true")
predictions_df = predictions_df.drop(columns=["true"])
predict_colors_list = [label_color_dict[label] for label in predictions_df.columns]
predict_label_list = [label for label in predictions_df.columns]
predict_colors = pd.DataFrame(
pd.Series(predict_colors_list, index=predict_label_list), columns=["predicted"]
)
default_kwargs_dict = {
"row_cluster": False,
"col_cluster": False,
"row_colors": label_colors,
"col_colors": predict_colors,
"linewidths": 0,
"xticklabels": False,
"yticklabels": False,
"center": 0,
"cmap": "vlag",
}
if kwargs_dict is not None:
default_kwargs_dict.update(kwargs_dict)
g = sns.clustermap(predictions_df, **default_kwargs_dict)
plt.setp(g.ax_row_colors.get_xmajorticklabels(), rotation=45, ha="right")
for label_color in list(label_color_dict.keys()):
g.ax_col_dendrogram.bar(
0, 0, color=label_color_dict[label_color], label=label_color, linewidth=0
)
g.ax_col_dendrogram.legend(
title=f"{title}",
loc="lower center",
ncol=4,
bbox_to_anchor=(0.5, 1),
facecolor="white",
)
output_file = (Path(output_dir) / f"{output_prefix}_pred").with_suffix(".pdf")
plt.savefig(output_file, bbox_inches="tight")
|