[66326d]: / src / utils / plotting.py

Download this file

584 lines (456 with data), 16.0 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
"""
Functions for plotting results and descriptive analysis of data.
"""
import time
import json
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pathlib import Path
from datetime import datetime
from collections import defaultdict
ROOT_DIR = Path(__file__).parents[2]
RESULTS_DIR = ROOT_DIR / "results"
METRIC_FULL_NAME = {
"Top1_Acc": "Accuracy",
"BalAcc": "Balanced Accuracy",
"Loss": "Loss",
}
STRATEGY_CATEGORY = {
"Naive": "Baseline",
"Cumulative": "Baseline",
"EWC": "Regularization",
"OnlineEWC": "Regularization",
"SI": "Regularization",
"LwF": "Regularization",
"Replay": "Rehearsal",
"GEM": "Rehearsal",
"AGEM": "Rehearsal",
"GDumb": "Rehearsal",
}
STRATEGY_COLOURS = {
"Naive": "dodgerblue",
"Cumulative": "deepskyblue",
"EWC": "orange",
"OnlineEWC": "gold",
"SI": "tomato",
"LwF": "peru",
"Replay": "forestgreen",
"GEM": "limegreen",
"AGEM": "yellowgreen",
"GDumb": "palegreen",
}
def get_timestamp():
"""
Returns current timestamp as string.
"""
ts = time.time()
return datetime.fromtimestamp(ts).strftime("%Y-%m-%d-%H-%M-%S")
###################################
# Plot figs (metrics over epoch)
###################################
def stack_results(results, metric, mode, type="experience"):
"""
Stacks results for multiple experiments along same axis in df.
Either stacks:
- multiple experiences' metric for same model/strategy, or
- multiple strategies' [avg/stream] metrics for same model
"""
results_dfs = []
# Get metrics for each training "experience"'s test set
n_repeats = len(results)
for i in range(n_repeats):
metric_dict = defaultdict(list)
for k, v in results[i].items():
if f"{metric}_Exp/eval_phase/{mode}_stream" in k:
new_k = (
k.split("/")[-1].replace("Exp00", "Task ").replace("Exp0", "Task ")
)
metric_dict[new_k] = v[1]
df = pd.DataFrame.from_dict(metric_dict)
df.index.rename("Epoch", inplace=True)
stacked = df.stack().reset_index()
stacked.rename(
columns={"level_1": "Task", 0: METRIC_FULL_NAME[metric]}, inplace=True
)
results_dfs.append(stacked)
stacked = pd.concat(results_dfs, sort=False)
return stacked
def stack_avg_results(results_strats, metric, mode):
"""
Stack avg results for multiple strategies across epoch.
"""
results_dfs = []
# Get metrics for each training "experience"'s test set
n_repeats = len(list(results_strats.values())[0])
for i in range(n_repeats):
metric_dict = defaultdict(list)
# Get avg (stream) metrics for each strategy
for strat, metrics in results_strats.items():
for k, v in metrics[i].items():
# if train stream in keys "BalancedAccuracy_On_Trained_Experiences"
if (
f"{METRIC_FULL_NAME[metric].replace(' ', '')}_On_Trained_Experiences/eval_phase/{mode}_stream"
in k
):
# JA: early stopping means uneven length arrays. Must subsample at n_tasks
metric_dict[strat] = v[1]
break
elif f"{metric}_Stream/eval_phase/{mode}_stream" in k:
metric_dict[strat] = v[1]
df = pd.DataFrame.from_dict(metric_dict)
df.index.rename("Epoch", inplace=True)
stacked = df.stack().reset_index()
stacked.rename(
columns={"level_1": "Strategy", 0: METRIC_FULL_NAME[metric]}, inplace=True
)
results_dfs.append(stacked)
stacked = pd.concat(results_dfs, sort=False)
return stacked
def plot_metric(method, model, results, mode, metric, ax=None):
"""
Plots given metric from dict.
Stacks multiple plots (i.e. different per-task metrics) over training time.
`mode`: ['train','test'] (which stream to plot)
"""
ax = ax or plt.gca()
stacked = stack_results(results, metric, mode)
# Only plot task accuracies after examples have been encountered
# JA: this len() etc will screw up when plotting CI's
tasks = stacked["Task"].str.split(" ", expand=True)[1].astype(int)
n_epochs_per_task = (stacked["Epoch"].max() + 1) // stacked["Task"].nunique()
stacked = stacked[tasks * n_epochs_per_task <= stacked["Epoch"].astype(int)]
sns.lineplot(data=stacked, x="Epoch", y=METRIC_FULL_NAME[metric], hue="Task", ax=ax)
ax.set_title(method, size=10)
ax.set_ylabel(model)
ax.set_xlabel("")
def plot_avg_metric(model, results, mode, metric, ax=None):
"""
Plots given metric from dict.
Stacks multiple plots (i.e. different strategies' metrics) over training time.
`mode`: ['train','test'] (which stream to plot)
"""
ax = ax or plt.gca()
stacked = stack_avg_results(results, metric, mode)
sns.lineplot(
data=stacked,
x="Epoch",
y=METRIC_FULL_NAME[metric],
hue="Strategy",
ax=ax,
palette=STRATEGY_COLOURS,
)
ax.set_title("Average performance over all tasks", size=10)
ax.set_ylabel(model)
ax.set_xlabel("")
def barplot_avg_metric(model, results, mode, metric, ax=None):
ax = ax or plt.gca()
stacked = stack_avg_results(results, metric, mode)
stacked = stacked[stacked["Epoch"] == stacked["Epoch"].max()]
sns.barplot(
data=stacked,
x="Strategy",
y=METRIC_FULL_NAME[metric],
ax=ax,
palette=STRATEGY_COLOURS,
)
ax.set_title("Final average performance over all tasks", size=10)
ax.set_xlabel("")
###################################
# Clean up plots
###################################
def clean_subplot(i, j, axes, metric):
"""Removes top and rights spines, titles, legend. Fixes y limits."""
ax = axes[i, j]
ax.spines[["top", "right"]].set_visible(False)
if i > 0:
ax.set_title("")
if i > 0 or j > 0:
try:
ax.get_legend().remove()
except AttributeError:
pass
if metric == "Loss":
ylim = (0, 4)
elif metric == "BalAcc":
ylim = (0.5, 1)
plt.setp(axes, ylim=ylim)
else:
ylim = (0.5, 1)
# plt.setp(axes, ylim=ylim)
def clean_plot(fig, axes, metric):
"""Cleans all subpots. Removes duplicate legends."""
for i in range(len(axes)):
for j in range(len(axes[0])):
clean_subplot(i, j, axes, metric)
handles, labels = axes[0, 0].get_legend_handles_labels()
axes[0, 0].get_legend().remove()
fig.legend(handles, labels, loc="center right", title="Task")
def annotate_plot(fig, domain, outcome, metric):
"""Adds x/y labels and suptitles."""
fig.supxlabel("Epoch")
fig.supylabel(METRIC_FULL_NAME[metric], x=0)
fig.suptitle(
f"Continual Learning model comparison \n"
f"Outcome: {outcome} | Domain Increment: {domain}",
y=1.1,
)
###################################
# Decorating functions for plotting everything
###################################
def plot_all_model_strats(data, domain, outcome, mode, metric, timestamp, savefig=True):
"""Pairplot of all models vs strategies."""
# Load results
with open(
RESULTS_DIR / f"results_{data}_{outcome}_{domain}.json", encoding="utf-8"
) as handle:
res = json.load(handle)
models = res.keys()
strategies = next(iter(res.values())).keys()
n_rows = len(models)
n_cols = len(strategies)
# Experience plots
fig, axes = plt.subplots(
n_rows,
n_cols,
sharex=True,
sharey=True,
figsize=(2 * 20 * 4 / n_cols, 20 * n_rows / n_cols),
squeeze=False,
dpi=250,
)
for i, model in enumerate(models):
for j, strategy in enumerate(strategies):
plot_metric(strategy, model, res[model][strategy], mode, metric, axes[i, j])
clean_plot(fig, axes, metric)
annotate_plot(fig, domain, outcome, metric)
if savefig:
file_loc = RESULTS_DIR / "figs" / data / outcome / domain / timestamp / mode
file_loc.mkdir(parents=True, exist_ok=True)
plt.savefig(file_loc / f"Exp_{metric}.png")
# Stream plots
fig, axes = plt.subplots(
n_rows,
2,
sharex=False,
sharey=True,
figsize=(20, 20 * n_rows / n_cols),
squeeze=False,
dpi=250,
)
for i, model in enumerate(models):
plot_avg_metric(model, res[model], mode, metric, axes[i, 0])
barplot_avg_metric(model, res[model], mode, metric, axes[i, 1])
clean_plot(fig, axes, metric)
annotate_plot(fig, domain, outcome, metric)
if savefig:
file_loc = RESULTS_DIR / "figs" / data / outcome / domain / timestamp / mode
file_loc.mkdir(parents=True, exist_ok=True)
plt.savefig(file_loc / f"Stream_{metric}.png")
def results_to_latex():
"""Returns results in LaTeX format for paper tables."""
raise NotImplementedError
def plot_all_figs(data, domain, outcome):
"""Plots all results figs for paper."""
timestamp = get_timestamp()
for mode in ["train", "test"]:
for metric in ["Loss", "Top1_Acc", "BalAcc"]:
plot_all_model_strats(data, domain, outcome, mode, metric, timestamp)
#####################
# DESCRIPTIVE PLOTS
#####################
def plot_demographics():
"""
Plots demographic information of eICU dataset.
"""
df = pd.DataFrame() # data_processing.load_eicu(drop_dupes=True)
_, axes = plt.subplots(3, 2, sharey=True, figsize=(18, 18), squeeze=False)
df["gender"].value_counts().plot.bar(ax=axes[0, 0], rot=0, title="Gender")
df["ethnicity"].value_counts().plot.bar(ax=axes[1, 0], rot=0, title="Ethnicity")
df["ethnicity_coarse"].value_counts().plot.bar(
ax=axes[1, 1], rot=0, title="Ethnicity (coarse)"
)
df["age"].plot.hist(bins=20, label="age", ax=axes[0, 1], title="Age")
df["region"].value_counts().plot.bar(
ax=axes[2, 0], rot=0, title="Region (North America)"
)
df["hospitaldischargestatus"].value_counts().plot.bar(
ax=axes[2, 1], rot=0, title="Outcome"
)
plt.show()
plt.close()
########################
# LATEX TABLES
########################
def ci_bound(std, count, ci=0.95):
"""Return Confidence Interval radius."""
return (1 + ci) * std / np.sqrt(count)
def results_to_table(data, domain, outcome, mode, metric, verbose=False, n="max"):
"""Pairplot of all models vs strategies."""
# Load results
with open(
RESULTS_DIR / f"results_{data}_{outcome}_{domain}.json", encoding="utf-8"
) as handle:
res = json.load(handle)
models = [k for k in res.keys() if k in ["MLP", "CNN", "LSTM", "Transformer"]]
dfs = []
for model in models:
df = stack_avg_results(res[model], metric, mode)
df["Model"] = model
dfs.append(df)
df = pd.concat(dfs)
# Get final performance val
if n == "max":
df = df[df["Epoch"] == df["Epoch"].max()]
domain_col = domain
else:
df = df[df["Epoch"] == n]
domain_col = f"{domain} ({n})"
stats = df.groupby(["Model", "Strategy"])[METRIC_FULL_NAME[metric]].agg(
["mean", "count", "std"]
)
stats["ci95"] = ci_bound(stats["std"], stats["count"])
if verbose:
stats["ci95_lo"] = stats["mean"] + stats["ci95"]
stats["ci95_hi"] = stats["mean"] - stats["ci95"]
stats[domain_col] = stats.apply(
lambda x: f"{x['mean']:.3f} ({x.ci95_lo:.3f}, {x.ci95_hi:.3f})", axis=1
)
else:
stats[domain_col] = stats.apply(
lambda x: f"{100 * x['mean']:.1f}$_{{\pm{100 * x.ci95:.1f}}}$", axis=1
)
stats = pd.DataFrame(stats[domain_col])
stats.reset_index(inplace=True)
stats["Category"] = stats["Strategy"].apply(lambda x: STRATEGY_CATEGORY[x])
stats = stats.pivot(["Category", "Strategy"], "Model")
return stats
def generate_table_results(
data="mimic3", outcome="mortality_48h", mode="test", metric="BalAcc", latex=False
):
"""
Latex table of main results
"""
domains = ["age", "ethnicity_coarse", "ward", "time_season"]
dfs = []
for domain in domains:
try:
dfs.append(results_to_table(data, domain, outcome, mode, metric))
except:
pass
df = pd.concat(dfs, axis=1)
if latex:
idx = pd.IndexSlice
sub_idx = idx["Regularization":"Rehearsal", :]
df = df.style.highlight_max(
axis=0,
props="bfseries: ;",
subset=sub_idx,
).to_latex()
return df
else:
return df
def generate_hp_table_super(outcome="mortality_48h"):
"""
Combines all tables into a nice latex format.
"""
prefix = r"""
\begin{table}[h]
\centering
"""
box_prefix = r"""
\begin{adjustbox}{max width=\columnwidth}
"""
old = r"""\begin{tabular}{lllllll}"""
repl = r"""\begin{tabular}{lllllll}
\multicolumn{7}{c}{\textsc{Age}} \\
"""
box_suffix = r"""
\end{adjustbox}
"""
suffix = rf"""
\caption{{Tuned hyperparameters for main experiments (outcome of {outcome}).}}
\label{{tab:hyperparameters}}
\end{{table}}
"""
latex = (
prefix
+ box_prefix
+ generate_hp_table(outcome=outcome, domain="age").to_latex().replace(old, repl)
+ generate_hp_table(outcome=outcome, domain="ethnicity_coarse")
.to_latex()
.replace(old, repl.replace("Age", "Ethnicity (broad)"))
+ box_suffix
+ box_prefix
+ generate_hp_table(outcome=outcome, domain="time_season")
.to_latex()
.replace(old, repl.replace("Age", "Time (season)"))
+ generate_hp_table(outcome=outcome, domain="ward")
.to_latex()
.replace(old, repl.replace("Age", "ICU Ward"))
+ box_suffix
+ suffix
)
return latex
def generate_table_hospitals(
outcome="ARF_4h",
mode="test",
metric="BalAcc",
hospitals=[6, 12, 18, 24, 30, 36],
latex=False,
):
"""
Latex table of main results
"""
dfs = [
results_to_table("eicu", "hospital", outcome, mode, metric, n=n)
for n in hospitals
]
df = pd.concat(dfs, axis=1)
if latex:
idx = pd.IndexSlice
sub_idx = idx["Regularization":"Rehearsal", :]
df = df.style.highlight_max(
axis=0,
props="bfseries: ;",
subset=sub_idx,
).to_latex()
return df
else:
return df
def generate_hp_table(data="mimic3", outcome="mortality_48h", domain="age"):
models = ["MLP", "CNN", "LSTM", "Transformer"]
strategies = ["EWC", "OnlineEWC", "LwF", "SI", "Replay", "AGEM", "GEM"]
dfs = []
col_rename_map = {
"ewc_lambda": "lambda",
"alpha": "lambda",
"si_lambda": "lambda",
"memory_strength": "temperature",
"mem_size": "sample_size",
}
for model in models:
for strategy in strategies:
try:
with open(
ROOT_DIR
/ "config"
/ data
/ outcome
/ domain
/ f"config_{model}_{strategy}.json",
encoding="utf-8",
) as handle:
res = json.load(handle)["strategy"]
df = pd.DataFrame([res]).rename(columns=col_rename_map)
df["Model"] = model
df["Strategy"] = strategy
dfs.append(df)
except:
pass
df = pd.concat(dfs)
df = df.set_index(["Model", "Strategy"])
df = df.replace(np.NaN, "")
df = df.drop("mode", axis=1)
return df