[735bb5]: / src / evaluation / plots / barplots.py

Download this file

82 lines (67 with data), 2.3 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
# Base Dependencies
# -----------------
from pathlib import Path
from os.path import join as pjoin
# Local Dependencies
# ------------------
from evaluation.io import collect_step_times
# 3rd-Party Dependencies
# ----------------------
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Constants
# ---------
COLOR_PALETTE = "Set2"
# Auxiliar Functions
# ------------------
def rename_strategies(x):
if x == "BatchLC" or x == "BatchBALD":
return "BatchLC / BatchBALD"
else:
return x
# Main Functions
# --------------
def step_time_barplot():
"""Plots a strip plot showing the step time results for the DDI Extraction corpus"""
title = "Average Step Time per Method and Query Strategy"
output_file = Path(pjoin("results", "step_time_barplot.png"))
# collect results
n2c2_results = collect_step_times(Path(pjoin("results", "n2c2", "all")))
ddi_results = collect_step_times(Path(pjoin("results", "ddi")))
n2c2_results["Corpus"] = ["n2c2"] * len(n2c2_results)
ddi_results["Corpus"] = ["DDI"] * len(ddi_results)
results = pd.concat([n2c2_results, ddi_results], ignore_index=True)
# convert step time to minutes
results["iter_time (average)"] = results["iter_time (average)"].apply(
lambda x: x / 60
)
results["strategy"] = results["strategy"].apply(rename_strategies)
# plot
sns.set()
sns.set_style("whitegrid") # grid style
colors = sns.color_palette(COLOR_PALETTE)
fig, axes = plt.subplots(2)
g = sns.FacetGrid(results, row="Corpus", aspect=2, legend_out=True, sharex=False)
g.map(sns.barplot, "iter_time (average)", "method", "strategy", palette=colors)
g.add_legend(title="Query Strategy")
g.set_ylabels("Method")
g.set_xlabels("Step Time (minutes)")
g.set_yticklabels(["RF", "BiLSTM", "CBERT", "CBERT-pairs"])
sns.move_legend(
g,
"lower center",
bbox_to_anchor=(0.45, 1),
ncol=3,
title=None,
frameon=True,
)
# ax.bar_label(ax.containers[0], fmt='%.f%%')
if title:
# plt.title(title)
pass
if output_file:
plt.savefig(output_file, bbox_inches="tight")
else:
plt.show()
plt.clf()