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