[5d4777d] | 1 | """ |
---|
| 2 | Convert models to and from sasview. |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | def _rename_pars(pars, mapping): |
---|
| 6 | """ |
---|
| 7 | Rename the parameters and any associated polydispersity attributes. |
---|
| 8 | """ |
---|
| 9 | newpars = pars.copy() |
---|
| 10 | for old,new in mapping.items(): |
---|
[f4cf580] | 11 | if old == new: continue |
---|
[5d4777d] | 12 | # Bumps style parameter names |
---|
| 13 | for variant in ("", "_pd", "_pd_n", "_pd_nsigma", "_pd_type"): |
---|
| 14 | if old+variant in newpars: |
---|
[fd1c792] | 15 | if new is not None: |
---|
| 16 | newpars[new+variant] = pars[old+variant] |
---|
[5d4777d] | 17 | del newpars[old+variant] |
---|
| 18 | # Sasview style parameter names |
---|
| 19 | for variant in (".width", ".nsigmas", ".type", ".npts"): |
---|
| 20 | if old+variant in newpars: |
---|
[fd1c792] | 21 | if new is not None: |
---|
| 22 | newpars[new+variant] = pars[old+variant] |
---|
[5d4777d] | 23 | del newpars[old+variant] |
---|
| 24 | return newpars |
---|
| 25 | |
---|
| 26 | def _rescale_sld(pars): |
---|
| 27 | """ |
---|
| 28 | rescale all sld parameters in the new model definition by 1e6 so the |
---|
| 29 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
| 30 | new model definition end with sld. |
---|
| 31 | """ |
---|
| 32 | return dict((p, (v*1e6 if p.endswith('sld') else v)) |
---|
| 33 | for p,v in pars.items()) |
---|
| 34 | |
---|
| 35 | def convert_model(name, pars): |
---|
| 36 | """ |
---|
| 37 | Convert model from old style parameter names to new style. |
---|
| 38 | """ |
---|
[3c56da87] | 39 | _,_ = name,pars # lint |
---|
[a503bfd] | 40 | raise NotImplementedError |
---|
| 41 | # need to load all new models in order to determine old=>new |
---|
| 42 | # model name mapping |
---|
[5d4777d] | 43 | |
---|
| 44 | def _unscale_sld(pars): |
---|
| 45 | """ |
---|
| 46 | rescale all sld parameters in the new model definition by 1e6 so the |
---|
| 47 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
| 48 | new model definition end with sld. |
---|
| 49 | """ |
---|
| 50 | return dict((p, (v*1e-6 if p.endswith('sld') else v)) |
---|
| 51 | for p,v in pars.items()) |
---|
| 52 | |
---|
[aa4946b] | 53 | def revert_model(model_definition, pars): |
---|
[5d4777d] | 54 | """ |
---|
| 55 | Convert model from new style parameter names to old style. |
---|
| 56 | """ |
---|
[aa4946b] | 57 | mapping = model_definition.oldpars |
---|
| 58 | oldname = model_definition.oldname |
---|
[5d4777d] | 59 | oldpars = _rename_pars(_unscale_sld(pars), mapping) |
---|
| 60 | return oldname, oldpars |
---|
| 61 | |
---|