source: sasmodels/sasmodels/convert.py @ d5e650d

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since d5e650d was 5054e80, checked in by krzywon, 8 years ago

Converted the correlation length model

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