.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/flim/plot_marker.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_flim_plot_marker.py: ============================= Marker in Scanning microscopy ============================= In TTTR scanning microscopy the detected photons and the markers that mark the beginning of a new scanning line and frame are events in stream. There are no real standards how markers for lines and frames are implemented. Thus, often the data in a TTTR stream needs to be inspected manually to decipher the content. In this tutorial, the TTTR stream of a custom build confocal microscope equipped with PicoQuant counting electronics and a TTTR stream of a Leica SP8 instrument is inspected. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python from __future__ import print_function import tttrlib import numpy as np import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 19-24 Read the TTTR Stream -------------------- First, the data corresponding to the image needs to be saved as TTTR-file. Leica uses PicoQuant (PQ) PTU files that can be loaded in ``tttrlib``. The custom setup saved the CLSM data to PQ HT3 files. .. GENERATED FROM PYTHON SOURCE LINES 24-31 .. code-block:: Python fn_custom_lsm = '../../tttr-data/imaging/pq/ht3/pq_ht3_clsm.ht3' fn_leica_sp8 = '../../tttr-data/imaging/leica/sp8/da/G-28_C-28_S1_6_1.ptu' # To decipher the data, we first read the TTTR streams. tttr_custom = tttrlib.TTTR(fn_custom_lsm, 1) tttr_leica = tttrlib.TTTR(fn_leica_sp8) .. GENERATED FROM PYTHON SOURCE LINES 32-37 CLSM marker (Custom setup) -------------------------- The custom setup used a "standard" encoding for the frame and line markers. The frame and line markers are so called "special" events (no photons). Thus, we select all events where the event type equals to 1 (photons have an event type of 0). .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python e = tttr_custom.get_event_type() c = tttr_custom.get_routing_channel() special_events = np.where(e == 1) .. GENERATED FROM PYTHON SOURCE LINES 42-45 Inspecting the routing channels of the special events, we find that the routing channel numbers are 4, 0, and 2. The 4 marks the beginning of a new frame. The 0 the beginning of a new line and the 2 the end of a line. .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: Python fig, ax = plt.subplots(3, 1, sharey=False) plt.setp(ax[0].get_xticklabels(), visible=False) ax[0].plot(c[special_events][:7000], 'g') ax[1].plot(c[special_events][:50], 'g') ax[2].plot(c[special_events][len(c[special_events])-200:], 'g') plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_001.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-57 CLSM marker (Leica) ------------------- Below, an analysis of the frame and line marker is briefly outlined for a Leica SP8 dataset. .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: Python e = tttr_leica.event_types c = tttr_leica.routing_channels t = tttr_leica.macro_times m = tttr_leica.micro_times .. GENERATED FROM PYTHON SOURCE LINES 63-67 The routing channels are inspected to determine the actual channel numbers of the detectors. A bincount over the channel numbers determines how often a channel occurs in the data stream. Look for used channels .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python y = np.bincount(c) print(y) plt.plot(y) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_002.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0 3009504 0 0 0 0 0 0 0 0 0 0 0 0 0 95325] .. GENERATED FROM PYTHON SOURCE LINES 73-94 In the dataset two channels were populated (channel 1 and channel 15). For the measurements only a single detector was used. Hence, likely, channel 15 was used to encode other information. * 1 - 3009504 * 15 - 95325 .. note:: Usually, the TTTR records utilize the event type to distinguish markers from photons. Leica decided to use the routing channel number to identify markers. Based on these counts, channel 15 very likely identifies the markers. The number of events 95325 closely matches a multiple of 2 (95325 = 93 * 512 + 3). Note, there are 1024 lines in the images, 4 images in the file. By looking at the macro time one can also finds that there are 93 images in the file, as intensity within the image in non-uniform. Hence, the macro time fluctuates. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python plt.plot(tttr_leica.macro_times) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_003.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-101 To make sure that the routing channel 1 is indeed a detection channels, one can create (in a time-resolved experiment) a bincount of the associated micro times. .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: Python m_ch_1 = m[np.where(c == 1)] y = np.bincount(m_ch_1) plt.plot(y) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_004.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-111 Next, to identify if in addition to the channel number 15 the markers are identified by non-photon event marker we make a bincount of the channel numbers, where the event type is 1 (photon events have the event type 0, non-photon events have the event type 1). .. GENERATED FROM PYTHON SOURCE LINES 111-115 .. code-block:: Python y = np.bincount(c[np.where(e == 1)]) plt.plot(y) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_005.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-124 To sum up, channel 1 is a routing channels of the detector. Channel 15 is the routing channel used to inject the special markers. Next, we inspect the micro time and the macro time of the events registered by the routing channel 15. The plot of the micro times for the events of the routing channel 15 reveals, that the micro time is either 1, 2, or 4. A more close inspection reveals that a micro time value of 1 is always succeeded by a micro time value of 2. .. GENERATED FROM PYTHON SOURCE LINES 124-129 .. code-block:: Python m_ch_15 = m[np.where(c == 15)] plt.plot(m_ch_15) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_006.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 130-144 A micro time value of 4 is followed by a micro time value of 1. This means, that the micro time encodes the frame marker and the line start/stop markers. * micro time 1 - line start * micro time 2 - line stop * micro time 4 - frame start .. note:: The first frame does not have a frame start. Next, the macro time of the events where the routing channel number equals 15 is inspected. As anticipated, the macro time increases on first glance continuously. On closer inspection, however, steps in the macro time are visible. .. GENERATED FROM PYTHON SOURCE LINES 144-148 .. code-block:: Python plt.plot(t[np.where(c == 15)]) plt.show() .. image-sg:: /auto_examples/flim/images/sphx_glr_plot_marker_007.png :alt: plot marker :srcset: /auto_examples/flim/images/sphx_glr_plot_marker_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-161 To sum up, in the Leica SP8 PTU files 1. line and frame markers are treated as regular photons. 2. the line and frame markers are identified by the routing channel number 15 3. the type of a marker is encoded in the micro time of channels with a channel number 15 .. note:: Usually, the TTTR records utilize the event type to distinguish markers from photons. Here, Leica decided to use the routing channel number to identify markers. When opening an image in ``tttrlib`` this special case is considered by specifying the reading routine. .. GENERATED FROM PYTHON SOURCE LINES 163-171 Conclusion ---------- It is not always fully documented how a manufacturer of a microscope encodes # laser scanning microscopy data in TTTR files. Often it is unclear how the frame and line marker are integrated into the event data stream. The encoding of the frame and line markers of the custom setup is more widespread. Thus, it is the default when reading CLSM data. Moreover, the meta data (which was not discussed here) contains additional (optional) information on how to read/process the photon stream. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 15.533 seconds) .. _sphx_glr_download_auto_examples_flim_plot_marker.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_marker.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_marker.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_