.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/single_molecule/plot_single_molecule_burst_selection.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_single_molecule_plot_single_molecule_burst_selection.py: ======================== Burst selection routines ======================== The function ``tttrlib.ranges_by_time_window`` can be used to define ranges in a photon stream based on time windows and a minimum number of photons within the time windows. The function has two main parameters that determine the selection of the ranges besides the stream of time events provided by the parameter ``time``: 1. The minimum window length ``minimum_window_length`` 2. The minimum number of photons in a time window ``minimum_number_of_photons_in_time_window`` Additional parameters to discriminate bursts are: 3. The maximum allowed window length ``maximum_window_length`` 4. The maximum number of events in a window ``maximum_number_of_photons_in_time_window`` The the parameters of the C function and the function header are shown below .. code:: c void ranges_by_time_window( int **output, int *n_output, unsigned long long *input, int n_input, double minimum_window_length, double maximum_window_length=-1, int minimum_number_of_photons_in_time_window=-1, int maximum_number_of_photons_in_time_window=-1, double macro_time_calibration=1.0, bool invert=false ) The units of the macro time are defined by the parameter ``macro_time_calibration``. For a given TTTR object the functionality is provided by the method ``TTTR.get_time_window_ranges``. A typical use case of this function is to select single molecule events confocal single-molecule FRET experiments as shown below. .. GENERATED FROM PYTHON SOURCE LINES 41-58 .. code-block:: Python import numpy as np import tttrlib import pylab as plt data = tttrlib.TTTR('../../tttr-data/bh/bh_spc132.spc', 'SPC-130') def save_burst_id( fn: str, start_stop: np.ndarray ): with open(fn, "w") as fp: for start, stop in start_stop: fp.write("%i\t%i\n" % (start, stop)) .. GENERATED FROM PYTHON SOURCE LINES 59-62 This selects ranges in the event stream with a length of at least 250 microseconds with that with at least 20 events. This selection will select "bright" regions in the event stream. .. GENERATED FROM PYTHON SOURCE LINES 62-70 .. code-block:: Python minimum_window_length = 2e-3 # 2 millisecond maximum_window_length = 30e-3 # 30 millisecond / optional parameter tw_ranges = data.get_time_window_ranges( minimum_window_length=minimum_window_length, maximum_window_length=maximum_window_length, minimum_number_of_photons_in_time_window=40 ) .. GENERATED FROM PYTHON SOURCE LINES 71-73 The method returns an array of indices that mark the start and the stop of the time window in the TTTR events (start1, stop1, start2, stop2, ...). .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: Python start_stop = tw_ranges.reshape([len(tw_ranges) // 2, 2]) sel = list() for start, stop in start_stop: sel += range(start, stop) sel = np.array(sel) .. GENERATED FROM PYTHON SOURCE LINES 80-82 Select the "green" donor and the "red" acceptor events separately for plotting and compute intensity traces The selection is plotted in blue. .. GENERATED FROM PYTHON SOURCE LINES 82-90 .. code-block:: Python green_indeces = data.get_selection_by_channel([0, 8]) red_indeces = data.get_selection_by_channel([1, 9]) intensity_trace_green = data[green_indeces].get_intensity_trace(maximum_window_length) intensity_trace_red = data[red_indeces].get_intensity_trace(maximum_window_length) bursts_selected = data[sel].get_intensity_trace(maximum_window_length) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Save the traces to files .. GENERATED FROM PYTHON SOURCE LINES 92-115 .. code-block:: Python # folder = "S:/Papers/00_in_preparation/tttrlib/Figures/Fig1_Introduction/" # np.savetxt(folder + "red_trace.txt", intensity_trace_red) # np.savetxt(folder + "green_trace.txt", intensity_trace_green) # np.savetxt(folder + "bursts_selected.txt", bursts_selected) fig, ax = plt.subplots(3, sharex=True, sharey=False) plt.setp(ax[0].get_xticklabels(), visible=False) ax[0].plot(intensity_trace_green, 'g') ax[1].plot(intensity_trace_red, 'r') ax[1].invert_yaxis() ax[2].plot(intensity_trace_green, 'g') ax[2].plot(intensity_trace_red, 'r') ax[2].plot(bursts_selected, 'b') plt.subplots_adjust( left=None, bottom=None, right=None, top=None, wspace=None, hspace=0 ) plt.show() .. image-sg:: /auto_examples/single_molecule/images/sphx_glr_plot_single_molecule_burst_selection_001.png :alt: plot single molecule burst selection :srcset: /auto_examples/single_molecule/images/sphx_glr_plot_single_molecule_burst_selection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.232 seconds) .. _sphx_glr_download_auto_examples_single_molecule_plot_single_molecule_burst_selection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_single_molecule_burst_selection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_single_molecule_burst_selection.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_