Source code for blochsim.sequence._description

"""Backend-independent description of an MR sequence state machine."""

from __future__ import annotations

__all__ = [
    "AdcRole",
    "EventAction",
    "EventType",
    "RfDefinition",
    "RfMode",
    "RfShape",
    "RfUse",
    "SequenceDescription",
    "SequenceEvent",
    "ShimDefinition",
    "decompress_shape",
    "ideal_rf_definition",
]

from collections.abc import Mapping
from dataclasses import dataclass, field
from enum import IntEnum, IntFlag
from typing import Any

import numpy as np

# ``np.trapz`` was renamed to ``np.trapezoid`` in NumPy 2.0.
_trapezoid = getattr(np, "trapezoid", None) or np.trapz


class EventType(IntEnum):
    """Kind of a state-machine event, in the numbering of Pulserver SEQDESC streams."""

    WAIT = 0
    RF = 1
    ADC = 2


class EventAction(IntFlag):
    """What the sequence does around an event, beyond the event itself.

    A Pulseq file has no gradient "use" field, so a crusher cannot be told from
    a phase encode by looking at waveforms -- that would mean tracking the
    k-space moment through the whole TR. The sequence says instead what each
    event's role implies, and the description carries it, which is what lets an
    autogenerated stream reach the kernels without a policy being named.

    ``CRUSH_BEFORE`` and ``CRUSH_AFTER`` are the unbalanced gradients a
    refocusing pulse sits between. ``SHIFT_AFTER`` is one played after a
    readout, and ``SPOIL_AFTER`` is ideal transverse spoiling there instead.

    The values are the bits the packed action word carries, so a description
    and a kernel never hold two numberings of the same fact.
    """

    NONE = 0
    CRUSH_BEFORE = 1
    CRUSH_AFTER = 2
    SPOIL_AFTER = 8
    SHIFT_AFTER = 16


class RfMode(IntEnum):
    """How a pulse's rotation is reached.

    The three are alternatives rather than combinable. ``INSTANT`` turns
    through a flip angle and a phase. ``PROFILED`` reads the rotation from a
    table over slice position and effective flip. ``DYNAMIC`` reads it per
    voxel, which is what a pulse whose channel weights vary while it plays
    needs -- and which subsumes a profile, since a rotation integrated at the
    voxel's own position is one.
    """

    INSTANT = 0
    PROFILED = 1
    DYNAMIC = 2


class RfUse(IntEnum):
    """Pulseq RF-use tag of a pulse.

    The whole tag set, not the subset the operators that ship here happen to
    emit. A file written elsewhere may carry any of them, and an operator
    written later may emit one nothing emits today.
    """

    UNKNOWN = 0
    EXCITATION = 1
    REFOCUSING = 2
    INVERSION = 3
    SATURATION = 4
    PREPARATION = 5
    OTHER = 6


class AdcRole(IntEnum):
    """Role of an ADC event among those that make up one echo.

    ``NON_ACQUIRED`` is a window that is not sampled, ``SINGLE`` an echo read
    by one event, ``ECHO_CENTER`` the event of a multi-event echo that sits at
    its centre, and ``NON_CENTER`` the others.
    """

    NON_ACQUIRED = 0
    SINGLE = 1
    ECHO_CENTER = 2
    NON_CENTER = 3


