<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/models/demo_prospective_mri.py" -->
<!-- LINE NUMBERS ARE GIVEN BELOW. -->

#### NOTE
New to DeepInverse? Get started with the basics with the
[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md#sphx-glr-auto-examples-basics-demo-quickstart-py)..

<a id="sphx-glr-auto-examples-models-demo-prospective-mri-py"></a>

# Reconstruct prospectively-undersampled raw multicoil MRI

This example reconstructs real prospectively undersampled multicoil brain k-space from Yu *et al.*<sup>[1](#footcite-yu2022validation)</sup>.

This demonstrates the performance of reconstruction algorithms in a deployment scenario rather than a typical simulated (retrospective) scenario.
Here, it is impossible to compute full-reference metrics, since fully-sampled ground-truth does not exist.

We compare two types of reconstruction:

* Deep learning, using a pretrained general model [`deepinv.models.RAM`](https://deepinv.org/api/stubs/deepinv.models.RAM.html.md#deepinv.models.RAM) from Terris *et al.*<sup>[2](#footcite-terris2025reconstruct)</sup>
* Compressed sensing, using FISTA with a wavelet prior <sup>[3](#footcite-chambolle2015convergence)</sup>

The data is stored in the raw ISMRMRD format.

#### NOTE
This example requires `ismrmrd` to load the data. Install it with `pip install ismrmrd`.

<!-- GENERATED FROM PYTHON SOURCE LINES 22-29 -->
```Python
import torch
import deepinv as dinv

device = dinv.utils.get_device()
```

```none
Selected GPU 0 with 20138.4375 MiB free memory
```

<!-- GENERATED FROM PYTHON SOURCE LINES 31-37 -->

## Load the raw k-space

The data is stored in the ISMRMRD format, as 3D multicoil kspace of shape `(1, 2, N, D, H, W)`.
We inverse-FFT the fully-sampled readout/slice dimension `D` and take the middle slice to obtain a 2D multicoil kspace
for demonstration purposes.

<!-- GENERATED FROM PYTHON SOURCE LINES 37-48 -->
```Python
dinv.datasets.download_archive(
    dinv.utils.get_image_url("t2_space_fs_sag_cs7_iso.h5"),
    dinv.utils.get_cache_home() / "mridata" / "prospective_t2.h5",
)

y = dinv.io.load_ismrmrd_raw(
    dinv.utils.get_cache_home() / "mridata" / "prospective_t2.h5", ifft_slice_dim=True
)
y = y[..., y.shape[-1] // 2, :, :].to(device)
```

```none
File already downloaded: /local/jtachell/.cache/deepinv/mridata/prospective_t2.h5. Skipping...
```

<!-- GENERATED FROM PYTHON SOURCE LINES 49-56 -->

## Build the physics

We recover the prospective sampling mask from the kspace zeros
We also estimate coil maps using ESPIRiT <sup>[4](#footcite-uecker2013espirit)</sup>.
We estimate manually the noise level as `sigma=0.02`. Decreasing it increases the noise in the reconstruction, whereas
increasing it increases the smoothness in the reconstruction.

<!-- GENERATED FROM PYTHON SOURCE LINES 56-69 -->
```Python
mask = (y != 0).any(1).any(1, keepdim=True).float()  # (1, 1, H, W)
coil_maps = dinv.physics.MultiCoilMRI.estimate_coil_maps(
    y, calib_size=24, espirit_crop=0.99
)  # (1, N, H, W) complex

physics = dinv.physics.MultiCoilMRI(
    mask=mask,
    coil_maps=coil_maps,
    device=device,
    noise_model=dinv.physics.GaussianNoise(sigma=0.02),
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 70-73 -->

## Baseline reconstructions

As with any MRI problem, the baselines can be considered to be the zero-filled reconstruction, and the least-squares conjugate-gradient SENSE <sup>[5](#footcite-pruessmann1999sense)</sup>.

<!-- GENERATED FROM PYTHON SOURCE LINES 73-78 -->
```Python
with torch.no_grad():
    x_zf = physics.A_adjoint(y)
    x_sense = physics.A_dagger(y)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 79-92 -->

## Deep learning reconstruction with RAM

Reconstruct Anything Model <sup>[2](#footcite-terris2025reconstruct)</sup> was not trained on multicoil MRI physics, nor on axial knee MRI slices. Here, we therefore
test its generalisability.

#### NOTE
ESPIRiT estimates coil maps with arbitrary phase per pixel, because the phases are unconstrained, leading to low spatial correlation.
Even though RAM is not trained on multicoil MRI, it performs better when the phase maps are also smooth. We use
`physics.phase_correct_maps` to constrain the phases to a smooth map, improving performance.

#### TIP
The `sigma` of the physics noise model controls the denoising strength. Here, we show a few options.

<!-- GENERATED FROM PYTHON SOURCE LINES 92-113 -->
```Python
model = dinv.models.RAM(device=device, pretrained=True)

with torch.no_grad():
    coil_maps = physics.phase_correct_maps(x_zf)
    physics.update(coil_maps=coil_maps)

    # Although RAM is scale-equivariant, we bring y into friendlier scale (currently it's very small)
    x_ram = model(y / x_zf.max(), physics) * x_zf.max()

    physics.update(sigma=0.04)
    x_ram_high = model(y / x_zf.max(), physics) * x_zf.max()

    physics.update(sigma=0.005)
    x_ram_low = model(y / x_zf.max(), physics) * x_zf.max()

dinv.utils.plot(
    [x_ram_low, x_ram, x_ram_high],
    titles=["RAM sigma=0.005", "RAM sigma=0.02", "RAM sigma=0.04"],
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 114-121 -->

## Compressed sensing reconstruction

We compare to compressed sensing reconstruction using the FISTA algorithm with a wavelet prior and L2 data fidelity.

#### TIP
The `lambda_reg` of the regularisation controls the regularisation strength. Here, we choose 2e-5.
We choose to use max 50 iterations for the demo. In practice, increase this to run to convergence.

<!-- GENERATED FROM PYTHON SOURCE LINES 121-145 -->
```Python
prior = dinv.optim.WaveletPrior(
    level=3,
    wv=["db1", "db2", "db3", "db4", "db5", "db6", "db7", "db8"],
    p=1,
    device="cpu",
    clamp_min=0,
)

model = dinv.optim.FISTA(
    prior=prior,
    data_fidelity=dinv.optim.L2(),
    stepsize=0.1,
    lambda_reg=2e-5,
    early_stop=True,
    max_iter=50,
    verbose=True,
    custom_init=lambda y, physics: (physics.A_dagger(y), physics.A_dagger(y)),
    show_progress_bar=True,
)

x_fista = model(y, physics)
```

```none
  0%|          | 0/50 [00:00<?, ?it/s]
  2%|▏         | 1/50 [00:00<00:07,  6.61it/s]
  4%|▍         | 2/50 [00:00<00:06,  7.86it/s]
  6%|▌         | 3/50 [00:00<00:05,  8.37it/s]
  8%|▊         | 4/50 [00:00<00:05,  8.62it/s]
 10%|█         | 5/50 [00:00<00:05,  8.78it/s]
 12%|█▏        | 6/50 [00:00<00:04,  8.88it/s]
 14%|█▍        | 7/50 [00:00<00:04,  8.95it/s]
 16%|█▌        | 8/50 [00:00<00:04,  8.99it/s]
 18%|█▊        | 9/50 [00:01<00:04,  9.03it/s]
 20%|██        | 10/50 [00:01<00:04,  9.05it/s]
 22%|██▏       | 11/50 [00:01<00:04,  9.05it/s]
 24%|██▍       | 12/50 [00:01<00:04,  9.06it/s]
 26%|██▌       | 13/50 [00:01<00:04,  9.07it/s]
 28%|██▊       | 14/50 [00:01<00:03,  9.08it/s]
 30%|███       | 15/50 [00:01<00:03,  9.08it/s]
 32%|███▏      | 16/50 [00:01<00:03,  9.08it/s]
 34%|███▍      | 17/50 [00:01<00:03,  9.07it/s]
 36%|███▌      | 18/50 [00:02<00:03,  9.08it/s]
 38%|███▊      | 19/50 [00:02<00:03,  9.08it/s]
 40%|████      | 20/50 [00:02<00:03,  9.08it/s]
 42%|████▏     | 21/50 [00:02<00:03,  9.08it/s]
 44%|████▍     | 22/50 [00:02<00:03,  9.07it/s]
 46%|████▌     | 23/50 [00:02<00:02,  9.07it/s]
 48%|████▊     | 24/50 [00:02<00:02,  9.08it/s]
 50%|█████     | 25/50 [00:02<00:02,  9.07it/s]
 52%|█████▏    | 26/50 [00:02<00:02,  9.06it/s]
 54%|█████▍    | 27/50 [00:03<00:02,  9.07it/s]
 56%|█████▌    | 28/50 [00:03<00:02,  9.07it/s]
 58%|█████▊    | 29/50 [00:03<00:02,  9.08it/s]
 60%|██████    | 30/50 [00:03<00:02,  9.08it/s]
 62%|██████▏   | 31/50 [00:03<00:02,  9.08it/s]
 64%|██████▍   | 32/50 [00:03<00:01,  9.08it/s]
 66%|██████▌   | 33/50 [00:03<00:01,  9.09it/s]
 68%|██████▊   | 34/50 [00:03<00:01,  9.05it/s]
 70%|███████   | 35/50 [00:03<00:01,  8.99it/s]
 72%|███████▏  | 36/50 [00:04<00:01,  9.02it/s]
 74%|███████▍  | 37/50 [00:04<00:01,  9.04it/s]
 76%|███████▌  | 38/50 [00:04<00:01,  9.05it/s]
 78%|███████▊  | 39/50 [00:04<00:01,  9.06it/s]
 80%|████████  | 40/50 [00:04<00:01,  9.06it/s]
 82%|████████▏ | 41/50 [00:04<00:00,  9.07it/s]
 84%|████████▍ | 42/50 [00:04<00:00,  9.08it/s]
 86%|████████▌ | 43/50 [00:04<00:00,  9.09it/s]
 88%|████████▊ | 44/50 [00:04<00:00,  9.08it/s]
 90%|█████████ | 45/50 [00:05<00:00,  9.08it/s]
 92%|█████████▏| 46/50 [00:05<00:00,  9.08it/s]
 94%|█████████▍| 47/50 [00:05<00:00,  9.06it/s]
 96%|█████████▌| 48/50 [00:05<00:00,  9.06it/s]
 98%|█████████▊| 49/50 [00:05<00:00,  9.06it/s]
100%|██████████| 50/50 [00:05<00:00,  9.05it/s]
100%|██████████| 50/50 [00:05<00:00,  9.00it/s]
```

<!-- GENERATED FROM PYTHON SOURCE LINES 146-147 -->

Plot the final comparison between methods:

<!-- GENERATED FROM PYTHON SOURCE LINES 147-153 -->
```Python
dinv.utils.plot(
    [x_zf, x_sense, x_ram, x_fista],
    titles=["Zero-filled", "SENSE", "RAM", "FISTA+wavelets"],
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 154-157 -->
* **References:**

* <a id='footcite-yu2022validation'>**[1]**</a> Thomas Yu, Tom Hilbert, Gian Franco Piredda, Arun Joseph, Gabriele Bonanno, Salim Zenkhri, Patrick Omoumi, Meritxell Bach Cuadra, Erick Canales Rodriguez, Tobias Kober, and Jean-Philippe Thiran. Validation and generalizability of self-supervised image reconstruction methods for undersampled mri. *Machine Learning for Biomedical Imaging*, 1(September 2022 issue):1–31, September 2022.
* <a id='footcite-terris2025reconstruct'>**[2]**</a> Matthieu Terris, Samuel Hurault, Maxime Song, and Julián Tachella. Reconstruct anything model: a lightweight foundation model for computational imaging. *arXiv preprint arXiv:2503.08915*, 2025.
* <a id='footcite-chambolle2015convergence'>**[3]**</a> Antonin Chambolle and Charles H Dossal. On the convergence of the iterates of” fista”. *Journal of Optimization Theory and Applications*, 166(3):25, 2015.
* <a id='footcite-uecker2013espirit'>**[4]**</a> Martin Uecker, Peng Lai, Mark Murphy, Patrick Virtue, Michael Elad, John Pauly, Shreyas Vasanawala, and Michael Lustig. Espirit — an eigenvalue approach to autocalibrating parallel mri: where sense meets grappa. *Magnetic Resonance in Medicine*, May 2013. [doi:10.1002/mrm.24751](https://doi.org/10.1002/mrm.24751).
* <a id='footcite-pruessmann1999sense'>**[5]**</a> Klaas Pruessmann, Markus Weiger, Markus Scheidegger, and Peter Boesiger. Sense: sensitivity encoding for fast mri. *Magnetic Resonance in Medicine*, 1999. [doi:10.1002/(SICI)1522-2594(199911)42:5%3C952::AID-MRM16%3E3.0.CO;2-S](https://doi.org/10.1002/(SICI)1522-2594(199911)42:5%3C952::AID-MRM16%3E3.0.CO;2-S).

**Total running time of the script:** (2 minutes 11.957 seconds)

🏷 Tags: MRI

<a id="sphx-glr-download-auto-examples-models-demo-prospective-mri-py"></a>

[`Download Jupyter notebook: demo_prospective_mri.ipynb`](https://deepinv.org/auto_examples/models/_downloads/7ec3e981a83e1c7cd7c32b49f575d439/demo_prospective_mri.ipynb)

[`Download Python source code: demo_prospective_mri.py`](https://deepinv.org/auto_examples/models/_downloads/f07553f901b39b4641fa3143df1f8f79/demo_prospective_mri.py)

[`Download zipped: demo_prospective_mri.zip`](https://deepinv.org/auto_examples/models/_downloads/0944d1b0a7d125f9aa00f82f2d972f6c/demo_prospective_mri.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
