Changeset fcd7bbd in sasmodels


Ignore:
Timestamp:
Mar 16, 2016 9:27:13 PM (9 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:
70bbb74
Parents:
63776d3
Message:

use named tuple for parameter information

Location:
sasmodels
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/bumps_model.py

    r190fc2b rfcd7bbd  
    8383    pars = {} 
    8484    for p in model_info['parameters']: 
    85         name, default, limits = p[0], p[2], p[3] 
    86         value = kwargs.pop(name, default) 
    87         pars[name] = Parameter.default(value, name=name, limits=limits) 
     85        value = kwargs.pop([p.name, p.default]) 
     86        pars[p.name] = Parameter.default(value, name=p.name, limits=p.limits) 
    8887    for name in model_info['partype']['pd-2d']: 
    8988        for xpart, xdefault, xlimits in [ 
    90                 ('_pd', 0., limits), 
     89                ('_pd', 0., pars[name].limits), 
    9190                ('_pd_n', 35., (0, 1000)), 
    9291                ('_pd_nsigma', 3., (0, 10)), 
  • sasmodels/compare.py

    r91c5fdc rfcd7bbd  
    681681    """ 
    682682    # Get the default values for the parameters 
    683     pars = dict((p[0], p[2]) for p in model_info['parameters']) 
     683    pars = dict((p.name, p.default) for p in model_info['parameters']) 
    684684 
    685685    # Fill in default values for the polydispersity parameters 
    686686    for p in model_info['parameters']: 
    687         if p[4] in ('volume', 'orientation'): 
    688             pars[p[0]+'_pd'] = 0.0 
    689             pars[p[0]+'_pd_n'] = 0 
    690             pars[p[0]+'_pd_nsigma'] = 3.0 
    691             pars[p[0]+'_pd_type'] = "gaussian" 
     687        if p.type in ('volume', 'orientation'): 
     688            pars[p.name+'_pd'] = 0.0 
     689            pars[p.name+'_pd_n'] = 0 
     690            pars[p.name+'_pd_nsigma'] = 3.0 
     691            pars[p.name+'_pd_type'] = "gaussian" 
    692692 
    693693    # Plug in values given in demo 
     
    833833        pars = suppress_pd(pars) 
    834834    pars.update(presets)  # set value after random to control value 
     835    #import pprint; pprint.pprint(model_info) 
    835836    constrain_pars(model_info, pars) 
    836837    constrain_new_to_old(model_info, pars) 
  • sasmodels/generate.py

    r2f0c07d rfcd7bbd  
    8080    is selected, or when an initial value is not otherwise specified. 
    8181 
    82     [*lb*, *ub*] are the hard limits on the parameter value, used to limit 
    83     the polydispersity density function.  In the fit, the parameter limits 
     82    *limits = [lb, ub]* are the hard limits on the parameter value, used to 
     83    limit the polydispersity density function.  In the fit, the parameter limits 
    8484    given to the fit are the limits  on the central value of the parameter. 
    8585    If there is polydispersity, it will evaluate parameter values outside 
     
    216216import re 
    217217import string 
     218from collections import namedtuple 
    218219 
    219220import numpy as np 
     221 
     222PARAMETER_FIELDS = ['name', 'units', 'default', 'limits', 'type', 'description'] 
     223Parameter = namedtuple('Parameter', PARAMETER_FIELDS) 
    220224 
    221225#TODO: determine which functions are useful outside of generate 
     
    293297    """ 
    294298    column_widths = [ 
    295         max(len(p[0]) for p in pars), 
    296         max(len(p[-1]) for p in pars), 
    297         max(len(format_units(p[1])) for p in pars), 
     299        max(len(p.name) for p in pars), 
     300        max(len(p.description) for p in pars), 
     301        max(len(format_units(p.units)) for p in pars), 
    298302        PARTABLE_VALUE_WIDTH, 
    299303        ] 
     
    310314    for p in pars: 
    311315        lines.append(" ".join([ 
    312             "%-*s" % (column_widths[0], p[0]), 
    313             "%-*s" % (column_widths[1], p[-1]), 
    314             "%-*s" % (column_widths[2], format_units(p[1])), 
    315             "%*g" % (column_widths[3], p[2]), 
     316            "%-*s" % (column_widths[0], p.name), 
     317            "%-*s" % (column_widths[1], p.description), 
     318            "%-*s" % (column_widths[2], format_units(p.units)), 
     319            "%*g" % (column_widths[3], p.default), 
    316320            ])) 
    317321    lines.append(sep) 
     
    469473    fixed_2d = partype['fixed-1d'] 
    470474 
    471     iq_parameters = [p[0] 
     475    iq_parameters = [p.name 
    472476                     for p in model_info['parameters'][2:]  # skip scale, background 
    473                      if p[0] in set(fixed_1d + pd_1d)] 
    474     iqxy_parameters = [p[0] 
     477                     if p.name in set(fixed_1d + pd_1d)] 
     478    iqxy_parameters = [p.name 
    475479                       for p in model_info['parameters'][2:]  # skip scale, background 
    476                        if p[0] in set(fixed_2d + pd_2d)] 
    477     volume_parameters = [p[0] 
     480                       if p.name in set(fixed_2d + pd_2d)] 
     481    volume_parameters = [p.name 
    478482                         for p in model_info['parameters'] 
    479                          if p[4] == 'volume'] 
     483                         if p.type == 'volume'] 
    480484 
    481485    # Fill in defintions for volume parameters 
     
    606610 
    607611    for p in pars: 
    608         name, ptype = p[0], p[4] 
    609         if ptype == 'volume': 
    610             partype['pd-1d'].append(name) 
    611             partype['pd-2d'].append(name) 
    612             partype['pd-rel'].add(name) 
    613         elif ptype == 'magnetic': 
    614             partype['fixed-2d'].append(name) 
    615         elif ptype == 'orientation': 
    616             partype['pd-2d'].append(name) 
    617         elif ptype == '': 
    618             partype['fixed-1d'].append(name) 
    619             partype['fixed-2d'].append(name) 
     612        if p.type == 'volume': 
     613            partype['pd-1d'].append(p.name) 
     614            partype['pd-2d'].append(p.name) 
     615            partype['pd-rel'].add(p.name) 
     616        elif p.type == 'magnetic': 
     617            partype['fixed-2d'].append(p.name) 
     618        elif p.type == 'orientation': 
     619            partype['pd-2d'].append(p.name) 
     620        elif p.type == '': 
     621            partype['fixed-1d'].append(p.name) 
     622            partype['fixed-2d'].append(p.name) 
    620623        else: 
    621             raise ValueError("unknown parameter type %r" % ptype) 
    622         partype[ptype].append(name) 
     624            raise ValueError("unknown parameter type %r" % p.type) 
     625        partype[p.type].append(p.name) 
    623626 
    624627    return partype 
     
    628631    Process parameter block, precalculating parameter details. 
    629632    """ 
     633    # convert parameters into named tuples 
     634    pars = [Parameter(*p) for p in model_info['parameters']] 
    630635    # Fill in the derived attributes 
    631     partype = categorize_parameters(model_info['parameters']) 
    632     model_info['limits'] = dict((p[0], p[3]) for p in model_info['parameters']) 
     636    model_info['parameters'] = pars 
     637    partype = categorize_parameters(pars) 
     638    model_info['limits'] = dict((p.name, p.limits) for p in pars) 
    633639    model_info['partype'] = partype 
    634     model_info['defaults'] = dict((p[0], p[2]) for p in model_info['parameters']) 
     640    model_info['defaults'] = dict((p.name, p.default) for p in pars) 
    635641    if model_info.get('demo', None) is None: 
    636642        model_info['demo'] = model_info['defaults'] 
     
    649655    * *id* is the id of the kernel 
    650656    * *name* is the display name of the kernel 
     657    * *filename* is the full path to the module defining the file (if any) 
    651658    * *title* is a short description of the kernel 
    652659    * *description* is a long description of the kernel (this doesn't seem 
  • sasmodels/kernelpy.py

    r17bbadd rfcd7bbd  
    111111 
    112112        # First two fixed pars are scale and background 
    113         pars = [p[0] for p in model_info['parameters'][2:]] 
     113        pars = [p.name for p in model_info['parameters'][2:]] 
    114114        offset = len(self.q_input.q_vectors) 
    115115        self.args = self.q_input.q_vectors + [None] * len(pars) 
  • sasmodels/list_pars.py

    r17bbadd rfcd7bbd  
    2626        model_info = load_model_info(name) 
    2727        for p in model_info['parameters']: 
    28             pname = p[0] 
    29             partable.setdefault(pname, []) 
    30             partable[pname].append(name) 
     28            partable.setdefault(p.name, []) 
     29            partable[p.name].append(name) 
    3130    return partable 
    3231 
  • sasmodels/model_test.py

    r17bbadd rfcd7bbd  
    251251  python -m sasmodels.model_test [-v] [opencl|dll] model1 model2 ... 
    252252 
    253 If -v is included on the 
     253If -v is included on the command line, then use verboe output. 
     254 
    254255If neither opencl nor dll is specified, then models will be tested with 
    255256both opencl and dll; the compute target is ignored for pure python models. 
  • sasmodels/product.py

    r35b4c47 rfcd7bbd  
    2828    s_id, s_name, s_pars = s_info['id'], s_info['name'], s_info['parameters'] 
    2929    # We require models to start with scale and background 
    30     assert s_pars[SCALE][0] == 'scale' 
    31     assert s_pars[BACKGROUND][0] == 'background' 
     30    assert s_pars[SCALE].name == 'scale' 
     31    assert s_pars[BACKGROUND].name == 'background' 
    3232    # We require structure factors to start with effect radius and volfraction 
    33     assert s_pars[EFFECT_RADIUS][0] == 'effect_radius' 
    34     assert s_pars[VOLFRACTION][0] == 'volfraction' 
     33    assert s_pars[EFFECT_RADIUS].name == 'effect_radius' 
     34    assert s_pars[VOLFRACTION].name == 'volfraction' 
    3535    # Combine the parameter sets.  We are skipping the first three 
    3636    # parameters of S since scale, background are defined in P and 
     
    3838    pars = p_pars + s_pars[3:] 
    3939    # check for duplicates; can't use assertion since they may never be checked 
    40     if len(set(p[0] for p in pars)) != len(pars): 
     40    if len(set(p.name for p in pars)) != len(pars): 
    4141        raise ValueError("Duplicate parameters in %s and %s"%(p_id)) 
    4242    # For comparison with sasview, determine the old parameters. 
     
    5252    model_info['title'] = 'Product of %s and structure factor %s'%(p_name, s_name) 
    5353    model_info['description'] = model_info['title'] 
     54    model_info['docs'] = model_info['title'] 
    5455    model_info['category'] = "custom" 
    5556    model_info['parameters'] = pars 
    56     # Remember the component info blocks so we can build the product model 
    57     model_info['composition'] = ('product', [p_info, s_info]) 
     57    #model_info['single'] = p_info['single'] and s_info['single'] 
     58    model_info['structure_factor'] = False 
     59    model_info['variant_info'] = None 
     60    #model_info['tests'] = [] 
     61    #model_info['source'] = [] 
     62    # Iq, Iqxy, form_volume, ER, VR and sesans 
    5863    model_info['oldname'] = oldname 
    5964    model_info['oldpars'] = oldpars 
     65    model_info['composition'] = ('product', [p_info, s_info]) 
    6066    process_parameters(model_info) 
    6167    return model_info 
  • sasmodels/sasview_model.py

    r28da77d rfcd7bbd  
    6060        self.dispersion = dict() 
    6161        partype = model.info['partype'] 
    62         for name, units, default, limits, _, _ in model.info['parameters']: 
    63             self.params[name] = default 
    64             self.details[name] = [units] + limits 
     62        for p in model.info['parameters']: 
     63            self.params[p.name] = p.default 
     64            self.details[p.name] = [p.units] + p.limits 
    6565 
    6666        for name in partype['pd-2d']: 
Note: See TracChangeset for help on using the changeset viewer.