
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "example_gallery/visualize_picmus_dataset.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_example_gallery_visualize_picmus_dataset.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_example_gallery_visualize_picmus_dataset.py:


Loading and Visualizing Ultrasound Images
==========================================

This example demonstrates how to:

1. List available datasets
2. Download a PICMUS dataset using the public API
3. Visualize the beamformed ultrasound data

This serves as a minimal example of using the ultrasound_metrics.data module.
The PICMUS (Plane-wave Imaging Challenge in Medical UltraSound) dataset [1]_
provides standardized ultrasound data for algorithm comparison.

References
----------
.. [1] H. Liebgott, A. Rodriguez-Molares, F. Cervenansky, J. A. Jensen and O. Bernard,
       "Plane-Wave Imaging Challenge in Medical Ultrasound," 2016 IEEE International
       Ultrasonics Symposium (IUS), Tours, France, 2016, pp. 1-4,
       doi: 10.1109/ULTSYM.2016.7728908.

Example
-------

.. GENERATED FROM PYTHON SOURCE LINES 28-30

1. Import required modules and functions
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 30-42

.. code-block:: Python


    import matplotlib.pyplot as plt

    from ultrasound_metrics.data import (
        create_bmode_image,
        db_zero,
        list_available_datasets,
    )
    from ultrasound_metrics.data.uff import (
        load_uff_dataset,
    )








.. GENERATED FROM PYTHON SOURCE LINES 43-45

2. List available datasets
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 45-51

.. code-block:: Python

    print("Available datasets:")
    datasets = list_available_datasets()
    for name, info in datasets.items():
        print(f"- {name}: {info['description']}")
    print()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Available datasets:
    - picmus_resolution_experiment: PICMUS challenge resolution/distortion test (experiment)
    - picmus_contrast_experiment: PICMUS challenge contrast/speckle test (experiment)





.. GENERATED FROM PYTHON SOURCE LINES 52-54

3. Choose a dataset to visualize
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 54-58

.. code-block:: Python


    dataset_name = "picmus_resolution_experiment"
    print(f"Loading dataset: {dataset_name}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Loading dataset: picmus_resolution_experiment




.. GENERATED FROM PYTHON SOURCE LINES 59-61

4. Load the dataset
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 61-70

.. code-block:: Python


    beamformed_image, scan = load_uff_dataset(dataset_name)

    print("Dataset loaded successfully!")
    print(f"Data shape: {beamformed_image.shape}")
    print(f"Data type: {beamformed_image.dtype}")
    print(f"Data range: [{beamformed_image.min():.3f}, {beamformed_image.max():.3f}]")
    print(f"Scan geometry: x={scan.x_axis.size}, z={scan.z_axis.size}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Dataset loaded successfully!
    Data shape: (387, 609)
    Data type: complex64
    Data range: [-56.675+21.870j, 52.407-5.226j]
    Scan geometry: x=387, z=609




.. GENERATED FROM PYTHON SOURCE LINES 71-73

5. Convert to B-mode magnitude for visualization
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 73-78

.. code-block:: Python


    data_array = create_bmode_image(beamformed_image)

    print(f"After taking magnitude - Data range: [{data_array.min():.3f}, {data_array.max():.3f}]")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    After taking magnitude - Data range: [0.003, 62.383]




.. GENERATED FROM PYTHON SOURCE LINES 79-81

6. Simple visualization with physical coordinates
------------------------------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 81-121

.. code-block:: Python


    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

    # Get physical coordinates from scan geometry
    x_coords = scan.x_axis
    z_coords = scan.z_axis

    # Plot the magnitude data with physical coordinates
    # Use 'equal' aspect for square pixels, assuming isotropic grid spacing
    im1 = ax1.imshow(
        data_array.T,
        cmap="gray",
        aspect="equal",
        extent=[x_coords.min(), x_coords.max(), z_coords.max(), z_coords.min()],
    )
    ax1.set_title(f"Magnitude of Beamformed Data\n{dataset_name}")
    ax1.set_xlabel("Lateral Position (m)")
    ax1.set_ylabel("Axial Position (m)")

    # Add colorbar for linear magnitude image
    fig.colorbar(im1, ax=ax1, orientation="vertical", label="Magnitude (linear)")

    # Plot in dB scale (common for ultrasound visualization)
    data_db = db_zero(data_array)

    im2 = ax2.imshow(
        data_db.T,
        cmap="gray",
        aspect="equal",
        vmin=-60,
        vmax=0,
        extent=[x_coords.min(), x_coords.max(), z_coords.max(), z_coords.min()],
    )
    ax2.set_title("B-mode (dB scale)\nNormalized to 0dB")
    ax2.set_xlabel("Lateral Position (m)")
    ax2.set_ylabel("Axial Position (m)")

    # Add colorbar for dB scale image
    fig.colorbar(im2, ax=ax2, orientation="vertical", label="dB")




.. image-sg:: /example_gallery/images/sphx_glr_visualize_picmus_dataset_001.png
   :alt: Magnitude of Beamformed Data picmus_resolution_experiment, B-mode (dB scale) Normalized to 0dB
   :srcset: /example_gallery/images/sphx_glr_visualize_picmus_dataset_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <matplotlib.colorbar.Colorbar object at 0x7f672ea007d0>



.. GENERATED FROM PYTHON SOURCE LINES 122-128

.. code-block:: Python

    print("\nVisualization complete!")
    print("Left: Raw beamformed data")
    print("Right: B-mode in dB scale (typical ultrasound display)")
    print("\nNote: This example demonstrates the new module structure:")
    print("- General utilities (create_bmode_image, db_zero) from ultrasound_metrics.data")
    print("- UFF-specific functionality (load_uff_dataset) from ultrasound_metrics.data.uff")




.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Visualization complete!
    Left: Raw beamformed data
    Right: B-mode in dB scale (typical ultrasound display)

    Note: This example demonstrates the new module structure:
    - General utilities (create_bmode_image, db_zero) from ultrasound_metrics.data
    - UFF-specific functionality (load_uff_dataset) from ultrasound_metrics.data.uff





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.192 seconds)


.. _sphx_glr_download_example_gallery_visualize_picmus_dataset.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: visualize_picmus_dataset.ipynb <visualize_picmus_dataset.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: visualize_picmus_dataset.py <visualize_picmus_dataset.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: visualize_picmus_dataset.zip <visualize_picmus_dataset.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
