source: sasmodels/sasmodels/convert.py @ 97cb037

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

Converted BEPolyelectrolyte

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