Skip to content

MRI Reconstruction

This guide demonstrates how to perform MRI reconstruction using torchlinops with three key components:

  • NUFFT: Non-uniform Fast Fourier Transform for radial sampling
  • Dense operator: Multi-coil sensitivity encoding
  • Diagonal operator: Density compensation for radial trajectories

Problem Formulation

The MRI forward model can be written as:

\[y = E F S x + n\]

Where:

  • \(S\): coil sensitivities (Dense operator)
  • \(F\): NUFFT (non-uniform FFT)
  • \(E\): density compensation (Diagonal operator)
  • \(x\): image to reconstruct
  • \(y\): acquired k-space data
  • \(n\): noise

The reconstruction solves the least-squares problem:

\[\min_x \|A x - y\|_2^2\]

where \(A = E F S\) is the combined forward operator.

Setup and Imports

import sys
import warnings

import marimo as mo
import matplotlib.pyplot as plt
import torch

from torchlinops import NUFFT, Dense, Diagonal, Dim
from torchlinops.alg import conjugate_gradients, power_method

# Set random seed for reproducibility
torch.manual_seed(42)

# Ignore warnings
warnings.filterwarnings("ignore", message=r".*FigureCanvasAgg.*")

# Reconstruction parameters
image_size = (128, 128)  # 2D image dimensions
num_coils = 8  # Number of receiver coils
num_spokes = 37  # Number of radial spokes
num_readouts = 256  # Readout points per spoke
noise_level = 1e-5  # Additive noise level
use_cartesian = False  # Use Cartesian trajectory for debugging

Helper Functions

We define the helper functions here for generating trajectories, sensitivities, and density compensation weights.

def generate_radial_trajectory(num_spokes, num_readouts, grid_size):
    """Generate radial k-space trajectory.

    Parameters
    ----------
    num_spokes : int
        Number of radial spokes
    num_readouts : int
        Number of readout points per spoke
    grid_size : tuple
        Image grid size (Nx, Ny)

    Returns
    -------
    torch.Tensor
        K-space locations with shape (num_spokes * num_readouts, 2)
    """
    Nx, Ny = grid_size
    max_radius = min(Nx, Ny) // 2
    angles = torch.linspace(0, 2 * torch.pi, num_spokes + 1)[:-1]
    readout_pos = torch.linspace(-max_radius, max_radius, num_readouts)
    locs = torch.zeros(num_spokes * num_readouts, 2)
    for _i, angle in enumerate(angles):
        start_idx = _i * num_readouts
        end_idx = (_i + 1) * num_readouts
        x = readout_pos * torch.cos(angle)
        y = readout_pos * torch.sin(angle)
        locs[start_idx:end_idx, 0] = x
        locs[start_idx:end_idx, 1] = y
    locs = locs / (2 * max_radius)
    return locs

def generate_cartesian_trajectory(grid_size, oversamp=2.0):
    """Generate Cartesian k-space trajectory.

    Parameters
    ----------
    grid_size : tuple
        Image grid size (Nx, Ny)

    Returns
    -------
    torch.Tensor
        K-space locations with shape (Nx * Ny, 2)
    """
    Nx, Ny = grid_size
    x = torch.linspace(-0.5, 0.5, int(Nx * oversamp))
    y = torch.linspace(-0.5, 0.5, int(Ny * oversamp))
    X, Y = torch.meshgrid(x, y, indexing="ij")
    locs = torch.stack([X.flatten(), Y.flatten()], dim=1)
    return locs

def generate_gaussian_coil_sensitivities(grid_size, num_coils):
    """Generate Gaussian coil sensitivity maps.

    Parameters
    ----------
    grid_size : tuple
        Image grid size (Nx, Ny)
    num_coils : int
        Number of receiver coils

    Returns
    -------
    torch.Tensor
        Coil sensitivity maps with shape (num_coils, Nx, Ny)
    """
    Nx, Ny = grid_size
    x = torch.linspace(-0.5, 0.5, Nx)
    y = torch.linspace(-0.5, 0.5, Ny)
    X, Y = torch.meshgrid(x, y, indexing="ij")
    coil_sens = torch.zeros(num_coils, Nx, Ny, dtype=torch.complex64)
    for c in range(num_coils):
        angle = 2 * torch.pi * c / num_coils
        center_x = 0.3 * torch.cos(torch.tensor(angle))
        center_y = 0.3 * torch.sin(torch.tensor(angle))
        sigma = 1.0
        gaussian = torch.exp(
            -((X - center_x) ** 2 + (Y - center_y) ** 2) / (2 * sigma**2)
        )
        phase = torch.exp(1j * 2 * torch.pi * (0.1 * X + 0.1 * Y))
        coil_sens[c] = gaussian * phase
    return coil_sens

