Fit Results API (pyrate_ta.results)#

Kinetic, Global & Target Fit Results (pyrate_ta.results.fits)#

Fit result objects and the linearised uncertainty estimate.

A result carries enough to reproduce itself: the model identity, the initial guesses, the bounds, the fixed/free mask, the delay and probe ranges actually fitted (clipping changes what the fit saw), the solver’s report and the statistic. A lifetime without its uncertainty and its fixed/free flag is not a result, so tau_err and is_fixed travel with every lifetime.

Uncertainties are 1-sigma from the linearised covariance at the optimum. They are conditional on the model being right: a three-component fit of two-component data can return small uncertainties on three meaningless lifetimes. Fixed parameters get nan, never a small number that might read as precision.

pyrate_ta.results.fits.covariance_from_jacobian(J, residuals, *, weighted, dof)[source]#

Linearised parameter covariance at the optimum.

Parameters:
  • J (array_like (N, n_free)) – Jacobian of the residual vector with respect to the free parameters, as scipy.optimize.least_squares returns it.

  • residuals (array_like) – The residual vector at the optimum (already weighted, if weights were used – that is what the optimiser saw).

  • weighted (bool) – With proper 1/sigma weights the residuals are dimensionless and the covariance is (J^T J)^-1. Unweighted, it is scaled by the residual variance SSR/dof, which assumes the model is correct and the noise uniform.

  • dof (int) – Degrees of freedom used for that scaling.

Returns:

(n_free, n_free), or None when the normal matrix is singular – in which case the parameters are not determined and no uncertainty should be invented for them.

Return type:

numpy.ndarray or None

class pyrate_ta.results.fits.KineticFit(taus, tau_err, is_fixed, S, R, model_type, t, probe=None, C=None, t0=0.0, t0_err=None, irf_fwhm=None, irf_err=None, S_abs=None, gs_scale=None, gs_source=None, a_max=None, statistic=None, report=None, weights=None, param_names=<factory>, initial_params=None, bounds=None, covariance=None, n_components=0, n_artifact=0, delay_range=None, probe_range=None, t_min=None, detector=0, source=None, sorted_by_lifetime=False, permutation=None, settings=<factory>, timestamp=<factory>)[source]#

Bases: object

Result of a multi-exponential fit to one or more traces.

Variables:
  • tau_err (taus,) – Lifetimes and their 1-sigma uncertainties, in the dataset’s time unit. A fixed lifetime has nan uncertainty.

  • is_fixed (numpy.ndarray of bool) – Per-lifetime fixed/free flag.

  • S (numpy.ndarray (Np, Nc)) – Component spectra (amplitudes for a single trace).

  • R (numpy.ndarray (Nd, Np)) – Residual matrix.

  • model_type (str) – "Parallel" / "Sequential" / "Target"; decides whether the spectra are DAS, EAS or SAS, and what PyMORGAN prints in the legend.

Parameters:
taus: ndarray#
tau_err: ndarray#
is_fixed: ndarray#
S: ndarray#
R: ndarray#
model_type: str#
t: ndarray#
probe: ndarray | None = None#
C: ndarray | None = None#
t0: float = 0.0#
t0_err: float | None = None#
irf_fwhm: float | None = None#
irf_err: float | None = None#
S_abs: ndarray | None = None#
gs_scale: float | None = None#
gs_source: str | None = None#
a_max: float | None = None#
statistic: Any = None#
report: Any = None#
weights: Any = None#
param_names: list[str]#
initial_params: ndarray | None = None#
bounds: tuple[ndarray, ndarray] | None = None#
covariance: ndarray | None = None#
n_components: int = 0#
n_artifact: int = 0#

Number of coherent-artefact columns appended to C (and to S). They are not kinetic components and carry no lifetime.

delay_range: tuple[float, float] | None = None#
probe_range: tuple[float, float] | None = None#
t_min: float | None = None#
detector: int = 0#
source: str | None = None#
sorted_by_lifetime: bool = False#
permutation: ndarray | None = None#
settings: dict#
timestamp: str#
property converged: bool#
property n_infinite: int#

