tikreg Module¶
Functions¶
-
L_curve(K, S, lambda_array, L=None)¶ Generate Tikhonov L-curve
Parameters: - K (numpy.ndarray) – Kernel Matrix
- S (numpy.ndarray) – Experimental DEER trace
- lambda (numpy.ndarray) – Array of Regularization parameters
- L (None, numpy.ndarray) – Tikhonov regularization operator, uses 2nd derivative if argument is None
Returns: tuple containing:
rho_array (numpy.ndarray): Residual Norm
eta_array (numpy.ndarray): Solution Norm
Return type:
-
add_noise(S, sigma)¶ Add noise to array
Parameters: - S (numpy.ndarray) – Array to add noise to
- sigma (float) – Standard deviation of noise
Returns: Array with noise added
Return type: numpy.ndarray
-
autophase(S)¶ Optimize phase of complex data by maximizing the sum of imaginary over sum of real
\[ \begin{align}\begin{aligned}\phi = \arctan \left( \frac{\sum_i^N \Im(s_i) }{ \sum_i^N \Re(s_i) } \right)\\S_{\phi} = S e^{-i \phi}\end{aligned}\end{align} \]Parameters: S (numpy.ndarray) – Complex data Returns: Automatically phased complex data Return type: numpy.ndarray
-
background(t, tau, A, B, d=3.0)¶ DEER Background function
\[A + B e^{- t^{d/3}/\tau}\]Parameters: Returns: Background signal
Return type: numpy.ndarray
-
background_dist(t)¶ Calculate the distance above which P(r) should be zero in background fit.
Parameters: t (numpy.ndarray) – Time axes Returns: Distance value for background fit Return type: r (float)
-
background_x0(t, data)¶ Guess initial parameters for background function
Parameters: - data (numpy.ndarray) – Array of data
- t (numpy.ndarray) – Array of axes
Returns: List of parameters for fit initial guess
Return type:
-
deer_trace(t, r, method='fresnel', angles=1000)¶ Calculate the DEER trace corresponding to a given time axes and distance value
Method Description ‘fresnel’ Fresnel Integral ‘brute force’ Brute Force Method Parameters: Returns: DEER trace
Return type: numpy.ndarray
Example:
import numpy as np from matplotlib.pylab import * r = 4e-9 t = np.r[0.:10e-6:1000j] trace = deer_trace(t,r) figure() plot(t,trace) show()
-
exp_background(t, data, background_function=<function background>, t_min=0.0, x0=None)¶ Fit DEER data to background function
Parameters: Returns: Fit of data
Return type: numpy.ndarray
-
gaussian(r, sigma, mu, Normalize=False)¶ Return Gaussian Distribution from given distance array, standard deviation, and mean distance
If Normalize = True:
\[\frac{1}{\sqrt{2 \pi {\sigma}^2}} e^{-{(r-\mu)}^2/(2\sigma^2)}\]If Normalize = False:
\[e^{-{(r-\mu)}^2/(2\sigma^2)}\]Parameters: Returns: Gaussian distribution
Return type: numpy.ndarray
-
gaussians(r, x)¶ Return sum of Gaussian distributions from given distance array and list of lists defining amplitude, standard deviation, and mean distance for each Gaussian
\[\sum_{i = 1}^{N} A_i e^{-{(r-\mu_i)}^2/(2\sigma_i^2)}\]Parameters: - r (numpy.ndarray) – Numpy array of distance values
- x (list) – list of lists. Each gaussian is definied by a list of 3 parameters. The parameters are ordered: A - amplitude, sigma - standard deviation, mu - center of distribution.
Returns: Gaussian distribution
Return type: numpy.ndarray
-
kernel(t, r, method='fresnel', angles=5000)¶ Return the Kernel Matrix.
\[ \begin{align}\begin{aligned}K(r,t) = \int_{0}^{\pi/2} \cos(\theta) \cos[(3 \cos(\theta)^2 - 1)\omega_{ee} t] d\theta\\\omega_{ee} = \frac{\gamma_e^2\hbar}{r^3}\end{aligned}\end{align} \]Method Description ‘fresnel’ Fresnel Integral ‘brute force’ Brute Force Method Parameters: Returns: Numpy array of kernel. The first dimension is the time dimension. The second dimension is the distance dimension.
Return type: numpy.ndarray
Note
The distance array (r) must have all values greater than zero to generate a proper kernel.
Warning
The number of angles must be carefully selected to ensure the Kernel matrix properly averages the angles for short distances.
Example:
t = np.r_[-0.1e-6:10e-6:1000j] r = np.r_[1.5e-9:10e-9:100j] K = kernel(t,r,angles = 2000)
-
load_kernel(filename='default_kernel.csv', directory='kernels')¶ Import Kernel Matrix
-
maximum_entropy(K, S, lambda_)¶ Maximum Entropy method for determining P(r)
\[\Phi_{ME}[P] = \|K P(r) - S\|^2 + \lambda^2 \times \int [P(r) \ln \frac{P(r)}{P_0(r)} + \frac{P_0(r)}{e}] dr \Rightarrow \min\]Parameters: - K (numpy.ndarray) – Kernel Matrix
- S (numpy.ndarray) – Data array
- lambda (float) – Regularization parameter
Returns: Distance distribution minimized by maximum entropy method.
Return type: numpy.ndarray
-
model_free(K, S, lambda_=1.0, L=None)¶ Model Free P(r) with non-negative constraints
\[\Phi_{MF}[P(r)] = \|K P(r) - S\|^2 + \lambda^2 \| LP(r) \|^2 \Rightarrow \min\]Parameters: Returns: Distance distribution from model free fit
Return type: numpy.ndarray
-
model_gaussian(K, S, r, x0=None)¶ Gaussian based fit for distance distribution
Parameters: - K (numpy.ndarray) – Kernel Matrix
- S (numpy.ndarray) – Data array
- r (numpy.ndarray) – Array of distance values
- x0 (None, List) – Initial guess. If None, the initial guess is automatically chosen based on Tikhonov regularization P(r)
Returns: tuple containing:
P_gauss (numpy.ndarray): distance distribution
x_fit (dict): Dictionary of fitting parameters
Return type:
-
operator(n, L)¶ Return operator for Regularization
Operator (L) Description ‘Identity’ Identity Matrix ‘1st Derivative’ 1st Derivative Matrix ‘2nd Derivative’ 2nd Derivative Matrix Parameters: Returns: Regularization operator as numpy array
Return type: numpy.ndarray
-
save_kernel(k, filename, directory='kernels')¶ Save Kernel Matrix
Parameters:
-
svd(K, S, cutoff=None)¶ Performs SVD on Kernel Matrix, singular values above cutoff index are set to zero, then calculates distance distribution with cutoff applied pseudo inverse.
\[ \begin{align}\begin{aligned}S = K P\\S = U \Sigma V^T P\\P = V \Sigma^{-1} U^T S\end{aligned}\end{align} \]Parameters: - K (numpy.ndarray) – Kernel
- S (numpy.ndarray) – Data array
- cutoff (int) – Number of singular values to include. None correponds to including all singular values (no cutoff applied).
Returns: Distance distribution array
Return type: P (numpy.ndarray)
-
tikhonov(K, S, lambda_=1.0, L=None)¶ Perform Tikhonov Regularization
\[P_\lambda = {(K^TK + \lambda^2 L^TL)}^{-1} K^TS\]Parameters: Returns: Distance distribution from Tikhonov regularization
Return type: numpy.ndarray
-
tikhonov_background(t, r, K, data, background_function=<function background>, r_background=None, lambda_=1.0, L='Identity', x0=None)¶ Fit DEER data to background function by forcing P(r) to be zero
Parameters: - t (numpy.ndarray) – Array of time axis values
- r (numpy.ndarray) – Array of distance values for Kernel
- K (numpy.ndarray) – Kernel matrix
- data (numpy.ndarray) – Array of data values
- background_function (func) – Background function
- r_background (float) – Distance above which P(r) is optimized to zero
- lambda (float) – Regularization parameter
- L (str, numpy.ndarray) – Regularization operator, by default Identity for background optimization
- x0 (list) – Initial guess for background fit parameters
Returns: Background fit of data
Return type: numpy.ndarray
-
truncate(t, S, t_truncate)¶ Truncate time axes and data at given time
Parameters: - t (numpy.ndarray) – Time axes
- S (numpy.ndarray) – Data Axes
- t_truncate (float) – time to trunctate data after
Returns: tuple containing
numpy.ndarray: Truncated time axes
numpy.ndarray: Truncated data axes
Return type:
-
zero_time(t, S, method='polyfit', **kwargs)¶ Shift DEER data to correct zero time offset
Method Description ‘max’ Set zero time to maximum of data ‘polyfit’ polynomial fit about time zero Parameters for ‘polyfit’ Method:
Argument Description Default ‘time_width’ Time width about zero for polynomial fit (in seconds) 100e-9 ‘deg’ degree of polynomial fit 3 Parameters: - t (numpy.ndarray) – Time axes
- S (numpy.ndarray) – Data array
- method (str) – Method to use for zero time correction
Returns: tuple containing
numpy.ndarray: Shifted time axes
numpy.ndarray: Data array
Return type: