Changeset 17bbadd in sasmodels for sasmodels/convert.py


Ignore:
Timestamp:
Mar 15, 2016 12:47:12 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:
754e27b
Parents:
5ceb7d0
Message:

refactor so all model defintion queries use model_info; better documentation of model_info structure; initial implementation of product model (broken)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/convert.py

    r0d0aee1 r17bbadd  
    44import warnings 
    55 
     6STRUCTURE_FACTORS = [ 
     7    'hardsphere', 
     8    'stickyhardsphere', 
     9    'squarewell', 
     10    'HayterMSAsq' 
     11] 
    612# List of models which SasView versions don't contain the explicit 'scale' argument. 
    713# When converting such a model, please update this list. 
    8 MODELS_WITHOUT_SCALE = [ 
     14MODELS_WITHOUT_SCALE = STRUCTURE_FACTORS + [ 
    915    'teubner_strey', 
    1016    'broad_peak', 
     
    1521    'be_polyelectrolyte', 
    1622    'correlation_length', 
     23    'fractal_core_shell' 
    1724    'binary_hard_sphere', 
    18     'fractal_core_shell' 
    1925] 
    2026 
    2127# List of models which SasView versions don't contain the explicit 'background' argument. 
    2228# When converting such a model, please update this list. 
    23 MODELS_WITHOUT_BACKGROUND = [ 
     29MODELS_WITHOUT_BACKGROUND = STRUCTURE_FACTORS + [ 
    2430    'guinier', 
    2531] 
     
    5258    new model definition end with sld. 
    5359    """ 
    54     return dict((p, (v*1e6 if p.endswith('sld') else v*1e-15 if 'ndensity' in p else v)) 
     60    return dict((p, (v*1e6 if p.endswith('sld') 
     61                     else v*1e-15 if 'ndensity' in p 
     62                     else v)) 
    5563                for p, v in pars.items()) 
    5664 
     
    7078    new model definition end with sld. 
    7179    """ 
    72     return dict((p, (v*1e-6 if p.endswith('sld') else v*1e15 if 'ndensity' in p else v)) 
     80    return dict((p, (v*1e-6 if p.endswith('sld') 
     81                     else v*1e15 if 'ndensity' in p 
     82                     else v)) 
    7383                for p, v in pars.items()) 
    7484 
     
    109119    return newpars 
    110120 
    111 def revert_model(model_definition, pars): 
     121def revert_pars(model_info, pars): 
    112122    """ 
    113123    Convert model from new style parameter names to old style. 
    114124    """ 
    115     mapping = model_definition.oldpars 
    116     oldname = model_definition.oldname 
     125    mapping = model_info['oldpars'] 
    117126    oldpars = _revert_pars(_unscale_sld(pars), mapping) 
    118127 
    119128    # Note: update compare.constrain_pars to match 
    120     name = model_definition.name 
     129    name = model_info['id'] 
    121130    if name in MODELS_WITHOUT_SCALE: 
    122131        if oldpars.pop('scale', 1.0) != 1.0: 
    123132            warnings.warn("parameter scale not used in sasview %s"%name) 
    124     elif name in MODELS_WITHOUT_BACKGROUND: 
     133    if name in MODELS_WITHOUT_BACKGROUND: 
    125134        if oldpars.pop('background', 0.0) != 0.0: 
    126135            warnings.warn("parameter background not used in sasview %s"%name) 
    127     elif getattr(model_definition, 'category', None) == 'structure-factor': 
    128         if oldpars.pop('scale', 1.0) != 1.0: 
    129             warnings.warn("parameter scale not used in sasview %s"%name) 
    130         if oldpars.pop('background', 0.0) != 0.0: 
    131             warnings.warn("parameter background not used in sasview %s"%name) 
    132     elif name == 'pearl_necklace': 
    133         _remove_pd(oldpars, 'num_pearls', name) 
    134         _remove_pd(oldpars, 'thick_string', name) 
    135     elif name == 'core_shell_parallelepiped': 
    136         _remove_pd(oldpars, 'rimA', name) 
    137         _remove_pd(oldpars, 'rimB', name) 
    138         _remove_pd(oldpars, 'rimC', name) 
    139     elif name == 'rpa': 
    140         # convert scattering lengths from femtometers to centimeters 
    141         for p in "La", "Lb", "Lc", "Ld": 
    142             if p in oldpars: oldpars[p] *= 1e-13 
    143136 
    144     return oldname, oldpars 
     137    # If it is a product model P*S, then check the individual forms for special 
     138    # cases.  Note: despite the structure factor alone not having scale or 
     139    # background, the product model does, so this is below the test for 
     140    # models without scale or background. 
     141    namelist = name.split('*') if '*' in name else [name] 
     142    for name in namelist: 
     143        if name == 'pearl_necklace': 
     144            _remove_pd(oldpars, 'num_pearls', name) 
     145            _remove_pd(oldpars, 'thick_string', name) 
     146        elif name == 'core_shell_parallelepiped': 
     147            _remove_pd(oldpars, 'rimA', name) 
     148            _remove_pd(oldpars, 'rimB', name) 
     149            _remove_pd(oldpars, 'rimC', name) 
     150        elif name == 'rpa': 
     151            # convert scattering lengths from femtometers to centimeters 
     152            for p in "La", "Lb", "Lc", "Ld": 
     153                if p in oldpars: oldpars[p] *= 1e-13 
    145154 
    146 def constrain_new_to_old(model_definition, pars): 
     155    return oldpars 
     156 
     157def constrain_new_to_old(model_info, pars): 
    147158    """ 
    148159    Restrict parameter values to those that will match sasview. 
    149160    """ 
     161    name = model_info['id'] 
    150162    # Note: update convert.revert_model to match 
    151     name = model_definition.name 
    152163    if name in MODELS_WITHOUT_SCALE: 
    153164        pars['scale'] = 1 
    154     elif name in MODELS_WITHOUT_BACKGROUND: 
     165    if name in MODELS_WITHOUT_BACKGROUND: 
    155166        pars['background'] = 0 
    156     elif name == 'pearl_necklace': 
    157         pars['string_thickness_pd_n'] = 0 
    158         pars['number_of_pearls_pd_n'] = 0 
    159     elif name == 'line': 
    160         pars['scale'] = 1 
    161         pars['background'] = 0 
    162     elif name == 'rpa': 
    163         pars['case_num'] = int(pars['case_num']) 
    164     elif getattr(model_definition, 'category', None) == 'structure-factor': 
    165         pars['scale'], pars['background'] = 1, 0 
    166167 
     168    # If it is a product model P*S, then check the individual forms for special 
     169    # cases.  Note: despite the structure factor alone not having scale or 
     170    # background, the product model does, so this is below the test for 
     171    # models without scale or background. 
     172    namelist = name.split('*') if '*' in name else [name] 
     173    for name in namelist: 
     174        if name == 'pearl_necklace': 
     175            pars['string_thickness_pd_n'] = 0 
     176            pars['number_of_pearls_pd_n'] = 0 
     177        elif name == 'line': 
     178            pars['scale'] = 1 
     179            pars['background'] = 0 
     180        elif name == 'rpa': 
     181            pars['case_num'] = int(pars['case_num']) 
Note: See TracChangeset for help on using the changeset viewer.