{ "cells": [ { "cell_type": "markdown", "id": "cd94ab79-9146-4514-8e0c-7e38f18372ff", "metadata": {}, "source": [ "# Fluorescence decays" ] }, { "cell_type": "markdown", "id": "5da52053-a912-4cbc-b21c-5a1ff2804b01", "metadata": {}, "source": [ "## Analysis" ] }, { "cell_type": "markdown", "id": "a4be661a-64b8-490e-b39c-f5ffd2637aad", "metadata": {}, "source": [ "### Phasor\n", "\n", "Changing the data representation from the classical time delay\n", "histogram to the phasor representation provides a global view\n", "of the fluorescence decay at each pixel of an image. In the phasor\n", "representation reveal the presence of different in a a fluorescence\n", "decay. The analysis of time-resolve fluorescence data in the phasor space is done\n", "by observing clustering of populations in the phasor plot rather\n", "than by fitting the fluorescence decay using exponentials. The\n", "analysis is instantaneous since is not based on calculations or nonlinear\n", "fitting. Thus, the phasor approach can simplify the way data\n", "are analyzed and makes FLIM technique accessible to the nonexpert\n", "in spectroscopy and data analysis :cite:`DIGMAN2008L14`.\n", "\n", "The phasor transformation can use data collected in either the time or the\n", "frequency domain. In the time-correlated single-photon counting (TCSPC)\n", "technique, the histogram of the photon arrival time at each pixel of the image\n", "is transformed to Fourier space, and the phasor coordinates are calculated\n", "using the following relations:\n", "\n", "$$\n", " g_{i,j}(\\omega) = \\int_{0}^{T} I(t) \\cdot \\cos(n\\omega t) dt / \\int_{0}^{T} I(t) dt\n", "$$\n", "\n", "$$\n", " s_{i,j}(\\omega) = \\int_{0}^{T} I(t) \\cdot \\sin(n\\omega t) dt / \\int_{0}^{T} I(t) dt\n", "$$\n", "\n", "in which gi,j(ω) and si,j(ω) are the x and y coordinates of the phasor plot,\n", "n and ω are the harmonic frequency and the angular frequency of excitation,\n", "respectively, and T is the repeat frequency of the acquisition :cite:`ranjit2018fit`.\n" ] }, { "cell_type": "markdown", "id": "010676e6-99fd-4164-b277-77dd82aa77a4", "metadata": {}, "source": [ "### Fluorescence lifetime analysis\n", "\n", "\n", "Maximum likelihood fits\n", "=======================\n", "\n", "tttrlib provides a set of precompile models with fits (e.g. fit23, fit24,\n", "and fit25) that have their own model function, target (objective) function,\n", "fit function, and parameter correction function. Briefly, the purpose of these\n", "functions is as follows.\n", "\n", "----------------------------------------------------------------------+\n", "| Function | Description |\n", "==========================+============================================+\n", "| Model function | Computes the model for a set of parameters |\n", "+--------------------------+--------------------------------------------+\n", "| Target/objective function| Computes ob and returns a score for data |\n", "+--------------------------+--------------------------------------------+\n", "| Fit function | Optimizes the parameters to the data |\n", "+--------------------------+--------------------------------------------+\n", "| Correction function | Assures reasonable range of parameters |\n", "+--------------------------+--------------------------------------------+\n", "\n", "The model function computes for a set of input parameters the corresponding\n", "realization of the model (the model fluorescence decay curve(s)). The target\n", "function (more often called objective function) computes the disagreement of the\n", "data and the model function for a given set of model parameters. The fitting\n", "function optimizes a set of selected input parameters to the data by minimizing\n", "the disagreement of the data and the model. Finally, the estimates of the model\n", "parameters are corrected by a correction function. These functions have names\n", "that relate to the name of the model, e.g., `DecayFit23.target` corresponds to `fit23`.\n", "For computationally efficiency, the functions are hardcoded in C.\n", "\n", "The models of the different fits differ in their parameters. The parameters of\n", "the fits are described in detail below. The target functions explicitly consider\n", "the counting noise and provide an estimate of the likelihood of the parameters.\n", "The initial values of the parameters are specified by the user. Next, the likelihood\n", "of the target is maximized by the Limited-Memory Broyden-Fletcher-Goldfarb-Shanno\n", "Algorithm (L-BFGS). L-BFGS is an iterative method for solving unconstrained nonlinear\n", "optimization problems. Gradients for the L-BFGS algorithm are approximated by linear\n", "interpolation to handle the differentiable components, i.e., the model parameters\n", "that are free. The parameters of a model ca be either specified as free or as fixed.\n", "Only free parameters are optimized.\n", "\n", "Python\n", "^^^^^^\n", "For a use in python the ``tttrlib`` module exposes a set of C functions that can be\n", "used to (1) compute model and (2) target/objective function and (3) optimize\n", "model parameters to experimental data. Besides the exposed functions the fit models\n", "are accessible via a simplified object-based interface that reduces the number of\n", "lines of code that need to be written for analyzing fluorescence decay histograms.\n", "The code blocks that are used below to illustrate the tttrlib functionality are\n", "extracts from the tests located in the test folder of the tttrlib repository. The\n", "test can be used as a more detailed refernce on how to use tttrlib.\n", "\n", "Model functions can be computed using for instance the ``DecayFit23.modelf``\n", "function of the ``tttrlib`` module. Here, the ``23`` represents a particular model\n", "function. The use of a ``tttrlib`` model function is for the model function\n", "23 (``DecayFit23.model``) below.\n", "\n", ".. literalinclude:: ../examples/fluorescence_decay/plot_fit23_usage_1.py\n", " :language: python\n", " :lines: 36-44\n", " :linenos:\n", "\n", "To compute a model, first a set of model parameters and a set of corrections need\n", "to be specified. All input parameters for ``DecayFit23.model`` are numpy arrays.\n", "In addition to the model parameters and the corrections ``DecayFit.23.model``\n", "requires an instrument response function (irf) and a background pattern. The model\n", "functions operate on numpy arrays and modify the numpy array for a model in-place.\n", "This means, that the output of the function is written to the input numpy-array of the model. In the\n", "example above the output is written to the array ``model``.\n", "\n", "\n", "To compute the value of a target for a realization of model parameters ``DecayFit23``\n", "provides the functions ``DecayFit23.target``. The inputs of a target function (here\n", "``DecayFit23.target``) are an numpy array containing the model parameter values and a\n", "structure that contains the corrections and all other necessary data to compute\n", "the value of the objective function (for fit23 i.e. data, irf, background, time\n", "resolution).\n", "\n", ".. literalinclude:: ../examples/fluorescence_decay/plot_fit23_usage_2.py\n", " :language: python\n", " :lines: 171-179\n", " :linenos:\n", "\n", "The data needed in addition to the model parameters are passed to the target function\n", "using ``fit2x.MParam`` objects that can be created by the ``fit2x.CreateMParam``\n", "function from numpy arrays. The return value of a target function is the score\n", "of the model parameters\n", "\n", "Model parameters can be optimized to the data by fit functions for fit 23 the\n", "fit function ``fit2x.DecayFit23.fit`` is used.\n", "\n", ".. literalinclude:: ../examples/fluorescence_decay/plot_fit23_usage_2.py\n", " :language: python\n", " :lines: 250-253\n", " :linenos:\n", "\n", "The fit functions takes like the target function an object of the type\n", "``fit2x.MParam`` in addition to the initial values, and a list of fixed model\n", "parameters as an input. The array containing the initial values of the model\n", "parameters are modified in-place buy the fit function.\n", "\n", "Alternatively, a simplified python interface can be used to optimize a set of\n", "model as illustrated by the source code embedded in the plot below. The simplified\n", "interface handles the creation of auxiliary data structures such as ``tttrlib.MParam``.\n", "\n", ".. plot:: ../examples/fluorescence_decay/plot_fit23_mini_example.py\n", "\n", " Analysis result of fit23 by the simplified python interface provided\n", " by ``tttrlib``\n", "\n", "In the example shown above, first a fit object of the type ``fit2x.DecayFit23`` is\n", "created. All necessary data except for the experimental data for a fit is passed\n", "to the fit object when it is created. To perform a fit on experimental data for\n", "a set for a set of initial values, the fit object is called using the inital values\n", "and the data as parameters.\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "43c224f0-a626-4e18-910f-a37702227e75", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" } }, "nbformat": 4, "nbformat_minor": 5 }