source: sasmodels/sasmodels/convert.py @ d18582e

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since d18582e was d15a908, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

doc and delint

  • Property mode set to 100644
File size: 5.0 KB
Line 
1"""
2Convert models to and from sasview.
3"""
4import 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.
8MODELS_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.
19MODELS_WITHOUT_BACKGROUND = [
20    'guinier',
21]
22
23PD_DOT = [
24    ("", ""),
25    ("_pd", ".width"),
26    ("_pd_n", ".npts"),
27    ("_pd_nsigma", ".nsigmas"),
28    ("_pd_type", ".type"),
29    ]
30def _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
44def _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
53def 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
62def _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
71def _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
86def _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
107def 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
138def 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
Note: See TracBrowser for help on using the repository browser.