Note
Go to the end to download the full example code or to run this example in your browser via Binder.
MP2RAGE lookup table#
You map T1 from a two-block MP2RAGE acquisition in two ways, by interpolating along the signal curve and by dictionary matching, and sweep the number of points on the curve to see which method depends on it.
With a single unknown, the dictionary atoms lie on a curve instead of filling
a space. Interpolating between the two nearest points on the curve costs
nothing and removes the grid spacing from the error; this is what
LookupTable does.
Prerequisites: Course lesson 1. Your first simulation.
import numpy as np
import torch
from blochsim.estimators import DictionaryMatcher, LookupTable
from blochsim.simulators import MP2RAGESimulator
Phantom#
The phantom is BrainWeb subject 0, slice 90: an axial 1 mm slice through the lateral ventricles. BrainWeb provides fuzzy tissue memberships, so each voxel holds a fraction of each tissue and its T1 is the fraction-weighted average. About a third of the voxels are mixtures, so the ground truth is a continuum of values rather than four.

Protocol#
MP2RAGE applies one inversion and reads two spoiled gradient-echo blocks at two inversion times. Each block samples the centre of k-space of its own shot train, so a voxel contributes two values. The train is spoiled after every readout, so T1 is the only tissue property that changes them.
PROTOCOL = dict(
TI=(800.0, 2700.0),
flip=(4.0, 5.0),
TRspgr=6.7,
TRmp2rage=6000.0,
nshots=128,
)
INVERSION_EFFICIENCY = 0.96
simulator = MP2RAGESimulator(**PROTOCOL, inv_efficiency=INVERSION_EFFICIENCY)
Signal curve#
Neither block alone determines T1, since both are scaled by the proton density and the receive gain. The unified combination divides this scale out and leaves a value between -0.5 and 0.5 that depends on T1 alone:
def unified(blocks):
"""The MP2RAGE unified image: independent of the scale, a function of T1."""
return (blocks[..., 0] * blocks[..., 1]) / blocks.square().sum(-1).clamp_min(1e-12)
Whether the unified image is monotonic in T1 depends on the protocol. Where it turns back it has no inverse, so the table keeps the longest monotonic run and reports the range it spans. The dashed line marks the turning point of the unified image at this protocol, beyond which T1 is not invertible.
sweep = torch.arange(50.0, 6000.0, 10.0)
curve = unified(simulator.simulate(T1=sweep, M0=1.0))

Measurement#
The measurement is both blocks at the true T1 and proton density of every brain voxel, with Gaussian noise of 0.5% of the peak magnetization.
Estimators#
Both estimators are fitted on the same T1 grid, with the proton density
fixed to one. The matcher scores the two-block signal against every atom and
returns the nearest. The table reduces both blocks to the unified value and
interpolates along the curve; combine is the only information it is
given about the sequence. Neither is given the invertible range.
grid = torch.linspace(50.0, 6000.0, 60)
lookup = LookupTable(simulator.bind(M0=1.0), combine=unified).fit(T1=grid, seed=0)
maps = lookup.map(measured) # {"T1": ...}, one value per voxel
match = DictionaryMatcher(simulator.bind(M0=1.0)).fit(T1=grid, seed=0)
Table size#
Sweeping the number of grid points separates the method from the sampling. Times are the best of three passes over the slice.

points match table match table
error error time time
-----------------------------------------------
30 7.93% 0.88% 1.4ms 0.5ms
60 3.29% 0.75% 1.8ms 0.4ms
120 0.78% 0.71% 2.6ms 0.6ms
250 0.80% 0.71% 4.5ms 0.5ms
500 0.62% 0.71% 8.8ms 0.6ms
1000 0.66% 0.71% 21.7ms 0.7ms
2000 0.71% 0.71% 40.1ms 0.7ms
The error floor is set by the noise. The table is within 0.2 percentage points of it from 30 points on. The match is about nine times worse at 30 points and reaches the same floor at about 120, at a cost that grows with the number of points: it takes one comparison per atom per voxel, where the table takes a binary search that grows with the logarithm of the number of points. A sufficiently fine grid makes the match equal to the table; the table does not need to be told how fine the grid must be.
Maps#
The maps use the number of points each method needs: 60 for the table and 2000 for the match, a grid fine enough not to limit it.
the table keeps 38 of 60 points -- the monotonic run -- and spans unified -0.50 to 0.49
Neither estimator returns M0. The two blocks predicted for the estimated T1 are a shape of which the measurement is a multiple, so the proton density is one inner product per voxel:
method train map model peak T1 M0
------------------------------------------------------------------------------
lookup, 60 points 0.00s 0.4ms 0.00 MiB -- 0.75% 0.46%
match, 2000 atoms 0.00s 41.6ms 0.05 MiB -- 0.71% 0.45%
Specification#
What this page did, stated the way you would ask an agent for it:
With blochsim, map T1 and M0 in BrainWeb subject 0, slice 90, from a
two-block MP2RAGE (TI 800 and 2700 ms, flip angles 4 and 5 deg, TR 6.7 ms,
128 shots, TR_MP2RAGE 6 s, inversion efficiency 0.96) at 0.5% noise.
Fit a LookupTable on the unified image and a DictionaryMatcher over the
same T1 grid. Sweep the grid from 30 to 2000 points and report the median
relative T1 error and the mapping time of both; derive M0 from the
predicted blocks.
Total running time of the script: (0 minutes 1.606 seconds)

