source: sasview/guiframe/local_perspectives/plotting/SectorSlicer.py @ 8f584c9

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 8f584c9 was 30ae3bb, checked in by Gervaise Alina <gervyh@…>, 16 years ago

remove all data.info field on postdata function

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