source: sasview/guiframe/local_perspectives/plotting/boxSlicer.py @ 6345640

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6345640 was a4d4b35, checked in by Jae Cho <jhjcho@…>, 15 years ago

fixed slabY bug: binning x-direction

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