3. What the scanner plays#

Open in Colab

When the operator presses Scan, pulserver does three things before the scanner plays a single block: it designs the whole scan with your sequence function, checks it against the scanner’s limits, and converts it into the form the scanner plays. In lesson 1 all three were one call, console.design("generate", ...). In this lesson you run them one at a time and look at what each one produces.

Learning objectives

  • See which checks a design must pass, and what the operator reads when one fails.

  • Fix a failing design by designing it under gentler limits.

  • See how a scan of hundreds of blocks becomes a couple of segments the scanner prepares once, and what changes each time it plays them.

Previous: 2. Your sequence in the scanner UI. Next: 4. What your recon receives, where you follow the raw data on its way back.

The whole scan#

The scan is your sequence function called with the final protocol. Here it is gre2d at a 64 by 64 matrix, the protocol you scanned in lesson 1, written to a Pulseq file as pulserver writes it.

import tempfile
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import pypulseqpp as pp
from pypulseqpp.sequences.sequence.gre2D_sequence import gre2d

system = pp.Opts(max_grad=40, grad_unit="mT/m", max_slew=150, slew_unit="T/m/s")

seq = gre2d(system, n_x=64, n_y=64)
path = Path(tempfile.mkdtemp()) / "gre2d.seq"
seq.write(path)
print(f"{seq.num_blocks} blocks, {seq.duration()[0]:.2f} s")
534 blocks, 22.25 s

The checks#

A sequence designed under the scanner’s limits can still break them, for example on a rotated slice, where two gradient axes add up on one physical axis. So pulserver checks the design as the scanner will play it, rotated to the prescription, with check():

  • the timing: every block on its raster, dead times respected;

  • the gradient amplitude and the slew rate against system;

  • peripheral nerve stimulation (PNS) and mechanical resonances, under the models every scanner sends;

  • acoustic noise, when the scanner has a model of it.

The PNS model belongs to the gradient coil, and the scanner sends it with its limits. Here is an example rheobase-chronaxie model:

from pypulseqpp import safety

from pulserver import ir

coil = safety.ChronaxieModel(chronaxie=360e-6, rheobase=20, alpha=0.333)
limits = ir.CheckLimits(pns=coil)

problems = ir.check(path, system, limits=limits)
print("\n".join(problems) or "no problems")
PNS of 104% of the chronaxie threshold exceeds 100%, on z in block 26

The design is refused. On a scanner the design service then designs it again at a lower slew rate, as this lesson does by hand below; for a plugin that asks to be held strictly, this message is what the operator reads instead of a scan starting, and nothing is played. Safety checks lists every check and what each one needs from the scanner.

The figure shows why. Over the first 12 ms of a TR, the gradients stay within 40 mT/m and the slew rate within 150 T/m/s, but the slice rephaser ramps fast enough to drive the nerve response just above its threshold.

03 scanner representation

Designing under gentler limits#

The remedy is a change of design, not of the check: design the sequence with a lower slew rate, and check it against the same scanner. A scanner can send such design limits with its own (design_max_slew), and pulserver then hands your sequence function the gentler system.

gentle = pp.Opts(max_grad=40, grad_unit="mT/m", max_slew=100, slew_unit="T/m/s")

seq = gre2d(gentle, n_x=64, n_y=64)
seq.write(path)
problems = ir.check(path, system, limits=limits)
print("\n".join(problems) or "no problems")
03 scanner representation
no problems

The same TE and TR, with the nerve response under its threshold.

What the scanner prepares#

The scanner does not read the Pulseq file block by block while it plays. pulserver first finds what repeats: the TR, here 6 blocks played once per line and once per dummy TR (64 lines and 25 dummies). It cuts the TR into segments, short runs of blocks the scanner prepares once, and lists the order to play them in. The result is the cache the scanner loads, written beside the Pulseq file by convert():

print(ir.convert(path, system).name)

report = ir.summary(path, system, cache_ext=".pseg")
repetition = report["subsequences"][0]
print(
    f"{seq.num_blocks} blocks = {repetition['num_trs']} TRs "
    f"of {repetition['tr_size']} blocks"
)
for number, segment in enumerate(report["segments"]):
    kind = "a pure delay" if segment["pure_delay"] else "RF, gradients and ADC"
    print(f"segment {number}: {segment['num_blocks']} block(s), {kind}")
gre2d.pseg
534 blocks = 89 TRs of 6 blocks
segment 0: 5 block(s), RF, gradients and ADC
segment 1: 1 block(s), a pure delay

Segment 0 excites, encodes and reads out; segment 1 is the wait until the next TR. Every TR plays the two of them in turn, as the scanner will:

03 scanner representation

What changes from one TR to the next#

Every TR plays the same segments, and what differs between TRs, such as the phase encoding, is carried by the segment instances. The scanner prepares a segment’s timing and waveform shapes once. Each time it plays the segment, the instance sets everything else:

  • the amplitude of each gradient, and its rotation;

  • another gradient waveform in place of the prepared one, provided its timing is the same;

  • the RF amplitude, its phase and frequency offsets, and the shim of each transmit channel;

  • the ADC phase and frequency offsets, or no acquisition at all;

  • whether it waits for a trigger, and the digital outputs.

play() walks the cache with the same C library the scanner links, one entry per block played, so you can read these values:

gy = played["gradient_hz_per_m"][:, 1].reshape(-1, blocks)
phase_encoding = np.abs(gy).max(axis=0).argmax()
rf_phase = played["rf_phase_rad"].reshape(-1, blocks)[:, 0]
acquires = played["adc"].reshape(-1, blocks).any(axis=1)
03 scanner representation

The first TRs are the dummy TRs: the same segment, with no phase encoding and the ADC off. Then the phase-encoding amplitude steps through the 64 lines. The RF phase follows the quadratic schedule of RF spoiling, its increment growing by 117° per TR. The scanner prepares segment 0 once and only changes these numbers, so its preparation does not grow with the length of the scan.

Where the segment boundaries fall can be tuned to the scanner’s interpreter; PulSeg representation explains the model.

As a spec#

What this lesson did, stated the way you would ask an agent for it:

Design pypulseqpp's gre2d at 64 x 64 for a 40 mT/m, 150 T/m/s scanner and
check it with pulserver.ir.check against a chronaxie PNS model
(chronaxie 360 us, rheobase 20 T/m/s, alpha 0.333). If it is refused,
redesign it at 100 T/m/s and check again. Convert the accepted design
with pulserver.ir.convert, report its segments, and plot the
phase-encoding amplitude and RF phase of every TR from pulserver.ir.play.

Total running time of the script: (0 minutes 0.623 seconds)

Gallery generated by Sphinx-Gallery