- Timestamp:
- Jul 8, 2008 11:40:33 AM (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:
- 792db7d5
- Parents:
- 9a3adab
- Location:
- guitools
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
guitools/PlotPanel.py
ra17ffdf r1c94a9f1 150 150 self.axes = [self.subplot] 151 151 152 ## Fit dialog 153 self._fit_dialog = None 154 152 155 # Interactor 153 156 self.connect = BindArtist(self.subplot.figure) … … 163 166 self.xminView=0.0 164 167 self.xmaxView=0.0 168 self._scale_xlo = None 169 self._scale_xhi = None 170 self._scale_ylo = None 171 self._scale_yhi = None 165 172 self.Avalue=None 166 173 self.Bvalue=None … … 224 231 self.dragHelper(0,0) 225 232 233 def _offset_graph(self): 234 """ 235 Zoom and offset the graph to the last known 236 settings 237 """ 238 239 for ax in self.axes: 240 if self._scale_xhi is not None and self._scale_xlo is not None: 241 ax.set_xlim(self._scale_xlo, self._scale_xhi) 242 if self._scale_yhi is not None and self._scale_ylo is not None: 243 ax.set_ylim(self._scale_ylo, self._scale_yhi) 244 245 226 246 def dragHelper(self,xdelta,ydelta): 227 247 """ dragging occurs here""" … … 238 258 newhi= math.log10(hi)-xdelta 239 259 if self.xscale=='log': 260 self._scale_xlo = math.pow(10,newlo) 261 self._scale_xhi = math.pow(10,newhi) 240 262 ax.set_xlim(math.pow(10,newlo),math.pow(10,newhi)) 241 263 else: 264 self._scale_xlo = newlo 265 self._scale_xhi = newhi 242 266 ax.set_xlim(newlo,newhi) 243 267 #print "new lo %f and new hi %f"%(newlo,newhi) … … 253 277 #print "new lo %f and new hi %f"%(newlo,newhi) 254 278 if self.yscale=='log': 279 self._scale_ylo = math.pow(10,newlo) 280 self._scale_yhi = math.pow(10,newhi) 255 281 ax.set_ylim(math.pow(10,newlo),math.pow(10,newhi)) 256 282 else: 283 self._scale_ylo = newlo 284 self._scale_yhi = newhi 257 285 ax.set_ylim(newlo,newhi) 258 286 self.canvas.draw_idle() … … 268 296 self.xminView=0.0 269 297 self.xmaxView=0.0 298 self._scale_xlo = None 299 self._scale_xhi = None 300 self._scale_ylo = None 301 self._scale_yhi = None 270 302 self.Avalue=None 271 303 self.Bvalue=None … … 287 319 lo,hi = _rescale(lo,hi,step,pt=event.xdata,scale=ax.get_xscale()) 288 320 if not self.xscale=='log' or lo>0: 321 self._scale_xlo = lo 322 self._scale_xhi = hi 289 323 ax.set_xlim((lo,hi)) 290 324 … … 292 326 lo,hi = _rescale(lo,hi,step,pt=event.ydata,scale=ax.get_yscale()) 293 327 if not self.yscale=='log' or lo>0: 328 self._scale_ylo = lo 329 self._scale_yhi = hi 294 330 ax.set_ylim((lo,hi)) 295 331 else: … … 309 345 lo,hi = _rescale(lo,hi,step,bal=xdata,scale=ax.get_xscale()) 310 346 if not self.xscale=='log' or lo>0: 347 self._scale_xlo = lo 348 self._scale_xhi = hi 311 349 ax.set_xlim((lo,hi)) 312 350 if ydata is not None: … … 314 352 lo,hi = _rescale(lo,hi,step,bal=ydata,scale=ax.get_yscale()) 315 353 if not self.yscale=='log' or lo>0: 354 self._scale_ylo = lo 355 self._scale_yhi = hi 316 356 ax.set_ylim((lo,hi)) 317 357 … … 354 394 """ 355 395 when clicking on linear Fit on context menu , display Fitting Dialog 396 @param plot: PlotPanel owning the graph 356 397 """ 357 398 from fitDialog import LinearFit 358 399 359 dlg = LinearFit( None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') 360 361 if (self.xmin !=0.0 )and ( self.xmax !=0.0)\ 362 and(self.xminView !=0.0 )and ( self.xmaxView !=0.0): 363 dlg.setFitRange(self.xminView,self.xmaxView,self.xmin,self.xmax) 364 dlg.ShowModal() 400 if self._fit_dialog is not None: 401 return 402 403 self._fit_dialog = LinearFit( None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') 404 405 # Set the zoom area 406 if self._scale_xhi is not None and self._scale_xlo is not None: 407 self._fit_dialog.set_fit_region(self._scale_xlo, self._scale_xhi) 408 409 # Register the close event 410 self._fit_dialog.register_close(self._linear_fit_close) 411 412 # Show a non-model dialog 413 self._fit_dialog.Show() 414 415 def _linear_fit_close(self): 416 """ 417 A fit dialog was closed 418 """ 419 self._fit_dialog = None 420 365 421 366 422 def _onProperties(self, event): … … 369 425 The user selects a transformation for x or y value and a new plot is displayed 370 426 """ 427 if self._fit_dialog is not None: 428 self._fit_dialog.Destroy() 429 self._fit_dialog = None 430 371 431 list =[] 372 432 list = self.graph.returnPlottable() … … 641 701 return self.symbollist[s%len(self.symbollist)] 642 702 643 def _replot(self ):703 def _replot(self, remove_fit=False): 644 704 """ 645 705 Rescale the plottables according to the latest 646 706 user selection and update the plot 707 708 @param remove_fit: Fit line will be removed if True 647 709 """ 648 710 self.graph.reset_scale() 649 self._onEVT_FUNC_PROPERTY(remove_fit= False)711 self._onEVT_FUNC_PROPERTY(remove_fit=remove_fit) 650 712 651 713 #TODO: Why do we have to have the following line? … … 660 722 and set the scale 661 723 """ 724 # The logic should be in the right order 725 # Delete first, and then get the whole list... 726 if remove_fit: 727 self.graph.delete(self.fit_result) 728 662 729 list =[] 663 730 list = self.graph.returnPlottable() 664 731 665 if remove_fit: 666 self.fit_result.x =[] 667 self.fit_result.y =[] 668 self.fit_result.dx=None 669 self.fit_result.dy=None 670 self.graph.delete(self.fit_result) 732 # Changing the scale might be incompatible with 733 # currently displayed data (for instance, going 734 # from ln to log when all plotted values have 735 # negative natural logs). 736 # Go linear and only change the scale at the end. 737 self.set_xscale("linear") 738 self.set_yscale("linear") 739 _xscale = 'linear' 740 _yscale = 'linear' 741 671 742 672 743 for item in list: 744 print item.name 673 745 item.setLabel(self.xLabel,self.yLabel) 674 746 if ( self.xLabel=="x" ): 675 747 item.transformX(transform.toX,transform.errToX) 676 self.set_xscale("linear")677 748 name, units = item.get_xaxis() 678 749 self.graph.xaxis("%s" % name, "%s" % units) … … 681 752 if ( self.xLabel=="x^(2)" ): 682 753 item.transformX(transform.toX2,transform.errToX2) 683 self.set_xscale('linear')684 754 name, units = item.get_xaxis() 685 755 units=convertUnit(2,units) … … 689 759 if (self.xLabel=="log10(x)" ): 690 760 item.transformX(transform.toX_pos,transform.errToX_pos) 691 self.set_xscale("log")761 _xscale = 'log' 692 762 name, units = item.get_xaxis() 693 763 self.graph.xaxis("\log_{10}\ \ (%s)" % name, "%s" % units) … … 696 766 if ( self.yLabel=="ln(y)" ): 697 767 item.transformY(transform.toLogX,transform.errToLogX) 698 self.set_yscale("linear")699 768 name, units = item.get_yaxis() 700 769 self.graph.yaxis("\log\ \ %s" % name, "%s" % units) … … 703 772 if ( self.yLabel=="y" ): 704 773 item.transformY(transform.toX,transform.errToX) 705 self.set_yscale("linear")706 774 name, units = item.get_yaxis() 707 775 self.graph.yaxis("%s" % name, "%s" % units) … … 710 778 if ( self.yLabel=="log10(y)" ): 711 779 item.transformY(transform.toX_pos,transform.errToX_pos) 712 self.set_yscale("log")780 _yscale = 'log' 713 781 name, units = item.get_yaxis() 714 782 self.graph.yaxis("\log_{10}\ \ (%s)" % name, "%s" % units) … … 717 785 if ( self.yLabel=="y^(2)" ): 718 786 item.transformY( transform.toX2,transform.errToX2 ) 719 self.set_yscale("linear")720 787 name, units = item.get_yaxis() 721 788 units=convertUnit(2,units) … … 725 792 if ( self.yLabel =="1/y"): 726 793 item.transformY(transform.toOneOverX,transform.errOneOverX ) 727 self.set_yscale("linear")728 794 name, units = item.get_yaxis() 729 795 units=convertUnit(-1,units) … … 732 798 if ( self.yLabel =="1/sqrt(y)" ): 733 799 item.transformY(transform.toOneOverSqrtX,transform.errOneOverSqrtX ) 734 self.set_yscale("linear")735 800 name, units = item.get_yaxis() 736 801 units=convertUnit(-0.5,units) … … 739 804 if ( self.yLabel =="ln(y*x)"): 740 805 item.transformY( transform.toLogXY,transform.errToLogXY) 741 self.set_yscale("linear")742 806 yname, yunits = item.get_yaxis() 743 807 xname, xunits = item.get_xaxis() … … 747 811 if ( self.yLabel =="ln(y*x^(2))"): 748 812 item.transformY( transform.toLogYX2,transform.errToLogYX2) 749 self.set_yscale("linear")750 813 yname, yunits = item.get_yaxis() 751 814 xname, xunits = item.get_xaxis() … … 756 819 if ( self.yLabel =="ln(y*x^(4))"): 757 820 item.transformY(transform.toLogYX4,transform.errToLogYX4) 758 self.set_yscale("linear")759 821 yname, yunits = item.get_yaxis() 760 822 xname, xunits = item.get_xaxis() … … 765 827 766 828 item.transformX(transform.toX2,transform.errToX2) 767 self.set_xscale('linear')768 829 name, units = item.get_xaxis() 769 830 units = convertUnit(2,units) … … 772 833 773 834 item.transformY(transform.toLogX,transform.errToLogX ) 774 self.set_yscale("linear")775 835 name, units = item.get_yaxis() 776 836 self.graph.yaxis("\log\ \ %s" % name, "%s" % units) … … 779 839 item.transformView() 780 840 781 782 841 self.resetFitView() 783 842 self.prevXtrans = self.xLabel 784 843 self.prevYtrans = self.yLabel 785 844 self.graph.render(self) 845 846 self.set_xscale(_xscale) 847 self.set_yscale(_yscale) 848 786 849 self.subplot.figure.canvas.draw_idle() 787 850 … … 824 887 self.graph.add(self.fit_result) 825 888 self.graph.render(self) 889 self._offset_graph() 826 890 self.subplot.figure.canvas.draw_idle() 827 891 -
guitools/fitDialog.py
rad8bcd6 r1c94a9f1 27 27 class LinearFit(wx.Dialog): 28 28 def __init__(self, parent, plottable, push_data,transform, id, title): 29 wx.Dialog.__init__(self, parent, id, title, size=(400, 3 80))29 wx.Dialog.__init__(self, parent, id, title, size=(400, 350)) 30 30 31 31 """ … … 35 35 self.parent = parent 36 36 self.transform = transform 37 38 # Registered owner for close event 39 self._registered_close = None 37 40 38 41 #dialog panel self call function to plot the fitting function … … 65 68 self.xmaxFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 66 69 self.xmaxFit.SetToolTipString("Enter the maximum value on the x-axis to be included in the fit.") 67 self.xminTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))68 self.xminTransFit.SetToolTipString("Minimum value on the x-axis for the plotted data.")69 self.xmaxTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))70 self.xmaxTransFit.SetToolTipString("Maximum value on the x-axis for the plotted data.")71 70 self.initXmin = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 72 self.initXmin.SetToolTipString("Minimum value availableon the x-axis for the plotted data.")71 self.initXmin.SetToolTipString("Minimum value on the x-axis for the plotted data.") 73 72 self.initXmax = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20)) 74 self.initXmax.SetToolTipString("Maximum value availableon the x-axis for the plotted data.")73 self.initXmax.SetToolTipString("Maximum value on the x-axis for the plotted data.") 75 74 76 75 # Make the info box not editable 77 76 #_BACKGROUND_COLOR = '#ffdf85' 78 77 _BACKGROUND_COLOR = self.GetBackgroundColour() 79 self.xminTransFit.SetEditable(False)80 self.xminTransFit.SetBackgroundColour(_BACKGROUND_COLOR)81 self.xmaxTransFit.SetEditable(False)82 self.xmaxTransFit.SetBackgroundColour(_BACKGROUND_COLOR)83 78 self.initXmin.SetEditable(False) 84 79 self.initXmin.SetBackgroundColour(_BACKGROUND_COLOR) … … 93 88 self.btFit.SetToolTipString("Perform fit.") 94 89 self.btClose =wx.Button(self, wx.ID_CANCEL,'Close') 90 self.btClose.Bind(wx.EVT_BUTTON, self._on_close) 95 91 96 92 # Intro … … 136 132 iy += 1 137 133 ix = 0 138 sizer.Add(wx.StaticText(self, -1, 'Maximum range '),(iy, ix),(1,1),\134 sizer.Add(wx.StaticText(self, -1, 'Maximum range (linear scale)'),(iy, ix),(1,1),\ 139 135 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 140 136 ix +=1 … … 147 143 ix = 0 148 144 sizer.Add(wx.StaticText(self, -1, 'Fit range of '+self.xLabel),(iy, ix),(1,1),\ 149 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)150 ix += 1151 sizer.Add(self.xminTransFit, (iy, ix),(1,1),\152 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)153 ix += 2154 sizer.Add(self.xmaxTransFit, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)155 156 iy += 1157 ix = 0158 sizer.Add(wx.StaticText(self, -1, 'Fit range of x'),(iy, ix),(1,1),\159 145 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 160 146 ix += 1 … … 216 202 self.tcChi.SetLabel(format_number(self.Chivalue)) 217 203 if self.plottable.x !=[]: 218 self.mini =min(self.plottable.x)219 self.maxi =max(self.plottable.x)220 204 #store the values of View in self.x,self.y,self.dx,self.dy 221 205 self.x,self.y,self.dx,self.dy= self.plottable.returnValuesOfView() 222 206 223 224 self. xminTransFit.SetLabel(format_number(min(self.x)))225 self.xmaxTransFit.SetLabel(format_number(max(self.x)))226 227 self.initXmin.SetValue(format_number( self.mini))228 self.initXmax.SetValue(format_number( self.maxi))207 self.mini =min(self.x) 208 self.maxi =max(self.x) 209 210 211 self.initXmin.SetValue(format_number(min(self.plottable.x))) 212 self.initXmax.SetValue(format_number(max(self.plottable.x))) 229 213 230 214 self.xminFit.SetLabel(format_number(self.mini)) … … 232 216 233 217 218 def register_close(self, owner): 219 """ 220 Method to register the close event to a parent 221 window that needs notification when the dialog 222 is closed 223 @param owner: parent window 224 """ 225 self._registered_close = owner 226 227 def _on_close(self, event): 228 """ 229 Close event. 230 Notify registered owner if available. 231 """ 232 event.Skip() 233 if self._registered_close is not None: 234 self._registered_close() 235 234 236 def _onFit(self ,event): 235 237 """ … … 252 254 if(self.checkFitValues(self.xminFit) == True): 253 255 #Check if the field of Fit Dialog contain values and use the x max and min of the user 254 xmin,xmax = self._checkVal(self.xminFit.GetValue(),self.xmaxFit.GetValue()) 255 256 xminView=self.floatTransform(xmin) 257 xmaxView=self.floatTransform(xmax) 258 if (self.xLabel=="log10(x)"): 259 self.xminTransFit.SetValue(format_number(math.log10(xminView))) 260 self.xmaxTransFit.SetValue(format_number(math.log10(xmaxView))) 261 else: 262 self.xminTransFit.SetValue(format_number(xminView)) 263 self.xmaxTransFit.SetValue(format_number(xmaxView)) 256 xminView,xmaxView = self._checkVal(self.xminFit.GetValue(),self.xmaxFit.GetValue()) 257 xmin = self.floatInvTransform(xminView) 258 xmax = self.floatInvTransform(xmaxView) 259 260 264 261 # Store the transformed values of view x, y,dy in variables before the fit 265 262 if self.yLabel.lower() == "log10(y)": … … 402 399 not in x 403 400 """ 401 #TODO: refactor this with proper object-oriented design 402 # This code stinks. 403 404 404 if ( self.xLabel=="x" ): 405 405 return transform.toX(x) … … 413 413 else: 414 414 raise ValueError,"cannot compute log of a negative number" 415 416 def floatInvTransform(self,x): 417 """ 418 transform a float.It is use to determine the x.View min and x.View max for values 419 not in x 420 """ 421 #TODO: refactor this. This is just a hack to make the 422 # functionality work without rewritting the whole code 423 # with good design (which really should be done...). 424 425 if ( self.xLabel=="x^(2)" ): 426 return math.sqrt(x) 427 428 elif (self.xLabel=="log10(x)" ): 429 return math.pow(10, x) 430 431 return x 415 432 416 433 def checkFitValues(self,item): … … 438 455 self.xminFit.SetValue(format_number(xmin)) 439 456 self.xmaxFit.SetValue(format_number(xmax)) 440 self.xminTransFit.SetValue(format_number(xminTrans)) 441 self.xmaxTransFit.SetValue(format_number(xmaxTrans)) 442 457 458 def set_fit_region(self, xmin, xmax): 459 """ 460 Set the fit region 461 @param xmin: minimum x-value to be included in fit 462 @param xmax: maximum x-value to be included in fit 463 """ 464 # Check values 465 try: 466 float(xmin) 467 float(xmax) 468 except: 469 raise ValueError, "LinearFit.set_fit_region: fit range must be floats" 470 self.xminFit.SetValue(format_number(xmin)) 471 self.xmaxFit.SetValue(format_number(xmax)) 443 472 444 473 class MyApp(wx.App): -
guitools/fittings.py
r5789654 r1c94a9f1 68 68 p = [param() for param in pars] 69 69 out, cov_x, info, mesg, success = optimize.leastsq(f, p, full_output=1, warning=True) 70 print info, mesg, success71 70 # Calculate chi squared 72 71 if len(pars)>1:
Note: See TracChangeset
for help on using the changeset viewer.