Changes in / [9acade6:5fd684d] in sasmodels


Ignore:
Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    r9c1a59c r3bcb88c  
    565565        parameters = self._model_info.parameters 
    566566        pairs = [self._get_weights(p) for p in parameters.call_parameters] 
    567         #weights.plot_weights(self._model_info, pairs) 
    568567        call_details, values, is_magnetic = make_kernel_args(calculator, pairs) 
    569568        #call_details.show() 
     
    619618            # remember them is kind of funky. 
    620619            # Note: can't seem to get disperser parameters from sasview 
    621             # (1) Could create a sasview model that has not yet been 
     620            # (1) Could create a sasview model that has not yet # been 
    622621            # converted, assign the disperser to one of its polydisperse 
    623622            # parameters, then retrieve the disperser parameters from the 
    624             # sasview model. 
    625             # (2) Could write a disperser parameter retriever in sasview. 
    626             # (3) Could modify sasview to use sasmodels.weights dispersers. 
     623            # sasview model.  (2) Could write a disperser parameter retriever 
     624            # in sasview.  (3) Could modify sasview to use sasmodels.weights 
     625            # dispersers. 
    627626            # For now, rely on the fact that the sasview only ever uses 
    628627            # new dispersers in the set_dispersion call and create a new 
    629628            # one instead of trying to assign parameters. 
     629            dispersion = weights.MODELS[dispersion.type]() 
    630630            self.dispersion[parameter] = dispersion.get_pars() 
    631631        else: 
     
    658658        elif par.polydisperse: 
    659659            dis = self.dispersion[par.name] 
    660             if dis['type'] == 'array': 
    661                 value, weight = dis['values'], dis['weights'] 
    662             else: 
    663                 value, weight = weights.get_weights( 
    664                     dis['type'], dis['npts'], dis['width'], dis['nsigmas'], 
    665                     self.params[par.name], par.limits, par.relative_pd) 
     660            value, weight = weights.get_weights( 
     661                dis['type'], dis['npts'], dis['width'], dis['nsigmas'], 
     662                self.params[par.name], par.limits, par.relative_pd) 
    666663            return value, weight / np.sum(weight) 
    667664        else: 
  • sasmodels/weights.py

    r6cefbc9 rfa800e72  
    33""" 
    44# TODO: include dispersion docs with the disperser models 
    5 from __future__ import division, print_function 
    6  
    75from math import sqrt  # type: ignore 
    8 from collections import OrderedDict 
    9  
    106import numpy as np  # type: ignore 
    117from scipy.special import gammaln  # type: ignore 
     
    5854            else: 
    5955                return np.array([], 'd'), np.array([], 'd') 
    60         x, px = self._weights(center, sigma, lb, ub) 
    61         return x, px 
     56        return self._weights(center, sigma, lb, ub) 
    6257 
    6358    def _weights(self, center, sigma, lb, ub): 
     
    8479    default = dict(npts=35, width=0, nsigmas=3) 
    8580    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 
    8981        x = self._linspace(center, sigma, lb, ub) 
    9082        px = np.exp((x-center)**2 / (-2.0 * sigma * sigma)) 
     
    173165 
    174166    def _weights(self, center, sigma, lb, ub): 
    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 
     167        # TODO: interpolate into the array dispersion using npts 
     168        x = center + self.values*sigma 
    179169        idx = (x >= lb) & (x <= ub) 
    180170        x = x[idx] 
     
    184174 
    185175# dispersion name -> disperser lookup table. 
    186 # Maintain order since this is used by sasview GUI to order the options in 
    187 # the dispersion type combobox. 
    188 MODELS = OrderedDict((d.type, d) for d in ( 
    189     RectangleDispersion, 
    190     ArrayDispersion, 
    191     LogNormalDispersion, 
    192     GaussianDispersion, 
    193     SchulzDispersion, 
     176MODELS = dict((d.type, d) for d in ( 
     177    GaussianDispersion, RectangleDispersion, 
     178    ArrayDispersion, SchulzDispersion, LogNormalDispersion 
    194179)) 
    195180 
     
    209194    *value* is the value of the parameter in the model. 
    210195 
    211     *limits* is [lb, ub], the lower and upper bound on the possible values. 
     196    *limits* is [lb, ub], the lower and upper bound of the weight value. 
    212197 
    213198    *relative* is true if *width* is defined in proportion to the value 
     
    216201    Returns *(value, weight)*, where *value* and *weight* are vectors. 
    217202    """ 
    218     if disperser == "array": 
    219         raise NotImplementedError("Don't handle arrays through get_weights; use values and weights directly") 
    220203    cls = MODELS[disperser] 
    221204    obj = cls(n, width, nsigmas) 
    222205    v, w = obj.get_weights(value, limits[0], limits[1], relative) 
    223206    return v, w 
    224  
    225  
    226 def 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.