.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04-drift_correction.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_04-drift_correction.py: Drift correction ================ This example demonstrates how to correct for sample drift in a movie using the :func:`massphoto.correct_drift` function. This correction is automatically applied when using :func:`massphoto.fit` with the `movie_correct_drift=True` argument, but here we show how to do it manually and visualize the results. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 17-22 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 file. .. GENERATED FROM PYTHON SOURCE LINES 22-40 .. code-block:: Python from zenodozipdownloader import ZenodoZipDownloader import massphoto as mp files = ZenodoZipDownloader( doi="10.5281/zenodo.15800729", download_dir="example_data", ).download(inner_pattern="005_mfp1_500x.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 file." ) 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/005_mfp1_500x.mp .. GENERATED FROM PYTHON SOURCE LINES 41-46 Determine and correct drift --------------------------- Load the movie and choose a template image used for alignment. A robust choice is the mean image over time (also the default template in `correct_drift`). .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: Python movie, metadata = mp.read_movie(path_movie, convert_metadata=True) movie = movie.astype(float) template = np.mean(movie, axis=0) movie_corrected, drift = mp.correct_drift(movie, template=template, return_drift=True) .. GENERATED FROM PYTHON SOURCE LINES 55-60 Plot drift over time -------------------- `drift` contains the per-frame translation (in pixels) applied to align each frame to the template. .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. code-block:: Python plt.figure(figsize=(10, 5)) plt.plot(drift[:, 0], label="Y drift") plt.plot(drift[:, 1], label="X drift") plt.xlabel("Frame") plt.ylabel("Drift (pixels)") plt.legend() plt.title("Sample Drift Over Time") plt.grid(True) plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_04-drift_correction_001.png :alt: Sample Drift Over Time :srcset: /auto_examples/images/sphx_glr_04-drift_correction_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-77 Visual sanity check (before/after) ---------------------------------- Compare the difference between the first and last frame. With drift, the difference image often shows edge-like structures; after correction, these should be reduced. .. GENERATED FROM PYTHON SOURCE LINES 77-92 .. code-block:: Python diff_before = movie[-1] - movie[0] diff_after = movie_corrected[-1] - movie_corrected[0] vmin, vmax = np.quantile(diff_before, [0.01, 0.99]) plt.figure(figsize=(12, 6)) plt.subplot(2, 1, 1) plt.imshow(diff_before, cmap="bwr", vmin=vmin, vmax=vmax) plt.title("Difference Before Drift Correction") plt.colorbar(label="Intensity difference (counts)") plt.subplot(2, 1, 2) plt.imshow(diff_after, cmap="bwr", vmin=vmin, vmax=vmax) plt.title("Difference After Drift Correction") plt.colorbar(label="Intensity difference (counts)") plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_04-drift_correction_002.png :alt: Difference Before Drift Correction, Difference After Drift Correction :srcset: /auto_examples/images/sphx_glr_04-drift_correction_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.895 seconds) .. _sphx_glr_download_auto_examples_04-drift_correction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04-drift_correction.ipynb <04-drift_correction.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04-drift_correction.py <04-drift_correction.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04-drift_correction.zip <04-drift_correction.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_