1 '''
2 Created on 30.06.2010
3
4 @author: Robert
5 '''
6
7 import numpy
8 import pylab
9 from dsc_suite.tools.toolpaths import FOLDER_FOR_DATA, FOLDER_FOR_PLOTS
10 from dsc_suite.analyses.characteristics import get_statistic_characteristics
11 from matplotlib.colors import rgb2hex
12 from colorsys import hsv_to_rgb
13 from time import strftime
14 from os import path
15 from scipy import histogram, histogram2d
16
17
18
20 """ Plot cost distribution.
21
22 entries ...
23 num_bins ...
24 normalize ...
25 create_png ...
26
27 entry format: list of dictionaries with the following keys:
28 ['benchmark', 'data structure', 'filename',
29 'samples', 'depth', 'cost criteria',
30 'mean value', 'standard deviation']
31
32 If multiple entries from the same data structure are given
33 they are grouped together. Currently it is not controlled if
34 different entries have the same criteria.
35
36 """
37
38 labels = parameter["labels"]
39 sizes = parameter["sizes"]
40 axes = parameter["axes"]
41
42
43 title = labels["title"]
44 legend_position = labels["legend-position"]
45 legend_title = labels["legend-title"]
46 legend_parameter = labels["legend-parameter"]
47 xlabel = labels["x-label"]
48 ylabel = labels["y-label"]
49
50
51 width = sizes["dimensions"]["width"]
52 length = sizes["dimensions"]["length"]
53 left = sizes["margins"]["left"]
54 right = sizes["margins"]["right"]
55 top = sizes["margins"]["top"]
56 bottom = sizes["margins"]["bottom"]
57
58
59 if axes["x-axe"]["xmin"] == "auto":
60 xmin = float('inf')
61 else:
62 xmin = axes["x-axe"]["xmin"]
63 if axes["x-axe"]["xmax"] == "auto":
64 xmax = -float('inf')
65 else:
66 xmax = axes["x-axe"]["xmin"]
67 if axes["y-axe"]["ymin"] == "auto":
68 ymin = 0.0
69 else:
70 ymin = axes["y-axe"]["ymin"]
71 if axes["y-axe"]["ymax"] == "auto":
72 ymax = 0.0
73 else:
74 ymax = axes["y-axe"]["ymax"]
75
76
77 pylab.figure(1,(length,width))
78 pylab.subplots_adjust(left,bottom,right,top)
79 pylab.ticklabel_format(style = "sci", scilimits = (-2,5))
80
81
82 cost_criteria = None
83 for file in filenames:
84 if cost_criteria == None:
85 criteria = information[file]["cost_criteria"].keys()
86 for crit in criteria:
87 if crit in file:
88 cost_criteria = crit
89 elif cost_criteria in file:
90 continue
91 else:
92 raise IndexError
93 print cost_criteria
94 number_of_files = len(filenames)
95 """ determine colors for the different data structures """
96 ds_colors = [rgb2hex(hsv_to_rgb((x+1)/float(number_of_files), 0.7, 0.5)) for x in range(number_of_files)]
97 avg_mus = {}
98 avg_sigmas = {}
99 for file in filenames:
100 dict_of_crit = information[file]["cost_criteria"][cost_criteria]
101 mean_value = dict_of_crit["mean_value"]
102 avg_mus.update({file : mean_value})
103 sigma = dict_of_crit["standard_deviation"]
104 avg_sigmas.update({file : sigma})
105
106 """ determine plot range """
107 for file in filenames:
108 xmin = min(xmin, avg_mus[file] - 3 * avg_sigmas[file])
109 xmax = max(xmax, avg_mus[file] + 5 * avg_sigmas[file])
110 stepsize = (xmax - xmin) / float(num_bins)
111 calculated_bins = pylab.frange(xmin, xmax, stepsize)
112 """ plot entries """
113 for file in filenames:
114 data = numpy.load(path.join(FOLDER_FOR_DATA, file))
115 """ normalize """
116 data -= entry['mean value']
117 data *= 1.0/entry['standard deviation']
118 data *= avg_sigmas[entry['data structure']]
119 data += avg_mus[entry['data structure']]
120 histo, bins = histogram(data, calculated_bins)
121 histo = numpy.array(histo, dtype=numpy.float)
122 histo *= 1.0/entry['samples']
123 bincenters = 0.5*(bins[1:] + bins[:-1])
124 pylab.plot(bincenters, histo, ',', alpha=0.7,
125 color=ds_colors[data_structures.index(entry['data structure'])])
126
127 pylab.xlabel(xlabel)
128 pylab.ylabel(ylabel)
129
130 pylab.axis([xmin, xmax, ymin, ymax])
131
132 pylab.grid(True)
133
134 pylab.suptitle(title)
135
136 pylab.legend(title = legend_title, loc = legend_position)
137
138 if create_png:
139 i = 1
140 filename = 'Cost Distribution %s %02i.png' % (strftime("%Y-%m-%d %H%M%S"), i)
141 while path.exists(FOLDER_FOR_PLOTS + '/' + filename):
142 i += 1
143 filename = 'Cost Distribution %s %02i.png' % (strftime("%Y-%m-%d %H%M%S"), i)
144 pylab.savefig(FOLDER_FOR_PLOTS + '/' + filename)
145 pylab.close()
146 return filename
147 else:
148 pylab.show()
149
150 if __name__ == '__main__':
151 test_entries = [{'benchmark' : 'xerox.yal',
152 'data structure' : '3D Slicing Tree',
153 'filename' : 'lalelu.npy',
154 'samples' : 2000000,
155 'depth' : None,
156 'cost criteria' : 'MPMWE',
157 'mean value' : None,
158 'standard deviation' : None},
159 {'benchmark' : 'xerox.yal',
160 'data structure' : 'Sequence Triple',
161 'filename' : 'lilalu.npy',
162 'samples' : 2000000,
163 'depth' : None,
164 'cost criteria' : 'MPMWE',
165 'mean value' : None,
166 'standard deviation' : None},
167 {'benchmark' : 'xerox.yal',
168 'data structure' : 'T-Tree',
169 'filename' : 'Monte Carlo 2010-07-07 142112 01 - MPMWE TTree xerox 2000000.npy',
170 'samples' : 2000000,
171 'depth' : None,
172 'cost criteria' : 'MPMWE',
173 'mean value' : None,
174 'standard deviation' : None},
175 {'benchmark' : 'xerox.yal',
176 'data structure' : 'Sequence Quintuple',
177 'filename' : 'lulila.npy',
178 'samples' : 2000000,
179 'depth' : None,
180 'cost criteria' : 'MPMWE',
181 'mean value' : None,
182 'standard deviation' : None}]
183
184 test_hentry = {'benchmark' : 'xerox.yal',
185 'data structure' : 'T-Tree',
186 'filename' : 'Monte Carlo 2010-07-07 142112 01 - MPMWE TTree xerox 2000000.npy',
187 'samples' : 2000000,
188 'cost criteria' : 'MPMWE'}
189
190 test_ventry = {'benchmark' : 'xerox.yal',
191 'data structure' : 'T-Tree',
192 'filename' : 'Monte Carlo 2010-07-07 142112 02 - HESA TTree xerox 2000000.npy',
193 'samples' : 2000000,
194 'cost criteria' : 'HESA'}
195 plot_cost_distribution(test_entries)
196