Changeset aa25fc7 in sasmodels
- Timestamp:
- May 23, 2018 3:55:27 PM (7 years ago)
- Branches:
- master
- Children:
- 910c0f4
- Parents:
- 33969b6
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/guide/pd/polydispersity.rst
r29afc50 raa25fc7 28 28 sigmas $N_\sigma$ to include from the tails of the distribution, and the 29 29 number of points used to compute the average. The center of the distribution 30 is set by the value of the model parameter. The meaning of a polydispersity 31 parameter *PD* (not to be confused with a molecular weight distributions 32 in polymer science) in a model depends on the type of parameter it is being 30 is set by the value of the model parameter. The meaning of a polydispersity 31 parameter *PD* (not to be confused with a molecular weight distributions 32 in polymer science) in a model depends on the type of parameter it is being 33 33 applied too. 34 34 35 The distribution width applied to *volume* (ie, shape-describing) parameters 36 is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$. 37 However, the distribution width applied to *orientation* (ie, angle-describing) 35 The distribution width applied to *volume* (ie, shape-describing) parameters 36 is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$. 37 However, the distribution width applied to *orientation* (ie, angle-describing) 38 38 parameters is just $\sigma = \mathrm{PD}$. 39 39 … … 73 73 or angular orientations, use the Gaussian or Boltzmann distributions. 74 74 75 If applying polydispersion to parameters describing angles, use the Uniform 76 distribution. Beware of using distributions that are always positive (eg, the 75 If applying polydispersion to parameters describing angles, use the Uniform 76 distribution. Beware of using distributions that are always positive (eg, the 77 77 Lognormal) because angles can be negative! 78 78 … … 315 315 .. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 316 316 317 User-defined Distributions 318 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 319 320 You can define your own distribution by creating a python file defining a 321 *Distribution* object. The distribution is parameterized by *center* 322 (which is always zero for orientation dispersity, or parameter value for 323 size dispersity), *sigma* (which is the distribution width in degrees for 324 orientation parameters, or center times width for size dispersity), and 325 bounds *lb* and *ub* (which are the bounds on the possible values of the 326 parameter given in the model definition). 327 328 For example, the following wraps the Laplace distribution from scipy stats:: 329 330 import numpy as np 331 from scipy.stats import laplace 332 333 from sasmodels import weights 334 335 class Dispersion(weights.Dispersion): 336 r""" 337 Laplace distribution 338 339 .. math:: 340 341 w(x) = e^{-\sigma |x - \mu|} 342 """ 343 type = "laplace" 344 default = dict(npts=35, width=0, nsigmas=3) # default values 345 def _weights(self, center, sigma, lb, ub): 346 x = self._linspace(center, sigma, lb, ub) 347 wx = laplace.pdf(x, center, sigma) 348 return x, wx 349 350 To see that the distribution is correct use the following:: 351 352 from numpy import inf 353 from matplotlib import pyplot as plt 354 from sasmodels import weights 355 356 # reload the user-defined weights 357 weights.load_weights() 358 x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50, 359 limits=[0, inf], relative=True) 360 361 # plot the weights 362 plt.interactive(True) 363 plt.plot(x, wx, 'x') 364 365 Any python code can be used to define the distribution. The distribution 366 parameters are available as *self.npts*, *self.width* and *self.nsigmas*. 367 Try to follow the convention of gaussian width, npts and number of sigmas 368 in the tail, but if your distribution requires more parameters you are free 369 to interpret them as something else. In particular, npts allows you to 370 trade accuracy against running time when evaluating your models. The 371 *self._linspace* function uses *self.npts* and *self.nsigmas* to define 372 the set of *x* values to use for the distribution (along with the *center*, 373 *sigma*, *lb*, and *ub* passed as parameters). You can use an arbitrary 374 set of *x* points. 375 376 .. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 377 317 378 Note about DLS polydispersity 318 379 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -
sasmodels/compare.py
r1fbadb2 raa25fc7 41 41 from . import kerneldll 42 42 from . import kernelcl 43 from . import weights 43 44 from .data import plot_theory, empty_data1D, empty_data2D, load_data 44 45 from .direct_model import DirectModel, get_mesh 45 46 from .generate import FLOAT_RE, set_integration_size 46 from .weights import plot_weights47 47 48 48 # pylint: disable=unused-import … … 771 771 model_info = base._kernel.info 772 772 dim = base._kernel.dim 773 plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim))773 weights.plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) 774 774 if opts['plot']: 775 775 import matplotlib.pyplot as plt … … 1353 1353 #import pprint; pprint.pprint(model_info) 1354 1354 1355 # Hack to load user-defined distributions; run through all parameters 1356 # and make sure any pd_type parameter is a defined distribution. 1357 if (any(p.endswith('pd_type') and v not in weights.MODELS 1358 for p, v in pars.items()) or 1359 any(p.endswith('pd_type') and v not in weights.MODELS 1360 for p, v in pars2.items())): 1361 weights.load_weights() 1362 1355 1363 if opts['show_pars']: 1356 1364 if model_info.name != model_info2.name or pars != pars2: -
sasmodels/sasview_model.py
rd533590 raa25fc7 30 30 from . import modelinfo 31 31 from .details import make_kernel_args, dispersion_mesh 32 33 # Hack: load in any custom distributions 34 # Uses ~/.sasview/weights/*.py unless SASMODELS_WEIGHTS is set in the environ. 35 # Override with weights.load_weights(pattern="<weights_path>/*.py") 36 weights.load_weights() 32 37 33 38 # pylint: disable=unused-import -
sasmodels/weights.py
r3d58247 raa25fc7 231 231 )) 232 232 233 SASMODELS_WEIGHTS = "~/.sasview/weights/*.py" 234 def load_weights(pattern=None): 235 # type: (str) -> None 236 """ 237 Load dispersion distributions matching the given pattern 238 """ 239 import logging 240 import os 241 import os.path 242 import glob 243 import traceback 244 from .custom import load_custom_kernel_module 245 if pattern is None: 246 pattern = os.environ.get("SASMODELS_WEIGHTS", SASMODELS_WEIGHTS) 247 for filename in sorted(glob.glob(os.path.expanduser(pattern))): 248 try: 249 print("loading weights from", filename) 250 module = load_custom_kernel_module(filename) 251 MODELS[module.Dispersion.type] = module.Dispersion 252 except Exception as exc: 253 logging.error(traceback.format_exc(exc)) 233 254 234 255 def get_weights(disperser, n, width, nsigmas, value, limits, relative):
Note: See TracChangeset
for help on using the changeset viewer.