Changeset 7e224c2 in sasmodels for sasmodels/bumps_model.py
- Timestamp:
- Mar 3, 2015 2:47:02 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:
- 1353f60
- Parents:
- 53d0e24
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/bumps_model.py
r5134b2c r7e224c2 2 2 Sasmodels core. 3 3 """ 4 import sys, os5 4 import datetime 6 5 … … 9 8 # CRUFT python 2.6 10 9 if not hasattr(datetime.timedelta, 'total_seconds'): 11 def delay(dt): return dt.days *86400 + dt.seconds + 1e-6*dt.microseconds10 def delay(dt): return dt.days * 86400 + dt.seconds + 1e-6 * dt.microseconds 12 11 else: 13 12 def delay(dt): return dt.total_seconds() … … 17 16 try: 18 17 from .kernelcl import load_model as _loader 19 except RuntimeError, exc:18 except RuntimeError, exc: 20 19 import warnings 21 20 warnings.warn(str(exc)) … … 27 26 Load model by name. 28 27 """ 29 sasmodels = __import__('sasmodels.models.' +modelname)28 sasmodels = __import__('sasmodels.models.' + modelname) 30 29 module = getattr(sasmodels.models, modelname, None) 31 30 model = _loader(module, dtype=dtype) … … 41 40 """ 42 41 then = datetime.datetime.now() 43 return lambda: delay(datetime.datetime.now() -then)42 return lambda: delay(datetime.datetime.now() - then) 44 43 45 44 … … 52 51 data = loader.load(filename) 53 52 if data is None: 54 raise IOError("Data %r could not be loaded" %filename)53 raise IOError("Data %r could not be loaded" % filename) 55 54 return data 56 55 … … 65 64 from sas.dataloader.data_info import Data1D 66 65 67 Iq = 100 *np.ones_like(q)66 Iq = 100 * np.ones_like(q) 68 67 dIq = np.sqrt(Iq) 69 data = Data1D(q, Iq, dx=0.05 *q, dy=dIq)68 data = Data1D(q, Iq, dx=0.05 * q, dy=dIq) 70 69 data.filename = "fake data" 71 70 data.qmin, data.qmax = q.min(), q.max() … … 85 84 if qy is None: 86 85 qy = qx 87 Qx, Qy = np.meshgrid(qx,qy)88 Qx, Qy = Qx.flatten(), Qy.flatten()89 Iq = 100 *np.ones_like(Qx)86 Qx, Qy = np.meshgrid(qx, qy) 87 Qx, Qy = Qx.flatten(), Qy.flatten() 88 Iq = 100 * np.ones_like(Qx) 90 89 dIq = np.sqrt(Iq) 91 90 mask = np.ones(len(Iq), dtype='bool') … … 100 99 101 100 # 5% dQ/Q resolution 102 data.dqx_data = 0.05 *Qx103 data.dqy_data = 0.05 *Qy101 data.dqx_data = 0.05 * Qx 102 data.dqy_data = 0.05 * Qy 104 103 105 104 detector = Detector() … … 114 113 data.Q_unit = "1/A" 115 114 data.I_unit = "1/cm" 116 data.q_data = np.sqrt(Qx **2 + Qy**2)115 data.q_data = np.sqrt(Qx ** 2 + Qy ** 2) 117 116 data.xaxis("Q_x", "A^{-1}") 118 117 data.yaxis("Q_y", "A^{-1}") … … 129 128 data.mask = Ringcut(0, radius)(data) 130 129 if outer is not None: 131 data.mask += Ringcut(outer, np.inf)(data)130 data.mask += Ringcut(outer, np.inf)(data) 132 131 else: 133 data.mask = (data.x >=radius)132 data.mask = (data.x >= radius) 134 133 if outer is not None: 135 data.mask &= (data.x <outer)134 data.mask &= (data.x < outer) 136 135 137 136 … … 165 164 import matplotlib.pyplot as plt 166 165 if hasattr(data, 'qx_data'): 167 iq = iq +0166 iq = iq + 0 168 167 valid = np.isfinite(iq) 169 168 if scale == 'log': 170 169 valid[valid] = (iq[valid] > 0) 171 170 iq[valid] = np.log10(iq[valid]) 172 iq[~valid |data.mask] = 0171 iq[~valid | data.mask] = 0 173 172 #plottable = iq 174 plottable = masked_array(iq, ~valid |data.mask)173 plottable = masked_array(iq, ~valid | data.mask) 175 174 xmin, xmax = min(data.qx_data), max(data.qx_data) 176 175 ymin, ymax = min(data.qy_data), max(data.qy_data) 177 176 try: 178 if vmin is None: vmin = iq[valid &~data.mask].min()179 if vmax is None: vmax = iq[valid &~data.mask].max()177 if vmin is None: vmin = iq[valid & ~data.mask].min() 178 if vmax is None: vmax = iq[valid & ~data.mask].max() 180 179 except: 181 180 vmin, vmax = 0, 1 182 plt.imshow(plottable.reshape(128, 128),181 plt.imshow(plottable.reshape(128, 128), 183 182 interpolation='nearest', aspect=1, origin='upper', 184 183 extent=[xmin, xmax, ymin, ymax], vmin=vmin, vmax=vmax) … … 189 188 else: 190 189 idx = np.isfinite(iq) 191 idx[idx] = (iq[idx] >0)190 idx[idx] = (iq[idx] > 0) 192 191 plt.loglog(data.x[idx], iq[idx]) 193 192 … … 206 205 mdata[mdata <= 0] = masked 207 206 mtheory = masked_array(theory, mdata.mask) 208 mresid = masked_array((theory -data.y)/data.dy, mdata.mask)207 mresid = masked_array((theory - data.y) / data.dy, mdata.mask) 209 208 210 209 plt.subplot(121) … … 214 213 plt.subplot(122) 215 214 plt.plot(data.x, mresid, 'x') 216 #plt.axhline(1, color='black', ls='--',lw=1, hold=True)217 #plt.axhline(0, color='black', lw=1, hold=True)218 #plt.axhline(-1, color='black', ls='--',lw=1, hold=True)219 215 220 216 def _plot_sesans(data, theory, view): 221 217 import matplotlib.pyplot as plt 222 resid = (theory - data.y) /data.dy218 resid = (theory - data.y) / data.dy 223 219 plt.subplot(121) 224 220 plt.errorbar(data.x, data.y, yerr=data.dy) … … 236 232 """ 237 233 import matplotlib.pyplot as plt 238 resid = (theory -data.data)/data.err_data234 resid = (theory - data.data) / data.err_data 239 235 plt.subplot(131) 240 236 plot_data(data, data.data, scale=view) … … 270 266 self.model = model 271 267 self.cutoff = cutoff 272 # TODO if isinstance(data,SESANSData1D) 268 # TODO if isinstance(data,SESANSData1D) 273 269 if hasattr(data, 'lam'): 274 270 self.data_type = 'sesans' … … 283 279 if self.data_type == 'sesans': 284 280 q = sesans.make_q(data.sample.zacceptance, data.Rmax) 285 self.index = slice(None, None)281 self.index = slice(None, None) 286 282 self.iq = data.y 287 283 self.diq = data.dy … … 289 285 q_vectors = [q] 290 286 elif self.data_type == 'Iqxy': 291 self.index = (data.mask ==0) & (~np.isnan(data.data))287 self.index = (data.mask == 0) & (~np.isnan(data.data)) 292 288 self.iq = data.data[self.index] 293 289 self.diq = data.err_data[self.index] 294 290 self._theory = np.zeros_like(data.data) 295 291 if not partype['orientation'] and not partype['magnetic']: 296 q_vectors = [np.sqrt(data.qx_data **2+data.qy_data**2)]292 q_vectors = [np.sqrt(data.qx_data ** 2 + data.qy_data ** 2)] 297 293 else: 298 294 q_vectors = [data.qx_data, data.qy_data] 299 295 elif self.data_type == 'Iq': 300 self.index = (data.x >=data.qmin) & (data.x<=data.qmax) & ~np.isnan(data.y)296 self.index = (data.x >= data.qmin) & (data.x <= data.qmax) & ~np.isnan(data.y) 301 297 self.iq = data.y[self.index] 302 298 self.diq = data.dy[self.index] … … 319 315 pars.append(name) 320 316 for name in partype['pd-2d']: 321 for xpart, xdefault,xlimits in [317 for xpart, xdefault, xlimits in [ 322 318 ('_pd', 0, limits), 323 ('_pd_n', 35, (0, 1000)),319 ('_pd_n', 35, (0, 1000)), 324 320 ('_pd_nsigma', 3, (0, 10)), 325 321 ('_pd_type', 'gaussian', None), 326 322 ]: 327 xname = name +xpart323 xname = name + xpart 328 324 xvalue = kw.pop(xname, xdefault) 329 325 if xlimits is not None: … … 333 329 self._parameter_names = pars 334 330 if kw: 335 raise TypeError("unexpected parameters: %s" %(", ".join(sorted(kw.keys()))))331 raise TypeError("unexpected parameters: %s" % (", ".join(sorted(kw.keys())))) 336 332 self.update() 337 333 … … 340 336 341 337 def numpoints(self): 338 """ 339 Return the number of points 340 """ 342 341 return len(self.iq) 343 342 344 343 def parameters(self): 345 return dict((k,getattr(self,k)) for k in self._parameter_names) 344 """ 345 Return a dictionary of parameters 346 """ 347 return dict((k, getattr(self, k)) for k in self._parameter_names) 346 348 347 349 def theory(self): 348 350 if 'theory' not in self._cache: 349 351 if self._fn is None: 350 input = self.model.make_input(self._fn_inputs)351 self._fn = self.model(input )352 353 fixed_pars = [getattr(self, p).value for p in self._fn.fixed_pars]352 input_value = self.model.make_input(self._fn_inputs) 353 self._fn = self.model(input_value) 354 355 fixed_pars = [getattr(self, p).value for p in self._fn.fixed_pars] 354 356 pd_pars = [self._get_weights(p) for p in self._fn.pd_pars] 355 357 #print fixed_pars,pd_pars … … 357 359 #self._theory[:] = self._fn.eval(pars, pd_pars) 358 360 if self.data_type == 'sesans': 359 P = sesans.hankel(self.data.x, self.data.lam *1e-9,360 self.data.sample.thickness /10, self._fn_inputs[0],361 P = sesans.hankel(self.data.x, self.data.lam * 1e-9, 362 self.data.sample.thickness / 10, self._fn_inputs[0], 361 363 self._theory) 362 364 self._cache['theory'] = P … … 367 369 def residuals(self): 368 370 #if np.any(self.err ==0): print "zeros in err" 369 return (self.theory()[self.index] -self.iq)/self.diq371 return (self.theory()[self.index] - self.iq) / self.diq 370 372 371 373 def nllf(self): 372 374 R = self.residuals() 373 375 #if np.any(np.isnan(R)): print "NaN in residuals" 374 return 0.5 *np.sum(R**2)376 return 0.5 * np.sum(R ** 2) 375 377 376 378 def __call__(self): 377 return 2 *self.nllf()/self.dof379 return 2 * self.nllf() / self.dof 378 380 379 381 def plot(self, view='log'): … … 396 398 noise = self.diq[self.index] 397 399 else: 398 noise = 0.01 *noise400 noise = 0.01 * noise 399 401 self.diq[self.index] = noise 400 402 y = self.theory() 401 y += y *np.random.randn(*y.shape)*noise403 y += y * np.random.randn(*y.shape) * noise 402 404 if self.data_type == 'Iq': 403 405 self.data.y[self.index] = y … … 413 415 414 416 def _get_weights(self, par): 417 """ 418 Get parameter dispersion weights 419 """ 415 420 from . import weights 416 421 417 422 relative = self.model.info['partype']['pd-rel'] 418 423 limits = self.model.info['limits'] 419 disperser, value,npts,width,nsigma = [getattr(self, par+ext)420 for ext in ('_pd_type','','_pd_n','_pd','_pd_nsigma')]421 v, w = weights.get_weights(424 disperser, value, npts, width, nsigma = \ 425 [getattr(self, par + ext) for ext in ('_pd_type', '', '_pd_n', '_pd', '_pd_nsigma')] 426 v, w = weights.get_weights( 422 427 disperser, int(npts.value), width.value, nsigma.value, 423 428 value.value, limits[par], par in relative) 424 return v, w/w.max()429 return v, w / w.max() 425 430 426 431 def __getstate__(self):
Note: See TracChangeset
for help on using the changeset viewer.