[3964f92] | 1 | """ |
---|
| 2 | Convert models to and from sasview. |
---|
| 3 | """ |
---|
| 4 | import warnings |
---|
| 5 | |
---|
[17bbadd] | 6 | STRUCTURE_FACTORS = [ |
---|
| 7 | 'hardsphere', |
---|
| 8 | 'stickyhardsphere', |
---|
| 9 | 'squarewell', |
---|
| 10 | 'HayterMSAsq' |
---|
| 11 | ] |
---|
[3964f92] | 12 | # List of models which SasView versions don't contain the explicit 'scale' argument. |
---|
| 13 | # When converting such a model, please update this list. |
---|
[17bbadd] | 14 | MODELS_WITHOUT_SCALE = STRUCTURE_FACTORS + [ |
---|
[3964f92] | 15 | 'teubner_strey', |
---|
| 16 | 'broad_peak', |
---|
| 17 | 'two_lorentzian', |
---|
[34d6cab] | 18 | "two_power_law", |
---|
[3964f92] | 19 | 'gel_fit', |
---|
| 20 | 'gauss_lorentz_gel', |
---|
| 21 | 'be_polyelectrolyte', |
---|
| 22 | 'correlation_length', |
---|
[7d4b2ae] | 23 | 'fractal_core_shell' |
---|
[17bbadd] | 24 | 'binary_hard_sphere', |
---|
[3964f92] | 25 | ] |
---|
| 26 | |
---|
| 27 | # List of models which SasView versions don't contain the explicit 'background' argument. |
---|
| 28 | # When converting such a model, please update this list. |
---|
[17bbadd] | 29 | MODELS_WITHOUT_BACKGROUND = STRUCTURE_FACTORS + [ |
---|
[3964f92] | 30 | 'guinier', |
---|
| 31 | ] |
---|
| 32 | |
---|
| 33 | PD_DOT = [ |
---|
| 34 | ("", ""), |
---|
| 35 | ("_pd", ".width"), |
---|
| 36 | ("_pd_n", ".npts"), |
---|
| 37 | ("_pd_nsigma", ".nsigmas"), |
---|
| 38 | ("_pd_type", ".type"), |
---|
| 39 | ] |
---|
| 40 | def _convert_pars(pars, mapping): |
---|
| 41 | """ |
---|
| 42 | Rename the parameters and any associated polydispersity attributes. |
---|
| 43 | """ |
---|
| 44 | newpars = pars.copy() |
---|
| 45 | for new, old in mapping.items(): |
---|
| 46 | if old == new: continue |
---|
| 47 | for pd, dot in PD_DOT: |
---|
| 48 | if old+dot in newpars: |
---|
| 49 | if new is not None: |
---|
| 50 | newpars[new+pd] = pars[old+dot] |
---|
| 51 | del newpars[old+dot] |
---|
| 52 | return newpars |
---|
| 53 | |
---|
| 54 | def _rescale_sld(pars): |
---|
| 55 | """ |
---|
| 56 | rescale all sld parameters in the new model definition by 1e6 so the |
---|
| 57 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
| 58 | new model definition end with sld. |
---|
| 59 | """ |
---|
[17bbadd] | 60 | return dict((p, (v*1e6 if p.endswith('sld') |
---|
| 61 | else v*1e-15 if 'ndensity' in p |
---|
| 62 | else v)) |
---|
[3964f92] | 63 | for p, v in pars.items()) |
---|
| 64 | |
---|
| 65 | def convert_model(name, pars): |
---|
| 66 | """ |
---|
| 67 | Convert model from old style parameter names to new style. |
---|
| 68 | """ |
---|
| 69 | _, _ = name, pars # lint |
---|
| 70 | raise NotImplementedError |
---|
| 71 | # need to load all new models in order to determine old=>new |
---|
| 72 | # model name mapping |
---|
| 73 | |
---|
| 74 | def _unscale_sld(pars): |
---|
| 75 | """ |
---|
| 76 | rescale all sld parameters in the new model definition by 1e6 so the |
---|
| 77 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
| 78 | new model definition end with sld. |
---|
| 79 | """ |
---|
[17bbadd] | 80 | return dict((p, (v*1e-6 if p.endswith('sld') |
---|
| 81 | else v*1e15 if 'ndensity' in p |
---|
| 82 | else v)) |
---|
[3964f92] | 83 | for p, v in pars.items()) |
---|
| 84 | |
---|
| 85 | def _remove_pd(pars, key, name): |
---|
| 86 | """ |
---|
| 87 | Remove polydispersity from the parameter list. |
---|
| 88 | |
---|
| 89 | Note: operates in place |
---|
| 90 | """ |
---|
| 91 | # Bumps style parameter names |
---|
| 92 | pd = pars.pop(key+".width", 0.0) |
---|
| 93 | pd_n = pars.pop(key+".npts", 0) |
---|
| 94 | if pd != 0.0 and pd_n != 0: |
---|
| 95 | warnings.warn("parameter %s not polydisperse in sasview %s"%(key, name)) |
---|
| 96 | pars.pop(key+".nsigmas", None) |
---|
| 97 | pars.pop(key+".type", None) |
---|
| 98 | return pars |
---|
| 99 | |
---|
| 100 | def _revert_pars(pars, mapping): |
---|
| 101 | """ |
---|
| 102 | Rename the parameters and any associated polydispersity attributes. |
---|
| 103 | """ |
---|
| 104 | newpars = pars.copy() |
---|
| 105 | |
---|
| 106 | for new, old in mapping.items(): |
---|
| 107 | for pd, dot in PD_DOT: |
---|
| 108 | if old and old+pd == new+dot: |
---|
| 109 | continue |
---|
| 110 | if new+pd in newpars: |
---|
| 111 | if old is not None: |
---|
| 112 | newpars[old+dot] = pars[new+pd] |
---|
| 113 | del newpars[new+pd] |
---|
| 114 | for k in list(newpars.keys()): |
---|
| 115 | for pd, dot in PD_DOT[1:]: # skip "" => "" |
---|
| 116 | if k.endswith(pd): |
---|
| 117 | newpars[k[:-len(pd)]+dot] = newpars[k] |
---|
| 118 | del newpars[k] |
---|
| 119 | return newpars |
---|
| 120 | |
---|
[17bbadd] | 121 | def revert_pars(model_info, pars): |
---|
[3964f92] | 122 | """ |
---|
| 123 | Convert model from new style parameter names to old style. |
---|
| 124 | """ |
---|
[17bbadd] | 125 | mapping = model_info['oldpars'] |
---|
[3964f92] | 126 | oldpars = _revert_pars(_unscale_sld(pars), mapping) |
---|
| 127 | |
---|
| 128 | # Note: update compare.constrain_pars to match |
---|
[17bbadd] | 129 | name = model_info['id'] |
---|
[3964f92] | 130 | if name in MODELS_WITHOUT_SCALE: |
---|
| 131 | if oldpars.pop('scale', 1.0) != 1.0: |
---|
| 132 | warnings.warn("parameter scale not used in sasview %s"%name) |
---|
[17bbadd] | 133 | if name in MODELS_WITHOUT_BACKGROUND: |
---|
[3964f92] | 134 | if oldpars.pop('background', 0.0) != 0.0: |
---|
| 135 | warnings.warn("parameter background not used in sasview %s"%name) |
---|
[17bbadd] | 136 | |
---|
| 137 | # If it is a product model P*S, then check the individual forms for special |
---|
| 138 | # cases. Note: despite the structure factor alone not having scale or |
---|
| 139 | # background, the product model does, so this is below the test for |
---|
| 140 | # models without scale or background. |
---|
| 141 | namelist = name.split('*') if '*' in name else [name] |
---|
| 142 | for name in namelist: |
---|
| 143 | if name == 'pearl_necklace': |
---|
| 144 | _remove_pd(oldpars, 'num_pearls', name) |
---|
| 145 | _remove_pd(oldpars, 'thick_string', name) |
---|
| 146 | elif name == 'core_shell_parallelepiped': |
---|
| 147 | _remove_pd(oldpars, 'rimA', name) |
---|
| 148 | _remove_pd(oldpars, 'rimB', name) |
---|
| 149 | _remove_pd(oldpars, 'rimC', name) |
---|
| 150 | elif name == 'rpa': |
---|
| 151 | # convert scattering lengths from femtometers to centimeters |
---|
| 152 | for p in "La", "Lb", "Lc", "Ld": |
---|
| 153 | if p in oldpars: oldpars[p] *= 1e-13 |
---|
| 154 | |
---|
| 155 | return oldpars |
---|
| 156 | |
---|
| 157 | def constrain_new_to_old(model_info, pars): |
---|
[3964f92] | 158 | """ |
---|
| 159 | Restrict parameter values to those that will match sasview. |
---|
| 160 | """ |
---|
[17bbadd] | 161 | name = model_info['id'] |
---|
[3964f92] | 162 | # Note: update convert.revert_model to match |
---|
| 163 | if name in MODELS_WITHOUT_SCALE: |
---|
| 164 | pars['scale'] = 1 |
---|
[17bbadd] | 165 | if name in MODELS_WITHOUT_BACKGROUND: |
---|
[0a4628d] | 166 | pars['background'] = 0 |
---|
[667a6f2] | 167 | # sasview multiplies background by structure factor |
---|
| 168 | if '*' in name: |
---|
| 169 | pars['background'] = 0 |
---|
[9a66e65] | 170 | |
---|
[17bbadd] | 171 | # If it is a product model P*S, then check the individual forms for special |
---|
| 172 | # cases. Note: despite the structure factor alone not having scale or |
---|
| 173 | # background, the product model does, so this is below the test for |
---|
| 174 | # models without scale or background. |
---|
| 175 | namelist = name.split('*') if '*' in name else [name] |
---|
| 176 | for name in namelist: |
---|
| 177 | if name == 'pearl_necklace': |
---|
| 178 | pars['string_thickness_pd_n'] = 0 |
---|
| 179 | pars['number_of_pearls_pd_n'] = 0 |
---|
| 180 | elif name == 'line': |
---|
| 181 | pars['scale'] = 1 |
---|
| 182 | pars['background'] = 0 |
---|
| 183 | elif name == 'rpa': |
---|
| 184 | pars['case_num'] = int(pars['case_num']) |
---|