19. Parameter and model definitions¶
sasmodels.modelinfo
¶
Model Info and Parameter Tables¶
Defines ModelInfo
and ParameterTable
and the routines for
manipulating them. In particular, make_model_info()
converts a kernel
module into the model info block as seen by the rest of the sasmodels library.
-
class
sasmodels.modelinfo.
ModelInfo
¶ Bases:
object
Interpret the model definition file, categorizing the parameters.
The module can be loaded with a normal python import statement if you know which module you need, or with __import__(‘sasmodels.model.’+name) if the name is in a string.
The structure should be mostly static, other than the delayed definition of Iq and Iqxy if they need to be defined.
Returns the set of hidden parameters for the model. control is the value of the control parameter. Note that multiplicity models have an implicit control parameter, which is the parameter that controls the multiplicity.
-
ER
= None¶ Returns the effective radius of the model given its volume parameters. The presence of ER indicates that the model is a form factor model that may be used together with a structure factor to form an implicit multiplication model.
The parameters to the ER function must be marked with type volume. in the parameter table. They will appear in the same order as they do in the table. The values passed to ER will be vectors, with one value for each polydispersity condition. For example, if the model is polydisperse over both length and radius, then both length and radius will have the same number of values in the vector, with one value for each length X radius. If only radius is polydisperse, then the value for length will be repeated once for each value of radius. The ER function should return one effective radius for each parameter set. Multiplicity parameters will be received as arrays, with one row per polydispersity condition.
-
Iq
= None¶ Returns I(q, a, b, …) for parameters a, b, etc. defined by the parameter table. Iq can be defined as a python function, or as a C function. If it is defined in C, then set Iq to the body of the C function, including the return statement. This function takes values for q and each of the parameters as separate double values (which may be converted to float or long double by sasmodels). All source code files listed in
sources
will be loaded before the Iq function is defined. If Iq is not present, then sources should define static double Iq(double q, double a, double b, …) which will return I(q, a, b, …). Multiplicity parameters are sent as pointers to doubles. Constants in floating point expressions should include the decimal point. Seegenerate
for more details.
-
VR
= None¶ Returns the occupied volume and the total volume for each parameter set. See
ER
for details on the parameters.
-
category
= None¶ Location of the model description in the documentation. This takes the form of “section” or “section:subsection”. So for example, porod uses category=”shape-independent” so it is in the Shape-Independent Functions section whereas capped_cylinder uses: category=”shape:cylinder”, which puts it in the Cylinder Functions section.
-
composition
= None¶ Composition is None if this is an independent model, or it is a tuple with comoposition type (‘product’ or ‘misture’) and a list of
ModelInfo
blocks for the composed objects. This allows us to rebuild a complete mixture or product model from the info block. composition is not given in the model definition file, but instead arises when the model is constructed using names such as sphere*hardsphere or cylinder+sphere.
-
control
= None¶ Name of the control parameter for a variant model such as rpa. The control parameter should appear in the parameter table, with limits defined as [CASES], for case names such as CASES = [“diblock copolymer”, “triblock copolymer”, …]. This should give limits=[[case1, case2, …]], but the model loader translates this to limits=[0, len(CASES)-1], and adds choices=CASES to the
Parameter
definition. Note that models can use a list of cases as a parameter without it being a control parameter. Either way, the parameter is sent to the model evaluator as float(choice_num), where choices are numbered from 0. See alsohidden
.
-
demo
= None¶ Demo parameters as a parameter:value map used as the default values for
compare
. Any parameters not set in demo will use the defaults from the parameter table. That means no polydispersity, and in the case of multiplicity models, a minimal model with no interesting scattering.
-
description
= None¶ Long description of the model.
-
docs
= None¶ Doc string from the top of the model file. This should be formatted using ReStructuredText format, with latex markup in “.. math” environments, or in dollar signs. This will be automatically extracted to a .rst file by
generate.make_docs()
, then converted to HTML or PDF by Sphinx.
-
filename
= None¶ Full path to the file defining the kernel, if any.
-
form_volume
= None¶ Returns the form volume for python-based models. Form volume is needed for volume normalization in the polydispersity integral. If no parameters are volume parameters, then form volume is not needed. For C-based models, (with
sources
defined, or withIq
defined using a string containing C code), form_volume must also be C code, either defined as a string, or in the sources.
Different variants require different parameters. In order to show just the parameters needed for the variant selected by
control
, you should provide a function hidden(control) -> set([‘a’, ‘b’, …]) indicating which parameters need to be hidden. For multiplicity models, you need to use the complete name of the parameter, including its number. So for example, if variant “a” uses only sld1 and sld2, then sld3, sld4 and sld5 of multiplicity parameter sld[5] should be in the hidden set.
-
id
= None¶ Id of the kernel used to load it from the filesystem.
-
name
= None¶ Display name of the model, which defaults to the model id but with capitalization of the parts so for example core_shell defaults to “Core Shell”.
-
opencl
= None¶ True if the model can be run as an opencl model. If for some reason the model cannot be run in opencl (e.g., because the model passes functions by reference), then set this to false.
-
parameters
= None¶ Model parameter table. Parameters are defined using a list of parameter definitions, each of which is contains parameter name, units, default value, limits, type and description. See
Parameter
for details on the individual parameters. The parameters are gathered into aParameterTable
, which provides various views into the parameter list.
-
profile
= None¶ Returns a model profile curve x, y. If profile is defined, this curve will appear in response to the Show button in SasView. Use
profile_axes
to set the axis labels. Note that y values will be scaled by 1e6 before plotting.
-
profile_axes
= None¶ Axis labels for the
profile
plot. The default is [‘x’, ‘y’]. Only the x component is used for now.
-
sesans
= None¶ Returns sesans(z, a, b, …) for models which can directly compute the SESANS correlation function. Note: not currently implemented.
-
single
= None¶ True if the model can be computed accurately with single precision. This is True by default, but models such as bcc_paracrystal set it to False because they require double precision calculations.
-
source
= None¶ List of C source files used to define the model. The source files should define the Iq function, and possibly Iqxy, though a default Iqxy = Iq(sqrt(qx**2+qy**2) will be created if no Iqxy is provided. Files containing the most basic functions must appear first in the list, followed by the files that use those functions. Form factors are indicated by providing a
ER
function.
-
structure_factor
= None¶ True if the model is a structure factor used to model the interaction between form factor models. This will default to False if it is not provided in the file.
-
tests
= None¶ The set of tests that must pass. The format of the tests is described in
model_test
.
-
title
= None¶ Short description of the model.
-
class
sasmodels.modelinfo.
Parameter
(name, units='', default=None, limits=(-inf, inf), ptype='', description='')¶ Bases:
object
The available kernel parameters are defined as a list, with each parameter defined as a sublist with the following elements:
name is the name that will be displayed to the user. Names should be lower case, with words separated by underscore. If acronyms are used, the whole acronym should be upper case. For vector parameters, the name will be followed by [len] where len is an integer length of the vector, or the name of the parameter which controls the length. The attribute id will be created from name without the length.
units should be one of degrees for angles, Ang for lengths, 1e-6/Ang^2 for SLDs.
default value will be the initial value for the model when it is selected, or when an initial value is not otherwise specified.
limits = [lb, ub] are the hard limits on the parameter value, used to limit the polydispersity density function. In the fit, the parameter limits given to the fit are the limits on the central value of the parameter. If there is polydispersity, it will evaluate parameter values outside the fit limits, but not outside the hard limits specified in the model. If there are no limits, use +/-inf imported from numpy.
type indicates how the parameter will be used. “volume” parameters will be used in all functions. “orientation” parameters will be used in Iqxy and Imagnetic. “magnetic* parameters will be used in Imagnetic only. If type is the empty string, the parameter will be used in all of Iq, Iqxy and Imagnetic. “sld” parameters can automatically be promoted to magnetic parameters, each of which will have a magnitude and a direction, which may be different from other sld parameters. The volume parameters are used for calls to form_volume within the kernel (required for volume normalization) and for calls to ER and VR for effective radius and volume ratio respectively.
description is a short description of the parameter. This will be displayed in the parameter table and used as a tool tip for the parameter value in the user interface.
Additional values can be set after the parameter is created:
- length is the length of the field if it is a vector field
- length_control is the parameter which sets the vector length
- is_control is True if the parameter is a control parameter for a vector
- polydisperse is true if the parameter accepts a polydispersity
- relative_pd is true if that polydispersity is a portion of the value (so a 10% length dipsersity would use a polydispersity value of 0.1) rather than absolute dispersisity (such as an angle plus or minus 15 degrees).
choices is the option names for a drop down list of options, as for example, might be used to set the value of a shape parameter.
These values are set by
make_parameter_table()
andparse_parameter()
therein.-
as_call_reference
(prefix='')¶ Return prefix + parameter name. For struct references, use “v.” as the prefix.
-
as_definition
()¶ Declare space for the variable in a parameter structure.
For example, the parameter thickness with length 3 will return “double thickness[3];”, with no spaces before and no new line character afterward.
-
as_function_argument
()¶ Declare the variable as a function argument.
For example, the parameter thickness with length 3 will return “double *thickness”, with no spaces before and no comma afterward.
-
class
sasmodels.modelinfo.
ParameterTable
(parameters)¶ Bases:
object
ParameterTable manages the list of available parameters.
There are a couple of complications which mean that the list of parameters for the kernel differs from the list of parameters that the user sees.
(1) Common parameters. Scale and background are implicit to every model, but are not passed to the kernel.
(2) Vector parameters. Vector parameters are passed to the kernel as a pointer to an array, e.g., thick[], but they are seen by the user as n separate parameters thick1, thick2, …
Therefore, the parameter table is organized by how it is expected to be used. The following information is needed to set up the kernel functions:
- kernel_parameters is the list of parameters in the kernel parameter table, with vector parameter p declared as p[].
- iq_parameters is the list of parameters to the Iq(q, …) function, with vector parameter p sent as p[].
- iqxy_parameters is the list of parameters to the Iqxy(qx, qy, …) function, with vector parameter p sent as p[].
- form_volume_parameters is the list of parameters to the form_volume(…) function, with vector parameter p sent as p[].
Problem details, which sets up the polydispersity loops, requires the following:
- theta_offset is the offset of the theta parameter in the kernel parameter table, with vector parameters counted as n individual parameters p1, p2, …, or offset is -1 if there is no theta parameter.
- max_pd is the maximum number of polydisperse parameters, with vector parameters counted as n individual parameters p1, p2, … Note that this number is limited to sasmodels.modelinfo.MAX_PD.
- npars is the total number of parameters to the kernel, with vector parameters counted as n individual parameters p1, p2, …
- call_parameters is the complete list of parameters to the kernel, including scale and background, with vector parameters recorded as individual parameters p1, p2, …
- active_1d is the set of names that may be polydisperse for 1d data
- active_2d is the set of names that may be polydisperse for 2d data
User parameters are the set of parameters visible to the user, including the scale and background parameters that the kernel does not see. User parameters don’t use vector notation, and instead use p1, p2, …
-
user_parameters
(pars, is2d=True)¶ Return the list of parameters for the given data type.
Vector parameters are expanded in place. If multiple parameters share the same vector length, then the parameters will be interleaved in the result. The control parameters come first. For example, if the parameter table is ordered as:
sld_core sld_shell[num_shells] sld_solvent thickness[num_shells] num_shells
and pars[num_shells]=2 then the returned list will be:
num_shells scale background sld_core sld_shell1 thickness1 sld_shell2 thickness2 sld_solvent
Note that shell/thickness pairs are grouped together in the result even though they were not grouped in the incoming table. The control parameter is always returned first since the GUI will want to set it early, and rerender the table when it is changed.
Parameters marked as sld will automatically have a set of associated magnetic parameters (m0:p, mtheta:p, mphi:p), as well as polarization information (up:theta, up:frac_i, up:frac_f).
-
COMMON
= [P<scale>, P<background>]¶
-
p
= ('background', '1/cm', 0.001, (-inf, inf), '', 'Source background')¶
-
sasmodels.modelinfo.
expand_pars
(partable, pars)¶ Create demo parameter set from key-value pairs.
pars are the key-value pairs to use for the parameters. Any parameters not specified in pars are set from the partable defaults.
If pars references vector fields, such as thickness[n], then support different ways of assigning the demo values, including assigning a specific value (e.g., thickness3=50.0), assigning a new value to all (e.g., thickness=50.0) or assigning values using list notation.
-
sasmodels.modelinfo.
isstr
(x)¶ Return True if the object is a string.
-
sasmodels.modelinfo.
make_model_info
(kernel_module)¶ Extract the model definition from the loaded kernel module.
Fill in default values for parts of the module that are not provided.
Note: vectorized Iq and Iqxy functions will be created for python models when the model is first called, not when the model is loaded.
-
sasmodels.modelinfo.
make_parameter_table
(pars)¶ Construct a parameter table from a list of parameter definitions.
This is used by the module processor to convert the parameter block into the parameter table seen in the
ModelInfo
for the module.
-
sasmodels.modelinfo.
parse_parameter
(name, units='', default=nan, user_limits=None, ptype='', description='')¶ Parse an individual parameter from the parameter definition block.
This does type and value checking on the definition, leading to early failure in the model loading process and easier debugging.
-
sasmodels.modelinfo.
prefix_parameter
(par, prefix)¶ Return a copy of the parameter with its name prefixed.
-
sasmodels.modelinfo.
suffix_parameter
(par, suffix)¶ Return a copy of the parameter with its name prefixed.
-
sasmodels.modelinfo.
MAX_PD
= 4¶ Maximum number of simultaneously polydisperse parameters