-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts_maker.py
141 lines (113 loc) · 4.22 KB
/
charts_maker.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
import pandas as pd
import argparse
from os import walk, sep, makedirs
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.transform import dodge
from bokeh.io import save, export_png
from bokeh.palettes import Dark2_5 as palette
import itertools
SAVE_FN = save
SAVE_EXT = "html"
def _calculate_data_for_plots(dfs: dict[str, pd.DataFrame],
metric: str) -> dict[str, float]:
tot_res = {}
indici = list(dfs['P18'].describe().keys()[1:])
for indice in indici:
res = []
for name, df in dfs.items():
tmp = df.describe().transpose()[metric].transpose()
res.append((name, tmp[indice]))
res = [x[1] for x in sorted(res, key=lambda y: y[0])]
tot_res[indice] = res
return tot_res
def make_charts(data_path: str, dest_path: str):
dfs = {}
for (dirpath, _, filenames) in walk(data_path):
for filename in filenames:
if filename == "metrics.csv":
name = dirpath.split(sep)[-1]
dfs[name] = pd.read_csv(sep.join([dirpath, filename]))
# PATIENTS CHARTS
for name, df in dfs.items():
t = df.iloc[:, 1:]
colors = itertools.cycle(palette)
p = figure(x_range=t['Filename'], y_range=(-1, 1.5))
p.title = name
for feat, color in zip(t.iloc[:, 1:], colors):
p.line(t['Filename'],
t[feat],
line_width=2,
color=color,
legend_label=f"{feat} {name}")
p.legend.location = "top_left"
p.legend.click_policy = "mute"
p.xaxis.major_label_orientation = "vertical"
SAVE_FN(p, filename=sep.join([dest_path, f"{name}.{SAVE_EXT}"]))
# METRICS CHARTS
statistiche = list(dfs['P18'].describe().transpose().keys())
for statistica in statistiche:
names_ordered = sorted(dfs.keys())
act_values = _calculate_data_for_plots(dfs, statistica)
data = {
'x': names_ordered,
'DCI': act_values['DCI'],
'TI': act_values['TI'],
'Em': act_values['Em'],
'Bpn': act_values['Bpn'],
}
source = ColumnDataSource(data=data)
p = figure(x_range=names_ordered, title=statistica.upper(), height=350)
p.vbar(x=dodge('x', -0.37, range=p.x_range),
top='DCI',
source=source,
width=0.2,
color="#c9d9d3",
legend_label="DCI")
p.vbar(x=dodge('x', -0.12, range=p.x_range),
top='TI',
source=source,
width=0.2,
color="#718dbf",
legend_label="TI")
p.vbar(x=dodge('x', 0.12, range=p.x_range),
top='Em',
source=source,
width=0.2,
color="#e84d60",
legend_label="Em")
p.vbar(x=dodge('x', 0.37, range=p.x_range),
top='Bpn',
source=source,
width=0.2,
color="#6f7c00",
legend_label="Bpn")
SAVE_FN(p, filename=sep.join([dest_path, f"{statistica}.{SAVE_EXT}"]))
def main():
parser = argparse.ArgumentParser(
description='Auto generate charts from results data')
parser.add_argument('source_dir',
type=str,
help='Folder where data are stored')
parser.add_argument('dest_dir',
type=str,
help='Folder where to save charts')
parser.add_argument('--png',
action="store_true",
default=False,
help='Export chart to PNG')
args = parser.parse_args()
global SAVE_FN
global SAVE_EXT
if args.png:
SAVE_FN = export_png
SAVE_EXT = "png"
# take the first element from the generator
# because I only need folders of depth 1 (source_dir subdirs)
dirpath, dirnames, _ = next(walk(args.source_dir))
for dirname in dirnames:
dest_dir = sep.join([args.dest_dir, dirname])
makedirs(dest_dir, exist_ok=True)
make_charts(sep.join([dirpath, dirname]), dest_dir)
if __name__ == "__main__":
main()