@dataclass(frozen=True)
class SequenceEvent:
    """One WAIT, RF, or ADC state-machine event.

    ``params`` deliberately accepts tensors. This lets a hard-coded sequence
    description carry differentiable flip angles, phases, and timings without
    introducing a second representation for sequence optimization.

    Attributes
    ----------
    type : EventType
        Whether this is a wait, an RF pulse or an ADC.
    timestamp_us : float or torch.Tensor
        When it happens, in microseconds from the start of the stream.
    params : tuple
        The payload its type defines, read through the named properties rather
        than by position.
    action : EventAction
        What the sequence does around it -- the crushers a refocusing pulse
        sits between, the gradient or the ideal spoiling after a readout.
    """

    type: EventType
    timestamp_us: Any
    params: tuple[Any, ...] = ()
    action: EventAction = EventAction.NONE

    @classmethod
    def wait(cls, timestamp_us: Any) -> SequenceEvent:
        """Create an event that only marks a time.

        Parameters
        ----------
        timestamp_us : float or torch.Tensor
            Time of the event, in microseconds from the start of the stream.

        Returns
        -------
        SequenceEvent
            A ``WAIT`` event with no payload and no gradient action.
        """
        return cls(EventType.WAIT, timestamp_us)

    @classmethod
    def rf(
        cls,
        timestamp_us: Any,
        definition_id: int,
        use: RfUse,
        amplitude_hz: Any,
        phase_rad: Any = 0.0,
        frequency_hz: Any = 0.0,
        shim_id: int = 0,
        slice_select_gradient_hz_per_m: Any = 0.0,
        action: EventAction = EventAction.NONE,
    ) -> SequenceEvent:
        """Create an RF event in the Pulserver SEQDESC field order.

        Parameters
        ----------
        timestamp_us : float or torch.Tensor
            Time of the pulse, in microseconds from the start of the stream.
        definition_id : int
            Key of the :class:`RfDefinition` played; 0 is the ideal pulse.
        use : RfUse
            Pulseq RF-use tag; selects the operator a model plays it with.
        amplitude_hz : float or torch.Tensor
            Amplitude of the occurrence, in the units the definition is
            normalized to. For the definitions built here and for a Pulseq
            file it is the flip angle in radians.
        phase_rad : float or torch.Tensor, default=0.0
            Phase offset, in radians.
        frequency_hz : float or torch.Tensor, default=0.0
            Frequency offset from the centre frequency, in Hz.
        shim_id : int, default=0
            Key of the :class:`ShimDefinition` driving the channels; 0 for none.
        slice_select_gradient_hz_per_m : float or torch.Tensor, default=0.0
            Slice-select gradient played with the pulse, in Hz/m.
        action : EventAction, default=EventAction.NONE
            Gradient action played around the event.

        Returns
        -------
        SequenceEvent
            An ``RF`` event.
        """
        return cls(
            EventType.RF,
            timestamp_us,
            (
                definition_id,
                use,
                amplitude_hz,
                phase_rad,
                frequency_hz,
                shim_id,
                slice_select_gradient_hz_per_m,
            ),
            action,
        )

    @classmethod
    def adc(
        cls,
        timestamp_us: Any,
        role: AdcRole = AdcRole.SINGLE,
        phase_rad: Any = 0.0,
        *,
        is_echo: bool = True,
        action: EventAction = EventAction.NONE,
    ) -> SequenceEvent:
        """Create an ADC event in the Pulserver SEQDESC field order.

        Parameters
        ----------
        timestamp_us : float or torch.Tensor
            Time of the sample, in microseconds from the start of the stream.
        role : AdcRole, default=AdcRole.SINGLE
            Role of the sample among the events of one echo.
        phase_rad : float or torch.Tensor, default=0.0
            Demodulation phase, in radians.
        is_echo : bool, default=True
            Whether the sample is at the k-space origin.
        action : EventAction, default=EventAction.NONE
            Gradient action played after the sample.

        Returns
        -------
        SequenceEvent
            An ``ADC`` event.
        """
        return cls(EventType.ADC, timestamp_us, (role, phase_rad, int(is_echo)), action)

    @property
    def rf_definition_id(self) -> int:
        """Key of the RF definition played (RF events)."""
        self._require(EventType.RF)
        return int(self.params[0])

    @property
    def rf_use(self) -> RfUse:
        """Pulseq RF-use tag (RF events)."""
        self._require(EventType.RF)
        return RfUse(int(self.params[1]))

    @property
    def rf_amplitude_hz(self) -> Any:
        """Amplitude of the pulse, in the units of its definition (RF events).

        The flip angle in radians for the definitions built here and for a
        Pulseq file.
        """
        self._require(EventType.RF)
        return self.params[2]

    @property
    def rf_phase_rad(self) -> Any:
        """Phase offset, in radians (RF events)."""
        self._require(EventType.RF)
        return self.params[3]

    @property
    def rf_frequency_hz(self) -> Any:
        """Frequency offset, in Hz (RF events)."""
        self._require(EventType.RF)
        return self.params[4]

    @property
    def rf_shim_id(self) -> int:
        """Key of the transmit shim, 0 for none (RF events)."""
        self._require(EventType.RF)
        return int(self.params[5])

    @property
    def slice_select_gradient_hz_per_m(self) -> Any:
        """Slice-select gradient played with the pulse, in Hz/m (RF events)."""
        self._require(EventType.RF)
        return self.params[6]

    @property
    def adc_role(self) -> AdcRole:
        """Role of the sample among the events of one echo (ADC events)."""
        self._require(EventType.ADC)
        return AdcRole(int(self.params[0]))

    @property
    def adc_phase_rad(self) -> Any:
        """Demodulation phase, in radians (ADC events)."""
        self._require(EventType.ADC)
        return self.params[1]

    @property
    def is_echo(self) -> bool:
        """Whether the sample is at the k-space origin (ADC events)."""
        self._require(EventType.ADC)
        return bool(int(self.params[2]))

    def _require(self, expected: EventType) -> None:
        if self.type is not expected:
            raise AttributeError(
                f"{expected.name} payload requested from {self.type.name} event"
            )


