1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Plotting of variables, adjectives, ... using gnuplot"""
19
20 __revision__ = "$Id: doc.py,v 1.15 2010-10-29 19:24:41 rliebscher Exp $"
21
22 import sys
23 import Gnuplot
24 import Gnuplot.funcutils
25
27 """get tuple with minimum and maximum x-values used by the set."""
28 x_min = None
29 x_max = None
30 for x in set.getValuesX():
31 if x_min is None:
32 x_min = x
33 x_max = x
34
35 return (x_min,x_max)
36
38 """get tuple with minimum and maximum x-values used by the sets of this dicts of sets."""
39 x_min = None
40 x_max = None
41 for s in sets.values():
42 (x_min2,x_max2) = getMinMax(s)
43 if x_min is None or x_min2 < x_min:
44 x_min = x_min2
45 if x_max is None or x_max2 > x_max:
46 x_max = x_max2
47 return (x_min,x_max)
48
49
51 """Collect all important points of all adjectives in this dict of sets."""
52
53 from fuzzy.set.Set import merge
54
55 temp = None
56 for s in sets.values():
57 if temp is None:
58 temp = s
59 else:
60 temp = merge(max,temp,s)
61
62
63
64 points = [p[0] for p in temp.points]
65
66 return points[:1] + [p0 for p0,p1 in zip(points[1:],points) if p0!=p1]
67
68
70 """Get all sets of adjectives in this variable."""
71 sets = {}
72 for a_name,adj in variable.adjectives.items():
73 sets[a_name] = adj.set
74 return sets
75
76
78 """Main object. Get an instance of this to do your work."""
79
81 self.directory = directory
82 self.overscan = 0.1
83
85 g("set terminal png small transparent truecolor nocrop")
86 g("set output '%s/%s.png'" % (self.directory,filename))
87
88 - def initGnuplot2D(self,filename="plot",xlabel=None,ylabel=None,title=None,xrange_=None,yrange=None,x_logscale=0,y_logscale=0):
89 g = Gnuplot.Gnuplot(debug=0)
90 self.setTerminal(g,filename)
91
92 if xlabel is not None: g.xlabel(xlabel)
93 if ylabel is not None: g.ylabel(ylabel)
94 if title is not None: g.title(title)
95 if xrange_ is not None: g('set xrange [%f:%f]' % xrange_)
96 else: g('set autoscale x')
97 if yrange is not None: g('set yrange [%f:%f]' % yrange)
98 else: g('set autoscale y')
99 if x_logscale: g('set logscale x'); g('set autoscale x')
100 if y_logscale: g('set logscale y'); g('set autoscale y')
101 return g
102
103 - def initGnuplot3D(self,filename="plot3D",xlabel=None,ylabel=None,zlabel=None,title=None,xrange_=None,yrange=None,zrange=None,x_logscale=0,y_logscale=0,z_logscale=0):
104 g = Gnuplot.Gnuplot(debug=0)
105 self.setTerminal(g,filename)
106
107 if xlabel is not None: g.xlabel(xlabel)
108 if ylabel is not None: g.ylabel(ylabel)
109 if zlabel is not None: g("set zlabel '%s'" % zlabel)
110 if title is not None: g.title(title)
111 if xrange_ is not None: g('set xrange [%f:%f]' % xrange_)
112 else: g('set autoscale x')
113 if yrange is not None: g('set yrange [%f:%f]' % yrange)
114 else: g('set autoscale y')
115 if zrange is not None: g('set zrange [%f:%f]' % zrange)
116 else: g('set autoscale z')
117 if x_logscale: g('set logscale x');g('set autoscale x')
118 if y_logscale: g('set logscale y');g('set autoscale y')
119 if z_logscale: g('set logscale z');g('set autoscale z')
120 g('set style data lines')
121 g('set hidden')
122 g('set pm3d at s')
123 g('set pm3d ftriangles interpolate 50,50')
124 g('set contour surface')
125 return g
126
127
130
131
133 (x_min,x_max) = getGlobalMinMax(sets)
134 width = x_max - x_min
135 x_min = x_min - self.overscan * width
136 x_max = x_max + self.overscan * width
137 width = x_max - x_min
138
139 values = [x_min]+getPoints(sets)+[x_max]
140
141 return (x_min,x_max,values)
142
143
159
161 """Creates a 2D plot of a variable"""
162
163 self.createDocSets(getSets(v),name,x_logscale,y_logscale,description=v.description,units=v.unit)
164
165 - def createDocSets(self,sets,name,x_logscale=0,y_logscale=0,description=None,units=None):
166 """Creates a 2D plot of dict of sets"""
167
168 import fuzzy.set.Polygon
169
170
171 def sort_key(a):
172 s = sets[a]
173 x = s.getValuesX().next()
174 return (x,-s(x))
175
176 (x_min,x_max,x) = self.getValuesSets(sets)
177
178
179 plot_items = []
180 for s_name in sorted(sets,key=sort_key):
181 s = sets[s_name]
182 if isinstance(s,fuzzy.set.Polygon.Polygon):
183 p = [(x_min,s(x_min))] + s.points + [(x_max,s(x_max))]
184 plot_item = Gnuplot.PlotItems.Data(p,title=s_name)
185 else:
186 plot_item = Gnuplot.funcutils.compute_Data(x,s,title=s_name)
187 plot_items.append(plot_item)
188
189 xlabel = description or ""
190 if units is not None:
191 xlabel += " [%s]" % units
192 g = self.initGnuplot2D(filename=name,xlabel=xlabel,ylabel="membership",title=name,xrange_=(x_min,x_max),yrange=(-0.2,1.2),x_logscale=x_logscale,y_logscale=y_logscale)
193 g('set style fill transparent solid 0.5 border')
194 g('set style data filledcurves y1=0')
195 g.plot(*plot_items)
196 g.close()
197
198 - def create2DPlot(self,system,x_name,y_name,input_dict=None,output_dict=None,x_logscale=0,y_logscale=0):
199 """Creates a 2D plot of an input variable and an output variable.
200 Other (const) variables have to be set beforehand in the dictionary input_dict.
201
202 @param system: the fuzzy system to use
203 @type system: L{fuzzy.System.System}
204 @param x_name: name of input variable used for x coordinate values
205 @type x_name: string
206 @param y_name: name of output variable used for y coordinate values
207 @type y_name: string
208 @param input_dict: dictionary used for input values, can be used to predefine other input values
209 @type input_dict: dict
210 @param output_dict: dictionary used for output values
211 @type output_dict: dict
212 @param x_logscale: use logarithmic scale for x values
213 @type x_logscale: bool
214 @param y_logscale: use logarithmic scale for y values
215 @type y_logscale: bool
216 """
217
218 input_dict = input_dict or {}
219 output_dict = output_dict or {}
220
221 (x_min,x_max,x) = self.getValues(system.variables[x_name])
222
223 def f(x):
224 input_dict[x_name] = x
225 output_dict[y_name] = 0.0
226
227 system.calculate(input_dict,output_dict)
228
229 return output_dict[y_name]
230
231 g = self.initGnuplot2D(filename=x_name+"_"+y_name,xlabel=x_name,ylabel=y_name,title=y_name+"=f("+x_name+")",xrange_=(x_min,x_max),x_logscale=x_logscale,y_logscale=y_logscale)
232 g('set style data lines')
233 g.plot(Gnuplot.funcutils.compute_Data(x, f))
234 g.close()
235
236 - def create3DPlot(self,system,x_name,y_name,z_name,input_dict=None,output_dict=None,x_logscale=0,y_logscale=0,z_logscale=0):
237 """Creates a 3D plot of 2 input variables and an output variable.
238 Other (const) variables have to be set beforehand in the dictionary input_dict.
239
240 @param system: the fuzzy system to use
241 @type system: L{fuzzy.System.System}
242 @param x_name: name of input variable used for x coordinate values
243 @type x_name: string
244 @param y_name: name of input variable used for y coordinate values
245 @type y_name: string
246 @param z_name: name of output variable used for z coordinate values
247 @type z_name: string
248 @param input_dict: dictionary used for input values, can be used to predefine other input values
249 @type input_dict: dict
250 @param output_dict: dictionary used for output values
251 @type output_dict: dict
252 @param x_logscale: use logarithmic scale for x values
253 @type x_logscale: bool
254 @param y_logscale: use logarithmic scale for y values
255 @type y_logscale: bool
256 @param z_logscale: use logarithmic scale for z values
257 @type z_logscale: bool
258 """
259 input_dict = input_dict or {}
260 output_dict = output_dict or {}
261
262 (x_min,x_max,x) = self.getValues(system.variables[x_name])
263 (y_min,y_max,y) = self.getValues(system.variables[y_name])
264
265 def f(x,y):
266 input_dict[x_name] = x
267 input_dict[y_name] = y
268 output_dict[z_name] = 0.0
269
270 system.calculate(input_dict,output_dict)
271
272 return output_dict[z_name]
273
274 g = self.initGnuplot3D(filename=x_name+"_"+y_name+"_"+z_name,xlabel=x_name,ylabel=y_name,zlabel=z_name,title="%s=f(%s,%s)" % (z_name,x_name,y_name),xrange_=(x_min,x_max),yrange=(y_min,y_max),x_logscale=x_logscale,y_logscale=y_logscale,z_logscale=z_logscale)
275 g.splot(Gnuplot.funcutils.compute_GridData(x,y, f,binary=0))
276 g.close()
277
278
279 - def create3DPlot_adjective(self,system,x_name,y_name,z_name,adjective,input_dict=None,output_dict=None,x_logscale=0,y_logscale=0,z_logscale=0):
280 """Creates a 3D plot of 2 input variables and an adjective of the output variable.
281 Other (const) variables have to be set beforehand in the dictionary input_dict.
282
283 @param system: the fuzzy system to use
284 @type system: L{fuzzy.System.System}
285 @param x_name: name of input variable used for x coordinate values
286 @type x_name: string
287 @param y_name: name of input variable used for y coordinate values
288 @type y_name: string
289 @param z_name: name of output variable used for z coordinate values
290 @type z_name: string
291 @param adjective: name of adjective of output variable used for z coordinate values
292 @type adjective: string
293 @param input_dict: dictionary used for input values, can be used to predefine other input values
294 @type input_dict: dict
295 @param output_dict: dictionary used for output values
296 @type output_dict: dict
297 @param x_logscale: use logarithmic scale for x values
298 @type x_logscale: bool
299 @param y_logscale: use logarithmic scale for y values
300 @type y_logscale: bool
301 @param z_logscale: use logarithmic scale for z values
302 @type z_logscale: bool
303 """
304 input_dict = input_dict or {}
305 output_dict = output_dict or {}
306
307 (x_min,x_max,x) = self.getValues(system.variables[x_name])
308 (y_min,y_max,y) = self.getValues(system.variables[y_name])
309
310 def f(x,y):
311 input_dict[x_name] = x
312 input_dict[y_name] = y
313 output_dict[z_name] = 0.0
314
315 system.calculate(input_dict,output_dict)
316
317 return output_dict[z_name][adjective]
318
319 g = self.initGnuplot3D(filename=x_name+"_"+y_name+"_"+z_name+"_"+adjective,xlabel=x_name,ylabel=y_name,zlabel=z_name,title="%s.%s=f(%s,%s)" % (z_name,adjective,x_name,y_name),xrange_=(x_min,x_max),yrange=(y_min,y_max),zrange=(0,1),x_logscale=x_logscale,y_logscale=y_logscale,z_logscale=z_logscale)
320 g("set xyplane at 0")
321 g("set cntrparam levels incremental 0.1,0.2,1.0")
322 g.splot(Gnuplot.funcutils.compute_GridData(x,y, f,binary=0))
323 g.close()
324