source: sasmodels/sasmodels/convert.py @ 34d6cab

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 34d6cab was 34d6cab, checked in by wojciech, 8 years ago

Documnentation and test clean-ups

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