Skip to main content

optimalcontrol

Python/Rust package for NMR spin dynamics implementing analytical ROPE/CROP theories, numerical GRAPE optimisation, and the Seedless band/restraint front-end for isolated spin-1/2 pulse design, with a Spinach-compatible Python API, ensemble support, and paper-reproduction examples. GRAPE propagation, exact coherent gradients, Bloch ensemble profiles, and the analytic spin-1/2 Seedless kernel run in a parallel native Rust extension.

Installation

pip install optimalcontrol-nmr

The import name is still optimalcontrol — only the PyPI distribution is named optimalcontrol-nmr (the bare name is blocked by an existing similar project).

Python 3.10 or newer is required. Environments without a compatible prebuilt wheel also need a stable Rust toolchain so pip can compile the native extension from the sdist.

Getting started

New to the package? Follow the step-by-step beginner manual in docs/user_manual.md: install, design a pulse by two different routes (GRAPE and Seedless), read the result, verify it against an independent Bloch model, and write a shape file. The topic-specific docs/guide_*.md files cover each subsystem in depth.

MCP server (use from Claude and other LLM clients)

The package ships an MCP server exposing the Seedless route, so an LLM client can design and verify pulses conversationally.

Easiest — install as a Claude Code plugin (this repository is its own plugin marketplace; requires uv, no pip install needed — the server is fetched from PyPI automatically):

/plugin marketplace add deepnmr/optimalcontrol
/plugin install optimalcontrol@optimalcontrol

Or register the MCP server directly:

pip install "optimalcontrol-nmr[mcp]"
claude mcp add optimalcontrol -- optimalcontrol-mcp   # Claude Code

No standing server is involved — the client launches optimalcontrol-mcp on demand and talks to it over stdio. Two tools are exposed:

  • design_seedless_pulse — declarative band/restraint input; runs a multi-seed phase-only optimisation, verifies the winner against the independent Bloch model, and writes a Bruker .shape file plus a design JSON. Returns file paths and summary numbers only.
  • bloch_offset_profile — reloads a design JSON and computes the mx/my/mz offset response with the independent Bloch model.

Any MCP-capable client works; for others, register the same optimalcontrol-mcp command in that client's server configuration.

Source references

  • JMR 2003 (ROPE): Unterbeck & Glaser, Journal of Magnetic Resonance 160 (2003) 88–101 — analytical optimal control for heteronuclear transfer under relaxation.
  • PNAS 2003 (CROP): Unterbeck & Glaser, Proc. Natl. Acad. Sci. USA 100 (2003) 5172–5177 — cross-correlated relaxation-optimised pulses.
  • Nat. Commun. 2025 (Seedless): Buchanan et al., Nature Communications 16, 7276 (2025) — "Seedless: on-the-fly pulse calculation for NMR experiments"; the band/restraint formalism implemented by optimalcontrol.ocseed.
  • Spinach: https://spindynamics.org/wiki/index.php?title=Main_Page — MATLAB spin dynamics library whose grape_xy / control struct API this package mirrors.

Local development commands

Install a stable Rust toolchain (rustc and cargo) first. On macOS with Homebrew:

brew install rust
# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Lint
ruff check .

# Typecheck
mypy optimalcontrol

# Test
python3 -m pytest

Set OPTIMALCONTROL_DISABLE_RUST=1 to run the NumPy/SciPy fallback for numerical comparisons. Normal installations build and use the Rust extension automatically.

Performance

On an Apple Silicon development machine, the native path reduced the full example regression runtime from 20.08 s to 2.91 s (6.9x). The focused 72-slice GRAPE benchmark improved single-member gradient evaluation by 2.2x and five-member ensemble gradient evaluation by 3.4x. Run python benchmarks/bench_grape_hotpath.py and pytest tests/test_examples.py to measure the local machine.

Symmetric methyl 180 pulse with water preservation

python -m examples.methyl_water_binary_symmetric_180 writes a Bruker shape and a dense-grid diagnostic plot for a 1.2 GHz proton spectrometer with the carrier at water (4.7 ppm). The 1.740 ms pulse covers methyl protons from -3 to 3 ppm, preserves water Iz, uses only 0/180 degree phase, is exactly time symmetric, and is capped at 10 kHz. It implements the inversion-quality requirement motivated by Kay's methyl-HMQC refocusing-artifact analysis (JBNMR 2019).

The cached candidate was validated at 2401 methyl offsets and 9 water offsets. Worst fidelities are 0.999186 (Ix -> Ix), 0.999128 (-Iy -> Iy), 0.999098 (Iz -> -Iz), and 0.999892 for water Iz -> Iz; the worst predicted inner artifact is 0.09027% of the central line. It is the shortest passing point on the tested local duration grid: 1.740 ms passed while 1.735 ms failed. This is a numerical grid-search result, not a proof of the continuous global minimum. The recorded duration audit is in examples/expected/methyl_water_binary_symmetric_180_duration_search.csv.

Versioning policy

This package follows semantic versioning: MAJOR.MINOR.PATCH.

  • PATCH releases contain backwards-compatible fixes only and must not introduce breaking API, file-format, or numerical-contract changes.
  • MINOR releases may add features and deprecate existing APIs; deprecations must emit warnings before removal.
  • MAJOR releases may remove deprecated APIs or introduce intentional breaking changes, with migration notes recorded in CHANGELOG.md.

Metadata

Release files for optimalcontrol-nmr 0.5.0

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

Source distribution (sdist)

Source distribution for optimalcontrol-nmr 0.5.0
File Size Uploaded Provenance Actions
optimalcontrol_nmr-0.5.0.tar.gz 265.3 kB Without provenance Details for optimalcontrol_nmr-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for optimalcontrol-nmr 0.5.0
File Interpreter ABI Platform Actions
optimalcontrol_nmr-0.5.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details for optimalcontrol_nmr-0.5.0-cp314-cp314-macosx_11_0_arm64.whl

Total release size: 785.6 kB

Release files / optimalcontrol_nmr-0.5.0.tar.gz

Download URL optimalcontrol_nmr-0.5.0.tar.gz
Size 265.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a56ed5329a5212bd354a0eaf50900646d7cc14b33ee8bd631587e774f866a7d0
BLAKE2b-256 checksum
How to use checksums
8f9c9d060eb3dcfdfcc8c4ceb976109d71233909823a053b8e82e6f0dacd2324
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / optimalcontrol_nmr-0.5.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL optimalcontrol_nmr-0.5.0-cp314-cp314-macosx_11_0_arm64.whl
Size 520.3 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
32b50b48a22d45fb52d905c9e29d579444edaf1d7414ab4aed3b38c6dfb779b1
BLAKE2b-256 checksum
How to use checksums
4f7bc81091a25249ca4d537be4ecb661e298286aee5b8dbf6c331c013d1c261e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for optimalcontrol-nmr 0.5.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files No provenance

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