Number of non-decaying (offset) components.

property spectra_kind: str#

DAS, EAS or SAS.

On the base class because the name is a property of the model family, not of which engine produced the result – a preview has to be able to label its own spectra too.

Type:

What this model’s amplitude spectra are called

property n_kinetic: int#

Columns of C/S that are kinetic components, artefact aside.

property artifact_spectra#

Amplitude spectra of the coherent-artefact columns, or None.

Worth looking at: a fitted artefact that does not look like a derivative of the IRF, or that carries amplitude far from time zero, is a sign the artefact is absorbing real kinetics.

as_species_args()[source]#

The plain-array tuple plot_species_spectra expects.

Returns (Sfit, Taus, TauErr, isFixTau, modelType). Keeping this adapter on the PyRATE-TA side is what lets PyMORGAN render a PyRATE-TA fit without ever importing pyrate_ta.

Only the kinetic columns are handed over: an artefact column has no lifetime, and passing it would label it with someone else’s.

species_labels()[source]#

Names of the species/compartments for DAS/EAS/SAS plots and legends.

Return type:

list[str]

summary()[source]#

One-line-per-lifetime summary, with the statistic and convergence.

How the lifetimes are quoted follows the [plots] settings show_uncertainties and round_uncertainties, so the summary agrees with the legends rather than reporting a different precision.

Return type:

str

log_summary()[source]#

Send summary() to the pyrate logger at info level.

Return type:

None

format_report(*, dataset=None, timestamp=None, bleach_result=None)[source]#

Format a detailed terminal-style fit report string.

Parameters:
  • dataset (Any)

  • timestamp (str | None)

  • bleach_result (Any)

Return type:

str

export_spectra(path=None, *, dataset=None, recover_gsb=False, S_abs=None, bleach_result=None, gs_spectrum=None, em_spectrum=None, include_artifact=False, metadata=False, timestamp=None, write_summary=False, directory=None, fmt='%.12g')[source]#

Export DAS/EAS/SAS spectra to a single-header CSV file.

Parameters:
  • dataset (Any)

  • recover_gsb (bool)

  • S_abs (Any)

  • bleach_result (Any)

  • gs_spectrum (Any)

  • em_spectrum (Any)

  • include_artifact (bool)

  • metadata (bool)

  • timestamp (str | None)

  • write_summary (bool)

  • fmt (str)

export_concentrations(path=None, *, dataset=None, normalise=False, include_irf=True, include_artifact=False, metadata=False, timestamp=None, write_summary=False, directory=None, fmt='%.12g')[source]#

Export concentration profiles C(t) to a single-header CSV file.

Parameters:
  • dataset (Any)

  • normalise (bool)

  • include_irf (bool)

  • include_artifact (bool)

  • metadata (bool)

  • timestamp (str | None)

  • write_summary (bool)

  • fmt (str)

property fitted_surface: ndarray#

Reconstructed 2D fitted surface C @ S.T of shape (Nd, Np).

property data_surface: ndarray#

Fitted-window 2D data surface C @ S.T + R of shape (Nd, Np).

export_surface(path=None, *, kind='fit', dataset=None, surface_format='csv', include_artifact=True, weighted_residuals=False, metadata=False, timestamp=None, write_summary=False, directory=None, fmt='%.12g')[source]#

Export one 2D surface (data, fit, or residuals) as CSV/TSV or PDAT.

Parameters:
  • kind (str)

  • dataset (Any)

  • surface_format (str)

  • include_artifact (bool)

  • weighted_residuals (bool)

  • metadata (bool)

  • timestamp (str | None)

  • write_summary (bool)

  • fmt (str)

export_surfaces(directory=None, *, dataset=None, surface_format='csv', as_zip=False, zip_path=None, keep_files=False, stem_override=None, include_artifact=True, weighted_residuals=False, metadata=False, timestamp=None, bleach_result=None, fmt='%.12g')[source]#

Export all 3 surfaces (data, fit, residuals) + .txt summary.

