1 | |
---|
2 | |
---|
3 | |
---|
4 | import math |
---|
5 | import wx |
---|
6 | from copy import deepcopy |
---|
7 | from BaseInteractor import _BaseInteractor |
---|
8 | from sans.guicomm.events import SlicerParamUpdateEvent,EVT_SLICER_PARS,StatusEvent |
---|
9 | |
---|
10 | |
---|
11 | class BoxSum(_BaseInteractor): |
---|
12 | """ |
---|
13 | Boxsum Class: determine 2 rectangular area to compute the sum of pixel of |
---|
14 | a Data. |
---|
15 | Uses PointerInteractor , VerticalDoubleLine,HorizontalDoubleLine. |
---|
16 | @param zorder: Artists with lower zorder values are drawn first. |
---|
17 | @param x_min: the minimum value of the x coordinate |
---|
18 | @param x_max: the maximum value of the x coordinate |
---|
19 | @param y_min: the minimum value of the y coordinate |
---|
20 | @param y_max: the maximum value of the y coordinate |
---|
21 | |
---|
22 | """ |
---|
23 | def __init__(self,base,axes,color='black', zorder=3, x_min=0.008, |
---|
24 | x_max=0.008, y_min=0.0025, y_max=0.0025): |
---|
25 | |
---|
26 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
27 | ## class initialization |
---|
28 | ## list of Boxsmun markers |
---|
29 | self.markers = [] |
---|
30 | self.axes = axes |
---|
31 | ## connect the artist for the motion |
---|
32 | self.connect = self.base.connect |
---|
33 | ## when qmax is reached the selected line is reset the its previous value |
---|
34 | self.qmax = min(self.base.data2D.xmax, self.base.data2D.xmin) |
---|
35 | ## Define the boxsum limits |
---|
36 | self.xmin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
37 | self.ymin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
38 | |
---|
39 | self.xmax= 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
40 | self.ymax= 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
41 | ## center of the boxSum |
---|
42 | self.center_x= 0.0002 |
---|
43 | self.center_y= 0.0003 |
---|
44 | ## Number of points on the plot |
---|
45 | self.nbins = 20 |
---|
46 | ## Define initial result the summation |
---|
47 | self.count=0 |
---|
48 | self.error=0 |
---|
49 | ## Flag to determine if the current figure has moved |
---|
50 | ## set to False == no motion , set to True== motion |
---|
51 | self.has_move= False |
---|
52 | ## Create Boxsum edges |
---|
53 | self.horizontal_lines= HorizontalDoubleLine(self, self.base.subplot,color='blue', |
---|
54 | zorder=zorder, |
---|
55 | y= self.ymax, |
---|
56 | x= self.xmax, |
---|
57 | center_x= self.center_x, |
---|
58 | center_y= self.center_y) |
---|
59 | self.horizontal_lines.qmax = self.qmax |
---|
60 | |
---|
61 | self.vertical_lines= VerticalDoubleLine(self, self.base.subplot,color='black', |
---|
62 | zorder=zorder, |
---|
63 | y= self.ymax, |
---|
64 | x= self.xmax, |
---|
65 | center_x= self.center_x, |
---|
66 | center_y= self.center_y) |
---|
67 | self.vertical_lines.qmax = self.qmax |
---|
68 | |
---|
69 | self.center= PointInteractor(self, self.base.subplot,color='grey', |
---|
70 | zorder=zorder, |
---|
71 | center_x= self.center_x, |
---|
72 | center_y= self.center_y) |
---|
73 | ## Save the name of the slicer panel associate with this slicer |
---|
74 | self.panel_name="" |
---|
75 | ## Update and post slicer parameters |
---|
76 | self.update() |
---|
77 | self._post_data() |
---|
78 | ## Bind to slice parameter events |
---|
79 | self.base.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
80 | |
---|
81 | |
---|
82 | def set_panel_name(self, name): |
---|
83 | """ |
---|
84 | Store the name of the panel associated to this slicer |
---|
85 | @param name: the name of this panel |
---|
86 | """ |
---|
87 | self.panel_name= name |
---|
88 | |
---|
89 | |
---|
90 | def _onEVT_SLICER_PARS(self, event): |
---|
91 | """ |
---|
92 | receive an event containing parameters values to reset the slicer |
---|
93 | @param event: event of type SlicerParameterEvent with params as |
---|
94 | attribute |
---|
95 | """ |
---|
96 | ## Post e message to declare what kind of event has being received |
---|
97 | wx.PostEvent(self.base.parent, StatusEvent(status="Boxsum._onEVT_SLICER_PARS")) |
---|
98 | event.Skip() |
---|
99 | ## reset the slicer with the values contains the event.params dictionary |
---|
100 | if event.type == self.__class__.__name__: |
---|
101 | self.set_params(event.params) |
---|
102 | self.base.update() |
---|
103 | |
---|
104 | |
---|
105 | def set_layer(self, n): |
---|
106 | """ |
---|
107 | Allow adding plot to the same panel |
---|
108 | @param n: the number of layer |
---|
109 | """ |
---|
110 | self.layernum = n |
---|
111 | self.update() |
---|
112 | |
---|
113 | |
---|
114 | def clear(self): |
---|
115 | """ |
---|
116 | Clear the slicer and all connected events related to this slicer |
---|
117 | """ |
---|
118 | self.clear_markers() |
---|
119 | self.horizontal_lines.clear() |
---|
120 | self.vertical_lines.clear() |
---|
121 | self.center.clear() |
---|
122 | self.base.connect.clearall() |
---|
123 | self.base.Unbind(EVT_SLICER_PARS) |
---|
124 | |
---|
125 | |
---|
126 | def update(self): |
---|
127 | """ |
---|
128 | Respond to changes in the model by recalculating the profiles and |
---|
129 | resetting the widgets. |
---|
130 | """ |
---|
131 | ## check if the center point has moved and update the figure accordingly |
---|
132 | if self.center.has_move: |
---|
133 | self.center.update() |
---|
134 | self.horizontal_lines.update( center= self.center) |
---|
135 | self.vertical_lines.update( center= self.center) |
---|
136 | ## check if the horizontal lines have moved and update the figure accordingly |
---|
137 | if self.horizontal_lines.has_move: |
---|
138 | self.horizontal_lines.update() |
---|
139 | self.vertical_lines.update(y1=self.horizontal_lines.y1, |
---|
140 | y2=self.horizontal_lines.y2, |
---|
141 | height= self.horizontal_lines.half_height ) |
---|
142 | ## check if the vertical lines have moved and update the figure accordingly |
---|
143 | if self.vertical_lines.has_move: |
---|
144 | self.vertical_lines.update() |
---|
145 | self.horizontal_lines.update(x1=self.vertical_lines.x1, |
---|
146 | x2=self.vertical_lines.x2, |
---|
147 | width=self.vertical_lines.half_width) |
---|
148 | |
---|
149 | |
---|
150 | def save(self, ev): |
---|
151 | """ |
---|
152 | Remember the roughness for this layer and the next so that we |
---|
153 | can restore on Esc. |
---|
154 | """ |
---|
155 | self.base.freeze_axes() |
---|
156 | self.horizontal_lines.save(ev) |
---|
157 | self.vertical_lines.save(ev) |
---|
158 | self.center.save(ev) |
---|
159 | |
---|
160 | def _post_data(self): |
---|
161 | """ |
---|
162 | Get the limits of the boxsum and compute the sum of the pixel |
---|
163 | contained in that region and the error on that sum |
---|
164 | """ |
---|
165 | ## Data 2D for which the pixel will be summed |
---|
166 | data = self.base.data2D |
---|
167 | ## the region of the summation |
---|
168 | x_min= self.horizontal_lines.x2 |
---|
169 | x_max= self.horizontal_lines.x1 |
---|
170 | y_min= self.vertical_lines.y2 |
---|
171 | y_max= self.vertical_lines.y1 |
---|
172 | ##computation of the sum and its error |
---|
173 | from DataLoader.manipulations import Boxavg |
---|
174 | box = Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
175 | self.count, self.error = box(self.base.data2D) |
---|
176 | |
---|
177 | |
---|
178 | def moveend(self, ev): |
---|
179 | """ |
---|
180 | After a dragging motion this function is called to compute |
---|
181 | the error and the sum of pixel of a given data 2D |
---|
182 | """ |
---|
183 | self.base.thaw_axes() |
---|
184 | ## compute error an d sum of data's pixel |
---|
185 | self._post_data() |
---|
186 | ## Create and event ( posted to guiframe)that set the |
---|
187 | ##current slicer parameter to a panel of name self.panel_name |
---|
188 | self.type= self.__class__.__name__ |
---|
189 | params= self.get_params() |
---|
190 | event = SlicerParamUpdateEvent(type=self.type, params=params, |
---|
191 | panel_name= self.panel_name) |
---|
192 | wx.PostEvent(self.base.parent, event) |
---|
193 | |
---|
194 | |
---|
195 | def restore(self): |
---|
196 | """ |
---|
197 | Restore the roughness for this layer. |
---|
198 | """ |
---|
199 | self.horizontal_lines.restore() |
---|
200 | self.vertical_lines.restore() |
---|
201 | self.center.restore() |
---|
202 | |
---|
203 | |
---|
204 | def move(self, x, y, ev): |
---|
205 | """ |
---|
206 | Process move to a new position, making sure that the move is allowed. |
---|
207 | """ |
---|
208 | pass |
---|
209 | |
---|
210 | |
---|
211 | def set_cursor(self, x, y): |
---|
212 | pass |
---|
213 | |
---|
214 | |
---|
215 | def get_params(self): |
---|
216 | """ |
---|
217 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
218 | @return params: the dictionary created |
---|
219 | """ |
---|
220 | params = {} |
---|
221 | params["Width"] = math.fabs(self.vertical_lines.half_width)*2 |
---|
222 | params["Height"] = math.fabs(self.horizontal_lines.half_height)*2 |
---|
223 | params["center_x"] = self.center.x |
---|
224 | params["center_y"] =self.center.y |
---|
225 | params["count"] = self.count |
---|
226 | params["errors"]= self.error |
---|
227 | return params |
---|
228 | |
---|
229 | |
---|
230 | def get_result(self): |
---|
231 | """ |
---|
232 | return the result of box summation |
---|
233 | """ |
---|
234 | result={} |
---|
235 | result["count"] = self.count |
---|
236 | result["error"] = self.error |
---|
237 | return result |
---|
238 | |
---|
239 | |
---|
240 | def set_params(self, params): |
---|
241 | """ |
---|
242 | Receive a dictionary and reset the slicer with values contained |
---|
243 | in the values of the dictionary. |
---|
244 | @param params: a dictionary containing name of slicer parameters and |
---|
245 | values the user assigned to the slicer. |
---|
246 | """ |
---|
247 | x_max = math.fabs(params["Width"] )/2 |
---|
248 | y_max = math.fabs(params["Height"] )/2 |
---|
249 | |
---|
250 | self.center_x=params["center_x"] |
---|
251 | self.center_y=params["center_y"] |
---|
252 | #update the slicer given values of params |
---|
253 | self.center.update(center_x=self.center_x,center_y=self.center_y) |
---|
254 | self.horizontal_lines.update(center= self.center, |
---|
255 | width=x_max, |
---|
256 | height=y_max) |
---|
257 | self.vertical_lines.update(center= self.center, |
---|
258 | width=x_max, |
---|
259 | height=y_max) |
---|
260 | #compute the new error and sum given values of params |
---|
261 | self._post_data() |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | def freeze_axes(self): |
---|
266 | self.base.freeze_axes() |
---|
267 | |
---|
268 | |
---|
269 | def thaw_axes(self): |
---|
270 | self.base.thaw_axes() |
---|
271 | |
---|
272 | |
---|
273 | def draw(self): |
---|
274 | self.base.draw() |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | class PointInteractor(_BaseInteractor): |
---|
279 | """ |
---|
280 | Draw a point that can be dragged with the marker. |
---|
281 | this class controls the motion the center of the BoxSum |
---|
282 | """ |
---|
283 | def __init__(self,base,axes,color='black', zorder=5, |
---|
284 | center_x= 0.0, |
---|
285 | center_y= 0.0): |
---|
286 | |
---|
287 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
288 | ## Initialization the class |
---|
289 | self.markers = [] |
---|
290 | self.axes = axes |
---|
291 | # center coordinates |
---|
292 | self.x = center_x |
---|
293 | self.y = center_y |
---|
294 | ## saved value of the center coordinates |
---|
295 | self.save_x = center_x |
---|
296 | self.save_y = center_y |
---|
297 | ## Create a marker |
---|
298 | try: |
---|
299 | self.center_marker = self.axes.plot([self.x],[self.y], linestyle='', |
---|
300 | marker='s', markersize=10, |
---|
301 | color=self.color, alpha=0.6, |
---|
302 | pickradius=5, label="pick", |
---|
303 | zorder=zorder, # Prefer this to other lines |
---|
304 | visible=True)[0] |
---|
305 | except: |
---|
306 | self.center_marker = self.axes.plot([self.x],[self.y], linestyle='', |
---|
307 | marker='s', markersize=10, |
---|
308 | color=self.color, alpha=0.6, |
---|
309 | label="pick", |
---|
310 | visible=True)[0] |
---|
311 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
312 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
313 | owner=self.base.base.parent |
---|
314 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
315 | |
---|
316 | ## Draw a point |
---|
317 | self.center = self.axes.plot([self.x],[self.y], |
---|
318 | linestyle='-', marker='', |
---|
319 | color=self.color, |
---|
320 | visible=True)[0] |
---|
321 | ## Flag to determine the motion this point |
---|
322 | self.has_move=False |
---|
323 | ## connecting the marker to allow them to move |
---|
324 | self.connect_markers([self.center_marker]) |
---|
325 | ## Update the figure |
---|
326 | self.update() |
---|
327 | |
---|
328 | |
---|
329 | def set_layer(self, n): |
---|
330 | """ |
---|
331 | Allow adding plot to the same panel |
---|
332 | @param n: the number of layer |
---|
333 | """ |
---|
334 | self.layernum = n |
---|
335 | self.update() |
---|
336 | |
---|
337 | |
---|
338 | def clear(self): |
---|
339 | """ |
---|
340 | Clear this figure and its markers |
---|
341 | """ |
---|
342 | self.clear_markers() |
---|
343 | try: |
---|
344 | self.center.remove() |
---|
345 | self.center_marker.remove() |
---|
346 | except: |
---|
347 | # Old version of matplotlib |
---|
348 | for item in range(len(self.axes.lines)): |
---|
349 | del self.axes.lines[0] |
---|
350 | |
---|
351 | |
---|
352 | def update(self, center_x=None,center_y=None): |
---|
353 | """ |
---|
354 | Draw the new roughness on the graph. |
---|
355 | """ |
---|
356 | if center_x !=None: self.x= center_x |
---|
357 | if center_y !=None: self.y= center_y |
---|
358 | |
---|
359 | self.center_marker.set(xdata=[self.x], ydata=[self.y]) |
---|
360 | self.center.set(xdata=[self.x], ydata=[self.y]) |
---|
361 | |
---|
362 | |
---|
363 | def save(self, ev): |
---|
364 | """ |
---|
365 | Remember the roughness for this layer and the next so that we |
---|
366 | can restore on Esc. |
---|
367 | """ |
---|
368 | self.save_x= self.x |
---|
369 | self.save_y= self.y |
---|
370 | self.base.freeze_axes() |
---|
371 | |
---|
372 | |
---|
373 | def moveend(self, ev): |
---|
374 | |
---|
375 | self.has_move=False |
---|
376 | self.base.moveend(ev) |
---|
377 | |
---|
378 | def restore(self): |
---|
379 | """ |
---|
380 | Restore the roughness for this layer. |
---|
381 | """ |
---|
382 | self.y= self.save_y |
---|
383 | self.x= self.save_x |
---|
384 | |
---|
385 | |
---|
386 | def move(self, x, y, ev): |
---|
387 | """ |
---|
388 | Process move to a new position, making sure that the move is allowed. |
---|
389 | """ |
---|
390 | self.x= x |
---|
391 | self.y= y |
---|
392 | |
---|
393 | self.has_move=True |
---|
394 | self.base.base.update() |
---|
395 | |
---|
396 | |
---|
397 | def set_cursor(self, x, y): |
---|
398 | self.move(x, y, None) |
---|
399 | self.update() |
---|
400 | |
---|
401 | |
---|
402 | def get_params(self): |
---|
403 | params = {} |
---|
404 | params["x"] = self.x |
---|
405 | params["y"] = self.y |
---|
406 | return params |
---|
407 | |
---|
408 | |
---|
409 | def set_params(self, params): |
---|
410 | center_x = params["x"] |
---|
411 | center_y = params["y"] |
---|
412 | self.update(center_x=center_x,center_y=center_y) |
---|
413 | |
---|
414 | |
---|
415 | class VerticalDoubleLine(_BaseInteractor): |
---|
416 | """ |
---|
417 | Draw 2 vertical lines moving in opposite direction and centered on |
---|
418 | a point (PointInteractor) |
---|
419 | """ |
---|
420 | def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5, |
---|
421 | center_x= 0.0, |
---|
422 | center_y= 0.0): |
---|
423 | |
---|
424 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
425 | ## Initialization the class |
---|
426 | self.markers = [] |
---|
427 | self.axes = axes |
---|
428 | ## Center coordinates |
---|
429 | self.center_x = center_x |
---|
430 | self.center_y = center_y |
---|
431 | ## defined end points vertical lignes and their saved values |
---|
432 | self.y1 = y + self.center_y |
---|
433 | self.save_y1= self.y1 |
---|
434 | |
---|
435 | delta= self.y1- self.center_y |
---|
436 | self.y2= self.center_y - delta |
---|
437 | self.save_y2= self.y2 |
---|
438 | |
---|
439 | self.x1 = x + self.center_x |
---|
440 | self.save_x1 = self.x1 |
---|
441 | |
---|
442 | delta= self.x1- self.center_x |
---|
443 | self.x2= self.center_x - delta |
---|
444 | self.save_x2 = self.x2 |
---|
445 | ## save the color of the line |
---|
446 | self.color = color |
---|
447 | ## the height of the rectangle |
---|
448 | self.half_height= math.fabs(y) |
---|
449 | self.save_half_height= math.fabs(y) |
---|
450 | ## the with of the rectangle |
---|
451 | self.half_width= math.fabs(self.x1- self.x2)/2 |
---|
452 | self.save_half_width=math.fabs(self.x1- self.x2)/2 |
---|
453 | ## Create marker |
---|
454 | try: |
---|
455 | self.right_marker = self.axes.plot([self.x1],[0], linestyle='', |
---|
456 | marker='s', markersize=10, |
---|
457 | color=self.color, alpha=0.6, |
---|
458 | pickradius=5, label="pick", |
---|
459 | zorder=zorder, # Prefer this to other lines |
---|
460 | visible=True)[0] |
---|
461 | except: |
---|
462 | self.right_marker = self.axes.plot([self.x1],[0], linestyle='', |
---|
463 | marker='s', markersize=10, |
---|
464 | color=self.color, alpha=0.6, |
---|
465 | label="pick", |
---|
466 | visible=True)[0] |
---|
467 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
468 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
469 | owner=self.base.base.parent |
---|
470 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
471 | |
---|
472 | ## define the left and right lines of the rectangle |
---|
473 | self.right_line = self.axes.plot([self.x1,self.x1],[self.y1,self.y2], |
---|
474 | linestyle='-', marker='', |
---|
475 | color=self.color, |
---|
476 | visible=True)[0] |
---|
477 | self.left_line = self.axes.plot([self.x2,self.x2],[self.y1,self.y2], |
---|
478 | linestyle='-', marker='', |
---|
479 | color=self.color, |
---|
480 | visible=True)[0] |
---|
481 | ## Flag to determine if the lines have moved |
---|
482 | self.has_move=False |
---|
483 | ## connection the marker and draw the pictures |
---|
484 | self.connect_markers([self.right_marker]) |
---|
485 | self.update() |
---|
486 | |
---|
487 | |
---|
488 | def set_layer(self, n): |
---|
489 | """ |
---|
490 | Allow adding plot to the same panel |
---|
491 | @param n: the number of layer |
---|
492 | """ |
---|
493 | self.layernum = n |
---|
494 | self.update() |
---|
495 | |
---|
496 | |
---|
497 | def clear(self): |
---|
498 | """ |
---|
499 | Clear this slicer and its markers |
---|
500 | """ |
---|
501 | self.clear_markers() |
---|
502 | try: |
---|
503 | self.right_marker.remove() |
---|
504 | self.right_line.remove() |
---|
505 | self.left_line.remove() |
---|
506 | except: |
---|
507 | # Old version of matplotlib |
---|
508 | for item in range(len(self.axes.lines)): |
---|
509 | del self.axes.lines[0] |
---|
510 | |
---|
511 | |
---|
512 | def update(self,x1=None,x2=None, y1=None,y2=None,width=None, height=None, center=None): |
---|
513 | """ |
---|
514 | Draw the new roughness on the graph. |
---|
515 | @param x1: new maximum value of x coordinates |
---|
516 | @param x2: new minimum value of x coordinates |
---|
517 | @param y1: new maximum value of y coordinates |
---|
518 | @param y2: new minimum value of y coordinates |
---|
519 | @param width: is the width of the new rectangle |
---|
520 | @param height: is the height of the new rectangle |
---|
521 | @param center: provided x, y coordinates of the center point |
---|
522 | """ |
---|
523 | ## save the new height, witdh of the rectangle if given as a param |
---|
524 | if width!=None: |
---|
525 | self.half_width= width |
---|
526 | if height!=None: |
---|
527 | self.half_height= height |
---|
528 | ## If new center coordinates are given draw the rectangle |
---|
529 | ##given these value |
---|
530 | if center!=None: |
---|
531 | self.center_x= center.x |
---|
532 | self.center_y= center.y |
---|
533 | |
---|
534 | self.x1 = self.half_width + self.center_x |
---|
535 | self.x2= -self.half_width + self.center_x |
---|
536 | |
---|
537 | self.y1 = self.half_height + self.center_y |
---|
538 | self.y2= -self.half_height + self.center_y |
---|
539 | |
---|
540 | self.right_marker.set(xdata=[self.x1],ydata=[self.center_y]) |
---|
541 | self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2]) |
---|
542 | self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2]) |
---|
543 | return |
---|
544 | ## if x1, y1, y2, y3 are given draw the rectangle with this value |
---|
545 | if x1 !=None: |
---|
546 | self.x1= x1 |
---|
547 | if x2 !=None: |
---|
548 | self.x2= x2 |
---|
549 | if y1 !=None: |
---|
550 | self.y1= y1 |
---|
551 | if y2 !=None: |
---|
552 | self.y2= y2 |
---|
553 | ## Draw 2 vertical lines and a marker |
---|
554 | self.right_marker.set(xdata=[self.x1],ydata=[self.center_y]) |
---|
555 | self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2]) |
---|
556 | self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2]) |
---|
557 | |
---|
558 | |
---|
559 | def save(self, ev): |
---|
560 | """ |
---|
561 | Remember the roughness for this layer and the next so that we |
---|
562 | can restore on Esc. |
---|
563 | """ |
---|
564 | self.save_x2= self.x2 |
---|
565 | self.save_y2= self.y2 |
---|
566 | |
---|
567 | self.save_x1= self.x1 |
---|
568 | self.save_y1= self.y1 |
---|
569 | |
---|
570 | self.save_half_height= self.half_height |
---|
571 | self.save_half_width = self.half_width |
---|
572 | |
---|
573 | self.base.freeze_axes() |
---|
574 | |
---|
575 | |
---|
576 | def moveend(self, ev): |
---|
577 | """ |
---|
578 | After a dragging motion reset the flag self.has_move to False |
---|
579 | """ |
---|
580 | self.has_move=False |
---|
581 | self.base.moveend(ev) |
---|
582 | |
---|
583 | |
---|
584 | def restore(self): |
---|
585 | """ |
---|
586 | Restore the roughness for this layer. |
---|
587 | """ |
---|
588 | self.y2= self.save_y2 |
---|
589 | self.x2= self.save_x2 |
---|
590 | |
---|
591 | self.y1= self.save_y1 |
---|
592 | self.x1= self.save_x1 |
---|
593 | |
---|
594 | self.half_height= self.save_half_height |
---|
595 | self.half_width= self.save_half_width |
---|
596 | |
---|
597 | |
---|
598 | def move(self, x, y, ev): |
---|
599 | """ |
---|
600 | Process move to a new position, making sure that the move is allowed. |
---|
601 | """ |
---|
602 | self.x1= x |
---|
603 | delta= self.x1- self.center_x |
---|
604 | self.x2= self.center_x - delta |
---|
605 | |
---|
606 | self.half_width= math.fabs(self.x1-self.x2)/2 |
---|
607 | |
---|
608 | self.has_move=True |
---|
609 | self.base.base.update() |
---|
610 | |
---|
611 | |
---|
612 | def set_cursor(self, x, y): |
---|
613 | """ |
---|
614 | Update the figure given x and y |
---|
615 | """ |
---|
616 | self.move(x, y, None) |
---|
617 | self.update() |
---|
618 | |
---|
619 | |
---|
620 | def get_params(self): |
---|
621 | """ |
---|
622 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
623 | @return params: the dictionary created |
---|
624 | """ |
---|
625 | params = {} |
---|
626 | params["x"] = self.x1 |
---|
627 | params["y"] = self.y1 |
---|
628 | |
---|
629 | return params |
---|
630 | |
---|
631 | def set_params(self, params): |
---|
632 | """ |
---|
633 | Receive a dictionary and reset the slicer with values contained |
---|
634 | in the values of the dictionary. |
---|
635 | @param params: a dictionary containing name of slicer parameters and |
---|
636 | values the user assigned to the slicer. |
---|
637 | """ |
---|
638 | x = params["x"] |
---|
639 | y = params["y"] |
---|
640 | self.update(x=x, y=y, center_x=None,center_y=None) |
---|
641 | |
---|
642 | |
---|
643 | class HorizontalDoubleLine(_BaseInteractor): |
---|
644 | """ |
---|
645 | Select an annulus through a 2D plot |
---|
646 | """ |
---|
647 | def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5, |
---|
648 | center_x= 0.0, |
---|
649 | center_y= 0.0): |
---|
650 | |
---|
651 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
652 | ## Initialization the class |
---|
653 | self.markers = [] |
---|
654 | self.axes = axes |
---|
655 | ## Center coordinates |
---|
656 | self.center_x = center_x |
---|
657 | self.center_y = center_y |
---|
658 | |
---|
659 | self.y1 = y + self.center_y |
---|
660 | self.save_y1= self.y1 |
---|
661 | |
---|
662 | delta= self.y1- self.center_y |
---|
663 | self.y2= self.center_y - delta |
---|
664 | self.save_y2= self.y2 |
---|
665 | |
---|
666 | self.x1 = x + self.center_x |
---|
667 | self.save_x1 = self.x1 |
---|
668 | |
---|
669 | delta= self.x1- self.center_x |
---|
670 | self.x2= self.center_x - delta |
---|
671 | self.save_x2 = self.x2 |
---|
672 | |
---|
673 | self.color=color |
---|
674 | |
---|
675 | self.half_height= math.fabs(y) |
---|
676 | self.save_half_height= math.fabs(y) |
---|
677 | |
---|
678 | self.half_width= math.fabs(x) |
---|
679 | self.save_half_width=math.fabs(x) |
---|
680 | |
---|
681 | try: |
---|
682 | self.top_marker = self.axes.plot([0],[self.y1], linestyle='', |
---|
683 | marker='s', markersize=10, |
---|
684 | color=self.color, alpha=0.6, |
---|
685 | pickradius=5, label="pick", |
---|
686 | zorder=zorder, # Prefer this to other lines |
---|
687 | visible=True)[0] |
---|
688 | except: |
---|
689 | self.top_marker = self.axes.plot([0],[self.y1], linestyle='', |
---|
690 | marker='s', markersize=10, |
---|
691 | color=self.color, alpha=0.6, |
---|
692 | label="pick", |
---|
693 | visible=True)[0] |
---|
694 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
695 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
696 | owner=self.base.base.parent |
---|
697 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
698 | |
---|
699 | # Define 2 horizotnal lines |
---|
700 | self.top_line = self.axes.plot([self.x1,-self.x1],[self.y1,self.y1], |
---|
701 | linestyle='-', marker='', |
---|
702 | color=self.color, |
---|
703 | visible=True)[0] |
---|
704 | self.bottom_line = self.axes.plot([self.x1,-self.x1],[self.y2,self.y2], |
---|
705 | linestyle='-', marker='', |
---|
706 | color=self.color, |
---|
707 | visible=True)[0] |
---|
708 | ## Flag to determine if the lines have moved |
---|
709 | self.has_move=False |
---|
710 | ## connection the marker and draw the pictures |
---|
711 | self.connect_markers([self.top_marker]) |
---|
712 | self.update() |
---|
713 | |
---|
714 | |
---|
715 | def set_layer(self, n): |
---|
716 | """ |
---|
717 | Allow adding plot to the same panel |
---|
718 | @param n: the number of layer |
---|
719 | """ |
---|
720 | self.layernum = n |
---|
721 | self.update() |
---|
722 | |
---|
723 | |
---|
724 | def clear(self): |
---|
725 | """ |
---|
726 | Clear this figure and its markers |
---|
727 | """ |
---|
728 | self.clear_markers() |
---|
729 | try: |
---|
730 | self.top_marker.remove() |
---|
731 | self.bottom_line.remove() |
---|
732 | self.top_line.remove() |
---|
733 | except: |
---|
734 | # Old version of matplotlib |
---|
735 | for item in range(len(self.axes.lines)): |
---|
736 | del self.axes.lines[0] |
---|
737 | |
---|
738 | |
---|
739 | def update(self,x1=None,x2=None, y1=None,y2=None,width=None,height=None, center=None): |
---|
740 | """ |
---|
741 | Draw the new roughness on the graph. |
---|
742 | @param x1: new maximum value of x coordinates |
---|
743 | @param x2: new minimum value of x coordinates |
---|
744 | @param y1: new maximum value of y coordinates |
---|
745 | @param y2: new minimum value of y coordinates |
---|
746 | @param width: is the width of the new rectangle |
---|
747 | @param height: is the height of the new rectangle |
---|
748 | @param center: provided x, y coordinates of the center point |
---|
749 | """ |
---|
750 | ## save the new height, witdh of the rectangle if given as a param |
---|
751 | if width != None: |
---|
752 | self.half_width = width |
---|
753 | if height!= None: |
---|
754 | self.half_height= height |
---|
755 | ## If new center coordinates are given draw the rectangle |
---|
756 | ##given these value |
---|
757 | if center!=None: |
---|
758 | self.center_x= center.x |
---|
759 | self.center_y= center.y |
---|
760 | |
---|
761 | self.x1 = self.half_width + self.center_x |
---|
762 | self.x2= -self.half_width + self.center_x |
---|
763 | |
---|
764 | self.y1 = self.half_height + self.center_y |
---|
765 | self.y2= -self.half_height + self.center_y |
---|
766 | |
---|
767 | self.top_marker.set(xdata=[self.center_x],ydata=[self.y1]) |
---|
768 | self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1]) |
---|
769 | self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2]) |
---|
770 | return |
---|
771 | ## if x1, y1, y2, y3 are given draw the rectangle with this value |
---|
772 | if x1 !=None: |
---|
773 | self.x1= x1 |
---|
774 | if x2 !=None: |
---|
775 | self.x2= x2 |
---|
776 | if y1 !=None: |
---|
777 | self.y1= y1 |
---|
778 | if y2 !=None: |
---|
779 | self.y2= y2 |
---|
780 | ## Draw 2 vertical lines and a marker |
---|
781 | self.top_marker.set(xdata=[self.center_x],ydata=[self.y1]) |
---|
782 | self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1]) |
---|
783 | self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2]) |
---|
784 | |
---|
785 | |
---|
786 | def save(self, ev): |
---|
787 | """ |
---|
788 | Remember the roughness for this layer and the next so that we |
---|
789 | can restore on Esc. |
---|
790 | """ |
---|
791 | self.save_x2= self.x2 |
---|
792 | self.save_y2= self.y2 |
---|
793 | |
---|
794 | self.save_x1= self.x1 |
---|
795 | self.save_y1= self.y1 |
---|
796 | |
---|
797 | self.save_half_height= self.half_height |
---|
798 | self.save_half_width = self.half_width |
---|
799 | self.base.freeze_axes() |
---|
800 | |
---|
801 | |
---|
802 | def moveend(self, ev): |
---|
803 | """ |
---|
804 | After a dragging motion reset the flag self.has_move to False |
---|
805 | """ |
---|
806 | self.has_move=False |
---|
807 | self.base.moveend(ev) |
---|
808 | |
---|
809 | |
---|
810 | def restore(self): |
---|
811 | """ |
---|
812 | Restore the roughness for this layer. |
---|
813 | """ |
---|
814 | self.y2= self.save_y2 |
---|
815 | self.x2= self.save_x2 |
---|
816 | |
---|
817 | self.y1= self.save_y1 |
---|
818 | self.x1= self.save_x1 |
---|
819 | self.half_height= self.save_half_height |
---|
820 | self.half_width= self.save_half_width |
---|
821 | |
---|
822 | |
---|
823 | def move(self, x, y, ev): |
---|
824 | """ |
---|
825 | Process move to a new position, making sure that the move is allowed. |
---|
826 | """ |
---|
827 | self.y1= y |
---|
828 | delta= self.y1- self.center_y |
---|
829 | self.y2= self.center_y - delta |
---|
830 | self.half_height= math.fabs(self.y1)-self.center_y |
---|
831 | self.has_move=True |
---|
832 | self.base.base.update() |
---|
833 | |
---|
834 | |
---|
835 | def set_cursor(self, x, y): |
---|
836 | """ |
---|
837 | Update the figure given x and y |
---|
838 | """ |
---|
839 | self.move(x, y, None) |
---|
840 | self.update() |
---|
841 | |
---|
842 | |
---|
843 | def get_params(self): |
---|
844 | """ |
---|
845 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
846 | @return params: the dictionary created |
---|
847 | """ |
---|
848 | params = {} |
---|
849 | params["x"] = self.x |
---|
850 | params["y"] = self.y |
---|
851 | return params |
---|
852 | |
---|
853 | |
---|
854 | def set_params(self, params): |
---|
855 | """ |
---|
856 | Receive a dictionary and reset the slicer with values contained |
---|
857 | in the values of the dictionary. |
---|
858 | @param params: a dictionary containing name of slicer parameters and |
---|
859 | values the user assigned to the slicer. |
---|
860 | """ |
---|
861 | x = params["x"] |
---|
862 | y = params["y"] |
---|
863 | self.update(x=x, y=y, center_x=None,center_y=None) |
---|
864 | |
---|
865 | |
---|
866 | |
---|