source: sasview/guitools/fitDialog.py @ fcaada5

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

somechange on the size of fitdialog

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