1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # fitDialog.py |
---|
4 | |
---|
5 | import wx |
---|
6 | from PlotPanel import PlotPanel |
---|
7 | from plottables import Theory1D |
---|
8 | import math,pylab,fittings |
---|
9 | class 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 | #Dialog interface |
---|
24 | panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER) |
---|
25 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
26 | sizer = wx.GridBagSizer(5,0) |
---|
27 | vbox.Add(panel, 1, wx.EXPAND | wx.ALL) |
---|
28 | |
---|
29 | self.tcA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
30 | self.tcErrA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
31 | self.tcB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
32 | self.tcErrB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
33 | self.tcChi = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
34 | self.tcXmin = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
35 | self.tcXmax = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) |
---|
36 | self.btFit =wx.Button(panel,-1,'Fit' ) |
---|
37 | btClose =wx.Button(panel, wx.ID_CANCEL,'Close' ) |
---|
38 | |
---|
39 | ix = 1 |
---|
40 | iy = 1 |
---|
41 | |
---|
42 | sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix)) |
---|
43 | ix = 1 |
---|
44 | iy += 2 |
---|
45 | |
---|
46 | sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix)) |
---|
47 | ix += 1 |
---|
48 | sizer.Add(self.tcA, (iy, ix)) |
---|
49 | ix += 1 |
---|
50 | sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix)) |
---|
51 | ix += 1 |
---|
52 | sizer.Add(self.tcErrA, (iy, ix)) |
---|
53 | iy += 1 |
---|
54 | ix = 1 |
---|
55 | sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix)) |
---|
56 | ix += 1 |
---|
57 | sizer.Add(self.tcB, (iy, ix)) |
---|
58 | ix += 1 |
---|
59 | sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix)) |
---|
60 | ix += 1 |
---|
61 | sizer.Add(self.tcErrB, (iy, ix)) |
---|
62 | iy += 1 |
---|
63 | ix = 1 |
---|
64 | sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix)) |
---|
65 | ix += 1 |
---|
66 | sizer.Add(self.tcChi, (iy, ix)) |
---|
67 | iy += 1 |
---|
68 | ix = 1 |
---|
69 | sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix)) |
---|
70 | ix += 2 |
---|
71 | sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix)) |
---|
72 | iy += 1 |
---|
73 | ix = 1 |
---|
74 | sizer.Add(self.tcXmin, (iy, ix)) |
---|
75 | ix += 2 |
---|
76 | sizer.Add(self.tcXmax, (iy, ix)) |
---|
77 | iy += 1 |
---|
78 | ix = 3 |
---|
79 | sizer.Add(self.btFit, (iy, ix)) |
---|
80 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit) |
---|
81 | iy +=1 |
---|
82 | ix = 3 |
---|
83 | sizer.Add(btClose, (iy, ix)) |
---|
84 | |
---|
85 | panel.SetSizer(sizer) |
---|
86 | self.SetSizer(vbox) |
---|
87 | self.Centre() |
---|
88 | # Receives the type of model for the fitting |
---|
89 | from LineModel import LineModel |
---|
90 | self.model = LineModel() |
---|
91 | # new data for the fit |
---|
92 | self.file_data1 = Theory1D(x=[], y=[], dy=None) |
---|
93 | self.file_data1.name = "Fit" |
---|
94 | |
---|
95 | def _onFit(self ,event): |
---|
96 | """ |
---|
97 | Performs the fit. Receive an event when clicking on the button Fit.Computes chisqr , |
---|
98 | A and B parameters of the best linear fit y=Ax +B |
---|
99 | Push a plottable to |
---|
100 | """ |
---|
101 | temp=[] |
---|
102 | tempx=[] |
---|
103 | tempy=[] |
---|
104 | tempdy = [] |
---|
105 | |
---|
106 | #Check if the field of Fit Dialog contain values and use the x max and min of the user |
---|
107 | xmin = self._checkVal(self.tcXmin.GetValue()) |
---|
108 | xmax = self._checkVal(self.tcXmax.GetValue()) |
---|
109 | |
---|
110 | #store the values of View in x,y, dx,dy |
---|
111 | x,y,dx,dy=self.plottable.returnValuesOfView() |
---|
112 | # Receive transformations of x and y |
---|
113 | self.xtrans,self.ytrans= self.transform() |
---|
114 | # The x array min and max |
---|
115 | _min = min(x) |
---|
116 | _max = max(x) |
---|
117 | |
---|
118 | |
---|
119 | if (xmin ==None)and (xmax == None): |
---|
120 | #Display the min and the max of x on fit dialog fields |
---|
121 | self.tcXmin.SetValue(str(min(x))) |
---|
122 | self.tcXmax.SetValue(str(max(x))) |
---|
123 | |
---|
124 | |
---|
125 | #Display the fittings values |
---|
126 | default_A = self.model.getParam('A') |
---|
127 | default_B = self.model.getParam('B') |
---|
128 | cstA = fittings.Parameter(self.model, 'A', default_A) |
---|
129 | cstB = fittings.Parameter(self.model, 'B', default_B) |
---|
130 | |
---|
131 | if self.ytrans == "Log(y)": |
---|
132 | for y_i in y: |
---|
133 | temp.append(math.log(y_i)) |
---|
134 | tempdy.append(1.0) |
---|
135 | if (xmin !=None and xmin>= _min) and (xmax != None and xmax<= _max): |
---|
136 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
137 | [cstA, cstB],x, temp,tempdy,xmin,xmax) |
---|
138 | else: |
---|
139 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
140 | [cstA, cstB],x, temp,tempdy,_min,_max) |
---|
141 | else : |
---|
142 | if (xmin !=None and xmin >= _min) and (xmax != None and xmax <= _max): |
---|
143 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
144 | [cstA, cstB],x, y,dy,xmin,xmax) |
---|
145 | else: |
---|
146 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
147 | [cstA, cstB],x, y,dy,_min,_max) |
---|
148 | #Check that cov and out are iterable before displaying them |
---|
149 | if cov ==None: |
---|
150 | errA =0.0 |
---|
151 | errB =0.0 |
---|
152 | else: |
---|
153 | errA= math.sqrt(cov[0][0]) |
---|
154 | errB= math.sqrt(cov[1][1]) |
---|
155 | if out==None: |
---|
156 | cstA=0.0 |
---|
157 | cstB=0.0 |
---|
158 | else: |
---|
159 | cstA=out[0] |
---|
160 | cstB=out[1] |
---|
161 | self.model.setParam('A', float(cstA)) |
---|
162 | self.model.setParam('B', float(cstB)) |
---|
163 | |
---|
164 | #Check if View contains a x array and makes transformation for y as a line to fit |
---|
165 | if x != []: |
---|
166 | for j in range(len(x)): |
---|
167 | if (xmin !=None)and (xmax != None): |
---|
168 | if (x[j] > xmin and x[j] < xmax): |
---|
169 | y_model = self.model.run(x[j]) |
---|
170 | else: |
---|
171 | # x has a default value in case the user doesn't load data |
---|
172 | y_model = self.model.run(x[j]) |
---|
173 | if self.ytrans == "Log(y)": |
---|
174 | tempy.append(math.exp(y_model)) |
---|
175 | else: |
---|
176 | tempy.append(y_model) |
---|
177 | |
---|
178 | # Create new data plottable with result |
---|
179 | self.file_data1.y =[] |
---|
180 | self.file_data1.x =x |
---|
181 | self.file_data1.y =tempy |
---|
182 | self.file_data1.dx=None |
---|
183 | self.file_data1.dy=None |
---|
184 | |
---|
185 | #Load the view with the new values |
---|
186 | self.file_data1.reset_view() |
---|
187 | |
---|
188 | #Send the data to display to the PlotPanel |
---|
189 | self.push_data(self.file_data1) |
---|
190 | |
---|
191 | # Display the fitting value on the Fit Dialog |
---|
192 | self._onsetValues(cstA, cstB, errA,errB,chisqr) |
---|
193 | |
---|
194 | def _onsetValues(self,cstA,cstB,errA,errB,Chi): |
---|
195 | """ |
---|
196 | Display the value on fit Dialog |
---|
197 | """ |
---|
198 | self.tcA.SetValue(str(cstA)) |
---|
199 | self.tcB.SetValue(str(cstB)) |
---|
200 | self.tcErrA.SetValue(str(errA)) |
---|
201 | self.tcErrB.SetValue(str(errB)) |
---|
202 | self.tcChi.SetValue(str(Chi)) |
---|
203 | |
---|
204 | def _returnPlottable(self): |
---|
205 | return self.file_data1 |
---|
206 | |
---|
207 | def _checkVal(self,value): |
---|
208 | """ |
---|
209 | Ensure that field parameter contains a value |
---|
210 | before sending to fit |
---|
211 | """ |
---|
212 | try: |
---|
213 | param = float(value) |
---|
214 | except: |
---|
215 | param = None |
---|
216 | return param |
---|
217 | if __name__ == "__main__": |
---|
218 | app = wx.App() |
---|
219 | dialog=LinearFit(None, -1, 'Fitting') |
---|
220 | dialog.ShowModal() |
---|
221 | app.MainLoop() |
---|
222 | |
---|
223 | |
---|