source: sasmodels/sasmodels/convert.py @ fd1c792

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since fd1c792 was fd1c792, checked in by pkienzle, 9 years ago

support scale⇒None in compare models

  • Property mode set to 100644
File size: 2.0 KB
Line 
1"""
2Convert models to and from sasview.
3"""
4
5def _rename_pars(pars, mapping):
6    """
7    Rename the parameters and any associated polydispersity attributes.
8    """
9    newpars = pars.copy()
10    for old,new in mapping.items():
11        if old == new: continue
12        # Bumps style parameter names
13        for variant in ("", "_pd", "_pd_n", "_pd_nsigma", "_pd_type"):
14            if old+variant in newpars:
15                if new is not None:
16                    newpars[new+variant] = pars[old+variant]
17                del newpars[old+variant]
18        # Sasview style parameter names
19        for variant in (".width", ".nsigmas", ".type", ".npts"):
20            if old+variant in newpars:
21                if new is not None:
22                    newpars[new+variant] = pars[old+variant]
23                del newpars[old+variant]
24    return newpars
25
26def _rescale_sld(pars):
27    """
28    rescale all sld parameters in the new model definition by 1e6 so the
29    numbers are nicer.  Relies on the fact that all sld parameters in the
30    new model definition end with sld.
31    """
32    return dict((p, (v*1e6 if p.endswith('sld') else v))
33                for p,v in pars.items())
34
35def convert_model(name, pars):
36    """
37    Convert model from old style parameter names to new style.
38    """
39    raise NotImplementedError
40    # need to load all new models in order to determine old=>new
41    # model name mapping
42
43def _unscale_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*1e-6 if p.endswith('sld') else v))
50                for p,v in pars.items())
51
52def revert_model(name, pars):
53    """
54    Convert model from new style parameter names to old style.
55    """
56    sasmodels = __import__('sasmodels.models.'+name)
57    newmodel = getattr(sasmodels.models, name, None)
58    mapping = newmodel.oldpars
59    oldname = newmodel.oldname
60    oldpars = _rename_pars(_unscale_sld(pars), mapping)
61    return oldname, oldpars
62
Note: See TracBrowser for help on using the repository browser.