1 | |
---|
2 | |
---|
3 | import wx |
---|
4 | #import copy |
---|
5 | #from copy import deepcopy |
---|
6 | import math |
---|
7 | import numpy |
---|
8 | from sans.guiframe.events import NewPlotEvent |
---|
9 | from sans.guiframe.events import StatusEvent |
---|
10 | from sans.guiframe.events import SlicerParameterEvent |
---|
11 | from sans.guiframe.events import EVT_SLICER_PARS |
---|
12 | from BaseInteractor import _BaseInteractor |
---|
13 | from sans.guiframe.dataFitting import Data1D |
---|
14 | #import SlicerParameters |
---|
15 | |
---|
16 | |
---|
17 | class BoxInteractor(_BaseInteractor): |
---|
18 | """ |
---|
19 | BoxInteractor define a rectangle that return data1D average of Data2D |
---|
20 | in a rectangle area defined by -x, x ,y, -y |
---|
21 | """ |
---|
22 | def __init__(self, base, axes, color='black', zorder=3): |
---|
23 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
24 | ## Class initialization |
---|
25 | self.markers = [] |
---|
26 | self.axes = axes |
---|
27 | ##connecting artist |
---|
28 | self.connect = self.base.connect |
---|
29 | ## determine x y values |
---|
30 | self.x = 0.5 * min(math.fabs(self.base.data2D.xmax), |
---|
31 | math.fabs(self.base.data2D.xmin)) |
---|
32 | self.y = 0.5 * min(math.fabs(self.base.data2D.xmax), |
---|
33 | math.fabs(self.base.data2D.xmin)) |
---|
34 | ## when reach qmax reset the graph |
---|
35 | self.qmax = max(self.base.data2D.xmax, self.base.data2D.xmin, |
---|
36 | self.base.data2D.ymax, self.base.data2D.ymin) |
---|
37 | ## Number of points on the plot |
---|
38 | self.nbins = 30 |
---|
39 | ## If True, I(|Q|) will be return, otherwise, |
---|
40 | #negative q-values are allowed |
---|
41 | self.fold = True |
---|
42 | ## reference of the current Slab averaging |
---|
43 | self.averager = None |
---|
44 | ## Create vertical and horizaontal lines for the rectangle |
---|
45 | self.vertical_lines = VerticalLines(self, |
---|
46 | self.base.subplot, |
---|
47 | color='blue', |
---|
48 | zorder=zorder, |
---|
49 | y=self.y , |
---|
50 | x=self.x) |
---|
51 | self.vertical_lines.qmax = self.qmax |
---|
52 | |
---|
53 | self.horizontal_lines = HorizontalLines(self, |
---|
54 | self.base.subplot, |
---|
55 | color='green', |
---|
56 | zorder=zorder, |
---|
57 | x=self.x, |
---|
58 | y=self.y) |
---|
59 | self.horizontal_lines.qmax = self.qmax |
---|
60 | ## draw the rectangle and plost the data 1D resulting |
---|
61 | ## of averaging data2D |
---|
62 | self.update() |
---|
63 | self._post_data() |
---|
64 | ## Bind to slice parameter events |
---|
65 | self.base.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
66 | |
---|
67 | def _onEVT_SLICER_PARS(self, event): |
---|
68 | """ |
---|
69 | receive an event containing parameters values to reset the slicer |
---|
70 | |
---|
71 | :param event: event of type SlicerParameterEvent with params as |
---|
72 | attribute |
---|
73 | """ |
---|
74 | wx.PostEvent(self.base.parent, |
---|
75 | StatusEvent(status="BoxSlicer._onEVT_SLICER_PARS")) |
---|
76 | event.Skip() |
---|
77 | if event.type == self.__class__.__name__: |
---|
78 | self.set_params(event.params) |
---|
79 | self.base.update() |
---|
80 | |
---|
81 | def update_and_post(self): |
---|
82 | """ |
---|
83 | Update the slicer and plot the resulting data |
---|
84 | """ |
---|
85 | self.update() |
---|
86 | self._post_data() |
---|
87 | |
---|
88 | def set_layer(self, n): |
---|
89 | """ |
---|
90 | Allow adding plot to the same panel |
---|
91 | |
---|
92 | :param n: the number of layer |
---|
93 | |
---|
94 | """ |
---|
95 | self.layernum = n |
---|
96 | self.update() |
---|
97 | |
---|
98 | def clear(self): |
---|
99 | """ |
---|
100 | Clear the slicer and all connected events related to this slicer |
---|
101 | """ |
---|
102 | self.averager = None |
---|
103 | self.clear_markers() |
---|
104 | self.horizontal_lines.clear() |
---|
105 | self.vertical_lines.clear() |
---|
106 | self.base.connect.clearall() |
---|
107 | self.base.Unbind(EVT_SLICER_PARS) |
---|
108 | |
---|
109 | def update(self): |
---|
110 | """ |
---|
111 | Respond to changes in the model by recalculating the profiles and |
---|
112 | resetting the widgets. |
---|
113 | """ |
---|
114 | ##Update the slicer if an horizontal line is dragged |
---|
115 | if self.horizontal_lines.has_move: |
---|
116 | self.horizontal_lines.update() |
---|
117 | self.vertical_lines.update(y=self.horizontal_lines.y) |
---|
118 | ##Update the slicer if a vertical line is dragged |
---|
119 | if self.vertical_lines.has_move: |
---|
120 | self.vertical_lines.update() |
---|
121 | self.horizontal_lines.update(x=self.vertical_lines.x) |
---|
122 | |
---|
123 | def save(self, ev): |
---|
124 | """ |
---|
125 | Remember the roughness for this layer and the next so that we |
---|
126 | can restore on Esc. |
---|
127 | """ |
---|
128 | self.base.freeze_axes() |
---|
129 | self.vertical_lines.save(ev) |
---|
130 | self.horizontal_lines.save(ev) |
---|
131 | |
---|
132 | def _post_data(self): |
---|
133 | pass |
---|
134 | |
---|
135 | def post_data(self, new_slab=None, nbins=None, direction=None): |
---|
136 | """ |
---|
137 | post data averaging in Qx or Qy given new_slab type |
---|
138 | |
---|
139 | :param new_slab: slicer that determine with direction to average |
---|
140 | :param nbins: the number of points plotted when averaging |
---|
141 | :param direction: the direction of averaging |
---|
142 | |
---|
143 | """ |
---|
144 | x_min = -1 * math.fabs(self.vertical_lines.x) |
---|
145 | x_max = math.fabs(self.vertical_lines.x) |
---|
146 | y_min = -1 * math.fabs(self.horizontal_lines.y) |
---|
147 | y_max = math.fabs(self.horizontal_lines.y) |
---|
148 | |
---|
149 | if nbins != None: |
---|
150 | self.nbins = nbins |
---|
151 | if self.averager == None: |
---|
152 | if new_slab == None: |
---|
153 | msg = "post data:cannot average , averager is empty" |
---|
154 | raise ValueError, msg |
---|
155 | self.averager = new_slab |
---|
156 | if direction == "X": |
---|
157 | if self.fold: |
---|
158 | x_low = 0 |
---|
159 | else: |
---|
160 | x_low = math.fabs(x_min) |
---|
161 | bin_width = (x_max + x_low)/self.nbins |
---|
162 | else: |
---|
163 | if self.fold: |
---|
164 | y_low = 0 |
---|
165 | else: |
---|
166 | y_low = math.fabs(y_min) |
---|
167 | bin_width = (y_max + y_low)/self.nbins |
---|
168 | ## Average data2D given Qx or Qy |
---|
169 | box = self.averager(x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max, |
---|
170 | bin_width=bin_width) |
---|
171 | box.fold = self.fold |
---|
172 | boxavg = box(self.base.data2D) |
---|
173 | #3 Create Data1D to plot |
---|
174 | if hasattr(boxavg, "dxl"): |
---|
175 | dxl = boxavg.dxl |
---|
176 | else: |
---|
177 | dxl = None |
---|
178 | if hasattr(boxavg, "dxw"): |
---|
179 | dxw = boxavg.dxw |
---|
180 | else: |
---|
181 | dxw = None |
---|
182 | new_plot = Data1D(x=boxavg.x, y=boxavg.y, dy=boxavg.dy) |
---|
183 | new_plot.dxl = dxl |
---|
184 | new_plot.dxw = dxw |
---|
185 | new_plot.name = str(self.averager.__name__) + \ |
---|
186 | "(" + self.base.data2D.name + ")" |
---|
187 | new_plot.source = self.base.data2D.source |
---|
188 | new_plot.interactive = True |
---|
189 | new_plot.detector = self.base.data2D.detector |
---|
190 | ## If the data file does not tell us what the axes are, just assume... |
---|
191 | new_plot.xaxis("\\rm{Q}", "A^{-1}") |
---|
192 | if hasattr(self.base.data2D, "scale"): |
---|
193 | if self.base.data2D.scale == 'linear': |
---|
194 | new_plot.ytransform = 'y' |
---|
195 | new_plot.yaxis("\\rm{Residuals} ", "/") |
---|
196 | else: |
---|
197 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
198 | new_plot.group_id = str(self.averager.__name__) + self.base.data2D.name |
---|
199 | #new_plot.id = str(self.averager.__name__) |
---|
200 | #new_plot.is_data= True |
---|
201 | wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot, |
---|
202 | title=str(self.averager.__name__))) |
---|
203 | |
---|
204 | def moveend(self, ev): |
---|
205 | """ |
---|
206 | Called after a dragging event. |
---|
207 | Post the slicer new parameters and creates a new Data1D |
---|
208 | corresponding to the new average |
---|
209 | """ |
---|
210 | self.base.thaw_axes() |
---|
211 | # Post paramters |
---|
212 | event = SlicerParameterEvent() |
---|
213 | event.type = self.__class__.__name__ |
---|
214 | event.params = self.get_params() |
---|
215 | wx.PostEvent(self.base.parent, event) |
---|
216 | # create the new data1D |
---|
217 | self._post_data() |
---|
218 | |
---|
219 | def restore(self): |
---|
220 | """ |
---|
221 | Restore the roughness for this layer. |
---|
222 | """ |
---|
223 | self.horizontal_lines.restore() |
---|
224 | self.vertical_lines.restore() |
---|
225 | |
---|
226 | def move(self, x, y, ev): |
---|
227 | """ |
---|
228 | Process move to a new position, making sure that the move is allowed. |
---|
229 | """ |
---|
230 | pass |
---|
231 | |
---|
232 | def set_cursor(self, x, y): |
---|
233 | pass |
---|
234 | |
---|
235 | def get_params(self): |
---|
236 | """ |
---|
237 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
238 | |
---|
239 | :return params: the dictionary created |
---|
240 | |
---|
241 | """ |
---|
242 | params = {} |
---|
243 | params["x_max"] = math.fabs(self.vertical_lines.x) |
---|
244 | params["y_max"] = math.fabs(self.horizontal_lines.y) |
---|
245 | params["nbins"] = self.nbins |
---|
246 | return params |
---|
247 | |
---|
248 | def set_params(self, params): |
---|
249 | """ |
---|
250 | Receive a dictionary and reset the slicer with values contained |
---|
251 | in the values of the dictionary. |
---|
252 | |
---|
253 | :param params: a dictionary containing name of slicer parameters and |
---|
254 | values the user assigned to the slicer. |
---|
255 | """ |
---|
256 | self.x = float(math.fabs(params["x_max"])) |
---|
257 | self.y = float(math.fabs(params["y_max"] )) |
---|
258 | self.nbins = params["nbins"] |
---|
259 | |
---|
260 | self.horizontal_lines.update(x=self.x, y=self.y) |
---|
261 | self.vertical_lines.update(x=self.x, y=self.y) |
---|
262 | self.post_data(nbins=None) |
---|
263 | |
---|
264 | def freeze_axes(self): |
---|
265 | """ |
---|
266 | """ |
---|
267 | self.base.freeze_axes() |
---|
268 | |
---|
269 | def thaw_axes(self): |
---|
270 | """ |
---|
271 | """ |
---|
272 | self.base.thaw_axes() |
---|
273 | |
---|
274 | def draw(self): |
---|
275 | """ |
---|
276 | """ |
---|
277 | self.base.draw() |
---|
278 | |
---|
279 | |
---|
280 | class HorizontalLines(_BaseInteractor): |
---|
281 | """ |
---|
282 | Draw 2 Horizontal lines centered on (0,0) that can move |
---|
283 | on the x- direction and in opposite direction |
---|
284 | """ |
---|
285 | def __init__(self, base, axes, color='black', zorder=5, x=0.5, y=0.5): |
---|
286 | """ |
---|
287 | """ |
---|
288 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
289 | ##Class initialization |
---|
290 | self.markers = [] |
---|
291 | self.axes = axes |
---|
292 | ## Saving the end points of two lines |
---|
293 | self.x = x |
---|
294 | self.save_x = x |
---|
295 | |
---|
296 | self.y = y |
---|
297 | self.save_y = y |
---|
298 | ## Creating a marker |
---|
299 | try: |
---|
300 | # Inner circle marker |
---|
301 | self.inner_marker = self.axes.plot([0], [self.y], linestyle='', |
---|
302 | marker='s', markersize=10, |
---|
303 | color=self.color, alpha=0.6, |
---|
304 | pickradius=5, label="pick", |
---|
305 | # Prefer this to other lines |
---|
306 | zorder=zorder, |
---|
307 | visible=True)[0] |
---|
308 | except: |
---|
309 | self.inner_marker = self.axes.plot([0], [self.y], linestyle='', |
---|
310 | marker='s', markersize=10, |
---|
311 | color=self.color, alpha=0.6, |
---|
312 | label="pick", visible=True)[0] |
---|
313 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION " |
---|
314 | message += "OF MATPLOTLIB\n Get the SVN version that" |
---|
315 | message += " is at least as recent as June 1, 2007" |
---|
316 | owner = self.base.base.parent |
---|
317 | wx.PostEvent(owner, |
---|
318 | StatusEvent(status="AnnulusSlicer: %s" % message)) |
---|
319 | ## Define 2 horizontal lines |
---|
320 | self.top_line = self.axes.plot([self.x, -self.x], [self.y, self.y], |
---|
321 | linestyle='-', marker='', |
---|
322 | color=self.color, visible=True)[0] |
---|
323 | self.bottom_line = self.axes.plot([self.x, -self.x], [-self.y, -self.y], |
---|
324 | linestyle='-', marker='', |
---|
325 | color=self.color, visible=True)[0] |
---|
326 | ## Flag to check the motion of the lines |
---|
327 | self.has_move = False |
---|
328 | ## Connecting markers to mouse events and draw |
---|
329 | self.connect_markers([self.top_line, self.inner_marker]) |
---|
330 | self.update() |
---|
331 | |
---|
332 | def set_layer(self, n): |
---|
333 | """ |
---|
334 | Allow adding plot to the same panel |
---|
335 | |
---|
336 | :param n: the number of layer |
---|
337 | |
---|
338 | """ |
---|
339 | self.layernum = n |
---|
340 | self.update() |
---|
341 | |
---|
342 | def clear(self): |
---|
343 | """ |
---|
344 | Clear this slicer and its markers |
---|
345 | """ |
---|
346 | self.clear_markers() |
---|
347 | try: |
---|
348 | self.inner_marker.remove() |
---|
349 | self.top_line.remove() |
---|
350 | self.bottom_line.remove() |
---|
351 | except: |
---|
352 | # Old version of matplotlib |
---|
353 | for item in range(len(self.axes.lines)): |
---|
354 | del self.axes.lines[0] |
---|
355 | |
---|
356 | def update(self, x=None, y=None): |
---|
357 | """ |
---|
358 | Draw the new roughness on the graph. |
---|
359 | |
---|
360 | :param x: x-coordinates to reset current class x |
---|
361 | :param y: y-coordinates to reset current class y |
---|
362 | |
---|
363 | """ |
---|
364 | ## Reset x, y- coordinates if send as parameters |
---|
365 | if x != None: |
---|
366 | self.x = numpy.sign(self.x) * math.fabs(x) |
---|
367 | if y != None: |
---|
368 | self.y = numpy.sign(self.y) * math.fabs(y) |
---|
369 | ## Draw lines and markers |
---|
370 | self.inner_marker.set(xdata=[0], ydata=[self.y]) |
---|
371 | self.top_line.set(xdata=[self.x, -self.x], ydata=[self.y, self.y]) |
---|
372 | self.bottom_line.set(xdata=[self.x,-self.x], ydata=[-self.y, -self.y]) |
---|
373 | |
---|
374 | def save(self, ev): |
---|
375 | """ |
---|
376 | Remember the roughness for this layer and the next so that we |
---|
377 | can restore on Esc. |
---|
378 | """ |
---|
379 | self.save_x = self.x |
---|
380 | self.save_y = self.y |
---|
381 | self.base.freeze_axes() |
---|
382 | |
---|
383 | def moveend(self, ev): |
---|
384 | """ |
---|
385 | Called after a dragging this edge and set self.has_move to False |
---|
386 | to specify the end of dragging motion |
---|
387 | """ |
---|
388 | self.has_move = False |
---|
389 | self.base.moveend(ev) |
---|
390 | |
---|
391 | def restore(self): |
---|
392 | """ |
---|
393 | Restore the roughness for this layer. |
---|
394 | """ |
---|
395 | self.x = self.save_x |
---|
396 | self.y = self.save_y |
---|
397 | |
---|
398 | def move(self, x, y, ev): |
---|
399 | """ |
---|
400 | Process move to a new position, making sure that the move is allowed. |
---|
401 | """ |
---|
402 | self.y = y |
---|
403 | self.has_move = True |
---|
404 | self.base.base.update() |
---|
405 | |
---|
406 | |
---|
407 | class VerticalLines(_BaseInteractor): |
---|
408 | """ |
---|
409 | Select an annulus through a 2D plot |
---|
410 | """ |
---|
411 | def __init__(self, base, axes, color='black', zorder=5, x=0.5, y=0.5): |
---|
412 | """ |
---|
413 | """ |
---|
414 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
415 | self.markers = [] |
---|
416 | self.axes = axes |
---|
417 | self.x = math.fabs(x) |
---|
418 | self.save_x = self.x |
---|
419 | self.y = math.fabs(y) |
---|
420 | self.save_y = y |
---|
421 | try: |
---|
422 | # Inner circle marker |
---|
423 | self.inner_marker = self.axes.plot([self.x], [0], linestyle='', |
---|
424 | marker='s', markersize=10, |
---|
425 | color=self.color, alpha=0.6, |
---|
426 | pickradius=5, label="pick", |
---|
427 | # Prefer this to other lines |
---|
428 | zorder=zorder, visible=True)[0] |
---|
429 | except: |
---|
430 | self.inner_marker = self.axes.plot([self.x], [0], linestyle='', |
---|
431 | marker='s', markersize=10, |
---|
432 | color=self.color, alpha=0.6, |
---|
433 | label="pick", visible=True)[0] |
---|
434 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION" |
---|
435 | message += " OF MATPLOTLIB\n Get the SVN version that is" |
---|
436 | message += " at least as recent as June 1, 2007" |
---|
437 | |
---|
438 | self.right_line = self.axes.plot([self.x, self.x], |
---|
439 | [self.y , -self.y], |
---|
440 | linestyle='-', marker='', |
---|
441 | color=self.color, visible=True)[0] |
---|
442 | self.left_line = self.axes.plot([-self.x, -self.x], |
---|
443 | [self.y, -self.y], |
---|
444 | linestyle='-', marker='', |
---|
445 | color=self.color, visible=True)[0] |
---|
446 | self.has_move = False |
---|
447 | self.connect_markers([self.right_line, self.inner_marker]) |
---|
448 | self.update() |
---|
449 | |
---|
450 | def set_layer(self, n): |
---|
451 | """ |
---|
452 | Allow adding plot to the same panel |
---|
453 | |
---|
454 | :param n: the number of layer |
---|
455 | |
---|
456 | """ |
---|
457 | self.layernum = n |
---|
458 | self.update() |
---|
459 | |
---|
460 | def clear(self): |
---|
461 | """ |
---|
462 | Clear this slicer and its markers |
---|
463 | """ |
---|
464 | self.clear_markers() |
---|
465 | try: |
---|
466 | self.inner_marker.remove() |
---|
467 | self.left_line.remove() |
---|
468 | self.right_line.remove() |
---|
469 | except: |
---|
470 | # Old version of matplotlib |
---|
471 | for item in range(len(self.axes.lines)): |
---|
472 | del self.axes.lines[0] |
---|
473 | |
---|
474 | def update(self, x=None, y=None): |
---|
475 | """ |
---|
476 | Draw the new roughness on the graph. |
---|
477 | |
---|
478 | :param x: x-coordinates to reset current class x |
---|
479 | :param y: y-coordinates to reset current class y |
---|
480 | |
---|
481 | """ |
---|
482 | ## reset x, y -coordinates if given as parameters |
---|
483 | if x != None: |
---|
484 | self.x = numpy.sign(self.x) * math.fabs(x) |
---|
485 | if y != None: |
---|
486 | self.y = numpy.sign(self.y) * math.fabs(y) |
---|
487 | ## draw lines and markers |
---|
488 | self.inner_marker.set(xdata=[self.x], ydata=[0]) |
---|
489 | self.left_line.set(xdata=[-self.x, -self.x], ydata=[self.y, -self.y]) |
---|
490 | self.right_line.set(xdata=[self.x, self.x], ydata=[self.y, -self.y]) |
---|
491 | |
---|
492 | def save(self, ev): |
---|
493 | """ |
---|
494 | Remember the roughness for this layer and the next so that we |
---|
495 | can restore on Esc. |
---|
496 | """ |
---|
497 | self.save_x = self.x |
---|
498 | self.save_y = self.y |
---|
499 | self.base.freeze_axes() |
---|
500 | |
---|
501 | def moveend(self, ev): |
---|
502 | """ |
---|
503 | Called after a dragging this edge and set self.has_move to False |
---|
504 | to specify the end of dragging motion |
---|
505 | """ |
---|
506 | self.has_move = False |
---|
507 | self.base.moveend(ev) |
---|
508 | |
---|
509 | def restore(self): |
---|
510 | """ |
---|
511 | Restore the roughness for this layer. |
---|
512 | """ |
---|
513 | self.x = self.save_x |
---|
514 | self.y = self.save_y |
---|
515 | |
---|
516 | def move(self, x, y, ev): |
---|
517 | """ |
---|
518 | Process move to a new position, making sure that the move is allowed. |
---|
519 | """ |
---|
520 | self.has_move = True |
---|
521 | self.x = x |
---|
522 | self.base.base.update() |
---|
523 | |
---|
524 | |
---|
525 | class BoxInteractorX(BoxInteractor): |
---|
526 | """ |
---|
527 | Average in Qx direction |
---|
528 | """ |
---|
529 | def __init__(self, base, axes, color='black', zorder=3): |
---|
530 | BoxInteractor.__init__(self, base, axes, color=color) |
---|
531 | self.base = base |
---|
532 | self._post_data() |
---|
533 | |
---|
534 | def _post_data(self): |
---|
535 | """ |
---|
536 | Post data creating by averaging in Qx direction |
---|
537 | """ |
---|
538 | from DataLoader.manipulations import SlabX |
---|
539 | self.post_data(SlabX, direction="X") |
---|
540 | |
---|
541 | |
---|
542 | class BoxInteractorY(BoxInteractor): |
---|
543 | """ |
---|
544 | Average in Qy direction |
---|
545 | """ |
---|
546 | def __init__(self, base, axes, color='black', zorder=3): |
---|
547 | BoxInteractor.__init__(self, base, axes, color=color) |
---|
548 | self.base = base |
---|
549 | self._post_data() |
---|
550 | |
---|
551 | def _post_data(self): |
---|
552 | """ |
---|
553 | Post data creating by averaging in Qy direction |
---|
554 | """ |
---|
555 | from DataLoader.manipulations import SlabY |
---|
556 | self.post_data(SlabY, direction="Y") |
---|
557 | |
---|
558 | |
---|