Changeset 0954398 in sasview
- Timestamp:
- May 21, 2008 5:07:40 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:
- 73bbe35
- Parents:
- fa452e4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/test/FittingModule.py
racb8788 r0954398 3 3 from Loader import Load 4 4 from scipy import optimize 5 6 7 class FitArrange: 8 def __init__(self): 9 """ 10 @param model: the model selected by the user 11 @param Ldata: a list of data what the user want to fit 12 """ 13 self.model = None 14 self.dList =[] 15 16 def set_model(self,model): 17 """ set the model """ 18 self.model = model 19 20 def add_data(self,data): 21 """ 22 @param data: Data to add in the list 23 fill a self.dataList with data to fit 24 """ 25 if not data in self.dList: 26 self.dList.append(data) 27 28 def get_model(self): 29 return self.model 30 31 def get_data(self): 32 """ Return list of data""" 33 return self.dList 34 35 def delete_data(self,data): 36 """ 37 Remove one element from the list 38 @param data: Data to remove from the the lsit of data 39 """ 40 if data in self.dList: 41 self.dList.remove(data) 42 5 43 class Fitting: 6 44 """ … … 9 47 def __init__(self,data=[]): 10 48 #self.model is a list of all models to fit 11 self.model=[] 49 self.model={} 50 self.fitArrangeList={} 12 51 #the list of all data to fit 13 52 self.data = data … … 22 61 """ 23 62 return True 63 24 64 def fit(self,pars, qmin=None, qmax=None): 25 65 """ 26 66 Do the fit 27 67 """ 28 29 # Do the fit with 2 data set and one model 30 numberData= len(self.data) 31 numberModel= len(self.model) 32 if numberData==1 and numberModel==1: 33 if qmin==None: 34 xmin= min(self.data[0].x) 35 if qmax==None: 36 xmax= max(self.data[0].x) 37 38 #chisqr, out, cov = fitHelper(self.model[0],self.data[0],pars,xmin,xmax) 39 chisqr, out, cov =fitHelper(self.model[0], pars, self.data[0].x, 40 self.data[0].y, self.data[0].dy ,xmin,xmax) 41 else:# More than one data to fit with one model 42 xtemp=[] 43 ytemp=[] 44 dytemp=[] 45 for data in self.data: 46 for i in range(len(data.x)): 47 if not data.x[i] in xtemp: 48 xtemp.append(data.x[i]) 49 50 if not data.y[i] in ytemp: 51 ytemp.append(data.y[i]) 52 53 if not data.dy[i] in dytemp: 54 dytemp.append(data.dy[i]) 55 if qmin==None: 56 xmin= min(xtemp) 57 if qmax==None: 58 xmax= max(xtemp) 59 #chisqr, out, cov = fitHelper(self.model[0], 60 #temp,pars,min(temp.x),max(temp.x)) 61 chisqr, out, cov =fitHelper(self.model[0], pars, xtemp, 62 ytemp, dytemp ,xmin,xmax) 68 # Do the fit with more than one data set and one model 69 xtemp=[] 70 ytemp=[] 71 dytemp=[] 72 73 #for item in self.self.fitArrangeList.: 74 75 fitproblem=self.fitArrangeList.values()[0] 76 listdata=[] 77 model =fitproblem.get_model() 78 listdata= fitproblem.get_data() 79 80 for data in listdata: 81 for i in range(len(data.x)): 82 if not data.x[i] in xtemp: 83 xtemp.append(data.x[i]) 84 85 if not data.y[i] in ytemp: 86 ytemp.append(data.y[i]) 87 88 if not data.dy[i] in dytemp: 89 dytemp.append(data.dy[i]) 90 if qmin==None: 91 qmin= min(xtemp) 92 if qmax==None: 93 qmax= max(xtemp) 94 chisqr, out, cov = fitHelper(model, pars, xtemp,ytemp, dytemp ,qmin,qmax) 63 95 return chisqr, out, cov 64 96 65 def set_model(self,model ):97 def set_model(self,model,Uid): 66 98 """ Set model """ 67 self.model.append(model) 68 69 def set_data(self,data): 99 #self.model[Uid] = model 100 fitproblem= FitArrange() 101 fitproblem.set_model(model) 102 self.fitArrangeList[Uid]=fitproblem 103 104 def set_data(self,data,Uid): 70 105 """ Receive plottable and create a list of data to fit""" 71 self.data.append(data) 72 106 #self.data.append(data) 107 if self.fitArrangeList.has_key(Uid): 108 self.fitArrangeList[Uid].add_data(data) 109 else: 110 fitproblem= FitArrange() 111 fitproblem.add_data(data) 112 self.fitArrangeList[Uid]=fitproblem 113 73 114 def get_data(self): 74 115 """ return list of data""" … … 82 123 """ return the contraint value """ 83 124 return self.contraint 84 85 def get_residuals(model,data,qmin=None,qmax=None): 86 """ 87 Calculates the vector of residuals for each point 88 in y for a given set of input parameters. 89 @param params: list of parameter values 90 @return: vector of residuals 91 """ 92 residuals = [] 93 94 for j in range(len(data.x)): 95 if data.x[j]> qmin and data.x[j]< qmax: 96 residuals.append( ( data.y[j] - model.runXY(data.x[j]) ) / data.dy[j]) 97 98 return residuals 99 100 101 def chi2(params): 102 """ 103 Calculates chi^2 104 @param params: list of parameter values 105 @return: chi^2 106 """ 107 sum = 0 108 res = get_residuals(params) 109 for item in res: 110 sum += item*item 111 return sum 112 113 114 def residual(self): 115 return self.residuals 116 117 def fitHelper(model,data,pars,qmin=None,qmax=None): 118 """ Do the actual fitting""" 119 120 p = [param() for param in pars] 121 out, cov_x, info, mesg, success = optimize.leastsq(get_residuals, p, full_output=1, warning=True) 122 print info, mesg, success 123 # Calculate chi squared 124 if len(pars)>1: 125 chisqr = self.chi2(out) 126 elif len(pars)==1: 127 chisqr = self.chi2([out]) 128 129 return chisqr, out, cov_x 125 126 130 127 131 128 class Parameter: … … 213 210 load.load_data(data1) 214 211 Fit =Fitting() 215 Fit.set_data(data1)212 216 213 from sans.guitools.LineModel import LineModel 217 214 model = LineModel() 218 Fit.set_model(model )219 215 Fit.set_model(model,1 ) 216 Fit.set_data(data1,1) 220 217 default_A = model.getParam('A') 221 218 default_B = model.getParam('B') … … 227 224 228 225 # test fit with 2 data and one model 226 Fit =Fitting() 227 Fit.set_model(model,2 ) 229 228 load.set_filename("testdata1.txt") 230 229 load.set_values() … … 233 232 234 233 load.load_data(data2) 235 Fit.set_data(data2 )234 Fit.set_data(data2,2) 236 235 237 236 load.set_filename("testdata2.txt") … … 240 239 data3.name = "data2" 241 240 load.load_data(data3) 242 Fit.set_data(data3 )241 Fit.set_data(data3,2) 243 242 chisqr, out, cov=Fit.fit([cstA,cstB],None,None) 244 243 print"fit two data",chisqr, out, cov 244
Note: See TracChangeset
for help on using the changeset viewer.