- Timestamp:
- Apr 8, 2008 5:04:51 PM (17 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:
- 3d3a0e5
- Parents:
- 8742751
- Location:
- guitools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
guitools/PlotPanel.py
rd490a02 rf52bea1 58 58 self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] 59 59 #User scale 60 self.xscales =" "61 self.yscales =" "60 self.xscales ="x" 61 self.yscales ="Log(y)" 62 62 # keep track if the previous transformation of x and y in Property dialog 63 63 self.prevXtrans =" " 64 64 self.prevYtrans =" " 65 def returnTrans(self): 66 return self.xscales,self.yscales 65 67 66 68 def setTrans(self,xtrans,ytrans): … … 83 85 first_item = list.keys()[0] 84 86 #print first_item, list[first_item].__class__.__name__ 85 dlg = LinearFit( None, first_item, self.onFitDisplay, -1, 'Fitting')87 dlg = LinearFit( None, first_item, self.onFitDisplay,self.returnTrans, -1, 'Fitting') 86 88 dlg.ShowModal() 87 89 … … 125 127 else: 126 128 return math.sqrt(x) 127 129 def toLogX(self,x): 130 """ 131 This function is used to load value on Plottable.View 132 calculate log x 133 @param x: float value 134 """ 135 if not x > 0: 136 raise ValueError, "Log(X)of a negative value " 137 else: 138 return math.log(x) 139 140 def toOneOverX(self,x): 141 if x !=0: 142 return 1/x 143 else: 144 raise ValueError,"cannot divide by zero" 145 def toOneOverSqrtX(self,x): 146 if x > 0: 147 return 1/math.sqrt(x) 148 else: 149 raise ValueError,"cannot be computed" 150 def toLogYX2(self): 151 if y*(x**2) >0: 152 return math.log(y*(x**2)) 153 else: 154 raise ValueError,"cannot be computed" 128 155 def toLogXY(self,x,y): 129 156 """ … … 371 398 for item in list: 372 399 if ( self.xscales=="x" ): 373 item.transform_x( self.toX, self.err FunctoX )400 item.transform_x( self.toX, self.errToX ) 374 401 self.set_xscale("linear") 375 self.graph.xaxis('\\rm{q} ', 'A^{-1}') 402 name, units = item.get_xaxis() 403 self.graph.xaxis("%s" % name, "%s^{-1}" % units) 376 404 377 405 if ( self.xscales=="x^(2)" ): 378 item.transform_x( self.toX2, self.err FuncToX2 )406 item.transform_x( self.toX2, self.errToX2 ) 379 407 self.set_xscale('linear') 380 self.graph.xaxis('\\rm{q^{2}} ', 'A^{-2}') 408 name, units = item.get_xaxis() 409 self.graph.xaxis("%s^{2}" % name, "%s^{-2}" % units) 381 410 382 411 if (self.xscales=="Log(x)" ): 383 item.transform_x( self.toX, self.err FuncToLogX )412 item.transform_x( self.toX, self.errToLogX ) 384 413 self.set_xscale("log") 385 self.graph.xaxis('\\rm{q} ', 'A^{-1}') 414 name, units = item.get_xaxis() 415 self.graph.xaxis("%s" % name, "%s^{-1}" % units) 386 416 387 417 if ( self.yscales=="y" ): 388 item.transform_y( self.toX, self.err FunctoX )418 item.transform_y( self.toX, self.errToX ) 389 419 self.set_yscale("linear") 390 self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") 420 name, units = item.get_yaxis() 421 self.graph.yaxis("%s" % name, "%s^{-1}" % units) 391 422 392 423 if ( self.yscales=="Log(y)" ): 393 item.transform_y( self.toX, self.err FuncToLogX)424 item.transform_y( self.toX, self.errToLogX) 394 425 self.set_yscale("log") 395 self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") 426 name, units = item.get_yaxis() 427 self.graph.yaxis("%s" % name, "%s^{-1}" % units) 396 428 397 429 if ( self.yscales=="y^(2)" ): 398 item.transform_y( self.toX2, self.err FuncToX2 )430 item.transform_y( self.toX2, self.errToX2 ) 399 431 self.set_yscale("linear") 400 self.graph.yaxis("\\rm{Intensity^{2}} ","cm^{-2}") 432 name, units = item.get_yaxis() 433 self.graph.yaxis("%s^2" % name, "%s^{-2}" % units) 401 434 402 435 self.prevXtrans = self.xscales … … 405 438 self.graph.render(self) 406 439 self.subplot.figure.canvas.draw_idle() 407 def errFunctoX(self,x,dx=None): 440 441 def errToX(self,x,dx=None): 408 442 """ 409 443 calculate error of x**2 … … 411 445 @param dx: float value 412 446 """ 413 if math.fabs(dx) >= math.fabs(x):447 if (dx != None) and (math.fabs(dx) >= math.fabs(x)): 414 448 return math.fabs(0.9*x) 415 449 if dx==None: 416 450 return 0 417 451 return math.fabs(dx) 418 def errFuncToX2(self,x,dx=None): 452 453 def errToX2(self,x,dx=None): 419 454 """ 420 455 calculate error of x**2 … … 429 464 else: 430 465 return 0.0 431 def errF uncfromX2(self,x,dx=None):466 def errFromX2(self,x,dx=None): 432 467 """ 433 468 calculate error of sqrt(x) … … 447 482 return math.fabs(err) 448 483 449 def err FuncToLogX(self,x,dx=None):484 def errToLogX(self,x,dx=None): 450 485 """ 451 486 calculate error of Log(xy) … … 466 501 return math.fabs(err) 467 502 468 def err FuncfromLogXY(self,x,dx=None):503 def errToLogXY(self,x,y,dx=None, dy=None): 469 504 """ 470 505 calculate error of Log(xy) 471 @param x: float value 472 @param dx: float value 473 """ 474 return 475 476 506 """ 507 if dx==None: 508 err = x*(dy**2)/y 509 elif dy==None: 510 err = y*(dx**2)/x 511 elif (x!=0) and (y!=0): 512 err = y*(dx**2)/x + x*(dy**2)/y 513 if err >= 0: 514 if math.sqrt(err)> x: 515 err= 0.9*x 516 return math.sqrt(err) 517 else: 518 raise ValueError, "cannot compute this error" 519 477 520 def onFitDisplay(self, plottable): 478 521 """ … … 484 527 self.graph.add(plottable) 485 528 self.graph.render(self) 529 self.graph.delete(plottable) 486 530 self.subplot.figure.canvas.draw_idle() 487 531 -
guitools/fitDialog.py
r5789654 rf52bea1 9 9 class LinearFit(wx.Dialog): 10 10 #def __init__(self, parent, id, title): 11 def __init__(self, parent, plottable, push_data, id, title):11 def __init__(self, parent, plottable, push_data,transform, id, title): 12 12 wx.Dialog.__init__(self, parent, id, title, size=(550, 300)) 13 13 """ … … 15 15 """ 16 16 self.parent = parent 17 self.transform = transform 17 18 #dialog panel self call function to plot the fitting function 18 19 self.push_data = push_data 19 20 #dialog self plottable 20 21 self.plottable = plottable 22 21 23 22 24 #Dialog interface … … 75 77 sizer.Add(self.tcXmax, (iy, ix)) 76 78 iy += 1 77 ix = 179 ix = 3 78 80 sizer.Add(self.btFit, (iy, ix)) 79 81 self.btFit.Bind(wx.EVT_BUTTON, self._onFit) 80 ix += 2 82 iy +=1 83 ix = 3 81 84 sizer.Add(btClose, (iy, ix)) 82 85 … … 92 95 93 96 def _onFit(self ,event): 94 95 print "we are on fit" 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 """ 102 96 103 temp =[] 104 tempx=[] 105 tempy=[] 97 106 xmin = self._checkVal(self.tcXmin.GetValue()) 98 107 xmax = self._checkVal(self.tcXmax.GetValue()) … … 100 109 #store the values of View in x,y, dx,dy 101 110 x,y,dx,dy=self.plottable.returnValuesOfView() 102 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 103 141 if x != []: 104 142 if xmin !=None and xmax != None: 105 143 for j in range(len(x)): 106 if x[j] >xmin and x[j]<xmax:144 if x[j] > xmin and x[j] < xmax: 107 145 temp.append(self.model.run(x[j])) 108 109 for y_i in temp:110 tempdy.append(math.sqrt(y_i))111 146 else: 112 147 # x has a default value in case the user doesn't load data 113 148 for x_i in x: 114 149 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()) 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()) 120 154 121 155 self.file_data1.x =x 122 self.file_data1.y =temp 123 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 124 165 self.file_data1.dx=None 125 166 self.file_data1.dy=None 167 126 168 self.file_data1.reset_view() 127 128 # Display the fittings values129 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 169 136 137 #Check that cov and out are iterable before displaying them138 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 result154 self.file_data1.y = []155 156 for x_i in x:157 self.file_data1.y.append(self.model.run(x_i))158 170 #Send the data to display to the PlotPanel 159 self.file_data1.reset_view()171 # 160 172 self.push_data(self.file_data1) 161 173 # Display the fitting value on the Fit Dialog 162 self._onsetValues(cstA, cstB, errA,errB, str(chisqr))174 self._onsetValues(cstA, cstB, errA,errB,chisqr) 163 175 164 176 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) 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)) 171 185 172 186 def _returnPlottable(self): … … 175 189 def _checkVal(self,value): 176 190 """ 177 Ensure that field sparameter contains a value178 before sending to fit in Plotter1D191 Ensure that field parameter contains a value 192 before sending to fit 179 193 """ 180 194 try: -
guitools/plottables.py
r5789654 rf52bea1 392 392 393 393 def returnValuesOfView(self): 394 394 395 return self.view.returnXview() 395 396 -
guitools/requirements.txt
r8742751 rf52bea1 36 36 y = A * log(x) + B, which is a straight line in the y vs log(x) representation. 37 37 38 --> [ALINA] woring better...I am concerned how to fit a straight line when x is changing too? 39 38 40 39 41 - NOTES that might help for bugs … … 49 51 50 52 2- There are additional y scales we need: y, y^2, log(y), 1/y, 1/sqrt(y), log(y * x), log(y * x^2) 51 3- You should change the label according to what the user chose, and not hard-code them. 53 54 3-[DONE] You should change the label according to what the user chose, and not hard-code them. 52 55 For example, in PlotPanel._onEVT_FUNC_PROPERTY, replace 53 56 … … 60 63 That way it still works even if the user is not plotting Intensity versus Q. 61 64 62 4- In the fit dialog, put the "fit" and "Close" button in the bottom right corner, like in a standard file dialog. 65 4- [DONE] In the fit dialog, put the "fit" and "Close" button in the bottom right corner, like in a standard file dialog. 66 63 67 5- The layout of the Fit Dialog should be improved. 68 64 69 6- The code should be well documented and clean-up (no print statements). 65 70
Note: See TracChangeset
for help on using the changeset viewer.