source: sasview/guitools/fitDialog.py @ b6b9d76

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

fixing load file

  • Property mode set to 100644
File size: 6.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, 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,style=wx.SIMPLE_BORDER)
29        self.tcErrA = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
30        self.tcB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
31        self.tcErrB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
32        self.tcChi = wx.TextCtrl(panel, -1,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.x
100        if x:
101            if xmin !=None  and xmax != None:
102                for j in range(len(x)):
103                    if x[j]>xmin and x[j]<xmax:
104                        temp.append(self.model.run(x[j]))
105                        tempdx.append(math.sqrt(x[j]))
106                        for y_i in temp:
107                            tempdy.append(math.sqrt(y_i)) 
108            else:
109                # x has a default value in case the user doesn't load data
110                for x_i in x:
111                    temp.append(self.model.run(x_i))
112                    tempdx.append(math.sqrt(x_i))
113                for y_i in temp:
114                    tempdy.append(math.sqrt(y_i))
115                    self.tcXmin.SetValue(str(min(self.plottable.x)))
116                    self.tcXmax.SetValue(str(max(self.plottable.x)))
117                    xmin = self._checkVal(self.tcXmin.GetValue())
118                    xmax = self._checkVal(self.tcXmax.GetValue())
119               
120            self.file_data1.x =x
121            self.file_data1.y =temp
122            self.file_data1.dx=tempdx
123            self.file_data1.dy=tempdy
124           
125       
126            # Display the fittings values
127            default_A = self.model.getParam('A') 
128            default_B = self.model.getParam('B') 
129            cstA = fittings.Parameter(self.model, 'A', default_A)
130            cstB  = fittings.Parameter(self.model, 'B', default_B)       
131            chisqr, out, cov = fittings.sansfit(self.model, 
132                [cstA, cstB], self.plottable.x, 
133                self.plottable.y, self.plottable.dy,xmin,xmax)
134            # Create new data plottable with result
135           
136            self.file_data1.y = []
137            for x_i in self.file_data1.x:
138                self.file_data1.y.append(self.model.run(x_i))
139               
140            self.push_data(self.file_data1)
141           
142            self._onsetValues(str(out[0]),str(out[1]),\
143            str(math.sqrt(cov[0][0])),str(math.sqrt(cov[1][1])),str(chisqr))
144       
145    def _onsetValues(self,cstA,cstB,errA,errB,Chi):
146       
147         self.tcA.SetValue(cstA)
148         self.tcB.SetValue(cstB)
149         self.tcErrA.SetValue(cstB)
150         self.tcErrB.SetValue(cstA)
151         self.tcChi.SetValue(Chi)
152         
153    def _returnPlottable(self):
154        return self.file_data1
155   
156    def _checkVal(self,value):
157        """
158                Ensure that fields parameter contains a value
159                before sending to fit in Plotter1D
160        """
161        try:
162            param = float(value)
163        except:
164            param = None
165        return param
166if __name__ == "__main__": 
167    app = wx.App()
168    dialog=LinearFit(None, -1, 'Fitting')
169    dialog.ShowModal()
170    app.MainLoop()
171
172
Note: See TracBrowser for help on using the repository browser.