@dataclass(frozen=True)
class RfShape:
    """One Pulseq derivative/run-length encoded shape.

    Attributes
    ----------
    num_uncompressed : int
        How many samples the shape decodes to.
    samples : array-like
        The encoded samples, decoded by :meth:`decompress`.
    """

    num_uncompressed: int
    samples: Any = field(repr=False, compare=False)

    def decompress(self, scale: float = 1.0) -> np.ndarray:
        """Decode the shape.

        Parameters
        ----------
        scale : float, default=1.0
            Factor applied to the decoded samples, e.g. ``2 * pi`` to turn a
            phase in cycles into radians.

        Returns
        -------
        numpy.ndarray
            ``(num_uncompressed,)``, float32.
        """
        return decompress_shape(self.samples, self.num_uncompressed, scale=scale)


@dataclass(frozen=True)
class RfDefinition:
    """RF definition referenced by RF events.

    Attributes
    ----------
    id : int
        What an RF event names this pulse by.
    bandwidth_hz : float
        The pulse's bandwidth, which is the gradient the slice select is
        quoted against.
    num_bands : int
        How many slices the pulse excites at once.
    band_frequency_offsets_hz : tuple of float
        Where each band sits, in Hz off the centre frequency.
    band_bandwidth_hz : float
        The bandwidth of one band.
    total_b1sq_power : float
        The pulse's integrated squared amplitude, which is what a power limit
        is read against.
    magnitude : RfShape or tuple of RfShape
        The envelope, or one envelope per transmit channel.
    phase : RfShape or tuple of RfShape, default=None
        The phase, matching ``magnitude``. ``None`` for a pulse of constant
        phase.
    time : RfShape, default=None
        Sample times, for a pulse on a non-uniform raster. ``None`` for a
        uniform one.
    gradient : RfShape, default=None
        The slice-select gradient per sample, in units ``bandwidth_hz`` is
        quoted at. ``None`` where the gradient holds.
    """

    id: int
    bandwidth_hz: float
    num_bands: int
    band_frequency_offsets_hz: tuple[float, ...]
    band_bandwidth_hz: float
    total_b1sq_power: float
    magnitude: RfShape | tuple[RfShape, ...]
    phase: RfShape | tuple[RfShape, ...] | None = None
    time: RfShape | None = None
    gradient: RfShape | None = None

    def __post_init__(self) -> None:
        if isinstance(self.magnitude, tuple):
            if not self.magnitude:
                raise ValueError(f"RF definition {self.id} drives no channel")
            if isinstance(self.phase, tuple):
                if len(self.phase) != len(self.magnitude):
                    raise ValueError(
                        f"RF definition {self.id}: {len(self.magnitude)} "
                        f"magnitude channels against {len(self.phase)} phase"
                    )
            elif self.phase is not None:
                raise ValueError(
                    f"RF definition {self.id}: a per-channel magnitude takes a "
                    f"phase per channel, or none"
                )
        elif isinstance(self.phase, tuple):
            raise ValueError(
                f"RF definition {self.id}: a phase per channel takes a "
                f"magnitude per channel"
            )

    @property
    def channel_count(self) -> int:
        """How many transmit channels drive their own waveform."""
        return len(self.magnitude) if isinstance(self.magnitude, tuple) else 1

    def channel_envelopes(self) -> np.ndarray:
        """Return the normalized complex envelope per channel.

        The phase shapes are in cycles and are converted to radians.

        Returns
        -------
        numpy.ndarray
            ``(channels, samples)``, complex.

        Raises
        ------
        ValueError
            If the magnitude and phase, or the channels, differ in length.
        """
        magnitudes = (
            self.magnitude if isinstance(self.magnitude, tuple) else (self.magnitude,)
        )
        if isinstance(self.phase, tuple):
            phases: tuple[RfShape | None, ...] = self.phase
        else:
            phases = (self.phase,) * len(magnitudes)
        channels = []
        for magnitude_shape, phase_shape in zip(magnitudes, phases, strict=False):
            magnitude = magnitude_shape.decompress()
            if phase_shape is None:
                channels.append(magnitude.astype(np.complex64))
                continue
            phase = phase_shape.decompress(scale=2.0 * np.pi)
            if phase.size != magnitude.size:
                raise ValueError(
                    f"RF definition {self.id}: magnitude/phase size mismatch"
                )
            channels.append(magnitude * np.exp(1j * phase))
        widths = {channel.size for channel in channels}
        if len(widths) > 1:
            raise ValueError(
                f"RF definition {self.id}: channels of {sorted(widths)} samples"
            )
        return np.stack(channels)

    def complex_envelope(self) -> np.ndarray:
        """Return the normalized complex RF envelope.

        ``(samples,)`` for a pulse every channel plays alike, ``(channels,
        samples)`` for one whose channels each carry their own waveform.
        """
        channels = self.channel_envelopes()
        return channels[0] if self.channel_count == 1 else channels

    def combined_envelope(self) -> np.ndarray:
        """Return the envelope a voxel of unit sensitivity on every channel sees.

        The sum over channels, ``(samples,)``. This is the shape the flip
        angle, the integral and the saturation are all read against, so each
        keeps the meaning it has for a pulse driving a single channel.
        """
        return self.channel_envelopes().sum(axis=0)

    def gradient_waveform(self) -> np.ndarray | None:
        """Return the slice-select gradient per sample, or ``None`` if it holds.

        In units of the gradient ``bandwidth_hz`` is quoted at, so a shape of
        ones is the gradient the scanner does not move and reproduces the
        constant case exactly. What a spin at a normalized position accrues
        while sample ``s`` lasts is ``bandwidth_hz * position * g[s]`` in Hz.

        Returns
        -------
        numpy.ndarray or None
            ``(samples,)`` relative gradient; ``None`` when the gradient holds.

        Raises
        ------
        ValueError
            If the gradient and the envelope disagree on how many samples the
            pulse has.
        """
        if self.gradient is None:
            return None
        waveform = self.gradient.decompress()
        samples = self.channel_envelopes().shape[1]
        if waveform.size != samples:
            raise ValueError(
                f"RF definition {self.id}: the gradient carries "
                f"{waveform.size} samples and the pulse {samples}"
            )
        return waveform

    def sample_durations(self, *, rf_raster_time_s: float = 1e-6) -> np.ndarray:
        """Return how long each sample holds its field, in seconds.

        ``time`` names where along the pulse each sample sits, so what a sample
        lasts is the distance between the midpoints either side of it, and the
        whole spacing to its one neighbour at the two ends. A definition
        declaring no time is played one sample per raster step.

        This is what the rotation a pulse performs is composed from: the field
        is held constant across a sample, so a longer sample turns further and
        lets a spin off the slice centre accrue more phase.

        Parameters
        ----------
        rf_raster_time_s : float, default=1e-6
            RF raster that the sample times are counted in, in seconds.

        Returns
        -------
        numpy.ndarray
            ``(samples,)``, float64, in seconds.

        Raises
        ------
        ValueError
            If the declared times and the envelope disagree on how many
            samples the pulse has, or the times do not advance.
        """
        samples = int(self.channel_envelopes().shape[1])
        if self.time is None or samples < 2:
            return np.full(samples, float(rf_raster_time_s), dtype=np.float64)
        time_s = self.time.decompress(scale=rf_raster_time_s).astype(np.float64)
        if time_s.size != samples:
            raise ValueError(
                f"RF definition {self.id}: {time_s.size} sample times against "
                f"{samples} samples"
            )
        if not np.all(np.diff(time_s) > 0.0):
            raise ValueError(
                f"RF definition {self.id}: the sample times do not advance, so "
                f"they do not say how long a sample lasts"
            )
        return np.gradient(time_s)

    def rf_mode(self) -> RfMode:
        """Return which rotation this pulse's waveform asks the kernels for.

        Memoized on the (immutable) definition: every event naming this pulse
        asks the same question, and answering it reads the whole envelope.

        Returns
        -------
        RfMode
            ``DYNAMIC`` for several transmit channels, ``INSTANT`` for a
            constant envelope without slice selection, else ``PROFILED``.
        """
        cached = getattr(self, "_mode_cache", None)
        if cached is not None:
            return cached
        if self.channel_count > 1:
            mode = RfMode.DYNAMIC
        elif self._turns_about_one_axis():
            mode = RfMode.INSTANT
        else:
            mode = RfMode.PROFILED
        object.__setattr__(self, "_mode_cache", mode)
        return mode

    def _turns_about_one_axis(self) -> bool:
        """Whether a flip angle and a phase already say what this pulse does.

        A rectangle played without slice selection holds its effective field
        fixed for its whole duration, so integrating it lands on the rotation a
        hard pulse performs -- and lands there exactly, where reading a
        tabulated one at a flip between knots would not.
        """
        if self.bandwidth_hz != 0.0:
            return False
        envelope = self.combined_envelope()
        if envelope.size < 2:
            return True
        return bool(np.all(envelope == envelope[0]))

    def integral(
        self,
        *,
        rf_raster_time_s: float = 1e-6,
    ) -> complex:
        """Return the complex time integral of the normalized envelope.

        The envelope is normalized, so this depends only on the pulse shape and
        the raster, never on the amplitude of a particular occurrence. A
        sequence typically reuses one definition for every refocusing pulse and
        re-evaluates it on every simulation, so the result is memoized on the
        (immutable) definition.

        Parameters
        ----------
        rf_raster_time_s : float, default=1e-6
            RF raster that the sample times are counted in, in seconds.

        Returns
        -------
        complex
            Integral of the combined envelope over time, in seconds; 0 for a
            single sample.

        Raises
        ------
        ValueError
            If the declared times and the envelope differ in length.
        """
        cache = getattr(self, "_integral_cache", None)
        if cache is None:
            cache = {}
            object.__setattr__(self, "_integral_cache", cache)
        elif rf_raster_time_s in cache:
            return cache[rf_raster_time_s]

        envelope = self.combined_envelope()
        if envelope.size < 2:
            cache[rf_raster_time_s] = 0.0j
            return 0.0j
        if self.time is None:
            time_s = (
                np.arange(envelope.size, dtype=np.float64) + 0.5
            ) * rf_raster_time_s
        else:
            time_s = self.time.decompress(scale=rf_raster_time_s).astype(np.float64)
            if time_s.size != envelope.size:
                raise ValueError(
                    f"RF definition {self.id}: envelope/time size mismatch"
                )
        area = complex(_trapezoid(envelope, time_s))
        cache[rf_raster_time_s] = area
        return area

    def saturation(self, *, rf_raster_time_s: float = 1e-6) -> float:
        """Return what this shape deposits in a bound pool, per flip squared.

        The saturation an RF pulse applies to a pool with no transverse
        magnetization is ``-pi gamma**2 int B1**2 dt * G(df)``. Writing the
        pulse as a normalized envelope ``w`` driven to a flip angle,
        ``B1 = w / (gamma int w dt)`` per radian, so::

            -pi gamma**2 int B1**2 dt  ==  -pi int |w|**2 dt / |int w dt|**2

        and the gyromagnetic ratio cancels: what is left depends on the shape
        alone. Multiplied by the square of the flip a voxel actually takes and
        by the lineshape at the offset it is played at, this is the exponent of
        the bound pool's saturation.

        In 1/s. A pulse with no area saturates nothing that a flip angle can
        describe, so it reports zero.

        Parameters
        ----------
        rf_raster_time_s : float, default=1e-6
            RF raster that the sample times are counted in, in seconds.

        Returns
        -------
        float
            Saturation per squared flip angle (rad), before the lineshape at
            the offset; negative or zero.
        """
        cache = getattr(self, "_saturation_cache", None)
        if cache is None:
            cache = {}
            object.__setattr__(self, "_saturation_cache", cache)
        elif rf_raster_time_s in cache:
            return cache[rf_raster_time_s]

        area = abs(self.integral(rf_raster_time_s=rf_raster_time_s))
        envelope = self.combined_envelope()
        if area == 0.0 or envelope.size < 2:
            cache[rf_raster_time_s] = 0.0
            return 0.0
        if self.time is None:
            time_s = (
                np.arange(envelope.size, dtype=np.float64) + 0.5
            ) * rf_raster_time_s
        else:
            time_s = self.time.decompress(scale=rf_raster_time_s).astype(np.float64)
        power = float(_trapezoid(np.abs(envelope) ** 2, time_s))
        value = -np.pi * power / (area * area)
        cache[rf_raster_time_s] = value
        return value

    def flip_angle(
        self,
        amplitude_hz: Any,
        *,
        rf_raster_time_s: float = 1e-6,
    ) -> tuple[Any, Any]:
        """Return ``(flip_rad, integral_phase_rad)`` for one RF occurrence.

        Tensor amplitudes remain tensors, so both reverse- and forward-mode
        automatic differentiation pass through a description unchanged.

        Parameters
        ----------
        amplitude_hz : float or torch.Tensor
            Scale applied to the normalized envelope, in Hz.
        rf_raster_time_s : float, default=1e-6
            RF raster that the sample times are counted in, in seconds.

        Returns
        -------
        flip_rad : float or torch.Tensor
            Flip angle, ``2 pi |amplitude * integral|``, in radians.
        integral_phase_rad : float or torch.Tensor
            Phase of ``amplitude * integral``, in radians.
        """
        area = self.integral(rf_raster_time_s=rf_raster_time_s)
        try:
            import torch
        except ImportError:  # pragma: no cover - Torch is a required dependency
            torch = None
        if torch is not None and isinstance(amplitude_hz, torch.Tensor):
            # The area is a Python number, so it multiplies a tensor without
            # being made one first. A pulse whose envelope integrates to a real
            # number -- every shape that is symmetric about its centre, which
            # is most of them -- keeps the product real, and a real product is
            # both cheaper per event and the only kind the real-subspace
            # kernels can be handed.
            signed_area = amplitude_hz * (area.real if area.imag == 0.0 else area)
            return 2.0 * torch.pi * torch.abs(signed_area), torch.angle(signed_area)
        signed_area = amplitude_hz * area
        return 2.0 * np.pi * abs(signed_area), np.angle(signed_area)


