Changeset f24b8f4 in sasview
- Timestamp:
- May 23, 2008 11:10:39 AM (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:
- a1718df
- Parents:
- dfb58f8
- Location:
- park_integration
- Files:
-
- 1 added
- 1 deleted
- 1 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
park_integration/docs/FittingModule.py
reb06cbe rf24b8f4 8 8 def __init__(self): 9 9 """ 10 Store a set of data for a given model to perform the Fit 10 11 @param model: the model selected by the user 11 12 @param Ldata: a list of data what the user want to fit … … 47 48 """ 48 49 def __init__(self,data=[]): 49 #self.model is a list of all models to fit 50 #self.model={} 50 #this is a dictionary of FitArrange elements 51 51 self.fitArrangeList={} 52 #the list of all data to fit 53 self.data = data 54 #list of models parameters 55 self.parameters=[] 56 52 #the constraint of the Fit 57 53 self.constraint =None 54 #Specify the use of scipy or park fit 58 55 self.fitType =None 59 56 … … 73 70 fitproblem=self.fitArrangeList.values()[0] 74 71 listdata=[] 75 model =fitproblem.get_model() 76 listdata= fitproblem.get_data() 77 self.set_param(model, pars) 72 model = fitproblem.get_model() 73 listdata = fitproblem.get_data() 74 75 parameters = self.set_param(model,pars) 78 76 if listdata==[]: 79 77 raise ValueError, " data list missing" … … 83 81 ytemp=[] 84 82 dytemp=[] 85 86 83 87 84 for data in listdata: 88 85 for i in range(len(data.x)): … … 99 96 if qmax==None: 100 97 qmax= max(xtemp) 101 chisqr, out, cov = fitHelper(model, self.parameters, xtemp,ytemp, dytemp ,qmin,qmax)98 chisqr, out, cov = fitHelper(model,parameters, xtemp,ytemp, dytemp ,qmin,qmax) 102 99 return chisqr, out, cov 103 100 104 101 def set_model(self,model,Uid): 105 102 """ Set model """ 106 #self.model[Uid] = model 107 fitproblem= FitArrange() 108 fitproblem.set_model(model) 109 self.fitArrangeList[Uid]=fitproblem 103 if self.fitArrangeList.has_key(Uid): 104 self.fitArrangeList[Uid].set_model(model) 105 else: 106 fitproblem= FitArrange() 107 fitproblem.set_model(model) 108 self.fitArrangeList[Uid]=fitproblem 110 109 111 110 def set_data(self,data,Uid): 112 111 """ Receive plottable and create a list of data to fit""" 113 #self.data.append(data)112 114 113 if self.fitArrangeList.has_key(Uid): 115 114 self.fitArrangeList[Uid].add_data(data) … … 119 118 self.fitArrangeList[Uid]=fitproblem 120 119 121 def get_ data(self):120 def get_model(self,Uid): 122 121 """ return list of data""" 123 return self. data122 return self.fitArrangeList[Uid] 124 123 125 124 def set_param(self,model, pars): 126 125 """ Recieve a dictionary of parameter and save it """ 127 self.parameters=[]126 parameters=[] 128 127 if model==None: 129 128 raise ValueError, "Cannot set parameters for empty model" … … 131 130 #for key ,value in pars: 132 131 for key, value in pars.iteritems(): 133 print "this is the key",key134 print "this is the value",value135 132 param = Parameter(model, key, value) 136 self.parameters.append(param) 137 138 def add_contraint(self, contraint): 133 parameters.append(param) 134 return parameters 135 136 def add_constraint(self, constraint): 139 137 """ User specify contraint to fit """ 140 self.con traint = str(contraint)141 142 def get_con traint(self):138 self.constraint = str(constraint) 139 140 def get_constraint(self): 143 141 """ return the contraint value """ 144 return self.contraint 145 146 142 return self.constraint 143 144 def set_constraint(self,constraint): 145 """ 146 receive a string as a constraint 147 @param constraint: a string used to constraint some parameters to get a 148 specific value 149 """ 150 self.constraint= constraint 151 152 153 147 154 148 155 class Parameter: … … 235 242 Fit.set_model(model,1) 236 243 Fit.set_data(data1,1) 237 #default_A = model.getParam('A') 238 #default_B = model.getParam('B') 239 #cstA = Parameter(model, 'A', default_A) 240 #cstB = Parameter(model, 'B', default_B) 241 242 #chisqr, out, cov=Fit.fit([cstA,cstB],None,None) 244 243 245 chisqr, out, cov=Fit.fit({'A':2,'B':1},None,None) 244 246 print"fit only one data",chisqr, out, cov -
park_integration/test/testfitting.py
r93c2ef2 rf24b8f4 52 52 53 53 54 def testfit(self): 55 """ test fitting""" 54 def testfit_1Data_1Model(self): 55 """ test fitting for one data and one model""" 56 #load data 56 57 from Loader import Load 57 58 load= Load() 58 59 load.set_filename("testdata_line.txt") 59 60 load.set_values() 60 x,y,dx,dy = load.get_values()61 61 data1 = Data1D(x=[], y=[],dx=None, dy=None) 62 62 load.load_data(data1) 63 63 64 #Importing the Fit module 65 from FittingModule import Fitting 66 Fit= Fitting() 64 67 # Receives the type of model for the fitting 65 68 from sans.guitools.LineModel import LineModel 66 69 model = LineModel() 67 70 71 #Do the fit 72 Fit.set_data(data1,1) 73 Fit.set_model(model,1) 74 chisqr, out, cov=Fit.fit({'A':2,'B':1},None,None) 75 #print"fit only one data",chisqr, out, cov 76 #Testing results 77 self.assertEqual(Fit.fit_engine("scipy"),True) 78 self.assert_(math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) < 2) 79 self.assert_(math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) < 2) 80 self.assert_(chisqr/len(data1.x) < 2) 81 82 #print "chisqr",chisqr/len(data1.x) 83 #print "Error on A",math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) 84 #print "Error on B",math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) 85 86 def testfit_2Data_1Model(self): 87 """ test fitting for two set of data data and one model""" 88 from Loader import Load 89 load= Load() 90 #Load the first set of data 91 load.set_filename("testdata1.txt") 92 load.set_values() 93 data1 = Data1D(x=[], y=[],dx=None, dy=None) 94 load.load_data(data1) 95 96 #Load the second set of data 97 load.set_filename("testdata2.txt") 98 load.set_values() 99 data2 = Data1D(x=[], y=[],dx=None, dy=None) 100 load.load_data(data2) 101 102 #Importing the Fit module 68 103 from FittingModule import Fitting 69 104 Fit= Fitting() 70 Fit.set_data(data1) 71 Fit.set_model(model) 105 # Receives the type of model for the fitting 106 from sans.guitools.LineModel import LineModel 107 model = LineModel() 72 108 73 default_A = model.getParam('A') 74 default_B = model.getParam('B') 75 cstA = Parameter(model, 'A', default_A) 76 cstB = Parameter(model, 'B', default_B) 77 chisqr, out, cov=Fit.fit([cstA,cstB],None,None) 78 print"fit only one data",chisqr, out, cov 79 80 self.assertEqual(Fit.fit_engine(),True) 109 #Do the fit 110 Fit.set_data(data1,1) 111 Fit.set_model(model,1) 81 112 113 Fit.set_data(data2,2) 114 Fit.set_model(model,2) 115 116 chisqr, out, cov=Fit.fit({'A':2,'B':1},None,None) 117 #print"fit only one data",chisqr, out, cov 118 119 #Testing results 120 self.assertEqual(Fit.fit_engine("scipy"),True) 82 121 self.assert_(math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) < 2) 83 print "chisqr",chisqr/len(data1.x)122 self.assert_(math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) < 2) 84 123 self.assert_(chisqr/len(data1.x) < 2) 85 # self.assertAlmostEqual(out[1],2.5)86 #self.assertAlmostEquals(out[0],4.0)87 #self.assertAlmostEquals(out[1]+cov[1][1],2.5)88 #self.assertAlmostEquals(out[0]+cov[0][0],4.0)89 124
Note: See TracChangeset
for help on using the changeset viewer.