# 31. Sasview interface

## `sasmodels.sasview_model`

Sasview model constructor.

Given a module defining an OpenCL kernel such as sasmodels.models.cylinder, create a sasview model class to run that kernel as follows:

    from sasmodels.sasview_model import load_custom_model
    CylinderModel = load_custom_model('sasmodels/models/cylinder.py')

sasmodels.sasview_model.MODELS: dict[str, Callable[[int], SasviewModel]] = {}

  set of defined models (standard and custom)

sasmodels.sasview_model.MODEL_BY_PATH: dict[str, Callable[[int], SasviewModel]] = {}

  custom model {path: model} mapping so we can check timestamps

sasmodels.sasview_model.MultiplicationModel(form_factor: SasviewModel, structure_factor: SasviewModel) → SasviewModel

  Returns a constructed product model from form_factor and structure_factor.

class sasmodels.sasview_model.MultiplicityInfo(number, control, choices, x_axis_label)

  Bases: `tuple`

  choices

    Alias for field number 2

  control

    Alias for field number 1

  count(value, /)

    Return number of occurrences of value.

  index(value, start=0, stop=9223372036854775807, /)

    Return first index of value.

    Raises ValueError if the value is not present.

  number

    Alias for field number 0

  x_axis_label

    Alias for field number 3

sasmodels.sasview_model.MultiplicityInfoType

  alias of `MultiplicityInfo`

sasmodels.sasview_model.SUPPORT_OLD_STYLE_PLUGINS = True

  True if pre-existing plugins, with the old names and parameters, should continue to be supported.

class sasmodels.sasview_model.SasviewModel(multiplicity: int | None = None)

  Bases: `object`

  Sasview wrapper for opencl/ctypes model.

  calc_composition_models(qx)

    returns parts of the composition model or None if not a composition model.

  calculate_ER(mode: int = 1) → float

    Calculate the effective radius for P(q)*S(q)

    mode is the R_eff type, which defaults to 1 to match the ER calculation for sasview models from version 3.x.

    Returns:

      the value of the effective radius

  calculate_Iq(qx: Sequence[float], qy: Sequence[float] | None = None) → tuple[ndarray, Callable[[], OrderedDict[str, ndarray]]]

    Calculate Iq for one set of q with the current parameters.

    If the model is 1D, use q. If 2D, use qx, qy.

    This should NOT be used for fitting since it copies the q vectors to the card for each evaluation.

    The returned tuple contains the scattering intensity followed by a callable which returns a dictionary of intermediate data from ProductKernel.

  calculate_VR() → float

    Calculate the volf ratio for P(q)*S(q)

    Returns:

      the value of the form:shell volume ratio

  category: str = None

    default model category

  clone() → SasviewModel

    Return a identical copy of self

  cutoff = 1e-05

    default cutoff for polydispersity

  description: str = None

    short model description

  details: dict[str, Sequence[Any]] = None

    units and limits for each parameter

  dispersion: dict[str, Any] = None

    values for dispersion width, npts, nsigmas and type

  evalDistribution(qdist: ndarray | tuple[ndarray, ndarray] | list[ndarray]) → ndarray

    Evaluate a distribution of q-values.

    Parameters:

      qdist – array of q or a list of arrays [qx,qy]

    - For 1D, a numpy array is expected as input

          evalDistribution(q)

        where *q* is a numpy array.

    - For 2D, a list of [qx,qy] is expected with 1D arrays as input

        qx = [ qx[0], qx[1], qx[2], ....]
        qy = [ qy[0], qy[1], qy[2], ....]

    If the model is 1D only, then

    \[q = \sqrt{q_x^2+q_y^2}\]
  fixed: list[str] = None

    names of the fittable parameters

  getDispParamList() → Sequence[str]

    Return a list of polydispersity parameters for the model

  getParam(name: str) → float

    Set the value of a model parameter

    Parameters:

      name – name of the parameter

  getParamList() → Sequence[str]

    Return a list of all available parameters for the model

  getProfile() → tuple[ndarray, ndarray]

    Get SLD profile

    : return: (z, beta) where z is a list of depth of the transition points

      beta is a list of the corresponding SLD values

  id: str = None

    load/save name for the model

  input_name = 'Q'
  input_unit = 'A^{-1}'
  is_fittable(par_name: str) → bool

    Check if a given parameter is fittable or not

    Parameters:

      par_name – the parameter name to check

  is_form_factor = False

    True if model should appear as a form factor

  is_multiplicity_model = False

    True if model has multiplicity

  is_structure_factor = False

    True if model should appear as a structure factor

  magnetic_params: list[str] = None

    names of the magnetic parameters in the order they appear

  multiplicity: int | None = None

    multiplicity value, or None if no multiplicity on the model

  multiplicity_info: MultiplicityInfo = None

    Multiplicity information

  name: str = None

    display name for the model

  non_fittable: Sequence[str] = ()

    parameters that are not fitted

  orientation_params: list[str] = None

    names of the orientation parameters in the order they appear

  output_name = 'Intensity'
  output_unit = 'cm^{-1}'
  params: dict[str, float] = None

    parameter {name: value} mapping

  run(x: float | tuple[float, float] | Sequence[float] = 0.0) → float

    Evaluate the model

    Parameters:

      x – input q, or [q,phi]

    Returns:

      scattering function P(q)

    DEPRECATED: use calculate_Iq instead

  classmethod runTests()

    Run any tests built into the model and captures the test output.

    Returns success flag and output

  runXY(x: float | [<class 'float'>, <class 'float'>] | ~collections.abc.Sequence[float] = 0.0) → float

    Evaluate the model in cartesian coordinates

    Parameters:

      x – input q, or [qx, qy]

    Returns:

      scattering function P(q)

    DEPRECATED: use calculate_Iq instead

  setParam(name: str, value: float) → None

    Set the value of a model parameter

    Parameters:

      - name – name of the parameter

      - value – value of the parameter

  set_dispersion(parameter: str, dispersion: Dispersion) → None

    Set the dispersion object for a model parameter

    Parameters:

      - parameter – name of the parameter [string]

      - dispersion – dispersion object of type Dispersion

