Changeset f247314 in sasmodels for sasmodels/convert.py


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

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

File:
1 edited

Legend:

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