def analytic_radial_dcf(locs):
    """Compute analytic density compensation for radial trajectories.

    Parameters
    ----------
    locs : torch.Tensor
        K-space locations with shape (N, 2)

    Returns
    -------
    torch.Tensor
        Density compensation weights with shape (N,)
    """
    radius = torch.sqrt(locs[:, 0] ** 2 + locs[:, 1] ** 2)
    dcf = radius + 1e-06
    dcf = dcf / torch.mean(dcf)
    return dcf

def analytic_cartesian_dcf(locs):
    """Compute analytic density compensation for Cartesian trajectory.

    Parameters
    ----------
    locs : torch.Tensor
        K-space locations

    Returns
    -------
    torch.Tensor
        Density compensation weights (all ones)
    """
    return torch.ones(locs.shape[0])

def exact_dft(image, locs):
    """Compute exact Discrete Fourier Transform.

    Parameters
    ----------
    image : torch.Tensor
        Input image with shape (..., Nx, Ny)
    locs : torch.Tensor
        K-space locations with shape (Nk, 2) in [-0.5, 0.5]

    Returns
    -------
    torch.Tensor
        K-space data with shape (..., Nk)
    """
    *batch_dims, Nx, Ny = image.shape
    Nk = locs.shape[0]
    image_flat = image.reshape(*batch_dims, -1)
    x = torch.linspace(-0.5, 0.5, Nx, device=image.device)
    y = torch.linspace(-0.5, 0.5, Ny, device=image.device)
    X, Y = torch.meshgrid(x, y, indexing="ij")
    r = torch.stack([X.flatten(), Y.flatten()], dim=1)
    r = r.to(image.dtype)
    kspace_data = torch.zeros(
        *batch_dims, Nk, dtype=image.dtype, device=image.device
    )
    chunk_size = 4096
    for _i in range(0, Nk, chunk_size):
        end = min(_i + chunk_size, Nk)
        k_chunk = locs[_i:end]
        phase = -2j * torch.pi * (k_chunk.to(image.dtype) @ r.T)
        fourier_kernel = torch.exp(phase)
        kspace_chunk = image_flat @ fourier_kernel.T
        kspace_data[..., _i:end] = kspace_chunk
    return kspace_data

Generate Ground Truth Image

We create a simple circular phantom image for testing.

