Changeset 32e3c9b in sasmodels for sasmodels/convert.py


Ignore:
Timestamp:
Jul 21, 2016 2:08:04 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:
b966a96
Parents:
42356c8
Message:

dll version of magnetic sld

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/convert.py

    ra0494e9 r32e3c9b  
    77import math 
    88import warnings 
    9 import json 
     9 
     10from .conversion_table import CONVERSION_TABLE 
    1011 
    1112# List of models which SasView versions don't contain the explicit 'scale' argument. 
     
    4748    ] 
    4849 
    49 CONVERSION_TABLE = None 
    50  
    51 def _read_conversion_table(): 
    52     global CONVERSION_TABLE 
    53     if CONVERSION_TABLE is None: 
    54         path = joinpath(dirname(abspath(__file__)), "convert.json") 
    55         with open(path) as fid: 
    56             CONVERSION_TABLE = json_load_byteified(fid) 
    57  
    58 def json_load_byteified(file_handle): 
    59     return _byteify( 
    60         json.load(file_handle, object_hook=_byteify), 
    61         ignore_dicts=True 
    62     ) 
    63  
    64 def _byteify(data, ignore_dicts = False): 
    65     # if this is a unicode string, return its string representation 
    66     if isinstance(data, unicode): 
    67         return data.encode('utf-8') 
    68     # if this is a list of values, return list of byteified values 
    69     if isinstance(data, list): 
    70         return [ _byteify(item, ignore_dicts=True) for item in data ] 
    71     # if this is a dictionary, return dictionary of byteified keys and values 
    72     # but only if we haven't already byteified it 
    73     if isinstance(data, dict) and not ignore_dicts: 
    74         return dict((_byteify(key, ignore_dicts=True), 
    75                      _byteify(value, ignore_dicts=True)) 
    76                     for key, value in data.items()) 
    77     # if it's anything else, return it in its original form 
    78     return data 
    79  
    80  
    8150def _convert_pars(pars, mapping): 
    8251    """ 
     
    9362    return newpars 
    9463 
    95 def _rescale_sld(pars): 
    96     """ 
    97     rescale all sld parameters in the new model definition by 1e6 so the 
    98     numbers are nicer.  Relies on the fact that all sld parameters in the 
    99     new model definition end with sld. 
    100     """ 
    101     return dict((p, (v*1e6 if p.endswith('sld') 
    102                      else v*1e-15 if 'ndensity' in p 
    103                      else v)) 
    104                 for p, v in pars.items()) 
    105  
    10664def convert_model(name, pars): 
    10765    """ 
     
    11674    return [pk*scale for pk in par] if isinstance(par, list) else par*scale 
    11775 
    118 def _unscale_sld(pars): 
     76def _is_sld(modelinfo, id): 
     77    if id.startswith('M0:'): 
     78        return True 
     79    if (id.endswith('_pd') or id.endswith('_pd_n') or id.endswith('_pd_nsigma') 
     80            or id.endswith('_pd_width') or id.endswith('_pd_type')): 
     81        return False 
     82    for p in modelinfo.parameters.call_parameters: 
     83        if p.id == id: 
     84            return p.type == 'sld' 
     85    # check through kernel parameters in case it is a named as a vector 
     86    for p in modelinfo.parameters.kernel_parameters: 
     87        if p.id == id: 
     88            return p.type == 'sld' 
     89    raise ValueError("unknown parameter %r in conversion"%id) 
     90 
     91def _unscale_sld(modelinfo, pars): 
    11992    """ 
    12093    rescale all sld parameters in the new model definition by 1e6 so the 
     
    12295    new model definition end with sld. 
    12396    """ 
    124     return dict((p, (_unscale(v,1e-6) if p.startswith('sld') or p.endswith('sld') 
    125                      else _unscale(v,1e15) if 'ndensity' in p 
    126                      else v)) 
    127                 for p, v in pars.items()) 
     97    return dict((id, (_unscale(v,1e-6) if _is_sld(modelinfo, id) else v)) 
     98                for id, v in pars.items()) 
    12899 
    129100def _remove_pd(pars, key, name): 
     
    164135 
    165136def revert_name(model_info): 
    166     _read_conversion_table() 
    167137    oldname, oldpars = CONVERSION_TABLE.get(model_info.id, [None, {}]) 
    168138    return oldname 
    169139 
    170140def _get_translation_table(model_info): 
    171     _read_conversion_table() 
    172141    _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) 
    173142    translation = translation.copy() 
     
    186155 
    187156def _trim_vectors(model_info, pars, oldpars): 
    188     _read_conversion_table() 
    189157    _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) 
    190158    for p in model_info.parameters.kernel_parameters: 
     
    212180    else: 
    213181        translation = _get_translation_table(model_info) 
    214         oldpars = _revert_pars(_unscale_sld(pars), translation) 
     182        oldpars = _revert_pars(_unscale_sld(model_info, pars), translation) 
    215183        oldpars = _trim_vectors(model_info, pars, oldpars) 
    216184 
     
    243211            _remove_pd(oldpars, 'rimB', name) 
    244212            _remove_pd(oldpars, 'rimC', name) 
     213        elif name == 'polymer_micelle': 
     214            if 'ndensity' in oldpars: 
     215                oldpars['ndensity'] *= 1e15 
    245216        elif name == 'spherical_sld': 
    246217            for k in range(1, int(pars['n_shells'])+1): 
Note: See TracChangeset for help on using the changeset viewer.