[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 |
---|
[b297ba9] | 17 | from os.path import basename, splitext, abspath |
---|
[9f8ade1] | 18 | try: |
---|
| 19 | import _thread as thread |
---|
| 20 | except ImportError: |
---|
| 21 | import thread |
---|
[ce27e21] | 22 | |
---|
[7ae2b7f] | 23 | import numpy as np # type: ignore |
---|
[ce27e21] | 24 | |
---|
[aa4946b] | 25 | from . import core |
---|
[4d76711] | 26 | from . import custom |
---|
[a4f1a73] | 27 | from . import kernelcl |
---|
[a80e64c] | 28 | from . import product |
---|
[72a081d] | 29 | from . import generate |
---|
[fb5914f] | 30 | from . import weights |
---|
[6d6508e] | 31 | from . import modelinfo |
---|
[bde38b5] | 32 | from .details import make_kernel_args, dispersion_mesh |
---|
[ff7119b] | 33 | |
---|
[2d81cfe] | 34 | # pylint: disable=unused-import |
---|
[fa5fd8d] | 35 | try: |
---|
[2d81cfe] | 36 | from typing import (Dict, Mapping, Any, Sequence, Tuple, NamedTuple, |
---|
| 37 | List, Optional, Union, Callable) |
---|
[fa5fd8d] | 38 | from .modelinfo import ModelInfo, Parameter |
---|
| 39 | from .kernel import KernelModel |
---|
| 40 | MultiplicityInfoType = NamedTuple( |
---|
[a9bc435] | 41 | 'MultiplicityInfo', |
---|
[fa5fd8d] | 42 | [("number", int), ("control", str), ("choices", List[str]), |
---|
| 43 | ("x_axis_label", str)]) |
---|
[60f03de] | 44 | SasviewModelType = Callable[[int], "SasviewModel"] |
---|
[fa5fd8d] | 45 | except ImportError: |
---|
| 46 | pass |
---|
[2d81cfe] | 47 | # pylint: enable=unused-import |
---|
[fa5fd8d] | 48 | |
---|
[724257c] | 49 | logger = logging.getLogger(__name__) |
---|
| 50 | |
---|
[a38b065] | 51 | calculation_lock = thread.allocate_lock() |
---|
| 52 | |
---|
[724257c] | 53 | #: True if pre-existing plugins, with the old names and parameters, should |
---|
| 54 | #: continue to be supported. |
---|
[c95dfc63] | 55 | SUPPORT_OLD_STYLE_PLUGINS = True |
---|
| 56 | |
---|
[fa5fd8d] | 57 | # TODO: separate x_axis_label from multiplicity info |
---|
| 58 | MultiplicityInfo = collections.namedtuple( |
---|
| 59 | 'MultiplicityInfo', |
---|
| 60 | ["number", "control", "choices", "x_axis_label"], |
---|
| 61 | ) |
---|
| 62 | |
---|
[724257c] | 63 | #: set of defined models (standard and custom) |
---|
| 64 | MODELS = {} # type: Dict[str, SasviewModelType] |
---|
[839fd68] | 65 | # TODO: remove unused MODEL_BY_PATH cache once sasview no longer references it |
---|
[724257c] | 66 | #: custom model {path: model} mapping so we can check timestamps |
---|
| 67 | MODEL_BY_PATH = {} # type: Dict[str, SasviewModelType] |
---|
[d321747] | 68 | #: Track modules that we have loaded so we can determine whether the model |
---|
| 69 | #: has changed since we last reloaded. |
---|
| 70 | _CACHED_MODULE = {} # type: Dict[str, "module"] |
---|
[724257c] | 71 | |
---|
[a4f1a73] | 72 | def reset_environment(): |
---|
| 73 | # type: () -> None |
---|
| 74 | """ |
---|
| 75 | Clear the compute engine context so that the GUI can change devices. |
---|
| 76 | |
---|
| 77 | This removes all compiled kernels, even those that are active on fit |
---|
| 78 | pages, but they will be restored the next time they are needed. |
---|
| 79 | """ |
---|
| 80 | kernelcl.reset_environment() |
---|
| 81 | for model in MODELS.values(): |
---|
| 82 | model._model = None |
---|
| 83 | |
---|
[92d38285] | 84 | def find_model(modelname): |
---|
[b32dafd] | 85 | # type: (str) -> SasviewModelType |
---|
| 86 | """ |
---|
| 87 | Find a model by name. If the model name ends in py, try loading it from |
---|
| 88 | custom models, otherwise look for it in the list of builtin models. |
---|
| 89 | """ |
---|
[92d38285] | 90 | # TODO: used by sum/product model to load an existing model |
---|
| 91 | # TODO: doesn't handle custom models properly |
---|
| 92 | if modelname.endswith('.py'): |
---|
| 93 | return load_custom_model(modelname) |
---|
| 94 | elif modelname in MODELS: |
---|
| 95 | return MODELS[modelname] |
---|
| 96 | else: |
---|
| 97 | raise ValueError("unknown model %r"%modelname) |
---|
| 98 | |
---|
[56b2687] | 99 | |
---|
[fa5fd8d] | 100 | # TODO: figure out how to say that the return type is a subclass |
---|
[4d76711] | 101 | def load_standard_models(): |
---|
[60f03de] | 102 | # type: () -> List[SasviewModelType] |
---|
[4d76711] | 103 | """ |
---|
| 104 | Load and return the list of predefined models. |
---|
| 105 | |
---|
| 106 | If there is an error loading a model, then a traceback is logged and the |
---|
| 107 | model is not returned. |
---|
| 108 | """ |
---|
| 109 | for name in core.list_models(): |
---|
| 110 | try: |
---|
[92d38285] | 111 | MODELS[name] = _make_standard_model(name) |
---|
[ee8f734] | 112 | except Exception: |
---|
[724257c] | 113 | logger.error(traceback.format_exc()) |
---|
[c95dfc63] | 114 | if SUPPORT_OLD_STYLE_PLUGINS: |
---|
| 115 | _register_old_models() |
---|
| 116 | |
---|
[724257c] | 117 | return list(MODELS.values()) |
---|
[de97440] | 118 | |
---|
[4d76711] | 119 | |
---|
| 120 | def load_custom_model(path): |
---|
[60f03de] | 121 | # type: (str) -> SasviewModelType |
---|
[4d76711] | 122 | """ |
---|
| 123 | Load a custom model given the model path. |
---|
[ff7119b] | 124 | """ |
---|
[724257c] | 125 | #logger.info("Loading model %s", path) |
---|
[d321747] | 126 | |
---|
| 127 | # Load the kernel module. This may already be cached by the loader, so |
---|
| 128 | # only requires checking the timestamps of the dependents. |
---|
[4d76711] | 129 | kernel_module = custom.load_custom_kernel_module(path) |
---|
[d321747] | 130 | |
---|
| 131 | # Check if the module has changed since we last looked. |
---|
| 132 | reloaded = kernel_module != _CACHED_MODULE.get(path, None) |
---|
| 133 | _CACHED_MODULE[path] = kernel_module |
---|
| 134 | |
---|
| 135 | # Turn the module into a model. We need to do this in even if the |
---|
| 136 | # model has already been loaded so that we can determine the model |
---|
| 137 | # name and retrieve it from the MODELS cache. |
---|
| 138 | model = getattr(kernel_module, 'Model', None) |
---|
| 139 | if model is not None: |
---|
[9457498] | 140 | # Old style models do not set the name in the class attributes, so |
---|
| 141 | # set it here; this name will be overridden when the object is created |
---|
| 142 | # with an instance variable that has the same value. |
---|
| 143 | if model.name == "": |
---|
| 144 | model.name = splitext(basename(path))[0] |
---|
[20a70bc] | 145 | if not hasattr(model, 'filename'): |
---|
[724257c] | 146 | model.filename = abspath(kernel_module.__file__).replace('.pyc', '.py') |
---|
[e4bf271] | 147 | if not hasattr(model, 'id'): |
---|
| 148 | model.id = splitext(basename(model.filename))[0] |
---|
[724257c] | 149 | else: |
---|
[56b2687] | 150 | model_info = modelinfo.make_model_info(kernel_module) |
---|
[bcdd6c9] | 151 | model = make_model_from_info(model_info) |
---|
[ed10b57] | 152 | |
---|
[2f2c70c] | 153 | # If a model name already exists and we are loading a different model, |
---|
| 154 | # use the model file name as the model name. |
---|
| 155 | if model.name in MODELS and not model.filename == MODELS[model.name].filename: |
---|
| 156 | _previous_name = model.name |
---|
| 157 | model.name = model.id |
---|
[bf8c271] | 158 | |
---|
[2f2c70c] | 159 | # If the new model name is still in the model list (for instance, |
---|
| 160 | # if we put a cylinder.py in our plug-in directory), then append |
---|
| 161 | # an identifier. |
---|
| 162 | if model.name in MODELS and not model.filename == MODELS[model.name].filename: |
---|
| 163 | model.name = model.id + '_user' |
---|
[724257c] | 164 | logger.info("Model %s already exists: using %s [%s]", |
---|
| 165 | _previous_name, model.name, model.filename) |
---|
[ed10b57] | 166 | |
---|
[d321747] | 167 | # Only update the model if the module has changed |
---|
| 168 | if reloaded or model.name not in MODELS: |
---|
| 169 | MODELS[model.name] = model |
---|
| 170 | |
---|
| 171 | return MODELS[model.name] |
---|
[4d76711] | 172 | |
---|
[87985ca] | 173 | |
---|
[bcdd6c9] | 174 | def make_model_from_info(model_info): |
---|
| 175 | # type: (ModelInfo) -> SasviewModelType |
---|
| 176 | """ |
---|
| 177 | Convert *model_info* into a SasView model wrapper. |
---|
| 178 | """ |
---|
| 179 | def __init__(self, multiplicity=None): |
---|
| 180 | SasviewModel.__init__(self, multiplicity=multiplicity) |
---|
| 181 | attrs = _generate_model_attributes(model_info) |
---|
| 182 | attrs['__init__'] = __init__ |
---|
| 183 | attrs['filename'] = model_info.filename |
---|
| 184 | ConstructedModel = type(model_info.name, (SasviewModel,), attrs) # type: SasviewModelType |
---|
| 185 | return ConstructedModel |
---|
| 186 | |
---|
| 187 | |
---|
[4d76711] | 188 | def _make_standard_model(name): |
---|
[60f03de] | 189 | # type: (str) -> SasviewModelType |
---|
[ff7119b] | 190 | """ |
---|
[4d76711] | 191 | Load the sasview model defined by *name*. |
---|
[72a081d] | 192 | |
---|
[4d76711] | 193 | *name* can be a standard model name or a path to a custom model. |
---|
[87985ca] | 194 | |
---|
[4d76711] | 195 | Returns a class that can be used directly as a sasview model. |
---|
[ff7119b] | 196 | """ |
---|
[4d76711] | 197 | kernel_module = generate.load_kernel_module(name) |
---|
[fa5fd8d] | 198 | model_info = modelinfo.make_model_info(kernel_module) |
---|
[bcdd6c9] | 199 | return make_model_from_info(model_info) |
---|
[72a081d] | 200 | |
---|
| 201 | |
---|
[724257c] | 202 | def _register_old_models(): |
---|
| 203 | # type: () -> None |
---|
| 204 | """ |
---|
| 205 | Place the new models into sasview under the old names. |
---|
| 206 | |
---|
| 207 | Monkey patch sas.sascalc.fit as sas.models so that sas.models.pluginmodel |
---|
| 208 | is available to the plugin modules. |
---|
| 209 | """ |
---|
| 210 | import sys |
---|
| 211 | import sas # needed in order to set sas.models |
---|
| 212 | import sas.sascalc.fit |
---|
| 213 | sys.modules['sas.models'] = sas.sascalc.fit |
---|
| 214 | sas.models = sas.sascalc.fit |
---|
| 215 | import sas.models |
---|
| 216 | from sasmodels.conversion_table import CONVERSION_TABLE |
---|
[e65c3ba] | 217 | |
---|
[724257c] | 218 | for new_name, conversion in CONVERSION_TABLE.get((3, 1, 2), {}).items(): |
---|
| 219 | # CoreShellEllipsoidModel => core_shell_ellipsoid:1 |
---|
| 220 | new_name = new_name.split(':')[0] |
---|
| 221 | old_name = conversion[0] if len(conversion) < 3 else conversion[2] |
---|
| 222 | module_attrs = {old_name: find_model(new_name)} |
---|
| 223 | ConstructedModule = type(old_name, (), module_attrs) |
---|
| 224 | old_path = 'sas.models.' + old_name |
---|
| 225 | setattr(sas.models, old_path, ConstructedModule) |
---|
| 226 | sys.modules[old_path] = ConstructedModule |
---|
| 227 | |
---|
| 228 | |
---|
[a80e64c] | 229 | def MultiplicationModel(form_factor, structure_factor): |
---|
| 230 | # type: ("SasviewModel", "SasviewModel") -> "SasviewModel" |
---|
[e65c3ba] | 231 | """ |
---|
| 232 | Returns a constructed product model from form_factor and structure_factor. |
---|
| 233 | """ |
---|
[a80e64c] | 234 | model_info = product.make_product_info(form_factor._model_info, |
---|
| 235 | structure_factor._model_info) |
---|
[bcdd6c9] | 236 | ConstructedModel = make_model_from_info(model_info) |
---|
[a06af5d] | 237 | return ConstructedModel(form_factor.multiplicity) |
---|
[a80e64c] | 238 | |
---|
[ce27e21] | 239 | |
---|
[fa5fd8d] | 240 | def _generate_model_attributes(model_info): |
---|
| 241 | # type: (ModelInfo) -> Dict[str, Any] |
---|
| 242 | """ |
---|
| 243 | Generate the class attributes for the model. |
---|
| 244 | |
---|
| 245 | This should include all the information necessary to query the model |
---|
| 246 | details so that you do not need to instantiate a model to query it. |
---|
| 247 | |
---|
| 248 | All the attributes should be immutable to avoid accidents. |
---|
| 249 | """ |
---|
| 250 | |
---|
| 251 | # TODO: allow model to override axis labels input/output name/unit |
---|
| 252 | |
---|
[a18c5b3] | 253 | # Process multiplicity |
---|
[b171acd] | 254 | control_pars = [p.id for p in model_info.parameters.kernel_parameters |
---|
| 255 | if p.is_control] |
---|
[21c93c3] | 256 | control_id = control_pars[0] if control_pars else None |
---|
[fa5fd8d] | 257 | non_fittable = [] # type: List[str] |
---|
[04045f4] | 258 | xlabel = model_info.profile_axes[0] if model_info.profile is not None else "" |
---|
| 259 | variants = MultiplicityInfo(0, "", [], xlabel) |
---|
[a18c5b3] | 260 | for p in model_info.parameters.kernel_parameters: |
---|
[21c93c3] | 261 | if p.id == control_id: |
---|
[fa5fd8d] | 262 | non_fittable.append(p.name) |
---|
[04045f4] | 263 | variants = MultiplicityInfo( |
---|
[ce176ca] | 264 | len(p.choices) if p.choices else int(p.limits[1]), |
---|
| 265 | p.name, p.choices, xlabel |
---|
[fa5fd8d] | 266 | ) |
---|
| 267 | break |
---|
| 268 | |
---|
[50ec515] | 269 | # Only a single drop-down list parameter available |
---|
| 270 | fun_list = [] |
---|
| 271 | for p in model_info.parameters.kernel_parameters: |
---|
| 272 | if p.choices: |
---|
| 273 | fun_list = p.choices |
---|
| 274 | if p.length > 1: |
---|
| 275 | non_fittable.extend(p.id+str(k) for k in range(1, p.length+1)) |
---|
| 276 | break |
---|
| 277 | |
---|
[a18c5b3] | 278 | # Organize parameter sets |
---|
[fa5fd8d] | 279 | orientation_params = [] |
---|
| 280 | magnetic_params = [] |
---|
| 281 | fixed = [] |
---|
[85fe7f8] | 282 | for p in model_info.parameters.user_parameters({}, is2d=True): |
---|
[fa5fd8d] | 283 | if p.type == 'orientation': |
---|
| 284 | orientation_params.append(p.name) |
---|
| 285 | orientation_params.append(p.name+".width") |
---|
| 286 | fixed.append(p.name+".width") |
---|
[32e3c9b] | 287 | elif p.type == 'magnetic': |
---|
[fa5fd8d] | 288 | orientation_params.append(p.name) |
---|
| 289 | magnetic_params.append(p.name) |
---|
| 290 | fixed.append(p.name+".width") |
---|
[a18c5b3] | 291 | |
---|
[32e3c9b] | 292 | |
---|
[a18c5b3] | 293 | # Build class dictionary |
---|
| 294 | attrs = {} # type: Dict[str, Any] |
---|
| 295 | attrs['_model_info'] = model_info |
---|
| 296 | attrs['name'] = model_info.name |
---|
| 297 | attrs['id'] = model_info.id |
---|
| 298 | attrs['description'] = model_info.description |
---|
| 299 | attrs['category'] = model_info.category |
---|
| 300 | attrs['is_structure_factor'] = model_info.structure_factor |
---|
[6e7ba14] | 301 | attrs['is_form_factor'] = model_info.effective_radius_type is not None |
---|
[a18c5b3] | 302 | attrs['is_multiplicity_model'] = variants[0] > 1 |
---|
| 303 | attrs['multiplicity_info'] = variants |
---|
[fa5fd8d] | 304 | attrs['orientation_params'] = tuple(orientation_params) |
---|
| 305 | attrs['magnetic_params'] = tuple(magnetic_params) |
---|
| 306 | attrs['fixed'] = tuple(fixed) |
---|
| 307 | attrs['non_fittable'] = tuple(non_fittable) |
---|
[50ec515] | 308 | attrs['fun_list'] = tuple(fun_list) |
---|
[fa5fd8d] | 309 | |
---|
| 310 | return attrs |
---|
[4d76711] | 311 | |
---|
[ce27e21] | 312 | class SasviewModel(object): |
---|
| 313 | """ |
---|
| 314 | Sasview wrapper for opencl/ctypes model. |
---|
| 315 | """ |
---|
[fa5fd8d] | 316 | # Model parameters for the specific model are set in the class constructor |
---|
| 317 | # via the _generate_model_attributes function, which subclasses |
---|
| 318 | # SasviewModel. They are included here for typing and documentation |
---|
| 319 | # purposes. |
---|
| 320 | _model = None # type: KernelModel |
---|
| 321 | _model_info = None # type: ModelInfo |
---|
| 322 | #: load/save name for the model |
---|
| 323 | id = None # type: str |
---|
| 324 | #: display name for the model |
---|
| 325 | name = None # type: str |
---|
| 326 | #: short model description |
---|
| 327 | description = None # type: str |
---|
| 328 | #: default model category |
---|
| 329 | category = None # type: str |
---|
| 330 | |
---|
| 331 | #: names of the orientation parameters in the order they appear |
---|
[724257c] | 332 | orientation_params = None # type: List[str] |
---|
[fa5fd8d] | 333 | #: names of the magnetic parameters in the order they appear |
---|
[724257c] | 334 | magnetic_params = None # type: List[str] |
---|
[fa5fd8d] | 335 | #: names of the fittable parameters |
---|
[724257c] | 336 | fixed = None # type: List[str] |
---|
[fa5fd8d] | 337 | # TODO: the attribute fixed is ill-named |
---|
| 338 | |
---|
| 339 | # Axis labels |
---|
| 340 | input_name = "Q" |
---|
| 341 | input_unit = "A^{-1}" |
---|
| 342 | output_name = "Intensity" |
---|
| 343 | output_unit = "cm^{-1}" |
---|
| 344 | |
---|
| 345 | #: default cutoff for polydispersity |
---|
| 346 | cutoff = 1e-5 |
---|
| 347 | |
---|
| 348 | # Note: Use non-mutable values for class attributes to avoid errors |
---|
| 349 | #: parameters that are not fitted |
---|
| 350 | non_fittable = () # type: Sequence[str] |
---|
| 351 | |
---|
| 352 | #: True if model should appear as a structure factor |
---|
| 353 | is_structure_factor = False |
---|
| 354 | #: True if model should appear as a form factor |
---|
| 355 | is_form_factor = False |
---|
| 356 | #: True if model has multiplicity |
---|
| 357 | is_multiplicity_model = False |
---|
[1f35235] | 358 | #: Multiplicity information |
---|
[fa5fd8d] | 359 | multiplicity_info = None # type: MultiplicityInfoType |
---|
| 360 | |
---|
| 361 | # Per-instance variables |
---|
| 362 | #: parameter {name: value} mapping |
---|
| 363 | params = None # type: Dict[str, float] |
---|
| 364 | #: values for dispersion width, npts, nsigmas and type |
---|
| 365 | dispersion = None # type: Dict[str, Any] |
---|
| 366 | #: units and limits for each parameter |
---|
[60f03de] | 367 | details = None # type: Dict[str, Sequence[Any]] |
---|
| 368 | # # actual type is Dict[str, List[str, float, float]] |
---|
[04dc697] | 369 | #: multiplicity value, or None if no multiplicity on the model |
---|
[fa5fd8d] | 370 | multiplicity = None # type: Optional[int] |
---|
[04dc697] | 371 | #: memory for polydispersity array if using ArrayDispersion (used by sasview). |
---|
| 372 | _persistency_dict = None # type: Dict[str, Tuple[np.ndarray, np.ndarray]] |
---|
[fa5fd8d] | 373 | |
---|
| 374 | def __init__(self, multiplicity=None): |
---|
[04dc697] | 375 | # type: (Optional[int]) -> None |
---|
[2622b3f] | 376 | |
---|
[04045f4] | 377 | # TODO: _persistency_dict to persistency_dict throughout sasview |
---|
| 378 | # TODO: refactor multiplicity to encompass variants |
---|
| 379 | # TODO: dispersion should be a class |
---|
[fa5fd8d] | 380 | # TODO: refactor multiplicity info |
---|
| 381 | # TODO: separate profile view from multiplicity |
---|
| 382 | # The button label, x and y axis labels and scale need to be under |
---|
| 383 | # the control of the model, not the fit page. Maximum flexibility, |
---|
| 384 | # the fit page would supply the canvas and the profile could plot |
---|
| 385 | # how it wants, but this assumes matplotlib. Next level is that |
---|
| 386 | # we provide some sort of data description including title, labels |
---|
| 387 | # and lines to plot. |
---|
| 388 | |
---|
[1f35235] | 389 | # Get the list of hidden parameters given the multiplicity |
---|
[04045f4] | 390 | # Don't include multiplicity in the list of parameters |
---|
[fa5fd8d] | 391 | self.multiplicity = multiplicity |
---|
[04045f4] | 392 | if multiplicity is not None: |
---|
| 393 | hidden = self._model_info.get_hidden_parameters(multiplicity) |
---|
| 394 | hidden |= set([self.multiplicity_info.control]) |
---|
| 395 | else: |
---|
| 396 | hidden = set() |
---|
[8f93522] | 397 | if self._model_info.structure_factor: |
---|
| 398 | hidden.add('scale') |
---|
| 399 | hidden.add('background') |
---|
[04045f4] | 400 | |
---|
[bd547d0] | 401 | # Update the parameter lists to exclude any hidden parameters |
---|
| 402 | self.magnetic_params = tuple(pname for pname in self.magnetic_params |
---|
| 403 | if pname not in hidden) |
---|
| 404 | self.orientation_params = tuple(pname for pname in self.orientation_params |
---|
| 405 | if pname not in hidden) |
---|
| 406 | |
---|
[04dc697] | 407 | self._persistency_dict = {} |
---|
[fa5fd8d] | 408 | self.params = collections.OrderedDict() |
---|
[b3a85cd] | 409 | self.dispersion = collections.OrderedDict() |
---|
[fa5fd8d] | 410 | self.details = {} |
---|
[8977226] | 411 | for p in self._model_info.parameters.user_parameters({}, is2d=True): |
---|
[04045f4] | 412 | if p.name in hidden: |
---|
[fa5fd8d] | 413 | continue |
---|
[fcd7bbd] | 414 | self.params[p.name] = p.default |
---|
[fa5fd8d] | 415 | self.details[p.id] = [p.units, p.limits[0], p.limits[1]] |
---|
[fb5914f] | 416 | if p.polydisperse: |
---|
[fa5fd8d] | 417 | self.details[p.id+".width"] = [ |
---|
| 418 | "", 0.0, 1.0 if p.relative_pd else np.inf |
---|
| 419 | ] |
---|
[fb5914f] | 420 | self.dispersion[p.name] = { |
---|
| 421 | 'width': 0, |
---|
| 422 | 'npts': 35, |
---|
| 423 | 'nsigmas': 3, |
---|
| 424 | 'type': 'gaussian', |
---|
| 425 | } |
---|
[ce27e21] | 426 | |
---|
[de97440] | 427 | def __get_state__(self): |
---|
[fa5fd8d] | 428 | # type: () -> Dict[str, Any] |
---|
[de97440] | 429 | state = self.__dict__.copy() |
---|
[4d76711] | 430 | state.pop('_model') |
---|
[de97440] | 431 | # May need to reload model info on set state since it has pointers |
---|
| 432 | # to python implementations of Iq, etc. |
---|
| 433 | #state.pop('_model_info') |
---|
| 434 | return state |
---|
| 435 | |
---|
| 436 | def __set_state__(self, state): |
---|
[fa5fd8d] | 437 | # type: (Dict[str, Any]) -> None |
---|
[de97440] | 438 | self.__dict__ = state |
---|
[fb5914f] | 439 | self._model = None |
---|
[de97440] | 440 | |
---|
[ce27e21] | 441 | def __str__(self): |
---|
[fa5fd8d] | 442 | # type: () -> str |
---|
[ce27e21] | 443 | """ |
---|
| 444 | :return: string representation |
---|
| 445 | """ |
---|
| 446 | return self.name |
---|
| 447 | |
---|
| 448 | def is_fittable(self, par_name): |
---|
[fa5fd8d] | 449 | # type: (str) -> bool |
---|
[ce27e21] | 450 | """ |
---|
| 451 | Check if a given parameter is fittable or not |
---|
| 452 | |
---|
| 453 | :param par_name: the parameter name to check |
---|
| 454 | """ |
---|
[e758662] | 455 | return par_name in self.fixed |
---|
[ce27e21] | 456 | #For the future |
---|
| 457 | #return self.params[str(par_name)].is_fittable() |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | def getProfile(self): |
---|
[fa5fd8d] | 461 | # type: () -> (np.ndarray, np.ndarray) |
---|
[ce27e21] | 462 | """ |
---|
| 463 | Get SLD profile |
---|
| 464 | |
---|
| 465 | : return: (z, beta) where z is a list of depth of the transition points |
---|
| 466 | beta is a list of the corresponding SLD values |
---|
| 467 | """ |
---|
[745b7bb] | 468 | args = {} # type: Dict[str, Any] |
---|
[fa5fd8d] | 469 | for p in self._model_info.parameters.kernel_parameters: |
---|
| 470 | if p.id == self.multiplicity_info.control: |
---|
[745b7bb] | 471 | value = float(self.multiplicity) |
---|
[fa5fd8d] | 472 | elif p.length == 1: |
---|
[745b7bb] | 473 | value = self.params.get(p.id, np.NaN) |
---|
[fa5fd8d] | 474 | else: |
---|
[745b7bb] | 475 | value = np.array([self.params.get(p.id+str(k), np.NaN) |
---|
[b32dafd] | 476 | for k in range(1, p.length+1)]) |
---|
[745b7bb] | 477 | args[p.id] = value |
---|
| 478 | |
---|
[e7fe459] | 479 | x, y = self._model_info.profile(**args) |
---|
| 480 | return x, 1e-6*y |
---|
[ce27e21] | 481 | |
---|
| 482 | def setParam(self, name, value): |
---|
[fa5fd8d] | 483 | # type: (str, float) -> None |
---|
[ce27e21] | 484 | """ |
---|
| 485 | Set the value of a model parameter |
---|
| 486 | |
---|
| 487 | :param name: name of the parameter |
---|
| 488 | :param value: value of the parameter |
---|
| 489 | |
---|
| 490 | """ |
---|
| 491 | # Look for dispersion parameters |
---|
| 492 | toks = name.split('.') |
---|
[de0c4ba] | 493 | if len(toks) == 2: |
---|
[ce27e21] | 494 | for item in self.dispersion.keys(): |
---|
[e758662] | 495 | if item == toks[0]: |
---|
[ce27e21] | 496 | for par in self.dispersion[item]: |
---|
[e758662] | 497 | if par == toks[1]: |
---|
[ce27e21] | 498 | self.dispersion[item][par] = value |
---|
| 499 | return |
---|
| 500 | else: |
---|
| 501 | # Look for standard parameter |
---|
| 502 | for item in self.params.keys(): |
---|
[e758662] | 503 | if item == name: |
---|
[ce27e21] | 504 | self.params[item] = value |
---|
| 505 | return |
---|
| 506 | |
---|
[63b32bb] | 507 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 508 | |
---|
| 509 | def getParam(self, name): |
---|
[fa5fd8d] | 510 | # type: (str) -> float |
---|
[ce27e21] | 511 | """ |
---|
| 512 | Set the value of a model parameter |
---|
| 513 | |
---|
| 514 | :param name: name of the parameter |
---|
| 515 | |
---|
| 516 | """ |
---|
| 517 | # Look for dispersion parameters |
---|
| 518 | toks = name.split('.') |
---|
[de0c4ba] | 519 | if len(toks) == 2: |
---|
[ce27e21] | 520 | for item in self.dispersion.keys(): |
---|
[e758662] | 521 | if item == toks[0]: |
---|
[ce27e21] | 522 | for par in self.dispersion[item]: |
---|
[e758662] | 523 | if par == toks[1]: |
---|
[ce27e21] | 524 | return self.dispersion[item][par] |
---|
| 525 | else: |
---|
| 526 | # Look for standard parameter |
---|
| 527 | for item in self.params.keys(): |
---|
[e758662] | 528 | if item == name: |
---|
[ce27e21] | 529 | return self.params[item] |
---|
| 530 | |
---|
[63b32bb] | 531 | raise ValueError("Model does not contain parameter %s" % name) |
---|
[ce27e21] | 532 | |
---|
| 533 | def getParamList(self): |
---|
[04dc697] | 534 | # type: () -> Sequence[str] |
---|
[ce27e21] | 535 | """ |
---|
| 536 | Return a list of all available parameters for the model |
---|
| 537 | """ |
---|
[04dc697] | 538 | param_list = list(self.params.keys()) |
---|
[ce27e21] | 539 | # WARNING: Extending the list with the dispersion parameters |
---|
[de0c4ba] | 540 | param_list.extend(self.getDispParamList()) |
---|
| 541 | return param_list |
---|
[ce27e21] | 542 | |
---|
| 543 | def getDispParamList(self): |
---|
[04dc697] | 544 | # type: () -> Sequence[str] |
---|
[ce27e21] | 545 | """ |
---|
[fb5914f] | 546 | Return a list of polydispersity parameters for the model |
---|
[ce27e21] | 547 | """ |
---|
[1780d59] | 548 | # TODO: fix test so that parameter order doesn't matter |
---|
[3bcb88c] | 549 | ret = ['%s.%s' % (p_name, ext) |
---|
| 550 | for p_name in self.dispersion.keys() |
---|
| 551 | for ext in ('npts', 'nsigmas', 'width')] |
---|
[9404dd3] | 552 | #print(ret) |
---|
[1780d59] | 553 | return ret |
---|
[ce27e21] | 554 | |
---|
| 555 | def clone(self): |
---|
[04dc697] | 556 | # type: () -> "SasviewModel" |
---|
[ce27e21] | 557 | """ Return a identical copy of self """ |
---|
| 558 | return deepcopy(self) |
---|
| 559 | |
---|
| 560 | def run(self, x=0.0): |
---|
[fa5fd8d] | 561 | # type: (Union[float, (float, float), List[float]]) -> float |
---|
[ce27e21] | 562 | """ |
---|
| 563 | Evaluate the model |
---|
| 564 | |
---|
| 565 | :param x: input q, or [q,phi] |
---|
| 566 | |
---|
| 567 | :return: scattering function P(q) |
---|
| 568 | |
---|
| 569 | **DEPRECATED**: use calculate_Iq instead |
---|
| 570 | """ |
---|
[de0c4ba] | 571 | if isinstance(x, (list, tuple)): |
---|
[3c56da87] | 572 | # pylint: disable=unpacking-non-sequence |
---|
[ce27e21] | 573 | q, phi = x |
---|
[84f2962] | 574 | result, _ = self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)]) |
---|
| 575 | return result[0] |
---|
[ce27e21] | 576 | else: |
---|
[84f2962] | 577 | result, _ = self.calculate_Iq([x]) |
---|
| 578 | return result[0] |
---|
[ce27e21] | 579 | |
---|
| 580 | |
---|
| 581 | def runXY(self, x=0.0): |
---|
[fa5fd8d] | 582 | # type: (Union[float, (float, float), List[float]]) -> float |
---|
[ce27e21] | 583 | """ |
---|
| 584 | Evaluate the model in cartesian coordinates |
---|
| 585 | |
---|
| 586 | :param x: input q, or [qx, qy] |
---|
| 587 | |
---|
| 588 | :return: scattering function P(q) |
---|
| 589 | |
---|
| 590 | **DEPRECATED**: use calculate_Iq instead |
---|
| 591 | """ |
---|
[de0c4ba] | 592 | if isinstance(x, (list, tuple)): |
---|
[84f2962] | 593 | result, _ = self.calculate_Iq([x[0]], [x[1]]) |
---|
| 594 | return result[0] |
---|
[ce27e21] | 595 | else: |
---|
[84f2962] | 596 | result, _ = self.calculate_Iq([x]) |
---|
| 597 | return result[0] |
---|
[ce27e21] | 598 | |
---|
| 599 | def evalDistribution(self, qdist): |
---|
[04dc697] | 600 | # type: (Union[np.ndarray, Tuple[np.ndarray, np.ndarray], List[np.ndarray]]) -> np.ndarray |
---|
[d138d43] | 601 | r""" |
---|
[ce27e21] | 602 | Evaluate a distribution of q-values. |
---|
| 603 | |
---|
[d138d43] | 604 | :param qdist: array of q or a list of arrays [qx,qy] |
---|
[ce27e21] | 605 | |
---|
[d138d43] | 606 | * For 1D, a numpy array is expected as input |
---|
[ce27e21] | 607 | |
---|
[d138d43] | 608 | :: |
---|
[ce27e21] | 609 | |
---|
[d138d43] | 610 | evalDistribution(q) |
---|
[ce27e21] | 611 | |
---|
[d138d43] | 612 | where *q* is a numpy array. |
---|
[ce27e21] | 613 | |
---|
[d138d43] | 614 | * For 2D, a list of *[qx,qy]* is expected with 1D arrays as input |
---|
[ce27e21] | 615 | |
---|
[d138d43] | 616 | :: |
---|
[ce27e21] | 617 | |
---|
[d138d43] | 618 | qx = [ qx[0], qx[1], qx[2], ....] |
---|
| 619 | qy = [ qy[0], qy[1], qy[2], ....] |
---|
[ce27e21] | 620 | |
---|
[d138d43] | 621 | If the model is 1D only, then |
---|
[ce27e21] | 622 | |
---|
[d138d43] | 623 | .. math:: |
---|
[ce27e21] | 624 | |
---|
[d138d43] | 625 | q = \sqrt{q_x^2+q_y^2} |
---|
[ce27e21] | 626 | |
---|
| 627 | """ |
---|
[de0c4ba] | 628 | if isinstance(qdist, (list, tuple)): |
---|
[ce27e21] | 629 | # Check whether we have a list of ndarrays [qx,qy] |
---|
| 630 | qx, qy = qdist |
---|
[84f2962] | 631 | result, _ = self.calculate_Iq(qx, qy) |
---|
| 632 | return result |
---|
[ce27e21] | 633 | |
---|
| 634 | elif isinstance(qdist, np.ndarray): |
---|
| 635 | # We have a simple 1D distribution of q-values |
---|
[84f2962] | 636 | result, _ = self.calculate_Iq(qdist) |
---|
| 637 | return result |
---|
[ce27e21] | 638 | |
---|
| 639 | else: |
---|
[3c56da87] | 640 | raise TypeError("evalDistribution expects q or [qx, qy], not %r" |
---|
| 641 | % type(qdist)) |
---|
[ce27e21] | 642 | |
---|
[9dcb21d] | 643 | def calc_composition_models(self, qx): |
---|
[64614ad] | 644 | """ |
---|
[9dcb21d] | 645 | returns parts of the composition model or None if not a composition |
---|
| 646 | model. |
---|
[64614ad] | 647 | """ |
---|
[946c8d27] | 648 | # TODO: have calculate_Iq return the intermediates. |
---|
| 649 | # |
---|
| 650 | # The current interface causes calculate_Iq() to be called twice, |
---|
| 651 | # once to get the combined result and again to get the intermediate |
---|
| 652 | # results. This is necessary for now. |
---|
| 653 | # Long term, the solution is to change the interface to calculate_Iq |
---|
| 654 | # so that it returns a results object containing all the bits: |
---|
[9644b5a] | 655 | # the A, B, C, ... of the composition model (and any subcomponents?) |
---|
[d32de68] | 656 | # the P and S of the product model |
---|
[946c8d27] | 657 | # the combined model before resolution smearing, |
---|
| 658 | # the sasmodel before sesans conversion, |
---|
| 659 | # the oriented 2D model used to fit oriented usans data, |
---|
| 660 | # the final I(q), |
---|
| 661 | # ... |
---|
[9644b5a] | 662 | # |
---|
[946c8d27] | 663 | # Have the model calculator add all of these blindly to the data |
---|
| 664 | # tree, and update the graphs which contain them. The fitter |
---|
| 665 | # needs to be updated to use the I(q) value only, ignoring the rest. |
---|
| 666 | # |
---|
| 667 | # The simple fix of returning the existing intermediate results |
---|
| 668 | # will not work for a couple of reasons: (1) another thread may |
---|
| 669 | # sneak in to compute its own results before calc_composition_models |
---|
| 670 | # is called, and (2) calculate_Iq is currently called three times: |
---|
| 671 | # once with q, once with q values before qmin and once with q values |
---|
| 672 | # after q max. Both of these should be addressed before |
---|
| 673 | # replacing this code. |
---|
[9644b5a] | 674 | composition = self._model_info.composition |
---|
| 675 | if composition and composition[0] == 'product': # only P*S for now |
---|
| 676 | with calculation_lock: |
---|
[84f2962] | 677 | _, lazy_results = self._calculate_Iq(qx) |
---|
| 678 | # for compatibility with sasview 4.x |
---|
| 679 | results = lazy_results() |
---|
| 680 | pq_data = results.get("P(Q)") |
---|
| 681 | sq_data = results.get("S(Q)") |
---|
| 682 | return pq_data, sq_data |
---|
[9644b5a] | 683 | else: |
---|
| 684 | return None |
---|
[bf8c271] | 685 | |
---|
[84f2962] | 686 | def calculate_Iq(self, |
---|
| 687 | qx, # type: Sequence[float] |
---|
| 688 | qy=None # type: Optional[Sequence[float]] |
---|
[b297ba9] | 689 | ): |
---|
[84f2962] | 690 | # type: (...) -> Tuple[np.ndarray, Callable[[], collections.OrderedDict[str, np.ndarray]]] |
---|
[ff7119b] | 691 | """ |
---|
| 692 | Calculate Iq for one set of q with the current parameters. |
---|
| 693 | |
---|
| 694 | If the model is 1D, use *q*. If 2D, use *qx*, *qy*. |
---|
| 695 | |
---|
| 696 | This should NOT be used for fitting since it copies the *q* vectors |
---|
| 697 | to the card for each evaluation. |
---|
[84f2962] | 698 | |
---|
| 699 | The returned tuple contains the scattering intensity followed by a |
---|
| 700 | callable which returns a dictionary of intermediate data from |
---|
| 701 | ProductKernel. |
---|
[ff7119b] | 702 | """ |
---|
[a38b065] | 703 | ## uncomment the following when trying to debug the uncoordinated calls |
---|
| 704 | ## to calculate_Iq |
---|
| 705 | #if calculation_lock.locked(): |
---|
[724257c] | 706 | # logger.info("calculation waiting for another thread to complete") |
---|
| 707 | # logger.info("\n".join(traceback.format_stack())) |
---|
[a38b065] | 708 | |
---|
| 709 | with calculation_lock: |
---|
| 710 | return self._calculate_Iq(qx, qy) |
---|
| 711 | |
---|
| 712 | def _calculate_Iq(self, qx, qy=None): |
---|
[fb5914f] | 713 | if self._model is None: |
---|
[a4f1a73] | 714 | # Only need one copy of the compiled kernel regardless of how many |
---|
| 715 | # times it is used, so store it in the class. Also, to reset the |
---|
| 716 | # compute engine, need to clear out all existing compiled kernels, |
---|
| 717 | # which is much easier to do if we store them in the class. |
---|
| 718 | self.__class__._model = core.build_model(self._model_info) |
---|
[fa5fd8d] | 719 | if qy is not None: |
---|
| 720 | q_vectors = [np.asarray(qx), np.asarray(qy)] |
---|
| 721 | else: |
---|
| 722 | q_vectors = [np.asarray(qx)] |
---|
[a738209] | 723 | calculator = self._model.make_kernel(q_vectors) |
---|
[6a0d6aa] | 724 | parameters = self._model_info.parameters |
---|
| 725 | pairs = [self._get_weights(p) for p in parameters.call_parameters] |
---|
[9c1a59c] | 726 | #weights.plot_weights(self._model_info, pairs) |
---|
[bde38b5] | 727 | call_details, values, is_magnetic = make_kernel_args(calculator, pairs) |
---|
[4edec6f] | 728 | #call_details.show() |
---|
[05df1de] | 729 | #print("================ parameters ==================") |
---|
| 730 | #for p, v in zip(parameters.call_parameters, pairs): print(p.name, v[0]) |
---|
[ce99754] | 731 | #for k, p in enumerate(self._model_info.parameters.call_parameters): |
---|
| 732 | # print(k, p.name, *pairs[k]) |
---|
[4edec6f] | 733 | #print("params", self.params) |
---|
| 734 | #print("values", values) |
---|
| 735 | #print("is_mag", is_magnetic) |
---|
[6a0d6aa] | 736 | result = calculator(call_details, values, cutoff=self.cutoff, |
---|
[9eb3632] | 737 | magnetic=is_magnetic) |
---|
[84f2962] | 738 | lazy_results = getattr(calculator, 'results', |
---|
| 739 | lambda: collections.OrderedDict()) |
---|
[ce99754] | 740 | #print("result", result) |
---|
[84f2962] | 741 | |
---|
[a738209] | 742 | calculator.release() |
---|
[d533590] | 743 | #self._model.release() |
---|
[ce27e21] | 744 | |
---|
[84f2962] | 745 | return result, lazy_results |
---|
[ce27e21] | 746 | |
---|
[39a06c9] | 747 | |
---|
| 748 | def calculate_ER(self, mode=1): |
---|
[fa5fd8d] | 749 | # type: () -> float |
---|
[ce27e21] | 750 | """ |
---|
| 751 | Calculate the effective radius for P(q)*S(q) |
---|
| 752 | |
---|
[3a1afed] | 753 | *mode* is the R_eff type, which defaults to 1 to match the ER |
---|
| 754 | calculation for sasview models from version 3.x. |
---|
| 755 | |
---|
[ce27e21] | 756 | :return: the value of the effective radius |
---|
| 757 | """ |
---|
[3a1afed] | 758 | # ER and VR are only needed for old multiplication models, based on |
---|
| 759 | # sas.sascalc.fit.MultiplicationModel. Fail for now. If we want to |
---|
| 760 | # continue supporting them then add some test cases so that the code |
---|
| 761 | # is exercised. We can access ER/VR using the kernel Fq function by |
---|
| 762 | # extending _calculate_Iq so that it calls: |
---|
| 763 | # if er_mode > 0: |
---|
| 764 | # res = calculator.Fq(call_details, values, cutoff=self.cutoff, |
---|
| 765 | # magnetic=False, effective_radius_type=mode) |
---|
| 766 | # R_eff, form_shell_ratio = res[2], res[4] |
---|
| 767 | # return R_eff, form_shell_ratio |
---|
| 768 | # Then use the following in calculate_ER: |
---|
| 769 | # ER, VR = self._calculate_Iq(q=[0.1], er_mode=mode) |
---|
| 770 | # return ER |
---|
| 771 | # Similarly, for calculate_VR: |
---|
| 772 | # ER, VR = self._calculate_Iq(q=[0.1], er_mode=1) |
---|
| 773 | # return VR |
---|
| 774 | # Obviously a combined calculate_ER_VR method would be better, but |
---|
| 775 | # we only need them to support very old models, so ignore the 2x |
---|
| 776 | # performance hit. |
---|
| 777 | raise NotImplementedError("ER function is no longer available.") |
---|
[ce27e21] | 778 | |
---|
| 779 | def calculate_VR(self): |
---|
[fa5fd8d] | 780 | # type: () -> float |
---|
[ce27e21] | 781 | """ |
---|
| 782 | Calculate the volf ratio for P(q)*S(q) |
---|
| 783 | |
---|
[39a06c9] | 784 | :return: the value of the form:shell volume ratio |
---|
[ce27e21] | 785 | """ |
---|
[3a1afed] | 786 | # See comments in calculate_ER. |
---|
| 787 | raise NotImplementedError("VR function is no longer available.") |
---|
[ce27e21] | 788 | |
---|
| 789 | def set_dispersion(self, parameter, dispersion): |
---|
[7c3fb15] | 790 | # type: (str, weights.Dispersion) -> None |
---|
[ce27e21] | 791 | """ |
---|
| 792 | Set the dispersion object for a model parameter |
---|
| 793 | |
---|
| 794 | :param parameter: name of the parameter [string] |
---|
| 795 | :param dispersion: dispersion object of type Dispersion |
---|
| 796 | """ |
---|
[fa800e72] | 797 | if parameter in self.params: |
---|
[1780d59] | 798 | # TODO: Store the disperser object directly in the model. |
---|
[56b2687] | 799 | # The current method of relying on the sasview GUI to |
---|
[fa800e72] | 800 | # remember them is kind of funky. |
---|
[1780d59] | 801 | # Note: can't seem to get disperser parameters from sasview |
---|
[9c1a59c] | 802 | # (1) Could create a sasview model that has not yet been |
---|
[1780d59] | 803 | # converted, assign the disperser to one of its polydisperse |
---|
| 804 | # parameters, then retrieve the disperser parameters from the |
---|
[9c1a59c] | 805 | # sasview model. |
---|
| 806 | # (2) Could write a disperser parameter retriever in sasview. |
---|
| 807 | # (3) Could modify sasview to use sasmodels.weights dispersers. |
---|
[1780d59] | 808 | # For now, rely on the fact that the sasview only ever uses |
---|
| 809 | # new dispersers in the set_dispersion call and create a new |
---|
| 810 | # one instead of trying to assign parameters. |
---|
[ce27e21] | 811 | self.dispersion[parameter] = dispersion.get_pars() |
---|
| 812 | else: |
---|
[7c3fb15] | 813 | raise ValueError("%r is not a dispersity or orientation parameter" |
---|
| 814 | % parameter) |
---|
[ce27e21] | 815 | |
---|
[aa4946b] | 816 | def _dispersion_mesh(self): |
---|
[fa5fd8d] | 817 | # type: () -> List[Tuple[np.ndarray, np.ndarray]] |
---|
[ce27e21] | 818 | """ |
---|
| 819 | Create a mesh grid of dispersion parameters and weights. |
---|
| 820 | |
---|
| 821 | Returns [p1,p2,...],w where pj is a vector of values for parameter j |
---|
| 822 | and w is a vector containing the products for weights for each |
---|
| 823 | parameter set in the vector. |
---|
| 824 | """ |
---|
[4bfd277] | 825 | pars = [self._get_weights(p) |
---|
| 826 | for p in self._model_info.parameters.call_parameters |
---|
| 827 | if p.type == 'volume'] |
---|
[9eb3632] | 828 | return dispersion_mesh(self._model_info, pars) |
---|
[ce27e21] | 829 | |
---|
| 830 | def _get_weights(self, par): |
---|
[fa5fd8d] | 831 | # type: (Parameter) -> Tuple[np.ndarray, np.ndarray] |
---|
[de0c4ba] | 832 | """ |
---|
[fb5914f] | 833 | Return dispersion weights for parameter |
---|
[de0c4ba] | 834 | """ |
---|
[fa5fd8d] | 835 | if par.name not in self.params: |
---|
[b171acd] | 836 | if par.id == self.multiplicity_info.control: |
---|
[32f87a5] | 837 | return self.multiplicity, [self.multiplicity], [1.0] |
---|
[fa5fd8d] | 838 | else: |
---|
[17db833] | 839 | # For hidden parameters use default values. This sets |
---|
| 840 | # scale=1 and background=0 for structure factors |
---|
| 841 | default = self._model_info.parameters.defaults.get(par.name, np.NaN) |
---|
| 842 | return default, [default], [1.0] |
---|
[fa5fd8d] | 843 | elif par.polydisperse: |
---|
[32f87a5] | 844 | value = self.params[par.name] |
---|
[fb5914f] | 845 | dis = self.dispersion[par.name] |
---|
[9c1a59c] | 846 | if dis['type'] == 'array': |
---|
[32f87a5] | 847 | dispersity, weight = dis['values'], dis['weights'] |
---|
[9c1a59c] | 848 | else: |
---|
[32f87a5] | 849 | dispersity, weight = weights.get_weights( |
---|
[9c1a59c] | 850 | dis['type'], dis['npts'], dis['width'], dis['nsigmas'], |
---|
[32f87a5] | 851 | value, par.limits, par.relative_pd) |
---|
| 852 | return value, dispersity, weight |
---|
[fb5914f] | 853 | else: |
---|
[32f87a5] | 854 | value = self.params[par.name] |
---|
[ce99754] | 855 | return value, [value], [1.0] |
---|
[ce27e21] | 856 | |
---|
[12eec1e] | 857 | @classmethod |
---|
| 858 | def runTests(cls): |
---|
| 859 | """ |
---|
| 860 | Run any tests built into the model and captures the test output. |
---|
| 861 | |
---|
| 862 | Returns success flag and output |
---|
| 863 | """ |
---|
| 864 | from .model_test import check_model |
---|
| 865 | return check_model(cls._model_info) |
---|
| 866 | |
---|
[749a7d4] | 867 | def test_cylinder(): |
---|
[fa5fd8d] | 868 | # type: () -> float |
---|
[4d76711] | 869 | """ |
---|
[749a7d4] | 870 | Test that the cylinder model runs, returning the value at [0.1,0.1]. |
---|
[4d76711] | 871 | """ |
---|
| 872 | Cylinder = _make_standard_model('cylinder') |
---|
[fb5914f] | 873 | cylinder = Cylinder() |
---|
[b32dafd] | 874 | return cylinder.evalDistribution([0.1, 0.1]) |
---|
[de97440] | 875 | |
---|
[8f93522] | 876 | def test_structure_factor(): |
---|
| 877 | # type: () -> float |
---|
| 878 | """ |
---|
[749a7d4] | 879 | Test that 2-D hardsphere model runs and doesn't produce NaN. |
---|
[8f93522] | 880 | """ |
---|
| 881 | Model = _make_standard_model('hardsphere') |
---|
| 882 | model = Model() |
---|
[17db833] | 883 | value2d = model.evalDistribution([0.1, 0.1]) |
---|
| 884 | value1d = model.evalDistribution(np.array([0.1*np.sqrt(2)])) |
---|
| 885 | #print("hardsphere", value1d, value2d) |
---|
| 886 | if np.isnan(value1d) or np.isnan(value2d): |
---|
| 887 | raise ValueError("hardsphere returns nan") |
---|
[8f93522] | 888 | |
---|
[ce99754] | 889 | def test_product(): |
---|
| 890 | # type: () -> float |
---|
| 891 | """ |
---|
| 892 | Test that 2-D hardsphere model runs and doesn't produce NaN. |
---|
| 893 | """ |
---|
| 894 | S = _make_standard_model('hayter_msa')() |
---|
| 895 | P = _make_standard_model('cylinder')() |
---|
| 896 | model = MultiplicationModel(P, S) |
---|
[5024a56] | 897 | model.setParam(product.RADIUS_MODE_ID, 1.0) |
---|
[ce99754] | 898 | value = model.evalDistribution([0.1, 0.1]) |
---|
| 899 | if np.isnan(value): |
---|
| 900 | raise ValueError("cylinder*hatyer_msa returns null") |
---|
| 901 | |
---|
[04045f4] | 902 | def test_rpa(): |
---|
| 903 | # type: () -> float |
---|
| 904 | """ |
---|
[749a7d4] | 905 | Test that the 2-D RPA model runs |
---|
[04045f4] | 906 | """ |
---|
| 907 | RPA = _make_standard_model('rpa') |
---|
| 908 | rpa = RPA(3) |
---|
[b32dafd] | 909 | return rpa.evalDistribution([0.1, 0.1]) |
---|
[04045f4] | 910 | |
---|
[749a7d4] | 911 | def test_empty_distribution(): |
---|
| 912 | # type: () -> None |
---|
| 913 | """ |
---|
| 914 | Make sure that sasmodels returns NaN when there are no polydispersity points |
---|
| 915 | """ |
---|
| 916 | Cylinder = _make_standard_model('cylinder') |
---|
| 917 | cylinder = Cylinder() |
---|
| 918 | cylinder.setParam('radius', -1.0) |
---|
| 919 | cylinder.setParam('background', 0.) |
---|
| 920 | Iq = cylinder.evalDistribution(np.asarray([0.1])) |
---|
[2d81cfe] | 921 | assert Iq[0] == 0., "empty distribution fails" |
---|
[4d76711] | 922 | |
---|
| 923 | def test_model_list(): |
---|
[fa5fd8d] | 924 | # type: () -> None |
---|
[4d76711] | 925 | """ |
---|
[749a7d4] | 926 | Make sure that all models build as sasview models |
---|
[4d76711] | 927 | """ |
---|
| 928 | from .exception import annotate_exception |
---|
| 929 | for name in core.list_models(): |
---|
| 930 | try: |
---|
| 931 | _make_standard_model(name) |
---|
| 932 | except: |
---|
| 933 | annotate_exception("when loading "+name) |
---|
| 934 | raise |
---|
| 935 | |
---|
[c95dfc63] | 936 | def test_old_name(): |
---|
| 937 | # type: () -> None |
---|
| 938 | """ |
---|
[b297ba9] | 939 | Load and run cylinder model as sas.models.CylinderModel |
---|
[c95dfc63] | 940 | """ |
---|
| 941 | if not SUPPORT_OLD_STYLE_PLUGINS: |
---|
| 942 | return |
---|
| 943 | try: |
---|
| 944 | # if sasview is not on the path then don't try to test it |
---|
| 945 | import sas |
---|
| 946 | except ImportError: |
---|
| 947 | return |
---|
| 948 | load_standard_models() |
---|
| 949 | from sas.models.CylinderModel import CylinderModel |
---|
| 950 | CylinderModel().evalDistribution([0.1, 0.1]) |
---|
| 951 | |
---|
[293fee5] | 952 | def test_structure_factor_background(): |
---|
| 953 | # type: () -> None |
---|
| 954 | """ |
---|
| 955 | Check that sasview model and direct model match, with background=0. |
---|
| 956 | """ |
---|
| 957 | from .data import empty_data1D |
---|
| 958 | from .core import load_model_info, build_model |
---|
| 959 | from .direct_model import DirectModel |
---|
| 960 | |
---|
| 961 | model_name = "hardsphere" |
---|
| 962 | q = [0.0] |
---|
| 963 | |
---|
| 964 | sasview_model = _make_standard_model(model_name)() |
---|
| 965 | sasview_value = sasview_model.evalDistribution(np.array(q))[0] |
---|
| 966 | |
---|
| 967 | data = empty_data1D(q) |
---|
| 968 | model_info = load_model_info(model_name) |
---|
| 969 | model = build_model(model_info) |
---|
| 970 | direct_model = DirectModel(data, model) |
---|
| 971 | direct_value_zero_background = direct_model(background=0.0) |
---|
| 972 | |
---|
| 973 | assert sasview_value == direct_value_zero_background |
---|
| 974 | |
---|
| 975 | # Additionally check that direct value background defaults to zero |
---|
| 976 | direct_value_default = direct_model() |
---|
| 977 | assert sasview_value == direct_value_default |
---|
| 978 | |
---|
| 979 | |
---|
[05df1de] | 980 | def magnetic_demo(): |
---|
[b297ba9] | 981 | """ |
---|
| 982 | Demostrate call to magnetic model. |
---|
| 983 | """ |
---|
[05df1de] | 984 | Model = _make_standard_model('sphere') |
---|
| 985 | model = Model() |
---|
[610ef23] | 986 | model.setParam('sld_M0', 8) |
---|
[05df1de] | 987 | q = np.linspace(-0.35, 0.35, 500) |
---|
| 988 | qx, qy = np.meshgrid(q, q) |
---|
[84f2962] | 989 | result, _ = model.calculate_Iq(qx.flatten(), qy.flatten()) |
---|
[05df1de] | 990 | result = result.reshape(qx.shape) |
---|
| 991 | |
---|
| 992 | import pylab |
---|
| 993 | pylab.imshow(np.log(result + 0.001)) |
---|
| 994 | pylab.show() |
---|
| 995 | |
---|
[fb5914f] | 996 | if __name__ == "__main__": |
---|
[749a7d4] | 997 | print("cylinder(0.1,0.1)=%g"%test_cylinder()) |
---|
[05df1de] | 998 | #magnetic_demo() |
---|
[ce99754] | 999 | #test_product() |
---|
[17db833] | 1000 | #test_structure_factor() |
---|
| 1001 | #print("rpa:", test_rpa()) |
---|
[749a7d4] | 1002 | #test_empty_distribution() |
---|
[293fee5] | 1003 | #test_structure_factor_background() |
---|