[docs] @dataclass(frozen=True) class ShimDefinition: """Transmit shim: a complex weight per channel for static parallel transmit. Every channel plays the same waveform; the weights hold for the whole pulse. Attributes ---------- id : int Identifier RF events name the shim by. magnitudes : tuple of float Weight per transmit channel. phases_rad : tuple of float Phase per transmit channel, in radians. """ id: int magnitudes: tuple[float, ...] phases_rad: tuple[float, ...]
[docs] @dataclass(frozen=True) class SequenceDescription: """Event stream and RF resources of one repetition of a sequence. The ratio ``crusher_dephasing_rad / voxel_size_m`` sets the b-factor diffusion is damped by and the phase each dephasing order acquires under flow; a zero dephasing leaves both out. ``voxel_size_m`` also sets the distance a moving spin crosses, which washes out the voxel; ``None`` declares no voxel, so spin velocity has no effect. Attributes ---------- subsequence_index : int Index of this subsequence within a multi-part acquisition. tr_duration_us : float or torch.Tensor Duration of one playing of the stream, in microseconds. events : tuple of SequenceEvent Events in play order. rf_definitions : dict ``{id: RfDefinition}`` for the pulses the RF events name. shim_definitions : dict ``{id: ShimDefinition}`` for the shims the RF events name. crusher_dephasing_rad : float Dephasing wound across one voxel by the unbalanced gradient, in radians. voxel_size_m : float, default=None Voxel length the dephasing is wound across, in metres. rf_raster_time_s : float Dwell the RF shapes are sampled on, in seconds. Pulses are integrated against it. """ subsequence_index: int tr_duration_us: Any events: tuple[SequenceEvent, ...] rf_definitions: dict[int, RfDefinition] shim_definitions: dict[int, ShimDefinition] = field(default_factory=dict) crusher_dephasing_rad: float = 0.0 voxel_size_m: float | None = None rf_raster_time_s: float = 1e-6
[docs] @classmethod def from_operators( cls, *parts: Any, rf_definitions: Mapping[int, RfDefinition] | None = None, shim_definitions: Mapping[int, ShimDefinition] | None = None, subsequence_index: int = 0, crusher_dephasing_rad: float = 0.0, voxel_size_m: float | None = None, start_s: Any = 0.0, ) -> SequenceDescription: """Build a description by laying operators out end to end. Parameters ---------- *parts : Operator or tuple Operators in play order, or ``(offset_s, operator)`` pairs. rf_definitions : mapping, default=None ``{id: RfDefinition}``. One ideal pulse under id 0 when not given. shim_definitions : mapping, default=None ``{id: ShimDefinition}``. subsequence_index : int, default=0 Index of the subsequence within a scan. crusher_dephasing_rad : float, default=0.0 Dephasing of one crusher across a voxel, in radians. Diffusion and flow are zero without it. voxel_size_m : float, default=None Voxel length the dephasing is wound across, in metres. start_s : float or torch.Tensor, default=0.0 Start time of the first operator, in seconds. Returns ------- SequenceDescription Lasting as long as the operators laid out end to end. """ from ._operators import compose events, duration_s = compose(*parts, start_s=start_s) return cls( subsequence_index=subsequence_index, tr_duration_us=1e6 * duration_s, events=events, rf_definitions=dict( {0: ideal_rf_definition()} if rf_definitions is None else rf_definitions ), shim_definitions=dict(shim_definitions or {}), crusher_dephasing_rad=crusher_dephasing_rad, voxel_size_m=voxel_size_m, )
[docs] @classmethod def from_pulseq(cls, source: Any, **settings: Any) -> SequenceDescription: """Read one repetition of a Pulseq sequence. Parameters ---------- source : str, Path or sequence A ``.seq`` file, or an in-memory pypulseq or pypulseqpp ``Sequence``. **settings ``tr_index``, ``subsequence_index``, ``crusher_dephasing_rad`` and ``voxel_size_m``, as for ``read_pulseq_description``: ``tr_index`` is the repetition to read, in whole ``TRSize`` windows. Returns ------- SequenceDescription One repetition, with events in microseconds from the start of the repetition. Raises ------ ValueError If the file declares no ``TRSize``, ``tr_index`` is out of range, or the repetitions differ and none was named. """ from ._pulseq import read_pulseq_description return read_pulseq_description(source, **settings)
[docs] def plot(self, axis: Any = None, *, upto_s: float | None = None) -> Any: """Plot the events: RF as stems of height equal to the flip, ADC as markers. Parameters ---------- axis : matplotlib.axes.Axes, default=None Axes to draw on; a new figure when not given. upto_s : float, default=None Plot events up to this time, in seconds. Returns ------- matplotlib.axes.Axes The axes drawn on; time in ms against flip angle in degrees. Raises ------ ImportError If matplotlib is not installed. """ try: import matplotlib.pyplot as plt except ImportError as reason: # pragma: no cover - depends on the env raise ImportError( "plotting a description needs matplotlib, which blochsim does " "not depend on; pip install matplotlib" ) from reason axis = plt.subplots()[1] if axis is None else axis limit = float("inf") if upto_s is None else upto_s * 1e6 for event in self.events: when = float(event.timestamp_us) if when > limit: break if event.type is EventType.RF: axis.vlines( when / 1000.0, 0.0, float(event.rf_amplitude_hz) * 180.0 / np.pi, color="crimson", lw=2.5, ) elif event.type is EventType.ADC: axis.plot(when / 1000.0, 0.0, "v", color="tab:blue", ms=9) axis.plot([], [], color="crimson", lw=2.5, label="RF, height is the flip") axis.plot([], [], "v", color="tab:blue", ms=9, label="ADC") axis.set(xlabel="time [ms]", ylabel="flip angle [deg]") axis.grid(alpha=0.3) return axis
@property def adc_events(self) -> tuple[SequenceEvent, ...]: """Return ADC events in state-machine order.""" return tuple(event for event in self.events if event.type is EventType.ADC)
def decompress_shape( packed: Any, num_uncompressed: int, *, scale: float = 1.0, ) -> np.ndarray: """Decode a Pulseq derivative and run-length encoded shape. Parameters ---------- packed : array-like Encoded samples; the shape is stored as its differences, with a run of equal differences written as the value twice and a repeat count. num_uncompressed : int Number of samples the shape decodes to. An array of that length is taken as already decoded. scale : float, default=1.0 Factor applied to the decoded samples. Returns ------- numpy.ndarray ``(num_uncompressed,)``, float32. Raises ------ ValueError If the length is negative, or the encoding is truncated, has an invalid repeat count or does not expand to ``num_uncompressed``. """ packed = np.asarray(packed, dtype=np.float32).reshape(-1) if num_uncompressed < 0: raise ValueError("negative uncompressed shape length") if packed.size == num_uncompressed: return (packed * scale).astype(np.float32, copy=True) if num_uncompressed == 0 and packed.size == 0: return np.empty(0, dtype=np.float32) delta = np.empty(num_uncompressed, dtype=np.float32) source = 0 target = 0 while source < packed.size: value = packed[source] if source + 1 < packed.size and packed[source + 1] == value: if source + 2 >= packed.size: raise ValueError("truncated Pulseq RLE triplet") count_value = float(packed[source + 2]) + 2.0 count = round(count_value) if abs(count_value - count) > 1e-6 or count < 2: raise ValueError("invalid Pulseq RLE repeat count") if target + count > num_uncompressed: raise ValueError("Pulseq RLE shape expands past declared length") delta[target : target + count] = value target += count source += 3 else: if target >= num_uncompressed: raise ValueError("Pulseq shape expands past declared length") delta[target] = value target += 1 source += 1 if target != num_uncompressed: raise ValueError( f"Pulseq shape expanded to {target}, expected {num_uncompressed}" ) return (np.cumsum(delta, dtype=np.float32) * scale).astype(np.float32)
[docs] def rf_definition( waveform: Any, *, dwell_s: float, bandwidth_hz: float = 0.0, definition_id: int = 0, band_frequency_offsets_hz: Any = None, band_bandwidth_hz: float | None = None, rf_raster_time_s: float = 1e-6, ) -> RfDefinition: """Return the RF definition of a pulse envelope. The envelope is rescaled so that it integrates to ``1 / (2 pi)`` seconds, making an event amplitude in radians the flip angle in radians. The waveform's own phase is kept. Parameters ---------- waveform : array-like Complex envelope in arbitrary amplitude units, ``(samples,)`` or ``(channels, samples)``. dwell_s : float Sample duration, in seconds. bandwidth_hz : float, default=0.0 Pulse bandwidth, in Hz. Non-zero makes the pulse slice-selective. definition_id : int, default=0 Identifier events name the pulse by. band_frequency_offsets_hz : sequence of float, default=None Band centres of a multiband pulse, in Hz; one to eight values. ``None`` is one band at 0 Hz. band_bandwidth_hz : float, default=None Bandwidth of one band, in Hz. ``None`` is ``bandwidth_hz``. rf_raster_time_s : float, default=1e-6 RF raster the sample times are expressed in, in seconds. Returns ------- RfDefinition Normalized envelope as magnitude and phase (in cycles) shapes on the sample times ``0, dwell_s, 2 dwell_s, ...``. Raises ------ ValueError If the envelope is empty, ``dwell_s`` or ``rf_raster_time_s`` is not positive, the band count is outside one to eight, or the waveform integrates to zero. """ samples = np.asarray(waveform) if samples.ndim == 1: samples = samples[None, :] if samples.ndim != 2 or samples.size == 0: raise ValueError( "an RF envelope is (samples,) or (channels, samples), and not empty" ) if not dwell_s > 0.0: raise ValueError(f"dwell_s must be positive, not {dwell_s!r}") if not rf_raster_time_s > 0.0: raise ValueError(f"rf_raster_time_s must be positive, not {rf_raster_time_s!r}") count = samples.shape[-1] # In units of the raster, which is how a description carries sample times # and how a run reads them back. times = RfShape( count, (np.arange(count, dtype=np.float64) * dwell_s / rf_raster_time_s).astype( np.float32 ), ) def shapes(values: np.ndarray) -> Any: rows = tuple(RfShape(count, row.astype(np.float32)) for row in values) return rows[0] if len(rows) == 1 else rows def built(values: np.ndarray, **extra: Any) -> RfDefinition: return RfDefinition( id=definition_id, bandwidth_hz=float(bandwidth_hz), num_bands=len(offsets), band_frequency_offsets_hz=offsets, band_bandwidth_hz=float( bandwidth_hz if band_bandwidth_hz is None else band_bandwidth_hz ), magnitude=shapes(np.abs(values)), phase=shapes(np.angle(values) / (2.0 * np.pi)), time=times, **extra, ) offsets = ( (0.0,) if band_frequency_offsets_hz is None else tuple(float(value) for value in band_frequency_offsets_hz) ) if not 1 <= len(offsets) <= 8: raise ValueError( f"a pulse carries one to eight bands, not {len(offsets)}: that is " f"how many a description has room for" ) # Scaled against the integral the definition itself reads, rather than one # taken here: the two would have to agree about the quadrature and about # what the last sample's duration is, and only one of them decides. area = abs( built(samples, total_b1sq_power=0.0).integral(rf_raster_time_s=rf_raster_time_s) ) if area <= 0.0: raise ValueError( "this envelope integrates to nothing, so it has no flip angle to " "be read against" ) scaled = samples / (2.0 * np.pi * area) return built(scaled, total_b1sq_power=float((np.abs(scaled) ** 2).sum() * dwell_s))
def ideal_rf_definition(definition_id: int = 0) -> RfDefinition: """Return the definition of a pulse that turns instantly and selects nothing. Its envelope integrates to exactly ``1 / (2 pi)`` seconds, so an event amplitude expressed in radians comes back unchanged as the flip that event turns. It declares no bandwidth, which is what keeps a sequence built from it on the instant operator rather than on a tabulated rotation. Parameters ---------- definition_id : int, default=0 The identifier events name this pulse by. Returns ------- RfDefinition The ideal hard pulse. """ duration_us = 1e6 / (2.0 * np.pi) return RfDefinition( id=definition_id, bandwidth_hz=0.0, num_bands=1, band_frequency_offsets_hz=(0.0,), band_bandwidth_hz=0.0, total_b1sq_power=0.0, magnitude=RfShape(2, np.ones(2, dtype=np.float32)), time=RfShape(2, np.asarray([0.0, duration_us], dtype=np.float32)), )