T1 Mapping - Radial Look Locker Sequence#
T1 mapping using a continuous golden radial Look Locker inversion recovery sequence.
Imports#
import importlib
if not importlib.util.find_spec('mrseq'):
%pip install mrseq[examples]
import tempfile
from pathlib import Path
import matplotlib.pyplot as plt
import MRzeroCore as mr0
import numpy as np
import torch
from cmap import Colormap
from einops import rearrange
from mrpro.algorithms.reconstruction import DirectReconstruction
from mrpro.data import CsmData
from mrpro.data import KData
from mrpro.data import SpatialDimension
from mrpro.data.traj_calculators import KTrajectoryIsmrmrd
from mrpro.operators import DictionaryMatchOp
from mrpro.operators.models import TransientSteadyStateWithPreparation
from mrpro.phantoms.coils import birdcage_2d
from raw2ismrmrd.utils import combine_ismrmrd_files
from mrseq.sequences.t1_radial_look_locker import main as create_seq
from mrseq.utils import set_mr0_inversion_pulses
from mrseq.utils import sys_defaults
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
Settings#
We are going to use a numerical phantom with a matrix size of 120 x 120.
image_matrix_size = [120, 120]
rf_flip_angle = 9
tmp = tempfile.TemporaryDirectory()
fname_mrd = Path(tmp.name) / 't1_radial_look_locker.mrd'
Create the digital phantom#
We use the standard Brainweb phantom from MRzero.
im_dims = SpatialDimension(z=1, y=image_matrix_size[1], x=image_matrix_size[0])
coil_maps = birdcage_2d(6, image_dimensions=im_dims, relative_radius=0.8)
phantom = mr0.util.load_phantom(image_matrix_size)
phantom.coil_sens = rearrange(coil_maps[0, ...], 'coils z y x -> coils x y z')
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/MRzeroCore/util.py:816: UserWarning: VoxelGridPhantom.interpolate() changes the loaded phantom, use reslice_to in the BIfTI config instead.
phantom = phantom.interpolate(*size, 1)
Create the T1 radial Look Locker sequence#
To create the T1 radial Look Locker sequence, we use the previously imported t1_radial_look_locker script.
sequence, fname_seq = create_seq(
system=sys_defaults,
fov_xy=float(phantom.size.numpy()[0]),
n_readout=image_matrix_size[0],
n_spokes=600,
rf_flip_angle=rf_flip_angle,
test_report=False,
timing_check=False,
)
Current echo time = 2.423 ms
Current repetition time = 4.860 ms
Saving sequence file 't1_radial_look_locker_200fov_120nx_600na.seq' into folder '/home/runner/work/mrseq/mrseq/examples/output'.
Simulate the sequence#
Now, we pass the sequence and the phantom to the MRzero simulation and save the simulated signal as an (ISMR)MRD file.
mr0_sequence = mr0.Sequence.import_file(str(fname_seq.with_suffix('.seq')))
set_mr0_inversion_pulses(mr0_sequence, sequence)
signal, ktraj_adc = mr0.util.simulate(mr0_sequence, phantom, accuracy=1e0)
mr0.sig_to_mrd(fname_mrd, signal, sequence)
combine_ismrmrd_files(fname_mrd, Path(f'{fname_seq}_header.h5'))
>>>> Rust - compute_graph(...) >>>
Converting Python -> Rust: 0.003871898 s
Compute Graph
Computing Graph: 17.012335 s
Analyze Graph
Analyzing Graph: 0.29234743 s
Converting Rust -> Python: 2.7673485 s
<<<< Rust <<<<
<ismrmrd.hdf5.Dataset at 0x7f73a2530890>
Reconstruct the images at different inversion times#
We use MRpro for the image reconstruction.
kdata = KData.from_file(str(fname_mrd).replace('.mrd', '_with_traj.mrd'), trajectory=KTrajectoryIsmrmrd())
csm = CsmData.from_kdata_inati(kdata[0, ..., 400:, :], downsampled_size=64, smoothing_width=9)
n_acq_per_image = 32
n_overlap = 16
split_indices = torch.arange(kdata.shape[-2]).unfold(dimension=0, size=n_acq_per_image, step=n_overlap)
kdata_split = kdata[..., split_indices, :]
recon = DirectReconstruction(kdata_split, csm=csm)
idata = recon(kdata_split)
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/mrpro/data/KData.py:367: UserWarning: No vendor information found. Assuming Siemens time stamp format.
warnings.warn('No vendor information found. Assuming Siemens time stamp format.', stacklevel=1)
We visualize the coil sensitivity maps which we used for the iterative SENSE reconstruction.
fig, ax = plt.subplots(1, csm.shape[1], figsize=(9 * csm.shape[0], 9))
for i in range(csm.shape[1]):
ax[i].imshow(csm.data[0, i, 0, :, :].abs(), cmap='gray')
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/matplotlib/cbook.py:713: 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)
We can now plot the images at different time points along the acquisition, i.e. at different inversion times.
idat = idata.data.abs().numpy().squeeze()
fig, ax = plt.subplots(6, idat.shape[0] // 6, figsize=(4 * idata.shape[0] // 6, 6 * 4))
ax = ax.flatten()
for i in range(idat.shape[0]):
ax[i].imshow(idat[i, ::-1, :], cmap='gray')
ax[i].set_xticks([])
ax[i].set_yticks([])
Estimate the T1 maps#
We use a dictionary matching approach to estimate the T1 maps. Afterward, we compare them to the input and ensure they match.
sampling_time = idata.header.acquisition_time_stamp - kdata.header.acq_info.acquisition_time_stamp[0, 0, 0, 0, 0]
dictionary = DictionaryMatchOp(
TransientSteadyStateWithPreparation(
sampling_time=sampling_time.to(torch.float32),
repetition_time=idata.header.tr,
m0_scaling_preparation=-1,
delay_after_preparation=idata.header.ti,
),
index_of_scaling_parameter=0,
)
dictionary.append(
torch.tensor(1.0),
torch.linspace(0.1, 5.0, 200)[None, :, None],
torch.deg2rad(torch.linspace(rf_flip_angle * 0.8, rf_flip_angle * 1.2, 50))[None, None, :],
)
m0_match, t1_match, fa_match = dictionary(idata.data.mean(dim=1))
t1_input = rearrange(phantom.T1.numpy().squeeze(), 'x y -> y x')
fa_input = np.abs(rearrange(phantom.B1.numpy().squeeze(), 'x y -> y x')) * rf_flip_angle
obj_mask = np.zeros_like(t1_input)
obj_mask[t1_input > 0] = 1
fa_input = fa_input * obj_mask
t1_measured = t1_match.numpy().squeeze() * obj_mask
fa_measured = np.rad2deg(fa_match.numpy().squeeze() * obj_mask)
fig, ax = plt.subplots(2, 3, figsize=(15, 6))
for cax in ax.flatten():
cax.set_xticks([])
cax.set_yticks([])
im = ax[0, 0].imshow(t1_input, vmin=0, vmax=1.8, cmap=Colormap('lipari').to_mpl())
fig.colorbar(im, ax=ax[0, 0], label='Input T1 (s)')
im = ax[0, 1].imshow(t1_measured, vmin=0, vmax=1.8, cmap=Colormap('lipari').to_mpl())
fig.colorbar(im, ax=ax[0, 1], label='Measured T1 (s)')
im = ax[0, 2].imshow(t1_measured - t1_input, vmin=-1.8, vmax=1.8, cmap='bwr')
fig.colorbar(im, ax=ax[0, 2], label='Difference T1 (s)')
im = ax[1, 0].imshow(fa_input, vmin=0, vmax=12, cmap='magma')
fig.colorbar(im, ax=ax[1, 0], label='Input Flip Angle (°)')
im = ax[1, 1].imshow(fa_measured, vmin=0, vmax=12, cmap='magma')
fig.colorbar(im, ax=ax[1, 1], label='Measured Flip Angle (°)')
im = ax[1, 2].imshow(fa_measured - fa_input, vmin=-12, vmax=12, cmap='bwr')
fig.colorbar(im, ax=ax[1, 2], label='Difference Flip Angle (°)')
relative_error = np.sum(np.abs(t1_input - t1_measured)) / np.sum(np.abs(t1_input))
print(f'Relative error of T1 {relative_error}')
assert relative_error < 0.08
relative_error = np.sum(np.abs(fa_input - fa_measured)) / np.sum(np.abs(fa_input))
print(f'Relative error of Flip Angle {relative_error}')
assert relative_error < 0.07
Relative error of T1 0.07404140383005142
Relative error of Flip Angle 0.060752831399440765