1 | #TODO: the line slicer should listen to all 2DREFRESH events, get the data and slice it |
---|
2 | # before pushing a new 1D data update. |
---|
3 | |
---|
4 | # |
---|
5 | #TODO: NEED MAJOR REFACTOR |
---|
6 | # |
---|
7 | |
---|
8 | |
---|
9 | # Debug printout |
---|
10 | #from config import printEVT |
---|
11 | from BaseInteractor import _BaseInteractor |
---|
12 | from copy import deepcopy |
---|
13 | import math |
---|
14 | |
---|
15 | #from Plotter1D import AddPlotEvent |
---|
16 | import SlicerParameters |
---|
17 | import wx |
---|
18 | |
---|
19 | class BoxSum(_BaseInteractor): |
---|
20 | """ |
---|
21 | Select an annulus through a 2D plot |
---|
22 | """ |
---|
23 | def __init__(self,base,axes,color='black', zorder=3, x_min=0.0025, x_max=0.0025, y_min=0.0025, y_max=0.0025): |
---|
24 | |
---|
25 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
26 | self.markers = [] |
---|
27 | self.axes = axes |
---|
28 | self.qmax = self.base.qmax |
---|
29 | self.connect = self.base.connect |
---|
30 | |
---|
31 | self.xmin= -1* x_min |
---|
32 | self.ymin= -1* y_min |
---|
33 | self.xmax= x_max |
---|
34 | self.ymax= y_max |
---|
35 | # center of the figure |
---|
36 | self.center_x= 0.0 |
---|
37 | self.center_y= 0.0 |
---|
38 | |
---|
39 | ## Number of points on the plot |
---|
40 | self.nbins = 20 |
---|
41 | self.count=0 |
---|
42 | self.error=0 |
---|
43 | |
---|
44 | self.left_line = VerticalLine(self, self.base.subplot,color='blue', zorder=zorder, |
---|
45 | ymin= self.ymin, ymax= self.ymax, |
---|
46 | x= self.xmin, |
---|
47 | center_x= self.center_x, |
---|
48 | center_y= self.center_y) |
---|
49 | self.left_line.qmax = self.base.qmax |
---|
50 | |
---|
51 | self.right_line= VerticalLine(self, self.base.subplot,color='black', zorder=zorder, |
---|
52 | ymin= self.ymin, ymax= self.ymax, |
---|
53 | x=self.xmax, |
---|
54 | center_x= self.center_x, |
---|
55 | center_y= self.center_y) |
---|
56 | self.right_line.qmax = self.base.qmax |
---|
57 | |
---|
58 | self.top_line= HorizontalLine(self, self.base.subplot,color='green', zorder=zorder, |
---|
59 | y= self.ymax, |
---|
60 | xmin= self.xmin, xmax= self.xmax, |
---|
61 | center_x= self.center_x, |
---|
62 | center_y= self.center_y) |
---|
63 | self.top_line.qmax = self.base.qmax |
---|
64 | |
---|
65 | self.bottom_line= HorizontalLine(self, self.base.subplot,color='red', zorder=zorder, |
---|
66 | y =self.ymin, |
---|
67 | xmin= self.xmin, xmax= self.xmax, |
---|
68 | center_x= self.center_x, |
---|
69 | center_y= self.center_y) |
---|
70 | self.bottom_line.qmax = self.base.qmax |
---|
71 | #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) |
---|
72 | |
---|
73 | self.connect_markers([]) |
---|
74 | |
---|
75 | self.update() |
---|
76 | #self._post_data() |
---|
77 | |
---|
78 | # Bind to slice parameter events |
---|
79 | self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
80 | |
---|
81 | |
---|
82 | def _onEVT_SLICER_PARS(self, event): |
---|
83 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
84 | event.Skip() |
---|
85 | if event.type == self.__class__.__name__: |
---|
86 | #self.set_params(event.params) |
---|
87 | self.base.update() |
---|
88 | |
---|
89 | def update_and_post(self): |
---|
90 | self.update() |
---|
91 | self._post_data() |
---|
92 | |
---|
93 | def save_data(self, path, image, x, y): |
---|
94 | output = open(path, 'w') |
---|
95 | |
---|
96 | data_x, data_y = self.get_data(image, x, y) |
---|
97 | |
---|
98 | output.write("<phi> <average>\n") |
---|
99 | for i in range(len(data_x)): |
---|
100 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
101 | output.close() |
---|
102 | |
---|
103 | def set_layer(self, n): |
---|
104 | self.layernum = n |
---|
105 | self.update() |
---|
106 | |
---|
107 | def clear(self): |
---|
108 | self.clear_markers() |
---|
109 | self.left_line.clear() |
---|
110 | self.right_line.clear() |
---|
111 | self.top_line.clear() |
---|
112 | self.bottom_line.clear() |
---|
113 | |
---|
114 | #self.base.connect.disconnect() |
---|
115 | self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS) |
---|
116 | |
---|
117 | def update(self): |
---|
118 | """ |
---|
119 | Respond to changes in the model by recalculating the profiles and |
---|
120 | resetting the widgets. |
---|
121 | """ |
---|
122 | if self.left_line.has_move: |
---|
123 | print "left has moved" |
---|
124 | self.left_line.update(mline=[self.center_x, self.center_y],translation=True) |
---|
125 | |
---|
126 | #self.right_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
127 | self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
128 | mline= [self.center_x, self.center_y],translation=True) |
---|
129 | self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
130 | mline= [self.center_x, self.center_y],translation=True) |
---|
131 | if self.right_line.has_move: |
---|
132 | print "right has moved" |
---|
133 | self.right_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
134 | #self.left_line.update(mline=[self.center_x, self.center_y],translation=True) |
---|
135 | #self.left_line.update(xmin= self.right_line.x ,xmax=-1*self.right_line.x) |
---|
136 | self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
137 | mline= [self.center_x, self.center_y],translation=True) |
---|
138 | self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
139 | mline= [self.center_x, self.center_y],translation=True) |
---|
140 | |
---|
141 | |
---|
142 | if self.bottom_line.has_move: |
---|
143 | print "bottom has moved" |
---|
144 | self.bottom_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
145 | #self.top_line.update(y= -1*self.top_line.y,translation=True) |
---|
146 | self.left_line.update( ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
147 | mline= [self.center_x, self.center_y],translation=True) |
---|
148 | self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
149 | mline= [self.center_x, self.center_y],translation=True) |
---|
150 | |
---|
151 | if self.top_line.has_move: |
---|
152 | print "top has moved" |
---|
153 | self.top_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
154 | |
---|
155 | #self.bottom_line.update(y= -1*self.top_line.y,mline= [self.center_x, self.center_y], |
---|
156 | # translation=True ) |
---|
157 | self.left_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
158 | mline= [self.center_x, self.center_y],translation=True) |
---|
159 | self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
160 | mline= [self.center_x, self.center_y],translation=True) |
---|
161 | |
---|
162 | |
---|
163 | def save(self, ev): |
---|
164 | """ |
---|
165 | Remember the roughness for this layer and the next so that we |
---|
166 | can restore on Esc. |
---|
167 | """ |
---|
168 | self.base.freeze_axes() |
---|
169 | self.inner_circle.save(ev) |
---|
170 | self.outer_circle.save(ev) |
---|
171 | |
---|
172 | def _post_data(self): |
---|
173 | # Compute data |
---|
174 | data = self.base.data2D |
---|
175 | from DataLoader.manipulations import Boxavg |
---|
176 | radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2)) |
---|
177 | x_min= self.left_line.x |
---|
178 | x_max= self.right_line.x |
---|
179 | y_min= self.bottom_line.y |
---|
180 | y_max= self.top_line.y |
---|
181 | box = Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
182 | |
---|
183 | self.count, self.error= box(self.base.data2D) |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | def moveend(self, ev): |
---|
189 | self.base.thaw_axes() |
---|
190 | |
---|
191 | # Post paramters |
---|
192 | event = SlicerParameters.SlicerParameterEvent() |
---|
193 | event.type = self.__class__.__name__ |
---|
194 | #event.params = self.get_params() |
---|
195 | wx.PostEvent(self.base.parent, event) |
---|
196 | |
---|
197 | self._post_data() |
---|
198 | |
---|
199 | def restore(self): |
---|
200 | """ |
---|
201 | Restore the roughness for this layer. |
---|
202 | """ |
---|
203 | self.inner_circle.restore() |
---|
204 | self.outer_circle.restore() |
---|
205 | |
---|
206 | def move(self, x, y, ev): |
---|
207 | """ |
---|
208 | Process move to a new position, making sure that the move is allowed. |
---|
209 | """ |
---|
210 | print "in move" |
---|
211 | if self.xmin <= x and x<= self.xmax: |
---|
212 | print "has move whole", x |
---|
213 | |
---|
214 | def set_cursor(self, x, y): |
---|
215 | pass |
---|
216 | |
---|
217 | def get_params(self): |
---|
218 | params = {} |
---|
219 | params["x_min"] = self.left_line.x |
---|
220 | params["x_max"] = self.right_line.x |
---|
221 | params["y_min"] = self.bottom_line.y |
---|
222 | params["y_max"] = self.top_line.y |
---|
223 | params["count"] = self.count |
---|
224 | params["error"] = self.error |
---|
225 | params["center_x"] = self.center_x |
---|
226 | params["center_y"] = self.center_y |
---|
227 | return params |
---|
228 | |
---|
229 | def set_params(self, params): |
---|
230 | |
---|
231 | x_min = params["x_min"] |
---|
232 | x_max = params["x_max"] |
---|
233 | y_min = params["y_min"] |
---|
234 | y_max = params["y_max"] |
---|
235 | theta = params["theta"] |
---|
236 | center_x = params["center_x"] |
---|
237 | center_y = params["center_y"] |
---|
238 | |
---|
239 | self.left_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max) |
---|
240 | self.right_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max) |
---|
241 | self.top_line.update(mline= [center_x, center_y], xmin= x_min ,xmax= xmax) |
---|
242 | self.bottom_line.update(mline= [center_x, center_y],xmin= xmin ,xmax= xmax) |
---|
243 | |
---|
244 | |
---|
245 | self._post_data() |
---|
246 | def freeze_axes(self): |
---|
247 | self.base.freeze_axes() |
---|
248 | |
---|
249 | def thaw_axes(self): |
---|
250 | self.base.thaw_axes() |
---|
251 | |
---|
252 | def draw(self): |
---|
253 | self.base.draw() |
---|
254 | |
---|
255 | class HorizontalLine(_BaseInteractor): |
---|
256 | """ |
---|
257 | Select an annulus through a 2D plot |
---|
258 | """ |
---|
259 | def __init__(self,base,axes,color='black', zorder=5, y=0.5, |
---|
260 | xmin=0.0,xmax=0.5, |
---|
261 | center_x= 0.0, |
---|
262 | center_y= 0.0): |
---|
263 | |
---|
264 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
265 | self.markers = [] |
---|
266 | self.axes = axes |
---|
267 | # center |
---|
268 | self.center_x = center_x |
---|
269 | self.center_y = center_y |
---|
270 | |
---|
271 | self.y= y - self.center_y |
---|
272 | self.save_y= y- self.center_y |
---|
273 | |
---|
274 | self.xmin = xmin - self.center_x |
---|
275 | self.save_xmin = xmin - self.center_x |
---|
276 | self.xmax = xmax - self.center_x |
---|
277 | self.save_xmax = xmax - self.center_x |
---|
278 | |
---|
279 | |
---|
280 | self.line = self.axes.plot([self.xmin,self.xmax],[self.y,self.y], |
---|
281 | linestyle='-', marker='', |
---|
282 | color=self.color, |
---|
283 | visible=True)[0] |
---|
284 | |
---|
285 | |
---|
286 | self.npts = 20 |
---|
287 | self.has_move=False |
---|
288 | self.connect_markers([self.line]) |
---|
289 | self.update() |
---|
290 | |
---|
291 | def set_layer(self, n): |
---|
292 | self.layernum = n |
---|
293 | self.update() |
---|
294 | |
---|
295 | def clear(self): |
---|
296 | self.clear_markers() |
---|
297 | try: |
---|
298 | |
---|
299 | self.line.remove() |
---|
300 | except: |
---|
301 | # Old version of matplotlib |
---|
302 | for item in range(len(self.axes.lines)): |
---|
303 | del self.axes.lines[0] |
---|
304 | |
---|
305 | def get_radius(self): |
---|
306 | |
---|
307 | return 0 |
---|
308 | |
---|
309 | def update(self,xmin=None, xmax=None,y=None, mline=None,translation=False): |
---|
310 | """ |
---|
311 | Draw the new roughness on the graph. |
---|
312 | """ |
---|
313 | #print "update main line", self.has_move |
---|
314 | if xmin !=None: |
---|
315 | self.xmin = xmin # - self.center_x |
---|
316 | if xmax !=None: |
---|
317 | self.xmax = xmax #- self.center_x |
---|
318 | if y !=None: |
---|
319 | self.y = y #- self.center_y |
---|
320 | |
---|
321 | self.line.set(xdata=[self.xmin,self.xmax], ydata=[self.y,self.y]) |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | def save(self, ev): |
---|
326 | """ |
---|
327 | Remember the roughness for this layer and the next so that we |
---|
328 | can restore on Esc. |
---|
329 | """ |
---|
330 | self.save_xmin= self.xmin |
---|
331 | self.save_xmax= self.xmax |
---|
332 | |
---|
333 | self.save_y= self.y |
---|
334 | self.base.freeze_axes() |
---|
335 | |
---|
336 | def moveend(self, ev): |
---|
337 | |
---|
338 | self.has_move=False |
---|
339 | self.base.moveend(ev) |
---|
340 | |
---|
341 | def restore(self): |
---|
342 | """ |
---|
343 | Restore the roughness for this layer. |
---|
344 | """ |
---|
345 | self.y= self.save_y |
---|
346 | |
---|
347 | def move(self, x, y, ev): |
---|
348 | """ |
---|
349 | Process move to a new position, making sure that the move is allowed. |
---|
350 | """ |
---|
351 | self.y= y - self.center_y |
---|
352 | |
---|
353 | self.has_move=True |
---|
354 | self.base.base.update() |
---|
355 | |
---|
356 | def set_cursor(self, x, y): |
---|
357 | self.move(x, y, None) |
---|
358 | self.update() |
---|
359 | |
---|
360 | |
---|
361 | def get_params(self): |
---|
362 | params = {} |
---|
363 | params["radius"] = self.xmin |
---|
364 | params["theta"] = self.xmax |
---|
365 | return params |
---|
366 | |
---|
367 | def set_params(self, params): |
---|
368 | |
---|
369 | x = params["radius"] |
---|
370 | self.set_cursor(x, self._inner_mouse_y) |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | class VerticalLine(_BaseInteractor): |
---|
376 | """ |
---|
377 | Select an annulus through a 2D plot |
---|
378 | """ |
---|
379 | def __init__(self,base,axes,color='black', zorder=5, ymin=0.0, |
---|
380 | ymax=0.5,x= 0.5, |
---|
381 | center_x= 0, |
---|
382 | center_y= 0): |
---|
383 | |
---|
384 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
385 | self.markers = [] |
---|
386 | self.axes = axes |
---|
387 | # x coordinate of the vertical line |
---|
388 | self.center_x= center_x |
---|
389 | self.center_y= center_y |
---|
390 | self.x = x - center_x |
---|
391 | self.save_x = x - center_x |
---|
392 | # minimum value of y coordinate of the vertical line |
---|
393 | self.ymin = ymin - center_y |
---|
394 | self.save_ymin = ymin - center_y |
---|
395 | # maximum value of y coordinate of the vertical line |
---|
396 | self.ymax= ymax - center_y |
---|
397 | self.save_ymax= ymax - center_y |
---|
398 | |
---|
399 | # Draw vertical line |
---|
400 | self.line = self.axes.plot([self.x,self.x],[self.ymin,self.ymax], |
---|
401 | linestyle='-', marker='', |
---|
402 | color=self.color, |
---|
403 | visible=True)[0] |
---|
404 | |
---|
405 | self.npts = 20 |
---|
406 | # Check vertical line motion |
---|
407 | self.has_move=False |
---|
408 | self.connect_markers([self.line]) |
---|
409 | self.update() |
---|
410 | |
---|
411 | def set_layer(self, n): |
---|
412 | self.layernum = n |
---|
413 | self.update() |
---|
414 | |
---|
415 | def clear(self): |
---|
416 | self.clear_markers() |
---|
417 | try: |
---|
418 | |
---|
419 | self.line.remove() |
---|
420 | except: |
---|
421 | # Old version of matplotlib |
---|
422 | for item in range(len(self.axes.lines)): |
---|
423 | del self.axes.lines[0] |
---|
424 | |
---|
425 | def get_radius(self): |
---|
426 | return 0 |
---|
427 | |
---|
428 | def update(self,x=None,ymin=None, ymax=None, mline=None,translation=False): |
---|
429 | """ |
---|
430 | Draw the new roughness on the graph. |
---|
431 | """ |
---|
432 | if x!=None: |
---|
433 | self.x = x #-self.center_x |
---|
434 | if ymin !=None: |
---|
435 | self.ymin = ymin #- self.center_y |
---|
436 | if ymax !=None: |
---|
437 | self.ymax = ymax #- self.center_y |
---|
438 | |
---|
439 | self.line.set(xdata=[self.x,self.x], ydata=[self.ymin,self.ymax]) |
---|
440 | |
---|
441 | |
---|
442 | |
---|
443 | def save(self, ev): |
---|
444 | """ |
---|
445 | Remember the roughness for this layer and the next so that we |
---|
446 | can restore on Esc. |
---|
447 | """ |
---|
448 | self.save_x= self.x |
---|
449 | self.save_ymin= self.ymin |
---|
450 | self.save_ymax= self.ymax |
---|
451 | |
---|
452 | self.base.freeze_axes() |
---|
453 | |
---|
454 | def moveend(self, ev): |
---|
455 | |
---|
456 | self.has_move=False |
---|
457 | self.base.moveend(ev) |
---|
458 | |
---|
459 | def restore(self): |
---|
460 | """ |
---|
461 | Restore the roughness for this layer. |
---|
462 | """ |
---|
463 | self.x = self.save_x |
---|
464 | |
---|
465 | self.ymin=self.save_ymin |
---|
466 | self.ymax=self.save_ymax |
---|
467 | def move(self, x, y, ev): |
---|
468 | """ |
---|
469 | Process move to a new position, making sure that the move is allowed. |
---|
470 | """ |
---|
471 | self.x = x - self.center_x |
---|
472 | |
---|
473 | self.has_move=True |
---|
474 | self.base.base.update() |
---|
475 | |
---|
476 | |
---|
477 | def set_cursor(self, x, y): |
---|
478 | self.move(x, y, None) |
---|
479 | self.update() |
---|
480 | |
---|
481 | |
---|
482 | def get_params(self): |
---|
483 | params = {} |
---|
484 | params["x"] = self.xmin |
---|
485 | params["ymin"] = self.ymin |
---|
486 | params["ymax"] = self.ymax |
---|
487 | return params |
---|
488 | |
---|
489 | def set_params(self, params): |
---|
490 | """ |
---|
491 | Draw a vertical line given some value of params |
---|
492 | @param params: a dictionary containing value for x, ymin , ymax to draw |
---|
493 | a vertical line |
---|
494 | """ |
---|
495 | x = params["x"] |
---|
496 | ymin = params["ymin"] |
---|
497 | ymax = params["ymax"] |
---|
498 | #self.set_cursor(x, self._inner_mouse_y) |
---|
499 | self.update(self,x =x,ymin =ymin, ymax =ymax) |
---|
500 | |
---|
501 | |
---|
502 | |
---|