Getting started#
For every installation option (CPU-only PyTorch, GPU, the optional extras), see Installation.
Simulate and fit EPR/ESR spectra in Python — differentiable, GPU-capable, and validated against EasySpin.
torchspin reimplements the core physics engine of EasySpin (MATLAB) in
Python/PyTorch. You get the simulators you already know — pepper, garlic,
chili, salt, saffron, curry, cardamom, esfit — with the same units
and conventions, plus three things MATLAB cannot give you:
Gradients. Spectra are differentiable end to end, so you can fit with gradient descent, propagate uncertainty, or put a simulator inside a neural network.
A Python workflow. NumPy and torch tensors in and out, no license server, installable with
pip.Optional GPU.
Options(device='cuda')for batched and gradient-based work.
torchspin is a reimplementation, not a drop-in replacement for every EasySpin feature. Before relying on it for published results, read KNOWN_LIMITATIONS.md — it states per simulator what is validated against MATLAB and what is not.
Installation#
pip install torchspin # core
pip install "torchspin[plot]" # + matplotlib for the plotting helpers
pip install "torchspin[gui]" # + the interactive fitting panel (torchspin.fitgui)
pip install "torchspin[notebooks]" # + Jupyter and everything the tutorials need
torchspin cannot choose the PyTorch build for you. Install the build you want first, and torchspin keeps it:
# CPU only (on Linux this avoids several GB of CUDA packages)
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install torchspin
# NVIDIA GPU on Windows, or a specific CUDA version on Linux (pick the cuXXX for your driver)
pip install torch --index-url https://download.pytorch.org/whl/cu130
pip install torchspin
# uv
uv pip install torchspin --torch-backend=cpu # or auto, or cu130
On Linux, plain pip install torchspin already installs a CUDA build of PyTorch. On macOS torchspin runs
on the CPU (MPS lacks float64). Intel Macs are not supported, because the last PyTorch for them is 2.2.
Requires Python 3.10+, PyTorch 2.3+, NumPy 1.23+, SciPy 1.10+. Run the pip commands in a virtual environment
you have created and activated (python -m venv .venv). uv pip needs one made with uv venv.
docs/installation.md covers the details: per-platform notes, keeping an existing PyTorch, uv projects, the extras, and what package metadata can and cannot enforce.
Documentation#
https://follmerlab.github.io/torchspin/ has a user guide, the tutorials
below, and an API reference for every public function and class with the
parameters it accepts. Its sources are in docs/; CI builds it
on every change and publishes it from main. To build it locally:
pip install -e ".[docs]"
make -C docs html # then open docs/_build/html/index.html
Tutorials#
Start with 01_torchspin_quickstart.ipynb, then branch to autograd or fitting
depending on what you need. Full index: docs/index.md.
Notebook |
What it covers |
|---|---|
|
Core API: spin systems, Hamiltonians, powder and solution spectra |
|
GPU execution and differentiable spectra |
|
Parameter recovery with |
|
Timings against MATLAB/EasySpin |
|
Bad starting guesses, loss landscapes, global vs local search |
|
End-to-end Cu(II) case study |
|
Overlays against stored EasySpin references |
|
Simulate, add noise, fit with every |
|
Interactive fitting panel: sliders, live fit, RMSD trace |
They live in examples/notebooks/, with scripted equivalents of EasySpin’s
example collection in examples/{solidstate,liquids,slowmotion,endor,fitting,magnetometry}.
Quick start#
CW powder spectrum (pepper)#
from torchspin import SpinSystem, Experiment, Options, pepper
sys = SpinSystem(
S=[0.5],
g=[[2.009, 2.006, 2.002]],
Nucs=['14N'],
A=[[10.0, 10.0, 95.0]], # MHz
lw=[1.0, 0.0], # Gaussian FWHM (mT)
)
exp = Experiment(mwFreq=9.5, Range=[330, 350], nPoints=1024, Harmonic=1)
B, spec = pepper(sys, exp, Options(GridSize=50))
Solution EPR (garlic)#
from torchspin import SpinSystem, Experiment, garlic
sys = SpinSystem(S=[0.5], g=2.003, Nucs='1H', A=10.0, lw=[0.2, 0.0])
exp = Experiment(mwFreq=9.5, Range=[336, 342], nPoints=512, Harmonic=1)
B, spec = garlic(sys, exp)
Fitting (esfit)#
import numpy as np
from torchspin import esfit, FitOptions
def model(p):
sys_mod = SpinSystem(S=[0.5], g=[[p[0], p[1], p[2]]], lw=[1.0, 0.0])
_, spc = pepper(sys_mod, exp, Options(GridSize=31, Verbosity=0))
return spc.numpy()
result = esfit(
data_measured, model,
p0=np.array([2.00, 2.10, 2.20]),
lb=np.array([1.95, 2.05, 2.15]),
ub=np.array([2.05, 2.15, 2.25]),
options=FitOptions(method='global', progress='text'),
)
print('Best-fit g:', result.pfit)
progress='text' prints RMSD, evaluation count and ETA as it runs, and
interrupting the kernel returns the best fit so far.
With FitOptions(n_workers='auto'), model evaluations run in parallel worker
processes: each generation of the population methods (swarm, genetic, grid,
montecarlo, global), and the finite-difference probes of each levmar, trf and
lbfgsb Jacobian (1.5–3× faster for a 4-parameter pepper fit). This is off by default. Each worker is a separate Python process with its own torch
(about 0.3–0.5 GB), so 'auto' uses the cores you were allocated, at most 32, and
no more workers than fit in half the free memory. A script that uses it needs the
usual if __name__ == '__main__': guard around the code that runs the fit.
Interactive fitting panel (torchspin.fitgui)#
In JupyterLab (pip install "torchspin[gui]"), the same kind of fit runs in a panel
with parameter sliders, fix boxes, method controls, Start/Stop and a live plot of the
fit, residual and RMSD:
%matplotlib widget
import numpy as np
from torchspin import SpinSystem, Experiment, Options, pepper
from torchspin.esfit import FitOptions
from torchspin.fitgui import FitSession, FitPanel
exp = Experiment(mwFreq=9.5, Range=[330, 350], nPoints=1024, Harmonic=1)
def model(p): # p = [gx, gy, gz, Az (MHz), lwpp (mT)]
sys = SpinSystem(S=[0.5], g=p[:3], Nucs='14N', A=[[18, 18, p[3]]], lwpp=[p[4], 0])
return pepper(sys, exp, Options(GridSize=[31, 4], Verbosity=0))[1].numpy()
B = np.linspace(330, 350, 1024)
data = model([2.0085, 2.0060, 2.0025, 92.0, 0.35]) # or your measured spectrum
data = data + 0.02 * np.abs(data).max() * np.random.default_rng(1).standard_normal(data.size)
session = FitSession(data, model, p0=[2.011, 2.0085, 2.005, 101.0, 0.5],
vary=[0.004, 0.004, 0.004, 15.0, 0.25],
names=['gx', 'gy', 'gz', 'Az', 'lwpp'],
options=FitOptions(method='global', max_iter=100))
FitPanel(session, x=B, xlabel='B (mT)').show()
Press Start to fit; Stop returns the best fit so far, and session.result
holds the result afterwards. The panel’s n_workers box accepts auto for parallel
population fits. The full walkthrough is
examples/notebooks/09_fit_gui.ipynb.
Differentiable spectrum#
import torch
from torchspin import differentiable_spectrum
g = torch.tensor([2.0, 2.1, 2.2], dtype=torch.float64, requires_grad=True)
B, spec = differentiable_spectrum(g, mwFreq_GHz=9.5, B_range=(300, 380),
nPoints=512, lw_mT=1.0)
loss = ((spec - target_spec) ** 2).sum()
loss.backward()
print('dL/dg:', g.grad)
Units and conventions#
These match EasySpin, so parameters transfer directly:
Energy |
MHz |
Magnetic field |
mT |
Euler angles |
radians, z-y’-z’’ passive rotation |
Basis ordering |
m = S, S−1, …, −S (descending) |
Default dtype |
|
What’s included#
Category |
Modules |
|---|---|
CW EPR |
|
Pulse EPR |
|
Trajectory |
|
Magnetometry |
|
Fitting |
|
Autograd |
|
Machine learning |
|
Data I/O |
|
Batch / GPU |
|
Accuracy and performance#
Every simulator with an EasySpin counterpart is checked against stored MATLAB
reference output on every push and pull request to main (the full-suite CI job).
pepper and garlic agree at cosine ≥ 0.999 (most 0.9999–1.0000); salt at
≥ 0.999, or > 0.99 with orientation selection; curry within 0.5–1 % of the
maximum; absolute intensities are matched where EasySpin defines them. The pulse-EPR routes
(saffron, saffron_thyme, spidyan) are held to a stricter metric — the
relative L2 error on the raw and the mean-centered signal, 1e-16..1e-7 —
because a cosine is scale-invariant and dominated by a pulse-EPR trace’s DC
offset.
chili and the stochastic cardamom have documented caveats. Where EasySpin
and an objective check (a closed form, a converged grid, a rotation invariance)
disagree, torchspin follows the objective check: strain widths in tilted frames
are rotation covariant (0.9.33), and projected pepper/salt spectra use
centered bins instead of EasySpin’s half-bin offset (0.9.35). The EasySpin
comparisons those changes move are kept as strict expected failures with
their measured numbers. Details per simulator, with the evidence:
physics and validation
and KNOWN_LIMITATIONS.md.
On speed, torchspin is competitive rather than uniformly faster. On an audited
nine-workload comparison (measured on v0.2.2) it beat EasySpin on six — most strongly on
slow-motion chili (7.3×) and trajectory cardamom (2.0×) — and was slower on
three, including matrix-method Cu(II) powders (0.34×). The GPU path pays off
for batched and gradient-based work, not for single simulations. Numbers,
replicates and a per-workload device audit:
BENCHMARK_VERIFICATION.md.
Citation#
If you use torchspin in published work, please cite both:
TorchSpin — Follmer, A.H. arXiv:2610.18924 (2026) https://arxiv.org/abs/2610.18924.
EasySpin, the MATLAB toolbox this port is based on — Stoll, S. & Schweiger, A. J. Magn. Reson. 178 (2006) 42–55.
Contributing#
Bug reports, documentation fixes and pull requests are welcome. Development install and the test layout are in CONTRIBUTING.md:
git clone https://github.com/follmerlab/torchspin.git
cd torchspin
pip install -e ".[dev]"
pytest -q -m "not slow" -k "not matlab_validation" # fast pass, ~4 min
The MATLAB reference data ships in the repository but not in the PyPI package, so the cross-validation suite only runs from a checkout.
Acknowledgments#
torchspin was developed with help from Claude (Anthropic).
License#
MIT — see LICENSE.md. torchspin is derived from EasySpin, which is also MIT licensed; both copyrights are reproduced there.