[0d99a6a] | 1 | """ |
---|
| 2 | Model Info and Parameter Tables |
---|
| 3 | =============================== |
---|
| 4 | |
---|
| 5 | Defines :class:`ModelInfo` and :class:`ParameterTable` and the routines for |
---|
| 6 | manipulating them. In particular, :func:`make_model_info` converts a kernel |
---|
| 7 | module into the model info block as seen by the rest of the sasmodels library. |
---|
| 8 | """ |
---|
[04045f4] | 9 | from __future__ import print_function |
---|
| 10 | |
---|
[6d6508e] | 11 | from copy import copy |
---|
| 12 | from os.path import abspath, basename, splitext |
---|
[da63656] | 13 | import inspect |
---|
[db03406] | 14 | import logging |
---|
[69aa451] | 15 | |
---|
[7ae2b7f] | 16 | import numpy as np # type: ignore |
---|
[69aa451] | 17 | |
---|
[db03406] | 18 | from . import autoc |
---|
| 19 | |
---|
[a86f6c0] | 20 | # Optional typing |
---|
[2d81cfe] | 21 | # pylint: disable=unused-import |
---|
[a86f6c0] | 22 | try: |
---|
[04045f4] | 23 | from typing import Tuple, List, Union, Dict, Optional, Any, Callable, Sequence, Set |
---|
[e65c3ba] | 24 | from types import ModuleType |
---|
[a86f6c0] | 25 | except ImportError: |
---|
| 26 | pass |
---|
| 27 | else: |
---|
| 28 | Limits = Tuple[float, float] |
---|
[04dc697] | 29 | #LimitsOrChoice = Union[Limits, Tuple[Sequence[str]]] |
---|
[9a943d0] | 30 | ParameterDef = Tuple[str, str, float, Limits, str, str] |
---|
[a86f6c0] | 31 | ParameterSetUser = Dict[str, Union[float, List[float]]] |
---|
| 32 | ParameterSet = Dict[str, float] |
---|
| 33 | TestInput = Union[str, float, List[float], Tuple[float, float], List[Tuple[float, float]]] |
---|
| 34 | TestValue = Union[float, List[float]] |
---|
| 35 | TestCondition = Tuple[ParameterSetUser, TestInput, TestValue] |
---|
[2d81cfe] | 36 | # pylint: enable=unused-import |
---|
[a86f6c0] | 37 | |
---|
[db03406] | 38 | logger = logging.getLogger(__name__) |
---|
| 39 | |
---|
[6aee3ab] | 40 | # If MAX_PD changes, need to change the loop macros in kernel_iq.c |
---|
| 41 | MAX_PD = 5 #: Maximum number of simultaneously polydisperse parameters |
---|
[d19962c] | 42 | |
---|
[69aa451] | 43 | # assumptions about common parameters exist throughout the code, such as: |
---|
| 44 | # (1) kernel functions Iq, Iqxy, form_volume, ... don't see them |
---|
| 45 | # (2) kernel drivers assume scale is par[0] and background is par[1] |
---|
| 46 | # (3) mixture models drop the background on components and replace the scale |
---|
| 47 | # with a scale that varies from [-inf, inf] |
---|
| 48 | # (4) product models drop the background and reassign scale |
---|
| 49 | # and maybe other places. |
---|
| 50 | # Note that scale and background cannot be coordinated parameters whose value |
---|
| 51 | # depends on the some polydisperse parameter with the current implementation |
---|
[7edce22] | 52 | COMMON_PARAMETERS = [ |
---|
[9a943d0] | 53 | ("scale", "", 1, (0.0, np.inf), "", "Source intensity"), |
---|
[ca55aa0] | 54 | ("background", "1/cm", 1e-3, (-np.inf, np.inf), "", "Source background"), |
---|
[7edce22] | 55 | ] |
---|
| 56 | assert (len(COMMON_PARAMETERS) == 2 |
---|
[40a87fa] | 57 | and COMMON_PARAMETERS[0][0] == "scale" |
---|
| 58 | and COMMON_PARAMETERS[1][0] == "background"), "don't change common parameters" |
---|
[69aa451] | 59 | |
---|
[a86f6c0] | 60 | |
---|
[69aa451] | 61 | def make_parameter_table(pars): |
---|
[f6029fd] | 62 | # type: (List[ParameterDef]) -> ParameterTable |
---|
[7edce22] | 63 | """ |
---|
| 64 | Construct a parameter table from a list of parameter definitions. |
---|
| 65 | |
---|
| 66 | This is used by the module processor to convert the parameter block into |
---|
| 67 | the parameter table seen in the :class:`ModelInfo` for the module. |
---|
| 68 | """ |
---|
[69aa451] | 69 | processed = [] |
---|
| 70 | for p in pars: |
---|
[a86f6c0] | 71 | if not isinstance(p, (list, tuple)) or len(p) != 6: |
---|
[69aa451] | 72 | raise ValueError("Parameter should be [name, units, default, limits, type, desc], but got %r" |
---|
| 73 | %str(p)) |
---|
| 74 | processed.append(parse_parameter(*p)) |
---|
| 75 | partable = ParameterTable(processed) |
---|
| 76 | return partable |
---|
| 77 | |
---|
[a86f6c0] | 78 | def parse_parameter(name, units='', default=np.NaN, |
---|
[04045f4] | 79 | user_limits=None, ptype='', description=''): |
---|
[60f03de] | 80 | # type: (str, str, float, Sequence[Any], str, str) -> Parameter |
---|
[7edce22] | 81 | """ |
---|
| 82 | Parse an individual parameter from the parameter definition block. |
---|
| 83 | |
---|
| 84 | This does type and value checking on the definition, leading |
---|
| 85 | to early failure in the model loading process and easier debugging. |
---|
| 86 | """ |
---|
[69aa451] | 87 | # Parameter is a user facing class. Do robust type checking. |
---|
| 88 | if not isstr(name): |
---|
| 89 | raise ValueError("expected string for parameter name %r"%name) |
---|
| 90 | if not isstr(units): |
---|
| 91 | raise ValueError("expected units to be a string for %s"%name) |
---|
[04045f4] | 92 | |
---|
| 93 | # Process limits as [float, float] or [[str, str, ...]] |
---|
[c1a888b] | 94 | choices = [] # type: List[str] |
---|
[04045f4] | 95 | if user_limits is None: |
---|
| 96 | limits = (-np.inf, np.inf) |
---|
| 97 | elif not isinstance(user_limits, (tuple, list)): |
---|
| 98 | raise ValueError("invalid limits for %s"%name) |
---|
| 99 | else: |
---|
| 100 | # if limits is [[str,...]], then this is a choice list field, |
---|
| 101 | # and limits are 1 to length of string list |
---|
| 102 | if isinstance(user_limits[0], (tuple, list)): |
---|
| 103 | choices = user_limits[0] |
---|
| 104 | limits = (0., len(choices)-1.) |
---|
| 105 | if not all(isstr(k) for k in choices): |
---|
| 106 | raise ValueError("choices must be strings for %s"%name) |
---|
| 107 | else: |
---|
| 108 | try: |
---|
| 109 | low, high = user_limits |
---|
| 110 | limits = (float(low), float(high)) |
---|
| 111 | except Exception: |
---|
[bb4b509] | 112 | raise ValueError("invalid limits for %s: %r"%(name, user_limits)) |
---|
[50ec515] | 113 | if low >= high: |
---|
| 114 | raise ValueError("require lower limit < upper limit") |
---|
[69aa451] | 115 | |
---|
[04045f4] | 116 | # Process default value as float, making sure it is in range |
---|
[69aa451] | 117 | if not isinstance(default, (int, float)): |
---|
| 118 | raise ValueError("expected default %r to be a number for %s" |
---|
| 119 | % (default, name)) |
---|
[04045f4] | 120 | if default < limits[0] or default > limits[1]: |
---|
[69aa451] | 121 | raise ValueError("default value %r not in range for %s" |
---|
| 122 | % (default, name)) |
---|
| 123 | |
---|
[04045f4] | 124 | # Check for valid parameter type |
---|
[a86f6c0] | 125 | if ptype not in ("volume", "orientation", "sld", "magnetic", ""): |
---|
| 126 | raise ValueError("unexpected type %r for %s" % (ptype, name)) |
---|
[69aa451] | 127 | |
---|
[04045f4] | 128 | # Check for valid parameter description |
---|
[69aa451] | 129 | if not isstr(description): |
---|
| 130 | raise ValueError("expected description to be a string") |
---|
| 131 | |
---|
| 132 | # Parameter id for name[n] does not include [n] |
---|
| 133 | if "[" in name: |
---|
| 134 | if not name.endswith(']'): |
---|
| 135 | raise ValueError("Expected name[len] for vector parameter %s"%name) |
---|
| 136 | pid, ref = name[:-1].split('[', 1) |
---|
| 137 | ref = ref.strip() |
---|
| 138 | else: |
---|
| 139 | pid, ref = name, None |
---|
| 140 | |
---|
| 141 | # automatically identify sld types |
---|
[40a87fa] | 142 | if ptype == '' and (pid.startswith('sld') or pid.endswith('sld')): |
---|
[a86f6c0] | 143 | ptype = 'sld' |
---|
[69aa451] | 144 | |
---|
| 145 | # Check if using a vector definition, name[k], as the parameter name |
---|
| 146 | if ref: |
---|
| 147 | if ref == '': |
---|
| 148 | raise ValueError("Need to specify vector length for %s"%name) |
---|
| 149 | try: |
---|
| 150 | length = int(ref) |
---|
| 151 | control = None |
---|
[a86f6c0] | 152 | except ValueError: |
---|
[69aa451] | 153 | length = None |
---|
| 154 | control = ref |
---|
| 155 | else: |
---|
| 156 | length = 1 |
---|
| 157 | control = None |
---|
| 158 | |
---|
| 159 | # Build the parameter |
---|
| 160 | parameter = Parameter(name=name, units=units, default=default, |
---|
[a86f6c0] | 161 | limits=limits, ptype=ptype, description=description) |
---|
[69aa451] | 162 | |
---|
| 163 | # TODO: need better control over whether a parameter is polydisperse |
---|
[a86f6c0] | 164 | parameter.polydisperse = ptype in ('orientation', 'volume') |
---|
| 165 | parameter.relative_pd = ptype == 'volume' |
---|
[69aa451] | 166 | parameter.choices = choices |
---|
| 167 | parameter.length = length |
---|
| 168 | parameter.length_control = control |
---|
| 169 | |
---|
| 170 | return parameter |
---|
| 171 | |
---|
[ce896fd] | 172 | |
---|
[4bfd277] | 173 | def expand_pars(partable, pars): |
---|
[a86f6c0] | 174 | # type: (ParameterTable, ParameterSetUser) -> ParameterSet |
---|
[ce896fd] | 175 | """ |
---|
[6d6508e] | 176 | Create demo parameter set from key-value pairs. |
---|
[ce896fd] | 177 | |
---|
[4bfd277] | 178 | *pars* are the key-value pairs to use for the parameters. Any |
---|
| 179 | parameters not specified in *pars* are set from the *partable* defaults. |
---|
[ce896fd] | 180 | |
---|
[4bfd277] | 181 | If *pars* references vector fields, such as thickness[n], then support |
---|
[ce896fd] | 182 | different ways of assigning the demo values, including assigning a |
---|
| 183 | specific value (e.g., thickness3=50.0), assigning a new value to all |
---|
| 184 | (e.g., thickness=50.0) or assigning values using list notation. |
---|
| 185 | """ |
---|
[4bfd277] | 186 | if pars is None: |
---|
[ce896fd] | 187 | result = partable.defaults |
---|
| 188 | else: |
---|
[4bfd277] | 189 | lookup = dict((p.id, p) for p in partable.kernel_parameters) |
---|
[ce896fd] | 190 | result = partable.defaults.copy() |
---|
[9a943d0] | 191 | scalars = dict((name, value) for name, value in pars.items() |
---|
| 192 | if name not in lookup or lookup[name].length == 1) |
---|
[40a87fa] | 193 | vectors = dict((name, value) for name, value in pars.items() |
---|
[4bfd277] | 194 | if name in lookup and lookup[name].length > 1) |
---|
[256dfe1] | 195 | #print("lookup", lookup) |
---|
| 196 | #print("scalars", scalars) |
---|
| 197 | #print("vectors", vectors) |
---|
[ce896fd] | 198 | if vectors: |
---|
| 199 | for name, value in vectors.items(): |
---|
| 200 | if np.isscalar(value): |
---|
| 201 | # support for the form |
---|
[4bfd277] | 202 | # dict(thickness=0, thickness2=50) |
---|
| 203 | for k in range(1, lookup[name].length+1): |
---|
[ce896fd] | 204 | key = name+str(k) |
---|
| 205 | if key not in scalars: |
---|
[256dfe1] | 206 | scalars[key] = value |
---|
[ce896fd] | 207 | else: |
---|
| 208 | # supoprt for the form |
---|
[4bfd277] | 209 | # dict(thickness=[20,10,3]) |
---|
[40a87fa] | 210 | for (k, v) in enumerate(value): |
---|
[256dfe1] | 211 | scalars[name+str(k+1)] = v |
---|
[9a943d0] | 212 | result.update(scalars) |
---|
[256dfe1] | 213 | #print("expanded", result) |
---|
[ce896fd] | 214 | |
---|
[6d6508e] | 215 | return result |
---|
| 216 | |
---|
| 217 | def prefix_parameter(par, prefix): |
---|
[a86f6c0] | 218 | # type: (Parameter, str) -> Parameter |
---|
[6d6508e] | 219 | """ |
---|
| 220 | Return a copy of the parameter with its name prefixed. |
---|
| 221 | """ |
---|
| 222 | new_par = copy(par) |
---|
| 223 | new_par.name = prefix + par.name |
---|
| 224 | new_par.id = prefix + par.id |
---|
| 225 | |
---|
| 226 | def suffix_parameter(par, suffix): |
---|
[a86f6c0] | 227 | # type: (Parameter, str) -> Parameter |
---|
[6d6508e] | 228 | """ |
---|
| 229 | Return a copy of the parameter with its name prefixed. |
---|
| 230 | """ |
---|
| 231 | new_par = copy(par) |
---|
| 232 | # If name has the form x[n], replace with x_suffix[n] |
---|
| 233 | new_par.name = par.id + suffix + par.name[len(par.id):] |
---|
| 234 | new_par.id = par.id + suffix |
---|
[ce896fd] | 235 | |
---|
[69aa451] | 236 | class Parameter(object): |
---|
| 237 | """ |
---|
| 238 | The available kernel parameters are defined as a list, with each parameter |
---|
| 239 | defined as a sublist with the following elements: |
---|
| 240 | |
---|
[f88e248] | 241 | *name* is the name that will be displayed to the user. Names |
---|
[69aa451] | 242 | should be lower case, with words separated by underscore. If |
---|
[f88e248] | 243 | acronyms are used, the whole acronym should be upper case. For vector |
---|
| 244 | parameters, the name will be followed by *[len]* where *len* is an |
---|
| 245 | integer length of the vector, or the name of the parameter which |
---|
| 246 | controls the length. The attribute *id* will be created from name |
---|
| 247 | without the length. |
---|
[69aa451] | 248 | |
---|
| 249 | *units* should be one of *degrees* for angles, *Ang* for lengths, |
---|
| 250 | *1e-6/Ang^2* for SLDs. |
---|
| 251 | |
---|
| 252 | *default value* will be the initial value for the model when it |
---|
| 253 | is selected, or when an initial value is not otherwise specified. |
---|
| 254 | |
---|
| 255 | *limits = [lb, ub]* are the hard limits on the parameter value, used to |
---|
| 256 | limit the polydispersity density function. In the fit, the parameter limits |
---|
| 257 | given to the fit are the limits on the central value of the parameter. |
---|
| 258 | If there is polydispersity, it will evaluate parameter values outside |
---|
| 259 | the fit limits, but not outside the hard limits specified in the model. |
---|
| 260 | If there are no limits, use +/-inf imported from numpy. |
---|
| 261 | |
---|
| 262 | *type* indicates how the parameter will be used. "volume" parameters |
---|
| 263 | will be used in all functions. "orientation" parameters will be used |
---|
| 264 | in *Iqxy* and *Imagnetic*. "magnetic* parameters will be used in |
---|
| 265 | *Imagnetic* only. If *type* is the empty string, the parameter will |
---|
| 266 | be used in all of *Iq*, *Iqxy* and *Imagnetic*. "sld" parameters |
---|
| 267 | can automatically be promoted to magnetic parameters, each of which |
---|
| 268 | will have a magnitude and a direction, which may be different from |
---|
[6e7ff6d] | 269 | other sld parameters. The volume parameters are used for calls |
---|
| 270 | to form_volume within the kernel (required for volume normalization) |
---|
| 271 | and for calls to ER and VR for effective radius and volume ratio |
---|
| 272 | respectively. |
---|
[69aa451] | 273 | |
---|
| 274 | *description* is a short description of the parameter. This will |
---|
| 275 | be displayed in the parameter table and used as a tool tip for the |
---|
| 276 | parameter value in the user interface. |
---|
| 277 | |
---|
| 278 | Additional values can be set after the parameter is created: |
---|
| 279 | |
---|
[6e7ff6d] | 280 | * *length* is the length of the field if it is a vector field |
---|
| 281 | |
---|
| 282 | * *length_control* is the parameter which sets the vector length |
---|
| 283 | |
---|
| 284 | * *is_control* is True if the parameter is a control parameter for a vector |
---|
| 285 | |
---|
| 286 | * *polydisperse* is true if the parameter accepts a polydispersity |
---|
| 287 | |
---|
| 288 | * *relative_pd* is true if that polydispersity is a portion of the |
---|
[40a87fa] | 289 | value (so a 10% length dipsersity would use a polydispersity value |
---|
| 290 | of 0.1) rather than absolute dispersisity (such as an angle plus or |
---|
| 291 | minus 15 degrees). |
---|
[69aa451] | 292 | |
---|
[50ec515] | 293 | *choices* is the option names for a drop down list of options, as for |
---|
| 294 | example, might be used to set the value of a shape parameter. |
---|
| 295 | |
---|
[a5b8477] | 296 | These values are set by :func:`make_parameter_table` and |
---|
| 297 | :func:`parse_parameter` therein. |
---|
[69aa451] | 298 | """ |
---|
| 299 | def __init__(self, name, units='', default=None, limits=(-np.inf, np.inf), |
---|
[a86f6c0] | 300 | ptype='', description=''): |
---|
[c1a888b] | 301 | # type: (str, str, float, Limits, str, str) -> None |
---|
[a86f6c0] | 302 | self.id = name.split('[')[0].strip() # type: str |
---|
| 303 | self.name = name # type: str |
---|
| 304 | self.units = units # type: str |
---|
| 305 | self.default = default # type: float |
---|
| 306 | self.limits = limits # type: Limits |
---|
| 307 | self.type = ptype # type: str |
---|
| 308 | self.description = description # type: str |
---|
[69aa451] | 309 | |
---|
[d19962c] | 310 | # Length and length_control will be filled in once the complete |
---|
[69aa451] | 311 | # parameter table is available. |
---|
[a86f6c0] | 312 | self.length = 1 # type: int |
---|
| 313 | self.length_control = None # type: Optional[str] |
---|
| 314 | self.is_control = False # type: bool |
---|
[69aa451] | 315 | |
---|
| 316 | # TODO: need better control over whether a parameter is polydisperse |
---|
[a86f6c0] | 317 | self.polydisperse = False # type: bool |
---|
| 318 | self.relative_pd = False # type: bool |
---|
[69aa451] | 319 | |
---|
[ce896fd] | 320 | # choices are also set externally. |
---|
[a86f6c0] | 321 | self.choices = [] # type: List[str] |
---|
[ce896fd] | 322 | |
---|
[69aa451] | 323 | def as_definition(self): |
---|
[a86f6c0] | 324 | # type: () -> str |
---|
[69aa451] | 325 | """ |
---|
| 326 | Declare space for the variable in a parameter structure. |
---|
| 327 | |
---|
| 328 | For example, the parameter thickness with length 3 will |
---|
| 329 | return "double thickness[3];", with no spaces before and |
---|
| 330 | no new line character afterward. |
---|
| 331 | """ |
---|
| 332 | if self.length == 1: |
---|
| 333 | return "double %s;"%self.id |
---|
| 334 | else: |
---|
| 335 | return "double %s[%d];"%(self.id, self.length) |
---|
| 336 | |
---|
| 337 | def as_function_argument(self): |
---|
[a86f6c0] | 338 | # type: () -> str |
---|
[40a87fa] | 339 | r""" |
---|
[69aa451] | 340 | Declare the variable as a function argument. |
---|
| 341 | |
---|
| 342 | For example, the parameter thickness with length 3 will |
---|
[40a87fa] | 343 | return "double \*thickness", with no spaces before and |
---|
[69aa451] | 344 | no comma afterward. |
---|
| 345 | """ |
---|
| 346 | if self.length == 1: |
---|
| 347 | return "double %s"%self.id |
---|
| 348 | else: |
---|
| 349 | return "double *%s"%self.id |
---|
| 350 | |
---|
| 351 | def as_call_reference(self, prefix=""): |
---|
[c1a888b] | 352 | # type: (str) -> str |
---|
[bb4b509] | 353 | """ |
---|
| 354 | Return *prefix* + parameter name. For struct references, use "v." |
---|
| 355 | as the prefix. |
---|
| 356 | """ |
---|
[69aa451] | 357 | # Note: if the parameter is a struct type, then we will need to use |
---|
| 358 | # &prefix+id. For scalars and vectors we can just use prefix+id. |
---|
| 359 | return prefix + self.id |
---|
| 360 | |
---|
| 361 | def __str__(self): |
---|
[a86f6c0] | 362 | # type: () -> str |
---|
[69aa451] | 363 | return "<%s>"%self.name |
---|
| 364 | |
---|
| 365 | def __repr__(self): |
---|
[a86f6c0] | 366 | # type: () -> str |
---|
[69aa451] | 367 | return "P<%s>"%self.name |
---|
| 368 | |
---|
[d19962c] | 369 | |
---|
[69aa451] | 370 | class ParameterTable(object): |
---|
[d19962c] | 371 | """ |
---|
| 372 | ParameterTable manages the list of available parameters. |
---|
| 373 | |
---|
| 374 | There are a couple of complications which mean that the list of parameters |
---|
| 375 | for the kernel differs from the list of parameters that the user sees. |
---|
| 376 | |
---|
| 377 | (1) Common parameters. Scale and background are implicit to every model, |
---|
| 378 | but are not passed to the kernel. |
---|
| 379 | |
---|
| 380 | (2) Vector parameters. Vector parameters are passed to the kernel as a |
---|
| 381 | pointer to an array, e.g., thick[], but they are seen by the user as n |
---|
| 382 | separate parameters thick1, thick2, ... |
---|
| 383 | |
---|
| 384 | Therefore, the parameter table is organized by how it is expected to be |
---|
| 385 | used. The following information is needed to set up the kernel functions: |
---|
| 386 | |
---|
| 387 | * *kernel_parameters* is the list of parameters in the kernel parameter |
---|
[40a87fa] | 388 | table, with vector parameter p declared as p[]. |
---|
[d19962c] | 389 | |
---|
| 390 | * *iq_parameters* is the list of parameters to the Iq(q, ...) function, |
---|
[40a87fa] | 391 | with vector parameter p sent as p[]. |
---|
[d19962c] | 392 | |
---|
[8698a0d] | 393 | * [removed] *iqxy_parameters* is the list of parameters to the Iqxy(qx, qy, ...) |
---|
[40a87fa] | 394 | function, with vector parameter p sent as p[]. |
---|
[d19962c] | 395 | |
---|
| 396 | * *form_volume_parameters* is the list of parameters to the form_volume(...) |
---|
[40a87fa] | 397 | function, with vector parameter p sent as p[]. |
---|
[d19962c] | 398 | |
---|
| 399 | Problem details, which sets up the polydispersity loops, requires the |
---|
| 400 | following: |
---|
| 401 | |
---|
| 402 | * *theta_offset* is the offset of the theta parameter in the kernel parameter |
---|
[40a87fa] | 403 | table, with vector parameters counted as n individual parameters |
---|
| 404 | p1, p2, ..., or offset is -1 if there is no theta parameter. |
---|
[d19962c] | 405 | |
---|
| 406 | * *max_pd* is the maximum number of polydisperse parameters, with vector |
---|
[40a87fa] | 407 | parameters counted as n individual parameters p1, p2, ... Note that |
---|
| 408 | this number is limited to sasmodels.modelinfo.MAX_PD. |
---|
[d19962c] | 409 | |
---|
| 410 | * *npars* is the total number of parameters to the kernel, with vector |
---|
[40a87fa] | 411 | parameters counted as n individual parameters p1, p2, ... |
---|
[d19962c] | 412 | |
---|
| 413 | * *call_parameters* is the complete list of parameters to the kernel, |
---|
[40a87fa] | 414 | including scale and background, with vector parameters recorded as |
---|
| 415 | individual parameters p1, p2, ... |
---|
[d19962c] | 416 | |
---|
| 417 | * *active_1d* is the set of names that may be polydisperse for 1d data |
---|
| 418 | |
---|
| 419 | * *active_2d* is the set of names that may be polydisperse for 2d data |
---|
| 420 | |
---|
| 421 | User parameters are the set of parameters visible to the user, including |
---|
| 422 | the scale and background parameters that the kernel does not see. User |
---|
| 423 | parameters don't use vector notation, and instead use p1, p2, ... |
---|
| 424 | """ |
---|
[69aa451] | 425 | # scale and background are implicit parameters |
---|
| 426 | COMMON = [Parameter(*p) for p in COMMON_PARAMETERS] |
---|
| 427 | |
---|
| 428 | def __init__(self, parameters): |
---|
[a86f6c0] | 429 | # type: (List[Parameter]) -> None |
---|
[d19962c] | 430 | self.kernel_parameters = parameters |
---|
[8698a0d] | 431 | self._check_angles() |
---|
[60eab2a] | 432 | self._set_vector_lengths() |
---|
[32e3c9b] | 433 | |
---|
| 434 | self.npars = sum(p.length for p in self.kernel_parameters) |
---|
[9eb3632] | 435 | self.nmagnetic = sum(p.length for p in self.kernel_parameters |
---|
[bb4b509] | 436 | if p.type == 'sld') |
---|
[9eb3632] | 437 | self.nvalues = 2 + self.npars |
---|
| 438 | if self.nmagnetic: |
---|
| 439 | self.nvalues += 3 + 3*self.nmagnetic |
---|
[32e3c9b] | 440 | |
---|
[a86f6c0] | 441 | self.call_parameters = self._get_call_parameters() |
---|
| 442 | self.defaults = self._get_defaults() |
---|
[d19962c] | 443 | #self._name_table= dict((p.id, p) for p in parameters) |
---|
[60eab2a] | 444 | |
---|
[d19962c] | 445 | # Set the kernel parameters. Assumes background and scale are the |
---|
| 446 | # first two parameters in the parameter list, but these are not sent |
---|
| 447 | # to the underlying kernel functions. |
---|
| 448 | self.iq_parameters = [p for p in self.kernel_parameters |
---|
| 449 | if p.type not in ('orientation', 'magnetic')] |
---|
[8698a0d] | 450 | # note: orientation no longer sent to Iqxy, so its the same as |
---|
| 451 | #self.iqxy_parameters = [p for p in self.kernel_parameters |
---|
| 452 | # if p.type != 'magnetic'] |
---|
[d19962c] | 453 | self.form_volume_parameters = [p for p in self.kernel_parameters |
---|
| 454 | if p.type == 'volume'] |
---|
| 455 | |
---|
| 456 | # Theta offset |
---|
| 457 | offset = 0 |
---|
| 458 | for p in self.kernel_parameters: |
---|
| 459 | if p.name == 'theta': |
---|
| 460 | self.theta_offset = offset |
---|
| 461 | break |
---|
| 462 | offset += p.length |
---|
| 463 | else: |
---|
| 464 | self.theta_offset = -1 |
---|
[69aa451] | 465 | |
---|
[d19962c] | 466 | # number of polydisperse parameters |
---|
| 467 | num_pd = sum(p.length for p in self.kernel_parameters if p.polydisperse) |
---|
| 468 | # Don't use more polydisperse parameters than are available in the model |
---|
| 469 | self.max_pd = min(num_pd, MAX_PD) |
---|
[69aa451] | 470 | |
---|
[d19962c] | 471 | # true if has 2D parameters |
---|
| 472 | self.has_2d = any(p.type in ('orientation', 'magnetic') |
---|
| 473 | for p in self.kernel_parameters) |
---|
[8698a0d] | 474 | self.is_asymmetric = any(p.name == 'psi' for p in self.kernel_parameters) |
---|
[bb4b509] | 475 | self.magnetism_index = [k for k, p in enumerate(self.call_parameters) |
---|
[32e3c9b] | 476 | if p.id.startswith('M0:')] |
---|
[69aa451] | 477 | |
---|
[d19962c] | 478 | self.pd_1d = set(p.name for p in self.call_parameters |
---|
[a86f6c0] | 479 | if p.polydisperse and p.type not in ('orientation', 'magnetic')) |
---|
[32e3c9b] | 480 | self.pd_2d = set(p.name for p in self.call_parameters if p.polydisperse) |
---|
[69aa451] | 481 | |
---|
[8698a0d] | 482 | def _check_angles(self): |
---|
| 483 | theta = phi = psi = -1 |
---|
| 484 | for k, p in enumerate(self.kernel_parameters): |
---|
| 485 | if p.name == 'theta': |
---|
| 486 | theta = k |
---|
| 487 | if p.type != 'orientation': |
---|
| 488 | raise TypeError("theta must be an orientation parameter") |
---|
| 489 | elif p.name == 'phi': |
---|
| 490 | phi = k |
---|
| 491 | if p.type != 'orientation': |
---|
| 492 | raise TypeError("phi must be an orientation parameter") |
---|
| 493 | elif p.name == 'psi': |
---|
| 494 | psi = k |
---|
| 495 | if p.type != 'orientation': |
---|
| 496 | raise TypeError("psi must be an orientation parameter") |
---|
| 497 | if theta >= 0 and phi >= 0: |
---|
| 498 | if phi != theta+1: |
---|
| 499 | raise TypeError("phi must follow theta") |
---|
| 500 | if psi >= 0 and psi != phi+1: |
---|
| 501 | raise TypeError("psi must follow phi") |
---|
| 502 | elif theta >= 0 or phi >= 0 or psi >= 0: |
---|
| 503 | raise TypeError("oriented shapes must have both theta and phi and maybe psi") |
---|
| 504 | |
---|
[0bdddc2] | 505 | def __getitem__(self, key): |
---|
| 506 | # Find the parameter definition |
---|
| 507 | for par in self.call_parameters: |
---|
| 508 | if par.name == key: |
---|
[e65c3ba] | 509 | return par |
---|
| 510 | raise KeyError("unknown parameter %r"%key) |
---|
[0bdddc2] | 511 | |
---|
[e3571cb] | 512 | def __contains__(self, key): |
---|
| 513 | for par in self.call_parameters: |
---|
| 514 | if par.name == key: |
---|
| 515 | return True |
---|
[e65c3ba] | 516 | return False |
---|
[e3571cb] | 517 | |
---|
[a86f6c0] | 518 | def _set_vector_lengths(self): |
---|
[256dfe1] | 519 | # type: () -> List[str] |
---|
[a86f6c0] | 520 | """ |
---|
| 521 | Walk the list of kernel parameters, setting the length field of the |
---|
| 522 | vector parameters from the upper limit of the reference parameter. |
---|
| 523 | |
---|
| 524 | This needs to be done once the entire parameter table is available |
---|
| 525 | since the reference may still be undefined when the parameter is |
---|
| 526 | initially created. |
---|
| 527 | |
---|
[256dfe1] | 528 | Returns the list of control parameter names. |
---|
| 529 | |
---|
[a86f6c0] | 530 | Note: This modifies the underlying parameter object. |
---|
| 531 | """ |
---|
| 532 | # Sort out the length of the vector parameters such as thickness[n] |
---|
| 533 | for p in self.kernel_parameters: |
---|
| 534 | if p.length_control: |
---|
[e65c3ba] | 535 | ref = self._get_ref(p) |
---|
[a86f6c0] | 536 | ref.is_control = True |
---|
[256dfe1] | 537 | ref.polydisperse = False |
---|
[a86f6c0] | 538 | low, high = ref.limits |
---|
| 539 | if int(low) != low or int(high) != high or low < 0 or high > 20: |
---|
| 540 | raise ValueError("expected limits on %s to be within [0, 20]" |
---|
| 541 | % ref.name) |
---|
[04045f4] | 542 | p.length = int(high) |
---|
[a86f6c0] | 543 | |
---|
[e65c3ba] | 544 | def _get_ref(self, p): |
---|
| 545 | # type: (Parameter) -> Parameter |
---|
| 546 | for ref in self.kernel_parameters: |
---|
| 547 | if ref.id == p.length_control: |
---|
| 548 | return ref |
---|
| 549 | raise ValueError("no reference variable %r for %s" |
---|
| 550 | % (p.length_control, p.name)) |
---|
| 551 | |
---|
[a86f6c0] | 552 | def _get_defaults(self): |
---|
| 553 | # type: () -> ParameterSet |
---|
| 554 | """ |
---|
| 555 | Get a list of parameter defaults from the parameters. |
---|
| 556 | |
---|
| 557 | Expands vector parameters into parameter id+number. |
---|
| 558 | """ |
---|
| 559 | # Construct default values, including vector defaults |
---|
| 560 | defaults = {} |
---|
| 561 | for p in self.call_parameters: |
---|
| 562 | if p.length == 1: |
---|
| 563 | defaults[p.id] = p.default |
---|
| 564 | else: |
---|
| 565 | for k in range(1, p.length+1): |
---|
| 566 | defaults["%s%d"%(p.id, k)] = p.default |
---|
| 567 | return defaults |
---|
| 568 | |
---|
| 569 | def _get_call_parameters(self): |
---|
| 570 | # type: () -> List[Parameter] |
---|
| 571 | full_list = self.COMMON[:] |
---|
| 572 | for p in self.kernel_parameters: |
---|
| 573 | if p.length == 1: |
---|
| 574 | full_list.append(p) |
---|
| 575 | else: |
---|
| 576 | for k in range(1, p.length+1): |
---|
| 577 | pk = Parameter(p.id+str(k), p.units, p.default, |
---|
| 578 | p.limits, p.type, p.description) |
---|
| 579 | pk.polydisperse = p.polydisperse |
---|
| 580 | pk.relative_pd = p.relative_pd |
---|
[50ec515] | 581 | pk.choices = p.choices |
---|
[a86f6c0] | 582 | full_list.append(pk) |
---|
[32e3c9b] | 583 | |
---|
| 584 | # Add the magnetic parameters to the end of the call parameter list. |
---|
[9eb3632] | 585 | if self.nmagnetic > 0: |
---|
[32e3c9b] | 586 | full_list.extend([ |
---|
| 587 | Parameter('up:frac_i', '', 0., [0., 1.], |
---|
| 588 | 'magnetic', 'fraction of spin up incident'), |
---|
| 589 | Parameter('up:frac_f', '', 0., [0., 1.], |
---|
| 590 | 'magnetic', 'fraction of spin up final'), |
---|
| 591 | Parameter('up:angle', 'degress', 0., [0., 360.], |
---|
| 592 | 'magnetic', 'spin up angle'), |
---|
| 593 | ]) |
---|
| 594 | slds = [p for p in full_list if p.type == 'sld'] |
---|
| 595 | for p in slds: |
---|
| 596 | full_list.extend([ |
---|
| 597 | Parameter('M0:'+p.id, '1e-6/Ang^2', 0., [-np.inf, np.inf], |
---|
| 598 | 'magnetic', 'magnetic amplitude for '+p.description), |
---|
| 599 | Parameter('mtheta:'+p.id, 'degrees', 0., [-90., 90.], |
---|
[bb4b509] | 600 | 'magnetic', 'magnetic latitude for '+p.description), |
---|
[32e3c9b] | 601 | Parameter('mphi:'+p.id, 'degrees', 0., [-180., 180.], |
---|
[bb4b509] | 602 | 'magnetic', 'magnetic longitude for '+p.description), |
---|
[32e3c9b] | 603 | ]) |
---|
| 604 | #print("call parameters", full_list) |
---|
[a86f6c0] | 605 | return full_list |
---|
| 606 | |
---|
[85fe7f8] | 607 | def user_parameters(self, pars, is2d=True): |
---|
[0d99a6a] | 608 | # type: (Dict[str, float], bool) -> List[Parameter] |
---|
[69aa451] | 609 | """ |
---|
[d19962c] | 610 | Return the list of parameters for the given data type. |
---|
| 611 | |
---|
[c5ac2b2] | 612 | Vector parameters are expanded in place. If multiple parameters |
---|
[d19962c] | 613 | share the same vector length, then the parameters will be interleaved |
---|
| 614 | in the result. The control parameters come first. For example, |
---|
| 615 | if the parameter table is ordered as:: |
---|
| 616 | |
---|
| 617 | sld_core |
---|
| 618 | sld_shell[num_shells] |
---|
| 619 | sld_solvent |
---|
| 620 | thickness[num_shells] |
---|
| 621 | num_shells |
---|
| 622 | |
---|
| 623 | and *pars[num_shells]=2* then the returned list will be:: |
---|
| 624 | |
---|
| 625 | num_shells |
---|
| 626 | scale |
---|
| 627 | background |
---|
| 628 | sld_core |
---|
| 629 | sld_shell1 |
---|
| 630 | thickness1 |
---|
| 631 | sld_shell2 |
---|
| 632 | thickness2 |
---|
| 633 | sld_solvent |
---|
| 634 | |
---|
| 635 | Note that shell/thickness pairs are grouped together in the result |
---|
| 636 | even though they were not grouped in the incoming table. The control |
---|
| 637 | parameter is always returned first since the GUI will want to set it |
---|
| 638 | early, and rerender the table when it is changed. |
---|
[32e3c9b] | 639 | |
---|
| 640 | Parameters marked as sld will automatically have a set of associated |
---|
| 641 | magnetic parameters (m0:p, mtheta:p, mphi:p), as well as polarization |
---|
| 642 | information (up:theta, up:frac_i, up:frac_f). |
---|
[69aa451] | 643 | """ |
---|
[04045f4] | 644 | # control parameters go first |
---|
[d19962c] | 645 | control = [p for p in self.kernel_parameters if p.is_control] |
---|
| 646 | |
---|
| 647 | # Gather entries such as name[n] into groups of the same n |
---|
[04045f4] | 648 | dependent = {} # type: Dict[str, List[Parameter]] |
---|
| 649 | dependent.update((p.id, []) for p in control) |
---|
[d19962c] | 650 | for p in self.kernel_parameters: |
---|
| 651 | if p.length_control is not None: |
---|
| 652 | dependent[p.length_control].append(p) |
---|
| 653 | |
---|
| 654 | # Gather entries such as name[4] into groups of the same length |
---|
[32e3c9b] | 655 | fixed_length = {} # type: Dict[int, List[Parameter]] |
---|
[d19962c] | 656 | for p in self.kernel_parameters: |
---|
| 657 | if p.length > 1 and p.length_control is None: |
---|
[32e3c9b] | 658 | fixed_length.setdefault(p.length, []).append(p) |
---|
[d19962c] | 659 | |
---|
| 660 | # Using the call_parameters table, we already have expanded forms |
---|
| 661 | # for each of the vector parameters; put them in a lookup table |
---|
[f88e248] | 662 | # Note: p.id and p.name are currently identical for the call parameters |
---|
| 663 | expanded_pars = dict((p.id, p) for p in self.call_parameters) |
---|
[d19962c] | 664 | |
---|
[32e3c9b] | 665 | def append_group(name): |
---|
| 666 | """add the named parameter, and related magnetic parameters if any""" |
---|
| 667 | result.append(expanded_pars[name]) |
---|
| 668 | if is2d: |
---|
| 669 | for tag in 'M0:', 'mtheta:', 'mphi:': |
---|
| 670 | if tag+name in expanded_pars: |
---|
| 671 | result.append(expanded_pars[tag+name]) |
---|
| 672 | |
---|
[d19962c] | 673 | # Gather the user parameters in order |
---|
| 674 | result = control + self.COMMON |
---|
| 675 | for p in self.kernel_parameters: |
---|
| 676 | if not is2d and p.type in ('orientation', 'magnetic'): |
---|
| 677 | pass |
---|
| 678 | elif p.is_control: |
---|
| 679 | pass # already added |
---|
| 680 | elif p.length_control is not None: |
---|
| 681 | table = dependent.get(p.length_control, []) |
---|
| 682 | if table: |
---|
| 683 | # look up length from incoming parameters |
---|
[fb5914f] | 684 | table_length = int(pars.get(p.length_control, p.length)) |
---|
[d19962c] | 685 | del dependent[p.length_control] # first entry seen |
---|
| 686 | for k in range(1, table_length+1): |
---|
| 687 | for entry in table: |
---|
[32e3c9b] | 688 | append_group(entry.id+str(k)) |
---|
[d19962c] | 689 | else: |
---|
| 690 | pass # already processed all entries |
---|
| 691 | elif p.length > 1: |
---|
[32e3c9b] | 692 | table = fixed_length.get(p.length, []) |
---|
[d19962c] | 693 | if table: |
---|
| 694 | table_length = p.length |
---|
[32e3c9b] | 695 | del fixed_length[p.length] |
---|
[d19962c] | 696 | for k in range(1, table_length+1): |
---|
| 697 | for entry in table: |
---|
[32e3c9b] | 698 | append_group(entry.id+str(k)) |
---|
[d19962c] | 699 | else: |
---|
| 700 | pass # already processed all entries |
---|
| 701 | else: |
---|
[32e3c9b] | 702 | append_group(p.id) |
---|
| 703 | |
---|
| 704 | if is2d and 'up:angle' in expanded_pars: |
---|
| 705 | result.extend([ |
---|
| 706 | expanded_pars['up:frac_i'], |
---|
| 707 | expanded_pars['up:frac_f'], |
---|
| 708 | expanded_pars['up:angle'], |
---|
| 709 | ]) |
---|
[d19962c] | 710 | |
---|
| 711 | return result |
---|
[69aa451] | 712 | |
---|
| 713 | def isstr(x): |
---|
[a86f6c0] | 714 | # type: (Any) -> bool |
---|
[7edce22] | 715 | """ |
---|
| 716 | Return True if the object is a string. |
---|
| 717 | """ |
---|
[69aa451] | 718 | # TODO: 2-3 compatible tests for str, including unicode strings |
---|
| 719 | return isinstance(x, str) |
---|
| 720 | |
---|
[da63656] | 721 | |
---|
| 722 | def _find_source_lines(model_info, kernel_module): |
---|
[e65c3ba] | 723 | # type: (ModelInfo, ModuleType) -> None |
---|
[da63656] | 724 | """ |
---|
| 725 | Identify the location of the C source inside the model definition file. |
---|
| 726 | |
---|
| 727 | This code runs through the source of the kernel module looking for |
---|
| 728 | lines that start with 'Iq', 'Iqxy' or 'form_volume'. Clearly there are |
---|
| 729 | all sorts of reasons why this might not work (e.g., code commented out |
---|
| 730 | in a triple-quoted line block, code built using string concatenation, |
---|
| 731 | or code defined in the branch of an 'if' block), but it should work |
---|
| 732 | properly in the 95% case, and getting the incorrect line number will |
---|
| 733 | be harmless. |
---|
| 734 | """ |
---|
| 735 | # Check if we need line numbers at all |
---|
| 736 | if callable(model_info.Iq): |
---|
| 737 | return None |
---|
| 738 | |
---|
| 739 | if (model_info.Iq is None |
---|
[bb4b509] | 740 | and model_info.Iqxy is None |
---|
| 741 | and model_info.Imagnetic is None |
---|
| 742 | and model_info.form_volume is None): |
---|
[da63656] | 743 | return |
---|
| 744 | |
---|
| 745 | # find the defintion lines for the different code blocks |
---|
[98ba1fc] | 746 | try: |
---|
| 747 | source = inspect.getsource(kernel_module) |
---|
| 748 | except IOError: |
---|
| 749 | return |
---|
[da63656] | 750 | for k, v in enumerate(source.split('\n')): |
---|
[32e3c9b] | 751 | if v.startswith('Imagnetic'): |
---|
| 752 | model_info._Imagnetic_line = k+1 |
---|
| 753 | elif v.startswith('Iqxy'): |
---|
[da63656] | 754 | model_info._Iqxy_line = k+1 |
---|
| 755 | elif v.startswith('Iq'): |
---|
| 756 | model_info._Iq_line = k+1 |
---|
| 757 | elif v.startswith('form_volume'): |
---|
| 758 | model_info._form_volume_line = k+1 |
---|
| 759 | |
---|
| 760 | |
---|
[6d6508e] | 761 | def make_model_info(kernel_module): |
---|
[7edce22] | 762 | # type: (module) -> ModelInfo |
---|
| 763 | """ |
---|
| 764 | Extract the model definition from the loaded kernel module. |
---|
| 765 | |
---|
| 766 | Fill in default values for parts of the module that are not provided. |
---|
| 767 | |
---|
| 768 | Note: vectorized Iq and Iqxy functions will be created for python |
---|
| 769 | models when the model is first called, not when the model is loaded. |
---|
| 770 | """ |
---|
[65314f7] | 771 | if hasattr(kernel_module, "model_info"): |
---|
| 772 | # Custom sum/multi models |
---|
| 773 | return kernel_module.model_info |
---|
[6d6508e] | 774 | info = ModelInfo() |
---|
| 775 | #print("make parameter table", kernel_module.parameters) |
---|
[c1a888b] | 776 | parameters = make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
[4bfd277] | 777 | demo = expand_pars(parameters, getattr(kernel_module, 'demo', None)) |
---|
[724257c] | 778 | filename = abspath(kernel_module.__file__).replace('.pyc', '.py') |
---|
[6d6508e] | 779 | kernel_id = splitext(basename(filename))[0] |
---|
| 780 | name = getattr(kernel_module, 'name', None) |
---|
| 781 | if name is None: |
---|
| 782 | name = " ".join(w.capitalize() for w in kernel_id.split('_')) |
---|
| 783 | |
---|
| 784 | info.id = kernel_id # string used to load the kernel |
---|
[724257c] | 785 | info.filename = filename |
---|
[6d6508e] | 786 | info.name = name |
---|
| 787 | info.title = getattr(kernel_module, 'title', name+" model") |
---|
| 788 | info.description = getattr(kernel_module, 'description', 'no description') |
---|
| 789 | info.parameters = parameters |
---|
| 790 | info.demo = demo |
---|
| 791 | info.composition = None |
---|
| 792 | info.docs = kernel_module.__doc__ |
---|
| 793 | info.category = getattr(kernel_module, 'category', None) |
---|
| 794 | info.structure_factor = getattr(kernel_module, 'structure_factor', False) |
---|
[04045f4] | 795 | info.profile_axes = getattr(kernel_module, 'profile_axes', ['x', 'y']) |
---|
[db03406] | 796 | info.c_code = getattr(kernel_module, 'c_code', None) |
---|
[6d6508e] | 797 | info.source = getattr(kernel_module, 'source', []) |
---|
[e62a134] | 798 | # TODO: check the structure of the tests |
---|
[6d6508e] | 799 | info.tests = getattr(kernel_module, 'tests', []) |
---|
[9a943d0] | 800 | info.ER = getattr(kernel_module, 'ER', None) # type: ignore |
---|
| 801 | info.VR = getattr(kernel_module, 'VR', None) # type: ignore |
---|
| 802 | info.form_volume = getattr(kernel_module, 'form_volume', None) # type: ignore |
---|
| 803 | info.Iq = getattr(kernel_module, 'Iq', None) # type: ignore |
---|
| 804 | info.Iqxy = getattr(kernel_module, 'Iqxy', None) # type: ignore |
---|
[32e3c9b] | 805 | info.Imagnetic = getattr(kernel_module, 'Imagnetic', None) # type: ignore |
---|
[9a943d0] | 806 | info.profile = getattr(kernel_module, 'profile', None) # type: ignore |
---|
| 807 | info.sesans = getattr(kernel_module, 'sesans', None) # type: ignore |
---|
[5124c969] | 808 | # Default single and opencl to True for C models. Python models have callable Iq. |
---|
| 809 | info.opencl = getattr(kernel_module, 'opencl', not callable(info.Iq)) |
---|
| 810 | info.single = getattr(kernel_module, 'single', not callable(info.Iq)) |
---|
[0bdddc2] | 811 | info.random = getattr(kernel_module, 'random', None) |
---|
[256dfe1] | 812 | |
---|
| 813 | # multiplicity info |
---|
| 814 | control_pars = [p.id for p in parameters.kernel_parameters if p.is_control] |
---|
| 815 | default_control = control_pars[0] if control_pars else None |
---|
| 816 | info.control = getattr(kernel_module, 'control', default_control) |
---|
[04045f4] | 817 | info.hidden = getattr(kernel_module, 'hidden', None) # type: ignore |
---|
[6d6508e] | 818 | |
---|
[da63656] | 819 | _find_source_lines(info, kernel_module) |
---|
[db03406] | 820 | try: |
---|
| 821 | autoc.convert(info, kernel_module) |
---|
| 822 | except Exception as exc: |
---|
| 823 | raise |
---|
| 824 | logger.warn(str(exc)) |
---|
[da63656] | 825 | |
---|
[6d6508e] | 826 | return info |
---|
| 827 | |
---|
| 828 | class ModelInfo(object): |
---|
| 829 | """ |
---|
| 830 | Interpret the model definition file, categorizing the parameters. |
---|
| 831 | |
---|
| 832 | The module can be loaded with a normal python import statement if you |
---|
| 833 | know which module you need, or with __import__('sasmodels.model.'+name) |
---|
| 834 | if the name is in a string. |
---|
| 835 | |
---|
[7edce22] | 836 | The structure should be mostly static, other than the delayed definition |
---|
| 837 | of *Iq* and *Iqxy* if they need to be defined. |
---|
[6d6508e] | 838 | """ |
---|
[a5b8477] | 839 | #: Full path to the file defining the kernel, if any. |
---|
[340428e] | 840 | filename = None # type: Optional[str] |
---|
[a5b8477] | 841 | #: Id of the kernel used to load it from the filesystem. |
---|
[a86f6c0] | 842 | id = None # type: str |
---|
[a5b8477] | 843 | #: Display name of the model, which defaults to the model id but with |
---|
| 844 | #: capitalization of the parts so for example core_shell defaults to |
---|
| 845 | #: "Core Shell". |
---|
[a86f6c0] | 846 | name = None # type: str |
---|
[a5b8477] | 847 | #: Short description of the model. |
---|
[a86f6c0] | 848 | title = None # type: str |
---|
[a5b8477] | 849 | #: Long description of the model. |
---|
[a86f6c0] | 850 | description = None # type: str |
---|
[a5b8477] | 851 | #: Model parameter table. Parameters are defined using a list of parameter |
---|
| 852 | #: definitions, each of which is contains parameter name, units, |
---|
| 853 | #: default value, limits, type and description. See :class:`Parameter` |
---|
| 854 | #: for details on the individual parameters. The parameters are gathered |
---|
| 855 | #: into a :class:`ParameterTable`, which provides various views into the |
---|
| 856 | #: parameter list. |
---|
[a86f6c0] | 857 | parameters = None # type: ParameterTable |
---|
[a5b8477] | 858 | #: Demo parameters as a *parameter:value* map used as the default values |
---|
| 859 | #: for :mod:`compare`. Any parameters not set in *demo* will use the |
---|
| 860 | #: defaults from the parameter table. That means no polydispersity, and |
---|
| 861 | #: in the case of multiplicity models, a minimal model with no interesting |
---|
| 862 | #: scattering. |
---|
[a86f6c0] | 863 | demo = None # type: Dict[str, float] |
---|
[a5b8477] | 864 | #: Composition is None if this is an independent model, or it is a |
---|
| 865 | #: tuple with comoposition type ('product' or 'misture') and a list of |
---|
| 866 | #: :class:`ModelInfo` blocks for the composed objects. This allows us |
---|
| 867 | #: to rebuild a complete mixture or product model from the info block. |
---|
| 868 | #: *composition* is not given in the model definition file, but instead |
---|
| 869 | #: arises when the model is constructed using names such as |
---|
| 870 | #: *sphere*hardsphere* or *cylinder+sphere*. |
---|
[a86f6c0] | 871 | composition = None # type: Optional[Tuple[str, List[ModelInfo]]] |
---|
[a5b8477] | 872 | #: Name of the control parameter for a variant model such as :ref:`rpa`. |
---|
| 873 | #: The *control* parameter should appear in the parameter table, with |
---|
| 874 | #: limits defined as *[CASES]*, for case names such as |
---|
| 875 | #: *CASES = ["diblock copolymer", "triblock copolymer", ...]*. |
---|
| 876 | #: This should give *limits=[[case1, case2, ...]]*, but the |
---|
| 877 | #: model loader translates this to *limits=[0, len(CASES)-1]*, and adds |
---|
| 878 | #: *choices=CASES* to the :class:`Parameter` definition. Note that |
---|
| 879 | #: models can use a list of cases as a parameter without it being a |
---|
| 880 | #: control parameter. Either way, the parameter is sent to the model |
---|
| 881 | #: evaluator as *float(choice_num)*, where choices are numbered from 0. |
---|
| 882 | #: See also :attr:`hidden`. |
---|
[04045f4] | 883 | control = None # type: str |
---|
[a5b8477] | 884 | #: Different variants require different parameters. In order to show |
---|
| 885 | #: just the parameters needed for the variant selected by :attr:`control`, |
---|
| 886 | #: you should provide a function *hidden(control) -> set(['a', 'b', ...])* |
---|
| 887 | #: indicating which parameters need to be hidden. For multiplicity |
---|
| 888 | #: models, you need to use the complete name of the parameter, including |
---|
| 889 | #: its number. So for example, if variant "a" uses only *sld1* and *sld2*, |
---|
| 890 | #: then *sld3*, *sld4* and *sld5* of multiplicity parameter *sld[5]* |
---|
| 891 | #: should be in the hidden set. |
---|
| 892 | hidden = None # type: Optional[Callable[[int], Set[str]]] |
---|
| 893 | #: Doc string from the top of the model file. This should be formatted |
---|
| 894 | #: using ReStructuredText format, with latex markup in ".. math" |
---|
| 895 | #: environments, or in dollar signs. This will be automatically |
---|
| 896 | #: extracted to a .rst file by :func:`generate.make_docs`, then |
---|
| 897 | #: converted to HTML or PDF by Sphinx. |
---|
[a86f6c0] | 898 | docs = None # type: str |
---|
[a5b8477] | 899 | #: Location of the model description in the documentation. This takes the |
---|
| 900 | #: form of "section" or "section:subsection". So for example, |
---|
| 901 | #: :ref:`porod` uses *category="shape-independent"* so it is in the |
---|
[40a87fa] | 902 | #: :ref:`shape-independent` section whereas |
---|
| 903 | #: :ref:`capped-cylinder` uses: *category="shape:cylinder"*, which puts |
---|
[a5b8477] | 904 | #: it in the :ref:`shape-cylinder` section. |
---|
[a86f6c0] | 905 | category = None # type: Optional[str] |
---|
[a5b8477] | 906 | #: True if the model can be computed accurately with single precision. |
---|
[40a87fa] | 907 | #: This is True by default, but models such as :ref:`bcc-paracrystal` set |
---|
[a5b8477] | 908 | #: it to False because they require double precision calculations. |
---|
[a86f6c0] | 909 | single = None # type: bool |
---|
[bb4b509] | 910 | #: True if the model can be run as an opencl model. If for some reason |
---|
| 911 | #: the model cannot be run in opencl (e.g., because the model passes |
---|
| 912 | #: functions by reference), then set this to false. |
---|
| 913 | opencl = None # type: bool |
---|
[a5b8477] | 914 | #: True if the model is a structure factor used to model the interaction |
---|
| 915 | #: between form factor models. This will default to False if it is not |
---|
| 916 | #: provided in the file. |
---|
[a86f6c0] | 917 | structure_factor = None # type: bool |
---|
[a5b8477] | 918 | #: List of C source files used to define the model. The source files |
---|
| 919 | #: should define the *Iq* function, and possibly *Iqxy*, though a default |
---|
| 920 | #: *Iqxy = Iq(sqrt(qx**2+qy**2)* will be created if no *Iqxy* is provided. |
---|
| 921 | #: Files containing the most basic functions must appear first in the list, |
---|
| 922 | #: followed by the files that use those functions. Form factors are |
---|
| 923 | #: indicated by providing a :attr:`ER` function. |
---|
[a86f6c0] | 924 | source = None # type: List[str] |
---|
[a5b8477] | 925 | #: The set of tests that must pass. The format of the tests is described |
---|
| 926 | #: in :mod:`model_test`. |
---|
[a86f6c0] | 927 | tests = None # type: List[TestCondition] |
---|
[a5b8477] | 928 | #: Returns the effective radius of the model given its volume parameters. |
---|
| 929 | #: The presence of *ER* indicates that the model is a form factor model |
---|
| 930 | #: that may be used together with a structure factor to form an implicit |
---|
| 931 | #: multiplication model. |
---|
| 932 | #: |
---|
| 933 | #: The parameters to the *ER* function must be marked with type *volume*. |
---|
| 934 | #: in the parameter table. They will appear in the same order as they |
---|
| 935 | #: do in the table. The values passed to *ER* will be vectors, with one |
---|
| 936 | #: value for each polydispersity condition. For example, if the model |
---|
| 937 | #: is polydisperse over both length and radius, then both length and |
---|
| 938 | #: radius will have the same number of values in the vector, with one |
---|
| 939 | #: value for each *length X radius*. If only *radius* is polydisperse, |
---|
| 940 | #: then the value for *length* will be repeated once for each value of |
---|
| 941 | #: *radius*. The *ER* function should return one effective radius for |
---|
| 942 | #: each parameter set. Multiplicity parameters will be received as |
---|
| 943 | #: arrays, with one row per polydispersity condition. |
---|
[c1a888b] | 944 | ER = None # type: Optional[Callable[[np.ndarray], np.ndarray]] |
---|
[a5b8477] | 945 | #: Returns the occupied volume and the total volume for each parameter set. |
---|
| 946 | #: See :attr:`ER` for details on the parameters. |
---|
[c1a888b] | 947 | VR = None # type: Optional[Callable[[np.ndarray], Tuple[np.ndarray, np.ndarray]]] |
---|
[db03406] | 948 | #: Arbitrary C code containing supporting functions, etc., to be inserted |
---|
| 949 | #: after everything in source. This can include Iq and Iqxy functions with |
---|
| 950 | #: the full function signature, including all parameters. |
---|
| 951 | c_code = None |
---|
[a5b8477] | 952 | #: Returns the form volume for python-based models. Form volume is needed |
---|
| 953 | #: for volume normalization in the polydispersity integral. If no |
---|
| 954 | #: parameters are *volume* parameters, then form volume is not needed. |
---|
| 955 | #: For C-based models, (with :attr:`sources` defined, or with :attr:`Iq` |
---|
| 956 | #: defined using a string containing C code), form_volume must also be |
---|
| 957 | #: C code, either defined as a string, or in the sources. |
---|
[f619de7] | 958 | form_volume = None # type: Union[None, str, Callable[[np.ndarray], float]] |
---|
[a5b8477] | 959 | #: Returns *I(q, a, b, ...)* for parameters *a*, *b*, etc. defined |
---|
| 960 | #: by the parameter table. *Iq* can be defined as a python function, or |
---|
| 961 | #: as a C function. If it is defined in C, then set *Iq* to the body of |
---|
| 962 | #: the C function, including the return statement. This function takes |
---|
| 963 | #: values for *q* and each of the parameters as separate *double* values |
---|
| 964 | #: (which may be converted to float or long double by sasmodels). All |
---|
| 965 | #: source code files listed in :attr:`sources` will be loaded before the |
---|
| 966 | #: *Iq* function is defined. If *Iq* is not present, then sources should |
---|
| 967 | #: define *static double Iq(double q, double a, double b, ...)* which |
---|
| 968 | #: will return *I(q, a, b, ...)*. Multiplicity parameters are sent as |
---|
| 969 | #: pointers to doubles. Constants in floating point expressions should |
---|
| 970 | #: include the decimal point. See :mod:`generate` for more details. |
---|
[f619de7] | 971 | Iq = None # type: Union[None, str, Callable[[np.ndarray], np.ndarray]] |
---|
[a5b8477] | 972 | #: Returns *I(qx, qy, a, b, ...)*. The interface follows :attr:`Iq`. |
---|
[f619de7] | 973 | Iqxy = None # type: Union[None, str, Callable[[np.ndarray], np.ndarray]] |
---|
[32e3c9b] | 974 | #: Returns *I(qx, qy, a, b, ...)*. The interface follows :attr:`Iq`. |
---|
| 975 | Imagnetic = None # type: Union[None, str, Callable[[np.ndarray], np.ndarray]] |
---|
[a5b8477] | 976 | #: Returns a model profile curve *x, y*. If *profile* is defined, this |
---|
| 977 | #: curve will appear in response to the *Show* button in SasView. Use |
---|
| 978 | #: :attr:`profile_axes` to set the axis labels. Note that *y* values |
---|
| 979 | #: will be scaled by 1e6 before plotting. |
---|
[c1a888b] | 980 | profile = None # type: Optional[Callable[[np.ndarray], None]] |
---|
[a5b8477] | 981 | #: Axis labels for the :attr:`profile` plot. The default is *['x', 'y']*. |
---|
| 982 | #: Only the *x* component is used for now. |
---|
| 983 | profile_axes = None # type: Tuple[str, str] |
---|
| 984 | #: Returns *sesans(z, a, b, ...)* for models which can directly compute |
---|
| 985 | #: the SESANS correlation function. Note: not currently implemented. |
---|
[c1a888b] | 986 | sesans = None # type: Optional[Callable[[np.ndarray], np.ndarray]] |
---|
[e65c3ba] | 987 | #: Returns a random parameter set for the model |
---|
| 988 | random = None # type: Optional[Callable[[], Dict[str, float]]] |
---|
[6d6508e] | 989 | |
---|
[da63656] | 990 | # line numbers within the python file for bits of C source, if defined |
---|
[98ba1fc] | 991 | # NB: some compilers fail with a "#line 0" directive, so default to 1. |
---|
[32e3c9b] | 992 | _Imagnetic_line = 1 |
---|
[98ba1fc] | 993 | _Iqxy_line = 1 |
---|
| 994 | _Iq_line = 1 |
---|
| 995 | _form_volume_line = 1 |
---|
[da63656] | 996 | |
---|
| 997 | |
---|
[6d6508e] | 998 | def __init__(self): |
---|
[a86f6c0] | 999 | # type: () -> None |
---|
[6d6508e] | 1000 | pass |
---|
| 1001 | |
---|
[04045f4] | 1002 | def get_hidden_parameters(self, control): |
---|
[a5b8477] | 1003 | """ |
---|
| 1004 | Returns the set of hidden parameters for the model. *control* is the |
---|
| 1005 | value of the control parameter. Note that multiplicity models have |
---|
| 1006 | an implicit control parameter, which is the parameter that controls |
---|
| 1007 | the multiplicity. |
---|
| 1008 | """ |
---|
[04045f4] | 1009 | if self.hidden is not None: |
---|
| 1010 | hidden = self.hidden(control) |
---|
| 1011 | else: |
---|
[ce176ca] | 1012 | controls = [p for p in self.parameters.kernel_parameters |
---|
| 1013 | if p.is_control] |
---|
[04045f4] | 1014 | if len(controls) != 1: |
---|
| 1015 | raise ValueError("more than one control parameter") |
---|
| 1016 | hidden = set(p.id+str(k) |
---|
| 1017 | for p in self.parameters.kernel_parameters |
---|
| 1018 | for k in range(control+1, p.length+1) |
---|
| 1019 | if p.length > 1) |
---|
| 1020 | return hidden |
---|