# sas.sascalc.pr package

## Submodules

## sas.sascalc.pr.calc module

Converted invertor.c’s methods. Implements low level inversion functionality, with conditional Numba njit compilation.

sas.sascalc.pr.calc.dprdr(pars, d_max, r)

  dP(r)/dr calculated from the expansion.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - r – r-value.

  Returns:

    dP(r)/dr.

sas.sascalc.pr.calc.dprdr_calc(i, d_max, r)

sas.sascalc.pr.calc.int_pr(pars, d_max, nslice)

  Integral of P(r).

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    Integral of P(r).

sas.sascalc.pr.calc.int_pr_square(pars, d_max, nslice)

  Regularization term calculated from the expansion.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    Regularization term calculated from the expansion.

sas.sascalc.pr.calc.iq(pars, d_max, q)

  Scattering intensity calculated from the expansion.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - q – q (vector).

  Returns:

    Scattering intensity from the expansion across all q.

sas.sascalc.pr.calc.iq_smeared(p, q, d_max, height, width, npts)

  Scattering intensity calculated from the expansion, slit-smeared.

  Parameters:

    - p – c-parameters.

    - q – q (vector).

    - height – slit_height.

    - width – slit_width.

    - npts – npts.

  Returns:

    Scattering intensity from the expansion slit-smeared across all q.

sas.sascalc.pr.calc.njit(*args, **kw)

sas.sascalc.pr.calc.npeaks(pars, d_max, nslice)

  Get the number of P(r) peaks.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    Number of P(r) peaks.

sas.sascalc.pr.calc.ortho(d_max, n, r)

  Orthogonal Functions: B(r) = 2r sin(pi*nr/d)

  Parameters:

    - d_max – d_max.

    - n –

  Returns:

    B(r).

sas.sascalc.pr.calc.ortho_derived(d_max, n, r)

  First derivative in of the orthogonal function dB(r)/dr.

  Parameters:

    - d_max – d_max.

    - n –

  Returns:

    First derivative in dB(r)/dr.

sas.sascalc.pr.calc.ortho_transformed(q, d_max, n)

  Fourier transform of the nth orthogonal function.

  Parameters:

    - q – q (vector).

    - d_max – d_max.

    - n –

  Returns:

    Fourier transform of nth orthogonal function across all q.

sas.sascalc.pr.calc.ortho_transformed_smeared(q, d_max, n, height, width, npts)

  Slit-smeared Fourier transform of the nth orthogonal function. Smearing follows Lake, Acta Cryst. (1967) 23, 191.

  Parameters:

    - q – q (vector).

    - d_max – d_max.

    - n –

    - height – slit_height.

    - width – slit_width.

    - npts – npts.

  Returns:

    Slit-smeared Fourier transform of nth orthogonal function across all q.

sas.sascalc.pr.calc.positive_errors(pars, err, d_max, nslice)

  Get the fraction of the integral of P(r) over the whole range of r that is at least one sigma above 0.

  Parameters:

    - pars – c-parameters.

    - err – error terms.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    The fraction of the integral of P(r) over the whole range of r that is at least one sigma above 0.

sas.sascalc.pr.calc.positive_integral(pars, d_max, nslice)

  Get the fraction of the integral of P(r) over the whole range of r that is above 0. A valid P(r) is defined as being positive for all r.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    The fraction of the integral of P(r) over the whole range of r that is above 0.

sas.sascalc.pr.calc.pr(pars, d_max, r)

  P(r) calculated from the expansion

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - r – r-value to evaluate P(r).

  Returns:

    P(r).

sas.sascalc.pr.calc.pr_err(pars, err, d_max, r)

  P(r) calculated from the expansion, with errors.

  Parameters:

    - pars – c-parameters.

    - err – err.

    - r – r-value.

  Returns:

    [P(r), dP(r)].

sas.sascalc.pr.calc.reg_term(pars, d_max, nslice)

  Regularization term calculated from the expansion.

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    Regularization term calculated from the expansion.

sas.sascalc.pr.calc.rg(pars, d_max, nslice)

  R_g radius of gyration calculation

  R_g**2 = integral[r**2 * p(r) dr] / (2.0 * integral[p(r) dr])

  Parameters:

    - pars – c-parameters.

    - d_max – d_max.

    - nslice – nslice.

  Returns:

    R_g radius of gyration.

## sas.sascalc.pr.distance_explorer module

Module to explore the P(r) inversion results for a range of D_max value. User picks a number of points and a range of distances, then get a series of outputs as a function of D_max over that range.

class sas.sascalc.pr.distance_explorer.DistExplorer(pr_state)

  Bases: `object`

  The explorer class

class sas.sascalc.pr.distance_explorer.Results

  Bases: `object`

  Class to hold the inversion output parameters as a function of D_max

## sas.sascalc.pr.invertor module

