source: sasmodels/sasmodels/convert.py @ 3964f92

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 3964f92 was 3964f92, checked in by butler, 7 years ago

edit convert.py to prepare for binary_hard_sphere

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