[87985ca] | 1 | """ |
---|
| 2 | Sasview model constructor. |
---|
| 3 | |
---|
| 4 | Given a module defining an OpenCL kernel such as sasmodels.models.cylinder, |
---|
| 5 | create a sasview model class to run that kernel as follows:: |
---|
| 6 | |
---|
| 7 | from sasmodels.sasview_model import make_class |
---|
| 8 | from sasmodels.models import cylinder |
---|
| 9 | CylinderModel = make_class(cylinder, dtype='single') |
---|
| 10 | |
---|
| 11 | The model parameters for sasmodels are different from those in sasview. |
---|
| 12 | When reloading previously saved models, the parameters should be converted |
---|
| 13 | using :func:`sasmodels.convert.convert`. |
---|
| 14 | """ |
---|
| 15 | |
---|
[ce27e21] | 16 | import math |
---|
| 17 | from copy import deepcopy |
---|
[ff7119b] | 18 | import warnings |
---|
[2622b3f] | 19 | import collections |
---|
[ce27e21] | 20 | |
---|
| 21 | import numpy as np |
---|
| 22 | |
---|
[aa4946b] | 23 | from . import core |
---|
[ff7119b] | 24 | |
---|
[de97440] | 25 | def standard_models(): |
---|
| 26 | return [make_class(model_name) for model_name in core.list_models()] |
---|
| 27 | |
---|
| 28 | def make_class(model_name, namestyle='name'): |
---|
[ff7119b] | 29 | """ |
---|
| 30 | Load the sasview model defined in *kernel_module*. |
---|
[87985ca] | 31 | |
---|
[de97440] | 32 | Returns a class that can be used directly as a sasview model.t |
---|
[0a82216] | 33 | |
---|
[aa4946b] | 34 | Defaults to using the new name for a model. Setting |
---|
| 35 | *namestyle='oldname'* will produce a class with a name |
---|
| 36 | compatible with SasView. |
---|
[ff7119b] | 37 | """ |
---|
[de97440] | 38 | model_info = core.load_model_info(model_name) |
---|
[32c160a] | 39 | def __init__(self, multfactor=1): |
---|
[de97440] | 40 | SasviewModel.__init__(self, model_info) |
---|
[a7684e5] | 41 | attrs = dict(__init__=__init__) |
---|
[de97440] | 42 | ConstructedModel = type(model_info[namestyle], (SasviewModel,), attrs) |
---|
[ce27e21] | 43 | return ConstructedModel |
---|
| 44 | |
---|
| 45 | class SasviewModel(object): |
---|
| 46 | """ |
---|
| 47 | Sasview wrapper for opencl/ctypes model. |
---|
| 48 | """ |
---|
[de97440] | 49 | def __init__(self, model_info): |
---|
| 50 | self._model_info = model_info |
---|
| 51 | self._kernel = None |
---|
[ce27e21] | 52 | |
---|
[de97440] | 53 | self.name = model_info['name'] |
---|
| 54 | self.oldname = model_info['oldname'] |
---|
| 55 | self.description = model_info['description'] |
---|
[ce27e21] | 56 | self.category = None |
---|
| 57 | self.multiplicity_info = None |
---|
| 58 | self.is_multifunc = False |
---|
| 59 | |
---|
| 60 | ## interpret the parameters |
---|
| 61 | ## TODO: reorganize parameter handling |
---|
| 62 | self.details = dict() |
---|
[2622b3f] | 63 | self.params = collections.OrderedDict() |
---|
[ce27e21] | 64 | self.dispersion = dict() |
---|
[de97440] | 65 | partype = model_info['partype'] |
---|
[2622b3f] | 66 | |
---|
[de97440] | 67 | for p in model_info['parameters']: |
---|
[fcd7bbd] | 68 | self.params[p.name] = p.default |
---|
| 69 | self.details[p.name] = [p.units] + p.limits |
---|
[ce27e21] | 70 | |
---|
| 71 | for name in partype['pd-2d']: |
---|
| 72 | self.dispersion[name] = { |
---|
| 73 | 'width': 0, |
---|
| 74 | 'npts': 35, |
---|
[1780d59] | 75 | 'nsigmas': 3, |
---|
[ce27e21] | 76 | 'type': 'gaussian', |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | self.orientation_params = ( |
---|
| 80 | partype['orientation'] |
---|
[de0c4ba] | 81 | + [n + '.width' for n in partype['orientation']] |
---|
[ce27e21] | 82 | + partype['magnetic']) |
---|
| 83 | self.magnetic_params = partype['magnetic'] |
---|
[de0c4ba] | 84 | self.fixed = [n + '.width' for n in partype['pd-2d']] |
---|
[ce27e21] | 85 | self.non_fittable = [] |
---|
| 86 | |
---|
| 87 | ## independent parameter name and unit [string] |
---|
[de97440] | 88 | self.input_name = model_info.get("input_name", "Q") |
---|
| 89 | self.input_unit = model_info.get("input_unit", "A^{-1}") |
---|
| 90 | self.output_name = model_info.get("output_name", "Intensity") |
---|
| 91 | self.output_unit = model_info.get("output_unit", "cm^{-1}") |
---|
[ce27e21] | 92 | |
---|
[87c722e] | 93 | ## _persistency_dict is used by sas.perspectives.fitting.basepage |
---|
[ce27e21] | 94 | ## to store dispersity reference. |
---|
| 95 | ## TODO: _persistency_dict to persistency_dict throughout sasview |
---|
| 96 | self._persistency_dict = {} |
---|
| 97 | |
---|
| 98 | ## New fields introduced for opencl rewrite |
---|
| 99 | self.cutoff = 1e-5 |
---|
| 100 | |
---|
[de97440] | 101 | def __get_state__(self): |
---|
| 102 | state = self.__dict__.copy() |
---|
| 103 | model_id = self._model_info['id'] |
---|
| 104 | state.pop('_kernel') |
---|
| 105 | # May need to reload model info on set state since it has pointers |
---|
| 106 | # to python implementations of Iq, etc. |
---|
| 107 | #state.pop('_model_info') |
---|
| 108 | return state |
---|
| 109 | |
---|
| 110 | def __set_state__(self, state): |
---|
| 111 | self.__dict__ = state |
---|
| 112 | self._kernel = None |
---|
| 113 | |
---|
[ce27e21] | 114 | def __str__(self): |
---|
| 115 | """ |
---|
| 116 | :return: string representation |
---|
| 117 | """ |
---|
| 118 | return self.name |
---|
| 119 | |
---|
| 120 | def is_fittable(self, par_name): |
---|
| 121 | """ |
---|
| 122 | Check if a given parameter is fittable or not |
---|
| 123 | |
---|
| 124 | :param par_name: the parameter name to check |
---|
| 125 | """ |
---|
| 126 | return par_name.lower() in self.fixed |
---|
| 127 | #For the future |
---|
| 128 | #return self.params[str(par_name)].is_fittable() |
---|
| 129 | |
---|
| 130 | |
---|
[3c56da87] | 131 | # pylint: disable=no-self-use |
---|
[ce27e21] | 132 | def getProfile(self): |
---|
| 133 | """ |
---|
| 134 | Get SLD profile |
---|
| 135 | |
---|
| 136 | : return: (z, beta) where z is a list of depth of the transition points |
---|
| 137 | beta is a list of the corresponding SLD values |
---|
| 138 | """ |
---|
| 139 | return None, None |
---|
| 140 | |
---|
| 141 | def setParam(self, name, value): |
---|
| 142 | """ |
---|
| 143 | Set the value of a model parameter |
---|
| 144 | |
---|
| 145 | :param name: name of the parameter |
---|
| 146 | :param value: value of the parameter |
---|
| 147 | |
---|
| 148 | """ |
---|
| 149 | # Look for dispersion parameters |
---|
| 150 | toks = name.split('.') |
---|
[de0c4ba] | 151 | if len(toks) == 2: |
---|
[ce27e21] | 152 | for item in self.dispersion.keys(): |
---|
[de0c4ba] | 153 | if item.lower() == toks[0].lower(): |
---|
[ce27e21] | 154 | for par in self.dispersion[item]: |
---|
| 155 | if par.lower() == toks[1].lower(): |
---|
| 156 | self.dispersion[item][par] = value |
---|
| 157 | return |
---|
| 158 | else: |
---|
| 159 | # Look for standard parameter |
---|
| 160 | for item in self.params.keys(): |
---|
[de0c4ba] | 161 | if item.lower() == name.lower(): |
---|
[ce27e21] | 162 | self.params[item] = value |
---|
| 163 | return |
---|
| 164 | |
---|
[63b32bb] | 165 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 166 | |
---|
| 167 | def getParam(self, name): |
---|
| 168 | """ |
---|
| 169 | Set the value of a model parameter |
---|
| 170 | |
---|
| 171 | :param name: name of the parameter |
---|
| 172 | |
---|
| 173 | """ |
---|
| 174 | # Look for dispersion parameters |
---|
| 175 | toks = name.split('.') |
---|
[de0c4ba] | 176 | if len(toks) == 2: |
---|
[ce27e21] | 177 | for item in self.dispersion.keys(): |
---|
[de0c4ba] | 178 | if item.lower() == toks[0].lower(): |
---|
[ce27e21] | 179 | for par in self.dispersion[item]: |
---|
| 180 | if par.lower() == toks[1].lower(): |
---|
| 181 | return self.dispersion[item][par] |
---|
| 182 | else: |
---|
| 183 | # Look for standard parameter |
---|
| 184 | for item in self.params.keys(): |
---|
[de0c4ba] | 185 | if item.lower() == name.lower(): |
---|
[ce27e21] | 186 | return self.params[item] |
---|
| 187 | |
---|
[63b32bb] | 188 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 189 | |
---|
| 190 | def getParamList(self): |
---|
| 191 | """ |
---|
| 192 | Return a list of all available parameters for the model |
---|
| 193 | """ |
---|
[de0c4ba] | 194 | param_list = self.params.keys() |
---|
[ce27e21] | 195 | # WARNING: Extending the list with the dispersion parameters |
---|
[de0c4ba] | 196 | param_list.extend(self.getDispParamList()) |
---|
| 197 | return param_list |
---|
[ce27e21] | 198 | |
---|
| 199 | def getDispParamList(self): |
---|
| 200 | """ |
---|
| 201 | Return a list of all available parameters for the model |
---|
| 202 | """ |
---|
[1780d59] | 203 | # TODO: fix test so that parameter order doesn't matter |
---|
[de0c4ba] | 204 | ret = ['%s.%s' % (d.lower(), p) |
---|
[de97440] | 205 | for d in self._model_info['partype']['pd-2d'] |
---|
[1780d59] | 206 | for p in ('npts', 'nsigmas', 'width')] |
---|
[9404dd3] | 207 | #print(ret) |
---|
[1780d59] | 208 | return ret |
---|
[ce27e21] | 209 | |
---|
| 210 | def clone(self): |
---|
| 211 | """ Return a identical copy of self """ |
---|
| 212 | return deepcopy(self) |
---|
| 213 | |
---|
| 214 | def run(self, x=0.0): |
---|
| 215 | """ |
---|
| 216 | Evaluate the model |
---|
| 217 | |
---|
| 218 | :param x: input q, or [q,phi] |
---|
| 219 | |
---|
| 220 | :return: scattering function P(q) |
---|
| 221 | |
---|
| 222 | **DEPRECATED**: use calculate_Iq instead |
---|
| 223 | """ |
---|
[de0c4ba] | 224 | if isinstance(x, (list, tuple)): |
---|
[3c56da87] | 225 | # pylint: disable=unpacking-non-sequence |
---|
[ce27e21] | 226 | q, phi = x |
---|
| 227 | return self.calculate_Iq([q * math.cos(phi)], |
---|
| 228 | [q * math.sin(phi)])[0] |
---|
| 229 | else: |
---|
| 230 | return self.calculate_Iq([float(x)])[0] |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | def runXY(self, x=0.0): |
---|
| 234 | """ |
---|
| 235 | Evaluate the model in cartesian coordinates |
---|
| 236 | |
---|
| 237 | :param x: input q, or [qx, qy] |
---|
| 238 | |
---|
| 239 | :return: scattering function P(q) |
---|
| 240 | |
---|
| 241 | **DEPRECATED**: use calculate_Iq instead |
---|
| 242 | """ |
---|
[de0c4ba] | 243 | if isinstance(x, (list, tuple)): |
---|
| 244 | return self.calculate_Iq([float(x[0])], [float(x[1])])[0] |
---|
[ce27e21] | 245 | else: |
---|
| 246 | return self.calculate_Iq([float(x)])[0] |
---|
| 247 | |
---|
| 248 | def evalDistribution(self, qdist): |
---|
[d138d43] | 249 | r""" |
---|
[ce27e21] | 250 | Evaluate a distribution of q-values. |
---|
| 251 | |
---|
[d138d43] | 252 | :param qdist: array of q or a list of arrays [qx,qy] |
---|
[ce27e21] | 253 | |
---|
[d138d43] | 254 | * For 1D, a numpy array is expected as input |
---|
[ce27e21] | 255 | |
---|
[d138d43] | 256 | :: |
---|
[ce27e21] | 257 | |
---|
[d138d43] | 258 | evalDistribution(q) |
---|
[ce27e21] | 259 | |
---|
[d138d43] | 260 | where *q* is a numpy array. |
---|
[ce27e21] | 261 | |
---|
[d138d43] | 262 | * For 2D, a list of *[qx,qy]* is expected with 1D arrays as input |
---|
[ce27e21] | 263 | |
---|
[d138d43] | 264 | :: |
---|
[ce27e21] | 265 | |
---|
[d138d43] | 266 | qx = [ qx[0], qx[1], qx[2], ....] |
---|
| 267 | qy = [ qy[0], qy[1], qy[2], ....] |
---|
[ce27e21] | 268 | |
---|
[d138d43] | 269 | If the model is 1D only, then |
---|
[ce27e21] | 270 | |
---|
[d138d43] | 271 | .. math:: |
---|
[ce27e21] | 272 | |
---|
[d138d43] | 273 | q = \sqrt{q_x^2+q_y^2} |
---|
[ce27e21] | 274 | |
---|
| 275 | """ |
---|
[de0c4ba] | 276 | if isinstance(qdist, (list, tuple)): |
---|
[ce27e21] | 277 | # Check whether we have a list of ndarrays [qx,qy] |
---|
| 278 | qx, qy = qdist |
---|
[de97440] | 279 | partype = self._model_info['partype'] |
---|
[5d4777d] | 280 | if not partype['orientation'] and not partype['magnetic']: |
---|
[de0c4ba] | 281 | return self.calculate_Iq(np.sqrt(qx ** 2 + qy ** 2)) |
---|
[5d4777d] | 282 | else: |
---|
| 283 | return self.calculate_Iq(qx, qy) |
---|
[ce27e21] | 284 | |
---|
| 285 | elif isinstance(qdist, np.ndarray): |
---|
| 286 | # We have a simple 1D distribution of q-values |
---|
| 287 | return self.calculate_Iq(qdist) |
---|
| 288 | |
---|
| 289 | else: |
---|
[3c56da87] | 290 | raise TypeError("evalDistribution expects q or [qx, qy], not %r" |
---|
| 291 | % type(qdist)) |
---|
[ce27e21] | 292 | |
---|
| 293 | def calculate_Iq(self, *args): |
---|
[ff7119b] | 294 | """ |
---|
| 295 | Calculate Iq for one set of q with the current parameters. |
---|
| 296 | |
---|
| 297 | If the model is 1D, use *q*. If 2D, use *qx*, *qy*. |
---|
| 298 | |
---|
| 299 | This should NOT be used for fitting since it copies the *q* vectors |
---|
| 300 | to the card for each evaluation. |
---|
| 301 | """ |
---|
[de97440] | 302 | if self._kernel is None: |
---|
| 303 | self._kernel = core.build_model(self._model_info) |
---|
[ce27e21] | 304 | q_vectors = [np.asarray(q) for q in args] |
---|
[de97440] | 305 | fn = self._kernel(q_vectors) |
---|
[ce27e21] | 306 | pars = [self.params[v] for v in fn.fixed_pars] |
---|
| 307 | pd_pars = [self._get_weights(p) for p in fn.pd_pars] |
---|
| 308 | result = fn(pars, pd_pars, self.cutoff) |
---|
[28da77d] | 309 | fn.q_input.release() |
---|
[ce27e21] | 310 | fn.release() |
---|
| 311 | return result |
---|
| 312 | |
---|
| 313 | def calculate_ER(self): |
---|
| 314 | """ |
---|
| 315 | Calculate the effective radius for P(q)*S(q) |
---|
| 316 | |
---|
| 317 | :return: the value of the effective radius |
---|
| 318 | """ |
---|
[de97440] | 319 | ER = self._model_info.get('ER', None) |
---|
[ce27e21] | 320 | if ER is None: |
---|
| 321 | return 1.0 |
---|
| 322 | else: |
---|
[aa4946b] | 323 | values, weights = self._dispersion_mesh() |
---|
[ce27e21] | 324 | fv = ER(*values) |
---|
[9404dd3] | 325 | #print(values[0].shape, weights.shape, fv.shape) |
---|
[de0c4ba] | 326 | return np.sum(weights * fv) / np.sum(weights) |
---|
[ce27e21] | 327 | |
---|
| 328 | def calculate_VR(self): |
---|
| 329 | """ |
---|
| 330 | Calculate the volf ratio for P(q)*S(q) |
---|
| 331 | |
---|
| 332 | :return: the value of the volf ratio |
---|
| 333 | """ |
---|
[de97440] | 334 | VR = self._model_info.get('VR', None) |
---|
[ce27e21] | 335 | if VR is None: |
---|
| 336 | return 1.0 |
---|
| 337 | else: |
---|
[aa4946b] | 338 | values, weights = self._dispersion_mesh() |
---|
[de0c4ba] | 339 | whole, part = VR(*values) |
---|
| 340 | return np.sum(weights * part) / np.sum(weights * whole) |
---|
[ce27e21] | 341 | |
---|
| 342 | def set_dispersion(self, parameter, dispersion): |
---|
| 343 | """ |
---|
| 344 | Set the dispersion object for a model parameter |
---|
| 345 | |
---|
| 346 | :param parameter: name of the parameter [string] |
---|
| 347 | :param dispersion: dispersion object of type Dispersion |
---|
| 348 | """ |
---|
[1780d59] | 349 | if parameter.lower() in (s.lower() for s in self.params.keys()): |
---|
| 350 | # TODO: Store the disperser object directly in the model. |
---|
| 351 | # The current method of creating one on the fly whenever it is |
---|
| 352 | # needed is kind of funky. |
---|
| 353 | # Note: can't seem to get disperser parameters from sasview |
---|
| 354 | # (1) Could create a sasview model that has not yet # been |
---|
| 355 | # converted, assign the disperser to one of its polydisperse |
---|
| 356 | # parameters, then retrieve the disperser parameters from the |
---|
| 357 | # sasview model. (2) Could write a disperser parameter retriever |
---|
| 358 | # in sasview. (3) Could modify sasview to use sasmodels.weights |
---|
| 359 | # dispersers. |
---|
| 360 | # For now, rely on the fact that the sasview only ever uses |
---|
| 361 | # new dispersers in the set_dispersion call and create a new |
---|
| 362 | # one instead of trying to assign parameters. |
---|
| 363 | from . import weights |
---|
| 364 | disperser = weights.dispersers[dispersion.__class__.__name__] |
---|
| 365 | dispersion = weights.models[disperser]() |
---|
[ce27e21] | 366 | self.dispersion[parameter] = dispersion.get_pars() |
---|
| 367 | else: |
---|
| 368 | raise ValueError("%r is not a dispersity or orientation parameter") |
---|
| 369 | |
---|
[aa4946b] | 370 | def _dispersion_mesh(self): |
---|
[ce27e21] | 371 | """ |
---|
| 372 | Create a mesh grid of dispersion parameters and weights. |
---|
| 373 | |
---|
| 374 | Returns [p1,p2,...],w where pj is a vector of values for parameter j |
---|
| 375 | and w is a vector containing the products for weights for each |
---|
| 376 | parameter set in the vector. |
---|
| 377 | """ |
---|
[de97440] | 378 | pars = self._model_info['partype']['volume'] |
---|
[aa4946b] | 379 | return core.dispersion_mesh([self._get_weights(p) for p in pars]) |
---|
[ce27e21] | 380 | |
---|
| 381 | def _get_weights(self, par): |
---|
[de0c4ba] | 382 | """ |
---|
| 383 | Return dispersion weights |
---|
| 384 | :param par parameter name |
---|
| 385 | """ |
---|
[ce27e21] | 386 | from . import weights |
---|
| 387 | |
---|
[de97440] | 388 | relative = self._model_info['partype']['pd-rel'] |
---|
| 389 | limits = self._model_info['limits'] |
---|
[ce27e21] | 390 | dis = self.dispersion[par] |
---|
[3c56da87] | 391 | value, weight = weights.get_weights( |
---|
[1780d59] | 392 | dis['type'], dis['npts'], dis['width'], dis['nsigmas'], |
---|
[ce27e21] | 393 | self.params[par], limits[par], par in relative) |
---|
[3c56da87] | 394 | return value, weight / np.sum(weight) |
---|
[de97440] | 395 | |
---|