-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisualization.py
More file actions
executable file
·64 lines (55 loc) · 2.71 KB
/
visualization.py
File metadata and controls
executable file
·64 lines (55 loc) · 2.71 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
'''
Some Visualization Functions
'''
from skopt.plots import plot_convergence, plot_evaluations, plot_objective
from matplotlib import pyplot as plt
import numpy as np
import matplotlib
import os
import json
matplotlib.rc('figure', figsize = (14, 7)) # Plot size to 14" x 7"
matplotlib.rc('font', size = 14) # Font size to 14
matplotlib.rc('axes.spines', top = False, right = False) # Do not display top and right frame lines
matplotlib.rc('axes', grid = False) # Remove grid lines
matplotlib.rc('axes', facecolor = 'white') # Set backgound color to white
def plot_multiple_mean_std(x,y_dict,y_label,x_label,title,colors_dict,savepath=False):
_, ax = plt.subplots()
for name,data in y_dict.items():
mean,std,color = data['mean'],data['std'],colors_dict[name]
ax.plot(x, mean, lw = 3, color=color, alpha = 0.8, label = name)
y_low = mean - std
y_high = mean + std
ax.fill_between(x, y_low, y_high, color=color, alpha = 0.1)
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.legend(loc = 'best')
if savepath: plt.savefig(savepath)
plt.close()
def save_history_plots(history,plotinfo,folderpath):
""" history - {'metric_name':{'train':shape(n,epochs),'test':shape(n,epochs),...},...}
\n this will take np.mean((n,epochs),axis=0) and std and plot each metric graph
\n make sure n is atleast 1, so the shape wont be (epochs)
\n plotinfo - {'metric_name':{'y_label':'','x_label':'','title':'','imgname':''},...},...}
"""
if not os.path.exists(folderpath): os.makedirs(folderpath)
num_epochs = len(history['fitness']['train']['mean'])
for metric_name,y_dict in history.items():
plot_multiple_mean_std(
x=np.arange(num_epochs), y_dict=y_dict,
y_label=plotinfo[metric_name]['y_label'], x_label=plotinfo[metric_name]['x_label'], title=plotinfo[metric_name]['title'],
colors_dict=plotinfo['colors'],
savepath=os.path.join(folderpath,plotinfo[metric_name]['img_name'])
)
def save_skopt_plots(dirpath,search_result,prior_names):
if not os.path.exists(dirpath): os.makedirs(dirpath)
# ---- Evalution
plot_evaluations(search_result, bins=20)
plt.savefig( os.path.join(dirpath,'evaluation_plot.png') )
# ---- Convergence (previously looked better enquire what is going on)
plot_convergence(search_result)
plt.savefig( os.path.join(dirpath,'convergence_plot.png') )
# ---- Partial Dependence plots are only approximations of the modelled fitness function
# - which in turn is only an approximation of the true fitness function in fitness
plot_objective(result=search_result)
plt.savefig( os.path.join(dirpath,'objective_plot.png') )