# Home

## 11. Simple interface

## `sasmodels.direct_model`

Class interface to the model calculator.

Calling a model is somewhat non-trivial since the functions called depend on the data type. For 1D data the Iq kernel needs to be called, for 2D data the Iqxy kernel needs to be called, and for SESANS data the Iq kernel needs to be called followed by a Hankel transform. Before the kernel is called an appropriate q calculation vector needs to be constructed. This is not the simple q vector where you have measured the data since the resolution calculation will require values beyond the range of the measured data. After the calculation the resolution calculator must be called to return the predicted value for each measured data point.

`DirectModel` is a callable object that takes parameter=value keyword arguments and returns the appropriate theory values for the data.

`DataMixin` does the real work of interpreting the data and calling the model calculator. This is used by `DirectModel`, which uses direct parameter values and by `bumps_model.Experiment` which wraps the parameter values in boxes so that the user can set fitting ranges, etc. on the individual parameters and send the model to the Bumps optimizers.

class sasmodels.direct_model.DataMixin

  Bases: `object`

  DataMixin captures the common aspects of evaluating a SAS model for a particular data set, including calculating Iq and evaluating the resolution function. It is used in particular by `DirectModel`, which evaluates a SAS model parameters as key word arguments to the calculator method, and by `bumps_model.Experiment`, which wraps the model and data for use with the Bumps fitting engine. It is not currently used by `sasview_model.SasviewModel` since this will require a number of changes to SasView before we can do it.

  _interpret_data initializes the data structures necessary to manage the calculations. This sets attributes in the child class such as data_type and resolution.

  _calc_theory evaluates the model at the given control values.

  _set_data sets the intensity data in the data object, possibly with random noise added. This is useful for simulating a dataset with the results from _calc_theory.

class sasmodels.direct_model.DirectModel(data: Data1D | Data2D | SesansData, model: KernelModel, cutoff: float = 1e-05)

  Bases: `DataMixin`

  Create a calculator object for a model.

  data is 1D SAS, 2D SAS or SESANS data

  model is a model calculator return from `core.load_model()`

  cutoff is the polydispersity weight cutoff.

  profile(**pars: float) → None

    Generate a plottable profile.

  simulate_data(noise: float | None = None, **pars: float) → None

    Generate simulated data for the model.

sasmodels.direct_model.Gxi(model, xi, **pars)

  Compute SESANS correlation G’ = G(xi) - G(0) for model. See `Iq()` for details on model and parameters.

sasmodels.direct_model.Iq(model, q, dq=None, ql=None, qw=None, **pars)

  Compute I(q) for model. Resolution is dq for pinhole or ql and qw for slit geometry. Use 0 or None for infinite slits.

  Model is the name of a builtin or custom model, or a model expression, such as sphere+sphere for a mixture of spheres of different radii, or sphere@hardsphere for concentrated solutions where the dilute approximation no longer applies.

  Use additional keywords for model parameters, tagged with _pd, _pd_n, _pd_nsigma, _pd_type to set polydispersity parameters, or _M0, _mphi, _mtheta for magnetic parameters.

  This is not intended for use when the same I(q) is evaluated many times with different parameter values. For that you should set up the model with model = build_model(load_model_info(model_name)), set up a data object to define q values and resolution, then use calculator = DirectModel(data, model) to set up a calculator, or problem = bumps.FitProblem(sasmodels.bumps_model.Experiment(data, model)) to define a fit problem for uses with the bumps optimizer. Data can be loaded using the sasdata package, or use one of the empty data generators from sasmodels.data.

  Models are cached. Custom models will not be reloaded even if the underlying files have changed. If you are using this in a long running application then you will need to call sasmodels.direct_model._model_cache.clear() to reset the cache and force custom model reload.

sasmodels.direct_model.Iqxy(model, qx, qy, dqx=None, dqy=None, **pars)

  Compute I(qx, qy) for model. Resolution is dqx and dqy. See `Iq()` for details on model and parameters.

sasmodels.direct_model.call_Fq(calculator: Kernel, pars: ParameterSet, cutoff: float = 0.0, mono: bool = False) → np.ndarray

  Like `call_kernel()`, but returning F, F^2, R_eff, V_shell, V_form/V_shell.

  For solid objects V_shell is equal to V_form and the volume ratio is 1.

  Use parameter radius_effective_mode to select the effective radius calculation to use amongst the radius_effective_modes list given in the model.

sasmodels.direct_model.call_kernel(calculator: Kernel, pars: ParameterSet, cutoff: float = 0.0, mono: bool = False) → np.ndarray

  Call kernel returned from model.make_kernel with parameters pars.

  cutoff is the limiting value for the product of dispersion weights used to perform the multidimensional dispersion calculation more quickly at a slight cost to accuracy. The default value of cutoff=0 integrates over the entire dispersion cube. Using cutoff=1e-5 can be 50% faster, but with an error of about 1%, which is usually less than the measurement uncertainty.

  mono is True if polydispersity should be set to none on all parameters.

sasmodels.direct_model.call_profile(model_info: ModelInfo, pars: ParameterSet = None) → Tuple[np.ndarray, np.ndarray, Tuple[str, str]]

  Returns the profile x, y, (xlabel, ylabel) representing the model.

sasmodels.direct_model.get_mesh(model_info: ModelInfo, values: Dict[str, float], dim: str = '1d', mono: bool = False) → List[Tuple[float, np.ndarray, np.ndarray]]

  Retrieve the dispersity mesh described by the parameter set.

  Returns a list of (value, dispersity, weights) with one tuple for each parameter in the model call parameters. Inactive parameters return the default value with a weight of 1.0.

sasmodels.direct_model.main() → None

  Program to evaluate a particular model at a set of q values.

sasmodels.direct_model.test_reparameterize() → None

  Check simple reparameterized models will load and build

sasmodels.direct_model.test_simple_interface()