Changeset 9a66e65 in sasmodels
- Timestamp:
- Dec 9, 2015 11:12:17 AM (9 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:
- 716abe0
- Parents:
- dcef2ee
- Location:
- sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/compare.py
rcf404cb r9a66e65 20 20 from .data import plot_theory, empty_data1D, empty_data2D 21 21 from .direct_model import DirectModel 22 from .convert import revert_model 22 from .convert import revert_model, constrain_new_to_old 23 23 kerneldll.ALLOW_SINGLE_PRECISION_DLLS = True 24 24 … … 81 81 82 82 for k,v in pars.items(): 83 if k.startswith("num_pearls_") or k.startswith("thick_string_"): 84 continue 85 if k.endswith("_pd"): 86 model.dispersion[k[:-3]]['width'] = v 87 elif k.endswith("_pd_n"): 88 model.dispersion[k[:-5]]['npts'] = v 89 elif k.endswith("_pd_nsigma"): 90 model.dispersion[k[:-10]]['nsigmas'] = v 91 elif k.endswith("_pd_type"): 92 model.dispersion[k[:-8]]['type'] = v 83 parts = k.split('.') # polydispersity components 84 if len(parts) == 2: 85 model.dispersion[parts[0]][parts[1]] = v 93 86 else: 94 87 model.setParam(k, v) … … 129 122 130 123 def constrain_pars(model_definition, pars): 124 """ 125 Restrict parameters to valid values. 126 """ 131 127 name = model_definition.name 132 128 if name == 'capped_cylinder' and pars['cap_radius'] < pars['radius']: … … 141 137 rg_max = np.sqrt(90*np.log(10) + 3*np.log(pars['scale']))/q_max 142 138 pars['rg'] = min(pars['rg'],rg_max) 143 144 # These constraints are only needed for comparison to sasview145 if name in ('teubner_strey','broad_peak'):146 del pars['scale']147 if name in ('guinier',):148 del pars['background']149 if getattr(model_definition, 'category', None) == 'structure-factor':150 del pars['scale'], pars['background']151 152 139 153 140 def parlist(pars): … … 277 264 pars.update(set_pars) # set value after random to control value 278 265 constrain_pars(model_definition, pars) 266 constrain_new_to_old(model_definition, pars) 279 267 280 268 # parameter selection -
sasmodels/compare_many.py
r5d316e9 r9a66e65 9 9 from .compare import (MODELS, randomize_model, suppress_pd, eval_sasview, 10 10 eval_opencl, eval_ctypes, make_data, get_demo_pars, 11 columnize, constrain_pars )11 columnize, constrain_pars, constrain_new_to_old) 12 12 13 13 def calc_stats(target, value, index): … … 74 74 pars_i, seed = randomize_model(pars) 75 75 constrain_pars(model_definition, pars_i) 76 constrain_new_to_old(model_definition, pars_i) 76 77 if mono: suppress_pd(pars_i) 77 78 -
sasmodels/convert.py
raa4946b r9a66e65 2 2 Convert models to and from sasview. 3 3 """ 4 import warnings 4 5 5 def _rename_pars(pars, mapping): 6 PD_DOT = [ 7 ("", ""), 8 ("_pd", ".width"), 9 ("_pd_n", ".npts"), 10 ("_pd_nsigma", ".nsigmas"), 11 ("_pd_type", ".type"), 12 ] 13 def _convert_pars(pars, mapping): 6 14 """ 7 15 Rename the parameters and any associated polydispersity attributes. 8 16 """ 9 17 newpars = pars.copy() 10 for old,newin mapping.items():18 for new, old in mapping.items(): 11 19 if old == new: continue 12 # Bumps style parameter names 13 for variant in ("", "_pd", "_pd_n", "_pd_nsigma", "_pd_type"): 14 if old+variant in newpars: 20 for pd, dot in PD_DOT: 21 if old+dot in newpars: 15 22 if new is not None: 16 newpars[new+variant] = pars[old+variant] 17 del newpars[old+variant] 18 # Sasview style parameter names 19 for variant in (".width", ".nsigmas", ".type", ".npts"): 20 if old+variant in newpars: 21 if new is not None: 22 newpars[new+variant] = pars[old+variant] 23 del newpars[old+variant] 23 newpars[new+pd] = pars[old+dot] 24 del newpars[old+dot] 24 25 return newpars 25 26 … … 51 52 for p,v in pars.items()) 52 53 54 def _remove_pd(pars, key, name): 55 """ 56 Remove polydispersity from the parameter list. 57 58 Note: operates in place 59 """ 60 # Bumps style parameter names 61 pd = pars.pop(key+".width", 0.0) 62 pd_n = pars.pop(key+".npts", 0) 63 if pd != 0.0 and pd_n != 0: 64 warnings.warn("parameter %s not polydisperse in sasview %s"%(key, name)) 65 pars.pop(key+".nsigmas", None) 66 pars.pop(key+".type", None) 67 return pars 68 69 def _revert_pars(pars, mapping): 70 """ 71 Rename the parameters and any associated polydispersity attributes. 72 """ 73 newpars = pars.copy() 74 for new, old in mapping.items(): 75 for pd, dot in PD_DOT: 76 if old+pd == new+dot: 77 continue 78 if new+pd in newpars: 79 if old is not None: 80 newpars[old+dot] = pars[new+pd] 81 del newpars[new+pd] 82 return newpars 83 53 84 def revert_model(model_definition, pars): 54 85 """ … … 57 88 mapping = model_definition.oldpars 58 89 oldname = model_definition.oldname 59 oldpars = _rename_pars(_unscale_sld(pars), mapping) 90 oldpars = _revert_pars(_unscale_sld(pars), mapping) 91 92 # Note: update compare.constrain_pars to match 93 name = model_definition.name 94 if name in ('teubner_strey', 'broad_peak'): 95 if oldpars.pop('scale', 1.0) != 1.0: 96 warnings.warn("parameter scale not used in sasview %s"%name) 97 elif name in ('guinier',): 98 if oldpars.pop('background', 0.0) != 0.0: 99 warnings.warn("parameter background not used in sasview %s"%name) 100 elif getattr(model_definition, 'category', None) == 'structure-factor': 101 if oldpars.pop('scale', 1.0) != 1.0: 102 warnings.warn("parameter scale not used in sasview %s"%name) 103 if oldpars.pop('background', 0.0) != 0.0: 104 warnings.warn("parameter background not used in sasview %s"%name) 105 elif name == 'pearl_necklace': 106 _remove_pd(oldpars, 'num_pearls', name) 107 _remove_pd(oldpars, 'thick_string', name) 108 60 109 return oldname, oldpars 61 110 111 def constrain_new_to_old(model_definition, pars): 112 """ 113 Restrict parameter values to those that will match sasview. 114 """ 115 # Note: update convert.revert_model to match 116 name = model_definition.name 117 if name in ('teubner_strey','broad_peak'): 118 pars['scale'] = 1 119 elif name in ('guinier',): 120 pars['background'] = 0 121 elif name == 'pearl_necklace': 122 pars['string_thickness_pd_n'] = 0 123 pars['number_of_pearls_pd_n'] = 0 124 elif getattr(model_definition, 'category', None) == 'structure-factor': 125 pars['scale'], pars['background'] = 1, 0 126
Note: See TracChangeset
for help on using the changeset viewer.