Changeset 7a03e65 in sasview


Ignore:
Timestamp:
Apr 10, 2008 4:29:17 PM (16 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:
370e587
Parents:
3d3a0e5
Message:

plotpanel modified plottables and fitdialog

Location:
guitools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • guitools/PlotPanel.py

    r3d3a0e5 r7a03e65  
    6363        self.prevXtrans =" " 
    6464        self.prevYtrans =" " 
     65         
    6566    def returnTrans(self): 
    6667        return self.xscales,self.yscales 
     
    8182        list = self.graph.returnPlottable() 
    8283        from fitDialog import LinearFit 
    83         print len(list) 
     84         
    8485        if len(list.keys())>0: 
    8586            first_item = list.keys()[0] 
     
    101102        dial.Destroy() 
    102103             
    103     def toX(self,x): 
     104    def toX(self,x,y=None): 
    104105        """ 
    105106            This function is used to load value on Plottable.View 
     
    109110        return x 
    110111     
    111     def toX2(self,x): 
     112    def toX2(self,x,y=None): 
    112113        """ 
    113114            This function is used to load value on Plottable.View 
     
    117118        return x*x 
    118119     
    119     def fromX2(self,x): 
     120    def fromX2(self,x,y=None): 
    120121         """ 
    121122             This function is used to load value on Plottable.View 
     
    127128         else: 
    128129             return math.sqrt(x) 
    129     def toLogX(self,x): 
     130    def toLogX(self,x,y=None): 
    130131        """ 
    131132            This function is used to load value on Plottable.View 
     
    138139            return math.log(x) 
    139140         
    140     def toOneOverX(self,x): 
     141    def toOneOverX(self,x,y=None): 
    141142        if x !=0: 
    142143            return 1/x 
    143144        else: 
    144145            raise ValueError,"cannot divide by zero" 
    145     def toOneOverSqrtX(self,x): 
    146         if x > 0: 
    147             return 1/math.sqrt(x) 
    148         else: 
    149             raise ValueError,"cannot be computed" 
     146    def toOneOverSqrtX(self,x=None,y=None): 
     147        if y!=None: 
     148            if y > 0: 
     149                return 1/math.sqrt(y) 
     150            else: 
     151                raise ValueError,"cannot be computed" 
     152        if x!= None: 
     153            if x > 0: 
     154                return 1/math.sqrt(x) 
     155            else: 
     156                raise ValueError,"cannot be computed" 
     157         
    150158    def toLogYX2(self,x,y): 
    151159        if y*(x**2) >0: 
     
    247255    def onSaveImage(self, evt): 
    248256        #figure.savefig 
    249         print "Save image not implemented" 
     257        #print "Save image not implemented" 
    250258        path = None 
    251259        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.png", wx.SAVE) 
     
    285293        pos = self.ScreenToClient(pos) 
    286294        self.PopupMenu(slicerpop, pos) 
    287          
    288295     
    289296    ## The following is plottable functionality 
     
    452459                name, units = item.get_yaxis() 
    453460                self.graph.yaxis("%s" %name,  "%s" % units) 
     461                 
    454462            if ( self.yscales =="Log(y*x)"): 
    455                 item.transform_xy( self.toXY ,self.errToXY ) 
    456                 self.set_yscale("log") 
     463                item.transform_y( self.toLogXY ,self.errToLogXY ) 
     464                self.set_yscale("linear") 
    457465                yname, yunits = item.get_yaxis() 
    458466                xname, xunits = item.get_xaxis() 
    459                 self.graph.yaxis("%s"+"*"+"%s" % (yname,xname),  "%s^{-1}"+"*"+"s^{-1}" % (yunits,xunits)) 
     467                self.graph.yaxis("%s%s" % (yname,xname),  "%s^{-1}%s^{-1}" % (yunits,xunits)) 
    460468            if ( self.yscales =="Log(y*x^(2)"): 
    461                 item.transform_xy( self.toYX2 ,self.errToYX2 ) 
    462                 self.set_yscale("log") 
     469                item.transform_y( self.toYX2 ,self.errToYX2 ) 
     470                self.set_yscale("linear") 
    463471                yname, yunits = item.get_yaxis() 
    464472                xname, xunits = item.get_xaxis() 
    465                 self.graph.yaxis("%s"+"*"+"%s^{2}" % (yname,xname),  "%s^{-1}"+"*"+"s^{-2}" % (yunits,xunits)) 
     473                self.graph.yaxis("%s%s^{2}" % (yname,xname),  "%s^{-1}%s^{-2}" % (yunits,xunits)) 
    466474    
    467475        self.prevXtrans = self.xscales  
     
    471479        self.subplot.figure.canvas.draw_idle() 
    472480         
    473     def errToX(self,x,dx=None): 
     481    def errToX(self,x,y=None,dx=None,dy=None): 
    474482        """ 
    475483            calculate error of x**2 
     
    477485            @param dx: float value 
    478486        """ 
    479         if (dx != None) and (math.fabs(dx) >= math.fabs(x)): 
    480             return math.fabs(0.9*x) 
    481         if dx==None: 
    482              return 0 
    483         return math.fabs(dx) 
    484      
    485     def errToX2(self,x,dx=None): 
     487        return dx 
     488     
     489     
     490    def errToX2(self,x,y=None,dx=None,dy=None): 
    486491        """ 
    487492            calculate error of x**2 
     
    496501        else: 
    497502            return 0.0 
    498     def errFromX2(self,x,dx=None): 
     503    def errFromX2(self,x,y=None,dx=None,dy=None): 
    499504        """ 
    500505            calculate error of sqrt(x) 
     
    514519            return math.fabs(err) 
    515520         
    516     def errToLogX(self,x,dx=None): 
    517         """ 
    518             calculate error of Log(xy) 
     521    def errToLogX(self,x,y=None,dx=None,dy=None): 
     522        """ 
     523            calculate error of Log(x) 
    519524            @param x: float value 
    520525            @param dx: float value 
    521526        """ 
    522         if x!=0: 
    523             if dx==None: 
    524                 #err = 1/x 
    525                 err = 0 
    526             else: 
    527                 err = dx/x 
    528             if math.fabs(err) >= math.fabs(x): 
    529                 err = 0.9*x 
    530         else: 
    531             err = 0.9*x 
    532          
    533         return math.fabs(err) 
     527        if math.fabs(dx) >= math.fabs(x): 
     528            return 0.9*x 
     529        return dx 
     530     
    534531    def errToXY(self, x, y, dx=None, dy=None): 
    535532        if dx==None: 
     
    541538            err =0.9*x 
    542539        return err  
     540     
    543541    def errToYX2(self, x, y, dx=None, dy=None): 
    544542        if dx==None: 
     
    585583        return math.sqrt(math.fabs(err))  
    586584             
    587     def errOneOverX(self,x,dx): 
     585    def errOneOverX(self,x,y=None,dx=None, dy=None): 
    588586        """ 
    589587             calculate error on 1/x 
    590588        """ 
    591         if (x != 0) and (dx!=None): 
     589        if (x != 0): 
    592590            if dx ==None: 
    593591                dx= 0 
     
    600598        return math.fabs(err) 
    601599     
    602     def errOneOverSqrtX(self): 
     600    def errOneOverSqrtX(self,x,y=None, dx=None,dy=None): 
    603601        """ 
    604602            Calculate error on 1/sqrt(x) 
    605603        """ 
    606         if (x >0) and (dx!=None): 
     604        if (x >0): 
    607605            if dx==None: 
    608606                dx =0 
     
    625623        self.graph.add(plottable) 
    626624        self.graph.render(self) 
     625        self.subplot.figure.canvas.draw_idle() 
    627626        self.graph.delete(plottable) 
    628         self.subplot.figure.canvas.draw_idle() 
     627         
     628      
    629629         
    630630     
  • guitools/fitDialog.py

    rf52bea1 r7a03e65  
    2020        #dialog self plottable 
    2121        self.plottable = plottable 
    22          
    2322         
    2423        #Dialog interface 
     
    10099            Push a plottable to  
    101100        """ 
    102         
    103         temp =[] 
     101        temp=[] 
    104102        tempx=[] 
    105103        tempy=[] 
     104        tempdy = [] 
     105         
     106        #Check if the field of Fit Dialog contain values and use the x max and min of the user 
    106107        xmin = self._checkVal(self.tcXmin.GetValue()) 
    107108        xmax = self._checkVal(self.tcXmax.GetValue()) 
     
    109110        #store the values of View in x,y, dx,dy 
    110111        x,y,dx,dy=self.plottable.returnValuesOfView() 
     112        # Receive transformations of x and y 
    111113        self.xtrans,self.ytrans= self.transform() 
     114        # The x array min and max 
     115        _min = min(x) 
     116        _max = max(x) 
     117        
     118                         
     119        if (xmin ==None)and (xmax == None): 
     120            #Display the min and the max of x on fit dialog fields 
     121            self.tcXmin.SetValue(str(min(x))) 
     122            self.tcXmax.SetValue(str(max(x))) 
     123         
     124           
    112125        #Display the fittings values 
    113126        default_A = self.model.getParam('A')  
     
    115128        cstA = fittings.Parameter(self.model, 'A', default_A) 
    116129        cstB  = fittings.Parameter(self.model, 'B', default_B) 
     130                   
    117131        if  self.ytrans == "Log(y)": 
    118132            for y_i in y: 
    119                 tempy.append(log(y_i))        
    120             chisqr, out, cov = fittings.sansfit(self.model,  
    121                             [cstA, cstB],x, tempy,dy,min(x),max(x)) 
     133                temp.append(math.log(y_i))  
     134                tempdy.append(1.0) 
     135            if (xmin !=None and xmin>= _min) and (xmax != None and xmax<= _max):    
     136                chisqr, out, cov = fittings.sansfit(self.model,  
     137                            [cstA, cstB],x, temp,tempdy,xmin,xmax) 
     138            else: 
     139                chisqr, out, cov = fittings.sansfit(self.model,  
     140                            [cstA, cstB],x, temp,tempdy,_min,_max) 
    122141        else : 
    123             chisqr, out, cov = fittings.sansfit(self.model,  
    124                             [cstA, cstB],x, y,dy,min(x),max(x)) 
     142            if (xmin !=None and xmin >= _min) and (xmax != None and xmax <= _max):    
     143                chisqr, out, cov = fittings.sansfit(self.model,  
     144                            [cstA, cstB],x, y,dy,xmin,xmax) 
     145            else: 
     146                chisqr, out, cov = fittings.sansfit(self.model,  
     147                            [cstA, cstB],x, y,dy,_min,_max) 
    125148        #Check that cov and out are iterable before displaying them 
    126149        if cov ==None: 
     
    139162        self.model.setParam('B', float(cstB)) 
    140163         
     164        #Check if View contains a x array and makes transformation for y as a line to fit 
    141165        if x != []: 
    142             if xmin !=None  and xmax != None: 
    143                 for j in range(len(x)): 
    144                     if x[j] > xmin and x[j] < xmax: 
    145                         temp.append(self.model.run(x[j])) 
    146             else: 
    147                 # x has a default value in case the user doesn't load data 
    148                 for x_i in x: 
    149                     temp.append(self.model.run(x_i)) 
    150                 self.tcXmin.SetValue(str(min(x))) 
    151                 self.tcXmax.SetValue(str(max(x))) 
    152                 xmin = self._checkVal(self.tcXmin.GetValue()) 
    153                 xmax = self._checkVal(self.tcXmax.GetValue()) 
    154                  
    155             self.file_data1.x =x 
     166            for j in range(len(x)):  
     167                    if (xmin !=None)and (xmax != None): 
     168                        if (x[j] > xmin and x[j] < xmax): 
     169                            y_model = self.model.run(x[j]) 
     170                    else: 
     171                        # x has a default value in case the user doesn't load data 
     172                        y_model = self.model.run(x[j]) 
     173                    if self.ytrans == "Log(y)": 
     174                        tempy.append(math.exp(y_model)) 
     175                    else: 
     176                        tempy.append(y_model) 
     177            
    156178            # Create new data plottable with result 
    157179            self.file_data1.y =[] 
    158             self.xtrans, self.ytrans= self.transform() 
    159              
    160             if self.ytrans == "Log(y)": 
    161                 for x_i in x: 
    162                     self.file_data1.y.append(exp(self.model.run(x_i))) 
    163             else: 
    164                 self.file_data1.y =temp      
     180            self.file_data1.x =x 
     181            self.file_data1.y =tempy      
    165182            self.file_data1.dx=None 
    166183            self.file_data1.dy=None 
    167184             
     185            #Load the view with the new values 
    168186            self.file_data1.reset_view() 
    169187             
    170188            #Send the data to display to the PlotPanel 
    171             # 
    172189            self.push_data(self.file_data1) 
     190             
    173191            # Display the fitting value on the Fit Dialog 
    174192            self._onsetValues(cstA, cstB, errA,errB,chisqr) 
    175         
     193    
    176194    def _onsetValues(self,cstA,cstB,errA,errB,Chi): 
    177195         """ 
  • guitools/plottables.py

    r3d3a0e5 r7a03e65  
    382382             
    383383        """ 
    384         self.view.transform_x(func, errfunc, self.x, self.dx) 
     384        self.view.transform_x(func, errfunc, x=self.x, y=self.y, dx=self.dx, dy=self.dy) 
    385385     
    386386    def transform_y(self, func, errfunc): 
     
    389389             
    390390        """ 
    391         self.view.transform_y(func, errfunc, self.y, self.dy) 
    392          
    393     def transform_xy(self,func,errfunc): 
    394         """ 
    395             @param func: reference to y transformation function 
    396              
    397         """ 
    398         self.view.transform_xy(func, errfunc, self.x, self.y,self.dx,self.dy) 
     391        self.view.transform_y(func, errfunc, self.y, self.x, self.dx,  self.dy) 
     392         
    399393         
    400394    def returnValuesOfView(self): 
     
    418412            self.dy = dy 
    419413             
    420         def transform_x(self, func, errfunc, x, dx): 
     414        def transform_x(self, func, errfunc, x,y=None,dx=None, dy=None): 
    421415            """ 
    422416                Transforms the x and dx vectors and stores the output. 
     
    429423            import copy 
    430424            import numpy 
     425             
    431426            # Sanity check 
     427            has_y = False 
    432428            if dx and not len(x)==len(dx): 
    433                 raise ValueError, "Plottable.View: Given x and dx are not of the same length" 
    434              
     429                    raise ValueError, "Plottable.View: Given x and dx are not of the same length"  
     430            # Check length of y array 
     431            if not y==None: 
     432                if not len(y)==len(x): 
     433                    raise ValueError, "Plottable.View: Given y and x are not of the same length" 
     434                else: 
     435                    has_y = True 
     436                if dy and not len(y)==len(dy): 
     437                    raise ValueError, "Plottable.View: Given y and dy are not of the same length" 
    435438             
    436439            self.x = numpy.zeros(len(x)) 
     
    438441             
    439442            for i in range(len(x)): 
    440                 self.x[i] = func(x[i]) 
    441                 if dx !=None: 
    442                     self.dx[i] = errfunc(x[i], dx[i]) 
     443                if has_y: 
     444                     self.x[i] = func(x[i],y[i]) 
     445                     if (dx!=None) and (dy !=None): 
     446                         self.dx[i] = errfunc(x[i], y[i], dx[i], dy[i]) 
     447                     elif (dx != None): 
     448                         self.dx[i] = errfunc(x[i], y[i], dx[i],0) 
     449                     elif (dy != None): 
     450                         self.dx[i] = errfunc(x[i], y[i],0,dy[i]) 
     451                     else: 
     452                         self.dx[i] = errfunc(x[i],y[i],0, 0) 
    443453                else: 
    444                    self.dx[i] = errfunc(x[i])        
    445         def transform_y(self, func, errfunc, y, dy): 
     454                    self.x[i] = func(x[i]) 
     455                    if (dx != None): 
     456                        self.dx[i] = errfunc(x[i], dx[i]) 
     457                    else: 
     458                        self.dx[i] = errfunc(x[i],None) 
     459                     
     460                          
     461        def transform_y(self, func, errfunc, y, x=None,dx=None,dy=None): 
    446462            """ 
    447                 Transforms the x and dx vectors and stores the output. 
     463                Transforms the y and dy vectors and stores the output. 
    448464                 
    449                 @param func: function to apply to the data 
     465                @param func: function to apply to the data y 
     466                @param x: array of x values 
     467                @param dx: array of error values 
    450468                @param y: array of y values 
    451469                @param dy: array of error values 
    452                 @param errfunc: function to apply to errors 
     470                @param errfunc: function to apply to errors dy 
    453471            """ 
    454472            import copy 
    455473            import numpy 
    456474            # Sanity check 
     475            has_x = False 
    457476            if dy and not len(y)==len(dy): 
    458477                raise ValueError, "Plottable.View: Given y and dy are not of the same length" 
     478            # Check length of x array 
     479            if not x==None: 
     480                if not len(y)==len(x): 
     481                    raise ValueError, "Plottable.View: Given y and x are not of the same length" 
     482                else: 
     483                    has_x = True 
     484                if dx and not len(x)==len(dx): 
     485                    raise ValueError, "Plottable.View: Given x and dx are not of the same length" 
    459486             
    460487            self.y = numpy.zeros(len(y)) 
     
    462489            
    463490            for i in range(len(y)): 
    464                  self.y[i] = func(y[i]) 
    465                  if dy !=None: 
    466                      self.dy[i] = errfunc(y[i], dy[i]) 
     491                 
     492                 if has_x: 
     493                     self.y[i] = func(y[i],x[i]) 
     494                     if (dx!=None) and (dy !=None): 
     495                         self.dy[i] = errfunc(y[i], x[i], dy[i], dx[i]) 
     496                     elif (dx != None): 
     497                         self.dy[i] = errfunc(y[i], x[i], 0, dx[i]) 
     498                     elif (dy != None): 
     499                         self.dy[i] = errfunc(y[i], x[i], dy[i], 0) 
     500                     else: 
     501                         self.dy[i] = errfunc(y[i], None) 
    467502                 else: 
    468                      self.dy[i] = errfunc(y[i]) 
    469         def transform_xy(self, func, errfunc, x, y, dx, dy): 
    470             """ 
    471                 Transforms the x, y, dx,and dy vectors and stores the output. 
     503                     self.y[i] = func(y[i]) 
     504                     if (dy != None): 
     505                         self.dy[i] = errfunc( y[i],dy[i]) 
     506                     else: 
     507                         self.dy[i] = errfunc( y[i],None) 
    472508                 
    473                 @param func: function to apply to the data 
    474                 @param x: array of x values 
    475                 @param dx: array of error values 
    476                 @param y: array of y values 
    477                 @param dy: array of error values 
    478                 @param errfunc: function to apply to errors 
    479             """ 
    480             import copy 
    481             import numpy 
    482             # Sanity check 
    483             if dx and not len(x)==len(dx): 
    484                 raise ValueError, "Plottable.View: Given x and dx are not of the same length" 
    485             if dy and not len(y)==len(dy): 
    486                 raise ValueError, "Plottable.View: Given y and dy are not of the same length" 
    487             if not len(x)==len(y): 
    488                 raise ValueError, "Plottable.View: Given x and y are not of the same length" 
    489              
    490             self.x = numpy.zeros(len(x)) 
    491             self.dx = numpy.zeros(len(x)) 
    492             self.y = numpy.zeros(len(y)) 
    493             self.dy = numpy.zeros(len(y)) 
    494              
    495             
    496             for i in range(len(y)): 
    497                  self.y[i] = func(x[i],y[i]) 
    498                  if (dx!=None) and (dy !=None): 
    499                      self.dy[i] = errfunc(x[i], y[i], dx[i], dy[i]) 
    500                  elif (dx != None): 
    501                      self.dy[i] = errfunc(x[i], y[i], dx[i]) 
    502                  elif (dy != None): 
    503                      self.dy[i] = errfunc(x[i], y[i],dy[i]) 
    504                  else: 
    505                      self.dy[i] = errfunc(x[i], y[i]) 
    506                       
     509      
    507510        def returnXview(self): 
    508511            return self.x,self.y,self.dx,self.dy 
Note: See TracChangeset for help on using the changeset viewer.