source: sasview/test/test_rename_guiframe/local_perspectives/plotting/SectorSlicer.py @ 6b33ffc

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 6b33ffc was 6b33ffc, checked in by Gervaise Alina <gervyh@…>, 13 years ago

rename

  • Property mode set to 100644
File size: 22.3 KB
Line 
1
2import math
3import wx
4#from copy import deepcopy
5from BaseInteractor import _BaseInteractor
6from sans.guiframe.events import NewPlotEvent
7from sans.guiframe.events import StatusEvent
8from sans.guiframe.events import SlicerParameterEvent
9from sans.guiframe.events import EVT_SLICER_PARS
10from sans.guiframe.dataFitting import Data1D
11
12
13class SectorInteractor(_BaseInteractor):
14    """
15    Draw a sector slicer.Allow to performQ averaging on data 2D
16    """
17    def __init__(self, base, axes, color='black', zorder=3):
18       
19        _BaseInteractor.__init__(self, base, axes, color=color)
20        ## Class initialization
21        self.markers = []
22        self.axes = axes   
23        ## connect the plot to event
24        self.connect = self.base.connect
25       
26        ## compute qmax limit to reset the graph     
27        x = math.pow(max(self.base.data2D.xmax, 
28                         math.fabs(self.base.data2D.xmin)), 2)
29        y = math.pow(max(self.base.data2D.ymax, 
30                         math.fabs(self.base.data2D.ymin)), 2)
31        self.qmax = math.sqrt(x + y)
32        ## Number of points on the plot
33        self.nbins = 20
34        ## Angle of the middle line
35        self.theta2 = math.pi/3
36        ## Absolute value of the Angle between the middle line and any side line
37        self.phi = math.pi/12
38        ## Middle line
39        self.main_line = LineInteractor(self, self.base.subplot, color='blue', 
40                                        zorder=zorder, r=self.qmax,
41                                           theta= self.theta2)
42        self.main_line.qmax = self.qmax
43        ## Right Side line
44        self.right_line = SideInteractor(self, self.base.subplot, color='black',
45                                          zorder=zorder, r=self.qmax,
46                                          phi=-1*self.phi, theta2=self.theta2)
47        self.right_line.qmax = self.qmax
48        ## Left Side line
49        self.left_line = SideInteractor(self, self.base.subplot, color='black',
50                                        zorder=zorder, r=self.qmax,
51                                           phi=self.phi, theta2=self.theta2)
52        self.left_line.qmax = self.qmax
53        ## draw the sector               
54        self.update()
55        self._post_data()
56        ## Bind to slice parameter events
57        self.base.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
58
59    def _onEVT_SLICER_PARS(self, event):
60        """
61        receive an event containing parameters values to reset the slicer
62       
63        :param event: event of type SlicerParameterEvent with params as
64        attribute
65       
66        """
67        wx.PostEvent(self.base.parent,
68                     StatusEvent(status="SectorSlicer._onEVT_SLICER_PARS"))
69        event.Skip()
70        if event.type == self.__class__.__name__:
71            self.set_params(event.params)
72            self.base.update()
73           
74    def set_layer(self, n):
75        """
76         Allow adding plot to the same panel
77         
78        :param n: the number of layer
79       
80        """
81        self.layernum = n
82        self.update()
83       
84    def clear(self):
85        """
86        Clear the slicer and all connected events related to this slicer
87        """
88        self.clear_markers()
89        self.main_line.clear()
90        self.left_line.clear()
91        self.right_line.clear()
92        self.base.connect.clearall()
93        self.base.Unbind(EVT_SLICER_PARS)
94       
95    def update(self):
96        """
97        Respond to changes in the model by recalculating the profiles and
98        resetting the widgets.
99        """
100        # Update locations 
101        ## Check if the middle line was dragged and
102        #update the picture accordingly     
103        if self.main_line.has_move:
104            self.main_line.update()
105            self.right_line.update(delta=-self.left_line.phi/2,
106                                    mline=self.main_line.theta)
107            self.left_line.update(delta=self.left_line.phi/2,
108                                   mline=self.main_line.theta)
109        ## Check if the left side has moved and update the slicer accordingly 
110        if self.left_line.has_move:
111            self.main_line.update()
112            self.left_line.update(phi=None, delta=None, mline=self.main_line,
113                                  side=True, left=True)
114            self.right_line.update(phi=self.left_line.phi, delta=None,
115                                     mline=self.main_line, side=True,
116                                     left=False, right=True)
117        ## Check if the right side line has moved and
118        #update the slicer accordingly
119        if self.right_line.has_move:
120            self.main_line.update()
121            self.right_line.update(phi=None, delta=None, mline=self.main_line,
122                                   side=True, left=False, right=True)
123            self.left_line.update(phi=self.right_line.phi, delta=None,
124                                    mline=self.main_line, side=True, left=False)
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.main_line.save(ev)
133        self.right_line.save(ev)
134        self.left_line.save(ev)
135
136    def _post_data(self, nbins=None):
137        """
138        compute sector averaging of data2D into data1D
139       
140        :param nbins: the number of point to plot for the average 1D data
141        """
142        ## get the data2D to average
143        data = self.base.data2D
144        # If we have no data, just return
145        if data == None:
146            return
147        ## Averaging
148        from DataLoader.manipulations import SectorQ
149        radius = self.qmax
150        phimin =  -self.left_line.phi + self.main_line.theta
151        phimax = self.left_line.phi + self.main_line.theta
152        if nbins == None:
153            nbins = 20
154        sect = SectorQ(r_min=0.0, r_max=radius,
155                        phi_min=phimin + math.pi,
156                        phi_max=phimax + math.pi, nbins=nbins)
157     
158        sector = sect(self.base.data2D)
159        ##Create 1D data resulting from average
160       
161        if hasattr(sector, "dxl"):
162            dxl = sector.dxl
163        else:
164            dxl = None
165        if hasattr(sector, "dxw"):
166            dxw = sector.dxw
167        else:
168            dxw = None
169        new_plot = Data1D(x=sector.x, y=sector.y, dy=sector.dy, dx=sector.dx)
170        new_plot.dxl = dxl
171        new_plot.dxw = dxw
172        new_plot.name = "SectorQ" + "(" + self.base.data2D.name + ")"
173        new_plot.source = self.base.data2D.source
174        #new_plot.info=self.base.data2D.info
175        new_plot.interactive = True
176        new_plot.detector = self.base.data2D.detector
177        ## If the data file does not tell us what the axes are, just assume...
178        new_plot.xaxis("\\rm{Q}", "A^{-1}")
179        new_plot.yaxis("\\rm{Intensity}", "cm^{-1}")
180        if hasattr(data, "scale") and data.scale == 'linear' and \
181                self.base.data2D.name.count("Residuals") > 0:
182            new_plot.ytransform = 'y'
183            new_plot.yaxis("\\rm{Residuals} ", "/")
184
185        new_plot.group_id = "SectorQ" + self.base.data2D.name
186        new_plot.id = None
187        new_plot.is_data = True
188
189        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot,
190                                    title="SectorQ" + self.base.data2D.name))
191       
192    def moveend(self, ev):
193        """
194        Called a dragging motion ends.Get slicer event
195        """
196        self.base.thaw_axes()
197        ## Post parameters
198        event = SlicerParameterEvent()
199        event.type = self.__class__.__name__
200        event.params = self.get_params()
201        ## Send slicer paramers to plotter2D
202        wx.PostEvent(self.base, event)
203       
204    def restore(self):
205        """
206        Restore the roughness for this layer.
207        """
208        self.main_line.restore()
209        self.left_line.restore()
210        self.right_line.restore()
211
212    def move(self, x, y, ev):
213        """
214        Process move to a new position, making sure that the move is allowed.
215        """
216        pass
217       
218    def set_cursor(self, x, y):
219        """
220        """
221        pass
222       
223    def get_params(self):
224        """
225        Store a copy of values of parameters of the slicer into a dictionary.
226       
227        :return params: the dictionary created
228       
229        """
230        params = {}
231        ## Always make sure that the left and the right line are at phi
232        ## angle of the middle line
233        if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi):
234            msg = "Phi left and phi right are different"
235            msg += " %f, %f" % (self.left_line.phi, self.right_line.phi)
236            raise ValueError, msg
237        params["Phi"] = self.main_line.theta
238        params["Delta_Phi"] = math.fabs(self.left_line.phi)
239        params["nbins"] = self.nbins
240        return params
241   
242    def set_params(self, params):
243        """
244        Receive a dictionary and reset the slicer with values contained
245        in the values of the dictionary.
246       
247        :param params: a dictionary containing name of slicer parameters and
248            values the user assigned to the slicer.
249        """
250        main = params["Phi"] 
251        phi = math.fabs(params["Delta_Phi"])
252        self.nbins = int(params["nbins"])
253        self.main_line.theta = main
254        ## Reset the slicer parameters
255        self.main_line.update()
256        self.right_line.update(phi=phi, delta=None, mline=self.main_line,
257                               side=True, right=True)
258        self.left_line.update(phi=phi, delta=None, 
259                              mline=self.main_line, side=True)
260        ## post the new corresponding data
261        self._post_data(nbins=self.nbins)
262       
263    def freeze_axes(self):
264        """
265        """
266        self.base.freeze_axes()
267   
268    def thaw_axes(self):
269        """
270        """
271        self.base.thaw_axes()
272
273    def draw(self):
274        """
275        """
276        self.base.draw()
277
278       
279class SideInteractor(_BaseInteractor):
280    """
281    Draw an oblique line
282   
283    :param phi: the phase between the middle line and one side line
284    :param theta2: the angle between the middle line and x- axis
285   
286    """
287    def __init__(self, base, axes, color='black', zorder=5, r=1.0,
288                 phi=math.pi/4, theta2= math.pi/3):
289        """
290        """
291        _BaseInteractor.__init__(self, base, axes, color=color)
292        ## Initialize the class
293        self.markers = []
294        self.axes = axes
295        ## compute the value of the angle between the current line and
296        ## the x-axis 
297        self.save_theta = theta2 + phi
298        self.theta = theta2 + phi
299        ## the value of the middle line angle with respect to the x-axis
300        self.theta2 = theta2
301        ## Radius to find polar coordinates this line's endpoints
302        self.radius = r
303        ## phi is the phase between the current line and the middle line
304        self.phi = phi
305        ## End points polar coordinates
306        x1 = self.radius * math.cos(self.theta)
307        y1 = self.radius * math.sin(self.theta)
308        x2 = -1 * self.radius * math.cos(self.theta)
309        y2 = -1 * self.radius * math.sin(self.theta)
310        ## defining a new marker
311        try:
312            self.inner_marker = self.axes.plot([x1/2.5], [y1/2.5], linestyle='',
313                                          marker='s', markersize=10,
314                                          color=self.color, alpha=0.6,
315                                          pickradius=5, label="pick", 
316                                          # Prefer this to other lines
317                                          zorder=zorder, visible=True)[0]
318        except:
319            self.inner_marker = self.axes.plot([x1/2.5],[y1/2.5], linestyle='',
320                                          marker='s', markersize=10,
321                                          color=self.color, alpha=0.6,
322                                          label="pick", visible=True)[0]
323            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST"
324            message += " VERSION OF MATPLOTLIB\n Get the SVN version that"
325            message += " is at least as recent as June 1, 2007"
326            owner = self.base.base.parent
327            wx.PostEvent(owner, 
328                         StatusEvent(status="sectorSlicer: %s" % message))
329       
330        ## Defining the current line
331        self.line = self.axes.plot([x1, x2], [y1, y2],
332                                      linestyle='-', marker='',
333                                      color=self.color, visible=True)[0]
334        ## Flag to differentiate the left line from the right line motion
335        self.left_moving = False
336        ## Flag to define a motion
337        self.has_move = False
338        ## connecting markers and draw the picture
339        self.connect_markers([self.inner_marker, self.line])
340       
341    def set_layer(self, n):
342        """
343        Allow adding plot to the same panel
344         
345        :param n: the number of layer
346       
347        """
348        self.layernum = n
349        self.update()
350       
351    def clear(self):
352        """
353        Clear the slicer and all connected events related to this slicer
354        """
355        self.clear_markers()
356        try:
357            self.line.remove()
358            self.inner_marker.remove()
359        except:
360            # Old version of matplotlib
361            for item in range(len(self.axes.lines)):
362                del self.axes.lines[0]
363   
364    def update(self, phi=None, delta=None, mline=None,
365               side=False, left= False, right=False):
366        """
367        Draw oblique line
368       
369        :param phi: the phase between the middle line and the current line
370        :param delta: phi/2 applied only when the mline was moved
371       
372        """
373        #print "update left or right ", self.has_move
374        self.left_moving = left
375        theta3 = 0
376        if phi != None:
377            self.phi = phi
378        if delta == None:
379            delta = 0
380        if  right:
381            self.phi = -1 * math.fabs(self.phi)
382            #delta=-delta
383        else:
384            self.phi = math.fabs(self.phi)
385        if side:
386            self.theta = mline.theta + self.phi
387                   
388        if mline != None :
389            if delta != 0:
390                self.theta2 = mline + delta
391            else:
392                self.theta2 = mline.theta
393        if delta == 0:
394            theta3 = self.theta + delta
395        else:
396            theta3 = self.theta2 + delta
397        x1 = self.radius * math.cos(theta3)
398        y1 = self.radius * math.sin(theta3)
399        x2 = -1 * self.radius * math.cos(theta3)
400        y2 = -1 * self.radius * math.sin(theta3)
401        self.inner_marker.set(xdata=[x1/2.5], ydata=[y1/2.5])
402        self.line.set(xdata=[x1, x2], ydata=[y1, y2]) 
403       
404    def save(self, ev):
405        """
406        Remember the roughness for this layer and the next so that we
407        can restore on Esc.
408        """
409        self.save_theta = self.theta
410        self.base.freeze_axes()
411
412    def moveend(self, ev):
413        """
414        """
415        self.has_move = False
416        self.base.moveend(ev)
417           
418    def restore(self):
419        """
420        Restore the roughness for this layer.
421        """
422        self.theta = self.save_theta
423
424    def move(self, x, y, ev):
425        """
426        Process move to a new position, making sure that the move is allowed.
427        """
428        self.theta = math.atan2(y, x)
429        self.has_move = True
430        #ToDo: Simplify below
431        if not self.left_moving:
432            if  self.theta2 - self.theta <= 0 and self.theta2 > 0:
433                self.restore()
434                return 
435            elif self.theta2 < 0 and self.theta < 0 and \
436                self.theta-self.theta2 >= 0:
437                self.restore()
438                return                             
439            elif  self.theta2 < 0 and self.theta > 0 and \
440                (self.theta2 + 2 * math.pi - self.theta) >= math.pi/2:
441                #print "my theta", self.theta
442                self.restore()
443                return 
444            elif  self.theta2 < 0 and self.theta < 0 and \
445                (self.theta2 - self.theta) >= math.pi/2:
446                #print "my theta", self.theta
447                self.restore()
448                return 
449            elif self.theta2 > 0 and (self.theta2-self.theta >= math.pi/2 or \
450                (self.theta2-self.theta >= math.pi/2)):
451                #print "self theta encore"
452                self.restore()
453                return 
454        else:
455            #print "left move"
456            if  self.theta < 0 and (self.theta + math.pi*2-self.theta2) <= 0:
457                self.restore()
458                return 
459            elif self.theta2 < 0 and (self.theta-self.theta2) <= 0:
460                self.restore()
461                return                             
462            elif  self.theta > 0 and self.theta-self.theta2 <= 0:
463                #print "my theta", self.theta
464                self.restore()
465                return 
466            elif self.theta-self.theta2 >= math.pi/2 or  \
467                ((self.theta + math.pi * 2 - self.theta2) >= math.pi/2 and \
468                 self.theta < 0 and self.theta2 > 0):
469                #print "self theta encore"
470                self.restore()
471                return 
472           
473        self.phi = math.fabs(self.theta2 - self.theta)
474        if self.phi > math.pi:
475            self.phi = 2 * math.pi - math.fabs(self.theta2 - self.theta)
476        self.base.base.update()
477       
478    def set_cursor(self, x, y):
479        """
480        """
481        self.move(x, y, None)
482        self.update()
483   
484    def get_params(self):
485        """
486        """
487        params = {}
488        params["radius"] = self.radius
489        params["theta"] = self.theta
490        return params
491   
492    def set_params(self, params):
493        """
494        """
495        x = params["radius"] 
496        self.set_cursor(x, self._inner_mouse_y)
497       
498
499class LineInteractor(_BaseInteractor):
500    """
501    Select an annulus through a 2D plot
502    """
503    def __init__(self, base, axes, color='black',
504                 zorder=5, r=1.0, theta=math.pi/4):
505        """
506        """
507        _BaseInteractor.__init__(self, base, axes, color=color)
508       
509        self.markers = []
510        self.axes = axes
511        self.save_theta = theta
512        self.theta= theta
513        self.radius = r
514        self.scale = 10.0
515        # Inner circle
516        x1 = self.radius * math.cos(self.theta)
517        y1 = self.radius * math.sin(self.theta)
518        x2 = -1*self.radius * math.cos(self.theta)
519        y2 = -1*self.radius * math.sin(self.theta)
520        try:
521            # Inner circle marker
522            self.inner_marker = self.axes.plot([x1/2.5], [y1/2.5], linestyle='',
523                                          marker='s', markersize=10,
524                                          color=self.color, alpha=0.6,
525                                          pickradius=5, label="pick", 
526                                          # Prefer this to other lines
527                                          zorder=zorder, 
528                                          visible=True)[0]
529        except:
530            self.inner_marker = self.axes.plot([x1/2.5], [y1/2.5], linestyle='',
531                                          marker='s', markersize=10,
532                                          color=self.color, alpha=0.6,
533                                          label="pick", 
534                                          visible=True)[0]
535            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION"
536            message += " OF MATPLOTLIB\n Get the SVN version that is at"
537            message += " least as recent as June 1, 2007"
538        self.line = self.axes.plot([x1, x2], [y1, y2],
539                                      linestyle='-', marker='',
540                                      color=self.color, visible=True)[0]
541        self.npts = 20
542        self.has_move = False
543        self.connect_markers([self.inner_marker, self.line])
544        self.update()
545
546    def set_layer(self, n):
547        """
548        """
549        self.layernum = n
550        self.update()
551       
552    def clear(self):
553        """
554        """
555        self.clear_markers()
556        try:
557            self.inner_marker.remove()
558            self.line.remove()
559        except:
560            # Old version of matplotlib
561            for item in range(len(self.axes.lines)):
562                del self.axes.lines[0]
563 
564    def update(self, theta=None):
565        """
566        Draw the new roughness on the graph.
567        """
568       
569        if theta != None:
570            self.theta = theta
571        x1 = self.radius * math.cos(self.theta)
572        y1 = self.radius * math.sin(self.theta)
573        x2 = -1 * self.radius * math.cos(self.theta)
574        y2 = -1 * self.radius * math.sin(self.theta)
575       
576        self.inner_marker.set(xdata=[x1/2.5], ydata=[y1/2.5])
577        self.line.set(xdata=[x1, x2], ydata=[y1, y2]) 
578   
579    def save(self, ev):
580        """
581        Remember the roughness for this layer and the next so that we
582        can restore on Esc.
583        """
584        self.save_theta= self.theta
585        self.base.freeze_axes()
586
587    def moveend(self, ev):
588        """
589        """
590        self.has_move = False
591        self.base.moveend(ev)
592           
593    def restore(self):
594        """
595        Restore the roughness for this layer.
596        """
597        self.theta = self.save_theta
598
599    def move(self, x, y, ev):
600        """
601        Process move to a new position, making sure that the move is allowed.
602        """
603        self.theta = math.atan2(y, x)
604        self.has_move = True
605        self.base.base.update()
606       
607    def set_cursor(self, x, y):
608        """
609        """
610        self.move(x, y, None)
611        self.update()
612       
613    def get_params(self):
614        """
615        """
616        params = {}
617        params["radius"] = self.radius
618        params["theta"] = self.theta
619        return params
620   
621    def set_params(self, params):
622        """
623        """
624        x = params["radius"] 
625        self.set_cursor(x, self._inner_mouse_y)
626       
Note: See TracBrowser for help on using the repository browser.