Changeset f247314 in sasmodels for sasmodels/convert.py
- Timestamp:
- Apr 4, 2016 5:23:23 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:
- ec45c4f
- Parents:
- 204fd9b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/convert.py
r2a3e1f5 rf247314 2 2 Convert models to and from sasview. 3 3 """ 4 from os.path import join as joinpath, abspath, dirname 4 5 import warnings 6 import json 5 7 6 8 # List of models which SasView versions don't contain the explicit 'scale' argument. … … 26 28 ] 27 29 30 # Convert new style names for polydispersity info to old style names 28 31 PD_DOT = [ 29 32 ("", ""), … … 33 36 ("_pd_type", ".type"), 34 37 ] 38 39 CONVERSION_TABLE = None 40 41 def _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 48 def json_load_byteified(file_handle): 49 return _byteify( 50 json.load(file_handle, object_hook=_byteify), 51 ignore_dicts=True 52 ) 53 54 def _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 35 72 def _convert_pars(pars, mapping): 36 73 """ … … 114 151 return newpars 115 152 153 def revert_name(model_info): 154 _read_conversion_table() 155 oldname, oldpars = CONVERSION_TABLE.get(model_info['id'], [None, {}]) 156 return oldname 157 158 def _get_old_pars(model_info): 159 _read_conversion_table() 160 oldname, oldpars = CONVERSION_TABLE.get(model_info['id'], [None, {}]) 161 return oldpars 162 116 163 def revert_pars(model_info, pars): 117 164 """ 118 165 Convert model from new style parameter names to old style. 119 166 """ 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 122 180 123 181 # Note: update compare.constrain_pars to match
Note: See TracChangeset
for help on using the changeset viewer.