sasmodels.sasview_model.find_model(modelname: str) → Callable[[int], SasviewModel]

  Find a model by name. If the model name ends in py, try loading it from custom models, otherwise look for it in the list of builtin models.

sasmodels.sasview_model.load_custom_model(path: str) → Callable[[int], SasviewModel]

  Load a custom model given the model path.

sasmodels.sasview_model.load_standard_models() → list[Callable[[int], SasviewModel]]

  Load and return the list of predefined models.

  If there is an error loading a model, then a traceback is logged and the model is not returned.

sasmodels.sasview_model.magnetic_demo()

  Demostrate call to magnetic model.

sasmodels.sasview_model.make_model_from_info(model_info: ModelInfo) → Callable[[int], SasviewModel]

  Convert model_info into a SasView model wrapper.

sasmodels.sasview_model.reset_environment() → None

  Clear the compute engine context so that the GUI can change devices.

  This removes all compiled kernels, even those that are active on fit pages, but they will be restored the next time they are needed.

sasmodels.sasview_model.test_cylinder() → float

  Test that the cylinder model runs, returning the value at [0.1,0.1].

sasmodels.sasview_model.test_empty_distribution() → None

  Make sure that sasmodels returns NaN when there are no polydispersity points

sasmodels.sasview_model.test_model_list() → None

  Make sure that all models build as sasview models

sasmodels.sasview_model.test_old_name() → None

  Load and run cylinder model as sas.models.CylinderModel

sasmodels.sasview_model.test_product() → float

  Test that 2-D hardsphere model runs and doesn’t produce NaN.

sasmodels.sasview_model.test_rpa() → float

  Test that the 2-D RPA model runs

sasmodels.sasview_model.test_structure_factor() → float

  Test that 2-D hardsphere model runs and doesn’t produce NaN.

sasmodels.sasview_model.test_structure_factor_background() → None

  Check that sasview model and direct model match, with background=0.