# Changeset aa25fc7 in sasmodels

Ignore:
Timestamp:
May 23, 2018 5:55:27 PM (7 months ago)
Branches:
ticket-608-user-defined-weights
Children:
910c0f4
Parents:
33969b6
Message:

load user-defined weight functions from ~/.sasview/weights/*.py

Files:
4 edited

Unmodified
Removed
• ## doc/guide/pd/polydispersity.rst

 r29afc50 sigmas $N_\sigma$ to include from the tails of the distribution, and the number of points used to compute the average. The center of the distribution is set by the value of the model parameter. The meaning of a polydispersity parameter *PD* (not to be confused with a molecular weight distributions in polymer science) in a model depends on the type of parameter it is being is set by the value of the model parameter. The meaning of a polydispersity parameter *PD* (not to be confused with a molecular weight distributions in polymer science) in a model depends on the type of parameter it is being applied too. The distribution width applied to *volume* (ie, shape-describing) parameters is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$. However, the distribution width applied to *orientation* (ie, angle-describing) The distribution width applied to *volume* (ie, shape-describing) parameters is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$. However, the distribution width applied to *orientation* (ie, angle-describing) parameters is just $\sigma = \mathrm{PD}$. or angular orientations, use the Gaussian or Boltzmann distributions. If applying polydispersion to parameters describing angles, use the Uniform distribution. Beware of using distributions that are always positive (eg, the If applying polydispersion to parameters describing angles, use the Uniform distribution. Beware of using distributions that are always positive (eg, the Lognormal) because angles can be negative! .. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ User-defined Distributions ^^^^^^^^^^^^^^^^^^^^^^^^^^ You can define your own distribution by creating a python file defining a *Distribution* object.  The distribution is parameterized by *center* (which is always zero for orientation dispersity, or parameter value for size dispersity), *sigma* (which is the distribution width in degrees for orientation parameters, or center times width for size dispersity), and bounds *lb* and *ub* (which are the bounds on the possible values of the parameter given in the model definition). For example, the following wraps the Laplace distribution from scipy stats:: import numpy as np from scipy.stats import laplace from sasmodels import weights class Dispersion(weights.Dispersion): r""" Laplace distribution .. math:: w(x) = e^{-\sigma |x - \mu|} """ type = "laplace" default = dict(npts=35, width=0, nsigmas=3)  # default values def _weights(self, center, sigma, lb, ub): x = self._linspace(center, sigma, lb, ub) wx = laplace.pdf(x, center, sigma) return x, wx To see that the distribution is correct use the following:: from numpy import inf from matplotlib import pyplot as plt from sasmodels import weights # reload the user-defined weights weights.load_weights() x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50, limits=[0, inf], relative=True) # plot the weights plt.interactive(True) plt.plot(x, wx, 'x') Any python code can be used to define the distribution.  The distribution parameters are available as *self.npts*, *self.width* and *self.nsigmas*. Try to follow the convention of gaussian width, npts and number of sigmas in the tail, but if your distribution requires more parameters you are free to interpret them as something else.  In particular, npts allows you to trade accuracy against running time when evaluating your models.  The *self._linspace* function uses *self.npts* and *self.nsigmas* to define the set of *x* values to use for the distribution (along with the *center*, *sigma*, *lb*, and *ub* passed as parameters).  You can use an arbitrary set of *x* points. .. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ Note about DLS polydispersity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
• ## sasmodels/compare.py

 r1fbadb2 from . import kerneldll from . import kernelcl from . import weights from .data import plot_theory, empty_data1D, empty_data2D, load_data from .direct_model import DirectModel, get_mesh from .generate import FLOAT_RE, set_integration_size from .weights import plot_weights # pylint: disable=unused-import model_info = base._kernel.info dim = base._kernel.dim plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) weights.plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) if opts['plot']: import matplotlib.pyplot as plt #import pprint; pprint.pprint(model_info) # Hack to load user-defined distributions; run through all parameters # and make sure any pd_type parameter is a defined distribution. if (any(p.endswith('pd_type') and v not in weights.MODELS for p, v in pars.items()) or any(p.endswith('pd_type') and v not in weights.MODELS for p, v in pars2.items())): weights.load_weights() if opts['show_pars']: if model_info.name != model_info2.name or pars != pars2:
• ## sasmodels/sasview_model.py

 rd533590 from . import modelinfo from .details import make_kernel_args, dispersion_mesh # Hack: load in any custom distributions # Uses ~/.sasview/weights/*.py unless SASMODELS_WEIGHTS is set in the environ. # Override with weights.load_weights(pattern="/*.py") weights.load_weights() # pylint: disable=unused-import
• ## sasmodels/weights.py

 r3d58247 )) SASMODELS_WEIGHTS = "~/.sasview/weights/*.py" def load_weights(pattern=None): # type: (str) -> None """ Load dispersion distributions matching the given pattern """ import logging import os import os.path import glob import traceback from .custom import load_custom_kernel_module if pattern is None: pattern = os.environ.get("SASMODELS_WEIGHTS", SASMODELS_WEIGHTS) for filename in sorted(glob.glob(os.path.expanduser(pattern))): try: print("loading weights from", filename) module = load_custom_kernel_module(filename) MODELS[module.Dispersion.type] = module.Dispersion except Exception as exc: logging.error(traceback.format_exc(exc)) def get_weights(disperser, n, width, nsigmas, value, limits, relative):
Note: See TracChangeset for help on using the changeset viewer.