# Home

## 9. Data layout and plotting routines

## `sasmodels.data`

SAS data representations.

Plotting functions for data sets:

> `plot_data()` plots the data file.

> `plot_theory()` plots a calculated result from the model.

Wrappers for the sasview data loader and data manipulations:

> `load_data()` loads a sasview data file.

> `set_beam_stop()` masks the beam stop from the data.

> `set_half()` selects the right or left half of the data, which can be useful for shear measurements which have not been properly corrected for path length and reflections.

> `set_top()` cuts the top part off the data.

Empty data sets for evaluating models without data:

> `empty_data1D()` creates an empty dataset, which is useful for plotting a theory function before the data is measured.

> `empty_data2D()` creates an empty 2D dataset.

Note that the empty datasets use a minimal representation of the SasView objects so that models can be run without SasView on the path. You could also use these for your own data loader.

class sasmodels.data.Data1D(x: ndarray | None = None, y: ndarray | None = None, dx: ndarray | None = None, dy: ndarray | None = None)

  Bases: `object`

  1D data object.

  Note that this definition matches the attributes from sasview, with some generic 1D data vectors and some SAS specific definitions. Some refactoring to allow consistent naming conventions between 1D, 2D and SESANS data would be helpful.

  Attributes

  x, dx: \(q\) vector and gaussian resolution

  y, dy: \(I(q)\) vector and measurement uncertainty

  mask: values to include in plotting/analysis

  dxl: slit widths for slit smeared data, with dx ignored

  qmin, qmax: range of \(q\) values in x

  filename: label for the data line

  _xaxis, _xunit: label and units for the x axis

  _yaxis, _yunit: label and units for the y axis

  xaxis(label: str, unit: str) → None

    set the x axis label and unit

  yaxis(label: str, unit: str) → None

    set the y axis label and unit

class sasmodels.data.Data2D(x: ndarray | None = None, y: ndarray | None = None, z: ndarray | None = None, dx: ndarray | None = None, dy: ndarray | None = None, dz: ndarray | None = None)

  Bases: `object`

  2D data object.

  Note that this definition matches the attributes from sasview. Some refactoring to allow consistent naming conventions between 1D, 2D and SESANS data would be helpful.

  Attributes

  qx_data, dqx_data: \(q_x\) matrix and gaussian resolution

  qy_data, dqy_data: \(q_y\) matrix and gaussian resolution

  data, err_data: \(I(q)\) matrix and measurement uncertainty

  mask: values to exclude from plotting/analysis

  qmin, qmax: range of \(q\) values in x

  filename: label for the data line

  _xaxis, _xunit: label and units for the x axis

  _yaxis, _yunit: label and units for the y axis

  _zaxis, _zunit: label and units for the y axis

  Q_unit, I_unit: units for Q and intensity

  x_bins, y_bins: grid steps in x and y directions

  xaxis(label: str, unit: str) → None

    set the x axis label and unit

  yaxis(label: str, unit: str) → None

    set the y axis label and unit

  zaxis(label: str, unit: str) → None

    set the y axis label and unit

class sasmodels.data.Detector(pixel_size: tuple[float, float] = (None, None), distance: float = None)

  Bases: `object`

  Detector attributes.

class sasmodels.data.Sample

  Bases: `object`

  Sample attributes.

class sasmodels.data.SesansData(**kw)

  Bases: `Data1D`

  SESANS data object.

  This is just `Data1D` with a wavelength parameter.

  x is spin echo length and y is polarization (P/P0).

  xaxis(label: str, unit: str) → None

    set the x axis label and unit

  yaxis(label: str, unit: str) → None

    set the y axis label and unit

  isSesans = True

class sasmodels.data.Source

  Bases: `object`

  Beam attributes.

class sasmodels.data.Vector(x: float = None, y: float = None, z: float | None = None)

  Bases: `object`

  3-space vector of x, y, z

sasmodels.data.demo() → None

  Load and plot a SAS dataset.

sasmodels.data.empty_data1D(q: ndarray, resolution: float = 0.0, L: float = 0.0, dL: float = 0.0) → Data1D

  Create empty 1D data using the given q as the x value.

  rms resolution \(\Delta q/q\) defaults to 0.0. Note that this is expressed as a fraction rather than a percentage.

  If wavelength L and rms wavelength divergence dL are given, then angle theta is infered from \(q = 4\pi/\lambda \sin(\theta)\), and the resolution term applies to rms \(\Delta \theta/\theta\) instead of \(\Delta q/q\). Resolution \(\Delta q\) is then calculated from wavelength and angular resolution.

sasmodels.data.empty_data2D(qx: ndarray, qy: ndarray | None = None, resolution: float = 0.0) → Data2D

  Create empty 2D data using the given mesh.

  If qy is missing, create a square mesh with qy=qx.

  resolution dq/q defaults to 0.0.

sasmodels.data.empty_sesans(z, wavelength=None, zacceptance=None)

sasmodels.data.load_data(filename: str, index: int = 0) → Data1D | Data2D | SesansData

  Load data using a sasview loader.

sasmodels.data.plot_data(data: Data1D | Data2D | SesansData, view: str = None, limits: tuple[float, float] | None = None) → None

  Plot data loaded by the sasview loader.

  data is a sasview data object, either 1D, 2D or SESANS.

  view is log, linear or normed.

  limits sets the intensity limits on the plot; if None then the limits are inferred from the data.

sasmodels.data.plot_theory(data: Data1D | Data2D | SesansData, theory: ndarray | None, resid: ndarray | None = None, view: str | None = None, use_data: bool = True, limits: tuple[float, float] | None = None, Iq_calc: ndarray | None = None) → None

  Plot theory calculation.

  data is needed to define the graph properties such as labels and units, and to define the data mask.

  theory is a matrix of the same shape as the data.

  view is log, linear or normed

  use_data is True if the data should be plotted as well as the theory.

  limits sets the intensity limits on the plot; if None then the limits are inferred from the data. If (-inf, inf) then use auto limits.

  Iq_calc is the raw theory values without resolution smearing

sasmodels.data.protect(func: Callable) → Callable

  Decorator to wrap calls in an exception trapper which prints the exception and continues. Keyboard interrupts are ignored.

sasmodels.data.set_beam_stop(data: Data1D | Data2D | SesansData, radius: float, outer: float | None = None) → None

  Add a beam stop of the given radius. If outer, make an annulus.

sasmodels.data.set_half(data: Data1D | Data2D | SesansData, half: str) → None

  Select half of the data, either “right” or “left”.

sasmodels.data.set_top(data: Data1D | Data2D | SesansData, cutoff: float) → None

  Chop the top off the data, above cutoff.