.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02-mass_distribution.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_02-mass_distribution.py: Mass distribution ================= This tutorial shows how to compute and fit mass distributions from a movie once a contrast-to-mass calibration is available: 1. Download an example movie. 2. Set a contrast-to-mass calibration. 3. Run detection + PSF fitting to obtain a table of event masses. 4. Fit peaks in the mass histogram and visualize the result. .. GENERATED FROM PYTHON SOURCE LINES 15-18 Imports ------- We first import necessary packages and modules. .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python from pprint import pprint import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 25-30 Download example data --------------------- This tutorial uses a short example movie from Zenodo. You can replace this block with a direct file path once you have a local movie. .. GENERATED FROM PYTHON SOURCE LINES 30-47 .. code-block:: Python from zenodozipdownloader import ZenodoZipDownloader import massphoto as mp files = ZenodoZipDownloader( doi="10.5281/zenodo.15800729", download_dir="example_data", ).download("Figure 2.zip/006_dynamin_6.mp") if not files: raise RuntimeError( "Failed to download example data. Please download it manually from https://zenodo.org/record/15800729 " "or set path_movie to your own movie." ) path_movie = files[0] print(f"Using movie: {path_movie}") .. rst-class:: sphx-glr-script-out .. code-block:: none Using movie: /home/runner/work/massphoto/massphoto/docs/source/_examples/example_data/006_dynamin_6.mp .. GENERATED FROM PYTHON SOURCE LINES 48-54 Analyse with default parameters ------------------------------- Set mass calibration parameters. See the mass calibration example for how to obtain these from a calibration movie: :ref:`sphx_glr_auto_examples_01-mass_calibration.py`. .. GENERATED FROM PYTHON SOURCE LINES 54-66 .. code-block:: Python # Disable progress bars for cleaner output in this tutorial mp.defaults.set("progress_bar", False) # Set contrast-to-mass calibration parameters (intercept, slope). Adjust to match your calibration. mp.defaults.set("mass_calibration", (-5.2, 3.1e4)) # Run detection and fitting. Keep the ratiometric frames for inspecting events. # Sequential execution limits peak memory while retaining those frames. fitres, movie_r = mp.fit(path_movie, return_ratiometric=True, scheduler="synchronous") fitres[["frame", "x", "y", "contrasts", "masses_kDa"]].head().round({"x": 2, "y": 2, "contrasts": 5, "masses_kDa": 1}) .. raw:: html
frame x y contrasts masses_kDa
0 20 86.09 6.87 0.00343 101.2
1 20 165.06 72.34 0.00604 182.1
2 20 205.38 74.17 0.00648 195.8
3 20 124.95 80.87 0.00580 174.5
4 20 189.78 93.18 0.01202 367.4


.. GENERATED FROM PYTHON SOURCE LINES 67-68 Fit mass distribution .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: Python histfit = mp.distfit.fit(fitres["masses_kDa"], bins=np.arange(-100, 700, 2.5), include_background=True) histfit.peaks[["mu", "sigma", "A"]].round(2) .. raw:: html
mu sigma A
0 89.37 7.15 4199.50
1 182.20 7.82 5248.13
2 370.17 9.50 3235.14
3 557.33 10.71 290.48


.. GENERATED FROM PYTHON SOURCE LINES 72-73 Plot results .. GENERATED FROM PYTHON SOURCE LINES 73-81 .. code-block:: Python plt.figure(figsize=(6, 3)) plt.bar(histfit.bin_centers, histfit.counts, width=histfit.bin_widths, edgecolor="none", label="Data") plt.plot(histfit.model_x, histfit.model_y, color="black", lw=1, label="Fit") plt.xlabel("Mass (kDa)") plt.ylabel("Counts") plt.legend() plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_02-mass_distribution_001.png :alt: 02 mass distribution :srcset: /auto_examples/images/sphx_glr_02-mass_distribution_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-85 Analyse with custom parameters ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 85-137 .. code-block:: Python # Read the movie movie, metadata = mp.read_movie(path_movie, convert_metadata=True) # Inspect the metadata print("Metadata:") pprint(metadata) # Check default thresholds for detection print("Default detection thresholds:", mp.defaults.get("event_correlation_step.thresholds")) # We could adjust these if we want to be more or less sensitive, for all following analyses. For example: # mp.defaults.set("event_correlation_step.thresholds", [0.5, 0.4, 0.001]) # Check default averaging window print("Default navg:", mp.defaults.get("navg")) # We could adjust the default like this: # mp.defaults.set("navg", 5) ## # We can also specify detection arguments here, which will only apply to this fit call. fitres = mp.fit( movie, movie_correct_drift=True, ratiometric_args={ "navg": 5, }, detection_args={ "thresholds": [ 0.5, # temporal threshold 0.4, # spatial threshold 0.001, # contrast threshold ], # navg for the temporal filter (longer than navg for the ratiometric filter is often beneficial) "correlation_step_args": {"navg": 10}, }, ) # We decreased the integration time and lowered the thresholds, which leads to more events being detected, but also more noise. # To remove some of the noise, we can filter based on fit quality or mass uncertainty. filter_str = "masses_kDa_se < 10 & r2_fit > 0.4" histfit = mp.distfit.fit( fitres.query(filter_str), bins=np.arange(-100, 700, 2.5), initial_peaks=(90, 180, 360, 540), include_background=False, ) histfit.peaks[["mu", "sigma", "A"]].round(2) .. rst-class:: sphx-glr-script-out .. code-block:: none Metadata: {'camera': 'GS3_U3_23S6M_C', 'exposuretime': 7.646799087524414, 'exposuretime_effective': 15.293598175048828, 'exposuretime_unit': 'ms', 'field_of_view': (11.7528, 16.4964), 'field_of_view_unit': 'um', 'framebinning': 2, 'framerate': 128.20632934570312, 'framerate_effective': 64.10316467285156, 'framerate_unit': 'Hz', 'image_shape': (166, 233), 'instrument': 'Refeyn TwoMP', 'pixelbinning': 6, 'pixelsize': 0.0118, 'pixelsize_effective': 0.0708, 'pixelsize_unit': 'um'} Default detection thresholds: (0.6, 0.5, 0.001) Default navg: 10 .. raw:: html
mu sigma A
0 89.72 8.04 4527.06
1 182.05 8.22 5284.42
2 369.90 9.73 3245.49
3 557.76 12.82 372.53


.. GENERATED FROM PYTHON SOURCE LINES 295-296 Compare the unfiltered distribution, selected events, and fitted peaks. .. GENERATED FROM PYTHON SOURCE LINES 296-306 .. code-block:: Python plt.figure(figsize=(6, 3)) plt.hist(fitres["masses_kDa"], bins=histfit.bin_edges, edgecolor="none", facecolor="orange", label="Data (unfiltered)") plt.bar(histfit.bin_centers, histfit.counts, width=histfit.bin_widths, edgecolor="none", label="Data") plt.plot(histfit.model_x, histfit.model_y, color="black", lw=1, label="Fit") plt.xlabel("Mass (kDa)") plt.ylabel("Counts") plt.legend() plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_02-mass_distribution_002.png :alt: 02 mass distribution :srcset: /auto_examples/images/sphx_glr_02-mass_distribution_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 3.736 seconds) .. _sphx_glr_download_auto_examples_02-mass_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02-mass_distribution.ipynb <02-mass_distribution.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02-mass_distribution.py <02-mass_distribution.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02-mass_distribution.zip <02-mass_distribution.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_