Changeset 6cefbc9 in sasmodels for sasmodels/weights.py


Ignore:
Timestamp:
Sep 27, 2016 9:52:00 AM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
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:
9acade6
Parents:
9c1a59c
Message:

preserve order of distribution list; fix array distribution handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/weights.py

    r56b2687 r6cefbc9  
    33""" 
    44# TODO: include dispersion docs with the disperser models 
     5from __future__ import division, print_function 
     6 
    57from math import sqrt  # type: ignore 
     8from collections import OrderedDict 
     9 
    610import numpy as np  # type: ignore 
    711from scipy.special import gammaln  # type: ignore 
     
    5458            else: 
    5559                return np.array([], 'd'), np.array([], 'd') 
    56         return self._weights(center, sigma, lb, ub) 
     60        x, px = self._weights(center, sigma, lb, ub) 
     61        return x, px 
    5762 
    5863    def _weights(self, center, sigma, lb, ub): 
     
    7984    default = dict(npts=35, width=0, nsigmas=3) 
    8085    def _weights(self, center, sigma, lb, ub): 
     86        # TODO: sample high probability regions more densely 
     87        # i.e., step uniformly in cumulative density rather than x value 
     88        # so weight = 1/Npts for all weights, but values are unevenly spaced 
    8189        x = self._linspace(center, sigma, lb, ub) 
    8290        px = np.exp((x-center)**2 / (-2.0 * sigma * sigma)) 
     
    165173 
    166174    def _weights(self, center, sigma, lb, ub): 
    167         # TODO: interpolate into the array dispersion using npts 
    168         x = center + self.values*sigma 
     175        # TODO: rebin the array dispersion using npts 
     176        # TODO: use a distribution that can be recentered and scaled 
     177        x = self.values 
     178        #x = center + self.values*sigma 
    169179        idx = (x >= lb) & (x <= ub) 
    170180        x = x[idx] 
     
    174184 
    175185# dispersion name -> disperser lookup table. 
    176 MODELS = dict((d.type, d) for d in ( 
    177     GaussianDispersion, RectangleDispersion, 
    178     ArrayDispersion, SchulzDispersion, LogNormalDispersion 
     186# Maintain order since this is used by sasview GUI to order the options in 
     187# the dispersion type combobox. 
     188MODELS = OrderedDict((d.type, d) for d in ( 
     189    RectangleDispersion, 
     190    ArrayDispersion, 
     191    LogNormalDispersion, 
     192    GaussianDispersion, 
     193    SchulzDispersion, 
    179194)) 
    180195 
     
    194209    *value* is the value of the parameter in the model. 
    195210 
    196     *limits* is [lb, ub], the lower and upper bound of the weight value. 
     211    *limits* is [lb, ub], the lower and upper bound on the possible values. 
    197212 
    198213    *relative* is true if *width* is defined in proportion to the value 
     
    201216    Returns *(value, weight)*, where *value* and *weight* are vectors. 
    202217    """ 
     218    if disperser == "array": 
     219        raise NotImplementedError("Don't handle arrays through get_weights; use values and weights directly") 
    203220    cls = MODELS[disperser] 
    204221    obj = cls(n, width, nsigmas) 
    205222    v, w = obj.get_weights(value, limits[0], limits[1], relative) 
    206223    return v, w 
     224 
     225 
     226def plot_weights(model_info, pairs): 
     227    # type: (ModelInfo, List[Tuple[np.ndarray, np.ndarray]]) -> None 
     228    """ 
     229    Plot the weights returned by :func:`get_weights`. 
     230 
     231    *model_info* is 
     232    :param model_info: 
     233    :param pairs: 
     234    :return: 
     235    """ 
     236    import pylab 
     237 
     238    if any(len(values)>1 for values, weights in pairs): 
     239        labels = [p.name for p in model_info.parameters.call_parameters] 
     240        pylab.interactive(True) 
     241        pylab.figure() 
     242        for (v,w), s in zip(pairs, labels): 
     243            if len(v) > 1: 
     244                #print("weights for", s, v, w) 
     245                pylab.plot(v, w, '-o', label=s) 
     246        pylab.grid(True) 
     247        pylab.legend() 
Note: See TracChangeset for help on using the changeset viewer.