Parameters:
  • dataset (Any)

  • surface_format (str)

  • as_zip (bool)

  • keep_files (bool)

  • stem_override (str | None)

  • include_artifact (bool)

  • weighted_residuals (bool)

  • metadata (bool)

  • timestamp (str | None)

  • bleach_result (Any)

  • fmt (str)

export_summary(path=None, *, dataset=None, timestamp=None, bleach_result=None, directory=None)[source]#

Export the companion human-readable .txt fit report.

Parameters:
  • dataset (Any)

  • timestamp (str | None)

  • bleach_result (Any)

export_results(directory=None, *, dataset=None, recover_gsb=False, S_abs=None, bleach_result=None, gs_spectrum=None, em_spectrum=None, normalise_concentrations=False, include_irf=True, include_artifact=False, include_surfaces=True, surface_format='csv', include_artifact_in_surfaces=True, weighted_residuals=False, as_zip=False, zip_path=None, keep_files=False, stem_override=None, metadata=False, timestamp=None, fmt='%.12g')[source]#

Export spectra, concentrations, 2D surfaces, and summary TXT (or single .zip).

Parameters:
  • dataset (Any)

  • recover_gsb (bool)

  • S_abs (Any)

  • bleach_result (Any)

  • gs_spectrum (Any)

  • em_spectrum (Any)

  • normalise_concentrations (bool)

  • include_irf (bool)

  • include_artifact (bool)

  • include_surfaces (bool)

  • surface_format (str)

  • include_artifact_in_surfaces (bool)

  • weighted_residuals (bool)

  • as_zip (bool)

  • keep_files (bool)

  • stem_override (str | None)

  • metadata (bool)

  • timestamp (str | None)

  • fmt (str)

class pyrate_ta.results.fits.GlobalFit(taus, tau_err, is_fixed, S, R, model_type, t, probe=None, C=None, t0=0.0, t0_err=None, irf_fwhm=None, irf_err=None, S_abs=None, gs_scale=None, gs_source=None, a_max=None, statistic=None, report=None, weights=None, param_names=<factory>, initial_params=None, bounds=None, covariance=None, n_components=0, n_artifact=0, delay_range=None, probe_range=None, t_min=None, detector=0, source=None, sorted_by_lifetime=False, permutation=None, settings=<factory>, timestamp=<factory>)[source]#

Bases: KineticFit

Global-analysis result: shared lifetimes, one spectrum per component.

The spectra are DAS for a parallel model and EAS for a sequential one – which of the two is recorded in model_type, because the distinction is what the legend and the interpretation hang on (see KineticFit.spectra_kind).

Parameters:
class pyrate_ta.results.fits.TargetFit(taus, tau_err, is_fixed, S, R, model_type, t, probe=None, C=None, t0=0.0, t0_err=None, irf_fwhm=None, irf_err=None, S_abs=None, gs_scale=None, gs_source=None, a_max=None, statistic=None, report=None, weights=None, param_names=<factory>, initial_params=None, bounds=None, covariance=None, n_components=0, n_artifact=0, delay_range=None, probe_range=None, t_min=None, detector=0, source=None, sorted_by_lifetime=False, permutation=None, settings=<factory>, timestamp=<factory>, scheme_key=None, scheme_label=None, scheme_text=None, K=None, eigenvalues=None)[source]#

Bases: GlobalFit

Target-analysis result, adding the scheme and its rate matrix.

The rates of a branched scheme are generally not identifiable from a single dataset even when the fit is perfect: the data fix the column space of C, and several rate sets can span it. eigenvalues is what the data actually determine, and it is stored alongside the rates for that reason.

Parameters:
scheme_key: str | None = None#
scheme_label: str | None = None#
scheme_text: str | None = None#

The scheme in its own notation, so a saved fit reopens as editable text.

K: ndarray | None = None#
eigenvalues: ndarray | None = None#
summary()[source]#

One-line-per-lifetime summary, with the statistic and convergence.

How the lifetimes are quoted follows the [plots] settings show_uncertainties and round_uncertainties, so the summary agrees with the legends rather than reporting a different precision.

