[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 | |
---|
[92d38285] | 7 | from sasmodels.sasview_model import load_custom_model |
---|
| 8 | CylinderModel = load_custom_model('sasmodels/models/cylinder.py') |
---|
[87985ca] | 9 | """ |
---|
[4d76711] | 10 | from __future__ import print_function |
---|
[87985ca] | 11 | |
---|
[ce27e21] | 12 | import math |
---|
| 13 | from copy import deepcopy |
---|
[2622b3f] | 14 | import collections |
---|
[4d76711] | 15 | import traceback |
---|
| 16 | import logging |
---|
[9457498] | 17 | from os.path import basename, splitext |
---|
[ce27e21] | 18 | |
---|
[7ae2b7f] | 19 | import numpy as np # type: ignore |
---|
[ce27e21] | 20 | |
---|
[aa4946b] | 21 | from . import core |
---|
[4d76711] | 22 | from . import custom |
---|
[a80e64c] | 23 | from . import product |
---|
[72a081d] | 24 | from . import generate |
---|
[fb5914f] | 25 | from . import weights |
---|
[6d6508e] | 26 | from . import modelinfo |
---|
[bde38b5] | 27 | from .details import make_kernel_args, dispersion_mesh |
---|
[ff7119b] | 28 | |
---|
[fa5fd8d] | 29 | try: |
---|
[60f03de] | 30 | from typing import Dict, Mapping, Any, Sequence, Tuple, NamedTuple, List, Optional, Union, Callable |
---|
[fa5fd8d] | 31 | from .modelinfo import ModelInfo, Parameter |
---|
| 32 | from .kernel import KernelModel |
---|
| 33 | MultiplicityInfoType = NamedTuple( |
---|
| 34 | 'MuliplicityInfo', |
---|
| 35 | [("number", int), ("control", str), ("choices", List[str]), |
---|
| 36 | ("x_axis_label", str)]) |
---|
[60f03de] | 37 | SasviewModelType = Callable[[int], "SasviewModel"] |
---|
[fa5fd8d] | 38 | except ImportError: |
---|
| 39 | pass |
---|
| 40 | |
---|
[c95dfc63] | 41 | SUPPORT_OLD_STYLE_PLUGINS = True |
---|
| 42 | |
---|
| 43 | def _register_old_models(): |
---|
| 44 | # type: () -> None |
---|
| 45 | """ |
---|
| 46 | Place the new models into sasview under the old names. |
---|
| 47 | |
---|
| 48 | Monkey patch sas.sascalc.fit as sas.models so that sas.models.pluginmodel |
---|
| 49 | is available to the plugin modules. |
---|
| 50 | """ |
---|
| 51 | import sys |
---|
[85fe7f8] | 52 | import sas # needed in order to set sas.models |
---|
[c95dfc63] | 53 | import sas.sascalc.fit |
---|
| 54 | sys.modules['sas.models'] = sas.sascalc.fit |
---|
| 55 | sas.models = sas.sascalc.fit |
---|
| 56 | |
---|
| 57 | import sas.models |
---|
| 58 | from sasmodels.conversion_table import CONVERSION_TABLE |
---|
| 59 | for new_name, conversion in CONVERSION_TABLE.items(): |
---|
[85fe7f8] | 60 | # CoreShellEllipsoidModel => core_shell_ellipsoid:1 |
---|
| 61 | new_name = new_name.split(':')[0] |
---|
[c95dfc63] | 62 | old_name = conversion[0] |
---|
[fd19811] | 63 | module_attrs = {old_name: find_model(new_name)} |
---|
[c95dfc63] | 64 | ConstructedModule = type(old_name, (), module_attrs) |
---|
| 65 | old_path = 'sas.models.' + old_name |
---|
| 66 | setattr(sas.models, old_path, ConstructedModule) |
---|
| 67 | sys.modules[old_path] = ConstructedModule |
---|
| 68 | |
---|
[9457498] | 69 | |
---|
[fa5fd8d] | 70 | # TODO: separate x_axis_label from multiplicity info |
---|
| 71 | MultiplicityInfo = collections.namedtuple( |
---|
| 72 | 'MultiplicityInfo', |
---|
| 73 | ["number", "control", "choices", "x_axis_label"], |
---|
| 74 | ) |
---|
| 75 | |
---|
[92d38285] | 76 | MODELS = {} |
---|
| 77 | def find_model(modelname): |
---|
[b32dafd] | 78 | # type: (str) -> SasviewModelType |
---|
| 79 | """ |
---|
| 80 | Find a model by name. If the model name ends in py, try loading it from |
---|
| 81 | custom models, otherwise look for it in the list of builtin models. |
---|
| 82 | """ |
---|
[92d38285] | 83 | # TODO: used by sum/product model to load an existing model |
---|
| 84 | # TODO: doesn't handle custom models properly |
---|
| 85 | if modelname.endswith('.py'): |
---|
| 86 | return load_custom_model(modelname) |
---|
| 87 | elif modelname in MODELS: |
---|
| 88 | return MODELS[modelname] |
---|
| 89 | else: |
---|
| 90 | raise ValueError("unknown model %r"%modelname) |
---|
| 91 | |
---|
[56b2687] | 92 | |
---|
[fa5fd8d] | 93 | # TODO: figure out how to say that the return type is a subclass |
---|
[4d76711] | 94 | def load_standard_models(): |
---|
[60f03de] | 95 | # type: () -> List[SasviewModelType] |
---|
[4d76711] | 96 | """ |
---|
| 97 | Load and return the list of predefined models. |
---|
| 98 | |
---|
| 99 | If there is an error loading a model, then a traceback is logged and the |
---|
| 100 | model is not returned. |
---|
| 101 | """ |
---|
| 102 | models = [] |
---|
| 103 | for name in core.list_models(): |
---|
| 104 | try: |
---|
[92d38285] | 105 | MODELS[name] = _make_standard_model(name) |
---|
| 106 | models.append(MODELS[name]) |
---|
[ee8f734] | 107 | except Exception: |
---|
[4d76711] | 108 | logging.error(traceback.format_exc()) |
---|
[c95dfc63] | 109 | if SUPPORT_OLD_STYLE_PLUGINS: |
---|
| 110 | _register_old_models() |
---|
| 111 | |
---|
[4d76711] | 112 | return models |
---|
[de97440] | 113 | |
---|
[4d76711] | 114 | |
---|
| 115 | def load_custom_model(path): |
---|
[60f03de] | 116 | # type: (str) -> SasviewModelType |
---|
[4d76711] | 117 | """ |
---|
| 118 | Load a custom model given the model path. |
---|
[ff7119b] | 119 | """ |
---|
[4d76711] | 120 | kernel_module = custom.load_custom_kernel_module(path) |
---|
[92d38285] | 121 | try: |
---|
| 122 | model = kernel_module.Model |
---|
[9457498] | 123 | # Old style models do not set the name in the class attributes, so |
---|
| 124 | # set it here; this name will be overridden when the object is created |
---|
| 125 | # with an instance variable that has the same value. |
---|
| 126 | if model.name == "": |
---|
| 127 | model.name = splitext(basename(path))[0] |
---|
[20a70bc] | 128 | if not hasattr(model, 'filename'): |
---|
| 129 | model.filename = kernel_module.__file__ |
---|
[64f0a1c] | 130 | # For old models, treat .pyc and .py files interchangeably. |
---|
| 131 | # This is needed because of the Sum|Multi(p1,p2) types of models |
---|
| 132 | # and the convoluted way in which they are created. |
---|
| 133 | if model.filename.endswith(".py"): |
---|
| 134 | logging.info("Loading %s as .pyc", model.filename) |
---|
| 135 | model.filename = model.filename+'c' |
---|
[e4bf271] | 136 | if not hasattr(model, 'id'): |
---|
| 137 | model.id = splitext(basename(model.filename))[0] |
---|
[92d38285] | 138 | except AttributeError: |
---|
[56b2687] | 139 | model_info = modelinfo.make_model_info(kernel_module) |
---|
[92d38285] | 140 | model = _make_model_from_info(model_info) |
---|
[ed10b57] | 141 | |
---|
[2f2c70c] | 142 | # If a model name already exists and we are loading a different model, |
---|
| 143 | # use the model file name as the model name. |
---|
| 144 | if model.name in MODELS and not model.filename == MODELS[model.name].filename: |
---|
| 145 | _previous_name = model.name |
---|
| 146 | model.name = model.id |
---|
| 147 | |
---|
| 148 | # If the new model name is still in the model list (for instance, |
---|
| 149 | # if we put a cylinder.py in our plug-in directory), then append |
---|
| 150 | # an identifier. |
---|
| 151 | if model.name in MODELS and not model.filename == MODELS[model.name].filename: |
---|
| 152 | model.name = model.id + '_user' |
---|
| 153 | logging.info("Model %s already exists: using %s [%s]", _previous_name, model.name, model.filename) |
---|
[ed10b57] | 154 | |
---|
[92d38285] | 155 | MODELS[model.name] = model |
---|
| 156 | return model |
---|
[4d76711] | 157 | |
---|
[87985ca] | 158 | |
---|
[4d76711] | 159 | def _make_standard_model(name): |
---|
[60f03de] | 160 | # type: (str) -> SasviewModelType |
---|
[ff7119b] | 161 | """ |
---|
[4d76711] | 162 | Load the sasview model defined by *name*. |
---|
[72a081d] | 163 | |
---|
[4d76711] | 164 | *name* can be a standard model name or a path to a custom model. |
---|
[87985ca] | 165 | |
---|
[4d76711] | 166 | Returns a class that can be used directly as a sasview model. |
---|
[ff7119b] | 167 | """ |
---|
[4d76711] | 168 | kernel_module = generate.load_kernel_module(name) |
---|
[fa5fd8d] | 169 | model_info = modelinfo.make_model_info(kernel_module) |
---|
[4d76711] | 170 | return _make_model_from_info(model_info) |
---|
[72a081d] | 171 | |
---|
| 172 | |
---|
[a80e64c] | 173 | def MultiplicationModel(form_factor, structure_factor): |
---|
| 174 | # type: ("SasviewModel", "SasviewModel") -> "SasviewModel" |
---|
| 175 | model_info = product.make_product_info(form_factor._model_info, |
---|
| 176 | structure_factor._model_info) |
---|
| 177 | ConstructedModel = _make_model_from_info(model_info) |
---|
| 178 | return ConstructedModel() |
---|
| 179 | |
---|
[4d76711] | 180 | def _make_model_from_info(model_info): |
---|
[60f03de] | 181 | # type: (ModelInfo) -> SasviewModelType |
---|
[4d76711] | 182 | """ |
---|
| 183 | Convert *model_info* into a SasView model wrapper. |
---|
| 184 | """ |
---|
[fa5fd8d] | 185 | def __init__(self, multiplicity=None): |
---|
| 186 | SasviewModel.__init__(self, multiplicity=multiplicity) |
---|
| 187 | attrs = _generate_model_attributes(model_info) |
---|
| 188 | attrs['__init__'] = __init__ |
---|
[2f2c70c] | 189 | attrs['filename'] = model_info.filename |
---|
[60f03de] | 190 | ConstructedModel = type(model_info.name, (SasviewModel,), attrs) # type: SasviewModelType |
---|
[ce27e21] | 191 | return ConstructedModel |
---|
| 192 | |
---|
[fa5fd8d] | 193 | def _generate_model_attributes(model_info): |
---|
| 194 | # type: (ModelInfo) -> Dict[str, Any] |
---|
| 195 | """ |
---|
| 196 | Generate the class attributes for the model. |
---|
| 197 | |
---|
| 198 | This should include all the information necessary to query the model |
---|
| 199 | details so that you do not need to instantiate a model to query it. |
---|
| 200 | |
---|
| 201 | All the attributes should be immutable to avoid accidents. |
---|
| 202 | """ |
---|
| 203 | |
---|
| 204 | # TODO: allow model to override axis labels input/output name/unit |
---|
| 205 | |
---|
[a18c5b3] | 206 | # Process multiplicity |
---|
[fa5fd8d] | 207 | non_fittable = [] # type: List[str] |
---|
[04045f4] | 208 | xlabel = model_info.profile_axes[0] if model_info.profile is not None else "" |
---|
| 209 | variants = MultiplicityInfo(0, "", [], xlabel) |
---|
[a18c5b3] | 210 | for p in model_info.parameters.kernel_parameters: |
---|
[04045f4] | 211 | if p.name == model_info.control: |
---|
[fa5fd8d] | 212 | non_fittable.append(p.name) |
---|
[04045f4] | 213 | variants = MultiplicityInfo( |
---|
[ce176ca] | 214 | len(p.choices) if p.choices else int(p.limits[1]), |
---|
| 215 | p.name, p.choices, xlabel |
---|
[fa5fd8d] | 216 | ) |
---|
| 217 | break |
---|
| 218 | |
---|
[50ec515] | 219 | # Only a single drop-down list parameter available |
---|
| 220 | fun_list = [] |
---|
| 221 | for p in model_info.parameters.kernel_parameters: |
---|
| 222 | if p.choices: |
---|
| 223 | fun_list = p.choices |
---|
| 224 | if p.length > 1: |
---|
| 225 | non_fittable.extend(p.id+str(k) for k in range(1, p.length+1)) |
---|
| 226 | break |
---|
| 227 | |
---|
[a18c5b3] | 228 | # Organize parameter sets |
---|
[fa5fd8d] | 229 | orientation_params = [] |
---|
| 230 | magnetic_params = [] |
---|
| 231 | fixed = [] |
---|
[85fe7f8] | 232 | for p in model_info.parameters.user_parameters({}, is2d=True): |
---|
[fa5fd8d] | 233 | if p.type == 'orientation': |
---|
| 234 | orientation_params.append(p.name) |
---|
| 235 | orientation_params.append(p.name+".width") |
---|
| 236 | fixed.append(p.name+".width") |
---|
[32e3c9b] | 237 | elif p.type == 'magnetic': |
---|
[fa5fd8d] | 238 | orientation_params.append(p.name) |
---|
| 239 | magnetic_params.append(p.name) |
---|
| 240 | fixed.append(p.name+".width") |
---|
[a18c5b3] | 241 | |
---|
[32e3c9b] | 242 | |
---|
[a18c5b3] | 243 | # Build class dictionary |
---|
| 244 | attrs = {} # type: Dict[str, Any] |
---|
| 245 | attrs['_model_info'] = model_info |
---|
| 246 | attrs['name'] = model_info.name |
---|
| 247 | attrs['id'] = model_info.id |
---|
| 248 | attrs['description'] = model_info.description |
---|
| 249 | attrs['category'] = model_info.category |
---|
| 250 | attrs['is_structure_factor'] = model_info.structure_factor |
---|
| 251 | attrs['is_form_factor'] = model_info.ER is not None |
---|
| 252 | attrs['is_multiplicity_model'] = variants[0] > 1 |
---|
| 253 | attrs['multiplicity_info'] = variants |
---|
[fa5fd8d] | 254 | attrs['orientation_params'] = tuple(orientation_params) |
---|
| 255 | attrs['magnetic_params'] = tuple(magnetic_params) |
---|
| 256 | attrs['fixed'] = tuple(fixed) |
---|
| 257 | attrs['non_fittable'] = tuple(non_fittable) |
---|
[50ec515] | 258 | attrs['fun_list'] = tuple(fun_list) |
---|
[fa5fd8d] | 259 | |
---|
| 260 | return attrs |
---|
[4d76711] | 261 | |
---|
[ce27e21] | 262 | class SasviewModel(object): |
---|
| 263 | """ |
---|
| 264 | Sasview wrapper for opencl/ctypes model. |
---|
| 265 | """ |
---|
[fa5fd8d] | 266 | # Model parameters for the specific model are set in the class constructor |
---|
| 267 | # via the _generate_model_attributes function, which subclasses |
---|
| 268 | # SasviewModel. They are included here for typing and documentation |
---|
| 269 | # purposes. |
---|
| 270 | _model = None # type: KernelModel |
---|
| 271 | _model_info = None # type: ModelInfo |
---|
| 272 | #: load/save name for the model |
---|
| 273 | id = None # type: str |
---|
| 274 | #: display name for the model |
---|
| 275 | name = None # type: str |
---|
| 276 | #: short model description |
---|
| 277 | description = None # type: str |
---|
| 278 | #: default model category |
---|
| 279 | category = None # type: str |
---|
| 280 | |
---|
| 281 | #: names of the orientation parameters in the order they appear |
---|
| 282 | orientation_params = None # type: Sequence[str] |
---|
| 283 | #: names of the magnetic parameters in the order they appear |
---|
| 284 | magnetic_params = None # type: Sequence[str] |
---|
| 285 | #: names of the fittable parameters |
---|
| 286 | fixed = None # type: Sequence[str] |
---|
| 287 | # TODO: the attribute fixed is ill-named |
---|
| 288 | |
---|
| 289 | # Axis labels |
---|
| 290 | input_name = "Q" |
---|
| 291 | input_unit = "A^{-1}" |
---|
| 292 | output_name = "Intensity" |
---|
| 293 | output_unit = "cm^{-1}" |
---|
| 294 | |
---|
| 295 | #: default cutoff for polydispersity |
---|
| 296 | cutoff = 1e-5 |
---|
| 297 | |
---|
| 298 | # Note: Use non-mutable values for class attributes to avoid errors |
---|
| 299 | #: parameters that are not fitted |
---|
| 300 | non_fittable = () # type: Sequence[str] |
---|
| 301 | |
---|
| 302 | #: True if model should appear as a structure factor |
---|
| 303 | is_structure_factor = False |
---|
| 304 | #: True if model should appear as a form factor |
---|
| 305 | is_form_factor = False |
---|
| 306 | #: True if model has multiplicity |
---|
| 307 | is_multiplicity_model = False |
---|
| 308 | #: Mulitplicity information |
---|
| 309 | multiplicity_info = None # type: MultiplicityInfoType |
---|
| 310 | |
---|
| 311 | # Per-instance variables |
---|
| 312 | #: parameter {name: value} mapping |
---|
| 313 | params = None # type: Dict[str, float] |
---|
| 314 | #: values for dispersion width, npts, nsigmas and type |
---|
| 315 | dispersion = None # type: Dict[str, Any] |
---|
| 316 | #: units and limits for each parameter |
---|
[60f03de] | 317 | details = None # type: Dict[str, Sequence[Any]] |
---|
| 318 | # # actual type is Dict[str, List[str, float, float]] |
---|
[04dc697] | 319 | #: multiplicity value, or None if no multiplicity on the model |
---|
[fa5fd8d] | 320 | multiplicity = None # type: Optional[int] |
---|
[04dc697] | 321 | #: memory for polydispersity array if using ArrayDispersion (used by sasview). |
---|
| 322 | _persistency_dict = None # type: Dict[str, Tuple[np.ndarray, np.ndarray]] |
---|
[fa5fd8d] | 323 | |
---|
| 324 | def __init__(self, multiplicity=None): |
---|
[04dc697] | 325 | # type: (Optional[int]) -> None |
---|
[2622b3f] | 326 | |
---|
[04045f4] | 327 | # TODO: _persistency_dict to persistency_dict throughout sasview |
---|
| 328 | # TODO: refactor multiplicity to encompass variants |
---|
| 329 | # TODO: dispersion should be a class |
---|
[fa5fd8d] | 330 | # TODO: refactor multiplicity info |
---|
| 331 | # TODO: separate profile view from multiplicity |
---|
| 332 | # The button label, x and y axis labels and scale need to be under |
---|
| 333 | # the control of the model, not the fit page. Maximum flexibility, |
---|
| 334 | # the fit page would supply the canvas and the profile could plot |
---|
| 335 | # how it wants, but this assumes matplotlib. Next level is that |
---|
| 336 | # we provide some sort of data description including title, labels |
---|
| 337 | # and lines to plot. |
---|
| 338 | |
---|
[04045f4] | 339 | # Get the list of hidden parameters given the mulitplicity |
---|
| 340 | # Don't include multiplicity in the list of parameters |
---|
[fa5fd8d] | 341 | self.multiplicity = multiplicity |
---|
[04045f4] | 342 | if multiplicity is not None: |
---|
| 343 | hidden = self._model_info.get_hidden_parameters(multiplicity) |
---|
| 344 | hidden |= set([self.multiplicity_info.control]) |
---|
| 345 | else: |
---|
| 346 | hidden = set() |
---|
[8f93522] | 347 | if self._model_info.structure_factor: |
---|
| 348 | hidden.add('scale') |
---|
| 349 | hidden.add('background') |
---|
| 350 | self._model_info.parameters.defaults['background'] = 0. |
---|
[04045f4] | 351 | |
---|
[04dc697] | 352 | self._persistency_dict = {} |
---|
[fa5fd8d] | 353 | self.params = collections.OrderedDict() |
---|
[b3a85cd] | 354 | self.dispersion = collections.OrderedDict() |
---|
[fa5fd8d] | 355 | self.details = {} |
---|
[8977226] | 356 | for p in self._model_info.parameters.user_parameters({}, is2d=True): |
---|
[04045f4] | 357 | if p.name in hidden: |
---|
[fa5fd8d] | 358 | continue |
---|
[fcd7bbd] | 359 | self.params[p.name] = p.default |
---|
[fa5fd8d] | 360 | self.details[p.id] = [p.units, p.limits[0], p.limits[1]] |
---|
[fb5914f] | 361 | if p.polydisperse: |
---|
[fa5fd8d] | 362 | self.details[p.id+".width"] = [ |
---|
| 363 | "", 0.0, 1.0 if p.relative_pd else np.inf |
---|
| 364 | ] |
---|
[fb5914f] | 365 | self.dispersion[p.name] = { |
---|
| 366 | 'width': 0, |
---|
| 367 | 'npts': 35, |
---|
| 368 | 'nsigmas': 3, |
---|
| 369 | 'type': 'gaussian', |
---|
| 370 | } |
---|
[ce27e21] | 371 | |
---|
[de97440] | 372 | def __get_state__(self): |
---|
[fa5fd8d] | 373 | # type: () -> Dict[str, Any] |
---|
[de97440] | 374 | state = self.__dict__.copy() |
---|
[4d76711] | 375 | state.pop('_model') |
---|
[de97440] | 376 | # May need to reload model info on set state since it has pointers |
---|
| 377 | # to python implementations of Iq, etc. |
---|
| 378 | #state.pop('_model_info') |
---|
| 379 | return state |
---|
| 380 | |
---|
| 381 | def __set_state__(self, state): |
---|
[fa5fd8d] | 382 | # type: (Dict[str, Any]) -> None |
---|
[de97440] | 383 | self.__dict__ = state |
---|
[fb5914f] | 384 | self._model = None |
---|
[de97440] | 385 | |
---|
[ce27e21] | 386 | def __str__(self): |
---|
[fa5fd8d] | 387 | # type: () -> str |
---|
[ce27e21] | 388 | """ |
---|
| 389 | :return: string representation |
---|
| 390 | """ |
---|
| 391 | return self.name |
---|
| 392 | |
---|
| 393 | def is_fittable(self, par_name): |
---|
[fa5fd8d] | 394 | # type: (str) -> bool |
---|
[ce27e21] | 395 | """ |
---|
| 396 | Check if a given parameter is fittable or not |
---|
| 397 | |
---|
| 398 | :param par_name: the parameter name to check |
---|
| 399 | """ |
---|
[e758662] | 400 | return par_name in self.fixed |
---|
[ce27e21] | 401 | #For the future |
---|
| 402 | #return self.params[str(par_name)].is_fittable() |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | def getProfile(self): |
---|
[fa5fd8d] | 406 | # type: () -> (np.ndarray, np.ndarray) |
---|
[ce27e21] | 407 | """ |
---|
| 408 | Get SLD profile |
---|
| 409 | |
---|
| 410 | : return: (z, beta) where z is a list of depth of the transition points |
---|
| 411 | beta is a list of the corresponding SLD values |
---|
| 412 | """ |
---|
[745b7bb] | 413 | args = {} # type: Dict[str, Any] |
---|
[fa5fd8d] | 414 | for p in self._model_info.parameters.kernel_parameters: |
---|
| 415 | if p.id == self.multiplicity_info.control: |
---|
[745b7bb] | 416 | value = float(self.multiplicity) |
---|
[fa5fd8d] | 417 | elif p.length == 1: |
---|
[745b7bb] | 418 | value = self.params.get(p.id, np.NaN) |
---|
[fa5fd8d] | 419 | else: |
---|
[745b7bb] | 420 | value = np.array([self.params.get(p.id+str(k), np.NaN) |
---|
[b32dafd] | 421 | for k in range(1, p.length+1)]) |
---|
[745b7bb] | 422 | args[p.id] = value |
---|
| 423 | |
---|
[e7fe459] | 424 | x, y = self._model_info.profile(**args) |
---|
| 425 | return x, 1e-6*y |
---|
[ce27e21] | 426 | |
---|
| 427 | def setParam(self, name, value): |
---|
[fa5fd8d] | 428 | # type: (str, float) -> None |
---|
[ce27e21] | 429 | """ |
---|
| 430 | Set the value of a model parameter |
---|
| 431 | |
---|
| 432 | :param name: name of the parameter |
---|
| 433 | :param value: value of the parameter |
---|
| 434 | |
---|
| 435 | """ |
---|
| 436 | # Look for dispersion parameters |
---|
| 437 | toks = name.split('.') |
---|
[de0c4ba] | 438 | if len(toks) == 2: |
---|
[ce27e21] | 439 | for item in self.dispersion.keys(): |
---|
[e758662] | 440 | if item == toks[0]: |
---|
[ce27e21] | 441 | for par in self.dispersion[item]: |
---|
[e758662] | 442 | if par == toks[1]: |
---|
[ce27e21] | 443 | self.dispersion[item][par] = value |
---|
| 444 | return |
---|
| 445 | else: |
---|
| 446 | # Look for standard parameter |
---|
| 447 | for item in self.params.keys(): |
---|
[e758662] | 448 | if item == name: |
---|
[ce27e21] | 449 | self.params[item] = value |
---|
| 450 | return |
---|
| 451 | |
---|
[63b32bb] | 452 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 453 | |
---|
| 454 | def getParam(self, name): |
---|
[fa5fd8d] | 455 | # type: (str) -> float |
---|
[ce27e21] | 456 | """ |
---|
| 457 | Set the value of a model parameter |
---|
| 458 | |
---|
| 459 | :param name: name of the parameter |
---|
| 460 | |
---|
| 461 | """ |
---|
| 462 | # Look for dispersion parameters |
---|
| 463 | toks = name.split('.') |
---|
[de0c4ba] | 464 | if len(toks) == 2: |
---|
[ce27e21] | 465 | for item in self.dispersion.keys(): |
---|
[e758662] | 466 | if item == toks[0]: |
---|
[ce27e21] | 467 | for par in self.dispersion[item]: |
---|
[e758662] | 468 | if par == toks[1]: |
---|
[ce27e21] | 469 | return self.dispersion[item][par] |
---|
| 470 | else: |
---|
| 471 | # Look for standard parameter |
---|
| 472 | for item in self.params.keys(): |
---|
[e758662] | 473 | if item == name: |
---|
[ce27e21] | 474 | return self.params[item] |
---|
| 475 | |
---|
[63b32bb] | 476 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 477 | |
---|
| 478 | def getParamList(self): |
---|
[04dc697] | 479 | # type: () -> Sequence[str] |
---|
[ce27e21] | 480 | """ |
---|
| 481 | Return a list of all available parameters for the model |
---|
| 482 | """ |
---|
[04dc697] | 483 | param_list = list(self.params.keys()) |
---|
[ce27e21] | 484 | # WARNING: Extending the list with the dispersion parameters |
---|
[de0c4ba] | 485 | param_list.extend(self.getDispParamList()) |
---|
| 486 | return param_list |
---|
[ce27e21] | 487 | |
---|
| 488 | def getDispParamList(self): |
---|
[04dc697] | 489 | # type: () -> Sequence[str] |
---|
[ce27e21] | 490 | """ |
---|
[fb5914f] | 491 | Return a list of polydispersity parameters for the model |
---|
[ce27e21] | 492 | """ |
---|
[1780d59] | 493 | # TODO: fix test so that parameter order doesn't matter |
---|
[3bcb88c] | 494 | ret = ['%s.%s' % (p_name, ext) |
---|
| 495 | for p_name in self.dispersion.keys() |
---|
| 496 | for ext in ('npts', 'nsigmas', 'width')] |
---|
[9404dd3] | 497 | #print(ret) |
---|
[1780d59] | 498 | return ret |
---|
[ce27e21] | 499 | |
---|
| 500 | def clone(self): |
---|
[04dc697] | 501 | # type: () -> "SasviewModel" |
---|
[ce27e21] | 502 | """ Return a identical copy of self """ |
---|
| 503 | return deepcopy(self) |
---|
| 504 | |
---|
| 505 | def run(self, x=0.0): |
---|
[fa5fd8d] | 506 | # type: (Union[float, (float, float), List[float]]) -> float |
---|
[ce27e21] | 507 | """ |
---|
| 508 | Evaluate the model |
---|
| 509 | |
---|
| 510 | :param x: input q, or [q,phi] |
---|
| 511 | |
---|
| 512 | :return: scattering function P(q) |
---|
| 513 | |
---|
| 514 | **DEPRECATED**: use calculate_Iq instead |
---|
| 515 | """ |
---|
[de0c4ba] | 516 | if isinstance(x, (list, tuple)): |
---|
[3c56da87] | 517 | # pylint: disable=unpacking-non-sequence |
---|
[ce27e21] | 518 | q, phi = x |
---|
[60f03de] | 519 | return self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)])[0] |
---|
[ce27e21] | 520 | else: |
---|
[60f03de] | 521 | return self.calculate_Iq([x])[0] |
---|
[ce27e21] | 522 | |
---|
| 523 | |
---|
| 524 | def runXY(self, x=0.0): |
---|
[fa5fd8d] | 525 | # type: (Union[float, (float, float), List[float]]) -> float |
---|
[ce27e21] | 526 | """ |
---|
| 527 | Evaluate the model in cartesian coordinates |
---|
| 528 | |
---|
| 529 | :param x: input q, or [qx, qy] |
---|
| 530 | |
---|
| 531 | :return: scattering function P(q) |
---|
| 532 | |
---|
| 533 | **DEPRECATED**: use calculate_Iq instead |
---|
| 534 | """ |
---|
[de0c4ba] | 535 | if isinstance(x, (list, tuple)): |
---|
[60f03de] | 536 | return self.calculate_Iq([x[0]], [x[1]])[0] |
---|
[ce27e21] | 537 | else: |
---|
[60f03de] | 538 | return self.calculate_Iq([x])[0] |
---|
[ce27e21] | 539 | |
---|
| 540 | def evalDistribution(self, qdist): |
---|
[04dc697] | 541 | # type: (Union[np.ndarray, Tuple[np.ndarray, np.ndarray], List[np.ndarray]]) -> np.ndarray |
---|
[d138d43] | 542 | r""" |
---|
[ce27e21] | 543 | Evaluate a distribution of q-values. |
---|
| 544 | |
---|
[d138d43] | 545 | :param qdist: array of q or a list of arrays [qx,qy] |
---|
[ce27e21] | 546 | |
---|
[d138d43] | 547 | * For 1D, a numpy array is expected as input |
---|
[ce27e21] | 548 | |
---|
[d138d43] | 549 | :: |
---|
[ce27e21] | 550 | |
---|
[d138d43] | 551 | evalDistribution(q) |
---|
[ce27e21] | 552 | |
---|
[d138d43] | 553 | where *q* is a numpy array. |
---|
[ce27e21] | 554 | |
---|
[d138d43] | 555 | * For 2D, a list of *[qx,qy]* is expected with 1D arrays as input |
---|
[ce27e21] | 556 | |
---|
[d138d43] | 557 | :: |
---|
[ce27e21] | 558 | |
---|
[d138d43] | 559 | qx = [ qx[0], qx[1], qx[2], ....] |
---|
| 560 | qy = [ qy[0], qy[1], qy[2], ....] |
---|
[ce27e21] | 561 | |
---|
[d138d43] | 562 | If the model is 1D only, then |
---|
[ce27e21] | 563 | |
---|
[d138d43] | 564 | .. math:: |
---|
[ce27e21] | 565 | |
---|
[d138d43] | 566 | q = \sqrt{q_x^2+q_y^2} |
---|
[ce27e21] | 567 | |
---|
| 568 | """ |
---|
[de0c4ba] | 569 | if isinstance(qdist, (list, tuple)): |
---|
[ce27e21] | 570 | # Check whether we have a list of ndarrays [qx,qy] |
---|
| 571 | qx, qy = qdist |
---|
[6d6508e] | 572 | if not self._model_info.parameters.has_2d: |
---|
[de0c4ba] | 573 | return self.calculate_Iq(np.sqrt(qx ** 2 + qy ** 2)) |
---|
[5d4777d] | 574 | else: |
---|
| 575 | return self.calculate_Iq(qx, qy) |
---|
[ce27e21] | 576 | |
---|
| 577 | elif isinstance(qdist, np.ndarray): |
---|
| 578 | # We have a simple 1D distribution of q-values |
---|
| 579 | return self.calculate_Iq(qdist) |
---|
| 580 | |
---|
| 581 | else: |
---|
[3c56da87] | 582 | raise TypeError("evalDistribution expects q or [qx, qy], not %r" |
---|
| 583 | % type(qdist)) |
---|
[ce27e21] | 584 | |
---|
[fa5fd8d] | 585 | def calculate_Iq(self, qx, qy=None): |
---|
| 586 | # type: (Sequence[float], Optional[Sequence[float]]) -> np.ndarray |
---|
[ff7119b] | 587 | """ |
---|
| 588 | Calculate Iq for one set of q with the current parameters. |
---|
| 589 | |
---|
| 590 | If the model is 1D, use *q*. If 2D, use *qx*, *qy*. |
---|
| 591 | |
---|
| 592 | This should NOT be used for fitting since it copies the *q* vectors |
---|
| 593 | to the card for each evaluation. |
---|
| 594 | """ |
---|
[6a0d6aa] | 595 | #core.HAVE_OPENCL = False |
---|
[fb5914f] | 596 | if self._model is None: |
---|
[d2bb604] | 597 | self._model = core.build_model(self._model_info) |
---|
[fa5fd8d] | 598 | if qy is not None: |
---|
| 599 | q_vectors = [np.asarray(qx), np.asarray(qy)] |
---|
| 600 | else: |
---|
| 601 | q_vectors = [np.asarray(qx)] |
---|
[a738209] | 602 | calculator = self._model.make_kernel(q_vectors) |
---|
[6a0d6aa] | 603 | parameters = self._model_info.parameters |
---|
| 604 | pairs = [self._get_weights(p) for p in parameters.call_parameters] |
---|
[9c1a59c] | 605 | #weights.plot_weights(self._model_info, pairs) |
---|
[bde38b5] | 606 | call_details, values, is_magnetic = make_kernel_args(calculator, pairs) |
---|
[4edec6f] | 607 | #call_details.show() |
---|
| 608 | #print("pairs", pairs) |
---|
| 609 | #print("params", self.params) |
---|
| 610 | #print("values", values) |
---|
| 611 | #print("is_mag", is_magnetic) |
---|
[6a0d6aa] | 612 | result = calculator(call_details, values, cutoff=self.cutoff, |
---|
[9eb3632] | 613 | magnetic=is_magnetic) |
---|
[a738209] | 614 | calculator.release() |
---|
[9f37726] | 615 | self._model.release() |
---|
[ce27e21] | 616 | return result |
---|
| 617 | |
---|
| 618 | def calculate_ER(self): |
---|
[fa5fd8d] | 619 | # type: () -> float |
---|
[ce27e21] | 620 | """ |
---|
| 621 | Calculate the effective radius for P(q)*S(q) |
---|
| 622 | |
---|
| 623 | :return: the value of the effective radius |
---|
| 624 | """ |
---|
[4bfd277] | 625 | if self._model_info.ER is None: |
---|
[ce27e21] | 626 | return 1.0 |
---|
| 627 | else: |
---|
[4bfd277] | 628 | value, weight = self._dispersion_mesh() |
---|
| 629 | fv = self._model_info.ER(*value) |
---|
[9404dd3] | 630 | #print(values[0].shape, weights.shape, fv.shape) |
---|
[4bfd277] | 631 | return np.sum(weight * fv) / np.sum(weight) |
---|
[ce27e21] | 632 | |
---|
| 633 | def calculate_VR(self): |
---|
[fa5fd8d] | 634 | # type: () -> float |
---|
[ce27e21] | 635 | """ |
---|
| 636 | Calculate the volf ratio for P(q)*S(q) |
---|
| 637 | |
---|
| 638 | :return: the value of the volf ratio |
---|
| 639 | """ |
---|
[4bfd277] | 640 | if self._model_info.VR is None: |
---|
[ce27e21] | 641 | return 1.0 |
---|
| 642 | else: |
---|
[4bfd277] | 643 | value, weight = self._dispersion_mesh() |
---|
| 644 | whole, part = self._model_info.VR(*value) |
---|
| 645 | return np.sum(weight * part) / np.sum(weight * whole) |
---|
[ce27e21] | 646 | |
---|
| 647 | def set_dispersion(self, parameter, dispersion): |
---|
[fa5fd8d] | 648 | # type: (str, weights.Dispersion) -> Dict[str, Any] |
---|
[ce27e21] | 649 | """ |
---|
| 650 | Set the dispersion object for a model parameter |
---|
| 651 | |
---|
| 652 | :param parameter: name of the parameter [string] |
---|
| 653 | :param dispersion: dispersion object of type Dispersion |
---|
| 654 | """ |
---|
[fa800e72] | 655 | if parameter in self.params: |
---|
[1780d59] | 656 | # TODO: Store the disperser object directly in the model. |
---|
[56b2687] | 657 | # The current method of relying on the sasview GUI to |
---|
[fa800e72] | 658 | # remember them is kind of funky. |
---|
[1780d59] | 659 | # Note: can't seem to get disperser parameters from sasview |
---|
[9c1a59c] | 660 | # (1) Could create a sasview model that has not yet been |
---|
[1780d59] | 661 | # converted, assign the disperser to one of its polydisperse |
---|
| 662 | # parameters, then retrieve the disperser parameters from the |
---|
[9c1a59c] | 663 | # sasview model. |
---|
| 664 | # (2) Could write a disperser parameter retriever in sasview. |
---|
| 665 | # (3) Could modify sasview to use sasmodels.weights dispersers. |
---|
[1780d59] | 666 | # For now, rely on the fact that the sasview only ever uses |
---|
| 667 | # new dispersers in the set_dispersion call and create a new |
---|
| 668 | # one instead of trying to assign parameters. |
---|
[ce27e21] | 669 | self.dispersion[parameter] = dispersion.get_pars() |
---|
| 670 | else: |
---|
| 671 | raise ValueError("%r is not a dispersity or orientation parameter") |
---|
| 672 | |
---|
[aa4946b] | 673 | def _dispersion_mesh(self): |
---|
[fa5fd8d] | 674 | # type: () -> List[Tuple[np.ndarray, np.ndarray]] |
---|
[ce27e21] | 675 | """ |
---|
| 676 | Create a mesh grid of dispersion parameters and weights. |
---|
| 677 | |
---|
| 678 | Returns [p1,p2,...],w where pj is a vector of values for parameter j |
---|
| 679 | and w is a vector containing the products for weights for each |
---|
| 680 | parameter set in the vector. |
---|
| 681 | """ |
---|
[4bfd277] | 682 | pars = [self._get_weights(p) |
---|
| 683 | for p in self._model_info.parameters.call_parameters |
---|
| 684 | if p.type == 'volume'] |
---|
[9eb3632] | 685 | return dispersion_mesh(self._model_info, pars) |
---|
[ce27e21] | 686 | |
---|
| 687 | def _get_weights(self, par): |
---|
[fa5fd8d] | 688 | # type: (Parameter) -> Tuple[np.ndarray, np.ndarray] |
---|
[de0c4ba] | 689 | """ |
---|
[fb5914f] | 690 | Return dispersion weights for parameter |
---|
[de0c4ba] | 691 | """ |
---|
[fa5fd8d] | 692 | if par.name not in self.params: |
---|
| 693 | if par.name == self.multiplicity_info.control: |
---|
[4edec6f] | 694 | return [self.multiplicity], [1.0] |
---|
[fa5fd8d] | 695 | else: |
---|
[8f93522] | 696 | # For hidden parameters use the default value. |
---|
| 697 | value = self._model_info.parameters.defaults.get(par.name, np.NaN) |
---|
| 698 | return [value], [1.0] |
---|
[fa5fd8d] | 699 | elif par.polydisperse: |
---|
[fb5914f] | 700 | dis = self.dispersion[par.name] |
---|
[9c1a59c] | 701 | if dis['type'] == 'array': |
---|
| 702 | value, weight = dis['values'], dis['weights'] |
---|
| 703 | else: |
---|
| 704 | value, weight = weights.get_weights( |
---|
| 705 | dis['type'], dis['npts'], dis['width'], dis['nsigmas'], |
---|
| 706 | self.params[par.name], par.limits, par.relative_pd) |
---|
[fb5914f] | 707 | return value, weight / np.sum(weight) |
---|
| 708 | else: |
---|
[4edec6f] | 709 | return [self.params[par.name]], [1.0] |
---|
[ce27e21] | 710 | |
---|
[fb5914f] | 711 | def test_model(): |
---|
[fa5fd8d] | 712 | # type: () -> float |
---|
[4d76711] | 713 | """ |
---|
| 714 | Test that a sasview model (cylinder) can be run. |
---|
| 715 | """ |
---|
| 716 | Cylinder = _make_standard_model('cylinder') |
---|
[fb5914f] | 717 | cylinder = Cylinder() |
---|
[b32dafd] | 718 | return cylinder.evalDistribution([0.1, 0.1]) |
---|
[de97440] | 719 | |
---|
[8f93522] | 720 | def test_structure_factor(): |
---|
| 721 | # type: () -> float |
---|
| 722 | """ |
---|
| 723 | Test that a sasview model (cylinder) can be run. |
---|
| 724 | """ |
---|
| 725 | Model = _make_standard_model('hardsphere') |
---|
| 726 | model = Model() |
---|
| 727 | value = model.evalDistribution([0.1, 0.1]) |
---|
| 728 | if np.isnan(value): |
---|
| 729 | raise ValueError("hardsphere returns null") |
---|
| 730 | |
---|
[04045f4] | 731 | def test_rpa(): |
---|
| 732 | # type: () -> float |
---|
| 733 | """ |
---|
| 734 | Test that a sasview model (cylinder) can be run. |
---|
| 735 | """ |
---|
| 736 | RPA = _make_standard_model('rpa') |
---|
| 737 | rpa = RPA(3) |
---|
[b32dafd] | 738 | return rpa.evalDistribution([0.1, 0.1]) |
---|
[04045f4] | 739 | |
---|
[4d76711] | 740 | |
---|
| 741 | def test_model_list(): |
---|
[fa5fd8d] | 742 | # type: () -> None |
---|
[4d76711] | 743 | """ |
---|
| 744 | Make sure that all models build as sasview models. |
---|
| 745 | """ |
---|
| 746 | from .exception import annotate_exception |
---|
| 747 | for name in core.list_models(): |
---|
| 748 | try: |
---|
| 749 | _make_standard_model(name) |
---|
| 750 | except: |
---|
| 751 | annotate_exception("when loading "+name) |
---|
| 752 | raise |
---|
| 753 | |
---|
[c95dfc63] | 754 | def test_old_name(): |
---|
| 755 | # type: () -> None |
---|
| 756 | """ |
---|
| 757 | Load and run cylinder model from sas.models.CylinderModel |
---|
| 758 | """ |
---|
| 759 | if not SUPPORT_OLD_STYLE_PLUGINS: |
---|
| 760 | return |
---|
| 761 | try: |
---|
| 762 | # if sasview is not on the path then don't try to test it |
---|
| 763 | import sas |
---|
| 764 | except ImportError: |
---|
| 765 | return |
---|
| 766 | load_standard_models() |
---|
| 767 | from sas.models.CylinderModel import CylinderModel |
---|
| 768 | CylinderModel().evalDistribution([0.1, 0.1]) |
---|
| 769 | |
---|
[fb5914f] | 770 | if __name__ == "__main__": |
---|
[ea05c87] | 771 | print("cylinder(0.1,0.1)=%g"%test_model()) |
---|