Nx, Ny = image_size
# Create a simple circular phantom image
x_true = torch.zeros(Nx, Ny, dtype=torch.complex64)
center_x, center_y = (Nx // 2, Ny // 2)
for _i in range(Nx):
    for j in range(Ny):
        if (_i - center_x) ** 2 + (j - center_y) ** 2 < 33**2:
            x_true[_i, j] = 1.0
print(f"Ground truth image shape: {x_true.shape}")
plt.figure(figsize=(6, 6))
plt.imshow(torch.abs(x_true), cmap="gray", vmin=0, vmax=1)
plt.title("Ground Truth Phantom (Magnitude)")
plt.colorbar(label="Signal Intensity")
plt.axis("off")
plt.show()

Output:

Ground truth image shape: torch.Size([128, 128])

output

Component 1: Radial Trajectory and NUFFT

Generate k-space trajectory and create the NUFFT operator for non-Cartesian sampling.

# Generate k-space trajectory
if use_cartesian:
    locs = generate_cartesian_trajectory(image_size)
    print(f"Trajectory shape (Cartesian): {locs.shape}")
else:
    locs = generate_radial_trajectory(num_spokes, num_readouts, image_size)
    print(f"Trajectory shape (Radial): {locs.shape}")

# Visualize Trajectory
plt.figure(figsize=(6, 6))
plt.scatter(locs[:, 0], locs[:, 1], s=0.5, alpha=0.5)
if use_cartesian:
    plt.title(f"Cartesian K-space Trajectory\n{len(locs)} points")
else:
    plt.title(
        f"Radial K-space Trajectory\n{num_spokes} spokes, {num_readouts} readouts"
    )
plt.xlabel("kx (normalized)")
plt.ylabel("ky (normalized)")
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.grid(True, alpha=0.3)
plt.show()

# Scale locs to -N//2, N//2 for NUFFT
locs = locs * torch.tensor(image_size, dtype=locs.dtype)

# Create NUFFT operator
nufft_op = NUFFT(
    locs=locs,
    grid_size=image_size,
    output_shape=Dim("R"),
    input_shape=Dim("NxNy"),
    batch_shape=Dim("C"),
    oversamp=2.0,
    width=6.0,
)

Output:

Trajectory shape (Radial): torch.Size([9472, 2])

output

Component 2: Coil Sensitivities (Dense Operator)

Generate multi-coil sensitivity maps and create the coil encoding operator.

coil_sens = generate_gaussian_coil_sensitivities(image_size, num_coils)
print(f"Coil sensitivities shape: {coil_sens.shape}")
plt.figure(figsize=(15, 3))
for _i in range(min(4, num_coils)):
    plt.subplot(1, 4, _i + 1)
    plt.imshow(torch.abs(coil_sens[_i]), cmap="jet")
    plt.title(f"Coil {_i + 1} Sensitivity")
    plt.axis("off")
plt.suptitle("Coil Sensitivity Maps (Magnitude - First 4 Coils)")
plt.tight_layout()
plt.show()

# Create Dense operator for coil encoding
coil_op = Dense(
    weight=coil_sens,
    weightshape=Dim("CNxNy"),
    ishape=Dim("NxNy"),
    oshape=Dim("CNxNy"),
)

Output:

Coil sensitivities shape: torch.Size([8, 128, 128])

output

Component 3: Density Compensation (Diagonal Operator)

Compute density compensation weights for radial trajectories.

# Compute analytic density compensation
if use_cartesian:
    dcf_weights = analytic_cartesian_dcf(locs)
else:
    dcf_weights = analytic_radial_dcf(locs)

print(f"Density weights shape: {dcf_weights.shape}")

# Visualize Density Compensation Weights
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.scatter(locs[:, 0], locs[:, 1], c=dcf_weights, s=1, cmap="viridis")
plt.title("DCF Weights in K-space")
plt.colorbar(label="Weight")
plt.axis("equal")

plt.subplot(1, 2, 2)
radius = torch.sqrt(locs[:, 0] ** 2 + locs[:, 1] ** 2)
plt.scatter(radius, dcf_weights, s=1, alpha=0.5)
plt.title("DCF Weights vs Radius")
plt.xlabel("Radius (normalized)")
plt.ylabel("Weight")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

# Create Diagonal operator for density compensation
density_op = Diagonal(
    weight=dcf_weights,
    ioshape=Dim("CR"),
)

Output:

Density weights shape: torch.Size([9472])

output

Complete Forward Model

Simulate k-space data from ground truth image using exact DFT.

# Simulate k-space data from ground truth image
multi_coil_image = coil_op(x_true)
print("Simulating forward model using exact DFT...")
kspace_data = exact_dft(multi_coil_image, locs)

# Add realistic noise
noise = torch.randn_like(kspace_data) * noise_level
kspace_noisy = kspace_data + noise

print(f"Forward model: {x_true.shape} -> {kspace_noisy.shape}")
print(
    f"Data SNR estimate: {20 * torch.log10(torch.norm(kspace_data) / torch.norm(noise)):.1f} dB"
)

Output:

Simulating forward model using exact DFT...
Forward model: torch.Size([128, 128]) -> torch.Size([8, 9472])
Data SNR estimate: 152.2 dB

Iterative Reconstruction

Solve the least-squares problem using conjugate gradients.

# Combine all operators: A = density_comp @ nufft @ coil_sens
A = (density_op ** (1 / 2)) @ nufft_op @ coil_op

# Normalize for numerical purposes
_, eigenval = power_method(
    A.N, torch.ones_like(x_true), tqdm_kwargs={"leave": False}
)
A = ((1 / (1.01 * eigenval)) ** 0.5) * A

# Apply density compensation to y
y = (density_op ** (1 / 2))(kspace_noisy)

# Compute RHS: A^H y
rhs = A.H(y)
rhs = rhs / torch.linalg.vector_norm(rhs)

plt.figure(figsize=(4, 4))
plt.imshow(torch.abs(rhs).cpu())
plt.title("adjoint reconstruction")
plt.show()

# Solve A^H A x = A^H y using conjugate gradients
x_recon = conjugate_gradients(
    A=A.N, y=rhs, max_num_iters=50, gtol=1e-4, tqdm_kwargs={"leave": False}
)

# Rescale recon to scale of x_true
if x_recon is not None:
    x_recon = x_recon * torch.max(x_true.abs()) / torch.max(x_recon.abs())
else:
    print(f"Reconstruction diverged.")
    sys.exit()

print(f"Reconstruction shape: {x_recon.shape}")
print(f"Ground truth shape: {x_true.shape}")

# Compute reconstruction error
recon_error = torch.norm(x_recon - x_true) / torch.norm(x_true)
print(f"Relative reconstruction error: {recon_error:.4f}")

# Visualize Reconstruction
plt.figure(figsize=(15, 5))

# Ground Truth
plt.subplot(1, 3, 1)
plt.imshow(torch.abs(x_true), cmap="gray", vmin=0, vmax=1)
plt.title("Ground Truth")
plt.axis("off")

# Reconstruction
plt.subplot(1, 3, 2)
plt.imshow(torch.abs(x_recon), cmap="gray", vmin=0, vmax=1)
plt.title(f"Reconstruction\n(Error: {recon_error:.4f})")
plt.axis("off")

# Error Map
plt.subplot(1, 3, 3)
error_map = torch.abs(x_true - x_recon)
plt.imshow(error_map, cmap="inferno")
plt.title("Error Map (|x_true - x_recon|)")
plt.colorbar(label="Error Magnitude")
plt.axis("off")

plt.tight_layout()
plt.show()

Warnings/Errors

Power Method:   0%|          | 0/50 [00:00<?, ?it/s]
Power Method:   0%|          | 0/50 [00:18<?, ?it/s, rdiff=0.961, e_val=4.99]
Power Method:   2%|▏         | 1/50 [00:18<14:49, 18.16s/it, rdiff=0.961, e_val=4.99]
Power Method:   2%|▏         | 1/50 [00:18<14:49, 18.16s/it, rdiff=0.0621, e_val=5.3]
Power Method:   2%|▏         | 1/50 [00:18<14:49, 18.16s/it, rdiff=0.364, e_val=7.23]
Power Method:   6%|▌         | 3/50 [00:18<03:44,  4.78s/it, rdiff=0.364, e_val=7.23]
Power Method:   6%|▌         | 3/50 [00:18<03:44,  4.78s/it, rdiff=1.37, e_val=17.2] 
Power Method:   8%|▊         | 4/50 [00:18<02:25,  3.16s/it, rdiff=1.37, e_val=17.2]
Power Method:   8%|▊         | 4/50 [00:18<02:25,  3.16s/it, rdiff=0.76, e_val=30.2]
Power Method:  10%|█         | 5/50 [00:18<01:36,  2.15s/it, rdiff=0.76, e_val=30.2]
Power Method:  10%|█         | 5/50 [00:18<01:36,  2.15s/it, rdiff=0.179, e_val=35.6]
Power Method:  12%|█▏        | 6/50 [00:18<01:05,  1.50s/it, rdiff=0.179, e_val=35.6]
Power Method:  12%|█▏        | 6/50 [00:18<01:05,  1.50s/it, rdiff=0.0457, e_val=37.2]
Power Method:  14%|█▍        | 7/50 [00:18<00:45,  1.06s/it, rdiff=0.0457, e_val=37.2]
Power Method:  14%|█▍        | 7/50 [00:18<00:45,  1.06s/it, rdiff=0.0124, e_val=37.7]
Power Method:  16%|█▌        | 8/50 [00:18<00:31,  1.32it/s, rdiff=0.0124, e_val=37.7]
Power Method:  16%|█▌        | 8/50 [00:18<00:31,  1.32it/s, rdiff=0.00367, e_val=37.8]
Power Method:  18%|█▊        | 9/50 [00:18<00:22,  1.79it/s, rdiff=0.00367, e_val=37.8]
Power Method:  18%|█▊        | 9/50 [00:19<00:22,  1.79it/s, rdiff=0.00124, e_val=37.9]
Power Method:  18%|█▊        | 9/50 [00:19<00:22,  1.79it/s, rdiff=0.000483, e_val=37.9]
Power Method:  22%|██▏       | 11/50 [00:19<00:13,  2.90it/s, rdiff=0.000483, e_val=37.9]
Power Method:  22%|██▏       | 11/50 [00:19<00:13,  2.90it/s, rdiff=0.000211, e_val=37.9]
Power Method:  24%|██▍       | 12/50 [00:19<00:10,  3.53it/s, rdiff=0.000211, e_val=37.9]
Power Method:  24%|██▍       | 12/50 [00:19<00:10,  3.53it/s, rdiff=0.000115, e_val=37.9]
Power Method:  24%|██▍       | 12/50 [00:19<00:10,  3.53it/s, rdiff=5.96e-5, e_val=37.9] 
Power Method:  28%|██▊       | 14/50 [00:19<00:07,  4.83it/s, rdiff=5.96e-5, e_val=37.9]
Power Method:  28%|██▊       | 14/50 [00:19<00:07,  4.83it/s, rdiff=3.73e-5, e_val=37.9]
Power Method:  30%|███       | 15/50 [00:19<00:06,  5.48it/s, rdiff=3.73e-5, e_val=37.9]
Power Method:  30%|███       | 15/50 [00:19<00:06,  5.48it/s, rdiff=2.75e-5, e_val=37.9]
Power Method:  32%|███▏      | 16/50 [00:19<00:05,  6.13it/s, rdiff=2.75e-5, e_val=37.9]
Power Method:  32%|███▏      | 16/50 [00:19<00:05,  6.13it/s, rdiff=1.31e-5, e_val=37.9]
Power Method:  34%|███▍      | 17/50 [00:19<00:04,  6.73it/s, rdiff=1.31e-5, e_val=37.9]
Power Method:  34%|███▍      | 17/50 [00:19<00:04,  6.73it/s, rdiff=1.54e-5, e_val=37.9]
Power Method:  36%|███▌      | 18/50 [00:19<00:04,  7.35it/s, rdiff=1.54e-5, e_val=37.9]
Power Method:  36%|███▌      | 18/50 [00:19<00:04,  7.35it/s, rdiff=7.14e-6, e_val=37.9]

output

Warnings/Errors

CG:   0%|          | 0/50 [00:00<?, ?it/s]
CG:   0%|          | 0/50 [00:00<?, ?it/s, ldiff=6.6, gnorm=0.484]
CG:   2%|▏         | 1/50 [00:00<00:09,  5.03it/s, ldiff=6.6, gnorm=0.484]
CG:   2%|▏         | 1/50 [00:00<00:09,  5.03it/s, ldiff=0.437, gnorm=0.121]
CG:   4%|▍         | 2/50 [00:00<00:09,  4.96it/s, ldiff=0.437, gnorm=0.121]
CG:   4%|▍         | 2/50 [00:00<00:09,  4.96it/s, ldiff=0.0308, gnorm=0.0396]
CG:   6%|▌         | 3/50 [00:00<00:09,  4.94it/s, ldiff=0.0308, gnorm=0.0396]
CG:   6%|▌         | 3/50 [00:00<00:09,  4.94it/s, ldiff=0.00334, gnorm=0.0179]
CG:   8%|▊         | 4/50 [00:00<00:09,  5.00it/s, ldiff=0.00334, gnorm=0.0179]
CG:   8%|▊         | 4/50 [00:01<00:09,  5.00it/s, ldiff=0.000992, gnorm=0.0121]
CG:  10%|█         | 5/50 [00:01<00:09,  4.91it/s, ldiff=0.000992, gnorm=0.0121]
CG:  10%|█         | 5/50 [00:01<00:09,  4.91it/s, ldiff=0.000344, gnorm=0.00626]
CG:  12%|█▏        | 6/50 [00:01<00:09,  4.56it/s, ldiff=0.000344, gnorm=0.00626]
CG:  12%|█▏        | 6/50 [00:01<00:09,  4.56it/s, ldiff=7.87e-5, gnorm=0.00324] 
CG:  14%|█▍        | 7/50 [00:01<00:09,  4.69it/s, ldiff=7.87e-5, gnorm=0.00324]
CG:  14%|█▍        | 7/50 [00:01<00:09,  4.69it/s, ldiff=2.62e-5, gnorm=0.00206]
CG:  16%|█▌        | 8/50 [00:01<00:08,  4.81it/s, ldiff=2.62e-5, gnorm=0.00206]
CG:  16%|█▌        | 8/50 [00:01<00:08,  4.81it/s, ldiff=1.48e-5, gnorm=0.00138]
CG:  18%|█▊        | 9/50 [00:01<00:08,  4.87it/s, ldiff=1.48e-5, gnorm=0.00138]
CG:  18%|█▊        | 9/50 [00:02<00:08,  4.87it/s, ldiff=1e-5, gnorm=0.00171]   
CG:  20%|██        | 10/50 [00:02<00:08,  4.93it/s, ldiff=1e-5, gnorm=0.00171]
CG:  20%|██        | 10/50 [00:02<00:08,  4.93it/s, ldiff=1.24e-5, gnorm=0.00138]
CG:  22%|██▏       | 11/50 [00:02<00:07,  4.96it/s, ldiff=1.24e-5, gnorm=0.00138]
CG:  22%|██▏       | 11/50 [00:02<00:07,  4.96it/s, ldiff=7.63e-6, gnorm=0.00126]
CG:  24%|██▍       | 12/50 [00:02<00:07,  4.97it/s, ldiff=7.63e-6, gnorm=0.00126]
CG:  24%|██▍       | 12/50 [00:02<00:07,  4.97it/s, ldiff=5.72e-6, gnorm=0.00122]
CG:  26%|██▌       | 13/50 [00:02<00:07,  5.00it/s, ldiff=5.72e-6, gnorm=0.00122]
CG:  26%|██▌       | 13/50 [00:02<00:07,  5.00it/s, ldiff=5.72e-6, gnorm=0.00122]
CG:  28%|██▊       | 14/50 [00:02<00:07,  5.03it/s, ldiff=5.72e-6, gnorm=0.00122]
CG:  28%|██▊       | 14/50 [00:03<00:07,  5.03it/s, ldiff=4.77e-6, gnorm=0.00114]
CG:  30%|███       | 15/50 [00:03<00:06,  5.05it/s, ldiff=4.77e-6, gnorm=0.00114]
CG:  30%|███       | 15/50 [00:03<00:06,  5.05it/s, ldiff=6.2e-6, gnorm=0.00117] 
CG:  32%|███▏      | 16/50 [00:03<00:06,  5.04it/s, ldiff=6.2e-6, gnorm=0.00117]
CG:  32%|███▏      | 16/50 [00:03<00:06,  5.04it/s, ldiff=6.2e-6, gnorm=0.00126]
CG:  34%|███▍      | 17/50 [00:03<00:06,  5.00it/s, ldiff=6.2e-6, gnorm=0.00126]
CG:  34%|███▍      | 17/50 [00:03<00:06,  5.00it/s, ldiff=7.15e-6, gnorm=0.00127]
CG:  36%|███▌      | 18/50 [00:03<00:06,  5.00it/s, ldiff=7.15e-6, gnorm=0.00127]
CG:  36%|███▌      | 18/50 [00:03<00:06,  5.00it/s, ldiff=6.2e-6, gnorm=0.00113] 
CG:  38%|███▊      | 19/50 [00:03<00:06,  5.03it/s, ldiff=6.2e-6, gnorm=0.00113]
CG:  38%|███▊      | 19/50 [00:04<00:06,  5.03it/s, ldiff=8.58e-6, gnorm=0.00109]
CG:  40%|████      | 20/50 [00:04<00:05,  5.06it/s, ldiff=8.58e-6, gnorm=0.00109]
CG:  40%|████      | 20/50 [00:04<00:05,  5.06it/s, ldiff=3.34e-6, gnorm=0.0011] 
CG:  42%|████▏     | 21/50 [00:04<00:05,  5.06it/s, ldiff=3.34e-6, gnorm=0.0011]
CG:  42%|████▏     | 21/50 [00:04<00:05,  5.06it/s, ldiff=6.68e-6, gnorm=0.000953]
CG:  44%|████▍     | 22/50 [00:04<00:05,  5.04it/s, ldiff=6.68e-6, gnorm=0.000953]
CG:  44%|████▍     | 22/50 [00:04<00:05,  5.04it/s, ldiff=2.86e-6, gnorm=0.000835]
CG:  46%|████▌     | 23/50 [00:04<00:05,  5.03it/s, ldiff=2.86e-6, gnorm=0.000835]
CG:  46%|████▌     | 23/50 [00:04<00:05,  5.03it/s, ldiff=3.34e-6, gnorm=0.000787]
CG:  48%|████▊     | 24/50 [00:04<00:05,  5.02it/s, ldiff=3.34e-6, gnorm=0.000787]
CG:  48%|████▊     | 24/50 [00:05<00:05,  5.02it/s, ldiff=2.86e-6, gnorm=0.000778]
CG:  50%|█████     | 25/50 [00:05<00:04,  5.05it/s, ldiff=2.86e-6, gnorm=0.000778]
CG:  50%|█████     | 25/50 [00:05<00:04,  5.05it/s, ldiff=1.43e-6, gnorm=0.000659]
CG:  52%|█████▏    | 26/50 [00:05<00:04,  5.06it/s, ldiff=1.43e-6, gnorm=0.000659]
CG:  52%|█████▏    | 26/50 [00:05<00:04,  5.06it/s, ldiff=1.91e-6, gnorm=0.000599]
CG:  54%|█████▍    | 27/50 [00:05<00:04,  5.01it/s, ldiff=1.91e-6, gnorm=0.000599]
CG:  54%|█████▍    | 27/50 [00:05<00:04,  5.01it/s, ldiff=2.38e-6, gnorm=0.000596]
CG:  56%|█████▌    | 28/50 [00:05<00:04,  4.99it/s, ldiff=2.38e-6, gnorm=0.000596]
CG:  56%|█████▌    | 28/50 [00:05<00:04,  4.99it/s, ldiff=1.91e-6, gnorm=0.000584]
CG:  58%|█████▊    | 29/50 [00:05<00:04,  4.99it/s, ldiff=1.91e-6, gnorm=0.000584]
CG:  58%|█████▊    | 29/50 [00:06<00:04,  4.99it/s, ldiff=9.54e-7, gnorm=0.000557]
CG:  60%|██████    | 30/50 [00:06<00:03,  5.03it/s, ldiff=9.54e-7, gnorm=0.000557]
CG:  60%|██████    | 30/50 [00:06<00:03,  5.03it/s, ldiff=1.43e-6, gnorm=0.000547]
CG:  62%|██████▏   | 31/50 [00:06<00:03,  5.04it/s, ldiff=1.43e-6, gnorm=0.000547]
CG:  62%|██████▏   | 31/50 [00:06<00:03,  5.04it/s, ldiff=9.54e-7, gnorm=0.00057] 
CG:  64%|██████▍   | 32/50 [00:06<00:03,  4.99it/s, ldiff=9.54e-7, gnorm=0.00057]
CG:  64%|██████▍   | 32/50 [00:06<00:03,  4.99it/s, ldiff=1.91e-6, gnorm=0.000568]
CG:  66%|██████▌   | 33/50 [00:06<00:03,  4.97it/s, ldiff=1.91e-6, gnorm=0.000568]
CG:  66%|██████▌   | 33/50 [00:06<00:03,  4.97it/s, ldiff=1.43e-6, gnorm=0.000573]
CG:  68%|██████▊   | 34/50 [00:06<00:03,  4.94it/s, ldiff=1.43e-6, gnorm=0.000573]
CG:  68%|██████▊   | 34/50 [00:07<00:03,  4.94it/s, ldiff=9.54e-7, gnorm=0.000551]
CG:  70%|███████   | 35/50 [00:07<00:03,  4.88it/s, ldiff=9.54e-7, gnorm=0.000551]
CG:  70%|███████   | 35/50 [00:07<00:03,  4.88it/s, ldiff=0, gnorm=0.000513]      
CG:  72%|███████▏  | 36/50 [00:07<00:02,  4.87it/s, ldiff=0, gnorm=0.000513]
CG:  72%|███████▏  | 36/50 [00:07<00:02,  4.87it/s, ldiff=2.86e-6, gnorm=0.00046]
CG:  74%|███████▍  | 37/50 [00:07<00:02,  4.76it/s, ldiff=2.86e-6, gnorm=0.00046]
CG:  74%|███████▍  | 37/50 [00:07<00:02,  4.76it/s, ldiff=9.54e-7, gnorm=0.000427]
CG:  76%|███████▌  | 38/50 [00:07<00:02,  4.79it/s, ldiff=9.54e-7, gnorm=0.000427]
CG:  76%|███████▌  | 38/50 [00:07<00:02,  4.79it/s, ldiff=4.77e-7, gnorm=0.000427]
CG:  78%|███████▊  | 39/50 [00:07<00:02,  4.79it/s, ldiff=4.77e-7, gnorm=0.000427]
CG:  78%|███████▊  | 39/50 [00:08<00:02,  4.79it/s, ldiff=4.77e-7, gnorm=0.000402]
CG:  80%|████████  | 40/50 [00:08<00:02,  4.80it/s, ldiff=4.77e-7, gnorm=0.000402]
CG:  80%|████████  | 40/50 [00:08<00:02,  4.80it/s, ldiff=1.43e-6, gnorm=0.000364]
CG:  82%|████████▏ | 41/50 [00:08<00:01,  4.78it/s, ldiff=1.43e-6, gnorm=0.000364]
CG:  82%|████████▏ | 41/50 [00:08<00:01,  4.78it/s, ldiff=1.43e-6, gnorm=0.000342]
CG:  84%|████████▍ | 42/50 [00:08<00:01,  4.78it/s, ldiff=1.43e-6, gnorm=0.000342]
CG:  84%|████████▍ | 42/50 [00:08<00:01,  4.78it/s, ldiff=4.77e-7, gnorm=0.000331]
CG:  86%|████████▌ | 43/50 [00:08<00:01,  4.82it/s, ldiff=4.77e-7, gnorm=0.000331]
CG:  86%|████████▌ | 43/50 [00:08<00:01,  4.82it/s, ldiff=4.77e-7, gnorm=0.000315]
CG:  88%|████████▊ | 44/50 [00:08<00:01,  4.84it/s, ldiff=4.77e-7, gnorm=0.000315]
CG:  88%|████████▊ | 44/50 [00:09<00:01,  4.84it/s, ldiff=4.77e-7, gnorm=0.000296]
CG:  90%|█████████ | 45/50 [00:09<00:01,  4.80it/s, ldiff=4.77e-7, gnorm=0.000296]
CG:  90%|█████████ | 45/50 [00:09<00:01,  4.80it/s, ldiff=1.43e-6, gnorm=0.000298]
CG:  92%|█████████▏| 46/50 [00:09<00:00,  4.80it/s, ldiff=1.43e-6, gnorm=0.000298]
CG:  92%|█████████▏| 46/50 [00:09<00:00,  4.80it/s, ldiff=4.77e-7, gnorm=0.000292]
CG:  94%|█████████▍| 47/50 [00:09<00:00,  4.70it/s, ldiff=4.77e-7, gnorm=0.000292]
CG:  94%|█████████▍| 47/50 [00:09<00:00,  4.70it/s, ldiff=4.77e-7, gnorm=0.000289]
CG:  96%|█████████▌| 48/50 [00:09<00:00,  4.71it/s, ldiff=4.77e-7, gnorm=0.000289]
CG:  96%|█████████▌| 48/50 [00:09<00:00,  4.71it/s, ldiff=9.54e-7, gnorm=0.000274]
CG:  98%|█████████▊| 49/50 [00:09<00:00,  4.76it/s, ldiff=9.54e-7, gnorm=0.000274]
CG:  98%|█████████▊| 49/50 [00:10<00:00,  4.76it/s, ldiff=9.54e-7, gnorm=0.000264]
CG: 100%|██████████| 50/50 [00:10<00:00,  4.76it/s, ldiff=9.54e-7, gnorm=0.000264]

Output:

Reconstruction shape: torch.Size([128, 128])
Ground truth shape: torch.Size([128, 128])
Relative reconstruction error: 0.1621

output

Results Summary

Summary of reconstruction parameters and achieved error.

# Print summary statistics
print("=== MRI Reconstruction Summary ===")
print(f"Image size: {image_size}")
print(f"Number of coils: {num_coils}")
if use_cartesian:
    print(f"Trajectory: Cartesian")
else:
    print(f"Trajectory: Radial ({num_spokes} spokes × {num_readouts} readouts)")
print(f"Total k-space samples: {len(locs)}")
print(f"Undersampling factor: {(image_size[0] * image_size[1]) / len(locs):.2f}×")
print(f"Noise level: {noise_level}")
print(f"Relative reconstruction error: {recon_error:.4f}")

Output:

=== MRI Reconstruction Summary ===
Image size: (128, 128)
Number of coils: 8
Trajectory: Radial (37 spokes × 256 readouts)
Total k-space samples: 9472
Undersampling factor: 1.73×
Noise level: 1e-05
Relative reconstruction error: 0.1621