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(): |
---|
11 | 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: |
---|
15 | 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] |
---|
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 | """ |
---|
39 | raise NotImplementedError |
---|
40 | # need to load all new models in order to determine old=>new |
---|
41 | # model name mapping |
---|
42 | |
---|
43 | def _unscale_sld(pars): |
---|
44 | """ |
---|
45 | rescale all sld parameters in the new model definition by 1e6 so the |
---|
46 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
47 | new model definition end with sld. |
---|
48 | """ |
---|
49 | return dict((p, (v*1e-6 if p.endswith('sld') else v)) |
---|
50 | for p,v in pars.items()) |
---|
51 | |
---|
52 | def revert_model(name, pars): |
---|
53 | """ |
---|
54 | Convert model from new style parameter names to old style. |
---|
55 | """ |
---|
56 | __import__('sasmodels.models.'+name) |
---|
57 | from . import models |
---|
58 | model = getattr(models, name, None) |
---|
59 | mapping = model.oldpars |
---|
60 | oldname = model.oldname |
---|
61 | oldpars = _rename_pars(_unscale_sld(pars), mapping) |
---|
62 | return oldname, oldpars |
---|
63 | |
---|