-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_directory.py
162 lines (152 loc) · 4.94 KB
/
plot_directory.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
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 27 18:17:08 2020
@author: cxue2
"""
import os
import sys
import glob
from collections import defaultdict
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from misc import calc_performance_metrics
from misc import get_roc_info, get_pr_info
from multi_curves import plot_curve
plt.style.use('fivethirtyeight')
plt.rcParams['axes.facecolor'] = 'w'
plt.rcParams['figure.facecolor'] = 'w'
plt.rcParams['savefig.facecolor'] = 'w'
def collect_output(string, string_list, only_print=False):
"""
collect output or just print;
"""
if only_print:
print(string)
else:
string_list.append(string)
def write_txt(string_list, txt_out):
"""
write txt;
"""
with open(txt_out, 'a') as outfile:
for string in string_list:
outfile.write(f'{string}\n')
def get_dir_list(parent_dir):
"""
get list of directories;
"""
return [os.path.join(parent_dir, d) for d in os.listdir(parent_dir)]
def plot_individual_curve(hmp_roc, legend_dict, curve_str, fig_name):
"""
plot all the curves;
"""
fig, ax = plt.subplots(1)
legend_str = {}
color, legend_ext = legend_dict[0]
p_mean, _, auc_mean, auc_std = plot_curve(curve_str, ax, hmp_roc['xs'],
hmp_roc['ys_mean'], hmp_roc['ys_upper'],
hmp_roc['ys_lower'], hmp_roc['auc_mean'], hmp_roc['auc_std'],
color=color)
msg = r'{}: {:.3f}$\pm${:.3f}'.format(legend_ext, auc_mean, auc_std)
legend_str[0] = (p_mean, msg)
p_mean_list = [v[0] for k, v in legend_str.items()]
msg_list = [v[1] for k, v in legend_str.items()]
ax.legend(p_mean_list, msg_list,
facecolor='w', prop={"weight":'bold', "size":17},
bbox_to_anchor=(0.04, 0.04, 0.5, 0.5),
loc='lower left')
fig.savefig(fig_name, dpi=300, format='svg', bbox_inches='tight')
plt.close('all')
# print(fig_name)
def main(fig_ext='Fusion'):
"""
main entrypoint
"""
parent_dir = sys.argv[1]
parent_dir_ext = os.path.normpath(parent_dir).split(os.sep)[-2]
seed_list = get_dir_list(parent_dir)
print(seed_list)
string_list = []
only_print = False
for _, dir_rsl in enumerate(seed_list):
current_string_list = [dir_rsl + "\n"]
mode = 'chunk'
# list of all csv files
num_csvs = None
if num_csvs is None:
lst_csv = glob.glob(dir_rsl + '/*.csv', recursive=False)
dirs_read = [dir_rsl]
else:
lst_csv = []
dirs_read = []
directories = [os.path.join(dir_rsl, d) for d in os.listdir(dir_rsl)]
directories = [d for d in directories if os.path.isdir(d)]
for directory in directories:
current_lst = glob.glob(directory + '/*.csv', recursive=False)
if len(current_lst) == int(num_csvs):
lst_csv.extend(current_lst)
dirs_read.append(directory)
lst_lbl, lst_scr = [], []
mtr_all = defaultdict(list)
print(dir_rsl)
print(len(lst_csv))
if lst_csv == [] or len(lst_csv) not in [5, 20]:
continue
print(f"{len(lst_csv)} csvs found;")
fn_metrics = {}
for fn in lst_csv:
df = pd.read_csv(fn)
# get scores and labels
if mode == 'chunk':
lbl = df.label.to_numpy()
scr = df.score.to_numpy()
elif mode == 'audio_avg':
tmp = df.groupby('audio_fn').mean().to_numpy()
lbl = tmp[:,0].astype(np.int)
scr = tmp[:,-1]
else:
raise AssertionError(f'invalid mode: {mode}')
mtr = calc_performance_metrics(scr, lbl)
for k, mtr_val in mtr.items():
if k == 'mat':
continue
mtr_all[k].append(mtr_val)
fn_metrics[fn] = {mk: mv for mk, mv in mtr.items() if mk != 'mat'}
lst_lbl.append(lbl)
lst_scr.append(scr)
print(f'loaded {len(fn_metrics)} results;')
for filename, fn_mtr in fn_metrics.items():
collect_output(filename, current_string_list, only_print=only_print)
for metric, metric_val in fn_mtr.items():
collect_output("\t{}, {:.3f}".format(metric, metric_val), current_string_list,
only_print=only_print)
collect_output('avg_performance:', current_string_list, only_print=only_print)
for k, v in mtr_all.items():
collect_output('{}: {:.3f}, {:.3f}'.format(k, np.mean(v), np.std(v)),
current_string_list, only_print=only_print)
try:
curr_hmp_roc = get_roc_info(lst_lbl, lst_scr)
curr_hmp_pr = get_pr_info(lst_lbl, lst_scr)
except (TypeError, ValueError) as error:
print(error)
continue
legend_dict = {0: ('magenta', fig_ext)}
fig_name = f'{dir_rsl}/individual_roc.svg'
plot_individual_curve(curr_hmp_roc, legend_dict, 'roc', fig_name)
fig_name = f'{dir_rsl}/individual_pr.svg'
plot_individual_curve(curr_hmp_pr, legend_dict, 'pr', fig_name)
string_list.extend(current_string_list)
now = str(datetime.now()).replace(' ', '_').replace(':', '_')
txt_out = os.path.join('txt', str(datetime.now()).split(' ', maxsplit=1)[0], parent_dir_ext)
if not os.path.isdir(txt_out):
os.makedirs(txt_out)
txt_out = os.path.join(txt_out, f'{now}_output.txt')
write_txt(string_list, txt_out)
print(txt_out)
if __name__ == '__main__':
fig_ext = sys.argv[2] if len(sys.argv) >= 3 else 'Fusion'
fig_ext = '' if fig_ext == 'e' else fig_ext
main(fig_ext=fig_ext)