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