.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tuto_02_noise.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_tuto_02_noise.py: 02. Noise operators =================================================== .. _tuto_noise: This tutorial shows how to use noise operators using the :mod:`spyrit.core.noise` submodule. .. image:: ../fig/tuto2.png :width: 600 :align: center :alt: Reconstruction architecture sketch | .. GENERATED FROM PYTHON SOURCE LINES 17-19 Load a batch of images ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-24 We load a batch of images from the `/images/` folder. Using the :func:`transform_gray_norm` function with the :attr:`normalize=False` argument returns images with values in (0,1). .. GENERATED FROM PYTHON SOURCE LINES 24-47 .. code-block:: Python import os import torch import torchvision import matplotlib.pyplot as plt from spyrit.misc.disp import imagesc from spyrit.misc.statistics import transform_gray_norm spyritPath = os.getcwd() imgs_path = os.path.join(spyritPath, "images/") # Grayscale images of size 64 x 64, no normalization to keep values in (0,1) transform = transform_gray_norm(img_size=64, normalize=False) # Create dataset and loader (expects class folder 'images/test/') dataset = torchvision.datasets.ImageFolder(root=imgs_path, transform=transform) dataloader = torch.utils.data.DataLoader(dataset, batch_size=7) x, _ = next(iter(dataloader)) print(f"Shape of input images: {x.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of input images: torch.Size([7, 1, 64, 64]) .. GENERATED FROM PYTHON SOURCE LINES 48-49 We select the first image in the batch and plot it. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python i_plot = 1 imagesc(x[i_plot, 0, :, :], r"$x$ in (0, 1)") .. image-sg:: /gallery/images/sphx_glr_tuto_02_noise_001.png :alt: $x$ in (0, 1) :srcset: /gallery/images/sphx_glr_tuto_02_noise_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) .. GENERATED FROM PYTHON SOURCE LINES 55-57 Gaussian noise ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 59-65 We consider additive Gaussiane noise, .. math:: y \sim z + \mathcal{N}(0,\sigma^2), where :math:`\mathcal{N}(\mu, \sigma^2)` is a Gaussian distribution with mean :math:`\mu` and variance :math:`\sigma^2`, and :math:`z` is the noiseless image. The larger :math:`\sigma`, the lower the signal-to-noise ratio. .. GENERATED FROM PYTHON SOURCE LINES 67-69 To add 10% Gaussian noise, we instantiate a :class:`spyrit.core.noise` operator with :attr:`sigma=0.1`. .. GENERATED FROM PYTHON SOURCE LINES 69-78 .. code-block:: Python from spyrit.core.noise import Gaussian noise_op = Gaussian(sigma=0.1) x_noisy = noise_op(x) imagesc(x_noisy[1, 0, :, :], r"10% Gaussian noise") # sphinx_gallery_thumbnail_number = 2 .. image-sg:: /gallery/images/sphx_glr_tuto_02_noise_002.png :alt: 10% Gaussian noise :srcset: /gallery/images/sphx_glr_tuto_02_noise_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) .. GENERATED FROM PYTHON SOURCE LINES 79-80 To add 2% Gaussian noise, we update the class attribute :attr:`sigma`. .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. code-block:: Python noise_op.sigma = 0.02 x_noisy = noise_op(x) imagesc(x_noisy[1, 0, :, :], r"2% Gaussian noise") .. image-sg:: /gallery/images/sphx_glr_tuto_02_noise_003.png :alt: 2% Gaussian noise :srcset: /gallery/images/sphx_glr_tuto_02_noise_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) .. GENERATED FROM PYTHON SOURCE LINES 87-89 Poisson noise ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 91-98 We now consider Poisson noise, .. math:: y \sim \mathcal{P}(\alpha z), \quad z \ge 0, where :math:`\alpha \ge 0` is a scalar value that represents the maximum image intensity (in photons). The larger :math:`\alpha`, the higher the signal-to-noise ratio. .. GENERATED FROM PYTHON SOURCE LINES 100-102 We consider the :class:`spyrit.core.noise.Poisson` class and set :math:`\alpha` to 100 photons (which corresponds to the Poisson parameter). .. GENERATED FROM PYTHON SOURCE LINES 102-109 .. code-block:: Python from spyrit.core.noise import Poisson from spyrit.misc.disp import add_colorbar, noaxis alpha = 100 # number of photons noise_op = Poisson(alpha) .. GENERATED FROM PYTHON SOURCE LINES 110-111 We simulate two noisy versions of the same images .. GENERATED FROM PYTHON SOURCE LINES 111-115 .. code-block:: Python y1 = noise_op(x) # first sample y2 = noise_op(x) # another sample .. GENERATED FROM PYTHON SOURCE LINES 116-117 We now consider the case :math:`\alpha = 1000` photons. .. GENERATED FROM PYTHON SOURCE LINES 117-121 .. code-block:: Python noise_op.alpha = 1000 y3 = noise_op(x) # noisy measurement vector .. GENERATED FROM PYTHON SOURCE LINES 122-123 We finally plot the noisy images .. GENERATED FROM PYTHON SOURCE LINES 123-146 .. code-block:: Python # plot f, axs = plt.subplots(1, 3, figsize=(10, 5)) axs[0].set_title("100 photons") im = axs[0].imshow(y1[1, 0].reshape(64, 64), cmap="gray") add_colorbar(im, "bottom") axs[1].set_title("100 photons") im = axs[1].imshow(y2[1, 0].reshape(64, 64), cmap="gray") add_colorbar(im, "bottom") axs[2].set_title("1000 photons") im = axs[2].imshow( y3[ 1, 0, ].reshape(64, 64), cmap="gray", ) add_colorbar(im, "bottom") noaxis(axs) .. image-sg:: /gallery/images/sphx_glr_tuto_02_noise_004.png :alt: 100 photons, 100 photons, 1000 photons :srcset: /gallery/images/sphx_glr_tuto_02_noise_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) /home/docs/checkouts/readthedocs.org/user_builds/spyrit/envs/3.0.1/lib/python3.11/site-packages/matplotlib/cbook.py:684: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword x = np.array(x, subok=True, copy=copy) .. GENERATED FROM PYTHON SOURCE LINES 147-154 As expected the signal-to-noise ratio of the measurement vector is higher for 1,000 photons than for 100 photons .. note:: Not only the signal-to-noise, but also the scale of the measurements depends on :math:`\alpha`, which motivates the introduction of the preprocessing operator. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.352 seconds) .. _sphx_glr_download_gallery_tuto_02_noise.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tuto_02_noise.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tuto_02_noise.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tuto_02_noise.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_