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