Changeset 3c24ccd in sasmodels


Ignore:
Timestamp:
Oct 18, 2017 12:03:43 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
ef8e68c
Parents:
fbb9397
Message:

add -weights option to sascomp to show dispersity distribution

Location:
sasmodels
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/compare.py

    rfbb9397 r3c24ccd  
    4242from . import exception 
    4343from .data import plot_theory, empty_data1D, empty_data2D, load_data 
    44 from .direct_model import DirectModel 
     44from .direct_model import DirectModel, get_mesh 
    4545from .convert import revert_name, revert_pars, constrain_new_to_old 
    4646from .generate import FLOAT_RE 
     47from .weights import plot_weights 
    4748 
    4849try: 
     
    102103    -abs/-rel* plot relative or absolute error 
    103104    -title="note" adds note to the plot title, after the model name 
     105    -weights shows weights plots for the polydisperse parameters 
    104106 
    105107    === output options === 
     
    834836        if opts['plot']: 
    835837            limits = plot_models(opts, result, limits=limits, setnum=k) 
     838        if opts['show_weights']: 
     839            base, _ = opts['engines'] 
     840            base_pars, _ = opts['pars'] 
     841            model_info = base._kernel.info 
     842            dim = base._kernel.dim 
     843            plot_weights(model_info, get_mesh(model_info, base_pars, dim=dim)) 
    836844    if opts['plot']: 
    837845        import matplotlib.pyplot as plt 
     
    9961004OPTIONS = [ 
    9971005    # Plotting 
    998     'plot', 'noplot', 
     1006    'plot', 'noplot', 'weights', 
    9991007    'linear', 'log', 'q4', 
    10001008    'rel', 'abs', 
     
    11461154        'engine'    : 'default', 
    11471155        'evals'     : '1', 
     1156        'show_weights' : False, 
    11481157    } 
    11491158    for arg in flags: 
     
    11941203        elif arg == '-demo':    opts['use_demo'] = True 
    11951204        elif arg == '-default': opts['use_demo'] = False 
     1205        elif arg == '-weights': opts['show_weights'] = True 
    11961206        elif arg == '-html':    opts['html'] = True 
    11971207        elif arg == '-help':    opts['html'] = True 
  • sasmodels/details.py

    r8698a0d r3c24ccd  
    234234    # skipping scale and background when building values and weights 
    235235    values, dispersity, weights = zip(*mesh[2:npars+2]) if npars else ((), (), ()) 
    236     #weights = correct_theta_weights(kernel.info.parameters, values, weights) 
     236    weights = correct_theta_weights(kernel.info.parameters, values, weights) 
    237237    length = np.array([len(w) for w in weights]) 
    238238    offset = np.cumsum(np.hstack((0, length))) 
  • sasmodels/direct_model.py

    r32f87a5 r3c24ccd  
    5555    *mono* is True if polydispersity should be set to none on all parameters. 
    5656    """ 
    57     parameters = calculator.info.parameters 
    58     if mono: 
    59         active = lambda name: False 
    60     elif calculator.dim == '1d': 
    61         active = lambda name: name in parameters.pd_1d 
    62     elif calculator.dim == '2d': 
    63         active = lambda name: name in parameters.pd_2d 
    64     else: 
    65         active = lambda name: True 
    66  
    67     #print("pars",[p.id for p in parameters.call_parameters]) 
    68     mesh = [get_weights(p, pars, active(p.name)) 
    69             for p in parameters.call_parameters] 
    70  
     57    mesh = get_mesh(calculator.info, pars, dim=calculator.dim, mono=mono) 
    7158    call_details, values, is_magnetic = make_kernel_args(calculator, mesh) 
    7259    #print("values:", values) 
    7360    return calculator(call_details, values, cutoff, is_magnetic) 
    74  
    7561 
    7662def call_ER(model_info, pars): 
     
    128114    return x, y, model_info.profile_axes 
    129115 
    130  
    131 def get_weights(parameter, values, active=True): 
    132     # type: (Parameter, Dict[str, float]) -> Tuple[np.ndarray, np.ndarray] 
     116def get_mesh(model_info, values, dim='1d', mono=False): 
     117    # type: (ModelInfo, Dict[str, float], str, bool) -> List[Tuple[float, np.ndarray, np.ndarry]] 
     118    """ 
     119    Retrieve the dispersity mesh described by the parameter set. 
     120 
     121    Returns a list of *(value, dispersity, weights)* with one tuple for each 
     122    parameter in the model call parameters.  Inactive parameters return the 
     123    default value with a weight of 1.0. 
     124    """ 
     125    parameters = model_info.parameters 
     126    if mono: 
     127        active = lambda name: False 
     128    elif dim == '1d': 
     129        active = lambda name: name in parameters.pd_1d 
     130    elif dim == '2d': 
     131        active = lambda name: name in parameters.pd_2d 
     132    else: 
     133        active = lambda name: True 
     134 
     135    #print("pars",[p.id for p in parameters.call_parameters]) 
     136    mesh = [_get_par_weights(p, values, active(p.name)) 
     137            for p in parameters.call_parameters] 
     138    return mesh 
     139 
     140 
     141def _get_par_weights(parameter, values, active=True): 
     142    # type: (Parameter, Dict[str, float]) -> Tuple[float, np.ndarray, np.ndarray] 
    133143    """ 
    134144    Generate the distribution for parameter *name* given the parameter values 
  • sasmodels/weights.py

    r8698a0d r3c24ccd  
    232232 
    233233 
    234 def plot_weights(model_info, pairs): 
    235     # type: (ModelInfo, List[Tuple[np.ndarray, np.ndarray]]) -> None 
     234def plot_weights(model_info, mesh): 
     235    # type: (ModelInfo, List[Tuple[float, np.ndarray, np.ndarray]]) -> None 
    236236    """ 
    237237    Plot the weights returned by :func:`get_weights`. 
    238238 
    239     *model_info* is 
    240     :param model_info: 
    241     :param pairs: 
    242     :return: 
     239    *model_info* defines model parameters, etc. 
     240 
     241    *mesh* is a list of tuples containing (*value*, *dispersity*, *weights*) 
     242    for each parameter, where (*dispersity*, *weights*) pairs are the 
     243    distributions to be plotted. 
    243244    """ 
    244245    import pylab 
    245246 
    246     if any(len(values)>1 for values, weights in pairs): 
     247    if any(len(dispersity)>1 for value, dispersity, weights in mesh): 
    247248        labels = [p.name for p in model_info.parameters.call_parameters] 
    248         pylab.interactive(True) 
     249        #pylab.interactive(True) 
    249250        pylab.figure() 
    250         for (v,w), s in zip(pairs, labels): 
    251             if len(v) > 1: 
    252                 #print("weights for", s, v, w) 
    253                 pylab.plot(v, w, '-o', label=s) 
     251        for (v,x,w), s in zip(mesh, labels): 
     252            if len(x) > 1: 
     253                pylab.plot(x, w, '-o', label=s) 
    254254        pylab.grid(True) 
    255255        pylab.legend() 
Note: See TracChangeset for help on using the changeset viewer.