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

__call__(dmin=None, dmax=None, npts=10)

Compute the outputs as a function of D_max.

Parameters:
  • dmin – minimum value for D_max

  • dmax – maximum value for D_max

  • npts – number of points for D_max

__dict__ = mappingproxy({'__module__': 'sas.sascalc.pr.distance_explorer', '__doc__': '\n    The explorer class\n    ', '__init__': <function DistExplorer.__init__>, '__call__': <function DistExplorer.__call__>, '__dict__': <attribute '__dict__' of 'DistExplorer' objects>, '__weakref__': <attribute '__weakref__' of 'DistExplorer' objects>, '__annotations__': {}})
__doc__ = '\n    The explorer class\n    '
__init__(pr_state)

Initialization.

Parameters:

pr_state – sas.sascalc.pr.invertor.Invertor object

__module__ = 'sas.sascalc.pr.distance_explorer'
__weakref__

list of weak references to the object

class sas.sascalc.pr.distance_explorer.Results

Bases: object

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

__dict__ = mappingproxy({'__module__': 'sas.sascalc.pr.distance_explorer', '__doc__': '\n    Class to hold the inversion output parameters\n    as a function of D_max\n    ', '__init__': <function Results.__init__>, '__dict__': <attribute '__dict__' of 'Results' objects>, '__weakref__': <attribute '__weakref__' of 'Results' objects>, '__annotations__': {}})
__doc__ = '\n    Class to hold the inversion output parameters\n    as a function of D_max\n    '
__init__()

Initialization. Create empty arrays and dictionary of labels.

__module__ = 'sas.sascalc.pr.distance_explorer'
__weakref__

list of weak references to the object

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

Bases: 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

__doc__ = '\n    Invertor class to perform P(r) inversion\n\n    The problem is solved by posing the problem as  Ax = b,\n    where x is the set of coefficients we are looking for.\n\n    Npts is the number of points.\n\n    In the following i refers to the ith base function coefficient.\n    The matrix has its entries j in its first Npts rows set to ::\n\n        A[j][i] = (Fourier transformed base function for point j)\n\n    We then choose a number of r-points, n_r, to evaluate the second\n    derivative of P(r) at. This is used as our regularization term.\n    For a vector r of length n_r, the following n_r rows are set to ::\n\n        A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2,\n        evaluated at r[j])\n\n    The vector b has its first Npts entries set to ::\n\n        b[j] = (I(q) observed for point j)\n\n    The following n_r entries are set to zero.\n\n    The result is found by using scipy.linalg.basic.lstsq to invert\n    the matrix and find the coefficients x.\n\n    Methods inherited from Cinvertor:\n\n    * ``get_peaks(pars)``: returns the number of P(r) peaks\n    * ``oscillations(pars)``: returns the oscillation parameters for the output P(r)\n    * ``get_positive(pars)``: returns the fraction of P(r) that is above zero\n    * ``get_pos_err(pars)``: returns the fraction of P(r) that is 1-sigma above zero\n    '
__getattr__(name)

Return the value of an attribute

__init__()
__module__ = 'sas.sascalc.pr.invertor'
__reduce_ex__(proto)

Overwrite the __reduce_ex__

__setattr__(name, value)

Set the value of an attribute. Access the parent class methods for x, y, err, d_max, q_min, q_max and alpha

__setstate__(state)

restore the state of invertor for pickle

_accept_q(q)

Check q-value against user-defined range

background = 0
chi2 = 0
clone()

Return a clone of this instance

cov = None
elapsed = 0
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

from_file(path)

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)

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)

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)

Function to call to evaluate the scattering intensity

Parameters:

args – c-parameters, and q

Returns:

I(q)

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.

nfunc = 10
out = None
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)

pr_fit(nfunc=5)

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)

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

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

__dict__ = mappingproxy({'__module__': 'sas.sascalc.pr.num_term', '__doc__': '\n    ', '__init__': <function NTermEstimator.__init__>, 'is_odd': <function NTermEstimator.is_odd>, 'sort_osc': <function NTermEstimator.sort_osc>, 'median_osc': <function NTermEstimator.median_osc>, 'get0_out': <function NTermEstimator.get0_out>, 'ls_osc': <function NTermEstimator.ls_osc>, 'compare_err': <function NTermEstimator.compare_err>, 'num_terms': <function NTermEstimator.num_terms>, '__dict__': <attribute '__dict__' of 'NTermEstimator' objects>, '__weakref__': <attribute '__weakref__' of 'NTermEstimator' objects>, '__annotations__': {}})
__doc__ = '\n    '
__init__(invertor)
__module__ = 'sas.sascalc.pr.num_term'
__weakref__

list of weak references to the 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)

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

Bases: object

