Skip to main content

Tests PyPI Docs License: MIT

blochsim

blochsim simulates the signal of an MR sequence, and its derivatives, in PyTorch. You describe your sequence once, as a signal model, and blochsim plays it over thousands of voxels at once, on the CPU or on an NVIDIA GPU.

Everything else is written once against that signal model: the derivative with respect to tissue that a parameter fit needs, the derivative with respect to the sequence that a protocol design needs, dictionary matching, model-based reconstruction, and reading the same sequence back from a Pulseq file or from the stream a scanner sends. Change the signal model and all of them follow.

blochsim ships signal models for 26 sequences (spin echo, FSE, SPGR, bSSFP, MPRAGE, MP2RAGE, MR fingerprinting, Look-Locker, CEST, ASL...), so for most work you write nothing at all.

How it works

  1. You construct a simulator with the sequence parameters: the flip angles, the TR, the echo spacing.
  2. You call simulate with the tissue: T1, T2, and any other property you care about (B1, off-resonance, diffusion, a second exchanging pool...). A scalar is one voxel, an array is a map, and every voxel runs at once. Naming a property is what turns its physics on; what you leave out costs nothing.
  3. blochsim turns the sequence into a stream of events (pulses, delays, readouts) once, and plays it through an extended phase graph (EPG) state machine, compiled for the CPU and the GPU. Calling again with new numbers reuses the stream.
  4. jacobian gives you the derivative with respect to tissue properties, which is what a fit descends. The flip angles are ordinary PyTorch tensors, so loss.backward() gives you the derivative with respect to the sequence, which is what a design optimizes.

Shipped sequences

import torch

from blochsim.simulators import FSESimulator

fse = FSESimulator(ESP=5.0, TR=3000.0)  # ms
flip = torch.full((48,), 180.0)  # degrees, one per echo

signal = fse.simulate(flip=flip, T1=1000.0, T2=torch.tensor([80.0, 110.0, 2000.0]))
signal, dT2 = fse.jacobian("T2", flip=flip, T1=1000.0, T2=80.0)
  • signal has one row per voxel and one column per echo.
  • Times are in milliseconds and angles in degrees, as on a scanner console.
  • Every shipped simulator also has a function form for one-off calls, such as blochsim.fse_sim(...).

Custom signal models

A signal model is two things:

  • handlers: which operator plays each kind of event. A sequence has excitations, readouts and waits, and may have refocusing, inversion, saturation, preparation and other pulses, one handler for each Pulseq RF use. Each is a class attribute; one you do not set keeps its default (a wait is Delay: free precession, no RF and no ADC).
  • layout: the events of one repetition, in order, written with those handlers. It is the sequence's default description: the constructor builds it from the parameters you pass. from_pulseq and from_description read the description from a .seq file or a scanner's stream instead, and skip the layout.

Here is saturation recovery, which saturates, waits, and reads what came back:

import torch

from blochsim import Delay, Excitation, SPGRReadout, Spoil
from blochsim import Simulator


def Saturate(flip_rad=torch.pi / 2, phase_rad=0.0):
    """A 90-degree pulse, then a spoiler."""
    return Excitation(flip_rad, phase_rad) @ Spoil()


class SaturationRecovery(Simulator):
    saturation = Saturate  # plays a saturation pulse
    excitation = Excitation  # plays an excitation pulse
    delay = Delay  # plays a wait
    readout = SPGRReadout  # plays a readout, then spoils
    states = 1

    def layout(self, *, TS, flip):
        angle = torch.deg2rad(torch.as_tensor(flip))
        parts = []
        for wait in torch.as_tensor(TS) * 1e-3:  # ms to s
            parts += [self.operators.saturation(), self.operators.delay(wait)]
            parts += [self.operators.excitation(angle), self.operators.readout()]
        return parts


recovery = SaturationRecovery(TS=[100.0, 400.0, 1600.0], flip=10.0)
signal = recovery.simulate(T1=830.0, T2=80.0)
  • layout uses only self.operators, the handlers you set. You never write timestamps; each operator holds the timeline for as long as it lasts.
  • The handlers play the events whichever way the description came, so a sequence read with from_pulseq or from_description still uses them. A Pulseq file or a scanner stream carries pulses, ADC windows and timing but no gradients, so the handlers are where the dephasing lives: the readout decides whether the states are spoiled, wound on or rewound.
  • @ composes two operators into one, so a pulse and its spoiler read as the single thing you would name.

Derivatives, devices, extra physics and the estimators all work on your class without another line.

Estimation, design and reconstruction

  • Parameter maps: dictionary matching, lookup tables and PERK, all built from a simulator (blochsim.estimators).
  • Model-based reconstruction: the signal model as a factor of the forward operator, linear (blochsim.Subspace) or nonlinear (blochsim.ModelOperator). bartorch supplies the encoding and the Gauss-Newton solvers, and fits the same model voxel by voxel with apps.mobafit.
  • Sequence design: flip angles, timings or RF pulse samples optimized by gradient descent through the simulation, within the scanner's limits (blochsim.optim.SequenceDesign).
  • Sequences you did not write: Simulator.from_pulseq reads a .seq file, and Simulator.from_description reads the sequence description pulserver streams from a running scanner.

Installation

Install the PyTorch build for your machine first, then blochsim:

pip install blochsim
pip install "blochsim[cu12]"  # the GPU kernels, beside a CUDA 12 build of torch

The User guide covers CUDA versions, macOS and building from source.

