trtoolbox package¶
Submodules¶
trtoolbox.expfit module¶
-
class
trtoolbox.expfit.Results(pre, tcs, time)[source]¶ Bases:
objectObject containing fit results
-
data¶ Time trace subjected to fitting.
Type: np.array
-
time¶ Time array
Type: np.array
-
tcs¶ Time constants
Type: np.array
-
pre¶ Prefactors
Type: np.array
-
var¶ Variance of tcs
Type: np.array
-
traces¶ Individual expontential fit traces
Type: np.array
-
fit¶ Fitted time trace
Type: np.array
-
-
trtoolbox.expfit.calculate_error(res, data)[source]¶ Returns the standard error of the optimized parameters.
Parameters: - res (scipy.optimize.OptimizeResult) – Results object obtained with least squares.
- data (np.array) – Data matrix.
Returns: perr – Standard error of the parameters.
Return type: np.array
-
trtoolbox.expfit.create_tr(pre, tcs, time)[source]¶ Creates fitted time trace
Parameters: - pre (np.array) – Prefactors
- tcs (np.array) – Time constants
- time (np.array) – Time array
Returns: tr – Fitted time trace
Return type: np.array
-
trtoolbox.expfit.dofit(data, time, init)[source]¶ Do exponential fitting
Parameters: - data (np.array) – Time trace subjected to fitting
- time (np.array) – Time array
- init (np.array) – Initial guesses. Prefactors first column, time constants second
Returns: res – Results object
Return type: self.Results()
trtoolbox.globalanalysis module¶
-
class
trtoolbox.globalanalysis.RateConstants(ks)[source]¶ Bases:
objectContainer for rate contants related stuff
-
ks¶ Rate constants
Type: np.array
-
tcs¶ Time constants
Type: np.array
-
nb_exps¶ shape of the ks-matrix
Type: tuple
-
kmatrix¶ K-matrix used for generating differential equations.
Type: np.array
-
style¶ Which style of k-matrix was used. ‘dec’: parallel decaying processes ‘seq’: sequential model ‘back’: sequential model with back reactions ‘custom’: custom k-matrix
Type: str
-
alphas¶ Defines starting population ratio of ‘custom’ is choosen
Type: np.array
-
ks_err¶ Standard error of the fit (rate constants).
Type: np.array
-
tcs_err¶ Standard error of the fit (time constants).
Type: np.array
-
-
class
trtoolbox.globalanalysis.Results[source]¶ Bases:
trtoolbox.pclasses.DataObject containing fit results.
-
offset¶ True if an offset was used.
Type: bool
-
spectral_offset¶ optional if offset was chosen
Type: np.array
-
method¶ Chosen method (default is ‘svd’).
Type: str
-
rate_constants¶ Object containing everything on the rate constants
Type: RateConstants
-
xas¶ Decay/Evolution/Species associated spectra.
Type: np.array
-
profile¶ Concentration profile determined by ks
Type: np.array
-
artefact¶ If True, the first two species are merged
Type: bool
-
estimates¶ Contribution of das to the dataset for each datapoint.
Type: np.array
-
fitdata¶ Fitted dataset.
Type: np.array
-
fittraces¶ optional if method=’svd’ was chosen. Fitted abstract time traces.
Type: np.array
-
svdtraces¶ SVD abstract time traces.
Type: np.array
-
r2¶ R^2 of fit.
Type: float
-
plot_fitdata(interpolate=False, step=0.5)[source]¶ Plots fitted data.
Parameters: - interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_fitdata_3d(interpolate=False, step=0.5)[source]¶ 3D plot fitted data.
Parameters: - interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_results()[source]¶ Plots the concentration profile, DAS, fitted data and fitted abstract time traces if method=’svd’ was chosen.
-
save_to_files(path, comment='')[source]¶ Saving results to .dat files.
Parameters: - path (str) – Path for saving.
- comment (str) – Personal comment.
-
tcs¶
-
-
trtoolbox.globalanalysis.calc_r2(data, res)[source]¶ Returns R^2 in percent.
Parameters: - data (np.array) – Data matrix.
- res (scipy.optimize.OptimizeResult) – Results object obtained with least squares.
Returns: r2 – R^2.
Return type: float
-
trtoolbox.globalanalysis.calculate_error(res, data)[source]¶ Returns the standard error of the optimized parameters.
Parameters: - res (scipy.optimize.OptimizeResult) – Results object obtained with least squares.
- data (np.array) – Data matrix.
Returns: perr – Standard error of the parameters.
Return type: np.array
-
trtoolbox.globalanalysis.calculate_estimate(das, data)[source]¶ Computes contributions of DAS in the raw data.
Parameters: - das (np.array) – DAS
- data (np.array) – Data matrix.
Returns: est – Contributions of the individual DAS.
Return type: np.array
-
trtoolbox.globalanalysis.calculate_fitdata(rate_constants, time, data)[source]¶ Computes the final fitted dataset.
Parameters: - rate_constants (RateConstants) – RateConstants object.
- time (np.array) – Time array.
- data (np.array) – Data matrix.
Returns: fitdata – Fitted dataset.
Return type: np.array
-
trtoolbox.globalanalysis.convert_tcs(arr)[source]¶ Converts time to rate constants and vice versa. Necessary due to zeros.
Parameters: arr (np.array) – Returns: Return type: np.array
-
trtoolbox.globalanalysis.create_profile(time, rate_constants)[source]¶ Computes a concentration profile according to the model() function.
Parameters: - time (np.array) – Time array.
- rate_constants (RateConstants) – RateConstants object.
Returns: profile – Concentration profile matrix.
Return type: np.array
-
trtoolbox.globalanalysis.create_tr_expfit(rate_constants, pre, time)[source]¶ Function returning exponential time traces for a given set of parameters. Uses expfit module.
Parameters: - rate_constants (RateConstants) – RateConstants object.
- pre (np.array) – Exponential pre-factors.
- time (np.array) – Time array.
Returns: profile – Concentration profile matrix.
Return type: np.array
-
trtoolbox.globalanalysis.create_tr_odeint(rate_constants, pre, time)[source]¶ - Function returning exponential time traces for a given set of parameters.
- Uses odeint function.
Parameters: - rate_constants (RateConstants) – RateConstants object.
- pre (np.array) – Exponential pre-factors.
- time (np.array) – Time array.
Returns: profile – Concentration profile matrix.
Return type: np.array
-
trtoolbox.globalanalysis.create_xas(profile, data)[source]¶ Obtains decay associated spectra.
Parameters: - profile (np.array) – Concentration profile matrix
- data (np.array) – Data matrix.
Returns: xas – Decay/Evolution/Species associated spectra
Return type: np.array
-
trtoolbox.globalanalysis.doglobalanalysis(data, time, wn, tcs, method='svd_expfit', svds=5, offset=False, offindex=-1, style='seq', kmatrix=None, alphas=None, artefact=False, silent=False)[source]¶ Wrapper for global fit routine.
Parameters: - time (np.array) – Time array.
- wn (np.array) – Frequency array.
- data (np.array) – Raw data matrix.
- tcs (np.array) – Initial time constants
- method (np.array) – Method for global fitting. raw for fitting the residuals of fitted data and input data, est for fitting the residuals between concentration profile and contributions of DAS, svd_odeint for fitting the SVD time traces with odeint function. *svd_expfit for fitting the SVD time traces with expfit module (default).
- svds (int) – Number of SVD components to be fitted. Default: 5.
- offset (boolean) – Considering the last spectrum to be an offset. Default: False.
- offindex (int) – Index of spectral offset.
- style (str) – Which style of K-matrix to use. ‘dec’: parallel decaying processes ‘seq’: sequential model ‘back’: sequential model with back reactions. tcs-matrix should have forward time constants in the first column and backward in the second. ‘custom’: custom k-matrix
- kmatrix (np.array) – K-matrix. Providing an 3D K-matrix is interpreted as parallel reaction pathways. This also useful if branching occurs due to heterogeneity. Starting populations can be set with the alpha attribute. For more info please see the documentation.
- alphas (np.array) – Sets starting population of the first species.
- artefact (bool) – If True, the first two species are merged
- silent (bool) – Supresses print output
Returns: gf_res – Results objects.
Return type: globalanalysis.results
-
trtoolbox.globalanalysis.is_square(mat)[source]¶ Checks if a matrix is square.
Parameters: mat (np.array) – Matrix Returns: Return type: bool
-
trtoolbox.globalanalysis.model(s, time, rate_constants)[source]¶ - Creates an array of differential equations according to
- (kmatrix * ks).dot(s) with ks as rate constants and s as species concentration.
Parameters: - s (np.array) – Starting concentrations for each species.
- time (np.array) – Time array.
- rate_constants (RateConstants) – RateConstants object.
Returns: arr – Array containing the differential equations.
Return type: np.array
-
trtoolbox.globalanalysis.opt_func_est(ks, rate_constants, time, data)[source]¶ - Optimization function for residuals of concentration profile
- and estimated contributions of DAS
Parameters: - ks (np.array) – Rate constants
- rate_constants (RateConstants) – RateConstants object.
- time (np.array) – Time array.
- data (np.array) – Data matrix.
Returns: R – Flattened array of residuals.
Return type: np.array
-
trtoolbox.globalanalysis.opt_func_raw(ks, rate_constants, time, data)[source]¶ Optimization function for residuals of fitted data - input data.
Parameters: - ks (np.array) – Rate constants
- rate_constants (RateConstants) – RateConstants object.
- time (np.array) – Time array.
- data (np.array) – Data matrix.
Returns: R – Flattened array of residuals.
Return type: np.array
-
trtoolbox.globalanalysis.opt_func_svd(pars, rate_constants, time, svdtraces, method)[source]¶ - Optimization function for residuals of SVD
- abstract time traces - fitted traces.
Parameters: - pars (np.array) – Flattened parameter array
- rate_constants (RateConstants) – RateConstants object.
- time (np.array) – Time array.
- svdtraces (np.array) – SVD traces
- method (basestring) – Chosen method
Returns: R – Flattened array of residuals.
Return type: np.array
trtoolbox.lda module¶
-
class
trtoolbox.lda.Results[source]¶ Bases:
trtoolbox.pclasses.DataObject containing fit results.
-
type¶ Results object type.
Type: str
-
taus¶ Time constants used for constructing D-matrix.
Type: np.array
-
dmatrix¶ D-matrix.
Type: np.array
-
alphas¶ Used alpha values for computation (tik method).
Type: np.array
-
lmatrix¶ L-matrix (tik method).
Type: np.array
-
lcurve¶ L-curve.
Type: np.array
-
k¶ Used SVD Components (tsvd method).
Type: int
-
method¶ Used method (tik or tsvd).
Type: string
-
x_k¶ Resulting exponential pre-factors.
Type: np.array
-
fitdata¶ Constructed data (dmatrix.dot(x_k))
Type: np.array
-
get_alpha(alpha=-1, index_alpha=-1)[source]¶ Gets alpha value and index.
Parameters: - alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
Returns: - alpha (float) – Alpha value.
- index_alpha (int) – Index of alpha value.
-
get_xk(alpha=-1, index_alpha=-1)[source]¶ Gets selected LDA map.
Parameters: - alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
Returns: - x_k (np.array) – LDA map.
- title (str) – Title for figure.
-
plot_fitdata(alpha=-1, index_alpha=-1, interpolate=False, step=0.5)[source]¶ Plots a nice looking heatmap of fitted data.
Parameters: - alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
- interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_fitdata_3d(alpha=-1, index_alpha=-1, interpolate=False, step=0.5)[source]¶ Plots a 3D surface of fitted data.
Parameters: - alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
- interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_ldamap(alpha=-1, index_alpha=-1)[source]¶ Plots a nice looking contourmap.
Parameters: - alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
-
-
trtoolbox.lda.calc_lcurve(data, dmatrix, lmatrix, x_ks)[source]¶ Calculates L-curve.
Parameters: - data (np.array) – Data matrix.
- dmatrix (np.array) – D-matrix.
- lmatrix (np.array) – L-matrix.
- x_ks (np.array) – LDA maps.
Returns: lcurve – First column is resdiual norm. Second column is smoothed norm.
Return type: np.array
-
trtoolbox.lda.dolda(data, time, wn, tlimits=[], tnum=100, alimits=[0.1, 5], anum=100, method='tik', seqmodel=False, k=5, prompt=False)[source]¶ Wrapper for doing a LDA.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
- tlimits (list) – Limits for time constants.
- tnum (int) – Number of time constants.
- alimits (list) – Limits for alpha values.
- anum (int) – Number if alpha values.
- method (str) – Chosen method for LDA. Either ‘tik’ or ‘tsvd’.
- seqmodel (boolean) – True for constructing the D-matrix assuming a sequential model.
- k (int) – Just used for ‘tsvd’. Specifies the position of truncation.
- prompt (boolean) – True for user prompts.
Returns: res – Results object.
Return type: lda.results
-
trtoolbox.lda.gen_alphas(a1, a2, n, space='log')[source]¶ Generates logarihmic spaced alpha values. Adds [1e-5, 1e-4, 1e-3, 1e-2] and [10, 40, 70, 100] for a better visualization of the L-curve.
Parameters: - a1 (float) – Bottom limit.
- a2 (float) – Upper limit.
- n (int) – Number of alpha values.
- space (str) – log for logarithmic and lin for linear spaced
Returns: alphas – Generated alpha values.
Return type: np.array
-
trtoolbox.lda.gen_dmatrix(time, taus, seqmodel=False)[source]¶ Generates D-matrix.
Parameters: - time (np.array) – Time array.
- taus (np.array) – Time constants array
- seqmodel (boolean) – Sets a sequential model.
Returns: dmatrix – D-matrix
Return type: np.array
-
trtoolbox.lda.gen_lmatrix(dmatrix)[source]¶ Generates L-matrix
Parameters: dmatrix (np.array) – D-matrix Returns: lamtrix – L-matrix Return type: np.array
-
trtoolbox.lda.gen_taus(t1, t2, n)[source]¶ Generates logarithmic spaced time constants.
Parameters: - t1 (float) – Bottom limit.
- t2 (float) – Upper limit.
- n (int) – Number of time constants.
Returns: taus – Generated time constants.
Return type: np.array
-
trtoolbox.lda.inversesvd(dmatrix, k=-1)[source]¶ Returns the inverse of matrix computed via SVD.
Parameters: - dmatrix (np.array) – Matrix to be inversed
- k (int) – Point of truncation. If -1 then all singular values are used.
Returns: v – Inverse of input matrix.
Return type: np.array
-
trtoolbox.lda.tik(data, dmatrix, alpha)[source]¶ - Function for Tikhonov regularization:
min_x ||Dx - A|| + alpha*||Lx|| D-matrix contains exponential profiles, x are prefactors/amplitudes, A is the dataset, alpha is the regularization factor and L is the identity matrix.
Details can be found in Dorlhiac, Gabriel F. et al. “PyLDM-An open source package for lifetime density analysis of time-resolved spectroscopic data.” PLoS computational biology 13.5 (2017)
Parameters: - data (np.array) – Data matrix to be analyzed
- dmatrix (np.array) – D-matrix
- alpha (float) – Regularization factor
Returns: x_k – Expontential prefactors/amplitudes.
Return type: np.array
-
trtoolbox.lda.tik_lstsq(data, dmatrix, alpha)[source]¶ - Different implementation of the tik function.
- Uses the ordinary lstsq solver of numpy.
Parameters: - data (np.array) – Data matrix to be analyzed.
- dmatrix (np.array) – D-matrix.
- alpha (float) – Regularization factor.
Returns: res – Expontential prefactors/amplitudes.
Return type: np.array
-
trtoolbox.lda.tiks(data, dmatrix, alphas)[source]¶ - Wrapper for computing LDA for various alpha values.
- Parallelization makes execution actually slower. I suspect that the svd numpy method already optimizes CPU usage.
Parameters: - data (np.array) – Data matrix to be analyzed.
- dmatrix (np.array) – D-matrix.
- alphas (np.array) – Array of regularization factors.
Returns: x_k – 3D matrix of expontential prefactors/amplitudes.
Return type: np.array
-
trtoolbox.lda.tsvd(data, dmatrix, k)[source]¶ - Truncated SVD for LDA. Similar to Tikhonov regularization
but here we have a clear cut-off after a specified singular value.
Details can be found in Hansen PC. The truncated SVD as a method for regularization. Bit. 1987
Parameters: - data (np.array) – Data matrix to be analyzed.
- dmatrix (np.array) – D-matrix.
- k (int) – Cut-off for singular values.
Returns: x_k – Expontential prefactors/amplitudes.
Return type: np.array
trtoolbox.plothelper module¶
-
class
trtoolbox.plothelper.MidpointNormalize(vmin=None, vmax=None, midpoint=None, clip=False)[source]¶ Bases:
matplotlib.colors.NormalizeClass for setting 0 as midpoint in the colormap.
-
midpoint¶ Midpoint of colormap
Type: float
-
-
class
trtoolbox.plothelper.PlotHelper[source]¶ Bases:
objectObject for interactive plotting. This class ensures that all the matplotlib objects are kept referenced which ensures proper function of the sliders.
-
fig_traces¶ Type: matplotlib.figure.Figure
-
ax_traces¶ Type: matplotlib.axes.Axes
-
l1_traces¶ Type: matplotlib.lines.Line2D
-
l2_traces¶ Type: matplotlib.lines.Line2D
-
axfreq¶ Type: matplotlib.axes.Axes
-
sfreq¶ Type: matplotlib.widgets.Slider
-
fig_spectra¶ Type: matplotlib.figure.Figure
-
ax_spectra¶ Type: matplotlib.axes.Axes
-
l1_spectra¶ Type: matplotlib.lines.Line2D
-
l2_spectra¶ Type: matplotlib.lines.Line2D
-
axtime¶ Type: matplotlib.axes.Axes
-
stime¶ Type: matplotlib.widgets.Slider
-
fig_lda¶ Type: matplotlib.figure.Figure()
-
axs_lda¶ Type: np.array
-
map_lda¶ Type: matplotlib.contour.QuadContourSet
-
ldadata¶ Type: matplotlib.collections.QuadMesh
-
l1_lda¶ Type: matplotlib.lines.Line2D
-
axalpha¶ Type: matplotlib.axes.Axes
-
salpha¶ Type: matplotlib.widgets.Slider
-
axcolor¶ Type: str
-
static
append_ldamap(res, index_alpha=-1)[source]¶ - Appends NaN values in order to expand the taus array
- to match the time array span.
Parameters: - res (mylda.Results) – Contains the results to be plotted.
- index_alpha (int) – Index of selected alpha value.
Returns: - x_k (np.array) – LDA map of selected alpha value.
- taus (np.array) – Extended taus array.
-
static
plot_contourmap(data, time, wn, title='data', newfig=True)[source]¶ Plots a nice looking contourmap.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
- title (np.array) – Title of plot. Default data.
- newfig (boolean) – Setting to False prevents the creation of a new figure.
-
plot_heatmap(data, time, wn, title='data', newfig=True, interpolate=False, step=0.5)[source]¶ Plots a nice looking heatmap.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
- title (np.array) – Title of plot. Default data.
- newfig (boolean) – Setting to False prevents the creation of a new figure.
- interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_ldaresults(res)[source]¶ Plots interactive resaults of LDA.
Parameters: res (mylda.Results) – Contains the results to be plotted.
-
plot_solutionvector(res, alpha=-1, index_alpha=-1)[source]¶ Plots interactive solution vector.
Parameters: - res (mylda.Results) – Contains the data to be plotted.
- alpha (float) – Plot for the closest alpha as specified.
- index_alpha (int) – Plot for specified alpha at index.
-
plot_spectra(res, alpha=-1, index_alpha=-1, rev=True)[source]¶ Plots interactive spectra.
Parameters: - res (mysvd.Results) – Contains the data to be plotted.
- alpha (float) – Alpha value.
- index_alpha (int) – Index of alpha value.
- rev (boolean) – Reverses the x-axis.
-
plot_surface(data, time, wn, title='data', interpolate=False, step=0.5)[source]¶ Plots a nice looking heatmap.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
- title (np.array) – Title of plot. Default data.
- interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
trtoolbox.svd module¶
-
class
trtoolbox.svd.Results[source]¶ Bases:
trtoolbox.pclasses.DataObject containing fit results.
-
type¶ Results object type.
Type: str
-
u¶ U matrix. Represents abstract spectra
Type: np.array
-
s¶ Singular values.
Type: np.array
-
vt¶ Transposed V matrix. Represents abstract time traces.
Type: np.array
-
n¶ Number of singular components used for data reconstruction.
Type: int
-
svddata¶ Reconstructed data.
Type: np.array
-
plot_data(newfig=True, interpolate=False, step=0.5)[source]¶ Plots a nice looking heatmap of the raw data.
Parameters: - newfig (boolean) – True for own figure.
- interpolate (boolean) – True for interpolation
- step (float) – Step size for frequency interpolation.
-
plot_results()[source]¶ Plots heatmaps of original and SVD data, interactive time traces and spectra.
-
plot_svddata(newfig=False, interpolate=False, step=0.5)[source]¶ Plots a nice looking heatmap of the reconstructed data.
-
-
trtoolbox.svd.dosvd(data, time, wn, n=-1)[source]¶ Wrapper for inspecting SVD components with reconstruction.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
- n (int, list or np.array) – Number of used SVD components. If a list or array is provided, non pythonic way of numbering is used. Meaning first component equals 1.
Returns: res – Results object.
Return type: svd.results
-
trtoolbox.svd.reconstruct(data, n)[source]¶ Reconstructs data with n singular components.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- n (int, list or np.array) – Number of used SVD components. If a list or array is provided, non pythonic way of numbering is used. Meaning first component equals 1.
Returns: res – Results object.
Return type: mysvd.results
-
trtoolbox.svd.show_svs(data, time, wn)[source]¶ Plots singular values and variance explained.
Parameters: - data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important.
- time (np.array) – Time array.
- wn (np.array) – Frequency array.
-
trtoolbox.svd.wrapper_svd(data)[source]¶ Simple wrapper for the scipy.linalg.svd() function.
Parameters: data (np.array) – Data matrix subjected to SVD. Assuming m x n with m as frequency and n as time. But it is actually not important. Returns: - u (np.array) – U matrix. Represents abstract spectra
- s (np.array) – Singular values.
- vt (np.array) – Transposed V matrix. Represents abstract time traces.