-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_sampling_unc.py
More file actions
179 lines (153 loc) · 5.98 KB
/
Copy pathplot_sampling_unc.py
File metadata and controls
179 lines (153 loc) · 5.98 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
import sys
import mysql.connector as db
import os
import math
import numpy as np
import matplotlib.pyplot as plt
local = True
base_dir = os.path.dirname(os.path.realpath(__file__))
pic_dir = f"{base_dir}/pic/sampling"
if not os.path.exists(pic_dir):
os.makedirs(pic_dir)
data_list = ["parkinsons", "vertebral","ionosphere", "climate", "blod", "breast","bank", "QSAR"] # ,"spambase", "madelon"
algo = "LR"
run_name = "Paper_results_main"
unc = "rl_e"
for data in data_list:
# prameters ############################################################################################################################################
plot_name = f"{data}_{algo}_unc"
# plot_name = run_name
query = f"SELECT results, id, prams, result_type FROM experiments Where dataset='Jdata/{data}' AND algo='{algo}' AND run_name='{run_name}' AND result_type='{unc}'" #
########################################################################################################################################################
max_run = 1000
xlabel = "Number of queried instances"
ylabel = "Uncertainty"
jobs = []
# get data from database based on the query above
mydb = db.connect(host="131.234.250.119", user="root", passwd="uncertainty", database="uncertainty")
mycursor = mydb.cursor()
mycursor.execute(query)
results = mycursor.fetchall()
for job in results:
jobs.append(job)
fig, ax1 = plt.subplots()
plot_list = []
for job in jobs:
dir = job[0]
if dir[0] == ".":
dir = base_dir + dir[1:]
if local:
dir = f"/home/mhshaker/Projects/Database/DB_files/job_{job[1]}"
isFile = os.path.isdir(dir)
if not isFile:
print("[Error] file does not exist")
print(dir)
exit()
plot_list.append(job[1])
legend = ""
prams = str(job[2])
pram_name = "batch_size"
search_pram = f"'{pram_name}': "
v_index_s = prams.index(search_pram)
v_index_e = prams.index("}", v_index_s)
batch_size = int(prams[v_index_s+len(search_pram) : v_index_e])
for text in job[3:]:
legend += " " +str(text)
# print(legend)
# exit()
# get the list of file names for unc_mean
dir_mean = dir + "/unc_mean"
file_list = []
for (dirpath, dirnames, filenames) in os.walk(dir_mean):
file_list.extend(filenames)
all_runs = []
run_count = 0
for f in file_list:
# print(f)
run_count += 1
run_result = np.loadtxt(dir_mean+"/"+f)
all_runs.append(run_result)
if run_count > max_run:
break
all_runs = np.array(all_runs)
run_mean = np.nanmean(all_runs, axis=0)
# get the list of file names for unc_std
dir_std = dir + "/unc_std"
file_list = []
for (dirpath, dirnames, filenames) in os.walk(dir_std):
file_list.extend(filenames)
all_runs = []
run_count = 0
for f in file_list:
# print(f)
run_count += 1
run_result = np.loadtxt(dir_std+"/"+f)
all_runs.append(run_result)
if run_count > max_run:
break
all_runs = np.array(all_runs)
run_std = np.nanmean(all_runs, axis=0)
# get the list of file names for unc_max
dir_max = dir + "/unc_max"
file_list = []
for (dirpath, dirnames, filenames) in os.walk(dir_max):
file_list.extend(filenames)
all_runs = []
run_count = 0
for f in file_list:
# print(f)
run_count += 1
run_result = np.loadtxt(dir_max+"/"+f)
all_runs.append(run_result)
if run_count > max_run:
break
all_runs = np.array(all_runs)
run_max = np.nanmean(all_runs, axis=0)
# get the list of file names for acc
dir_max = dir + "/acc"
file_list = []
for (dirpath, dirnames, filenames) in os.walk(dir_max):
file_list.extend(filenames)
all_runs = []
run_count = 0
for f in file_list:
# print(f)
run_count += 1
run_result = np.loadtxt(dir_max+"/"+f)
all_runs.append(run_result)
if run_count > max_run:
break
all_runs = np.array(all_runs)
run_acc = np.nanmean(all_runs, axis=0)
run_acc = run_acc * 100 # to have percentates and not decimals
legend = legend.replace("rl_e", "EU")
legend = legend.replace("rl_a", "AU")
legend = legend.replace("ent", "ENT")
legend = legend.replace("credal", "CU")
legend = legend.replace("random", "Rand")
legend = legend.replace("evid_e", "IEU")
legend = legend.replace("evid_a", "CEU")
steps = np.array(range(len(run_mean))) * batch_size
# steps = np.array(range(len(run_mean)))
low_std = run_mean - run_std
low_std = low_std.clip(min=0)
color = 'k'
ax1.set_xlabel(xlabel)
ax1.set_ylabel('Accuracy %', color=color)
ax1.plot(steps, run_acc, label= "Acc", color='k')
ax1.legend(loc="center left")
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
# color = 'tab:orange'
ax2.set_ylabel(ylabel, color=color) # we already handled the x-label with ax1
# ax2.plot(t, data2, color=color)
# ax2.tick_params(axis='y', labelcolor=color)
ax2.plot(steps, run_mean, label= legend +" Mean")
ax2.fill_between(steps, run_mean + run_std, low_std, alpha=0.2,label= legend +" std")
ax2.plot(steps, run_max, label=legend + " Max")
ax2.legend(loc="center right")
# plt.xlabel(xlabel)
# plt.ylabel(ylabel)
# plt.title(plot_list)
plt.legend(loc="center right")
fig.savefig(f"{pic_dir}/{plot_name}.png")
print(f"Plot {plot_name} Done")