Learn more

  • Course: five lessons, from a shipped simulator to your own signal model and your own operator.
  • Applications: parameter mapping, sequence design, model-based reconstruction and synthetic data, each a complete worked example.
  • Documentation, for the physics and every API detail.

How to cite

If you use blochsim in your work, please cite:

@inproceedings{cencini2025pulserver,
  title     = {Pulserver: an open-source Pulseq-based client-server framework for vendor agnostic, interactive {MR} sequence design},
  author    = {Cencini, Matteo and Wang, Kang and Huang, Sherry and Schulte, Rolf F. and Sprenger, Tim
               and Noll, Douglas C. and Tosetti, Michela and Nielsen, Jon-Fredrik},
  booktitle = {Proceedings of the International Society for Magnetic Resonance in Medicine},
  pages     = {1275},
  year      = {2025}
}

blochsim's state machine is the extended phase graph formalism (Weigel, 2015); please cite it too.

License

MIT, see LICENSE.

Metadata

Release files for blochsim 0.0.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blochsim 0.0.21
File Size Uploaded Provenance Actions
blochsim-0.0.21.tar.gz 20.8 MB With provenance Details for blochsim-0.0.21.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for blochsim 0.0.21
File Actions
blochsim-0.0.21-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for blochsim-0.0.21-cp310-abi3-win_amd64.whl
blochsim-0.0.21-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for blochsim-0.0.21-cp310-abi3-musllinux_1_2_x86_64.whl
blochsim-0.0.21-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details for blochsim-0.0.21-cp310-abi3-musllinux_1_2_aarch64.whl
blochsim-0.0.21-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details for blochsim-0.0.21-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
blochsim-0.0.21-cp310-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.27+ ARM64, Linux glibc 2.28+ ARM64 Details for blochsim-0.0.21-cp310-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
blochsim-0.0.21-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for blochsim-0.0.21-cp310-abi3-macosx_11_0_arm64.whl
blochsim-0.0.21-cp310-abi3-macosx_10_9_x86_64.whl CPython 3.10 abi3 macOS 10.9+ x86-64 Details for blochsim-0.0.21-cp310-abi3-macosx_10_9_x86_64.whl

Total release size: 33.0 MB

Release files / blochsim-0.0.21.tar.gz

Download URL blochsim-0.0.21.tar.gz
Size 20.8 MB
Tags Source
SHA-256 checksum
How to use checksums
025c4711074c5d0bc6f4231aaa9f4c2d18d43a63533feb2c9ee4dc2a9c9f9ae3
BLAKE2b-256 checksum
How to use checksums
ee26a0f5ac26de4c71c815519d4f51765fdd52ba0e77b0974659720660a3e0f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-win_amd64.whl

Download URL blochsim-0.0.21-cp310-abi3-win_amd64.whl
Size 1.0 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
553ae5b0d56661a991c9dfbe6c57da3376f4448a48cf44a47b3195f1ce550d0c
BLAKE2b-256 checksum
How to use checksums
967cc73328617c502e97835e57cb7c4dbcc5c0cc6943c9cfd0ceae6f76732bf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL blochsim-0.0.21-cp310-abi3-musllinux_1_2_x86_64.whl
Size 2.7 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
1f7dd7fd26730d0ebd97beda3754af635e3126a73ae73897b623121db5c16b26
BLAKE2b-256 checksum
How to use checksums
252da10f3e27d0222505e34d9f0115dcbfbc3ad18cb14b9566f5955c8042cb75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL blochsim-0.0.21-cp310-abi3-musllinux_1_2_aarch64.whl
Size 2.6 MB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
bf68beccfad47e38f87d88e758a10f28e147783eed226c2a77b08485915b21f4
BLAKE2b-256 checksum
How to use checksums
07533c8bda76fec8b99d3f310b05c6765d7ef61764051ea7d16e99e23147e987
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL blochsim-0.0.21-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 2.0 MB
Tags CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
2a3e813aeb089d873408c36509a017a1ff921202b206b47d7af5df809618169e
BLAKE2b-256 checksum
How to use checksums
590105d8c3af984a9c216d21898ba55be2fcf562e1b871a6f872df2d72f5b0b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl

Download URL blochsim-0.0.21-cp310-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Size 1.6 MB
Tags CPython 3.10 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
80909b1a093fc1ca795e07fa1149517ba76483250f3aaa9d6288e8d7db566e0b
BLAKE2b-256 checksum
How to use checksums
52777977de3984b8754851de50c5febcb2047df5d3d3f9a1835535949f6c1575
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-macosx_11_0_arm64.whl

Download URL blochsim-0.0.21-cp310-abi3-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
29f1261d08677c3edd7286767b828cb52d8def1bb4e9d0d830f3473eb9be42b2
BLAKE2b-256 checksum
How to use checksums
cefdc8ecef0bf92466a26967b6e5169627610bdadb1b619d024a4f9a8d238120
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / blochsim-0.0.21-cp310-abi3-macosx_10_9_x86_64.whl

Download URL blochsim-0.0.21-cp310-abi3-macosx_10_9_x86_64.whl
Size 1.3 MB
Tags CPython 3.10 abi3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
6c9b39c2724a821668c8b03df7671a353e6035d860f09087b2930d7afd2779c9
BLAKE2b-256 checksum
How to use checksums
6136178ff54627bea9520e7b8bc76c63d6dd7543957f6cdcfc23167623a9d1e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for blochsim 0.0.21

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for blochsim-0.0.21.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page