Changeset 05da1f89 in sasview for guitools/fitDialog.py
- Timestamp:
- May 29, 2008 12:18:32 PM (16 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- b6972a0f
- Parents:
- e39640f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
guitools/fitDialog.py
r2e07e8f r05da1f89 9 9 import transform 10 10 11 def format_number(value, high=False): 12 """ 13 Return a float in a standardized, human-readable formatted string 14 """ 15 if high: 16 return "%-6.4g" % value 17 else: 18 return "%-5.3g" % value 19 11 20 12 21 class LinearFit(wx.Dialog): 13 22 def __init__(self, parent, plottable, push_data,transform, id, title): 14 wx.Dialog.__init__(self, parent, id, title, size=(450, 400)) 23 wx.Dialog.__init__(self, parent, id, title, size=(400, 380)) 24 15 25 """ 16 26 Dialog window pops- up when select Linear fit on Context menu … … 19 29 self.parent = parent 20 30 self.transform = transform 31 21 32 #dialog panel self call function to plot the fitting function 22 33 self.push_data = push_data 23 34 #dialog self plottable 24 25 35 self.plottable = plottable 36 26 37 # Receive transformations of x and y 27 38 self.xLabel,self.yLabel,self.Avalue,self.Bvalue,\ 28 39 self.ErrAvalue,self.ErrBvalue,self.Chivalue= self.transform() 40 29 41 #Dialog interface 30 panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)31 42 vbox = wx.BoxSizer(wx.VERTICAL) 32 43 sizer = wx.GridBagSizer(5,5) 33 44 34 vbox.Add(panel, 1, wx.EXPAND | wx.ALL)45 _BOX_WIDTH = 100 35 46 36 self.tcA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) 37 self.tcErrA = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) 38 self.tcB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) 39 self.tcErrB = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) 40 self.tcChi = wx.TextCtrl(panel, -1,size=(120,20),style=wx.SIMPLE_BORDER) 41 self.xminFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 42 self.xmaxFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 43 self.xminTransFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 44 self.xmaxTransFit = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 45 self.initXmin = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 46 self.initXmax = wx.TextCtrl(panel,-1,size=(120,20),style=wx.SIMPLE_BORDER) 47 self.btFit =wx.Button(panel,-1,'Fit',size=(120, 30)) 48 self.btClose =wx.Button(panel, wx.ID_CANCEL,'Close',size=(90, 30) ) 49 self.static_line_1 = wx.StaticLine(panel, -1) 47 self.tcA = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 48 self.tcA.SetToolTipString("Fit value for the slope parameter.") 49 self.tcErrA = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 50 self.tcErrA.SetToolTipString("Error on the slope parameter.") 51 self.tcB = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 52 self.tcA.SetToolTipString("Fit value for the constant parameter.") 53 self.tcErrB = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 54 self.tcErrB.SetToolTipString("Error on the constant parameter.") 55 self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 56 self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") 57 self.xminFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 58 self.xminFit.SetToolTipString("Enter the minimum value on the axis to be included in the fit.") 59 self.xmaxFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 60 self.xmaxFit.SetToolTipString("Enter the maximum value on the axis to be included in the fit.") 61 self.xminTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 62 self.xmaxTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 63 self.initXmin = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 64 self.initXmax = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 65 66 # Make the info box not editable 67 #_BACKGROUND_COLOR = '#ffdf85' 68 _BACKGROUND_COLOR = self.GetBackgroundColour() 69 self.xminTransFit.SetEditable(False) 70 self.xminTransFit.SetBackgroundColour(_BACKGROUND_COLOR) 71 self.xmaxTransFit.SetEditable(False) 72 self.xmaxTransFit.SetBackgroundColour(_BACKGROUND_COLOR) 73 self.initXmin.SetEditable(False) 74 self.initXmin.SetBackgroundColour(_BACKGROUND_COLOR) 75 self.initXmax.SetEditable(False) 76 self.initXmax.SetBackgroundColour(_BACKGROUND_COLOR) 77 78 79 # Buttons on the bottom 80 self.static_line_1 = wx.StaticLine(self, -1) 81 self.btFit =wx.Button(self,-1,'Fit') 82 self.btFit.Bind(wx.EVT_BUTTON, self._onFit) 83 self.btFit.SetToolTipString("Perform fit.") 84 self.btClose =wx.Button(self, wx.ID_CANCEL,'Close') 85 86 # Intro 87 explanation = "Perform fit for y(x) = Ax + B" 88 89 vbox.Add(sizer) 50 90 51 91 ix = 0 52 92 iy = 1 53 sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix),(1,1),\ 93 sizer.Add(wx.StaticText(self, -1, explanation),(iy, ix),\ 94 (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 95 iy += 2 96 sizer.Add(wx.StaticText(self, -1, 'Parameter A'),(iy, ix),\ 97 (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 98 ix += 1 99 sizer.Add(self.tcA,(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 100 ix += 1 101 sizer.Add(wx.StaticText(self, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 102 ix += 1 103 sizer.Add(self.tcErrA, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 104 iy += 1 105 ix = 0 106 sizer.Add(wx.StaticText(self, -1, 'Parameter B'),(iy, ix),(1,1),\ 54 107 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 55 iy+=1 56 sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix),\ 57 (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 58 ix += 1 59 sizer.Add(self.tcA,(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 60 ix += 1 61 sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 62 ix += 1 63 sizer.Add(self.tcErrA, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 64 iy += 1 65 ix = 0 66 sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix),(1,1),\ 108 ix += 1 109 sizer.Add(self.tcB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 110 ix += 1 111 sizer.Add(wx.StaticText(self, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 112 ix += 1 113 sizer.Add(self.tcErrB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 114 iy += 1 115 ix = 0 116 sizer.Add(wx.StaticText(self, -1, 'Chi2/dof'),(iy, ix),(1,1),\ 67 117 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 68 118 ix += 1 69 sizer.Add(self.tcB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)70 ix += 171 sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)72 ix += 173 sizer.Add(self.tcErrB, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)74 iy += 175 ix = 076 sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix),(1,1),\77 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)78 ix += 179 119 sizer.Add(self.tcChi, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 80 iy += 1120 iy += 2 81 121 ix = 1 82 sizer.Add(wx.StaticText( panel, -1, 'Xmin'),(iy, ix),(1,1),\122 sizer.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ 83 123 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 84 124 ix += 2 85 sizer.Add(wx.StaticText( panel, -1, 'Xmax'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)86 iy += 1 87 ix = 0 88 sizer.Add(wx.StaticText( panel, -1, 'Plotted Range'),(iy, ix),(1,1),\125 sizer.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 126 iy += 1 127 ix = 0 128 sizer.Add(wx.StaticText(self, -1, 'Plotted Range'),(iy, ix),(1,1),\ 89 129 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 90 130 ix +=1 … … 96 136 iy += 1 97 137 ix = 0 98 sizer.Add(wx.StaticText( panel, -1, 'Fit Range of '+self.xLabel),(iy, ix),(1,1),\138 sizer.Add(wx.StaticText(self, -1, 'Fit Range of '+self.xLabel),(iy, ix),(1,1),\ 99 139 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 100 140 ix += 1 … … 106 146 iy += 1 107 147 ix = 0 108 sizer.Add(wx.StaticText( panel, -1, 'Fit Range of x'),(iy, ix),(1,1),\148 sizer.Add(wx.StaticText(self, -1, 'Fit Range of x'),(iy, ix),(1,1),\ 109 149 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 110 150 ix += 1 … … 116 156 ix = 1 117 157 158 vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) 159 160 sizer_button = wx.BoxSizer(wx.HORIZONTAL) 161 sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 162 sizer_button.Add(self.btFit, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 163 sizer_button.Add(self.btClose, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 164 vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) 165 166 167 118 168 sizer.Add(self.btFit, (iy, ix),(1,1), wx.LEFT|wx.ADJUST_MINSIZE, 0) 119 self.btFit.Bind(wx.EVT_BUTTON, self._onFit) 120 ix += 2 121 sizer.Add(self.btClose, (iy, ix),(1,1),\ 122 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 123 124 panel.SetSizer(sizer) 169 170 171 #panel.SetSizer(sizer) 125 172 self.SetSizer(vbox) 126 173 self.Centre() … … 164 211 self.x,self.y,self.dx,self.dy= self.plottable.returnValuesOfView() 165 212 166 self.xminTransFit.SetLabel(str(min(self.x))) 167 self.xmaxTransFit.SetLabel(str(max(self.x))) 168 self.xminTransFit.Disable() 169 self.xmaxTransFit.Disable() 170 171 self.initXmin.SetValue(str(self.mini)) 172 self.initXmax.SetValue(str(self.maxi)) 173 self.initXmin.Disable() 174 self.initXmax.Disable() 175 176 self.xminFit.SetLabel(str(self.mini)) 177 self.xmaxFit.SetLabel(str(self.maxi)) 213 214 self.xminTransFit.SetLabel(format_number(min(self.x))) 215 self.xmaxTransFit.SetLabel(format_number(max(self.x))) 216 217 self.initXmin.SetValue(format_number(self.mini)) 218 self.initXmax.SetValue(format_number(self.maxi)) 219 220 self.xminFit.SetLabel(format_number(self.mini)) 221 self.xmaxFit.SetLabel(format_number(self.maxi)) 178 222 179 223 … … 203 247 xmaxView=self.floatTransform(xmax) 204 248 if (self.xLabel=="log10(x)"): 205 self.xminTransFit.SetValue(str(math.log10(xminView))) 206 self.xmaxTransFit.SetValue(str(math.log10(xmaxView))) 207 else: 208 self.xminTransFit.SetValue(str(xminView)) 209 self.xmaxTransFit.SetValue(str(xmaxView)) 210 self.xminTransFit.Disable() 211 self.xmaxTransFit.Disable() 249 self.xminTransFit.SetValue(format_number(math.log10(xminView))) 250 self.xmaxTransFit.SetValue(format_number(math.log10(xmaxView))) 251 else: 252 self.xminTransFit.SetValue(format_number(xminView)) 253 self.xmaxTransFit.SetValue(format_number(xmaxView)) 212 254 # Store the transformed values of view x, y,dy in variables before the fit 213 255 if self.yLabel.lower() == "log10(y)": … … 240 282 chisqr, out, cov = fittings.sansfit(self.model, 241 283 [self.cstA, self.cstB],tempx, tempy,tempdy,xminView,xmaxView) 242 #print "this out",out 284 285 # Use chi2/dof 286 if len(tempx)>0: 287 chisqr = chisqr/len(tempx) 288 243 289 #Check that cov and out are iterable before displaying them 244 290 if cov ==None: … … 305 351 Display the value on fit Dialog 306 352 """ 307 self.tcA.SetValue( str(cstA))308 self.tcB.SetValue( str(cstB))309 self.tcErrA.SetValue( str(errA))310 self.tcErrB.SetValue( str(errB))311 self.tcChi.SetValue( str(Chi))353 self.tcA.SetValue(format_number(cstA)) 354 self.tcB.SetValue(format_number(cstB)) 355 self.tcErrA.SetValue(format_number(errA)) 356 self.tcErrB.SetValue(format_number(errB)) 357 self.tcChi.SetValue(format_number(Chi)) 312 358 313 359 def _ongetValues(self): … … 325 371 if float(usermin) < float(usermax): 326 372 if float(usermin) >= float(self.mini) and float(usermin) < float(self.maxi): 327 self.xminFit.SetValue( str(usermin))373 self.xminFit.SetValue(format_number(float(usermin))) 328 374 else: 329 self.xminFit.SetValue( str(self.mini))375 self.xminFit.SetValue(format_number(float(self.mini))) 330 376 331 377 if float(usermax) > float(self.mini) and float(usermax) <= float(self.maxi): 332 self.xmaxFit.SetLabel( str(usermax))378 self.xmaxFit.SetLabel(format_number(float(usermax))) 333 379 else: 334 self.xmaxFit.SetLabel( str(self.maxi))380 self.xmaxFit.SetLabel(format_number(float(self.maxi))) 335 381 336 382 mini =float(self.xminFit.GetValue()) … … 377 423 Set fit parameters 378 424 """ 379 self.xminFit.SetValue(str(xmin)) 380 self.xmaxFit.SetValue(str(xmax)) 381 self.xminTransFit.SetValue(str(xminTrans)) 382 self.xmaxTransFit.SetValue(str(xmaxTrans)) 383 384 385 if __name__ == "__main__": 386 app = wx.App() 387 dialog=LinearFit(None, -1, 'Fitting') 388 dialog.ShowModal() 425 self.xminFit.SetValue(format_number(xmin)) 426 self.xmaxFit.SetValue(format_number(xmax)) 427 self.xminTransFit.SetValue(format_number(xminTrans)) 428 self.xmaxTransFit.SetValue(format_number(xmaxTrans)) 429 430 431 class MyApp(wx.App): 432 def OnInit(self): 433 wx.InitAllImageHandlers() 434 plot = Theory1D([],[]) 435 dialog = LinearFit(None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') 436 if dialog.ShowModal() == wx.ID_OK: 437 pass 438 dialog.Destroy() 439 440 return 1 441 442 def onFitDisplay(self, tempx,tempy,xminView,xmaxView,xmin,xmax,func): 443 pass 444 445 def returnTrans(self): 446 return '','',0,0,0,0,0 447 448 # end of class MyApp 449 450 if __name__ == "__main__": 451 app = MyApp(0) 389 452 app.MainLoop() 390 391
Note: See TracChangeset
for help on using the changeset viewer.