Ignore:
Timestamp:
Dec 18, 2008 5:26:36 PM (15 years ago)
Author:
Gervaise Alina <gervyh@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
f2eee4a
Parents:
ab8f936
Message:

working on slicer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • guiframe/local_perspectives/plotting/SectorSlicer.py

    rcd84dca ref0c170  
    88 
    99# Debug printout 
    10  
     10from sans.guicomm.events import StatusEvent  
     11from sans.guicomm.events import NewPlotEvent 
    1112from BaseInteractor import _BaseInteractor 
    1213from copy import deepcopy 
    1314import math 
    1415 
    15 from sans.guicomm.events import NewPlotEvent, StatusEvent 
     16 
    1617import SlicerParameters 
    1718import wx 
     
    3132        ## Number of points on the plot 
    3233        self.nbins = 20 
    33         theta1=math.pi/8 
    34         theta2=math.pi/2 
    35         theta1=2*math.pi/3 
    36         theta2=-2*math.pi/3 
    37         r1=self.qmax/2.0 
    38         r2=self.qmax/1.8 
    39         
     34        self.theta1= math.pi/4 
     35        self.theta2= math.pi/3 
     36        self.phi=math.pi/12 
     37        #self.theta3= 2*self.theta2 -self.theta1 
    4038        # Inner circle 
    41         from Arc import ArcInteractor 
    42         self.inner_circle = ArcInteractor(self, self.base.subplot, zorder=zorder, r=self.qmax/2.0,theta1= theta1, 
    43                                            theta2=theta2) 
    44         self.inner_circle.qmax = self.base.qmax 
    45         self.outer_circle = ArcInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax/1.8,theta1= theta1, 
    46                                            theta2=theta2) 
    47         self.outer_circle.qmax = self.base.qmax*1.2 
     39        self.main_line = LineInteractor(self, self.base.subplot,color='blue', zorder=zorder, r=self.qmax, 
     40                                           theta= self.theta2) 
     41        self.main_line.qmax = self.base.qmax 
     42        #self.left_line = SectionInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax, 
     43        #                                   theta1= self.theta1, theta2= self.theta2) 
     44        #self.left_line.qmax = self.base.qmax 
     45        self.right_line= SideInteractor(self, self.base.subplot,color='black', zorder=zorder, 
     46                                     r=self.qmax, 
     47                                           phi= -1*self.phi, 
     48                                           theta2=self.theta2) 
     49        self.right_line.qmax = self.base.qmax 
     50        self.left_line= SideInteractor(self, self.base.subplot,color='green', zorder=zorder, 
     51                                     r=self.qmax, 
     52                                           phi= self.phi, 
     53                                           theta2=self.theta2) 
     54        self.left_line.qmax = self.base.qmax 
    4855        #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) 
    49         from Edge import RadiusInteractor 
    50         self.right_edge= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, 
    51                                              arc1=self.inner_circle, 
    52                                              arc2=self.outer_circle, 
    53                                             theta=theta1) 
    54         self.left_edge= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, 
    55                                              arc1=self.inner_circle, 
    56                                              arc2=self.outer_circle, 
    57                                             theta=theta2) 
     56         
     57                       
    5858        self.update() 
    5959        self._post_data() 
     60         
    6061        # Bind to slice parameter events 
    6162        self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS) 
     
    6364 
    6465    def _onEVT_SLICER_PARS(self, event): 
    65         #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") 
     66        wx.PostEvent(self.base.parent, StatusEvent(status="SectorSlicer._onEVT_SLICER_PARS")) 
    6667        event.Skip() 
    6768        if event.type == self.__class__.__name__: 
     
    6970            self.base.update() 
    7071 
    71     """ 
    7272    def update_and_post(self): 
    7373        self.update() 
    74         self._post_data() 
    75  
    76     """ 
     74        #self._post_data() 
     75 
    7776    def save_data(self, path, image, x, y): 
    7877        output = open(path, 'w') 
     
    9190    def clear(self): 
    9291        self.clear_markers() 
    93         self.outer_circle.clear() 
    94         self.inner_circle.clear() 
    95         self.right_edge.clear() 
    96         self.left_edge.clear() 
     92        self.main_line.clear() 
     93        self.left_line.clear() 
     94        self.right_line.clear() 
    9795        #self.base.connect.disconnect() 
    9896        self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS) 
     
    103101        resetting the widgets. 
    104102        """ 
    105         # Update locations    
    106         if self.inner_circle.has_move:     
    107             print "inner circle has moved"  
    108             self.inner_circle.update() 
    109             r1=self.inner_circle.get_radius() 
    110             r2=self.outer_circle.get_radius() 
    111             self.right_edge.update(r1,r2) 
    112             self.left_edge.update(r1,r2) 
    113         if self.outer_circle.has_move:     
    114             print "outer circle has moved"  
    115             self.outer_circle.update() 
    116             r1=self.inner_circle.get_radius() 
    117             r2=self.outer_circle.get_radius() 
    118             self.left_edge.update(r1,r2) 
    119             self.right_edge.update(r1,r2) 
    120         if self.right_edge.has_move: 
    121             print "right edge has moved" 
    122             self.right_edge.update() 
    123             self.inner_circle.update(theta1=self.right_edge.get_angle(), theta2=None) 
    124             self.outer_circle.update(theta1=self.right_edge.get_angle(), theta2=None) 
    125         if  self.left_edge.has_move: 
    126             print "left Edge has moved" 
    127             self.left_edge.update() 
    128             self.inner_circle.update(theta1=None, theta2=self.left_edge.get_angle()) 
    129             self.outer_circle.update(theta1=None, theta2=self.left_edge.get_angle()) 
    130               
    131          
     103        # Update locations         
     104         
     105        #if self.main_line.has_move: 
     106        #self.main_line.update()    
     107        #self.right_line.update()     
     108        #self.left_line.update()      
     109        if self.main_line.has_move: 
     110            self.main_line.update() 
     111            self.right_line.update( delta = self.main_line.get_radius(),mline= self.main_line) 
     112            self.left_line.update( delta = self.main_line.get_radius() ,mline= self.main_line) 
     113            print "Main line has moved ---> phi right",math.degrees(self.main_line.theta-self.right_line.theta) 
     114            print "Main line has moved ---> phi left",math.degrees(self.main_line.theta-self.left_line.theta) 
     115        if self.left_line.has_move: 
     116            print "left line has moved --->" 
     117            self.main_line.update() 
     118            self.left_line.update(phi=None,delta=None, mline=self.main_line,side=True) 
     119            #self.right_line.update(-1*delta,linem=self.main_line,linel=self.left_line) 
     120            self.right_line.update(phi=-1*self.left_line.phi,delta=None, mline=self.main_line,side=True) 
     121        if self.right_line.has_move: 
     122            print "right line has moved --->" 
     123            
     124            self.main_line.update() 
     125            self.right_line.update(phi=None,delta=None, mline=self.main_line,side=True) 
     126            #self.right_line.update(-1*delta,linem=self.main_line,linel=self.left_line) 
     127            self.left_line.update(phi=-1*self.right_line.phi,delta=None, mline=self.main_line,side=True) 
     128    
     129     
     130     
     131 
    132132    def save(self, ev): 
    133133        """ 
     
    138138        self.inner_circle.save(ev) 
    139139        self.outer_circle.save(ev) 
    140         self.right_edge.save(ev) 
    141         self.left_edge.save(ev) 
    142          
    143     def _post_data(self): 
    144         pass 
    145      
    146     def post_data(self,new_sector ): 
    147         """ post data averaging in Q""" 
    148         if self.inner_circle.get_radius() < self.outer_circle.get_radius(): 
    149             rmin=self.inner_circle.get_radius() 
    150             rmax=self.outer_circle.get_radius() 
    151         else: 
    152             rmin=self.outer_circle.get_radius() 
    153             rmax=self.inner_circle.get_radius() 
    154         if self.right_edge.get_angle() < self.left_edge.get_angle(): 
    155             phimin=self.right_edge.get_angle() 
    156             phimax=self.left_edge.get_angle() 
    157         else: 
    158             phimin=self.left_edge.get_angle() 
    159             phimax=self.right_edge.get_angle() 
    160              
    161         print "phimin, phimax, rmin ,rmax",math.degrees(phimin), math.degrees(phimax), rmin ,rmax 
    162         #from DataLoader.manipulations import SectorQ 
    163          
    164         sect = new_sector(r_min=rmin, r_max=rmax, phi_min=phimin, phi_max=phimax) 
     140 
     141    def _post_data(self, nbins=None): 
     142        # Compute data 
     143        data = self.base.data2D 
     144        # If we have no data, just return 
     145        if data == None: 
     146            return 
     147 
     148        name = "Sector " 
     149        from DataLoader.manipulations import SectorQ 
     150        radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2)) 
     151        phimin = self.right_line.theta 
     152        phimax = self.left_line.theta 
     153        sect = SectorQ(r_min=-1*radius , r_max= radius , phi_min=phimin, phi_max=phimax) 
     154        if nbins!=None: 
     155            sect.nbins = nbins 
     156         
    165157        sector = sect(self.base.data2D) 
    166158         
     
    176168        
    177169        new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw) 
    178         new_plot.name = str(new_sector.__name__) +"("+ self.base.data2D.name+")" 
     170        new_plot.name = "SectorQ" +"("+ self.base.data2D.name+")" 
    179171         
    180172        
     
    188180        new_plot.xaxis("\\rm{Q}", 'rad') 
    189181        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 
    190         new_plot.group_id = str(new_sector.__name__)+self.base.data2D.name 
     182        new_plot.group_id = "SectorQ"+self.base.data2D.name 
    191183        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot, 
    192                                                  title=str(new_sector.__name__) )) 
    193          
     184                                                 title="SectorQ" )) 
     185         
     186          
    194187         
    195188    def moveend(self, ev): 
    196         #self.base.thaw_axes() 
    197          
    198          # Post paramters 
    199         #event = SlicerParameters.SlicerParameterEvent() 
    200         #event.type = self.__class__.__name__ 
    201         #event.params = self.get_params() 
    202         #print "main moveend ", event.params 
    203         #wx.PostEvent(self.base.parent, event) 
     189        self.base.thaw_axes() 
     190         
     191        # Post paramters 
     192        event = SlicerParameters.SlicerParameterEvent() 
     193        event.type = self.__class__.__name__ 
     194        event.params = self.get_params() 
     195        wx.PostEvent(self.base.parent, event) 
    204196        self._post_data() 
    205197             
     
    208200        Restore the roughness for this layer. 
    209201        """ 
    210         self.inner_circle.restore() 
    211         self.outer_circle.restore() 
    212         self.right_edge.restore() 
    213         self.left_edge.restore() 
     202        self.main_line.restore() 
     203        self.left_line.restore() 
     204        self.right_line.restore() 
    214205 
    215206    def move(self, x, y, ev): 
     
    224215    def get_params(self): 
    225216        params = {} 
    226         params["r_min"] = self.inner_circle.get_radius() 
    227         params["r_max"] = self.outer_circle.get_radius() 
    228         params["phi_min"] = self.right_edge.get_angle() 
    229         params["phi_max"] = self.left_edge.get_angle() 
     217        params["main_phi"] = self.main_line.theta 
     218        if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi): 
     219            raise ValueError,"Phi left and phi right are different %f, %f"%(self.left_line.phi, self.right_line.phi) 
     220        params["left_phi"] = self.left_line.phi 
    230221        params["nbins"] = self.nbins 
    231222        return params 
    232223     
    233224    def set_params(self, params): 
    234         print "setparams on main slicer ",params 
    235         inner = params["r_min"]  
    236         outer = params["r_max"]  
    237         phi_min= params["phi_min"] 
    238         phi_max=params["phi_max"] 
     225         
     226        main = params["main_phi"]  
     227        phi = params["left_phi"]  
    239228        self.nbins = int(params["nbins"]) 
    240          
    241          
    242         self.inner_circle.set_cursor(inner, phi_min, phi_max,self.nbins) 
    243         self.outer_circle.set_cursor(outer,  phi_min, phi_max, self.nbins) 
    244         self.right_edge.set_cursor(inner, outer, phi_min) 
    245         self.left_edge.set_cursor(inner, outer, phi_max) 
    246         self._post_data() 
     229        self.main_line.theta= main 
     230         
     231        self.main_line.update() 
     232        self.right_line.update(phi=-1*phi,delta=None, mline=self.main_line,side=True) 
     233        self.left_line.update(phi=phi,delta=None, mline=self.main_line,side=True) 
     234        
     235        self._post_data(nbins=self.nbins) 
    247236         
    248237    def freeze_axes(self): 
     
    255244        self.base.draw() 
    256245 
    257 class SectorInteractorQ(SectorInteractor): 
    258     def __init__(self,base,axes,color='black', zorder=3): 
    259         SectorInteractor.__init__(self, base, axes, color=color) 
    260         self.base=base 
    261         self._post_data() 
    262     def _post_data(self): 
    263         from DataLoader.manipulations import SectorQ 
    264         self.post_data(SectorQ )    
    265          
    266  
    267 class SectorInteractorPhi(SectorInteractor): 
    268     def __init__(self,base,axes,color='black', zorder=3): 
    269         SectorInteractor.__init__(self, base, axes, color=color) 
    270         self.base=base 
    271         self._post_data() 
    272     def _post_data(self): 
    273         from DataLoader.manipulations import SectorPhi 
    274         self.post_data(SectorPhi )    
    275          
    276          
     246         
     247class SideInteractor(_BaseInteractor): 
     248    """ 
     249         Select an annulus through a 2D plot 
     250    """ 
     251    def __init__(self,base,axes,color='black', zorder=5, r=1.0,phi=math.pi/4, theta2= math.pi/3): 
     252         
     253        _BaseInteractor.__init__(self, base, axes, color=color) 
     254        self.markers = [] 
     255        self.axes = axes 
     256         
     257        self.save_theta = theta2 + phi 
     258        self.theta=  theta2 + phi 
     259        self.theta2 = theta2 
     260        self.radius = r 
     261        self.phi = phi 
     262        self.scale = 10.0 
     263        #print "init for line side theta2, phi, theta",math.degrees(theta2),math.degrees(phi),math.degrees(self.theta)  
     264        #raise "Version error", message 
     265           
     266        # Inner circle 
     267        x1= self.radius*math.cos(self.theta) 
     268        y1= self.radius*math.sin(self.theta) 
     269        x2= -1*self.radius*math.cos(self.theta) 
     270        y2= -1*self.radius*math.sin(self.theta) 
     271        
     272        self.line = self.axes.plot([x1,x2],[y1,y2], 
     273                                      linestyle='-', marker='', 
     274                                      color=self.color, 
     275                                      visible=True)[0] 
     276        
     277        self.npts = 20 
     278        self.has_move=False 
     279        self.connect_markers([self.line]) 
     280        #self.update() 
     281 
     282    def set_layer(self, n): 
     283        self.layernum = n 
     284        self.update() 
     285         
     286    def clear(self): 
     287        self.clear_markers() 
     288        try: 
     289             
     290            self.line.remove() 
     291        except: 
     292            # Old version of matplotlib 
     293            for item in range(len(self.axes.lines)): 
     294                del self.axes.lines[0] 
     295         
     296         
     297         
     298    def get_radius(self): 
     299         
     300        return self.theta - self.save_theta 
     301         
     302    def update(self,phi=None,delta=None, mline=None,side=False): 
     303        """ 
     304        Draw the new roughness on the graph. 
     305        """ 
     306        #print "update left or right ", self.has_move 
     307         
     308        if phi !=None: 
     309            self.phi = phi 
     310        if delta==None: 
     311            delta = 0 
     312        if side== True: 
     313            self.theta=  mline.theta + self.phi 
     314        if mline!=None: 
     315            self.theta2 = mline.theta 
     316        #print "U:for line side theta2, phi, theta",math.degrees(self.theta2),math.degrees(self.phi),math.degrees(self.theta)  
     317        #if self.theta2 >= self.theta and self.theta>=0: 
     318        #    print "between 0-pi",math.degrees(self.theta)  
     319         
     320        x1= self.radius*math.cos(self.theta + delta) 
     321        y1= self.radius*math.sin(self.theta + delta) 
     322        x2= -1*self.radius*math.cos(self.theta + delta) 
     323        y2= -1*self.radius*math.sin(self.theta + delta) 
     324        
     325         
     326        self.line.set(xdata=[x1,x2], ydata=[y1,y2])   
     327         
     328        
     329         
     330         
     331    def save(self, ev): 
     332        """ 
     333        Remember the roughness for this layer and the next so that we 
     334        can restore on Esc. 
     335        """ 
     336        self.save_theta= self.theta 
     337        self.base.freeze_axes() 
     338 
     339    def moveend(self, ev): 
     340         
     341        self.has_move=False 
     342        self.base.moveend(ev) 
     343             
     344    def restore(self): 
     345        """ 
     346        Restore the roughness for this layer. 
     347        """ 
     348        self.theta = self.save_theta 
     349 
     350    def move(self, x, y, ev): 
     351        """ 
     352        Process move to a new position, making sure that the move is allowed. 
     353        """ 
     354         
     355        self.theta= math.atan2(y,x) 
     356        self.phi= self.theta2 - self.theta 
     357         
     358        print "move left or right phi ---theta--thetaM", self.phi, self.theta, self.theta2 
     359        self.has_move=True 
     360        self.base.base.update() 
     361         
     362    def set_cursor(self, x, y): 
     363        self.move(x, y, None) 
     364        self.update() 
     365         
     366         
     367    def get_params(self): 
     368        params = {} 
     369        params["radius"] = self.radius 
     370        params["theta"] = self.theta 
     371        return params 
     372     
     373    def set_params(self, params): 
     374 
     375        x = params["radius"]  
     376        self.set_cursor(x, self._inner_mouse_y) 
     377         
     378 
     379 
     380         
     381         
     382class LineInteractor(_BaseInteractor): 
     383    """ 
     384         Select an annulus through a 2D plot 
     385    """ 
     386    def __init__(self,base,axes,color='black', zorder=5, r=1.0,theta=math.pi/4): 
     387         
     388        _BaseInteractor.__init__(self, base, axes, color=color) 
     389        self.markers = [] 
     390        self.axes = axes 
     391         
     392        self.save_theta = theta  
     393        self.theta= theta 
     394         
     395        self.radius = r 
     396       
     397        self.scale = 10.0 
     398       
     399        #raise "Version error", message 
     400             
     401        # Inner circle 
     402 
     403            
     404        x1= self.radius*math.cos(self.theta) 
     405        y1= self.radius*math.sin(self.theta) 
     406        x2= -1*self.radius*math.cos(self.theta) 
     407        y2= -1*self.radius*math.sin(self.theta) 
     408        
     409        self.line = self.axes.plot([x1,x2],[y1,y2], 
     410                                      linestyle='-', marker='', 
     411                                      color=self.color, 
     412                                      visible=True)[0] 
     413       
     414        self.npts = 20 
     415        self.has_move=False 
     416        self.connect_markers([self.line]) 
     417        self.update() 
     418 
     419    def set_layer(self, n): 
     420        self.layernum = n 
     421        self.update() 
     422         
     423    def clear(self): 
     424        self.clear_markers() 
     425        try: 
     426             
     427            self.line.remove() 
     428        except: 
     429            # Old version of matplotlib 
     430            for item in range(len(self.axes.lines)): 
     431                del self.axes.lines[0] 
     432         
     433         
     434         
     435    def get_radius(self): 
     436         
     437        return self.theta - self.save_theta 
     438         
     439    def update(self, theta=None): 
     440        """ 
     441        Draw the new roughness on the graph. 
     442        """ 
     443        print "update main line", self.theta 
     444        if theta !=None: 
     445            self.theta= theta 
     446        x1= self.radius*math.cos(self.theta) 
     447        y1= self.radius*math.sin(self.theta) 
     448        x2= -1*self.radius*math.cos(self.theta) 
     449        y2= -1*self.radius*math.sin(self.theta) 
     450       
     451        self.line.set(xdata=[x1,x2], ydata=[y1,y2])   
     452      
     453         
     454         
     455    def save(self, ev): 
     456        """ 
     457        Remember the roughness for this layer and the next so that we 
     458        can restore on Esc. 
     459        """ 
     460        self.save_theta= self.theta 
     461        self.base.freeze_axes() 
     462 
     463    def moveend(self, ev): 
     464         
     465        self.has_move=False 
     466        self.base.moveend(ev) 
     467             
     468    def restore(self): 
     469        """ 
     470        Restore the roughness for this layer. 
     471        """ 
     472        self.theta = self.save_theta 
     473 
     474    def move(self, x, y, ev): 
     475        """ 
     476        Process move to a new position, making sure that the move is allowed. 
     477        """ 
     478         
     479        self.theta= math.atan2(y,x) 
     480        print "main_line previous theta --- next theta ",math.degrees(self.save_theta),math.degrees(self.theta) 
     481         
     482        self.has_move=True 
     483        self.base.base.update() 
     484         
     485    def set_cursor(self, x, y): 
     486        self.move(x, y, None) 
     487        self.update() 
     488         
     489         
     490    def get_params(self): 
     491        params = {} 
     492        params["radius"] = self.radius 
     493        params["theta"] = self.theta 
     494        return params 
     495     
     496    def set_params(self, params): 
     497 
     498        x = params["radius"]  
     499        self.set_cursor(x, self._inner_mouse_y) 
     500         
     501 
     502 
     503         
Note: See TracChangeset for help on using the changeset viewer.