[aa4946b] | 1 | """ |
---|
| 2 | Core model handling routines. |
---|
| 3 | """ |
---|
[f734e7d] | 4 | |
---|
| 5 | from os.path import basename, dirname, join as joinpath |
---|
| 6 | from glob import glob |
---|
| 7 | |
---|
| 8 | import numpy as np |
---|
| 9 | |
---|
| 10 | from . import models |
---|
| 11 | from . import weights |
---|
[aa4946b] | 12 | from . import generate |
---|
[f734e7d] | 13 | |
---|
[aa4946b] | 14 | from . import kernelpy |
---|
| 15 | from . import kerneldll |
---|
[f734e7d] | 16 | try: |
---|
[aa4946b] | 17 | from . import kernelcl |
---|
| 18 | HAVE_OPENCL = True |
---|
[3c56da87] | 19 | except: |
---|
[aa4946b] | 20 | HAVE_OPENCL = False |
---|
| 21 | |
---|
[190fc2b] | 22 | __all__ = [ |
---|
| 23 | "list_models", "load_model_definition", "precompile_dll", |
---|
| 24 | "load_model", "make_kernel", "call_kernel", "call_ER", "call_VR", |
---|
| 25 | ] |
---|
[f734e7d] | 26 | |
---|
| 27 | def list_models(): |
---|
[aa4946b] | 28 | """ |
---|
| 29 | Return the list of available models on the model path. |
---|
| 30 | """ |
---|
[f734e7d] | 31 | root = dirname(__file__) |
---|
| 32 | files = sorted(glob(joinpath(root, 'models', "[a-zA-Z]*.py"))) |
---|
| 33 | available_models = [basename(f)[:-3] for f in files] |
---|
| 34 | return available_models |
---|
| 35 | |
---|
[aa4946b] | 36 | |
---|
[f734e7d] | 37 | def load_model_definition(model_name): |
---|
[aa4946b] | 38 | """ |
---|
| 39 | Load a model definition given the model name. |
---|
[1d4017a] | 40 | |
---|
| 41 | This returns a handle to the module defining the model. This can be |
---|
| 42 | used with functions in generate to build the docs or extract model info. |
---|
[aa4946b] | 43 | """ |
---|
[f734e7d] | 44 | __import__('sasmodels.models.'+model_name) |
---|
| 45 | model_definition = getattr(models, model_name, None) |
---|
| 46 | return model_definition |
---|
| 47 | |
---|
[aa4946b] | 48 | |
---|
| 49 | def precompile_dll(model_name, dtype="double"): |
---|
| 50 | """ |
---|
| 51 | Precompile the dll for a model. |
---|
| 52 | |
---|
| 53 | Returns the path to the compiled model. |
---|
| 54 | |
---|
| 55 | This can be used when build the windows distribution of sasmodels |
---|
| 56 | (which may be missing the OpenCL driver and the dll compiler), or |
---|
| 57 | otherwise sharing models with windows users who do not have a compiler. |
---|
| 58 | |
---|
| 59 | See :func:`sasmodels.kerneldll.make_dll` for details on controlling the |
---|
| 60 | dll path and the allowed floating point precision. |
---|
| 61 | """ |
---|
| 62 | model_definition = load_model_definition(model_name) |
---|
| 63 | source, info = generate.make(model_definition) |
---|
| 64 | return kerneldll.make_dll(source, info, dtype=dtype) |
---|
| 65 | |
---|
| 66 | |
---|
[bcd3aa3] | 67 | def isstr(s): |
---|
[d15a908] | 68 | """ |
---|
| 69 | Return True if *s* is a string-like object. |
---|
| 70 | """ |
---|
[bcd3aa3] | 71 | try: s + '' |
---|
| 72 | except: return False |
---|
| 73 | return True |
---|
| 74 | |
---|
[d18582e] | 75 | def load_model(model_definition, dtype=None, platform="ocl"): |
---|
[aa4946b] | 76 | """ |
---|
| 77 | Prepare the model for the default execution platform. |
---|
| 78 | |
---|
| 79 | This will return an OpenCL model, a DLL model or a python model depending |
---|
| 80 | on the model and the computing platform. |
---|
| 81 | |
---|
[bcd3aa3] | 82 | *model_definition* is the python module which defines the model. If the |
---|
| 83 | model name is given instead, then :func:`load_model_definition` will be |
---|
| 84 | called with the model name. |
---|
| 85 | |
---|
[aa4946b] | 86 | *dtype* indicates whether the model should use single or double precision |
---|
| 87 | for the calculation. Any valid numpy single or double precision identifier |
---|
| 88 | is valid, such as 'single', 'f', 'f32', or np.float32 for single, or |
---|
[d18582e] | 89 | 'double', 'd', 'f64' and np.float64 for double. If *None*, then use |
---|
| 90 | 'single' unless the model defines single=False. |
---|
[aa4946b] | 91 | |
---|
| 92 | *platform* should be "dll" to force the dll to be used for C models, |
---|
| 93 | otherwise it uses the default "ocl". |
---|
| 94 | """ |
---|
[bcd3aa3] | 95 | if isstr(model_definition): |
---|
| 96 | model_definition = load_model_definition(model_definition) |
---|
[d18582e] | 97 | if dtype is None: |
---|
| 98 | dtype = 'single' if getattr(model_definition, 'single', True) else 'double' |
---|
[aa4946b] | 99 | source, info = generate.make(model_definition) |
---|
| 100 | if callable(info.get('Iq', None)): |
---|
| 101 | return kernelpy.PyModel(info) |
---|
| 102 | |
---|
| 103 | ## for debugging: |
---|
| 104 | ## 1. uncomment open().write so that the source will be saved next time |
---|
| 105 | ## 2. run "python -m sasmodels.direct_model $MODELNAME" to save the source |
---|
| 106 | ## 3. recomment the open.write() and uncomment open().read() |
---|
| 107 | ## 4. rerun "python -m sasmodels.direct_model $MODELNAME" |
---|
| 108 | ## 5. uncomment open().read() so that source will be regenerated from model |
---|
| 109 | # open(info['name']+'.c','w').write(source) |
---|
| 110 | # source = open(info['name']+'.cl','r').read() |
---|
| 111 | |
---|
[d15a908] | 112 | if (platform == "dll" |
---|
[aa4946b] | 113 | or not HAVE_OPENCL |
---|
[5d316e9] | 114 | or not kernelcl.environment().has_type(dtype)): |
---|
[aa4946b] | 115 | return kerneldll.load_dll(source, info, dtype) |
---|
| 116 | else: |
---|
[cde11f0f] | 117 | return kernelcl.GpuModel(source, info, dtype) |
---|
[aa4946b] | 118 | |
---|
[f734e7d] | 119 | def make_kernel(model, q_vectors): |
---|
| 120 | """ |
---|
| 121 | Return a computation kernel from the model definition and the q input. |
---|
| 122 | """ |
---|
[eafc9fa] | 123 | return model(q_vectors) |
---|
[f734e7d] | 124 | |
---|
[3c56da87] | 125 | def get_weights(info, pars, name): |
---|
[f734e7d] | 126 | """ |
---|
| 127 | Generate the distribution for parameter *name* given the parameter values |
---|
| 128 | in *pars*. |
---|
| 129 | |
---|
[aa4946b] | 130 | Uses "name", "name_pd", "name_pd_type", "name_pd_n", "name_pd_sigma" |
---|
| 131 | from the *pars* dictionary for parameter value and parameter dispersion. |
---|
[f734e7d] | 132 | """ |
---|
[3c56da87] | 133 | relative = name in info['partype']['pd-rel'] |
---|
[7cf2cfd] | 134 | limits = info['limits'][name] |
---|
[f734e7d] | 135 | disperser = pars.get(name+'_pd_type', 'gaussian') |
---|
[3c56da87] | 136 | value = pars.get(name, info['defaults'][name]) |
---|
[f734e7d] | 137 | npts = pars.get(name+'_pd_n', 0) |
---|
| 138 | width = pars.get(name+'_pd', 0.0) |
---|
| 139 | nsigma = pars.get(name+'_pd_nsigma', 3.0) |
---|
[d15a908] | 140 | value, weight = weights.get_weights( |
---|
[7cf2cfd] | 141 | disperser, npts, width, nsigma, value, limits, relative) |
---|
| 142 | return value, weight / np.sum(weight) |
---|
[f734e7d] | 143 | |
---|
[9890053] | 144 | def dispersion_mesh(pars): |
---|
| 145 | """ |
---|
| 146 | Create a mesh grid of dispersion parameters and weights. |
---|
| 147 | |
---|
| 148 | Returns [p1,p2,...],w where pj is a vector of values for parameter j |
---|
| 149 | and w is a vector containing the products for weights for each |
---|
| 150 | parameter set in the vector. |
---|
| 151 | """ |
---|
[3c56da87] | 152 | value, weight = zip(*pars) |
---|
| 153 | if len(value) > 1: |
---|
| 154 | value = [v.flatten() for v in np.meshgrid(*value)] |
---|
| 155 | weight = np.vstack([v.flatten() for v in np.meshgrid(*weight)]) |
---|
| 156 | weight = np.prod(weight, axis=0) |
---|
| 157 | return value, weight |
---|
[9890053] | 158 | |
---|
[aa4946b] | 159 | def call_kernel(kernel, pars, cutoff=0): |
---|
| 160 | """ |
---|
| 161 | Call *kernel* returned from :func:`make_kernel` with parameters *pars*. |
---|
| 162 | |
---|
| 163 | *cutoff* is the limiting value for the product of dispersion weights used |
---|
| 164 | to perform the multidimensional dispersion calculation more quickly at a |
---|
| 165 | slight cost to accuracy. The default value of *cutoff=0* integrates over |
---|
| 166 | the entire dispersion cube. Using *cutoff=1e-5* can be 50% faster, but |
---|
| 167 | with an error of about 1%, which is usually less than the measurement |
---|
| 168 | uncertainty. |
---|
| 169 | """ |
---|
[f734e7d] | 170 | fixed_pars = [pars.get(name, kernel.info['defaults'][name]) |
---|
| 171 | for name in kernel.fixed_pars] |
---|
[3c56da87] | 172 | pd_pars = [get_weights(kernel.info, pars, name) for name in kernel.pd_pars] |
---|
[f734e7d] | 173 | return kernel(fixed_pars, pd_pars, cutoff=cutoff) |
---|
| 174 | |
---|
[3c56da87] | 175 | def call_ER(info, pars): |
---|
[aa4946b] | 176 | """ |
---|
| 177 | Call the model ER function using *pars*. |
---|
| 178 | |
---|
| 179 | *info* is either *model.info* if you have a loaded model, or *kernel.info* |
---|
| 180 | if you have a model kernel prepared for evaluation. |
---|
| 181 | """ |
---|
[3c56da87] | 182 | ER = info.get('ER', None) |
---|
[9890053] | 183 | if ER is None: |
---|
| 184 | return 1.0 |
---|
| 185 | else: |
---|
[3c56da87] | 186 | vol_pars = [get_weights(info, pars, name) |
---|
| 187 | for name in info['partype']['volume']] |
---|
| 188 | value, weight = dispersion_mesh(vol_pars) |
---|
| 189 | individual_radii = ER(*value) |
---|
[9404dd3] | 190 | #print(values[0].shape, weights.shape, fv.shape) |
---|
[3c56da87] | 191 | return np.sum(weight*individual_radii) / np.sum(weight) |
---|
[9890053] | 192 | |
---|
[3c56da87] | 193 | def call_VR(info, pars): |
---|
[aa4946b] | 194 | """ |
---|
| 195 | Call the model VR function using *pars*. |
---|
| 196 | |
---|
| 197 | *info* is either *model.info* if you have a loaded model, or *kernel.info* |
---|
| 198 | if you have a model kernel prepared for evaluation. |
---|
| 199 | """ |
---|
[3c56da87] | 200 | VR = info.get('VR', None) |
---|
[9890053] | 201 | if VR is None: |
---|
| 202 | return 1.0 |
---|
| 203 | else: |
---|
[3c56da87] | 204 | vol_pars = [get_weights(info, pars, name) |
---|
| 205 | for name in info['partype']['volume']] |
---|
| 206 | value, weight = dispersion_mesh(vol_pars) |
---|
[d15a908] | 207 | whole, part = VR(*value) |
---|
[3c56da87] | 208 | return np.sum(weight*part)/np.sum(weight*whole) |
---|
[9890053] | 209 | |
---|