Changeset fcd7bbd in sasmodels for sasmodels/generate.py


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

use named tuple for parameter information

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.