source: sasview/guitools/fitDialog.py @ f63f5ff

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

is working better

  • Property mode set to 100644
File size: 6.6 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, id, title):
12        wx.Dialog.__init__(self, parent, id, title, size=(500, 300))
13        """
14            for the fit window
15        """
16        self.parent = parent
17        #dialog panel self call function to plot the fitting function
18        self.push_data = push_data
19        #dialog self plottable
20        self.plottable = plottable
21       
22        #Dialog interface
23        panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)   
24        vbox  = wx.BoxSizer(wx.VERTICAL)
25        sizer = wx.GridBagSizer(5,0)
26        vbox.Add(panel, 1, wx.EXPAND | wx.ALL)
27 
28        self.tcA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
29        self.tcErrA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
30        self.tcB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
31        self.tcErrB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
32        self.tcChi = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER)
33        self.tcXmin = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
34        self.tcXmax = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
35        self.btFit =wx.Button(panel,-1,'Fit' )
36        btClose =wx.Button(panel, wx.ID_CANCEL,'Close' )
37       
38        ix = 1
39        iy = 1
40        sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix))
41        ix = 1
42        iy += 2
43        sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix))
44        ix += 1
45        sizer.Add(self.tcA, (iy, ix))
46        ix += 1
47        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
48        ix += 1
49        sizer.Add(self.tcErrA, (iy, ix))
50        iy += 1
51        ix = 1
52        sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix))
53        ix += 1
54        sizer.Add(self.tcB, (iy, ix))
55        ix += 1
56        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
57        ix += 1
58        sizer.Add(self.tcErrB, (iy, ix))
59        iy += 1
60        ix = 1
61        sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix))
62        ix += 1
63        sizer.Add(self.tcChi, (iy, ix))
64        iy += 1
65        ix = 1
66        sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix))
67        ix += 2
68        sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix))
69        iy += 1
70        ix = 1
71        sizer.Add(self.tcXmin, (iy, ix))
72        ix += 2
73        sizer.Add(self.tcXmax, (iy, ix))
74        iy += 1
75        ix = 1
76        sizer.Add(self.btFit, (iy, ix))
77        self.btFit.Bind(wx.EVT_BUTTON, self._onFit)
78        ix += 2
79        sizer.Add(btClose, (iy, ix))
80       
81        panel.SetSizer(sizer)
82        self.SetSizer(vbox)
83        self.Centre()
84        # Receives the type of model for the fitting
85        from LineModel import LineModel
86        self.model  = LineModel()
87        # new data for the fit
88        self.file_data1 = Theory1D(x=[], y=[], dy=None)
89        self.file_data1.name = "y= exp(A + bx**2)"
90       
91    def _onFit(self ,event):
92       
93        print "we are on fit"
94        temp =[]
95        tempdx =[]
96        tempdy =[]
97        xmin = self._checkVal(self.tcXmin.GetValue())
98        xmax = self._checkVal(self.tcXmax.GetValue())
99        #x= self.plottable.view.x
100        x=self.plottable.returnXvalueOfView()
101        print "x value :" ,x
102        if x != []:
103            if xmin !=None  and xmax != None:
104                for j in range(len(x)):
105                    if x[j]>xmin and x[j]<xmax:
106                        temp.append(self.model.run(x[j]))
107                        #tempdx.append(math.sqrt(x[j]))
108                        for y_i in temp:
109                            tempdy.append(math.sqrt(y_i)) 
110            else:
111                # x has a default value in case the user doesn't load data
112                for x_i in x:
113                    temp.append(self.model.run(x_i))
114                    tempdx.append(math.sqrt(x_i))
115                for y_i in temp:
116                    tempdy.append(math.sqrt(y_i))
117                    self.tcXmin.SetValue(str(min(x)))
118                    self.tcXmax.SetValue(str(max(x)))
119                    xmin = self._checkVal(self.tcXmin.GetValue())
120                    xmax = self._checkVal(self.tcXmax.GetValue())
121               
122            self.file_data1.x =x
123            self.file_data1.y =temp
124            #self.file_data1.dx=tempdx
125            self.file_data1.dx=None
126            #self.file_data1.dy=tempdy
127            self.file_data1.dy=None
128           
129       
130            # Display the fittings values
131            default_A = self.model.getParam('A') 
132            default_B = self.model.getParam('B') 
133            cstA = fittings.Parameter(self.model, 'A', default_A)
134            cstB  = fittings.Parameter(self.model, 'B', default_B)       
135            chisqr, out, cov = fittings.sansfit(self.model, 
136                [cstA, cstB], self.plottable.view.x, 
137                self.plottable.view.y, self.plottable.view.dy,xmin,xmax)
138            # Create new data plottable with result
139           
140            self.file_data1.y = []
141            #for x_i in self.file_data1.x:
142            for x_i in self.file_data1.x:
143                self.file_data1.y.append(self.model.run(x_i))
144               
145            self.push_data(self.file_data1)
146            if cov ==None:
147                errA =str(0.0)
148                errB =str(0.0)
149            else:
150                errA= str(math.sqrt(cov[0][0]))
151                errB= str(math.sqrt(cov[1][1]))
152            if out==None:
153                cstA=str(0.0)
154                cstB=str(0.0)
155            else:
156                cstA=str(out[0])
157                cstB=str(out[1])
158            self._onsetValues(cstA, cstB, errA,errB,str(chisqr))
159       
160    def _onsetValues(self,cstA,cstB,errA,errB,Chi):
161       
162         self.tcA.SetValue(cstA)
163         self.tcB.SetValue(cstB)
164         self.tcErrA.SetValue(errA)
165         self.tcErrB.SetValue(errB)
166         self.tcChi.SetValue(Chi)
167         
168    def _returnPlottable(self):
169        return self.file_data1
170   
171    def _checkVal(self,value):
172        """
173                Ensure that fields parameter contains a value
174                before sending to fit in Plotter1D
175        """
176        try:
177            param = float(value)
178        except:
179            param = None
180        return param
181if __name__ == "__main__": 
182    app = wx.App()
183    dialog=LinearFit(None, -1, 'Fitting')
184    dialog.ShowModal()
185    app.MainLoop()
186
187
Note: See TracBrowser for help on using the repository browser.