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