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