source: sasmodels/sasmodels/convert.py @ a503bfd

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

move sasview→sasmodels conversion info to model definition

  • 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                newpars[new+variant] = pars[old+variant]
16                del newpars[old+variant]
17        # Sasview style parameter names
18        for variant in (".width", ".nsigmas", ".type", ".npts"):
19            if old+variant in newpars:
20                newpars[new+variant] = pars[old+variant]
21                del newpars[old+variant]
22    return newpars
23
24def _rescale_sld(pars):
25    """
26    rescale all sld parameters in the new model definition by 1e6 so the
27    numbers are nicer.  Relies on the fact that all sld parameters in the
28    new model definition end with sld.
29    """
30    return dict((p, (v*1e6 if p.endswith('sld') else v))
31                for p,v in pars.items())
32
33def convert_model(name, pars):
34    """
35    Convert model from old style parameter names to new style.
36    """
37    raise NotImplementedError
38    # need to load all new models in order to determine old=>new
39    # model name mapping
40
41def _unscale_sld(pars):
42    """
43    rescale all sld parameters in the new model definition by 1e6 so the
44    numbers are nicer.  Relies on the fact that all sld parameters in the
45    new model definition end with sld.
46    """
47    return dict((p, (v*1e-6 if p.endswith('sld') else v))
48                for p,v in pars.items())
49
50def revert_model(name, pars):
51    """
52    Convert model from new style parameter names to old style.
53    """
54    sasmodels = __import__('sasmodels.models.'+name)
55    newmodel = getattr(sasmodels.models, name, None)
56    mapping = newmodel.oldpars
57    oldname = newmodel.oldname
58    oldpars = _rename_pars(_unscale_sld(pars), mapping)
59    return oldname, oldpars
60
Note: See TracBrowser for help on using the repository browser.