This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.9.40 instead.
Reason given by maintainers: Incorrect pulse-EPR output: saffron ESEEM/HYSCORE modulation and spidyan amplitudes were wrong. Fixed in 0.5.0.
torchspin
A differentiable PyTorch framework for EPR/ESR spin-Hamiltonian and spectrum simulation, developed against EasySpin.
torchspin reimplements the core physics engine of EasySpin (MATLAB) in Python/PyTorch, with end-to-end autograd support, optional GPU execution, and a Python-native API.
TorchSpin is a reimplementation, not a drop-in replacement for every EasySpin feature. See KNOWN_LIMITATIONS.md and benchmarks/results/BENCHMARK_REPORT.md before using it for publication-critical calculations.
- 2659 passing tests, 0 failures, including 30 MATLAB-validation modules that compare
against stored EasySpin outputs (cosine ≥ 0.999 for
pepper,garlic,chili,salt/endorfrq,saffron,curry,spidyan; fittedesfitparameters within 3×10⁻⁴ in g of EasySpin's) - Competitive with EasySpin on a multi-core node, not uniformly faster: on
the audited matched-host campaign (nine workloads, five replicates, medians
with IQRs) torchspin's best CPU configuration is faster on six — slow-motion
chili7.3× and 1.5×,pepperMn(II) 2.8×, trajectorycardamom2.0×, a 20-callpepperfit loop 1.9×, perturbativepepper1.3× — and slower on three:pepperCu/2N matrix 0.34×,pepperstrain summation 0.82×,saffronHYSCORE 0.88×. Details and the device audit:benchmarks/results/workstation_20260904_verified/BENCHMARK_VERIFICATION.md - Differentiable spectra (autograd through the eigendecomposition) and a GPU path whose payoff is batched/gradient fitting rather than raw throughput
- Fitting:
esfitwith local (simplex, Powell, L-BFGS-B, Levenberg–Marquardt, bounded trust-region reflectivetrf), global (swarm, genetic, Monte Carlo, grid) andglobal(swarm + simplex polish) methods; population methods run in a process pool (FitOptions(n_workers='auto');pip install cloudpicklefor models defined in notebooks);progress='text'reports RMSD/evaluations/ETA live and a kernel interrupt returns the best fit so far
Installation
torchspin is not on PyPI yet; install from source:
git clone https://github.com/follmerlab/torchspin.git
cd torchspin
pip install -e . # core
pip install -e ".[plot]" # with matplotlib for plotting helpers
pip install -e ".[gui]" # with the interactive fitting panel
CPU-only PyTorch:
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install -e .
Requires Python 3.10+, PyTorch 2.0+, NumPy 1.23+, SciPy 1.10+.
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)
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.0, 2.1, 2.2]),
lb=np.array([1.95, 2.05, 2.15]),
ub=np.array([2.05, 2.15, 2.25]),
options=FitOptions(method='simplex'),
)
print('Best-fit g:', result.pfit)
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)
Interactive notebooks
An index of all tutorials and reference documents is in docs/index.md.
Example notebooks live in examples/notebooks/ and cover quickstart usage,
GPU/autograd workflows, spectral fitting, benchmark reproduction, and two
end-to-end fitting case studies.
01_torchspin_quickstart.ipynb- core API tour: spin systems, Hamiltonians, powder and solution spectra.02_gpu_and_autograd.ipynb- GPU execution and differentiable spectrum workflows.03_spectral_fitting.ipynb- parameter recovery withesfiton simulated spectra.04_benchmarks.ipynb- MATLAB/EasySpin vs torchspin timings from the 2026-09 campaign (CPU threads, GPU, worker processes) plus live timing.05_fitting_real_world.ipynb- realistic multi-parameter fitting example.06_blue_copper_fitting.ipynb- end-to-end blue copper Cu(II) fitting case study.07_easyspin_parity.ipynb- overlays of torchspin against stored EasySpin references (pepper, garlic, chili, saffron, cardamom) with cosine similarities.08_pepper_playground.ipynb- simulate apepperspectrum, add noise, then fit it with everyesfitmethod and compare recovery, residual and time.09_fit_gui.ipynb- interactive fitting panel (torchspin.fitgui,pip install "torchspin[gui]"): sliders, fix boxes, Start/Stop, live fit and RMSD trace.
Recommended starting point: open 01_torchspin_quickstart.ipynb, then move to
02_gpu_and_autograd.ipynb or 03_spectral_fitting.ipynb depending on whether
you care more about differentiable simulation or fitting.
Features
| Category | Modules |
|---|---|
| CW EPR | pepper (powder), garlic (solution), chili (slow-motion), salt (ENDOR) |
| Pulse EPR | saffron (ESEEM/HYSCORE), spidyan (arbitrary sequences), saffron_thyme (real pulses) |
| Trajectory | cardamom (MD/diffusion/jump), mdload, mdhmm |
| Magnetometry | curry (susceptibility, magnetization) |
| Fitting | esfit (10 methods: simplex, L-BFGS-B, Powell, Levenberg–Marquardt, trust-region reflective trf, grid, Monte Carlo, genetic, swarm, and global = swarm + simplex polish); torchspin.fitgui interactive panel |
| Autograd | differentiable_spectrum (pepper's own forward path via pepper_autograd); garlic, salt, saffron and curry are differentiable too |
| ML | torchspin.ml — parameter packing (ParamSpec), batched simulation, SpectrumDataset, differentiable losses |
| Data I/O | eprload (14 vendor formats), eprsave (BES3T), orca2torchspin |
| Batch/GPU | batch_pepper, batch_simulate; Options.device='cuda' |
Conventions
- Energy units: MHz throughout
- Field units: mT
- Euler angles: radians, z-y'-z'' passive rotation
- Basis ordering: m = S, S−1, …, −S (descending)
- Default dtype:
torch.complex128
Validation status
Every simulator with an EasySpin counterpart is validated against stored MATLAB
outputs (tests/data/*.mat, generated by the scripts in tests/); the
validation modules are torchspin/tests/test_*_matlab_validation.py; what
changed when is in CHANGELOG.md.
As of 2026-10-04 (2659 passed, 0 failed, 4 skipped, 3 xfailed, on the clean-room host):
pepper(powders, crystals, strains, frequency sweeps, non-equilibrium populations, photoselection, ordering, isotopologues),garlic,salt,endorfrq,saffron,spidyan,curry/blochsteady: cosine ≥ 0.999 (most 0.9999–1.0000), absolute intensities included where EasySpin defines them;chili: 60-case suite, 59 at cosine ≥ 0.999;esfit: fitted parameters within 3×10⁻⁴ (g) and 0.03 mT (linewidth) of EasySpin's on the shared cross-validation cases;cardamom: stochastic, cosine > 0.75 at 100 trajectories by design.
Performance against MATLAB/EasySpin (same nine workloads, one workstation):
benchmarks/results/workstation_20260904_verified/BENCHMARK_VERIFICATION.md
(audited and authoritative) with the earlier rounds in
benchmarks/results/BENCHMARK_REPORT.md; remaining gaps and open items:
KNOWN_LIMITATIONS.md.
Citation
If you use torchspin in published work, please cite both:
- EasySpin (the MATLAB toolbox this port is based on): Stoll, S. & Schweiger, A. J. Magn. Reson. 178 (2006) 42–55.
- TorchSpin (the PyTorch reimplementation):
See
CITATION.cffin the repository root.
Related
License
MIT. See LICENSE.md.
Metadata
Release files for torchspin 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchspin-0.3.0.tar.gz | 463.4 kB | With provenance | Details for torchspin-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchspin-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for torchspin-0.3.0-py3-none-any.whl |
Total release size: 966.0 kB
Release files / torchspin-0.3.0.tar.gz
| Download URL | torchspin-0.3.0.tar.gz |
|---|---|
| Size | 463.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc5de0507d7f87bc69cc2770e58b04bff565cc97de5245e8bfd7bd8c7f7d117a
|
|
BLAKE2b-256 checksum How to use checksums |
1ddddfa85c996f693307ccfa15cbdeab0ebcecb86a6e7e56b25491bc93437143
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 5, 2026.
Transparency logRelease files / torchspin-0.3.0-py3-none-any.whl
| Download URL | torchspin-0.3.0-py3-none-any.whl |
|---|---|
| Size | 502.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea6894068725670ad1a977723c5c60736c31ac087fd10d1939807e175222455e
|
|
BLAKE2b-256 checksum How to use checksums |
c576e9145bc577c8b531d262c3cc580bb80989611f48f61a77b59963cb4008c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 5, 2026.
Transparency logSecurity for torchspin 0.3.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for torchspin-0.3.0.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 Oct 5, 2026.
Transparency logA 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.