.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tuto_01_a_acquisition_operators.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_01_a_acquisition_operators.py: 01.a. Acquisition operators (basic) ==================================================== .. _tuto_acquisition_operators: This tutorial shows how to simulate measurements using the :mod:`spyrit.core.meas` submodule. .. image:: ../fig/tuto1.png :width: 600 :align: center :alt: Reconstruction architecture sketch | All simulations are based on :class:`spyrit.core.meas.Linear` base class that simulates linear measurements .. math:: m = Hx, where :math:`H\in\mathbb{R}^{M\times N}` is the acquisition matrix, :math:`x \in \mathbb{R}^N` is the signal of interest, :math:`M` is the number of measurements, and :math:`N` is the dimension of the signal. .. important:: The vector :math:`x \in \mathbb{R}^N` represents a multi-dimensional array (e.g, an image :math:`X \in \mathbb{R}^{N_1 \times N_2}` with :math:`N = N_1 \times N_2`). Both variables are related through vectorization , i.e., :math:`x = \texttt{vec}(X)`. .. GENERATED FROM PYTHON SOURCE LINES 29-31 1D Measurements ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-34 We instantiate a measurement operator from a matrix of shape (10, 15). .. GENERATED FROM PYTHON SOURCE LINES 34-40 .. code-block:: Python import torch from spyrit.core.meas import Linear H = torch.randn(10, 15) meas_op = Linear(H) .. GENERATED FROM PYTHON SOURCE LINES 41-42 We consider 3 signals of length 15 .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python x = torch.randn(3, 15) .. GENERATED FROM PYTHON SOURCE LINES 45-47 We apply the operator to the batch of images, which produces 3 measurements of length 10 .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python m = meas_op(x) print(m.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none torch.Size([3, 10]) .. GENERATED FROM PYTHON SOURCE LINES 51-52 We now plot the matrix-vector products .. GENERATED FROM PYTHON SOURCE LINES 52-72 .. code-block:: Python from spyrit.misc.disp import add_colorbar, noaxis import matplotlib.pyplot as plt f, axs = plt.subplots(1, 3, figsize=(10, 5)) axs[0].set_title("Forward matrix H") im = axs[0].imshow(H, cmap="gray") add_colorbar(im, "bottom") axs[1].set_title("Signals x") im = axs[1].imshow(x.T, cmap="gray") add_colorbar(im, "bottom") axs[2].set_title("Measurements m") im = axs[2].imshow(m.T, cmap="gray") add_colorbar(im, "bottom") noaxis(axs) # sphinx_gallery_thumbnail_number = 1 .. image-sg:: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_001.png :alt: Forward matrix H, Signals x, Measurements m :srcset: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_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.1.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.1.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.1.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 73-75 2D Measurements ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 77-80 We load a batch of images from the :attr:`/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 80-96 .. code-block:: Python import os import torchvision from spyrit.misc.statistics import transform_gray_norm spyritPath = os.getcwd() imgs_path = os.path.join(spyritPath, "images/") # Grayscale images of size (32, 32), no normalization to keep values in (0,1) transform = transform_gray_norm(img_size=32, normalize=False) # Create dataset and loader (expects class folder :attr:'images/test/') dataset = torchvision.datasets.ImageFolder(root=imgs_path, transform=transform) dataloader = torch.utils.data.DataLoader(dataset, batch_size=7) x, _ = next(iter(dataloader)) .. GENERATED FROM PYTHON SOURCE LINES 97-98 We crop the batch to get image of shape (9, 25). .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python x = x[:, :, :9, :25] 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, 9, 25]) .. GENERATED FROM PYTHON SOURCE LINES 102-103 We plot the second image. .. GENERATED FROM PYTHON SOURCE LINES 103-108 .. code-block:: Python from spyrit.misc.disp import imagesc imagesc(x[1, 0, :, :], "Image X") .. image-sg:: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_002.png :alt: Image X :srcset: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_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.1.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 109-110 We instantiate a measurement operator from a random matrix with shape (10, 9*25). To indicate that the operator works in 2D, we use the :attr:`meas_shape` argument. .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python H = torch.randn(10, 9 * 25) meas_op = Linear(H, meas_shape=(9, 25)) .. GENERATED FROM PYTHON SOURCE LINES 114-115 We apply the operator to the batch of images, which produces a batch of measurement vectors of length 10. .. GENERATED FROM PYTHON SOURCE LINES 115-119 .. code-block:: Python m = meas_op(x) print(m.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none torch.Size([7, 1, 10]) .. GENERATED FROM PYTHON SOURCE LINES 120-121 We now plot the matrix-vector products corresponding to the second image in the batch. .. GENERATED FROM PYTHON SOURCE LINES 123-124 We first select the second image and the second measurement vector in the batch. .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: Python x_plot = x[1, 0, :, :] m_plot = m[1] .. GENERATED FROM PYTHON SOURCE LINES 128-129 Then we vectorize the image to get a 1D array of length 9*25. .. GENERATED FROM PYTHON SOURCE LINES 129-133 .. code-block:: Python x_plot = x_plot.reshape(1, -1) print(f"Vectorised image with shape: {x_plot.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Vectorised image with shape: torch.Size([1, 225]) .. GENERATED FROM PYTHON SOURCE LINES 134-135 We finally plot the matrix-vector products :math:`m = H x = H \texttt{vec}(X)`. .. GENERATED FROM PYTHON SOURCE LINES 135-153 .. code-block:: Python from spyrit.misc.disp import add_colorbar, noaxis import matplotlib.pyplot as plt f, axs = plt.subplots(1, 3) axs[0].set_title("Forward matrix H") im = axs[0].imshow(H, cmap="gray") # add_colorbar(im, "bottom") axs[1].set_title("x = vec(X)") im = axs[1].imshow(x_plot.mT, cmap="gray") # add_colorbar(im, "bottom") axs[2].set_title("Measurements m") im = axs[2].imshow(m_plot.mT, cmap="gray") # add_colorbar(im, "bottom") noaxis(axs) .. image-sg:: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_003.png :alt: Forward matrix H, x = vec(X), Measurements m :srcset: /gallery/images/sphx_glr_tuto_01_a_acquisition_operators_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.1.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.1.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.1.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) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.304 seconds) .. _sphx_glr_download_gallery_tuto_01_a_acquisition_operators.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tuto_01_a_acquisition_operators.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tuto_01_a_acquisition_operators.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tuto_01_a_acquisition_operators.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_