__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'sas.sascalc.pr.p_invertor', 'x': array([], dtype=float64), 'y': array([], dtype=float64), 'err': array([], dtype=float64), 'npoints': 0, 'ny': 0, 'nerr': 0, 'slit_height': 0.0, 'slit_width': 0.0, '__init__': <function Pinvertor.__init__>, 'residuals': <function Pinvertor.residuals>, 'pr_residuals': <function Pinvertor.pr_residuals>, 'set_x': <function Pinvertor.set_x>, 'get_x': <function Pinvertor.get_x>, 'set_y': <function Pinvertor.set_y>, 'get_y': <function Pinvertor.get_y>, 'set_err': <function Pinvertor.set_err>, 'get_err': <function Pinvertor.get_err>, 'set_dmax': <function Pinvertor.set_dmax>, 'get_dmax': <function Pinvertor.get_dmax>, 'set_qmin': <function Pinvertor.set_qmin>, 'get_qmin': <function Pinvertor.get_qmin>, 'set_qmax': <function Pinvertor.set_qmax>, 'get_qmax': <function Pinvertor.get_qmax>, 'set_alpha': <function Pinvertor.set_alpha>, 'get_alpha': <function Pinvertor.get_alpha>, 'set_slit_width': <function Pinvertor.set_slit_width>, 'get_slit_width': <function Pinvertor.get_slit_width>, 'set_slit_height': <function Pinvertor.set_slit_height>, 'get_slit_height': <function Pinvertor.get_slit_height>, 'set_est_bck': <function Pinvertor.set_est_bck>, 'get_est_bck': <function Pinvertor.get_est_bck>, 'get_nx': <function Pinvertor.get_nx>, 'get_ny': <function Pinvertor.get_ny>, 'get_nerr': <function Pinvertor.get_nerr>, 'iq': <function Pinvertor.iq>, 'get_iq_smeared': <function Pinvertor.get_iq_smeared>, 'pr': <function Pinvertor.pr>, 'get_pr_err': <function Pinvertor.get_pr_err>, 'is_valid': <function Pinvertor.is_valid>, 'basefunc_ft': <function Pinvertor.basefunc_ft>, 'oscillations': <function Pinvertor.oscillations>, 'get_peaks': <function Pinvertor.get_peaks>, 'get_positive': <function Pinvertor.get_positive>, 'get_pos_err': <function Pinvertor.get_pos_err>, 'rg': <function Pinvertor.rg>, 'iq0': <function Pinvertor.iq0>, 'accept_q': <function Pinvertor.accept_q>, 'check_for_zero': <function Pinvertor.check_for_zero>, '_get_matrix': <function Pinvertor._get_matrix>, '_get_invcov_matrix': <function Pinvertor._get_invcov_matrix>, '_get_reg_size': <function Pinvertor._get_reg_size>, '__dict__': <attribute '__dict__' of 'Pinvertor' objects>, '__weakref__': <attribute '__weakref__' of 'Pinvertor' objects>, '__doc__': None, '__annotations__': {}})
__doc__ = None
__init__()
__module__ = 'sas.sascalc.pr.p_invertor'
__weakref__

list of weak references to the object

_get_invcov_matrix(nfunc, nr, a_obj)

Compute the inverse covariance matrix, defined as inv_cov = a_transposed x a.

Parameters:
  • nfunc – number of base functions.

  • nr – number of r-points used when evaluating reg term.

  • a – A array to fill.

  • inv_cov – inverse covariance array to be filled.

Returns:

0

_get_matrix(nfunc, nr)

Returns A matrix and b vector for least square problem.

Parameters:
  • nfunc – number of base functions.

  • nr – number of r-points used when evaluating reg term.

  • a – A array to fill.

  • b – b vector to fill.

Returns:

0

_get_reg_size(nfunc, nr, a_obj)

Computes sum_sig and sum_reg of input array given.

param nfunc:

number of base functions.

param nr:

number of r-points used when evaluating reg term.

param a_obj:

Array to compute sum_sig and sum_reg of.

Returns:

Tuple of (sum_sig, sum_reg)

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

Gets the alpha parameter.

Returns:

alpha.

get_dmax()

Gets the maximum distance.

Returns:

d_max.

get_err(data)

Function to get the err data.

Parameters:

data – Array of doubles to place err into.

Returns:

npoints - number of entries found

get_est_bck()

Gets background flag.

Returns:

est_bck.

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

Gets the number of error points.

Returns:

nerr.

get_nx()

Gets the number of x points.

Returns:

npoints.

get_ny()

Gets the number of y points.

Returns:

ny.

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

get_qmax()

Gets the maximum q.

Returns:

q_max.

get_qmin()

Gets the minimum q.

Returns:

q_min.

get_slit_height()

Gets the slit height.

Returns:

slit_height.

get_slit_width()

Gets the slit width.

Returns:

slit_width.

get_x(data)

Function to get the x data.

Parameters:

data – Array to place x into

Returns:

npoints - Number of entries found.

get_y(data)

Function to get the y data.

Parameters:

data – Array of doubles to place y into.

Returns:

npoints - Number of entries found.

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

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)

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_residuals(pars)

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)

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

Parameters:

pars – input parameters.

Returns:

residuals - list of residuals.

rg(pars)

Returns the value of the radius of gyration Rg.

Parameters:

pars – c-parameters.

Returns:

Rg.

set_alpha(alpha)

Sets the alpha parameter.

Parameters:

alpha – float to set alpha to.

Returns:

alpha.

set_dmax(d_max)

Sets the maximum distance.

Parameters:

d_max – float to set d_max to.

Returns:

d_max

set_err(data)

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)

Sets background flag.

Parameters:

est_bck – int to set est_bck to.

Returns:

est_bck.

set_qmax(max_q)

Sets the maximum q.

Parameters:

max_q – float to set q_max to.

Returns:

q_max.

set_qmin(min_q)

Sets the minimum q.

Parameters:

min_q – float to set q_min to.

Returns:

q_min.

set_slit_height(slit_height)

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)

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)

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)

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