-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstudy_plots_utils.py
150 lines (137 loc) · 6.7 KB
/
study_plots_utils.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
import plotnine as p9
def plot_distributions_bar_plot_grid(dataframe, figure_size=(14, 4)):
"""
We create a function to plot the bar plot.
"""
return (
# Define the plot.
p9.ggplot(dataframe, p9.aes(x='threshold', fill='value'))
# Add the bars.
+ p9.geom_bar(position='dodge')
+ p9.geom_text(p9.aes(label='stat(count)'), stat='count', position=p9.position_dodge(0.9), size=7, va='bottom')
# Rename the x axis.
+ p9.scale_x_discrete(name='Threshold')
# Rename the y axis, give some space on top and bottom (mul_bottom, add_bottom, mul_top, add_top).
+ p9.scale_y_continuous(name='Count', expand=(0, 0, 0, 500))
# Replace the names in the legend and set the colors of the bars.
+ p9.scale_fill_manual(values={0: '#009e73', 1: '#d55e00'}, labels=lambda l: [{0: 'Stable', 1: 'Unstable'}[x] for x in l])
# Place the plots in a grid, renaming the labels.
+ p9.facet_grid('. ~ iterations', labeller=p9.labeller(cols=lambda x: f'iters = {x}'))
# Define the theme for the plot.
+ p9.theme(
# Remove the y axis name.
axis_title_y=p9.element_blank(),
# Set the size of x and y tick labels font.
axis_text_x=p9.element_text(size=7),
axis_text_y=p9.element_text(size=7),
# Place the legend on top, without title, and reduce the margin.
legend_title=p9.element_blank(),
legend_position='top',
legend_box_margin=2,
# Set the size for the figure.
figure_size=figure_size,
)
)
def plot_metrics_comparison_lineplot_grid(dataframe, models_labels, metrics_labels, figure_size=(14, 4)):
"""
We define a function to plot the grid.
"""
return (
# Define the plot.
p9.ggplot(dataframe, p9.aes(x='threshold', y='value', group='variable', color='variable', shape='variable'))
# Add the points and lines.
+ p9.geom_point()
+ p9.geom_line()
# Rename the x axis and give some space to left and right.
+ p9.scale_x_discrete(name='Threshold', expand=(0, 0.2))
# Rename the y axis, give some space on top and bottom, and print the tick labels with 2 decimal digits.
+ p9.scale_y_continuous(name='Value', expand=(0, 0.05), labels=lambda l: ['{:.2f}'.format(x) for x in l])
# Replace the names in the legend.
+ p9.scale_shape_discrete(name='Metric', labels=lambda l: [metrics_labels[x] for x in l])
# Define the colors for the metrics for color-blind people.
+ p9.scale_color_brewer(name='Metric', labels=lambda l: [metrics_labels[x] for x in l], type='qual', palette='Set2')
# Place the plots in a grid, renaming the labels for rows and columns.
+ p9.facet_grid('iterations ~ model', labeller=p9.labeller(rows=lambda x: f'iters = {x}', cols=lambda x: f'{models_labels[x]}'))
# Define the theme for the plot.
+ p9.theme(
# Remove the y axis name.
axis_title_y=p9.element_blank(),
# Set the size of x and y tick labels font.
axis_text_x=p9.element_text(size=7),
axis_text_y=p9.element_text(size=7),
# Place the legend on top, without title, and reduce the margin.
legend_title=p9.element_blank(),
legend_position='top',
legend_box_margin=2,
# Set the size for the figure.
figure_size=figure_size,
)
)
def plot_preprocessing_boxplot_overall(dataframe, metrics_labels, groups_labels, figure_size=(14, 4)):
"""
We define a function to plot the boxplot.
"""
return (
# Define the plot.
p9.ggplot(dataframe, p9.aes(x='group', y='value', fill='group'))
# Add the boxplots.
+ p9.geom_boxplot(position='dodge')
# Rename the x axis.
+ p9.scale_x_discrete(name='Group', labels=lambda l: [groups_labels[x] for x in l])
# Rename the y axis.
+ p9.scale_y_continuous(name='Value', labels=lambda l: ['{:.2f}'.format(x) for x in l])
# Define the colors for the groups for color-blind people.
+ p9.scale_fill_brewer(name='Group', labels=lambda l: [groups_labels[x] for x in l], type='qual', palette='Set2')
# Place the plots in a grid, renaming the labels.
+ p9.facet_grid('variable ~ .', scales='free_y', labeller=p9.labeller(rows=lambda x: f'{metrics_labels[x]}'))
# Define the theme for the plot.
+ p9.theme(
# Remove the x and y axis names.
axis_title_x=p9.element_blank(),
axis_title_y=p9.element_blank(),
# Set the size of x and y tick labels font.
axis_text_x=p9.element_text(size=7),
axis_text_y=p9.element_text(size=7),
# Place the legend on top, without title, and reduce the margin.
legend_title=p9.element_blank(),
legend_position='top',
legend_box_margin=2,
# Set the size for the figure.
figure_size=figure_size,
)
)
def plot_preprocessing_boxplot_bymodel(dataframe, models_labels, metrics_labels, groups_labels, figure_size=(14, 4)):
"""
We define a function to plot the grid.
"""
return (
# Define the plot.
p9.ggplot(dataframe, p9.aes(x='variable', y='value', fill='group'))
# Add the boxplots.
+ p9.geom_boxplot(position='dodge')
# Rename the x axis.
+ p9.scale_x_discrete(name='Metric', labels=lambda l: [metrics_labels[x] for x in l])
# Rename the y axis.
+ p9.scale_y_continuous(name='Value', expand=(0, 0.05),
# breaks=[-0.25, 0, 0.25, 0.5, 0.75, 1], limits=[-0.25, 1],
labels=lambda l: ['{:.2f}'.format(x) for x in l])
# Define the colors for the metrics for color-blind people.
+ p9.scale_fill_brewer(name='Group', labels=lambda l: [groups_labels[x] for x in l], type='qual', palette='Set2')
# Place the plots in a grid, renaming the labels.
+ p9.facet_grid('model ~ .', scales='free_y', labeller=p9.labeller(rows=lambda x: f'{models_labels[x]}'))
# Define the theme for the plot.
+ p9.theme(
# Remove the x and y axis names.
axis_title_x=p9.element_blank(),
axis_title_y=p9.element_blank(),
# Set the size of x and y tick labels font.
axis_text_x=p9.element_text(size=7),
axis_text_y=p9.element_text(size=7),
# Place the legend on top, without title, and reduce the margin.
legend_title=p9.element_blank(),
legend_position='top',
legend_box_margin=2,
# Set the size for the figure.
figure_size=figure_size,
)
)