Changeset 32e3c9b in sasmodels for sasmodels/convert.py
- Timestamp:
- Jul 21, 2016 2:08:04 PM (8 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/convert.py
ra0494e9 r32e3c9b 7 7 import math 8 8 import warnings 9 import json 9 10 from .conversion_table import CONVERSION_TABLE 10 11 11 12 # List of models which SasView versions don't contain the explicit 'scale' argument. … … 47 48 ] 48 49 49 CONVERSION_TABLE = None50 51 def _read_conversion_table():52 global CONVERSION_TABLE53 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=True62 )63 64 def _byteify(data, ignore_dicts = False):65 # if this is a unicode string, return its string representation66 if isinstance(data, unicode):67 return data.encode('utf-8')68 # if this is a list of values, return list of byteified values69 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 values72 # but only if we haven't already byteified it73 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 form78 return data79 80 81 50 def _convert_pars(pars, mapping): 82 51 """ … … 93 62 return newpars 94 63 95 def _rescale_sld(pars):96 """97 rescale all sld parameters in the new model definition by 1e6 so the98 numbers are nicer. Relies on the fact that all sld parameters in the99 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 p103 else v))104 for p, v in pars.items())105 106 64 def convert_model(name, pars): 107 65 """ … … 116 74 return [pk*scale for pk in par] if isinstance(par, list) else par*scale 117 75 118 def _unscale_sld(pars): 76 def _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 91 def _unscale_sld(modelinfo, pars): 119 92 """ 120 93 rescale all sld parameters in the new model definition by 1e6 so the … … 122 95 new model definition end with sld. 123 96 """ 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()) 128 99 129 100 def _remove_pd(pars, key, name): … … 164 135 165 136 def revert_name(model_info): 166 _read_conversion_table()167 137 oldname, oldpars = CONVERSION_TABLE.get(model_info.id, [None, {}]) 168 138 return oldname 169 139 170 140 def _get_translation_table(model_info): 171 _read_conversion_table()172 141 _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) 173 142 translation = translation.copy() … … 186 155 187 156 def _trim_vectors(model_info, pars, oldpars): 188 _read_conversion_table()189 157 _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) 190 158 for p in model_info.parameters.kernel_parameters: … … 212 180 else: 213 181 translation = _get_translation_table(model_info) 214 oldpars = _revert_pars(_unscale_sld( pars), translation)182 oldpars = _revert_pars(_unscale_sld(model_info, pars), translation) 215 183 oldpars = _trim_vectors(model_info, pars, oldpars) 216 184 … … 243 211 _remove_pd(oldpars, 'rimB', name) 244 212 _remove_pd(oldpars, 'rimC', name) 213 elif name == 'polymer_micelle': 214 if 'ndensity' in oldpars: 215 oldpars['ndensity'] *= 1e15 245 216 elif name == 'spherical_sld': 246 217 for k in range(1, int(pars['n_shells'])+1):
Note: See TracChangeset
for help on using the changeset viewer.