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, 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 = "Fit" |
---|
92 | |
---|
93 | def _onFit(self ,event): |
---|
94 | |
---|
95 | print "we are on fit" |
---|
96 | temp =[] |
---|
97 | xmin = self._checkVal(self.tcXmin.GetValue()) |
---|
98 | xmax = self._checkVal(self.tcXmax.GetValue()) |
---|
99 | |
---|
100 | #store the values of View in x,y, dx,dy |
---|
101 | x,y,dx,dy=self.plottable.returnValuesOfView() |
---|
102 | |
---|
103 | if x != []: |
---|
104 | if xmin !=None and xmax != None: |
---|
105 | for j in range(len(x)): |
---|
106 | if x[j]>xmin and x[j]<xmax: |
---|
107 | temp.append(self.model.run(x[j])) |
---|
108 | |
---|
109 | for y_i in temp: |
---|
110 | tempdy.append(math.sqrt(y_i)) |
---|
111 | else: |
---|
112 | # x has a default value in case the user doesn't load data |
---|
113 | for x_i in x: |
---|
114 | temp.append(self.model.run(x_i)) |
---|
115 | |
---|
116 | self.tcXmin.SetValue(str(min(x))) |
---|
117 | self.tcXmax.SetValue(str(max(x))) |
---|
118 | xmin = self._checkVal(self.tcXmin.GetValue()) |
---|
119 | xmax = self._checkVal(self.tcXmax.GetValue()) |
---|
120 | |
---|
121 | self.file_data1.x =x |
---|
122 | self.file_data1.y =temp |
---|
123 | |
---|
124 | self.file_data1.dx=None |
---|
125 | self.file_data1.dy=None |
---|
126 | self.file_data1.reset_view() |
---|
127 | |
---|
128 | # Display the fittings values |
---|
129 | default_A = self.model.getParam('A') |
---|
130 | default_B = self.model.getParam('B') |
---|
131 | cstA = fittings.Parameter(self.model, 'A', default_A) |
---|
132 | cstB = fittings.Parameter(self.model, 'B', default_B) |
---|
133 | chisqr, out, cov = fittings.sansfit(self.model, |
---|
134 | [cstA, cstB],x, y,dy,min(x),max(x)) |
---|
135 | |
---|
136 | |
---|
137 | #Check that cov and out are iterable before displaying them |
---|
138 | if cov ==None: |
---|
139 | errA =str(0.0) |
---|
140 | errB =str(0.0) |
---|
141 | else: |
---|
142 | errA= str(math.sqrt(cov[0][0])) |
---|
143 | errB= str(math.sqrt(cov[1][1])) |
---|
144 | if out==None: |
---|
145 | cstA=str(0.0) |
---|
146 | cstB=str(0.0) |
---|
147 | else: |
---|
148 | cstA=str(out[0]) |
---|
149 | cstB=str(out[1]) |
---|
150 | |
---|
151 | self.model.setParam('A', float(cstA)) |
---|
152 | self.model.setParam('B', float(cstB)) |
---|
153 | # Create new data plottable with result |
---|
154 | self.file_data1.y = [] |
---|
155 | |
---|
156 | for x_i in x: |
---|
157 | self.file_data1.y.append(self.model.run(x_i)) |
---|
158 | #Send the data to display to the PlotPanel |
---|
159 | self.file_data1.reset_view() |
---|
160 | self.push_data(self.file_data1) |
---|
161 | # Display the fitting value on the Fit Dialog |
---|
162 | self._onsetValues(cstA, cstB, errA,errB,str(chisqr)) |
---|
163 | |
---|
164 | def _onsetValues(self,cstA,cstB,errA,errB,Chi): |
---|
165 | |
---|
166 | self.tcA.SetValue(cstA) |
---|
167 | self.tcB.SetValue(cstB) |
---|
168 | self.tcErrA.SetValue(errA) |
---|
169 | self.tcErrB.SetValue(errB) |
---|
170 | self.tcChi.SetValue(Chi) |
---|
171 | |
---|
172 | def _returnPlottable(self): |
---|
173 | return self.file_data1 |
---|
174 | |
---|
175 | def _checkVal(self,value): |
---|
176 | """ |
---|
177 | Ensure that fields parameter contains a value |
---|
178 | before sending to fit in Plotter1D |
---|
179 | """ |
---|
180 | try: |
---|
181 | param = float(value) |
---|
182 | except: |
---|
183 | param = None |
---|
184 | return param |
---|
185 | if __name__ == "__main__": |
---|
186 | app = wx.App() |
---|
187 | dialog=LinearFit(None, -1, 'Fitting') |
---|
188 | dialog.ShowModal() |
---|
189 | app.MainLoop() |
---|
190 | |
---|
191 | |
---|