.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/external-libraries/demo_io.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note New to DeepInverse? Get started with the basics with the :ref:`5 minute quickstart tutorial `. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_external-libraries_demo_io.py: Loading scientific images ========================= This example presents the various input/output functions provided by DeepInverse for handling medical and scientific imaging formats. We demonstrate loading and plotting from DICOM, NIfTI, ISMRMRD, PyTorch, NumPy and raster data sources. .. GENERATED FROM PYTHON SOURCE LINES 12-14 .. code-block:: Python import deepinv as dinv .. GENERATED FROM PYTHON SOURCE LINES 15-24 1. Load DICOM ~~~~~~~~~~~~~ DICOM files are commonly used in clinical imaging. We can fetch and load them directly using :func:`deepinv.utils.load_dicom`. Here, we demonstrate on a demo DICOM from the Internet. This function requires `pydicom`. Install it with `pip install pydicom`. .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: Python x = dinv.io.load_dicom( dinv.io.load_url( "https://github.com/robyoung/dicom-test-files/raw/refs/heads/master/data/pydicom/693_J2KI.dcm" ) ) # (1,H,W) dinv.utils.plot({"dicom": x.unsqueeze(0)}) .. image-sg:: /auto_examples/external-libraries/images/sphx_glr_demo_io_001.png :alt: dicom :srcset: /auto_examples/external-libraries/images/sphx_glr_demo_io_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 34-41 2. Load NIfTI ~~~~~~~~~~~~~ NIfTI files are often used for volumetric neuroimaging data. We load them using :func:`deepinv.utils.load_nifti`. Since `nibabel` doesn't support file buffers, we download a sample volume first. This function requires `nibabel`. Install it with `pip install nibabel`. .. GENERATED FROM PYTHON SOURCE LINES 41-58 .. code-block:: Python import requests save_path = dinv.utils.get_data_home() / "tmp.nii.gz" with open(save_path, "wb") as f: f.write( requests.get( "https://github.com/neurolabusc/niivue-images/raw/refs/heads/main/Iguana.nii.gz" ).content ) x = dinv.io.load_nifti(save_path).unsqueeze(0) # (1, H, W, D) x = x[..., x.shape[-1] // 2] # take central slice, (1,H,W) dinv.utils.plot({"nifti slice": x.unsqueeze(0)}) .. image-sg:: /auto_examples/external-libraries/images/sphx_glr_demo_io_002.png :alt: nifti slice :srcset: /auto_examples/external-libraries/images/sphx_glr_demo_io_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-64 3. Load ISMRMRD ~~~~~~~~~~~~~~~ ISMRMRD is a standard raw MRI k-space format. We load a k-space slice using :func:`deepinv.utils.load_ismrmd` and compute a root-sum-of-squares reconstruction. .. GENERATED FROM PYTHON SOURCE LINES 64-77 .. code-block:: Python y = dinv.io.load_ismrmd( dinv.io.load_url(dinv.utils.demo.get_image_url("demo_fastmri_brain_multicoil.h5")), data_name="kspace", data_slice=0, ) # (2,N,H,W) where N is coils rss = dinv.physics.MultiCoilMRI(img_size=y.shape[-2:]).A_adjoint( y.unsqueeze(0), rss=True ) # (1,2,H,W) dinv.utils.plot({"ismrmrd rss": rss}) .. image-sg:: /auto_examples/external-libraries/images/sphx_glr_demo_io_003.png :alt: ismrmrd rss :srcset: /auto_examples/external-libraries/images/sphx_glr_demo_io_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-83 4. Load tensors and arrays ~~~~~~~~~~~~~~~~~~~~~~~~~~ We provide helper functions for loading tensors and arrays directly, for example provided in the `DeepInverse HuggingFace demo repository `. .. GENERATED FROM PYTHON SOURCE LINES 83-96 .. code-block:: Python x = dinv.io.load_torch( dinv.utils.load_url(dinv.utils.demo.get_image_url("CT100_256x256_0.pt")) ) # (1,1,H,W) y = dinv.io.load_np( dinv.utils.load_url( dinv.utils.demo.get_image_url("brainweb_t1_ICBM_1mm_subject_0_slice_0.npy") ) ) # (H,W) dinv.utils.plot({"torch": x, "numpy": y.unsqueeze(0).unsqueeze(0)}) .. image-sg:: /auto_examples/external-libraries/images/sphx_glr_demo_io_004.png :alt: torch, numpy :srcset: /auto_examples/external-libraries/images/sphx_glr_demo_io_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-104 6. Load raster images ~~~~~~~~~~~~~~~~~~~~~ Raster formats (e.g. GeoTIFF, COSAR) can be streamed with patches. See :func:`deepinv.utils.load_raster` for patch options. This function requires `rasterio`. Install it with `pip install rasterio`. .. GENERATED FROM PYTHON SOURCE LINES 104-116 .. code-block:: Python patches = dinv.io.load_raster( dinv.io.load_url( "https://download.osgeo.org/geotiff/samples/spot/chicago/SP27GTIF.TIF" ), patch=500, patch_start=(200, 200), ) x = next(patches) # Stream patch dinv.utils.plot({"raster": x.unsqueeze(0)}) .. image-sg:: /auto_examples/external-libraries/images/sphx_glr_demo_io_005.png :alt: raster :srcset: /auto_examples/external-libraries/images/sphx_glr_demo_io_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.646 seconds) .. _sphx_glr_download_auto_examples_external-libraries_demo_io.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_io.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_io.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_io.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_