Changeset 3c56da87 in sasmodels for sasmodels/bumps_model.py
- Timestamp:
- Mar 4, 2015 10:55:38 PM (9 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 3a45c2c
- Parents:
- b89f519
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/bumps_model.py
rb89f519 r3c56da87 8 8 # CRUFT python 2.6 9 9 if not hasattr(datetime.timedelta, 'total_seconds'): 10 def delay(dt): return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds 10 def delay(dt): 11 """Return number date-time delta as number seconds""" 12 return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds 11 13 else: 12 def delay(dt): return dt.total_seconds() 14 def delay(dt): 15 """Return number date-time delta as number seconds""" 16 return dt.total_seconds() 13 17 14 18 import numpy as np … … 141 145 from sas.dataloader.manipulations import Boxcut 142 146 if half == 'right': 143 data.mask += Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) 147 data.mask += \ 148 Boxcut(x_min=-np.inf, x_max=0.0, y_min=-np.inf, y_max=np.inf)(data) 144 149 if half == 'left': 145 data.mask += Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) 146 147 148 def set_top(data, max): 149 """ 150 Chop the top off the data, above *max*. 150 data.mask += \ 151 Boxcut(x_min=0.0, x_max=np.inf, y_min=-np.inf, y_max=np.inf)(data) 152 153 154 def set_top(data, cutoff): 155 """ 156 Chop the top off the data, above *cutoff*. 151 157 """ 152 158 from sas.dataloader.manipulations import Boxcut 153 data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) 154 155 156 def plot_data(data, iq, vmin=None, vmax=None, view='log'): 159 data.mask += \ 160 Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=cutoff)(data) 161 162 163 def plot_data(data, Iq, vmin=None, vmax=None, view='log'): 157 164 """ 158 165 Plot the target value for the data. This could be the data itself, … … 161 168 *scale* can be 'log' for log scale data, or 'linear'. 162 169 """ 163 from numpy.ma import masked_array , masked170 from numpy.ma import masked_array 164 171 import matplotlib.pyplot as plt 165 172 if hasattr(data, 'qx_data'): 166 iq = iq + 0167 valid = np.isfinite( iq)173 Iq = Iq + 0 174 valid = np.isfinite(Iq) 168 175 if view == 'log': 169 valid[valid] = ( iq[valid] > 0)170 iq[valid] = np.log10(iq[valid])176 valid[valid] = (Iq[valid] > 0) 177 Iq[valid] = np.log10(Iq[valid]) 171 178 elif view == 'q4': 172 iq[valid] = iq*(data.qx_data[valid]**2+data.qy_data[valid]**2)**2173 iq[~valid | data.mask] = 0174 #plottable = iq175 plottable = masked_array( iq, ~valid | data.mask)179 Iq[valid] = Iq*(data.qx_data[valid]**2+data.qy_data[valid]**2)**2 180 Iq[~valid | data.mask] = 0 181 #plottable = Iq 182 plottable = masked_array(Iq, ~valid | data.mask) 176 183 xmin, xmax = min(data.qx_data), max(data.qx_data) 177 184 ymin, ymax = min(data.qy_data), max(data.qy_data) 178 185 try: 179 if vmin is None: vmin = iq[valid & ~data.mask].min()180 if vmax is None: vmax = iq[valid & ~data.mask].max()186 if vmin is None: vmin = Iq[valid & ~data.mask].min() 187 if vmax is None: vmax = Iq[valid & ~data.mask].max() 181 188 except: 182 189 vmin, vmax = 0, 1 … … 186 193 else: # 1D data 187 194 if view == 'linear' or view == 'q4': 188 #idx = np.isfinite( iq)195 #idx = np.isfinite(Iq) 189 196 scale = data.x**4 if view == 'q4' else 1.0 190 plt.plot(data.x, scale* iq) #, '.')197 plt.plot(data.x, scale*Iq) #, '.') 191 198 else: 192 199 # Find the values that are finite and positive 193 idx = np.isfinite( iq)194 idx[idx] = iq[idx]>0195 iq[~idx] = np.nan196 plt.loglog(data.x, iq)200 idx = np.isfinite(Iq) 201 idx[idx] = (Iq[idx] > 0) 202 Iq[~idx] = np.nan 203 plt.loglog(data.x, Iq) 197 204 198 205 … … 220 227 plt.plot(data.x, mresid, 'x') 221 228 229 # pylint: disable=unused-argument 222 230 def _plot_sesans(data, theory, view): 223 231 import matplotlib.pyplot as plt … … 286 294 q = sesans.make_q(data.sample.zacceptance, data.Rmax) 287 295 self.index = slice(None, None) 288 self. iq = data.y289 self.d iq = data.dy296 self.Iq = data.y 297 self.dIq = data.dy 290 298 self._theory = np.zeros_like(q) 291 299 q_vectors = [q] 292 300 elif self.data_type == 'Iqxy': 293 301 self.index = (data.mask == 0) & (~np.isnan(data.data)) 294 self. iq = data.data[self.index]295 self.d iq = data.err_data[self.index]302 self.Iq = data.data[self.index] 303 self.dIq = data.err_data[self.index] 296 304 self._theory = np.zeros_like(data.data) 297 305 if not partype['orientation'] and not partype['magnetic']: … … 301 309 elif self.data_type == 'Iq': 302 310 self.index = (data.x >= data.qmin) & (data.x <= data.qmax) & ~np.isnan(data.y) 303 self. iq = data.y[self.index]304 self.d iq = data.dy[self.index]311 self.Iq = data.y[self.index] 312 self.dIq = data.dy[self.index] 305 313 self._theory = np.zeros_like(data.y) 306 314 q_vectors = [data.x] … … 316 324 pars = [] 317 325 for p in model.info['parameters']: 318 name, default, limits , ptype = p[0], p[2], p[3], p[4]326 name, default, limits = p[0], p[2], p[3] 319 327 value = kw.pop(name, default) 320 328 setattr(self, name, Parameter.default(value, name=name, limits=limits)) … … 335 343 self._parameter_names = pars 336 344 if kw: 337 raise TypeError("unexpected parameters: %s" % (", ".join(sorted(kw.keys())))) 345 raise TypeError("unexpected parameters: %s" 346 % (", ".join(sorted(kw.keys())))) 338 347 self.update() 339 348 … … 345 354 Return the number of points 346 355 """ 347 return len(self. iq)356 return len(self.Iq) 348 357 349 358 def parameters(self): … … 365 374 #self._theory[:] = self._fn.eval(pars, pd_pars) 366 375 if self.data_type == 'sesans': 367 P= sesans.hankel(self.data.x, self.data.lam * 1e-9,368 self.data.sample.thickness / 10, self._fn_inputs[0],369 self._theory)370 self._cache['theory'] = P376 result = sesans.hankel(self.data.x, self.data.lam * 1e-9, 377 self.data.sample.thickness / 10, 378 self._fn_inputs[0], self._theory) 379 self._cache['theory'] = result 371 380 else: 372 381 self._cache['theory'] = self._theory … … 375 384 def residuals(self): 376 385 #if np.any(self.err ==0): print "zeros in err" 377 return (self.theory()[self.index] - self. iq) / self.diq386 return (self.theory()[self.index] - self.Iq) / self.dIq 378 387 379 388 def nllf(self): 380 R= self.residuals()389 delta = self.residuals() 381 390 #if np.any(np.isnan(R)): print "NaN in residuals" 382 return 0.5 * np.sum( R** 2)383 384 def __call__(self):385 return 2 * self.nllf() / self.dof391 return 0.5 * np.sum(delta ** 2) 392 393 #def __call__(self): 394 # return 2 * self.nllf() / self.dof 386 395 387 396 def plot(self, view='log'): … … 402 411 print "noise", noise 403 412 if noise is None: 404 noise = self.d iq[self.index]413 noise = self.dIq[self.index] 405 414 else: 406 415 noise = 0.01 * noise 407 self.d iq[self.index] = noise416 self.dIq[self.index] = noise 408 417 y = self.theory() 409 418 y += y * np.random.randn(*y.shape) * noise … … 428 437 relative = self.model.info['partype']['pd-rel'] 429 438 limits = self.model.info['limits'] 430 disperser, value, npts, width, nsigma = \ 431 [getattr(self, par + ext) for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] 432 v, w = weights.get_weights( 439 disperser, value, npts, width, nsigma = [ 440 getattr(self, par + ext) 441 for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] 442 value, weight = weights.get_weights( 433 443 disperser, int(npts.value), width.value, nsigma.value, 434 444 value.value, limits[par], par in relative) 435 return v , w / w.max()445 return value, weight / np.sum(weight) 436 446 437 447 def __getstate__(self): … … 442 452 443 453 def __setstate__(self, state): 454 # pylint: disable=attribute-defined-outside-init 444 455 self.__dict__ = state 445 456
Note: See TracChangeset
for help on using the changeset viewer.