class sas.sascalc.pr.invertor.Invertor(logic: InversionLogic)

  Bases: `object`

  accept_q(q)

    Check whether a q-value is within acceptable limits.

    Returns:

      1 if accepted, 0 if rejected.

  basefunc_ft(d_max, n, q)

    Returns the value of the nth Fourier transformed base function.

    Parameters:

      - d_max – d_max.

      - n –

      - q – q, scalar or vector.

    Returns:

      nth Fourier transformed base function, evaluated at q.

  check_for_zero(x)
  clone() → Invertor
  property err: ndarray[tuple[Any, ...], dtype[float64]]
  estimate_alpha(nfunc)

    Returns a reasonable guess for the regularization constant alpha

    Parameters:

      nfunc – number of terms to use in the expansion.

    Returns:

      alpha, message, elapsed

    where alpha is the estimate for alpha, message is a message for the user, elapsed is the computation time

  estimate_numterms(isquit_func=None)

    Returns a reasonable guess for the number of terms

    Parameters:

      isquit_func – reference to thread function to call to check whether the computation needs to be stopped.

    Returns:

      number of terms, alpha, message

  get_iq_smeared(pars, q)

    Function to call to evaluate the scattering intensity. The scattering intensity is slit-smeared.

    Parameters:

      - pars – c-parameters

      - q – q, scalar or vector.

    Returns:

      I(q), either scalar or vector depending on q.

  get_peaks(pars)

    Returns the number of peaks in the output P(r) distribution for the given set of coefficients.

    Parameters:

      pars – c-parameters.

    Returns:

      number of P(r) peaks.

  get_pos_err(pars, pars_err)

    Returns the fraction of P(r) that is 1 standard deviation above zero over the full range of r for the given set of coefficients.

    Parameters:

      - pars – c-parameters.

      - pars_err – pars_err.

    Returns:

      fraction of P(r) that is positive.

  get_positive(pars)

    Returns the fraction of P(r) that is positive over the full range of r for the given set of coefficients.

    Parameters:

      pars – c-parameters.

    Returns:

      fraction of P(r) that is positive.

  get_pr_err(pars, pars_err, r)

    Function to call to evaluate P(r) with errors.

    Parameters:

      - pars – c-parameters.

      - pars_err – pars_err.

      - r – r-value.

    Returns:

      (P(r), dP(r))

  init_default_values()
  invert(nfunc=10, nr=20)

    Perform inversion to P(r)

    The problem is solved by posing the problem as Ax = b, where x is the set of coefficients we are looking for.

    Npts is the number of points.

    In the following i refers to the ith base function coefficient. The matrix has its entries j in its first Npts rows set to

        A[i][j] = (Fourier transformed base function for point j)

    We then choose a number of r-points, n_r, to evaluate the second derivative of P(r) at. This is used as our regularization term. For a vector r of length n_r, the following n_r rows are set to

        A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j])

    The vector b has its first Npts entries set to

        b[j] = (I(q) observed for point j)

    The following n_r entries are set to zero.

    The result is found by using scipy.linalg.basic.lstsq to invert the matrix and find the coefficients x.

    Parameters:

      - nfunc – number of base functions to use.

      - nr – number of r points to evaluate the 2nd derivative at for the reg. term.

    Returns:

      c_out, c_cov - the coefficients with covariance matrix

  iq(pars, q)

    Function to call to evaluate the scattering intensity.

    Parameters:

      - pars – c-parameters

      - q – q, scalar or vector.

    Returns:

      I(q)

  iq0(pars)

    Returns the value of I(q=0).

    Parameters:

      pars – c-parameters.

    Returns:

      I(q=0)

  is_valid() → bool
  lstsq(nfunc=5, nr=20)

    The problem is solved by posing the problem as Ax = b, where x is the set of coefficients we are looking for.

    Npts is the number of points.

    In the following i refers to the ith base function coefficient. The matrix has its entries j in its first Npts rows set to

        A[i][j] = (Fourier transformed base function for point j)

    We then choose a number of r-points, n_r, to evaluate the second derivative of P(r) at. This is used as our regularization term. For a vector r of length n_r, the following n_r rows are set to

        A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2,
        evaluated at r[j])

    The vector b has its first Npts entries set to

        b[j] = (I(q) observed for point j)

    The following n_r entries are set to zero.

    The result is found by using scipy.linalg.basic.lstsq to invert the matrix and find the coefficients x.

    Parameters:

      - nfunc – number of base functions to use.

      - nr – number of r points to evaluate the 2nd derivative at for the reg. term.

    If the result does not allow us to compute the covariance matrix, a matrix filled with zeros will be returned.

  property nerr: int
  property npoints: int
  property ny: int
  oscillations(pars)

    Returns the value of the oscillation figure of merit for the given set of coefficients. For a sphere, the oscillation figure of merit is 1.1.

    Parameters:

      pars – c-parameters.

    Returns:

      oscillation figure of merit.

  pr(pars, r)

    Function to call to evaluate P(r).

    Parameters:

      - pars – c-parameters.

      - r – r-value to evaluate P(r) at.

    Returns:

      P(r)

  pr_err(c, c_cov, r)

    Returns the value of P(r) for a given r, and base function coefficients, with error.

    Parameters:

      - c – base function coefficients

      - c_cov – covariance matrice of the base function coefficients

      - r – r-value to evaluate P(r) at

    Returns:

      P(r)

  rg(pars)

    Returns the value of the radius of gyration Rg.

    Parameters:

      pars – c-parameters.

    Returns:

      Rg.

  property x: ndarray[tuple[Any, ...], dtype[float64]]
  property y: ndarray[tuple[Any, ...], dtype[float64]]

sas.sascalc.pr.invertor.help()

  Provide general online help text Future work: extend this function to allow topic selection

## sas.sascalc.pr.num_term module

class sas.sascalc.pr.num_term.NTermEstimator(invertor)

  Bases: `object`

  compare_err()
  get0_out()
  is_odd(n)
  ls_osc()
  median_osc()
  num_terms(isquit_func=None)
  sort_osc()

sas.sascalc.pr.num_term.load(path)

## Module contents

P(r) inversion for SAS