Changeset 1e3169c in sasview
- Timestamp:
- Oct 27, 2009 1:56:39 PM (15 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:
- b37f72f
- Parents:
- cc1500d
- Location:
- park_integration
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/AbstractFitEngine.py
r2eaaf1a r1e3169c 1 1 import logging, sys 2 2 import park,numpy,math, copy 3 3 from DataLoader.data_info import Data1D 4 from DataLoader.data_info import Data2D 4 5 class SansParameter(park.Parameter): 5 6 """ … … 119 120 120 121 121 class FitData1D(object): 122 """ Wrapper class for SANS data """ 123 def __init__(self,sans_data1d, smearer=None): 124 """ 125 Data can be initital with a data (sans plottable) 126 or with vectors. 127 128 self.smearer is an object of class QSmearer or SlitSmearer 122 class FitData1D(Data1D): 123 """ 124 Wrapper class for SANS data 125 FitData1D inherits from DataLoader.data_info.Data1D. Implements 126 a way to get residuals from data. 127 """ 128 def __init__(self,x, y,dx= None, dy=None, smearer=None): 129 Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) 130 """ 131 @param smearer: is an object of class QSmearer or SlitSmearer 129 132 that will smear the theory data (slit smearing or resolution 130 133 smearing) when set. … … 134 137 135 138 from DataLoader.qsmearing import smear_selection 136 fitdata1d = FitData1D(some_data) 137 fitdata1d.smearer = smear_selection(some_data) 138 139 smearer = smear_selection(some_data) 140 fitdata1d = FitData1D( x= [1,3,..,], 141 y= [3,4,..,8], 142 dx=None, 143 dy=[1,2...], smearer= smearer) 144 139 145 Note that some_data _HAS_ to be of class DataLoader.data_info.Data1D 140 146 … … 144 150 145 151 self.smearer = smearer 146 147 # Initialize from Data1D object 148 self.data=sans_data1d 149 self.x= numpy.array(sans_data1d.x) 150 self.y= numpy.array(sans_data1d.y) 151 self.dx= sans_data1d.dx 152 if sans_data1d.dy ==None or sans_data1d.dy==[]: 152 if dy ==None or dy==[]: 153 153 self.dy= numpy.zeros(len(self.y)) 154 154 else: 155 self.dy= numpy.asarray( sans_data1d.dy)155 self.dy= numpy.asarray(dy) 156 156 157 157 # For fitting purposes, replace zero errors by 1 … … 162 162 ## Min Q-value 163 163 #Skip the Q=0 point, especially when y(q=0)=None at x[0]. 164 if min (self. data.x) ==0.0 and self.data.x[0]==0 and not numpy.isfinite(self.data.y[0]):165 self.qmin = min(self. data.x[self.data.x!=0])164 if min (self.x) ==0.0 and self.x[0]==0 and not numpy.isfinite(self.y[0]): 165 self.qmin = min(self.x[self.x!=0]) 166 166 else: 167 self.qmin= min (self. data.x)167 self.qmin= min (self.x) 168 168 ## Max Q-value 169 self.qmax = max (self.data.x)169 self.qmax = max (self.x) 170 170 171 171 # Range used for input to smearing … … 182 182 # Skip Q=0 point, (especially for y(q=0)=None at x[0]). 183 183 #ToDo: Fix this. 184 if qmin==0.0 and not numpy.isfinite(self. data.y[qmin]):185 self.qmin = min(self. data.x[self.data.x!=0])184 if qmin==0.0 and not numpy.isfinite(self.y[qmin]): 185 self.qmin = min(self.x[self.x!=0]) 186 186 elif qmin!=None: 187 187 self.qmin = qmin … … 196 196 197 197 self._first_unsmeared_bin = 0 198 self._last_unsmeared_bin = len(self. data.x)-1198 self._last_unsmeared_bin = len(self.x)-1 199 199 200 200 if self.smearer!=None: 201 201 self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax) 202 self._qmin_unsmeared = self. data.x[self._first_unsmeared_bin]203 self._qmax_unsmeared = self. data.x[self._last_unsmeared_bin]202 self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 203 self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 204 204 205 205 # Identify the bin range for the unsmeared and smeared spaces … … 249 249 250 250 251 class FitData2D( object):251 class FitData2D(Data2D): 252 252 """ Wrapper class for SANS data """ 253 def __init__(self,sans_data2d): 253 def __init__(self,sans_data2d ,data=None, err_data=None,): 254 Data2D.__init__(self, data= data, err_data= err_data) 254 255 """ 255 256 Data can be initital with a data (sans plottable) 256 257 or with vectors. 257 258 """ 258 self.data=sans_data2d 259 self.image = sans_data2d.data 260 self.err_image = sans_data2d.err_data 259 self.x_bins_array = [] 260 self.y_bins_array = [] 261 self.res_err_image=[] 262 self.index_model=[] 263 self.qmin= None 264 self.qmax= None 265 self.set_data(sans_data2d ) 266 267 268 def set_data(self, sans_data2d ): 269 """ 270 Determine the correct x_bin and y_bin to fit 271 """ 272 self.err_data = sans_data2d.err_data 261 273 self.x_bins_array= numpy.reshape(sans_data2d.x_bins, 262 274 [1,len(sans_data2d.x_bins)]) … … 264 276 [len(sans_data2d.y_bins),1]) 265 277 266 x = max(self.data.xmin, self.data.xmax)267 y = max(self.data.ymin, self.data.ymax)278 x_max = max(self.data.xmin, self.data.xmax) 279 y_max = max(self.data.ymin, self.data.ymax) 268 280 269 281 ## fitting range 270 282 self.qmin = 1e-16 271 self.qmax = math.sqrt(x *x +y*y)283 self.qmax = math.sqrt(x_max*x_max +y_max*y_max) 272 284 ## new error image for fitting purpose 273 285 if self.err_image== None or self.err_image ==[]: … … 467 479 """ 468 480 if data.__class__.__name__=='Data2D': 469 fitdata=FitData2D( data)470 else: 471 fitdata=FitData1D( data,smearer)481 fitdata=FitData2D(sans_data2d=data, data=data.data, err_data= data.err_data) 482 else: 483 fitdata=FitData1D(x=data.x, y=data.y , dx= data.dx,dy=data.dy,smearer=smearer) 472 484 473 485 fitdata.setFitRange(qmin=qmin,qmax=qmax) -
park_integration/test/small_test.py
r1ea3488 r1e3169c 3 3 """ 4 4 import unittest 5 from danse.common.plottools.plottables import Theory1D 6 from danse.common.plottools.plottables import Data1D 7 from sans.fit.AbstractFitEngine import Model,FitData1D 5 from sans.fit.AbstractFitEngine import Model 8 6 import math 9 7 from sans.fit.Fitting import Fit … … 13 11 """ test fitting """ 14 12 def test_park(self): 15 """ Simple cylinder model fit ( park) """13 """ Simple cylinder model fit (scipy) """ 16 14 17 out=Loader().load("cyl_400_20.txt") 18 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 19 15 out= Loader().load("cyl_400_20.txt") 16 20 17 fitter = Fit('scipy') 21 18 # Receives the type of model for the fitting … … 23 20 model1 = CylinderModel() 24 21 model1.setParam('contrast', 1) 25 data1.smearer = None26 data = FitData1D(data1)27 22 model = Model(model1) 28 23 29 24 pars1 =['length','radius','scale'] 30 fitter.set_data( data,1)25 fitter.set_data(out,1) 31 26 model.set( scale=1e-10 ) 32 27 fitter.set_model(model,1,pars1) -
park_integration/test/test_fit_cylinder.py
reb575b0 r1e3169c 5 5 import unittest 6 6 7 from danse.common.plottools.plottables import Data1D,Theory1D 8 from sans.fit.AbstractFitEngine import Model,FitData1D 7 from sans.fit.AbstractFitEngine import Model 9 8 import math 10 9 from sans.fit.Fitting import Fit … … 15 14 def setUp(self): 16 15 """ initialize data""" 17 out = Loader().load("cyl_400_20.txt") 18 #Create data that fitting engine understands 19 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) 20 self.data = FitData1D(data1) 16 self.data = Loader().load("cyl_400_20.txt") 21 17 # Create model that fitting engine understands 22 18 from sans.models.CylinderModel import CylinderModel … … 77 73 """ initialize data""" 78 74 79 out=Loader().load("cyl_400_20.txt") 80 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) 81 self.data1 = FitData1D(data1) 82 83 out2=Loader().load("cyl_400_40.txt") 84 data2 = Data1D(x=out2.x, y=out2.y, dx=out2.dx, dy=out2.dy) 85 self.data2 = FitData1D(data2) 75 self.data1=Loader().load("cyl_400_20.txt") 76 self.data2=Loader().load("cyl_400_40.txt") 86 77 87 78 # Receives the type of model for the fitting -
park_integration/test/test_fit_line.py
r2eaaf1a r1e3169c 5 5 import unittest 6 6 7 from danse.common.plottools.plottables import Data1D,Theory1D 8 from sans.fit.AbstractFitEngine import Model,FitData1D 7 from sans.fit.AbstractFitEngine import Model 9 8 import math 10 9 class testFitModule(unittest.TestCase): … … 15 14 #load data 16 15 from DataLoader.loader import Loader 17 data 1= Loader().load("testdata_line.txt")16 data = Loader().load("testdata_line.txt") 18 17 #Importing the Fit module 19 18 from sans.fit.Fitting import Fit … … 23 22 model1 = LineModel() 24 23 model1.name = "M1" 25 #data = Data(sans_data=data1 )26 #data1.smearer=None27 data = FitData1D(data1 )28 24 model = Model(model1) 29 25 #fit with scipy test … … 71 67 from DataLoader.loader import Loader 72 68 l = Loader() 73 out=l.load("testdata_line.txt")74 data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y)75 out=l.load("testdata_line1.txt")76 data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y)69 data1=l.load("testdata_line.txt") 70 71 data2=l.load("testdata_line1.txt") 72 77 73 #Importing the Fit module 78 74 from sans.fit.Fitting import Fit … … 84 80 model22 = LineModel() 85 81 model11.name= "M2" 86 data11.smearer=None 87 data22.smearer=None 88 data1 = FitData1D(data11 ) 89 data2 = FitData1D(data22 ) 90 82 91 83 model1 = Model(model11) 92 84 model2 = Model(model22) … … 124 116 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 125 117 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 126 self.assertTrue( result2.fitness/ len(data2.x) < 2)118 self.assertTrue( result2.fitness/(len(data1.x)+len(data2.x)) < 2) 127 119 128 120 … … 132 124 from DataLoader.loader import Loader 133 125 l = Loader() 134 out=l.load("testdata_line.txt") 135 data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 136 out=l.load("testdata_cst.txt") 137 data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 138 #Importing the Fit module 139 from sans.fit.Fitting import Fit 140 fitter = Fit('park') 141 # Receives the type of model for the fitting 142 from sans.models.LineModel import LineModel 143 from sans.models.Constant import Constant 126 data1= l.load("testdata_line.txt") 127 data2= l.load("testdata_cst.txt") 128 129 # Receives the type of model for the fitting 130 from sans.models.LineModel import LineModel 144 131 model11 = LineModel() 145 132 model11.name= "line" … … 147 134 model11.setParam("B",1.0) 148 135 136 from sans.models.Constant import Constant 149 137 model22 = Constant() 150 138 model22.name= "cst" 151 139 model22.setParam("value", 1.0) 152 140 153 # Constrain the constant value to be equal to parameter B (the real value is 2.5)154 155 data11.smearer= None156 data22.smearer= None157 data1 = FitData1D(data11 )158 data2 = FitData1D(data22 )159 141 model1 = Model(model11) 160 142 model2 = Model(model22) 161 143 model1.set(A=4) 162 144 model1.set(B=3) 145 # Constraint the constant value to be equal to parameter B (the real value is 2.5) 163 146 model2.set(value='line.B') 164 147 #fit with scipy test … … 166 149 pars2= ['value'] 167 150 151 #Importing the Fit module 152 from sans.fit.Fitting import Fit 153 fitter = Fit('park') 168 154 fitter.set_data(data1,1) 169 155 fitter.set_model(model1,1,pars1) … … 177 163 self.assertTrue( math.fabs(result2.pvec[0]-4.0)/3. <= result2.stderr[0]) 178 164 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3. <= result2.stderr[1]) 179 self.assertTrue( result2.fitness/ len(data2.x) < 2)165 self.assertTrue( result2.fitness/(len(data1.x)+len(data2.x)) < 2) 180 166 181 167 … … 185 171 from DataLoader.loader import Loader 186 172 l = Loader() 187 out=l.load("testdata_line.txt") 188 data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 189 out=l.load("testdata_line1.txt") 190 data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 191 #Importing the Fit module 192 from sans.fit.Fitting import Fit 193 fitter = Fit('scipy') 173 data1 = l.load("testdata_line.txt") 174 data2 = l.load("testdata_line1.txt") 175 176 194 177 # Receives the type of model for the fitting 195 178 from sans.models.LineModel import LineModel … … 199 182 model1.setParam("B",1.0) 200 183 model = Model(model1) 201 202 data11.smearer= None 203 data22.smearer= None 204 data1 = FitData1D(data11 ) 205 data2 = FitData1D(data22 ) 206 207 #fit with scipy test 208 pars1= ['A','B'] 184 185 #fit with scipy test 186 pars1= ['A','B'] 187 #Importing the Fit module 188 from sans.fit.Fitting import Fit 189 fitter = Fit('scipy') 209 190 fitter.set_data(data1,1,qmin=0, qmax=7) 210 191 fitter.set_model(model,1,pars1) … … 236 217 self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 237 218 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 238 #self.assertAlmostEquals( result1.fitness,result2.fitness ) 239 self.assertTrue( result1.fitness/len(data1.x) < 2 ) 240 self.assertTrue( result2.fitness/len(data2.x) < 2 ) 219 self.assertTrue( result2.fitness/(len(data2.x)+len(data1.x)) < 2 ) 241 220 242 221
Note: See TracChangeset
for help on using the changeset viewer.