1 | #!/usr/bin/python -tt
|
---|
2 | #
|
---|
3 | # Werner Lustermann, Dominik Neise
|
---|
4 | # ETH Zurich, TU Dortmund
|
---|
5 | #
|
---|
6 | # plotter.py
|
---|
7 |
|
---|
8 | import numpy as np
|
---|
9 | import matplotlib.pyplot as plt
|
---|
10 | import os.path
|
---|
11 | import sys
|
---|
12 |
|
---|
13 | # this class was formerly called Plotter in the depricated
|
---|
14 | # module plotter.py
|
---|
15 | class SimplePlotter(object):
|
---|
16 | """ simple x-y plot """
|
---|
17 | def __init__(self, name, x, style = 'b', xlabel='x', ylabel='y'):
|
---|
18 | """ initialize the object """
|
---|
19 |
|
---|
20 | self.name = name
|
---|
21 | self.fig = plt.figure()
|
---|
22 | self.line, = plt.plot(x, style)
|
---|
23 |
|
---|
24 | plt.title(name)
|
---|
25 | plt.xlabel(xlabel)
|
---|
26 | plt.ylabel(ylabel)
|
---|
27 | plt.grid(True)
|
---|
28 |
|
---|
29 | def __call__(self, ydata):
|
---|
30 | """ set ydata of plot """
|
---|
31 | plt.figure(self.fig.number)
|
---|
32 | plt.ylim( np.min(ydata), np.max(ydata) )
|
---|
33 | self.line.set_ydata(ydata)
|
---|
34 | plt.draw()
|
---|
35 |
|
---|
36 | class Plotter(object):
|
---|
37 | """ simple x-y plot """
|
---|
38 | def __init__(self, name, x=None, style = '.:', xlabel='x', ylabel='y', ion=True, grid=True, fname=None):
|
---|
39 | """ initialize the object """
|
---|
40 |
|
---|
41 | self.name = name
|
---|
42 | self.x = x
|
---|
43 | self.style = style
|
---|
44 | self.xlabel = xlabel
|
---|
45 | self.ylabel = ylabel
|
---|
46 |
|
---|
47 | #not sure if this should go here
|
---|
48 | if ion:
|
---|
49 | plt.ion()
|
---|
50 |
|
---|
51 | self.figure = plt.figure()
|
---|
52 | self.fig_id = self.figure.number
|
---|
53 |
|
---|
54 | plt.grid(grid)
|
---|
55 | self.fname = fname
|
---|
56 |
|
---|
57 | def __call__(self, ydata, label=None):
|
---|
58 | """ set ydata of plot """
|
---|
59 | style = self.style
|
---|
60 |
|
---|
61 | # make acitve and clear
|
---|
62 | plt.figure(self.fig_id)
|
---|
63 | plt.cla()
|
---|
64 |
|
---|
65 | # the following if else stuff is horrible,
|
---|
66 | # but I want all those possibilities, .... still working on it.
|
---|
67 |
|
---|
68 | # check if 1Dim oder 2Dim
|
---|
69 | ydata = np.array(ydata)
|
---|
70 | if ydata.ndim ==1:
|
---|
71 | if self.x==None:
|
---|
72 | plt.plot(ydata, self.style, label=label)
|
---|
73 | else:
|
---|
74 | plt.plot(self.x, ydata, self.style, label=label)
|
---|
75 | else:
|
---|
76 | for i in range(len(ydata)):
|
---|
77 | if self.x==None:
|
---|
78 | if label:
|
---|
79 | plt.plot(ydata[i], style, label=label[i])
|
---|
80 | else:
|
---|
81 | plt.plot(ydata[i], style)
|
---|
82 | else:
|
---|
83 | if label:
|
---|
84 | plt.plot(self.x, ydata[i], style, label=label[i])
|
---|
85 | else:
|
---|
86 | plt.plot(self.x, ydata[i], style)
|
---|
87 | plt.title(self.name)
|
---|
88 | plt.xlabel(self.xlabel)
|
---|
89 | plt.ylabel(self.ylabel)
|
---|
90 | if label:
|
---|
91 | plt.legend()
|
---|
92 |
|
---|
93 | if self.fname != None:
|
---|
94 | plt.savefig(self.fname)
|
---|
95 |
|
---|
96 | plt.draw()
|
---|
97 |
|
---|
98 |
|
---|
99 | class CamPlotter(object):
|
---|
100 | """ plotting data color-coded into FACT-camera """
|
---|
101 | def __init__(self, name, ion=True, grid=True, fname=None, map_file_path = '../map_dn.txt', vmin=None, vmax=None):
|
---|
102 | """ initialize the object """
|
---|
103 | path = os.path.abspath(__file__)
|
---|
104 | path = os.path.dirname(path)
|
---|
105 | map_file_path = os.path.join(path, map_file_path)
|
---|
106 | if not os.path.isfile(map_file_path):
|
---|
107 | print 'not able to find file:', map_file_path
|
---|
108 | sys.exit(-2)
|
---|
109 |
|
---|
110 | self.name = name
|
---|
111 | if ion:
|
---|
112 | plt.ion()
|
---|
113 |
|
---|
114 | chid, y,x,xe,ye,yh,xh,softid,hardid = np.loadtxt(map_file_path ,unpack=True)
|
---|
115 | self.xe = xe
|
---|
116 | self.ye = ye
|
---|
117 |
|
---|
118 | self.H = (6,0,30./180.*3.1415926)
|
---|
119 |
|
---|
120 | self.figure = plt.figure(figsize=(6, 6), dpi=80)
|
---|
121 | self.fig_id = self.figure.number
|
---|
122 |
|
---|
123 | self.grid = grid
|
---|
124 | self.fname = fname
|
---|
125 | self.vmin = vmin
|
---|
126 | self.vmax = vmax
|
---|
127 |
|
---|
128 | def __call__(self, data, mask=None):
|
---|
129 | # define some shortcuts
|
---|
130 | xe = self.xe
|
---|
131 | ye = self.ye
|
---|
132 | H = self.H
|
---|
133 | name = self.name
|
---|
134 | grid = self.grid
|
---|
135 | vmin = self.vmin
|
---|
136 | vmax = self.vmax
|
---|
137 |
|
---|
138 | # get the figure, clean it, and set it up nicely.
|
---|
139 | # maybe cleaning is not necessary and takes long, but
|
---|
140 | # I've got no time to test it at the moment.
|
---|
141 | plt.figure(self.fig_id)
|
---|
142 | plt.clf()
|
---|
143 | self.ax = self.figure.add_subplot(111, aspect='equal')
|
---|
144 | self.ax.axis([-22,22,-22,22])
|
---|
145 | self.ax.set_title(name)
|
---|
146 | self.ax.grid(grid)
|
---|
147 |
|
---|
148 | # throw data into numpy array for simplicity
|
---|
149 | data = np.array(data)
|
---|
150 |
|
---|
151 | #handle masked case specially
|
---|
152 | if mask!= None:
|
---|
153 | if len(mask)==0:
|
---|
154 | return
|
---|
155 |
|
---|
156 | elif mask.dtype == bool and data.ndim ==1 and len(mask)==1440:
|
---|
157 | length = mask.sum()
|
---|
158 | mask = np.where(mask)[0]
|
---|
159 | mxe = np.empty( length )
|
---|
160 | mye = np.empty( length )
|
---|
161 | mdata = np.empty( length )
|
---|
162 | for i,chid in enumerate(mask):
|
---|
163 | #print i , chid
|
---|
164 | mxe[i] = xe[chid]
|
---|
165 | mye[i] = ye[chid]
|
---|
166 | mdata[i] = data[chid]
|
---|
167 | #print 'mxe', mxe, 'len', len(mxe)
|
---|
168 | #print 'mye', mye, 'len', len(mye)
|
---|
169 | #print 'mxe', mdata, 'len', len(mdata)
|
---|
170 |
|
---|
171 | self.ax.axis([-22,22,-22,22])
|
---|
172 | self.ax.set_title(name)
|
---|
173 | self.ax.grid(grid)
|
---|
174 | # the next line is a stupid hack
|
---|
175 | # I plot invisible pixels, so that the axes show look ok.
|
---|
176 | # this must be possible differently, but I don't know how...
|
---|
177 | self.ax.scatter(xe,ye,s=25,alpha=0,marker=H)
|
---|
178 |
|
---|
179 | result = self.ax.scatter(mxe,mye,s=25,alpha=1.,
|
---|
180 | c=mdata, marker=H, linewidths=0., vmin=vmin, vmax=vmax)
|
---|
181 | self.figure.colorbar( result, shrink=0.8, pad=-0.04 )
|
---|
182 | plt.draw()
|
---|
183 |
|
---|
184 |
|
---|
185 | elif mask.dtype == int and data.ndim ==1:
|
---|
186 | length = len(mask)
|
---|
187 | mxe = np.empty( length )
|
---|
188 | mye = np.empty( length )
|
---|
189 | mdata = np.empty( length )
|
---|
190 | for i,chid in enumerate(mask):
|
---|
191 | mxe[i] = xe[chid]
|
---|
192 | mye[i] = ye[chid]
|
---|
193 | mdata[i] = data[chid]
|
---|
194 |
|
---|
195 | self.ax.axis([-22,22,-22,22])
|
---|
196 | self.ax.set_title(name)
|
---|
197 | self.ax.grid(grid)
|
---|
198 | # the next line is a stupid hack
|
---|
199 | # I plot invisible pixels, so that the axes show look ok.
|
---|
200 | # this must be possible differently, but I don't know how...
|
---|
201 | self.ax.scatter(xe,ye,s=25,alpha=0,marker=H)
|
---|
202 |
|
---|
203 | result = self.ax.scatter(mxe,mye,s=25,alpha=1.,
|
---|
204 | c=mdata, marker=H, linewidths=0., vmin=vmin, vmax=vmax)
|
---|
205 | self.figure.colorbar( result, shrink=0.8, pad=-0.04 )
|
---|
206 | plt.draw()
|
---|
207 |
|
---|
208 | else:
|
---|
209 | print "there is a mask, but I don't know how to treat it!!!"
|
---|
210 | sys.exit(-1)
|
---|
211 | else: # i.e. when mask is None
|
---|
212 | # handle 1D and 2D case differently
|
---|
213 | if data.ndim == 1 and len(data)==1440:
|
---|
214 | result = self.ax.scatter(xe,ye,s=25,alpha=1,
|
---|
215 | c=data, marker=H, linewidths=0., vmin=vmin, vmax=vmax)
|
---|
216 | self.figure.colorbar( result, shrink=0.8, pad=-0.04 )
|
---|
217 | plt.draw()
|
---|
218 |
|
---|
219 | elif data.ndim == 2 and data.shape[0] == 2 and data.shape[1] <=1440:
|
---|
220 | # I assume the first row of data, contains the CHIDs
|
---|
221 | # and the 2nd row contains the actual data.
|
---|
222 | chids = data[0]
|
---|
223 | # check if there are double chids in chids
|
---|
224 | if len(chids)!=len(set(chids)):
|
---|
225 | print 'warning: there are doubled chids in input data',
|
---|
226 | print 'you might want to plot something else, but I plot it anyway...'
|
---|
227 | print chids
|
---|
228 | data = data[1]
|
---|
229 | # now I have to mask the xe, and ye vectors accordingly
|
---|
230 | mxe = np.empty( len(chids) )
|
---|
231 | mye = np.empty( len(chids) )
|
---|
232 | for i,chid in enumerate(chids):
|
---|
233 | mxe[i] = xe[chid]
|
---|
234 | mye[i] = ye[chid]
|
---|
235 |
|
---|
236 | # check if I did it right
|
---|
237 | if len(mxe)!=len(data) or len(mye)!=len(data):
|
---|
238 | print 'the masking did not work:'
|
---|
239 | print 'len(mxe)', len(mxe)
|
---|
240 | print 'len(mye)', len(mye)
|
---|
241 | print 'len(data)', len(data)
|
---|
242 |
|
---|
243 | self.ax.axis([-22,22,-22,22])
|
---|
244 | self.ax.set_title(name)
|
---|
245 | self.ax.grid(grid)
|
---|
246 | # the next line is a stupid hack
|
---|
247 | # I plot invisible pixels, so that the axes show look ok.
|
---|
248 | # this must be possible differently, but I don't know how...
|
---|
249 | self.ax.scatter(xe,ye,s=25,alpha=0,marker=H)
|
---|
250 |
|
---|
251 | result = self.ax.scatter(mxe,mye,s=25,alpha=1.,
|
---|
252 | c=data, marker=H, linewidths=0., vmin=vmin, vmax=vmax)
|
---|
253 | self.figure.colorbar( result, shrink=0.8, pad=-0.04 )
|
---|
254 | plt.draw()
|
---|
255 |
|
---|
256 | else:
|
---|
257 | print 'CamPlotter call input data has bad format'
|
---|
258 | print 'data.ndim', data.ndim
|
---|
259 | print 'data.shape', data.shape
|
---|
260 | print 'data:----------------------------------'
|
---|
261 | print data
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|
266 | class HistPlotter(object):
|
---|
267 |
|
---|
268 | def __init__(self, name, bins, range, grid=True, ion=True):
|
---|
269 | """ initialize the object """
|
---|
270 | self.bins = bins
|
---|
271 | self.range = range
|
---|
272 | self.name = name
|
---|
273 | self.figure = plt.figure()
|
---|
274 | self.fig_id = self.figure.number
|
---|
275 | self.grid = grid
|
---|
276 |
|
---|
277 | if ion:
|
---|
278 | plt.ion()
|
---|
279 |
|
---|
280 | def __call__(self, ydata, label=None, log=False):
|
---|
281 | plt.figure(self.fig_id)
|
---|
282 | plt.cla()
|
---|
283 |
|
---|
284 | bins = self.bins
|
---|
285 | range = self.range
|
---|
286 | grid = self.grid
|
---|
287 |
|
---|
288 | ydata = np.array(ydata)
|
---|
289 |
|
---|
290 | if ydata.ndim > 1:
|
---|
291 | ydata = ydata.flatten()
|
---|
292 | if label:
|
---|
293 | plt.hist(ydata, bins, range, label=label, log=log)
|
---|
294 | plt.legend()
|
---|
295 | else:
|
---|
296 | plt.hist(ydata, bins, range, log=log)
|
---|
297 |
|
---|
298 | plt.title(self.name)
|
---|
299 |
|
---|
300 | plt.draw()
|
---|
301 |
|
---|
302 | def _test_SimplePlotter():
|
---|
303 | """ test of maintaining two independant plotter instances """
|
---|
304 | plt.ion()
|
---|
305 |
|
---|
306 | x = np.linspace(0., 10.)
|
---|
307 | plot1 = SimplePlotter('plot1', x, 'r')
|
---|
308 | print 'plot1.fig.number: ', plot1.fig.number
|
---|
309 | plot2 = SimplePlotter('plot2', x, 'g.')
|
---|
310 | print 'plot2.fig.number: ', plot2.fig.number
|
---|
311 |
|
---|
312 | plot1(np.sin(x) * 7.)
|
---|
313 | plot2(x*x)
|
---|
314 |
|
---|
315 | raw_input('next')
|
---|
316 |
|
---|
317 | plot1(np.cos(x) * 3.)
|
---|
318 | plot2(x)
|
---|
319 |
|
---|
320 | raw_input('next')
|
---|
321 |
|
---|
322 |
|
---|
323 | def _test_Plotter():
|
---|
324 | """ test of maintaining two independant plotter instances
|
---|
325 | with different examples for init and call
|
---|
326 | """
|
---|
327 | x = np.linspace(0., 2*np.pi , 100)
|
---|
328 | plot1 = Plotter('plot1', x, 'r.:')
|
---|
329 | plot2 = Plotter('plot2')
|
---|
330 |
|
---|
331 | y1 = np.sin(x) * 7
|
---|
332 | plot1(y1)
|
---|
333 |
|
---|
334 | number_of_graphs_in_plot2 = 3
|
---|
335 | no = number_of_graphs_in_plot2 # short form
|
---|
336 |
|
---|
337 | # this is where you do your analysis...
|
---|
338 | y2 = np.empty( (no, len(x)) ) # prepare some space
|
---|
339 | y2_labels = [] # prepare labels
|
---|
340 | for k in range(no):
|
---|
341 | y2[k] = np.sin( (k+1)*x )
|
---|
342 | y2_labels.append('sin(%d*x)' % (k+1) )
|
---|
343 |
|
---|
344 | # plot the result of your analysis
|
---|
345 | plot2(y2, y2_labels)
|
---|
346 | raw_input('next') # do not forget this line, or your graph is lost
|
---|
347 |
|
---|
348 | plot1(np.cos(x) * 3.)
|
---|
349 | plot2.name += ' without labels!!!' # changing titles 'on the fly' is possible
|
---|
350 | plot2(y2)
|
---|
351 | raw_input('next') # DO NOT forget
|
---|
352 |
|
---|
353 |
|
---|
354 | def _test_CamPlotter():
|
---|
355 | """ test of CamPlotter """
|
---|
356 |
|
---|
357 | c1 = np.array(range(20))
|
---|
358 | chids1 = np.empty( len(c1) , dtype=int)
|
---|
359 | for i in range(len(chids1)-2):
|
---|
360 | chids1[i] = np.random.randint(1440)
|
---|
361 | chids1[-1] = 15
|
---|
362 | chids1[-2] = 15
|
---|
363 |
|
---|
364 | c2 = np.linspace(0., 1., num=1440)
|
---|
365 | plot1 = CamPlotter('plot1')
|
---|
366 | plot2 = CamPlotter('plot2')
|
---|
367 |
|
---|
368 | plot1( (chids1,c1) )
|
---|
369 | plot2(c2)
|
---|
370 | raw_input('next')
|
---|
371 |
|
---|
372 | def _test_HistPlotter():
|
---|
373 | """ test of the HistPlotter """
|
---|
374 | plt.ion()
|
---|
375 |
|
---|
376 | data = np.random.randn(1000)
|
---|
377 | hp = HistPlotter('test hist plotter',34, (-5,4))
|
---|
378 |
|
---|
379 | hp(data, 'test-label')
|
---|
380 | raw_input('next')
|
---|
381 |
|
---|
382 | if __name__ == '__main__':
|
---|
383 | """ test the class """
|
---|
384 | print ' testing SimplePlotter'
|
---|
385 | _test_SimplePlotter()
|
---|
386 | print ' testing Plotter'
|
---|
387 | _test_Plotter()
|
---|
388 | print 'testing CamPlotter ... testing what happens if doubled IDs in mask'
|
---|
389 | _test_CamPlotter()
|
---|
390 | print 'testing basic HistPlotter functionality'
|
---|
391 | _test_HistPlotter()
|
---|
392 | |
---|