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)[source]

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)[source]
sas.sascalc.pr.calc.int_pr(pars, d_max, nslice)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

Bases: object

The explorer class

class sas.sascalc.pr.distance_explorer.Results[source]

Bases: object

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

sas.sascalc.pr.invertor module

Module to perform P(r) inversion. The module contains the Invertor class.

FIXME: The way the Invertor interacts with its C component should be cleaned up

class sas.sascalc.pr.invertor.Invertor[source]

Bases: sas.sascalc.pr.p_invertor.Pinvertor

Invertor class to perform P(r) inversion

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[j][i] = (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[j+Npts][i] = (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.

Methods inherited from Cinvertor:

  • get_peaks(pars): returns the number of P(r) peaks
  • oscillations(pars): returns the oscillation parameters for the output P(r)
  • get_positive(pars): returns the fraction of P(r) that is above zero
  • get_pos_err(pars): returns the fraction of P(r) that is 1-sigma above zero
background = 0
chi2 = 0
clone()[source]

Return a clone of this instance

cov = None
elapsed = 0
estimate_alpha(nfunc)[source]

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)[source]

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
from_file(path)[source]

Load the state of the Invertor from a file, to be able to generate P(r) from a set of parameters.

Parameters:path – path of the file to load
info = {}
invert(nfunc=10, nr=20)[source]

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

invert_optimize(nfunc=10, nr=20)[source]

Slower version of the P(r) inversion that uses scipy.optimize.leastsq.

This probably produce more reliable results, but is much slower. The minimization function is set to sum_i[ (I_obs(q_i) - I_theo(q_i))/err**2 ] + alpha * reg_term, where the reg_term is given by Svergun: it is the integral of the square of the first derivative of P(r), d(P(r))/dr, integrated over the full range of r.

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(out, q)[source]

Function to call to evaluate the scattering intensity

Parameters:args – c-parameters, and q
Returns:I(q)
lstsq(nfunc=5, nr=20)[source]

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.

nfunc = 10
out = None
pr_err(c, c_cov, r)[source]

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)

pr_fit(nfunc=5)[source]

This is a direct fit to a given P(r). It assumes that the y data is set to some P(r) distribution that we are trying to reproduce with a set of base functions.

This method is provided as a test.

suggested_alpha = 0
to_file(path, npts=100)[source]

Save the state to a file that will be readable by SliceView.

Parameters:
  • path – path of the file to write
  • npts – number of P(r) points to be written
sas.sascalc.pr.invertor.help()[source]

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)[source]

Bases: object

compare_err()[source]
get0_out()[source]
is_odd(n)[source]
ls_osc()[source]
median_osc()[source]
num_terms(isquit_func=None)[source]
sort_osc()[source]
sas.sascalc.pr.num_term.load(path)[source]

sas.sascalc.pr.p_invertor module

Python implementation of the P(r) inversion Pinvertor is the base class for the Invertor class and provides the underlying computations.

class sas.sascalc.pr.p_invertor.Pinvertor[source]

Bases: object

accept_q(q)[source]

Check whether a q-value is within acceptable limits.

Returns:1 if accepted, 0 if rejected.
basefunc_ft(d_max, n, q)[source]

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)[source]
err = array([], dtype=float64)
get_alpha()[source]

Gets the alpha parameter.

Returns:alpha.
get_dmax()[source]

Gets the maximum distance.

Returns:d_max.
get_err(data)[source]

Function to get the err data.

Parameters:data – Array of doubles to place err into.
Returns:npoints - number of entries found
get_est_bck()[source]

Gets background flag.

Returns:est_bck.
get_iq_smeared(pars, q)[source]

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_nerr()[source]

Gets the number of error points.

Returns:nerr.
get_nx()[source]

Gets the number of x points.

Returns:npoints.
get_ny()[source]

Gets the number of y points.

Returns:ny.
get_peaks(pars)[source]

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)[source]

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)[source]

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)[source]

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))

get_qmax()[source]

Gets the maximum q.

Returns:q_max.
get_qmin()[source]

Gets the minimum q.

Returns:q_min.
get_slit_height()[source]

Gets the slit height.

Returns:slit_height.
get_slit_width()[source]

Gets the slit width.

Returns:slit_width.
get_x(data)[source]

Function to get the x data.

Parameters:data – Array to place x into
Returns:npoints - Number of entries found.
get_y(data)[source]

Function to get the y data.

Parameters:data – Array of doubles to place y into.
Returns:npoints - Number of entries found.
iq(pars, q)[source]

Function to call to evaluate the scattering intensity.

Parameters:
  • pars – c-parameters
  • q – q, scalar or vector.
Returns:

I(q)

iq0(pars)[source]

Returns the value of I(q=0).

Parameters:pars – c-parameters.
Returns:I(q=0)
is_valid()[source]

Check the validity of the stored data.

Returns:Returns the number of points if it’s all good, -1 otherwise.
nerr = 0
npoints = 0
ny = 0
oscillations(pars)[source]

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)[source]

Function to call to evaluate P(r).

Parameters:
  • pars – c-parameters.
  • r – r-value to evaluate P(r) at.
Returns:

P(r)

pr_residuals(pars)[source]

Function to call to evaluate the residuals for P(r) minimization (for testing purposes).

Parameters:pars – input parameters.
Returns:residuals - list of residuals.
residuals(pars)[source]

Function to call to evaluate the residuals for P(r) inversion.

Parameters:pars – input parameters.
Returns:residuals - list of residuals.
rg(pars)[source]

Returns the value of the radius of gyration Rg.

Parameters:pars – c-parameters.
Returns:Rg.
set_alpha(alpha)[source]

Sets the alpha parameter.

Parameters:alpha – float to set alpha to.
Returns:alpha.
set_dmax(d_max)[source]

Sets the maximum distance.

Parameters:d_max – float to set d_max to.
Returns:d_max
set_err(data)[source]

Function to set the err data.

Parameters:data – Array of doubles to set err to.
Returns:nerr - Number of entries found.
set_est_bck(est_bck)[source]

Sets background flag.

Parameters:est_bck – int to set est_bck to.
Returns:est_bck.
set_qmax(max_q)[source]

Sets the maximum q.

Parameters:max_q – float to set q_max to.
Returns:q_max.
set_qmin(min_q)[source]

Sets the minimum q.

Parameters:min_q – float to set q_min to.
Returns:q_min.
set_slit_height(slit_height)[source]

Sets the slit height in units of q [A-1].

Parameters:slit_height – float to set slit-height to.
Returns:slit_height.
set_slit_width(slit_width)[source]

Sets the slit width in units of q [A-1].

Parameters:slit_width – float to set slit_width to.
Returns:slit_width.
set_x(data)[source]

Function to set the x data.

Parameters:data – Array of doubles to set x to.
Returns:npoints - Number of entries found, the size of x.
set_y(data)[source]

Function to set the y data.

Parameters:data – Array of doubles to set y to.
Returns:ny - Number of entries found.
slit_height = 0.0
slit_width = 0.0
x = array([], dtype=float64)
y = array([], dtype=float64)

Module contents

P(r) inversion for SAS