Changeset f52bea1 in sasview for guitools


Ignore:
Timestamp:
Apr 8, 2008 3:04:51 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:
3d3a0e5
Parents:
8742751
Message:

Plotpanel plottable fitdialog req modified

Location:
guitools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • guitools/PlotPanel.py

    rd490a02 rf52bea1  
    5858        self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] 
    5959        #User scale 
    60         self.xscales ="" 
    61         self.yscales ="" 
     60        self.xscales ="x" 
     61        self.yscales ="Log(y)" 
    6262        # keep track if the previous transformation of x and y in Property dialog 
    6363        self.prevXtrans =" " 
    6464        self.prevYtrans =" " 
     65    def returnTrans(self): 
     66        return self.xscales,self.yscales 
    6567         
    6668    def setTrans(self,xtrans,ytrans):  
     
    8385            first_item = list.keys()[0] 
    8486            #print first_item, list[first_item].__class__.__name__ 
    85             dlg = LinearFit( None, first_item, self.onFitDisplay, -1, 'Fitting') 
     87            dlg = LinearFit( None, first_item, self.onFitDisplay,self.returnTrans, -1, 'Fitting') 
    8688            dlg.ShowModal()  
    8789 
     
    125127         else: 
    126128             return math.sqrt(x) 
    127           
     129    def toLogX(self,x): 
     130        """ 
     131            This function is used to load value on Plottable.View 
     132            calculate log x 
     133            @param x: float value 
     134        """ 
     135        if not x > 0: 
     136            raise ValueError, "Log(X)of a negative value " 
     137        else: 
     138            return math.log(x) 
     139         
     140    def toOneOverX(self,x): 
     141        if x !=0: 
     142            return 1/x 
     143        else: 
     144            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" 
     150    def toLogYX2(self): 
     151        if y*(x**2) >0: 
     152            return math.log(y*(x**2)) 
     153        else: 
     154             raise ValueError,"cannot be computed" 
    128155    def toLogXY(self,x,y): 
    129156        """ 
     
    371398        for item in list: 
    372399            if ( self.xscales=="x" ): 
    373                 item.transform_x(  self.toX, self.errFunctoX ) 
     400                item.transform_x(  self.toX, self.errToX ) 
    374401                self.set_xscale("linear") 
    375                 self.graph.xaxis('\\rm{q} ', 'A^{-1}') 
     402                name, units = item.get_xaxis() 
     403                self.graph.xaxis("%s" % name,  "%s^{-1}" % units) 
    376404                 
    377405            if ( self.xscales=="x^(2)" ): 
    378                 item.transform_x(  self.toX2, self.errFuncToX2 ) 
     406                item.transform_x(  self.toX2, self.errToX2 ) 
    379407                self.set_xscale('linear') 
    380                 self.graph.xaxis('\\rm{q^{2}} ', 'A^{-2}') 
     408                name, units = item.get_xaxis() 
     409                self.graph.xaxis("%s^{2}" % name,  "%s^{-2}" % units) 
    381410                 
    382411            if (self.xscales=="Log(x)" ): 
    383                 item.transform_x(  self.toX, self.errFuncToLogX ) 
     412                item.transform_x(  self.toX, self.errToLogX ) 
    384413                self.set_xscale("log") 
    385                 self.graph.xaxis('\\rm{q} ', 'A^{-1}') 
     414                name, units = item.get_xaxis() 
     415                self.graph.xaxis("%s" % name,  "%s^{-1}" % units) 
    386416                 
    387417            if ( self.yscales=="y" ): 
    388                 item.transform_y(  self.toX, self.errFunctoX ) 
     418                item.transform_y(  self.toX, self.errToX ) 
    389419                self.set_yscale("linear") 
    390                 self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") 
     420                name, units = item.get_yaxis() 
     421                self.graph.yaxis("%s" % name,  "%s^{-1}" % units) 
    391422                 
    392423            if ( self.yscales=="Log(y)" ):  
    393                 item.transform_y(  self.toX, self.errFuncToLogX) 
     424                item.transform_y(  self.toX, self.errToLogX) 
    394425                self.set_yscale("log")   
    395                 self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") 
     426                name, units = item.get_yaxis() 
     427                self.graph.yaxis("%s" % name,  "%s^{-1}" % units) 
    396428                 
    397429            if ( self.yscales=="y^(2)" ): 
    398                 item.transform_y(  self.toX2, self.errFuncToX2 )     
     430                item.transform_y(  self.toX2, self.errToX2 )     
    399431                self.set_yscale("linear") 
    400                 self.graph.yaxis("\\rm{Intensity^{2}} ","cm^{-2}") 
     432                name, units = item.get_yaxis() 
     433                self.graph.yaxis("%s^2" % name,  "%s^{-2}" % units) 
    401434    
    402435        self.prevXtrans = self.xscales  
     
    405438        self.graph.render(self) 
    406439        self.subplot.figure.canvas.draw_idle() 
    407     def errFunctoX(self,x,dx=None): 
     440         
     441    def errToX(self,x,dx=None): 
    408442        """ 
    409443            calculate error of x**2 
     
    411445            @param dx: float value 
    412446        """ 
    413         if math.fabs(dx) >= math.fabs(x): 
     447        if (dx != None) and (math.fabs(dx) >= math.fabs(x)): 
    414448            return math.fabs(0.9*x) 
    415449        if dx==None: 
    416450             return 0 
    417451        return math.fabs(dx) 
    418     def errFuncToX2(self,x,dx=None): 
     452     
     453    def errToX2(self,x,dx=None): 
    419454        """ 
    420455            calculate error of x**2 
     
    429464        else: 
    430465            return 0.0 
    431     def errFuncfromX2(self,x,dx=None): 
     466    def errFromX2(self,x,dx=None): 
    432467        """ 
    433468            calculate error of sqrt(x) 
     
    447482            return math.fabs(err) 
    448483         
    449     def errFuncToLogX(self,x,dx=None): 
     484    def errToLogX(self,x,dx=None): 
    450485        """ 
    451486            calculate error of Log(xy) 
     
    466501        return math.fabs(err) 
    467502         
    468     def errFuncfromLogXY(self,x,dx=None): 
     503    def errToLogXY(self,x,y,dx=None, dy=None): 
    469504        """ 
    470505            calculate error of Log(xy) 
    471             @param x: float value 
    472             @param dx: float value 
    473         """ 
    474         return  
    475         
    476                     
     506        """ 
     507        if dx==None: 
     508            err = x*(dy**2)/y 
     509        elif dy==None: 
     510            err = y*(dx**2)/x 
     511        elif (x!=0) and (y!=0): 
     512            err = y*(dx**2)/x + x*(dy**2)/y 
     513        if err >= 0: 
     514            if  math.sqrt(err)> x: 
     515                err= 0.9*x 
     516            return math.sqrt(err) 
     517        else: 
     518            raise ValueError, "cannot compute this error" 
     519                       
    477520    def onFitDisplay(self, plottable): 
    478521        """ 
     
    484527        self.graph.add(plottable) 
    485528        self.graph.render(self) 
     529        self.graph.delete(plottable) 
    486530        self.subplot.figure.canvas.draw_idle() 
    487531         
  • guitools/fitDialog.py

    r5789654 rf52bea1  
    99class LinearFit(wx.Dialog): 
    1010    #def __init__(self, parent, id, title): 
    11     def __init__(self, parent, plottable, push_data, id, title): 
     11    def __init__(self, parent, plottable, push_data,transform, id, title): 
    1212        wx.Dialog.__init__(self, parent, id, title, size=(550, 300)) 
    1313        """ 
     
    1515        """ 
    1616        self.parent = parent 
     17        self.transform = transform 
    1718        #dialog panel self call function to plot the fitting function 
    1819        self.push_data = push_data 
    1920        #dialog self plottable 
    2021        self.plottable = plottable 
     22         
    2123         
    2224        #Dialog interface 
     
    7577        sizer.Add(self.tcXmax, (iy, ix)) 
    7678        iy += 1 
    77         ix = 1 
     79        ix = 3 
    7880        sizer.Add(self.btFit, (iy, ix)) 
    7981        self.btFit.Bind(wx.EVT_BUTTON, self._onFit) 
    80         ix += 2 
     82        iy +=1 
     83        ix = 3 
    8184        sizer.Add(btClose, (iy, ix)) 
    8285        
     
    9295         
    9396    def _onFit(self ,event): 
    94         
    95         print "we are on fit" 
     97        """ 
     98            Performs the fit. Receive an event when clicking on the button Fit.Computes chisqr , 
     99            A and B parameters of the best linear fit y=Ax +B 
     100            Push a plottable to  
     101        """ 
     102        
    96103        temp =[] 
     104        tempx=[] 
     105        tempy=[] 
    97106        xmin = self._checkVal(self.tcXmin.GetValue()) 
    98107        xmax = self._checkVal(self.tcXmax.GetValue()) 
     
    100109        #store the values of View in x,y, dx,dy 
    101110        x,y,dx,dy=self.plottable.returnValuesOfView() 
    102         
     111        self.xtrans,self.ytrans= self.transform() 
     112        #Display the fittings values 
     113        default_A = self.model.getParam('A')  
     114        default_B = self.model.getParam('B')  
     115        cstA = fittings.Parameter(self.model, 'A', default_A) 
     116        cstB  = fittings.Parameter(self.model, 'B', default_B) 
     117        if  self.ytrans == "Log(y)": 
     118            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)) 
     122        else : 
     123            chisqr, out, cov = fittings.sansfit(self.model,  
     124                            [cstA, cstB],x, y,dy,min(x),max(x)) 
     125        #Check that cov and out are iterable before displaying them 
     126        if cov ==None: 
     127            errA =0.0 
     128            errB =0.0 
     129        else: 
     130            errA= math.sqrt(cov[0][0]) 
     131            errB= math.sqrt(cov[1][1]) 
     132        if out==None: 
     133            cstA=0.0 
     134            cstB=0.0 
     135        else: 
     136            cstA=out[0] 
     137            cstB=out[1] 
     138        self.model.setParam('A', float(cstA)) 
     139        self.model.setParam('B', float(cstB)) 
     140         
    103141        if x != []: 
    104142            if xmin !=None  and xmax != None: 
    105143                for j in range(len(x)): 
    106                     if x[j]>xmin and x[j]<xmax: 
     144                    if x[j] > xmin and x[j] < xmax: 
    107145                        temp.append(self.model.run(x[j])) 
    108                      
    109                         for y_i in temp: 
    110                             tempdy.append(math.sqrt(y_i))  
    111146            else: 
    112147                # x has a default value in case the user doesn't load data 
    113148                for x_i in x: 
    114149                    temp.append(self.model.run(x_i)) 
    115          
    116                     self.tcXmin.SetValue(str(min(x))) 
    117                     self.tcXmax.SetValue(str(max(x))) 
    118                     xmin = self._checkVal(self.tcXmin.GetValue()) 
    119                     xmax = self._checkVal(self.tcXmax.GetValue()) 
     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()) 
    120154                 
    121155            self.file_data1.x =x 
    122             self.file_data1.y =temp 
    123            
     156            # Create new data plottable with result 
     157            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      
    124165            self.file_data1.dx=None 
    125166            self.file_data1.dy=None 
     167             
    126168            self.file_data1.reset_view() 
    127          
    128             # Display the fittings values 
    129             default_A = self.model.getParam('A')  
    130             default_B = self.model.getParam('B')  
    131             cstA = fittings.Parameter(self.model, 'A', default_A) 
    132             cstB  = fittings.Parameter(self.model, 'B', default_B)         
    133             chisqr, out, cov = fittings.sansfit(self.model,  
    134                [cstA, cstB],x, y,dy,min(x),max(x)) 
    135169             
    136             
    137             #Check that cov and out are iterable before displaying them 
    138             if cov ==None: 
    139                 errA =str(0.0) 
    140                 errB =str(0.0) 
    141             else: 
    142                 errA= str(math.sqrt(cov[0][0])) 
    143                 errB= str(math.sqrt(cov[1][1])) 
    144             if out==None: 
    145                 cstA=str(0.0) 
    146                 cstB=str(0.0) 
    147             else: 
    148                 cstA=str(out[0]) 
    149                 cstB=str(out[1]) 
    150             
    151             self.model.setParam('A', float(cstA)) 
    152             self.model.setParam('B', float(cstB)) 
    153              # Create new data plottable with result 
    154             self.file_data1.y = [] 
    155             
    156             for x_i in x: 
    157                 self.file_data1.y.append(self.model.run(x_i)) 
    158170            #Send the data to display to the PlotPanel 
    159             self.file_data1.reset_view() 
     171            # 
    160172            self.push_data(self.file_data1) 
    161173            # Display the fitting value on the Fit Dialog 
    162             self._onsetValues(cstA, cstB, errA,errB,str(chisqr)) 
     174            self._onsetValues(cstA, cstB, errA,errB,chisqr) 
    163175        
    164176    def _onsetValues(self,cstA,cstB,errA,errB,Chi): 
    165          
    166          self.tcA.SetValue(cstA) 
    167          self.tcB.SetValue(cstB) 
    168          self.tcErrA.SetValue(errA) 
    169          self.tcErrB.SetValue(errB) 
    170          self.tcChi.SetValue(Chi) 
     177         """ 
     178              Display  the value on fit Dialog  
     179         """ 
     180         self.tcA.SetValue(str(cstA)) 
     181         self.tcB.SetValue(str(cstB)) 
     182         self.tcErrA.SetValue(str(errA)) 
     183         self.tcErrB.SetValue(str(errB)) 
     184         self.tcChi.SetValue(str(Chi)) 
    171185          
    172186    def _returnPlottable(self): 
     
    175189    def _checkVal(self,value): 
    176190        """ 
    177                 Ensure that fields parameter contains a value  
    178                 before sending to fit in Plotter1D 
     191                Ensure that field parameter contains a value  
     192                before sending to fit  
    179193        """ 
    180194        try: 
  • guitools/plottables.py

    r5789654 rf52bea1  
    392392         
    393393    def returnValuesOfView(self): 
     394         
    394395        return self.view.returnXview() 
    395396         
  • guitools/requirements.txt

    r8742751 rf52bea1  
    3636         y = A * log(x) + B, which is a straight line in the y vs log(x) representation. 
    3737          
     38--> [ALINA] woring better...I am concerned how to fit a straight line when x is changing too? 
     39          
    3840       
    3941- NOTES that might help for bugs 
     
    4951   
    5052  2- There are additional y scales we need: y, y^2, log(y), 1/y, 1/sqrt(y), log(y * x), log(y * x^2) 
    51   3- You should change the label according to what the user chose, and not hard-code them. 
     53         
     54  3-[DONE] You should change the label according to what the user chose, and not hard-code them. 
    5255     For example, in PlotPanel._onEVT_FUNC_PROPERTY, replace 
    5356           
     
    6063     That way it still works even if the user is not plotting Intensity versus Q. 
    6164      
    62   4- In the fit dialog, put the "fit" and "Close" button in the bottom right corner, like in a standard file dialog. 
     65  4- [DONE] In the fit dialog, put the "fit" and "Close" button in the bottom right corner, like in a standard file dialog. 
     66   
    6367  5- The layout of the Fit Dialog should be improved. 
     68   
    6469  6- The code should be well documented and clean-up (no print statements). 
    6570   
Note: See TracChangeset for help on using the changeset viewer.