Changes in / [01dba26:2c12061] in sasmodels


Ignore:
Files:
5 deleted
4 edited

Legend:

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

    rd089a00 rd089a00  
    3434Each distribution is characterized by a center value $\bar x$ or 
    3535$x_\text{med}$, a width parameter $\sigma$ (note this is *not necessarily* 
    36 <<<<<<< HEAD 
    37 the standard deviation, so read the description carefully), the number of 
    38 sigmas $N_\sigma$ to include from the tails of the distribution, and the 
    39 number of points used to compute the average. The center of the distribution 
    40 is set by the value of the model parameter. The meaning of a polydispersity 
    41 parameter *PD* (not to be confused with a molecular weight distributions 
    42 in polymer science) in a model depends on the type of parameter it is being 
    43 applied too. 
    44  
    45 The distribution width applied to *volume* (ie, shape-describing) parameters 
    46 is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$. 
    47 However, the distribution width applied to *orientation* (ie, angle-describing) 
    48 parameters is just $\sigma = \mathrm{PD}$. 
    49 ======= 
    5036the standard deviation, so read the description of the distribution carefully),  
    5137the number of sigmas $N_\sigma$ to include from the tails of the distribution,  
     
    5743However, the distribution width applied to *orientation* parameters is just  
    5844$\sigma = \mathrm{PD}$. 
    59 >>>>>>> master 
    6045 
    6146$N_\sigma$ determines how far into the tails to evaluate the distribution, 
     
    8166*  *Schulz Distribution* 
    8267*  *Array Distribution* 
    83 *  *User-defined Distributions* 
    8468 
    8569These are all implemented as *number-average* distributions. 
    8670 
     71Additional distributions are under consideration. 
    8772 
    8873**Beware: when the Polydispersity & Orientational Distribution panel in SasView is** 
     
    10792or angular orientations, consider using the Gaussian or Boltzmann distributions. 
    10893 
    109 If applying polydispersion to parameters describing angles, use the Uniform 
    110 distribution. Beware of using distributions that are always positive (eg, the 
     94If applying polydispersion to parameters describing angles, use the Uniform  
     95distribution. Beware of using distributions that are always positive (eg, the  
    11196Lognormal) because angles can be negative! 
    11297 
    113 The array distribution provides a very simple means of implementing a user- 
    114 defined distribution, but without any fittable parameters. Greater flexibility 
    115 is conferred by the user-defined distribution.  
     98The array distribution allows a user-defined distribution to be applied. 
    11699 
    117100.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
     
    351334.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
    352335 
    353 User-defined Distributions 
    354 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    355  
    356 You can also define your own distribution by creating a python file defining a 
    357 *Distribution* object with a *_weights* method.  The *_weights* method takes 
    358 *center*, *sigma*, *lb* and *ub* as arguments, and can access *self.npts* 
    359 and *self.nsigmas* from the distribution.  They are interpreted as follows: 
    360  
    361 * *center* the value of the shape parameter (for size dispersity) or zero 
    362   if it is an angular dispersity.  This parameter may be fitted. 
    363  
    364 * *sigma* the width of the distribution, which is the polydispersity parameter 
    365   times the center for size dispersity, or the polydispersity parameter alone 
    366   for angular dispersity.  This parameter may be fitted. 
    367  
    368 * *lb*, *ub* are the parameter limits (lower & upper bounds) given in the model 
    369   definition file.  For example, a radius parameter has *lb* equal to zero.  A 
    370   volume fraction parameter would have *lb* equal to zero and *ub* equal to one. 
    371  
    372 * *self.nsigmas* the distance to go into the tails when evaluating the 
    373   distribution.  For a two parameter distribution, this value could be 
    374   co-opted to use for the second parameter, though it will not be available 
    375   for fitting. 
    376  
    377 * *self.npts* the number of points to use when evaluating the distribution. 
    378   The user will adjust this to trade calculation time for accuracy, but the 
    379   distribution code is free to return more or fewer, or use it for the third 
    380   parameter in a three parameter distribution. 
    381  
    382 As an example, the code following wraps the Laplace distribution from scipy stats:: 
    383  
    384     import numpy as np 
    385     from scipy.stats import laplace 
    386  
    387     from sasmodels import weights 
    388  
    389     class Dispersion(weights.Dispersion): 
    390         r""" 
    391         Laplace distribution 
    392  
    393         .. math:: 
    394  
    395             w(x) = e^{-\sigma |x - \mu|} 
    396         """ 
    397         type = "laplace" 
    398         default = dict(npts=35, width=0, nsigmas=3)  # default values 
    399         def _weights(self, center, sigma, lb, ub): 
    400             x = self._linspace(center, sigma, lb, ub) 
    401             wx = laplace.pdf(x, center, sigma) 
    402             return x, wx 
    403  
    404 You can plot the weights for a given value and width using the following:: 
    405  
    406     from numpy import inf 
    407     from matplotlib import pyplot as plt 
    408     from sasmodels import weights 
    409  
    410     # reload the user-defined weights 
    411     weights.load_weights() 
    412     x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50, 
    413                                 limits=[0, inf], relative=True) 
    414  
    415     # plot the weights 
    416     plt.interactive(True) 
    417     plt.plot(x, wx, 'x') 
    418  
    419 The *self.nsigmas* and *self.npts* parameters are normally used to control 
    420 the accuracy of the distribution integral. The *self._linspace* function 
    421 uses them to define the *x* values (along with the *center*, *sigma*, 
    422 *lb*, and *ub* which are passed as parameters).  If you repurpose npts or 
    423 nsigmas you will need to generate your own *x*.  Be sure to honour the 
    424 limits *lb* and *ub*, for example to disallow a negative radius or constrain 
    425 the volume fraction to lie between zero and one. 
    426  
    427 To activate a user-defined distribution, put it in a file such as *distname.py* 
    428 in the *SAS_WEIGHTS_PATH* folder.  This is defined with an environment 
    429 variable, defaulting to:: 
    430  
    431     SAS_WEIGHTS_PATH=~/.sasview/weights 
    432  
    433 The weights path is loaded on startup.  To update the distribution definition 
    434 in a running application you will need to enter the following python commands:: 
    435  
    436     import sasmodels.weights 
    437     sasmodels.weights.load_weights('path/to/distname.py') 
    438  
    439 .. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
    440  
    441336Note about DLS polydispersity 
    442337^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
  • sasmodels/compare.py

    rbd7630d rbd7630d  
    4141from . import kerneldll 
    4242from . import kernelcl 
    43 from . import weights 
    4443from .data import plot_theory, empty_data1D, empty_data2D, load_data 
    4544from .direct_model import DirectModel, get_mesh 
    4645from .generate import FLOAT_RE, set_integration_size 
     46from .weights import plot_weights 
    4747 
    4848# pylint: disable=unused-import 
     
    114114 
    115115    === environment variables === 
    116     -DSAS_MODELPATH=~/.sasmodels/custom_models sets path to custom models 
    117     -DSAS_WEIGHTS_PATH=~/.sasview/weights sets path to custom distributions 
     116    -DSAS_MODELPATH=path sets directory containing custom models 
    118117    -DSAS_OPENCL=vendor:device|none sets the target OpenCL device 
    119118    -DXDG_CACHE_HOME=~/.cache sets the pyopencl cache root (linux only) 
    120119    -DSAS_COMPILER=tinycc|msvc|mingw|unix sets the DLL compiler 
    121     -DSAS_OPENMP=0 set to 1 to turn on OpenMP for the DLLs 
    122     -DSAS_DLL_PATH=~/.sasmodels/compiled_models sets the DLL cache 
     120    -DSAS_OPENMP=1 turns on OpenMP for the DLLs 
     121    -DSAS_DLL_PATH=path sets the path to the compiled modules 
    123122 
    124123The interpretation of quad precision depends on architecture, and may 
     
    784783            model_info = base._kernel.info 
    785784            dim = base._kernel.dim 
    786             weights.plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) 
     785            plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) 
    787786        if opts['show_profile']: 
    788787            import pylab 
     
    14411440    #import pprint; pprint.pprint(model_info) 
    14421441 
    1443     # Hack to load user-defined distributions; run through all parameters 
    1444     # and make sure any pd_type parameter is a defined distribution. 
    1445     if (any(p.endswith('pd_type') and v not in weights.MODELS 
    1446             for p, v in pars.items()) or 
    1447         any(p.endswith('pd_type') and v not in weights.MODELS 
    1448             for p, v in pars2.items())): 
    1449        weights.load_weights() 
    1450  
    14511442    if opts['show_pars']: 
    14521443        if model_info.name != model_info2.name or pars != pars2: 
  • sasmodels/sasview_model.py

    raa25fc7 rd533590  
    3030from . import modelinfo 
    3131from .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() 
    3732 
    3833# pylint: disable=unused-import 
  • sasmodels/weights.py

    rf41027b r3d58247  
    231231)) 
    232232 
    233 SAS_WEIGHTS_PATH = "~/.sasview/weights" 
    234 def load_weights(pattern=None): 
    235     # type: (str) -> None 
    236     """ 
    237     Load dispersion distributions matching the given glob 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         path = os.environ.get("SAS_WEIGHTS_PATH", SAS_WEIGHTS_PATH) 
    247         pattern = os.path.join(path, "*.py") 
    248     for filename in sorted(glob.glob(os.path.expanduser(pattern))): 
    249         try: 
    250             #print("loading weights from", filename) 
    251             module = load_custom_kernel_module(filename) 
    252             MODELS[module.Dispersion.type] = module.Dispersion 
    253         except Exception as exc: 
    254             logging.error(traceback.format_exc(exc)) 
    255233 
    256234def get_weights(disperser, n, width, nsigmas, value, limits, relative): 
Note: See TracChangeset for help on using the changeset viewer.