Changeset f247314 in sasmodels


Ignore:
Timestamp:
Apr 4, 2016 3:23:23 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:
ec45c4f
Parents:
204fd9b
Message:

use sasmodels/convert.json for converting new models to old

Location:
sasmodels
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/compare.py

    rea75043 rf247314  
    4141from .data import plot_theory, empty_data1D, empty_data2D 
    4242from .direct_model import DirectModel 
    43 from .convert import revert_pars, constrain_new_to_old 
     43from .convert import revert_name, revert_pars, constrain_new_to_old 
    4444 
    4545USAGE = """ 
     
    347347def eval_sasview(model_info, data): 
    348348    """ 
    349     Return a model calculator using the SasView fitting engine. 
     349    Return a model calculator using the pre-4.0 SasView models. 
    350350    """ 
    351351    # importing sas here so that the error message will be that sas failed to 
     
    366366        composition_type, parts = model_info['composition'] 
    367367        if composition_type == 'product': 
    368             from sas.sascalc.fit.MultiplicationModel import MultiplicationModel 
    369             P, S = [get_model(p) for p in model_info['oldname']] 
     368            from sas.models.MultiplicationModel import MultiplicationModel 
     369            P, S = [get_model(revert_name(p)) for p in parts] 
    370370            model = MultiplicationModel(P, S) 
    371371        else: 
    372372            raise ValueError("sasview mixture models not supported by compare") 
    373373    else: 
    374         model = get_model(model_info['oldname']) 
     374        model = get_model(revert_name(model_info)) 
    375375 
    376376    # build a smearer with which to call the model, if necessary 
  • sasmodels/convert.py

    r2a3e1f5 rf247314  
    22Convert models to and from sasview. 
    33""" 
     4from os.path import join as joinpath, abspath, dirname 
    45import warnings 
     6import json 
    57 
    68# List of models which SasView versions don't contain the explicit 'scale' argument. 
     
    2628] 
    2729 
     30# Convert new style names for polydispersity info to old style names 
    2831PD_DOT = [ 
    2932    ("", ""), 
     
    3336    ("_pd_type", ".type"), 
    3437    ] 
     38 
     39CONVERSION_TABLE = None 
     40 
     41def _read_conversion_table(): 
     42    global CONVERSION_TABLE 
     43    if CONVERSION_TABLE is None: 
     44        path = joinpath(dirname(abspath(__file__)), "convert.json") 
     45        with open(path) as fid: 
     46            CONVERSION_TABLE = json_load_byteified(fid) 
     47 
     48def json_load_byteified(file_handle): 
     49    return _byteify( 
     50        json.load(file_handle, object_hook=_byteify), 
     51        ignore_dicts=True 
     52    ) 
     53 
     54def _byteify(data, ignore_dicts = False): 
     55    # if this is a unicode string, return its string representation 
     56    if isinstance(data, unicode): 
     57        return data.encode('utf-8') 
     58    # if this is a list of values, return list of byteified values 
     59    if isinstance(data, list): 
     60        return [ _byteify(item, ignore_dicts=True) for item in data ] 
     61    # if this is a dictionary, return dictionary of byteified keys and values 
     62    # but only if we haven't already byteified it 
     63    if isinstance(data, dict) and not ignore_dicts: 
     64        return { 
     65            _byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True) 
     66            for key, value in data.iteritems() 
     67        } 
     68    # if it's anything else, return it in its original form 
     69    return data 
     70 
     71 
    3572def _convert_pars(pars, mapping): 
    3673    """ 
     
    114151    return newpars 
    115152 
     153def revert_name(model_info): 
     154    _read_conversion_table() 
     155    oldname, oldpars = CONVERSION_TABLE.get(model_info['id'], [None, {}]) 
     156    return oldname 
     157 
     158def _get_old_pars(model_info): 
     159    _read_conversion_table() 
     160    oldname, oldpars = CONVERSION_TABLE.get(model_info['id'], [None, {}]) 
     161    return oldpars 
     162 
    116163def revert_pars(model_info, pars): 
    117164    """ 
    118165    Convert model from new style parameter names to old style. 
    119166    """ 
    120     mapping = model_info['oldpars'] 
    121     oldpars = _revert_pars(_unscale_sld(pars), mapping) 
     167    if model_info['composition'] is not None: 
     168        composition_type, parts = model_info['composition'] 
     169        if composition_type == 'product': 
     170            P, S = parts 
     171            oldpars = {'scale':'scale_factor'} 
     172            oldpars.update(_get_old_pars(P)) 
     173            oldpars.update(_get_old_pars(S)) 
     174        else: 
     175            raise NotImplementedError("cannot convert to sasview sum") 
     176    else: 
     177        oldpars = _get_old_pars(model_info) 
     178    oldpars = _revert_pars(_unscale_sld(pars), oldpars) 
     179 
    122180 
    123181    # Note: update compare.constrain_pars to match 
  • sasmodels/generate.py

    r9ef9dd9 rf247314  
    153153    parameter list.  *demo* is mostly needed to set the default 
    154154    polydispersity values for tests. 
    155  
    156     *oldname* is the name of the model in sasview before sasmodels 
    157     was split into its own package, and *oldpars* is a dictionary 
    158     of *parameter: old_parameter* pairs defining the new names for 
    159     the parameters.  This is used by *compare* to check the values 
    160     of the new model against the values of the old model before 
    161     you are ready to add the new model to sasmodels. 
    162  
    163155 
    164156An *model_info* dictionary is constructed from the kernel meta data and 
     
    692684      implementing the kernel for the module, or None if they are not 
    693685      defined in python 
    694     * *oldname* is the model name in pre-4.0 Sasview 
    695     * *oldpars* is the *{new: old}* parameter translation table 
    696       from pre-4.0 Sasview 
    697686    * *composition* is None if the model is independent, otherwise it is a 
    698687      tuple with composition type ('product' or 'mixture') and a list of 
     
    723712        demo=getattr(kernel_module, 'demo', None), 
    724713        source=getattr(kernel_module, 'source', []), 
    725         oldname=getattr(kernel_module, 'oldname', None), 
    726         oldpars=getattr(kernel_module, 'oldpars', {}), 
    727714        tests=getattr(kernel_module, 'tests', []), 
    728715        ) 
  • sasmodels/models/cylinder.py

    r6ef4293 rf247314  
    138138            phi_pd=10, phi_pd_n=5) 
    139139 
    140 # For testing against the old sasview models, include the converted parameter 
    141 # names and the target sasview model name. 
    142 oldname = 'CylinderModel' 
    143 oldpars = dict(theta='cyl_theta', phi='cyl_phi', sld='sldCyl', sld_solvent='sldSolv') 
    144  
    145  
    146140qx, qy = 0.2 * np.cos(2.5), 0.2 * np.sin(2.5) 
    147141tests = [[{}, 0.2, 0.042761386790780453], 
  • sasmodels/product.py

    r3c6d5bc rf247314  
    4242    if len(set(p.name for p in pars)) != len(pars): 
    4343        raise ValueError("Duplicate parameters in %s and %s"%(p_id)) 
    44     # For comparison with sasview, determine the old parameters. 
    45     oldname = [p_info['oldname'], s_info['oldname']] 
    46     oldpars = {'scale':'scale_factor'} 
    47     oldpars.update(p_info['oldpars']) 
    48     oldpars.update(s_info['oldpars']) 
    49  
    5044    model_info = {} 
    5145    model_info['id'] = '*'.join((p_id, s_id)) 
     
    6357    #model_info['source'] = [] 
    6458    # Iq, Iqxy, form_volume, ER, VR and sesans 
    65     model_info['oldname'] = oldname 
    66     model_info['oldpars'] = oldpars 
    6759    model_info['composition'] = ('product', [p_info, s_info]) 
    6860    process_parameters(model_info) 
  • sasmodels/sasview_model.py

    r787be86 rf247314  
    3232 
    3333    Returns a class that can be used directly as a sasview model.t 
    34  
    35     Defaults to using the new name for a model.  Setting 
    36     *namestyle='oldname'* will produce a class with a name 
    37     compatible with SasView. 
    3834    """ 
    3935    model_info = core.load_model_info(model_name) 
     
    6056 
    6157        self.name = model_info['name'] 
    62         self.oldname = model_info['oldname'] 
    6358        self.description = model_info['description'] 
    6459        self.category = None 
Note: See TracChangeset for help on using the changeset viewer.