.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tutorials/workflow.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_gallery_tutorials_workflow.py: Flow Cytometry Simulation: Full System Example ============================================== This tutorial demonstrates a complete flow cytometry simulation using the FlowCyPy library. It models fluidics, optics, signal processing, and classification of multiple particle populations. Steps Covered: -------------- 1. Configure simulation parameters and noise models 2. Define laser source, flow cell geometry, and fluidics 3. Add synthetic particle populations 4. Set up detectors, amplifier, and digitizer 5. Simulate analog and digital signal acquisition 6. Apply triggering and peak detection 7. Classify particle events based on peak features .. GENERATED FROM PYTHON SOURCE LINES 20-22 Step 0: Global Settings and Imports ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-37 .. code-block:: Python import numpy as np from TypedUnit import ureg from FlowCyPy import SimulationSettings SimulationSettings.include_noises = False SimulationSettings.include_shot_noise = True SimulationSettings.include_dark_current_noise = True SimulationSettings.include_source_noise = True SimulationSettings.include_amplifier_noise = True SimulationSettings.assume_perfect_hydrodynamic_focusing = True np.random.seed(3) .. GENERATED FROM PYTHON SOURCE LINES 38-40 Step 1: Define Flow Cell and Fluidics ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-82 .. code-block:: Python from FlowCyPy.flow_cell import FlowCell from FlowCyPy.fluidics import Fluidics, ScattererCollection, distribution, population from FlowCyPy.sampling_method import GammaModel, ExplicitModel flow_cell = FlowCell( sample_volume_flow=80 * ureg.microliter / ureg.minute, sheath_volume_flow=1 * ureg.milliliter / ureg.minute, width=200 * ureg.micrometer, height=100 * ureg.micrometer, ) scatterer_collection = ScattererCollection() medium_refractive_index = distribution.Delta(1.33 * ureg.RIU) population_0 = population.Sphere( name="Pop 0", medium_refractive_index=medium_refractive_index, particle_count=5e17 * ureg.particle / ureg.milliliter, diameter=distribution.RosinRammler(50 * ureg.nanometer, spread=30), refractive_index=distribution.Normal( 1.44 * ureg.RIU, standard_deviation=0.002 * ureg.RIU ), sampling_method=GammaModel(mc_samples=10_000), ) population_1 = population.Sphere( name="Pop 1", particle_count=5e9 * ureg.particle / ureg.milliliter, medium_refractive_index=medium_refractive_index, diameter=distribution.RosinRammler(150 * ureg.nanometer, spread=30), refractive_index=distribution.Normal( 1.44 * ureg.RIU, standard_deviation=0.002 * ureg.RIU ), ) scatterer_collection.add_population(population_0, population_1) scatterer_collection.dilute(factor=80) fluidics = Fluidics(scatterer_collection=scatterer_collection, flow_cell=flow_cell) .. GENERATED FROM PYTHON SOURCE LINES 83-85 Step 2: Define Optical Subsystem -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 85-132 .. code-block:: Python from FlowCyPy.opto_electronics import ( Detector, OptoElectronics, TransimpedanceAmplifier, source, ) source = source.GaussianBeam( numerical_aperture=0.1 * ureg.AU, wavelength=405 * ureg.nanometer, optical_power=200 * ureg.milliwatt, RIN=-180, ) detectors = [ Detector( name="forward", phi_angle=0 * ureg.degree, numerical_aperture=0.3 * ureg.AU, responsivity=1 * ureg.ampere / ureg.watt, ), Detector( name="side", phi_angle=90 * ureg.degree, numerical_aperture=0.3 * ureg.AU, responsivity=1 * ureg.ampere / ureg.watt, ), Detector( name="det 2", phi_angle=30 * ureg.degree, numerical_aperture=0.3 * ureg.AU, responsivity=1 * ureg.ampere / ureg.watt, ), ] amplifier = TransimpedanceAmplifier( gain=10 * ureg.volt / ureg.ampere, bandwidth=10 * ureg.megahertz, voltage_noise_density=0.1 * ureg.nanovolt / ureg.sqrt_hertz, current_noise_density=0.2 * ureg.femtoampere / ureg.sqrt_hertz, ) opto_electronics = OptoElectronics( detectors=detectors, source=source, amplifier=amplifier ) .. GENERATED FROM PYTHON SOURCE LINES 133-135 Step 3: Signal Processing Configuration --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 135-169 .. code-block:: Python from FlowCyPy.signal_processing import ( Digitizer, SignalProcessing, circuits, peak_locator, triggering_system, ) digitizer = Digitizer( bit_depth="14bit", saturation_levels="auto", sampling_rate=60 * ureg.megahertz ) analog_processing = [ circuits.BaselineRestorator(window_size=10 * ureg.microsecond), circuits.BesselLowPass(cutoff=2 * ureg.megahertz, order=4, gain=2), ] triggering = triggering_system.DynamicWindow( trigger_detector_name="forward", threshold=10 * ureg.microvolt, pre_buffer=20, post_buffer=20, max_triggers=-1, ) peak_algo = peak_locator.GlobalPeakLocator(compute_width=False) signal_processing = SignalProcessing( digitizer=digitizer, analog_processing=analog_processing, triggering_system=triggering, peak_algorithm=peak_algo, ) .. GENERATED FROM PYTHON SOURCE LINES 170-172 Step 4: Run Simulation ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 172-183 .. code-block:: Python from FlowCyPy import FlowCytometer cytometer = FlowCytometer( opto_electronics=opto_electronics, fluidics=fluidics, signal_processing=signal_processing, background_power=0.001 * ureg.milliwatt, ) run_record = cytometer.run(run_time=1 * ureg.millisecond) .. GENERATED FROM PYTHON SOURCE LINES 184-186 Step 5: Plot Events and Raw Analog Signals ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: Python _ = run_record.event_collection.plot(x="side") .. image-sg:: /gallery/tutorials/images/sphx_glr_workflow_001.png :alt: Distribution of side :srcset: /gallery/tutorials/images/sphx_glr_workflow_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 190-192 Plot raw analog signals ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: Python _ = run_record.plot_analog(figure_size=(12, 8)) .. image-sg:: /gallery/tutorials/images/sphx_glr_workflow_002.png :alt: workflow :srcset: /gallery/tutorials/images/sphx_glr_workflow_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 196-198 Step 6: Plot Triggered Analog Segments -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 198-201 .. code-block:: Python _ = run_record.plot_digital(figure_size=(12, 8)) .. image-sg:: /gallery/tutorials/images/sphx_glr_workflow_003.png :alt: workflow :srcset: /gallery/tutorials/images/sphx_glr_workflow_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 202-204 Step 7: Plot Peak Features -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 204-207 .. code-block:: Python _ = run_record.peaks.plot(x=("forward", "Height")) .. image-sg:: /gallery/tutorials/images/sphx_glr_workflow_004.png :alt: workflow :srcset: /gallery/tutorials/images/sphx_glr_workflow_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 208-210 Step 8: Classify Events from Peak Features ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 210-221 .. code-block:: Python from FlowCyPy.classifier import KmeansClassifier classifier = KmeansClassifier(number_of_clusters=2) classified = classifier.run( dataframe=run_record.peaks.unstack("Detector"), features=["Height"], detectors=["side", "forward"], ) _ = classified.plot(x=("side", "Height"), y=("forward", "Height")) .. image-sg:: /gallery/tutorials/images/sphx_glr_workflow_005.png :alt: Event classification :srcset: /gallery/tutorials/images/sphx_glr_workflow_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.319 seconds) .. _sphx_glr_download_gallery_tutorials_workflow.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: workflow.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: workflow.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: workflow.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_