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