source: sasmodels/sasmodels/core.py @ 3c56da87

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

lint cleanup

  • Property mode set to 100644
File size: 3.2 KB
Line 
1__all__ = ["list_models", "load_model_cl", "load_model_dll",
2           "load_model_definition", ]
3
4from os.path import basename, dirname, join as joinpath
5from glob import glob
6
7import numpy as np
8
9from . import models
10from . import weights
11
12try:
13    from .kernelcl import load_model as load_model_cl
14except:
15    # pylint: disable=invalid-name
16    load_model_cl = None
17from .kerneldll import load_model as load_model_dll
18
19def list_models():
20    root = dirname(__file__)
21    files = sorted(glob(joinpath(root, 'models', "[a-zA-Z]*.py")))
22    available_models = [basename(f)[:-3] for f in files]
23    return available_models
24
25def load_model_definition(model_name):
26    __import__('sasmodels.models.'+model_name)
27    model_definition = getattr(models, model_name, None)
28    return model_definition
29
30def make_kernel(model, q_vectors):
31    """
32    Return a computation kernel from the model definition and the q input.
33    """
34    model_input = model.make_input(q_vectors)
35    return model(model_input)
36
37def get_weights(info, pars, name):
38    """
39    Generate the distribution for parameter *name* given the parameter values
40    in *pars*.
41
42    Searches for "name", "name_pd", "name_pd_type", "name_pd_n", "name_pd_sigma"
43    """
44    relative = name in info['partype']['pd-rel']
45    limits = info['limits']
46    disperser = pars.get(name+'_pd_type', 'gaussian')
47    value = pars.get(name, info['defaults'][name])
48    npts = pars.get(name+'_pd_n', 0)
49    width = pars.get(name+'_pd', 0.0)
50    nsigma = pars.get(name+'_pd_nsigma', 3.0)
51    value,weight = weights.get_weights(
52        disperser, npts, width, nsigma,
53        value, limits[name], relative)
54    return value,weight/np.sum(weight)
55
56def dispersion_mesh(pars):
57    """
58    Create a mesh grid of dispersion parameters and weights.
59
60    Returns [p1,p2,...],w where pj is a vector of values for parameter j
61    and w is a vector containing the products for weights for each
62    parameter set in the vector.
63    """
64    value, weight = zip(*pars)
65    if len(value) > 1:
66        value = [v.flatten() for v in np.meshgrid(*value)]
67        weight = np.vstack([v.flatten() for v in np.meshgrid(*weight)])
68        weight = np.prod(weight, axis=0)
69    return value, weight
70
71def call_kernel(kernel, pars, cutoff=1e-5):
72    fixed_pars = [pars.get(name, kernel.info['defaults'][name])
73                  for name in kernel.fixed_pars]
74    pd_pars = [get_weights(kernel.info, pars, name) for name in kernel.pd_pars]
75    return kernel(fixed_pars, pd_pars, cutoff=cutoff)
76
77def call_ER(info, pars):
78    ER = info.get('ER', None)
79    if ER is None:
80        return 1.0
81    else:
82        vol_pars = [get_weights(info, pars, name)
83                    for name in info['partype']['volume']]
84        value, weight = dispersion_mesh(vol_pars)
85        individual_radii = ER(*value)
86        #print values[0].shape, weights.shape, fv.shape
87        return np.sum(weight*individual_radii) / np.sum(weight)
88
89def call_VR(info, pars):
90    VR = info.get('VR', None)
91    if VR is None:
92        return 1.0
93    else:
94        vol_pars = [get_weights(info, pars, name)
95                    for name in info['partype']['volume']]
96        value, weight = dispersion_mesh(vol_pars)
97        whole,part = VR(*value)
98        return np.sum(weight*part)/np.sum(weight*whole)
99
Note: See TracBrowser for help on using the repository browser.