source: sasmodels/sasmodels/convert.py @ 7d4b2ae

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

Added fractal_core_shell

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