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