[6cfe703] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | # fitDialog.py |
---|
| 4 | |
---|
| 5 | import wx |
---|
[e40b651] | 6 | from PlotPanel import PlotPanel |
---|
| 7 | from plottables import Theory1D |
---|
| 8 | import math,pylab,fittings |
---|
[6cfe703] | 9 | class LinearFit(wx.Dialog): |
---|
[e40b651] | 10 | #def __init__(self, parent, id, title): |
---|
[f52bea1] | 11 | def __init__(self, parent, plottable, push_data,transform, id, title): |
---|
[1bde57a] | 12 | wx.Dialog.__init__(self, parent, id, title, size=(550, 300)) |
---|
[6cfe703] | 13 | """ |
---|
| 14 | for the fit window |
---|
| 15 | """ |
---|
| 16 | self.parent = parent |
---|
[f52bea1] | 17 | self.transform = transform |
---|
[e40b651] | 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 |
---|
[6cfe703] | 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 | |
---|
[f63f5ff] | 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) |
---|
[d490a02] | 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) |
---|
[6cfe703] | 36 | self.btFit =wx.Button(panel,-1,'Fit' ) |
---|
| 37 | btClose =wx.Button(panel, wx.ID_CANCEL,'Close' ) |
---|
| 38 | |
---|
| 39 | ix = 1 |
---|
| 40 | iy = 1 |
---|
[1bde57a] | 41 | |
---|
[6cfe703] | 42 | sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix)) |
---|
| 43 | ix = 1 |
---|
| 44 | iy += 2 |
---|
[1bde57a] | 45 | |
---|
[6cfe703] | 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 |
---|
[e40b651] | 59 | sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix)) |
---|
[6cfe703] | 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 |
---|
[f52bea1] | 78 | ix = 3 |
---|
[6cfe703] | 79 | sizer.Add(self.btFit, (iy, ix)) |
---|
[e40b651] | 80 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit) |
---|
[f52bea1] | 81 | iy +=1 |
---|
| 82 | ix = 3 |
---|
[6cfe703] | 83 | sizer.Add(btClose, (iy, ix)) |
---|
| 84 | |
---|
| 85 | panel.SetSizer(sizer) |
---|
| 86 | self.SetSizer(vbox) |
---|
| 87 | self.Centre() |
---|
[e40b651] | 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) |
---|
[5789654] | 93 | self.file_data1.name = "Fit" |
---|
[e40b651] | 94 | |
---|
[6cfe703] | 95 | def _onFit(self ,event): |
---|
[f52bea1] | 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 | """ |
---|
[7a03e65] | 101 | temp=[] |
---|
[f52bea1] | 102 | tempx=[] |
---|
| 103 | tempy=[] |
---|
[7a03e65] | 104 | tempdy = [] |
---|
| 105 | |
---|
| 106 | #Check if the field of Fit Dialog contain values and use the x max and min of the user |
---|
[e40b651] | 107 | xmin = self._checkVal(self.tcXmin.GetValue()) |
---|
| 108 | xmax = self._checkVal(self.tcXmax.GetValue()) |
---|
[5789654] | 109 | |
---|
| 110 | #store the values of View in x,y, dx,dy |
---|
| 111 | x,y,dx,dy=self.plottable.returnValuesOfView() |
---|
[7a03e65] | 112 | # Receive transformations of x and y |
---|
[f52bea1] | 113 | self.xtrans,self.ytrans= self.transform() |
---|
[7a03e65] | 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 | |
---|
[f52bea1] | 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) |
---|
[7a03e65] | 130 | |
---|
[f52bea1] | 131 | if self.ytrans == "Log(y)": |
---|
| 132 | for y_i in y: |
---|
[7a03e65] | 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) |
---|
[f52bea1] | 141 | else : |
---|
[7a03e65] | 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) |
---|
[f52bea1] | 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 | |
---|
[7a03e65] | 164 | #Check if View contains a x array and makes transformation for y as a line to fit |
---|
[f63f5ff] | 165 | if x != []: |
---|
[7a03e65] | 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 | |
---|
[f52bea1] | 178 | # Create new data plottable with result |
---|
| 179 | self.file_data1.y =[] |
---|
[7a03e65] | 180 | self.file_data1.x =x |
---|
| 181 | self.file_data1.y =tempy |
---|
[061775ff] | 182 | self.file_data1.dx=None |
---|
| 183 | self.file_data1.dy=None |
---|
[f52bea1] | 184 | |
---|
[7a03e65] | 185 | #Load the view with the new values |
---|
[5789654] | 186 | self.file_data1.reset_view() |
---|
[e40b651] | 187 | |
---|
[5789654] | 188 | #Send the data to display to the PlotPanel |
---|
| 189 | self.push_data(self.file_data1) |
---|
[7a03e65] | 190 | |
---|
[5789654] | 191 | # Display the fitting value on the Fit Dialog |
---|
[f52bea1] | 192 | self._onsetValues(cstA, cstB, errA,errB,chisqr) |
---|
[7a03e65] | 193 | |
---|
[6cfe703] | 194 | def _onsetValues(self,cstA,cstB,errA,errB,Chi): |
---|
[f52bea1] | 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)) |
---|
[e40b651] | 203 | |
---|
| 204 | def _returnPlottable(self): |
---|
| 205 | return self.file_data1 |
---|
| 206 | |
---|
[6cfe703] | 207 | def _checkVal(self,value): |
---|
| 208 | """ |
---|
[f52bea1] | 209 | Ensure that field parameter contains a value |
---|
| 210 | before sending to fit |
---|
[6cfe703] | 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 | |
---|