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