source: sasmodels/sasmodels/convert.py @ 5d4777d

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 5d4777d was 5d4777d, checked in by Paul Kienzle <pkienzle@…>, 10 years ago

reorganize, check and update models

  • Property mode set to 100644
File size: 3.4 KB
Line 
1"""
2Convert models to and from sasview.
3"""
4PARAMETER_MAP = {
5    'CylinderModel': dict(
6        name='cylinder',
7        cyl_theta='theta', cyl_phi='phi',
8        sldCyl='sld', sldSolv='solvent_sld',
9        ),
10    'EllipsoidModel': dict(
11        name='ellipsoid',
12        axis_theta='theta', axis_phi='phi',
13        sldEll='sld', sldSolv='solvent_sld',
14        radius_a='rpolar', radius_b='requatorial',
15        ),
16    'CoreShellCylinderModel': dict(
17        name='core_shell_cylinder',
18        axis_theta='theta', axis_phi='phi',
19        ),
20    'TriaxialEllipsoidModel': dict(
21        name='triaxial_ellipsoid',
22        axis_theta='theta', axis_phi='phi', axis_psi='psi',
23        sldEll='sld', sldSolv='solvent_sld',
24        semi_axisA='req_minor', semi_axisB='req_major', semi_axisC='rpolar',
25        ),
26    'LamellarModel': dict(
27        name='lamellar',
28        sld_bi='sld', sld_sol='solvent_sld',
29        bi_thick='thickness',
30        ),
31    'CappedCylinderModel': dict(
32        name='capped_cylinder',
33        sld_capcyl='sld', sld_solv='solvent_sld',
34        len_cyl='length', rad_cyl='radius', rad_cap='cap_radius',
35        ),
36    'SphereModel': dict(
37        name='sphere',
38        sldSph='sld', sldSolv='solvent_sld',
39        #radius='radius',  # DO NOT LIST IDENTICAL PARAMETERS!
40        ),
41    }
42
43def _reverse_map():
44    retval = {}
45    for old_name,old_map in PARAMETER_MAP.items():
46        new_name = old_map['name']
47        new_map = dict((v,k) for k,v in old_map.items() if k != 'name')
48        new_map['name'] = old_name
49        retval[new_name] = new_map
50    return retval
51REVERSE_MAP = _reverse_map()
52del _reverse_map
53
54
55def _rename_pars(pars, mapping):
56    """
57    Rename the parameters and any associated polydispersity attributes.
58    """
59    newpars = pars.copy()
60    for old,new in mapping.items():
61        # Bumps style parameter names
62        for variant in ("", "_pd", "_pd_n", "_pd_nsigma", "_pd_type"):
63            if old+variant in newpars:
64                newpars[new+variant] = pars[old+variant]
65                del newpars[old+variant]
66        # Sasview style parameter names
67        for variant in (".width", ".nsigmas", ".type", ".npts"):
68            if old+variant in newpars:
69                newpars[new+variant] = pars[old+variant]
70                del newpars[old+variant]
71    return newpars
72
73def _rescale_sld(pars):
74    """
75    rescale all sld parameters in the new model definition by 1e6 so the
76    numbers are nicer.  Relies on the fact that all sld parameters in the
77    new model definition end with sld.
78    """
79    return dict((p, (v*1e6 if p.endswith('sld') else v))
80                for p,v in pars.items())
81
82def convert_model(name, pars):
83    """
84    Convert model from old style parameter names to new style.
85    """
86    mapping = PARAMETER_MAP[name]
87    newname = mapping['name']
88    newpars = _rescale_sld(_rename_pars(pars, mapping))
89    return newname, newpars
90
91def _unscale_sld(pars):
92    """
93    rescale all sld parameters in the new model definition by 1e6 so the
94    numbers are nicer.  Relies on the fact that all sld parameters in the
95    new model definition end with sld.
96    """
97    return dict((p, (v*1e-6 if p.endswith('sld') else v))
98                for p,v in pars.items())
99
100def revert_model(name, pars):
101    """
102    Convert model from new style parameter names to old style.
103    """
104    mapping = REVERSE_MAP[name]
105    oldname = mapping['name']
106    oldpars = _rename_pars(_unscale_sld(pars), mapping)
107    return oldname, oldpars
108
Note: See TracBrowser for help on using the repository browser.