Return type:

str

LDA Result (pyrate_ta.results.lda_result)#

Result object for Lifetime Density Analysis (LDA).

class pyrate_ta.results.lda_result.LDAResult(tau_grid, S_map, alpha_opt, penalty='d2', alpha_method='lcurve', l_curve_points=None, alphas=None, gcv_scores=None, residuals=None, t=None, probe=None, irf_fwhm=None, t0=0.0, svd_components=0, non_negative=False, n_bootstraps=0, bootstrap_std=None, peaks=<factory>, statistic=None, delay_range=None, probe_range=None, units=<factory>, settings=<factory>)[source]#

Bases: object

Result of Lifetime Density Analysis (LDA).

Variables:
  • tau_grid (numpy.ndarray (M,)) – Logarithmically spaced grid of lifetimes in dataset time units.

  • S_map (numpy.ndarray (Np, M)) – 2D matrix of lifetime amplitudes across probe pixels.

  • alpha_opt (float) – Chosen regularisation parameter alpha.

  • penalty (str) – Penalty type ("ridge" / "d1" / "d2").

  • alpha_method (str) – Method used to pick alpha ("lcurve" / "gcv" / "manual").

  • l_curve_points (numpy.ndarray (K, 2) or None) – (log_residual, log_norm) points across the alpha scan for L-curve plot.

  • alphas (numpy.ndarray (K,) or None) – Scanned alpha values.

  • gcv_scores (numpy.ndarray (K,) or None) – Scanned GCV score values.

  • residuals (numpy.ndarray (Nd, Np) or None) – Residual matrix D - C S^T.

  • t (numpy.ndarray (Nd,)) – Delays fitted.

  • probe (numpy.ndarray (Np,) or None) – Probe wavelengths/wavenumbers.

  • irf_fwhm (float or None) – IRF FWHM used in kernel construction.

  • t0 (float) – Time zero offset used in kernel construction.

Parameters:
tau_grid: ndarray#
S_map: ndarray#
alpha_opt: float#
penalty: str = 'd2'#
alpha_method: str = 'lcurve'#
l_curve_points: ndarray | None = None#
alphas: ndarray | None = None#
gcv_scores: ndarray | None = None#
residuals: ndarray | None = None#
t: ndarray | None = None#
probe: ndarray | None = None#
irf_fwhm: float | None = None#
t0: float = 0.0#
svd_components: int = 0#
non_negative: bool = False#
n_bootstraps: int = 0#
bootstrap_std: ndarray | None = None#
peaks: list[dict]#
statistic: Any = None#
delay_range: tuple[float, float] | None = None#
probe_range: tuple[float, float] | None = None#
units: dict#
settings: dict#
property n_taus: int#
integrated_dynamics(metric='abs')[source]#

Integrated dynamics profile across the lifetime grid.

Parameters:

metric ({"abs", "dynamical_content", "sqrt_sq"}) –

  • "abs": absolute sum \(A(\tau) = \int |S(\lambda, \tau)| d\lambda\).

  • "dynamical_content" / "sqrt_sq": \(D(\tau) = \sqrt{\int S(\lambda, \tau)^2 d\lambda}\).

Return type:

numpy.ndarray, shape (M,)

find_peaks(metric='abs', prominence=0.05)[source]#

Auto-detect major lifetime peak centroids.

Parameters:
  • metric ({"abs", "dynamical_content"}) – Which profile metric to detect peaks on.

  • prominence (float) – Peak prominence as a fraction of peak maximum.

Return type:

list of dict with keys "tau", "amplitude", "index".

citations()[source]#

Return literature citations for the specific algorithms used in this LDA fit.

Return type:

list[str]

format_citations()[source]#

Format citations as a clean readable bulleted list.

Return type:

str

summary()[source]#
Return type:

str

to_pdat(path)[source]#

Export the fitted 2D lifetime density map S(tau, lambda) as a PyMORGAN PDAT file.

Parameters:

path (str or Path) – Destination filepath.

Returns:

Path to the saved PDAT file.

Return type:

Path