source: sasview/guitools/fitDialog.py @ f52bea1

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

Plotpanel plottable fitdialog req modified

  • Property mode set to 100644
File size: 7.1 KB
Line 
1#!/usr/bin/python
2
3# fitDialog.py
4
5import wx
6from PlotPanel import PlotPanel
7from plottables import Theory1D
8import math,pylab,fittings
9class LinearFit(wx.Dialog):
10    #def __init__(self, parent, id, title):
11    def __init__(self, parent, plottable, push_data,transform, id, title):
12        wx.Dialog.__init__(self, parent, id, title, size=(550, 300))
13        """
14            for the fit window
15        """
16        self.parent = parent
17        self.transform = transform
18        #dialog panel self call function to plot the fitting function
19        self.push_data = push_data
20        #dialog self plottable
21        self.plottable = plottable
22       
23       
24        #Dialog interface
25        panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)   
26        vbox  = wx.BoxSizer(wx.VERTICAL)
27        sizer = wx.GridBagSizer(5,0)
28        vbox.Add(panel, 1, wx.EXPAND | wx.ALL)
29 
30        self.tcA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
31        self.tcErrA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
32        self.tcB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
33        self.tcErrB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
34        self.tcChi = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
35        self.tcXmin = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER)
36        self.tcXmax = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER)
37        self.btFit =wx.Button(panel,-1,'Fit' )
38        btClose =wx.Button(panel, wx.ID_CANCEL,'Close' )
39       
40        ix = 1
41        iy = 1
42       
43        sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix))
44        ix = 1
45        iy += 2
46       
47        sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix))
48        ix += 1
49        sizer.Add(self.tcA, (iy, ix))
50        ix += 1
51        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
52        ix += 1
53        sizer.Add(self.tcErrA, (iy, ix))
54        iy += 1
55        ix = 1
56        sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix))
57        ix += 1
58        sizer.Add(self.tcB, (iy, ix))
59        ix += 1
60        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
61        ix += 1
62        sizer.Add(self.tcErrB, (iy, ix))
63        iy += 1
64        ix = 1
65        sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix))
66        ix += 1
67        sizer.Add(self.tcChi, (iy, ix))
68        iy += 1
69        ix = 1
70        sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix))
71        ix += 2
72        sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix))
73        iy += 1
74        ix = 1
75        sizer.Add(self.tcXmin, (iy, ix))
76        ix += 2
77        sizer.Add(self.tcXmax, (iy, ix))
78        iy += 1
79        ix = 3
80        sizer.Add(self.btFit, (iy, ix))
81        self.btFit.Bind(wx.EVT_BUTTON, self._onFit)
82        iy +=1
83        ix = 3
84        sizer.Add(btClose, (iy, ix))
85       
86        panel.SetSizer(sizer)
87        self.SetSizer(vbox)
88        self.Centre()
89        # Receives the type of model for the fitting
90        from LineModel import LineModel
91        self.model  = LineModel()
92        # new data for the fit
93        self.file_data1 = Theory1D(x=[], y=[], dy=None)
94        self.file_data1.name = "Fit"
95       
96    def _onFit(self ,event):
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       
103        temp =[]
104        tempx=[]
105        tempy=[]
106        xmin = self._checkVal(self.tcXmin.GetValue())
107        xmax = self._checkVal(self.tcXmax.GetValue())
108       
109        #store the values of View in x,y, dx,dy
110        x,y,dx,dy=self.plottable.returnValuesOfView()
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       
141        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
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     
165            self.file_data1.dx=None
166            self.file_data1.dy=None
167           
168            self.file_data1.reset_view()
169           
170            #Send the data to display to the PlotPanel
171            #
172            self.push_data(self.file_data1)
173            # Display the fitting value on the Fit Dialog
174            self._onsetValues(cstA, cstB, errA,errB,chisqr)
175       
176    def _onsetValues(self,cstA,cstB,errA,errB,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))
185         
186    def _returnPlottable(self):
187        return self.file_data1
188   
189    def _checkVal(self,value):
190        """
191                Ensure that field parameter contains a value
192                before sending to fit
193        """
194        try:
195            param = float(value)
196        except:
197            param = None
198        return param
199if __name__ == "__main__": 
200    app = wx.App()
201    dialog=LinearFit(None, -1, 'Fitting')
202    dialog.ShowModal()
203    app.MainLoop()
204
205
Note: See TracBrowser for help on using the repository browser.