Changeset fd0d30fd in sasview
- Timestamp:
- Aug 10, 2009 9:31:31 AM (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:
- 785c8233
- Parents:
- b1d45c1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/AbstractFitEngine.py
r7f81665 rfd0d30fd 113 113 """ 114 114 try: 115 115 return self.model.evalDistribution(x) 116 116 except: 117 print "AbstractFitEngine.Model.eval [FIXME]:", sys.exc_value 118 raise 117 raise 119 118 120 class Data(object):121 """ Wrapper class for SANS data """122 def __init__(self,x=None,y=None,dy=None,dx=None,sans_data=None):123 """124 Data can be initital with a data (sans plottable)125 or with vectors.126 """127 if sans_data !=None:128 self.x= sans_data.x129 self.y= sans_data.y130 self.dx= sans_data.dx131 self.dy= sans_data.dy132 133 elif (x!=None and y!=None and dy!=None):134 self.x=x135 self.y=y136 self.dx=dx137 self.dy=dy138 else:139 raise ValueError,\140 "Data is missing x, y or dy, impossible to compute residuals later on"141 self.qmin=None142 self.qmax=None143 144 145 def setFitRange(self,mini=None,maxi=None):146 """ to set the fit range"""147 148 self.qmin=mini149 self.qmax=maxi150 151 152 def getFitRange(self):153 """154 @return the range of data.x to fit155 """156 return self.qmin, self.qmax157 158 159 def residuals(self, fn):160 """ @param fn: function that return model value161 @return residuals162 """163 x,y,dy = [numpy.asarray(v) for v in (self.x,self.y,self.dy)]164 if self.qmin==None and self.qmax==None:165 fx =numpy.asarray([fn(v) for v in x])166 return (y - fx)/dy167 else:168 idx = (x>=self.qmin) & (x <= self.qmax)169 fx = numpy.asarray([fn(item)for item in x[idx ]])170 return (y[idx] - fx)/dy[idx]171 172 def residuals_deriv(self, model, pars=[]):173 """174 @return residuals derivatives .175 @note: in this case just return empty array176 """177 return []178 179 119 180 120 class FitData1D(object): … … 206 146 # Initialize from Data1D object 207 147 self.data=sans_data1d 208 self.x= sans_data1d.x209 self.y= sans_data1d.y148 self.x= numpy.array(sans_data1d.x) 149 self.y= numpy.array(sans_data1d.y) 210 150 self.dx= sans_data1d.dx 211 self.dy= sans_data1d.dy 151 if sans_data1d.dy ==None or sans_data1d.dy==[]: 152 self.dy= numpy.zeros(len(y)) 153 else: 154 self.dy= numpy.asarray(sans_data1d.dy) 155 156 # For fitting purposes, replace zero errors by 1 157 #TODO: check validity for the rare case where only 158 # a few points have zero errors 159 self.dy[self.dy==0]=1 212 160 213 161 ## Min Q-value … … 223 171 self._qmin_unsmeared = self.qmin 224 172 self._qmax_unsmeared = self.qmax 173 # Identify the bin range for the unsmeared and smeared spaces 174 self.idx = (self.x>=self.qmin) & (self.x <= self.qmax) 175 self.idx_unsmeared = (self.x>=self._qmin_unsmeared) & (self.x <= self._qmax_unsmeared) 176 225 177 226 178 … … 256 208 except: 257 209 logging.error("FitData1D.setFitRange: %s" % sys.exc_value) 258 210 # Identify the bin range for the unsmeared and smeared spaces 211 self.idx = (self.x>=self.qmin) & (self.x <= self.qmax) 212 self.idx_unsmeared = (self.x>=self._qmin_unsmeared) & (self.x <= self._qmax_unsmeared) 213 259 214 260 215 def getFitRange(self): … … 274 229 @return residuals 275 230 """ 276 x,y = [numpy.asarray(v) for v in (self.x,self.y)]277 if self.dy ==None or self.dy==[]:278 dy= numpy.zeros(len(y))279 else:280 dy= numpy.asarray(self.dy)281 282 # For fitting purposes, replace zero errors by 1283 #TODO: check validity for the rare case where only284 # a few points have zero errors285 dy[dy==0]=1286 287 # Identify the bin range for the unsmeared and smeared spaces288 idx = (x>=self.qmin) & (x <= self.qmax)289 idx_unsmeared = (x>=self._qmin_unsmeared) & (x <= self._qmax_unsmeared)290 291 231 # Compute theory data f(x) 292 fx= numpy.zeros(len(x)) 293 232 fx= numpy.zeros(len(self.x)) 294 233 _first_bin = None 295 234 _last_bin = None 296 for i_x in range(len(x)): 297 try: 298 if idx_unsmeared[i_x]==True: 299 # Identify first and last bin 300 #TODO: refactor this to pass q-values to the smearer 301 # and let it figure out which bin range to use 302 if _first_bin is None: 303 _first_bin = i_x 304 else: 305 _last_bin = i_x 306 307 value = fn(x[i_x]) 308 fx[i_x] = value 309 except: 310 ## skip error for model.run(x) 311 pass 312 235 236 fx = fn(self.x[self.idx_unsmeared]) 237 238 239 for i_x in range(len(self.x)): 240 if self.idx_unsmeared[i_x]==True: 241 # Identify first and last bin 242 #TODO: refactor this to pass q-values to the smearer 243 # and let it figure out which bin range to use 244 if _first_bin is None: 245 _first_bin = i_x 246 else: 247 _last_bin = i_x 248 313 249 ## Smear theory data 314 250 if self.smearer is not None: … … 316 252 317 253 ## Sanity check 318 if numpy.size(dy)!= numpy.size(fx): 319 raise RuntimeError, "FitData1D: invalid error array %d <> %d" % (numpy.size(dy), numpy.size(fx)) 320 321 return (y[idx]-fx[idx])/dy[idx] 254 if numpy.size(self.dy)!= numpy.size(fx): 255 raise RuntimeError, "FitData1D: invalid error array %d <> %d" % (numpy.shape(self.dy), 256 numpy.size(fx)) 257 258 return (self.y[self.idx]-fx[self.idx])/self.dy[self.idx] 322 259 323 260 … … 346 283 [len(sans_data2d.y_bins),1]) 347 284 348 349 self.x_bins = sans_data2d.x_bins350 self.y_bins = sans_data2d.y_bins351 352 285 x = max(self.data.xmin, self.data.xmax) 353 286 y = max(self.data.ymin, self.data.ymax) … … 384 317 385 318 def residuals(self, fn): 319 386 320 res=self.index_model*(self.image - fn([self.y_bins_array, 387 self.x_bins_array]))/self.res_err_image321 self.x_bins_array]))/self.res_err_image 388 322 return res.ravel() 389 390 391 def old_residuals(self, fn): 392 """ @param fn: function that return model value 393 @return residuals 394 """ 395 res=[] 396 397 for i in range(len(self.x_bins)): 398 for j in range(len(self.y_bins)): 399 temp = math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2) 400 radius= math.sqrt(temp) 401 if self.qmin <= radius and radius <= self.qmax: 402 res.append( (self.image[j][i]- fn([self.x_bins[i],self.y_bins[j]]))\ 403 /self.res_err_image[j][i] ) 404 405 return numpy.array(res) 406 407 323 324 408 325 def residuals_deriv(self, model, pars=[]): 409 326 """
Note: See TracChangeset
for help on using the changeset viewer.