-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFigures.py
More file actions
176 lines (149 loc) · 7.67 KB
/
Figures.py
File metadata and controls
176 lines (149 loc) · 7.67 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
"""
Figures: <Description>
"""
import matplotlib.pyplot as plt
import matplotlib.backends.backend_pdf as mpb
import seaborn as sns
import pandas as pd
from pandas import DataFrame as df
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import gridplot
class Figure:
"""
__init__
COnstructor for the Figures class
parameters:
status - An instance of Status class used to get the plotting data from
returns:
-None-
"""
def __init__(self, status):
self.status = status
self.record = df(status.get_solution_record(), columns=['iteration', 'num_func_calls', 'current_best', 'current_soln'] )
"""
show_best_result_plot
Draws a trace plot for best result on the y_axis and iterations on the x-axis
parameters:
marker_size (optional) - To control the size of the trace marker
returns:
-None-
"""
def show_best_result_plot(self,marker="_", marker_size=2):
#f = plt.figure(1)
sns_plot = sns.lmplot(self.record.columns[0], self.record.columns[2], data=pd.concat([self.record['iteration'], self.record['current_best']], axis=1),
scatter_kws={"s": marker_size}, fit_reg=False, markers=marker)
#sns_plot = sns.pairplot(data=self.record)
sns_plot.axes[0, 0].set_ylim(0, )
sns_plot.axes[0, 0].set_xlim(0, )
#f.show()
sns.plt.show()
#plt.clf()
#return sns_plot
"""
save_best_result_plot
Saves a trace plot for best result on the y_axis and iterations on the x-axis in the given filename
parameters:
file_name (optional) - File name where the chart should be saved, if none provided then chart saved in a file "best_result.png"
marker_size (optional) - To control the size of the trace marker
returns:
-None-
"""
def save_best_result_plot(self, file_name="best_result.png", marker="_", marker_size=2):
sns_plot = sns.lmplot(self.record.columns[0], self.record.columns[2], data=pd.concat([self.record['iteration'], self.record['current_best']], axis=1),
scatter_kws={"s": marker_size},fit_reg=False, markers=marker )
#sns_plot = sns.pairplot(data=self.record)
sns_plot.axes[0, 0].set_ylim(0, )
sns_plot.axes[0, 0].set_xlim(0, )
sns_plot.savefig(file_name)
sns.plt.clf()
#sns.plt.show()
#return sns_plot
"""
show_current_result_plot
Draws a trace plot for current_result on the y_axis and iterations on the x-axis
parameters:
marker_size (optional) - To control the size of the trace marker
returns:
-None-
"""
def show_current_result_plot(self):
#sns_plot = sns.lmplot(self.record.columns[0], self.record.columns[3], data=pd.concat([self.record['iteration'], self.record['current_soln']], axis=1), scatter_kws={"s": marker_size}, fit_reg=False, markers=marker)
sns.set_style("darkgrid")
plt.plot(self.record['iteration'], self.record['current_soln'])
plt.axis([0,self.status.get_max_iterations(),0,self.status.sequence_length])
plt.xlabel(self.record.columns[0])
plt.ylabel(self.record.columns[3])
plt.show()
"""
save_current_result_plot
Saves a trace plot for current_result on the y_axis and iterations on the x-axis in the given filename
parameters:
file_name (optional) - File name where the chart should be saved, if none provided then chart saved in a file "current_result.png"
marker_size (optional) - To control the size of the trace marker
returns:
-None-
"""
def save_current_result_plot(self,file_name="current_result.png"):
#sns_plot = sns.lmplot(self.record.columns[0], self.record.columns[3], data=pd.concat([self.record['iteration'], self.record['current_soln']], axis=1), scatter_kws={"s": marker_size}, fit_reg=False, markers=marker)
sns.set_style("darkgrid")
plt.plot(self.record['iteration'], self.record['current_soln'])
plt.axis([0,self.status.get_max_iterations(),0,self.status.sequence_length])
plt.xlabel(self.record.columns[0])
plt.ylabel(self.record.columns[3])
plt.savefig(file_name)
#plt.show()
"""
save_multiple_plots
Saves multiple plots the given filename (has to be a pdf type)
parameters:
file_name - PDF file name where the chart should be saved
figures - An iterator containing objects of the type Figure
returns:
-None-
"""
def save_multiple_plots(self, filename_pdf, figures):
pdf = mpb.PdfPages(filename_pdf)
for figure in figures:
pdf.savefig(figure)
pdf.close()
"""
get_best_result_plot_html
Private function for getting a bokeh figure object for best_solution
parameters:
marker_size: Parameter for the line width of the chart, default value: 2
opt_title : Optional - used to add name of parameter, if using with multiple plots
returns:
An instance of figure from the Bokeh library
"""
def get_best_result_plot_html(self, marker_size=2, opt_title=''):
p = figure(title="Best_result"+opt_title, x_axis_label=self.record.columns[0], y_axis_label=self.record.columns[2])
p.line(self.record['iteration'], self.record['current_best'], legend=self.record.columns[2], line_width=marker_size)
return p
"""
get_current_result_plot_html
Private function for getting a bokeh figure object for current_solution
parameters:
marker_size: Parameter for the line width of the chart, default value: 2
returns:
An instance of figure from the Bokeh library
"""
def get_current_result_plot_bokeh(self, marker_size=2):
p = figure(title="Current_result", x_axis_label=self.record.columns[0], y_axis_label=self.record.columns[3])
p.line(self.record['iteration'], self.record['current_soln'], legend=self.record.columns[3], line_width=marker_size)
return p
"""
save_multiple_plots_bokeh
Function for saving multiple charts in a grid layout using the Bokeh library
parameters:
filename_html: The name of the html file where results should be saved
figures: A list of figures of the type Bokeh.figure
Side effect:
Displays the charts and saves them in the given file name. File overwritten if already present.
returns:
-None-
"""
def save_multiple_plots_bokeh(self, filename_html, figures):
# pdf = matplotlib.backends.backend_pdf.PdfPages(filename_pdf)
output_file(filename_html)
p = gridplot(figures, toolbar_location=None, ncols=2, plot_width=250, plot_height=250)
show(p)