Changeset 2b63df0 in sasview
- Timestamp:
- Apr 23, 2009 10:21:05 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:
- e473e4f5
- Parents:
- 3b69ca6
- Location:
- sansview/perspectives/fitting
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitpage.py
r6fdfc8f r2b63df0 39 39 ## fit page does not content npts txtcrtl 40 40 self.npts=None 41 ## if no dispersity parameters is avaible42 #self.text_disp_1=None41 ## thread for compute Chisqr 42 self.calc_Chisqr=None 43 43 ## default fitengine type 44 44 self.engine_type = None … … 53 53 self.enable_smearer.Disable() 54 54 self.disable_smearer.Disable() 55 55 #try: 56 ##Calculate chi2 57 # self.compute_chisqr(smearer= temp_smearer) 58 #except: 59 # raise 56 60 ## to update the panel according to the fit engine type selected 57 61 self.Bind(EVT_FITTER_TYPE,self._on_engine_change) … … 145 149 boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) 146 150 boxsizer1.SetMinSize((60,-1)) 147 self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) 151 temp_smearer = None 152 if self.enable_smearer.GetValue(): 153 temp_smearer= self.smearer 154 155 self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) 156 148 157 boxsizer1.Add( self.tcChi ) 149 158 sizer_smearer.Add( boxsizer1 ) … … 685 694 ## set smearing value whether or not the data contain the smearing info 686 695 self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), 687 696 qmax= float(self.qmax_x)) 688 697 ##Calculate chi2 689 698 self.compute_chisqr(smearer= temp_smearer) 690 699 ## save the state enable smearing 691 self.save_current_state() 692 693 700 self.save_current_state() 694 701 695 696 702 def complete_chisqr(self, output, elapsed=None): 703 """ 704 print result chisqr 705 """ 706 try: 707 self.tcChi.SetLabel(format_number(output)) 708 except: 709 raise 710 711 712 def compute_chisqr1D(self, smearer=None): 713 """ 714 Compute chisqr for 1D 715 """ 716 from sans.guiframe.utils import check_value 717 flag = check_value( self.qmin, self.qmax) 718 719 if not flag: 720 return 721 722 try: 723 self.qmin_x = float(self.qmin.GetValue()) 724 self.qmax_x = float(self.qmax.GetValue()) 725 ##return residuals within self.qmin_x and self.qmax_x 726 from gui_thread import CalcChisqr1D 727 ## If a thread is already started, stop it 728 if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): 729 self.calc_Chisqr.stop() 730 731 self.calc_Chisqr= CalcChisqr1D( x= self.data.x, 732 y= self.data.y, 733 dy= self.data.dy, 734 model= self.model, 735 smearer=smearer, 736 qmin=self.qmin_x, 737 qmax=self.qmax_x, 738 completefn = self.complete_chisqr, 739 updatefn = None) 740 741 self.calc_Chisqr.queue() 742 743 except: 744 raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name 745 746 747 748 749 697 750 def compute_chisqr2D(self): 698 751 """ … … 702 755 from sans.guiframe.utils import check_value 703 756 flag = check_value( self.qmin, self.qmax) 704 705 err_image = copy.deepcopy(self.data.err_data) 706 if err_image==[] or err_image==None: 707 err_image= numpy.zeros(len(self.data.x_bins),len(self.data.y_bins)) 708 709 err_image[err_image==0]=1 710 711 res=[] 712 if flag== True: 713 try: 714 self.qmin_x = float(self.qmin.GetValue()) 715 self.qmax_x = float(self.qmax.GetValue()) 716 for i in range(len(self.data.x_bins)): 717 for j in range(len(self.data.y_bins)): 718 #Check the range containing data between self.qmin_x and self.qmax_x 719 value = math.pow(self.data.x_bins[i],2)+ math.pow(self.data.y_bins[j],2) 720 if value >= math.pow(self.qmin_x,2) and value <= math.pow(self.qmax_x,2): 721 722 temp = [self.data.x_bins[i],self.data.y_bins[j]] 723 error= err_image[j][i] 724 chisqrji = (self.data.data[j][i]- self.model.runXY(temp ))/error 725 #Vector containing residuals 726 res.append( math.pow(chisqrji,2) ) 727 728 # compute sum of residual 729 sum=0 730 for item in res: 731 if numpy.isfinite(item): 732 sum +=item 733 self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) 734 except: 735 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 736 "Chisqr cannot be compute: %s"% sys.exc_value)) 737 return 738 757 if not flag: 758 return 759 760 try: 761 self.qmin_x = float(self.qmin.GetValue()) 762 self.qmax_x = float(self.qmax.GetValue()) 763 764 ##return residuals within self.qmin_x and self.qmax_x 765 from gui_thread import CalcChisqr2D 766 ## If a thread is already started, stop it 767 if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): 768 self.calc_Chisqr.stop() 769 770 self.calc_Chisqr= CalcChisqr2D( x_bins= self.data.x_bins, 771 y_bins= self.data.y_bins, 772 data= self.data.data, 773 err_data = self.data.err_data, 774 model= self.model, 775 qmin= self.qmin_x, 776 qmax = self.qmax_x, 777 completefn = self.complete_chisqr, 778 updatefn = None) 779 780 self.calc_Chisqr.queue() 781 782 except: 783 raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name 784 785 739 786 740 787 def compute_chisqr(self , smearer=None): … … 751 798 return 752 799 else: 753 self.qmin_x = float(self.qmin.GetValue()) 754 self.qmax_x = float(self.qmax.GetValue()) 755 # return residuals within self.qmin_x and self.qmax_x 756 x,y = [numpy.asarray(v) for v in (self.data.x,self.data.y)] 757 758 if self.data.dy==None: 759 dy= numpy.zeros(len(y)) 760 else: 761 dy= copy.deepcopy(self.data.dy) 762 dy= numpy.asarray(dy) 763 dy[dy==0]=1 764 765 if self.qmin_x==None and self.qmax_x==None: 766 fx =numpy.asarray([self.model.run(v) for v in x]) 767 if smearer!=None: 768 fx= smearer(fx) 769 temp=(y - fx)/dy 770 res= temp*temp 771 else: 772 idx = (x>= self.qmin_x) & (x <=self.qmax_x) 773 fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) 774 if smearer!=None: 775 fx= smearer(fx) 776 temp=(y[idx] - fx)/dy[idx] 777 res= temp*temp 778 #sum of residuals 779 sum=0 780 for item in res: 781 if numpy.isfinite(item): 782 sum +=item 783 self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) 800 self.compute_chisqr1D(smearer=smearer) 801 return 784 802 except: 785 803 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 786 "Chisqr cannot be compute: %s"% sys.exc_value))804 "Chisqr Error: %s"% sys.exc_value)) 787 805 return 788 806 -
sansview/perspectives/fitting/fitting.py
r8b31780 r2b63df0 3 3 import string, numpy, math 4 4 5 from danse.common.plottools.plottables import Data1D, Theory1D,Data2D 5 from danse.common.plottools.plottables import Data2D,Theory1D 6 from sans.guiframe import dataFitting 6 7 from danse.common.plottools.PlotPanel import PlotPanel 7 8 from sans.guicomm.events import NewPlotEvent, StatusEvent 8 9 from sans.guicomm.events import EVT_SLICER_PANEL,ERR_DATA 9 10 from sans.guiframe import dataFitting 10 from sans.fit.AbstractFitEngine import Model ,FitData1D,FitData2D11 from sans.fit.AbstractFitEngine import Model 11 12 12 13 from fitproblem import FitProblem … … 266 267 dx= item.dx 267 268 268 from sans.guiframe import dataFitting269 269 270 data= dataFitting.Data1D(x=item.x, y=item.y,dx=dx, dy=dy, dxl=dxl, dxw=dxw) 270 271 … … 988 989 new_plot.is_data =False 989 990 990 from DataLoader .data_info import Data1D991 info= Data1D(x= new_plot.x, y=new_plot.y)991 from DataLoader import data_info 992 info= data_info.Data1D(x= new_plot.x, y=new_plot.y) 992 993 info.title= new_plot.name 993 994 title= my_info.title -
sansview/perspectives/fitting/gui_thread.py
r41340860 r2b63df0 2 2 3 3 import time 4 import numpy 5 import copy 6 import math 4 7 import sys 5 8 import wx … … 7 10 from calcthread import CalcThread 8 11 9 10 class SmearPlot(CalcThread): 12 class CalcChisqr1D(CalcThread): 11 13 """ 12 Compute 2D model 13 This calculation assumes a 2-fold symmetry of the model 14 where points are computed for one half of the detector 15 and I(qx, qy) = I(-qx, -qy) is assumed. 14 Compute chisqr 16 15 """ 17 18 def __init__(self, enable_smearer=False,smearer=None,manager=None, 16 def __init__(self, x, y,dy, model, 17 smearer=None, 18 qmin=None, 19 qmax=None, 19 20 completefn = None, 20 21 updatefn = None, … … 26 27 yieldtime, 27 28 worktime) 28 self.enable_smearer = enable_smearer 29 self.smearer =smearer 30 self.y = numpy.array(y) 31 self.x = numpy.array(x) 32 self.dy= copy.deepcopy(dy) 33 self.model = model 34 self.qmin = qmin 35 self.qmax = qmax 29 36 self.smearer = smearer 30 self.manager= manager31 37 self.starttime = 0 38 39 def isquit(self): 40 """ 41 @raise KeyboardInterrupt: when the thread is interrupted 42 """ 43 try: 44 CalcThread.isquit(self) 45 except KeyboardInterrupt: 46 raise KeyboardInterrupt 47 32 48 33 49 def compute(self): … … 35 51 Compute the data given a model function 36 52 """ 37 ## set smearing value whether or not the data contain the smearing info 38 self.manager.set_smearer(self.smearer, qmin= float(self.qmin_x), 39 qmax= float(self.qmax_x)) 40 ##Calculate chi2 41 self.compute_chisqr(smearer= temp_smearer) 42 elapsed = time.time()-self.starttime 43 44 53 self.starttime = time.time() 54 x,y = [numpy.asarray(v) for v in (self.x,self.y)] 55 if self.dy==None or self.dy==[]: 56 self.dy= numpy.zeros(len(self.y)) 57 self.dy[self.dy==0]=1 58 59 if self.qmin==None: 60 self.qmin= min(self.x) 61 62 if self.qmax==None: 63 self.qmax= max(self.x) 64 65 fx = numpy.zeros(len(self.x)) 66 67 output= None 68 res=[] 69 try: 70 71 for i_x in range(len(self.x)): 72 73 # Check whether we need to bail out 74 self.isquit() 75 fx[i_x]=self.model.run(i_x) 76 77 if self.smearer!=None: 78 fx= self.smearer(fx) 79 for i_y in range(len(fx)): 80 # Check whether we need to bail out 81 self.isquit() 82 temp=(self.y[i_y] - fx[i_y])/self.dy[i_y] 83 res.append(temp*temp) 84 #sum of residuals 85 sum=0 86 for item in res: 87 # Check whether we need to bail out 88 self.isquit() 89 if numpy.isfinite(item): 90 sum +=item 91 output = sum/ len(res) 92 elapsed = time.time()-self.starttime 93 self.complete(output= output, elapsed=elapsed) 94 95 except KeyboardInterrupt: 96 # Thread was interrupted, just proceed and re-raise. 97 # Real code should not print, but this is an example... 98 raise 99 except: 100 raise 101 102 class CalcChisqr2D(CalcThread): 103 """ 104 Compute chisqr 105 """ 106 107 def __init__(self, x_bins, y_bins,data,err_data, model, 108 qmin, 109 qmax, 110 completefn = None, 111 updatefn = None, 112 yieldtime = 0.01, 113 worktime = 0.01 114 ): 115 CalcThread.__init__(self,completefn, 116 updatefn, 117 yieldtime, 118 worktime) 119 120 self.y_bins = y_bins 121 self.x_bins = x_bins 122 self.data= data 123 self.err_data= copy.deepcopy(err_data) 124 self.model = model 125 self.qmin = qmin 126 self.qmax = qmax 127 128 self.starttime = 0 129 130 def isquit(self): 131 """ 132 @raise KeyboardInterrupt: when the thread is interrupted 133 """ 134 try: 135 CalcThread.isquit(self) 136 except KeyboardInterrupt: 137 raise KeyboardInterrupt 138 139 140 def compute(self): 141 """ 142 Compute the data given a model function 143 """ 144 self.starttime = time.time() 145 if self.err_data==None or self.err_data==[]: 146 self.err_data= numpy.zeros(len(self.x_bins),len(self.y_bins)) 147 148 self.err_data[self.err_data==0]=1 149 150 output= None 151 res=[] 152 try: 153 154 for i in range(len(self.x_bins)): 155 # Check whether we need to bail out 156 self.isquit() 157 for j in range(len(self.y_bins)): 158 #Check the range containing data between self.qmin_x and self.qmax_x 159 value = math.pow(self.x_bins[i],2)+ math.pow(self.y_bins[j],2) 160 if value >= math.pow(self.qmin,2) and value <= math.pow(self.qmax,2): 161 162 temp = [self.x_bins[i],self.y_bins[j]] 163 error= self.err_data[j][i] 164 chisqrji = (self.data[j][i]- self.model.runXY(temp ))/error 165 #Vector containing residuals 166 res.append( math.pow(chisqrji,2) ) 167 168 sum=0 169 for item in res: 170 # Check whether we need to bail out 171 self.isquit() 172 if numpy.isfinite(item): 173 sum +=item 174 output = sum/ len(res) 175 elapsed = time.time()-self.starttime 176 self.complete(output= output, elapsed=elapsed) 177 178 except KeyboardInterrupt: 179 # Thread was interrupted, just proceed and re-raise. 180 # Real code should not print, but this is an example... 181 raise
Note: See TracChangeset
for help on using the changeset viewer.