torchspin 0.6.4__tar.gz → 0.6.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {torchspin-0.6.4 → torchspin-0.6.6}/CHANGELOG.md +47 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/CITATION.cff +2 -2
- {torchspin-0.6.4 → torchspin-0.6.6}/PKG-INFO +44 -3
- {torchspin-0.6.4 → torchspin-0.6.6}/README.md +43 -2
- {torchspin-0.6.4 → torchspin-0.6.6}/pyproject.toml +1 -1
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/__init__.py +1 -1
- torchspin-0.6.6/torchspin/_resources.py +115 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/curry.py +3 -3
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/esfit.py +11 -7
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/utils.py +9 -9
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin.egg-info/PKG-INFO +44 -3
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin.egg-info/SOURCES.txt +1 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/LICENSE.md +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/MANIFEST.in +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/setup.cfg +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_cardamom_istos.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_cardamom_propagatedm.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_cardamom_utils.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_compile.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_linalg.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_omp.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/_rng.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/angmom.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/autograd.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/autoguess.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/batch.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/blochsteady.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/cardamom.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/chili.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/chili_sle.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/constants.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/convspec.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ctafft.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/data/FourierSeriesCoefficients.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/data/GaussianCascadeCoefficients.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/data/isotopedata.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/data/spacegroups.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/dataproc.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/dipbackground.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/dipkernel.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/diptensor.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/endorfrq.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/endorfrq_perturb.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/eprload.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/eprsave.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/evolve.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ewrls.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/excitation.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/exciteprofile.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/experiment.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/exponfit.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/fastmotion.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/fdaxis.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/fitgui.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/garlic.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/gridcheck.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_cf.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_ee.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_ez.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_ezho.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_hf.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_nn.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_nq.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_nz.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_oz.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_so.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ham_zf.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/hamsymm.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/initstate.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/isotopologues.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/levels.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/levelsplot.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/lineshape.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/lpsvd.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/makespec.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/mdhmm.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/mdload.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/mdtraj2oripot.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ml.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/mlpsvd.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/nucdata.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/nucfrq2d.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/orca2torchspin.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/ordering.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/oripotentialplot.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/orisel.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/pepper.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/pepper_autograd.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/photoselect.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/plegendre.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/propint.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/pulse.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/py.typed +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/rapidscan2spc.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfields.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfields_batch.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfields_eig.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfields_hybrid.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfields_perturb.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfreqs_matrix.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resfreqs_perturb.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resonator.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/resonatorprofile.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/rfmixer.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/rotations.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/rotutils.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/saffron.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/saffron_pathways.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/saffron_peaks.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/saffron_thyme.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/salt.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/sigeq.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/signalprocessing.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/sitetransforms.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/sphgrid.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/spidyan.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/spinladder.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/spinops.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/spinsystem.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/stackplot.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/stev.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/stochtraj_diffusion.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/stochtraj_jump.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/strainwidth.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin/transmitter.py +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin.egg-info/dependency_links.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin.egg-info/requires.txt +0 -0
- {torchspin-0.6.4 → torchspin-0.6.6}/torchspin.egg-info/top_level.txt +0 -0
|
@@ -6,6 +6,53 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.6.6] — 2026-11-09
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- **Every cm⁻¹ conversion in `unitconvert` was off by a factor of 100**
|
|
14
|
+
(in every public release, 0.3.0–0.6.5). `CLIGHT` is stored in cm/s, but the
|
|
15
|
+
conversions multiplied it by 100 again, as if it were in m/s:
|
|
16
|
+
`unitconvert(1, 'cm^-1->MHz')` returned 2 997 924.58 instead of
|
|
17
|
+
29 979.2458, and 1 cm⁻¹ came out as 143.9 K instead of 1.4388 K. This
|
|
18
|
+
affected all eight `cm^-1` conversions (to and from MHz, mT, K and eV) and
|
|
19
|
+
the energy axis of `levelsplot(..., Units='cm^-1')`, which was 100× too
|
|
20
|
+
small. The old test built its expected value with the same factor; the new
|
|
21
|
+
tests compare against exact SI values (1 cm⁻¹ = 29 979.2458 MHz =
|
|
22
|
+
1.438 776 877 K = 1.239 841 984 × 10⁻⁴ eV).
|
|
23
|
+
- **`curry` docstring: wrong SI-to-CGS factor for the susceptibility.** It
|
|
24
|
+
said 1 m³/mol = 10⁶ cm³/mol; the correct relation is 1 cm³/mol (CGS) =
|
|
25
|
+
4π × 10⁻⁶ m³/mol, so SI values are converted to cm³/mol by multiplying by
|
|
26
|
+
10⁶/(4π). The values `curry` returns (SI) were always correct. The two
|
|
27
|
+
magnetometry examples used the wrong factor too, so they printed CGS values
|
|
28
|
+
4π too large (a Curie constant of 4.71 instead of 0.375 cm³·K/mol for
|
|
29
|
+
S = 1/2, g = 2); a new test checks χT = 0.375 cm³·K/mol.
|
|
30
|
+
|
|
31
|
+
## [0.6.5] — 2026-11-09
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
|
|
35
|
+
- **`FitOptions(n_workers='auto')` could start far more worker processes than
|
|
36
|
+
the machine or job could hold.** It meant one worker per core of the whole
|
|
37
|
+
machine (`os.cpu_count()`), and every worker is a separate Python process
|
|
38
|
+
with its own torch, about 0.3–0.5 GB before the model allocates anything. On
|
|
39
|
+
a 224-core node that is over 200 workers and roughly 80 GB, even inside a
|
|
40
|
+
SLURM job granted 16 cores and 8 GB. `'auto'` now takes the smallest of:
|
|
41
|
+
- the cores this process may use (CPU affinity, then `SLURM_CPUS_PER_TASK`);
|
|
42
|
+
- 32 (more workers did not make fits faster in our measurements);
|
|
43
|
+
- the workers that fit in half the available memory at 0.5 GB each, where
|
|
44
|
+
available memory honors the cgroup limit of a SLURM job or container and
|
|
45
|
+
the job's SLURM memory request;
|
|
46
|
+
- the number of tasks.
|
|
47
|
+
|
|
48
|
+
An explicit integer is used as given. In a 16-core SLURM job, `'auto'` now
|
|
49
|
+
gives 7 workers with `--mem=8G` and 2 with `--mem=3G`.
|
|
50
|
+
|
|
51
|
+
### Documentation
|
|
52
|
+
|
|
53
|
+
- README: how to launch the interactive fitting panel (`torchspin.fitgui`),
|
|
54
|
+
and what `n_workers='auto'` costs in memory.
|
|
55
|
+
|
|
9
56
|
## [0.6.4] — 2026-11-09
|
|
10
57
|
|
|
11
58
|
### Fixed
|
|
@@ -2,8 +2,8 @@ cff-version: 1.2.0
|
|
|
2
2
|
message: "If you use torchspin in your research, please cite both torchspin and the upstream EasySpin paper."
|
|
3
3
|
title: "torchspin: Differentiable PyTorch port of EasySpin for EPR spectroscopy"
|
|
4
4
|
type: software
|
|
5
|
-
version: "0.6.
|
|
6
|
-
date-released: "2026-11-
|
|
5
|
+
version: "0.6.6"
|
|
6
|
+
date-released: "2026-11-09"
|
|
7
7
|
license: MIT
|
|
8
8
|
authors:
|
|
9
9
|
- family-names: "Follmer"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: torchspin
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.6
|
|
4
4
|
Summary: Differentiable PyTorch port of EasySpin - EPR/ESR spin Hamiltonian and spectrum simulation
|
|
5
5
|
Author-email: Alec Follmer <ahfollmer@ucdavis.edu>
|
|
6
6
|
Maintainer-email: Alec Follmer <ahfollmer@ucdavis.edu>
|
|
@@ -151,8 +151,49 @@ print('Best-fit g:', result.pfit)
|
|
|
151
151
|
```
|
|
152
152
|
|
|
153
153
|
`progress='text'` prints RMSD, evaluation count and ETA as it runs, and
|
|
154
|
-
interrupting the kernel returns the best fit so far.
|
|
155
|
-
|
|
154
|
+
interrupting the kernel returns the best fit so far.
|
|
155
|
+
|
|
156
|
+
Population methods (swarm, genetic, grid, montecarlo, global) can evaluate each
|
|
157
|
+
generation in parallel worker processes with `FitOptions(n_workers='auto')`. This
|
|
158
|
+
is off by default. Each worker is a separate Python process with its own torch
|
|
159
|
+
(about 0.3–0.5 GB), so `'auto'` uses the cores you were allocated, at most 32, and
|
|
160
|
+
no more workers than fit in half the free memory. A script that uses it needs the
|
|
161
|
+
usual `if __name__ == '__main__':` guard around the code that runs the fit.
|
|
162
|
+
|
|
163
|
+
### Interactive fitting panel (`torchspin.fitgui`)
|
|
164
|
+
|
|
165
|
+
In JupyterLab (`pip install "torchspin[gui]"`), the same kind of fit runs in a panel
|
|
166
|
+
with parameter sliders, fix boxes, method controls, Start/Stop and a live plot of the
|
|
167
|
+
fit, residual and RMSD:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
%matplotlib widget
|
|
171
|
+
import numpy as np
|
|
172
|
+
from torchspin import SpinSystem, Experiment, Options, pepper
|
|
173
|
+
from torchspin.esfit import FitOptions
|
|
174
|
+
from torchspin.fitgui import FitSession, FitPanel
|
|
175
|
+
|
|
176
|
+
exp = Experiment(mwFreq=9.5, Range=[330, 350], nPoints=1024, Harmonic=1)
|
|
177
|
+
|
|
178
|
+
def model(p): # p = [gx, gy, gz, Az (MHz), lwpp (mT)]
|
|
179
|
+
sys = SpinSystem(S=[0.5], g=p[:3], Nucs='14N', A=[[18, 18, p[3]]], lwpp=[p[4], 0])
|
|
180
|
+
return pepper(sys, exp, Options(GridSize=[31, 4], Verbosity=0))[1].numpy()
|
|
181
|
+
|
|
182
|
+
B = np.linspace(330, 350, 1024)
|
|
183
|
+
data = model([2.0085, 2.0060, 2.0025, 92.0, 0.35]) # or your measured spectrum
|
|
184
|
+
data = data + 0.02 * np.abs(data).max() * np.random.default_rng(1).standard_normal(data.size)
|
|
185
|
+
|
|
186
|
+
session = FitSession(data, model, p0=[2.011, 2.0085, 2.005, 101.0, 0.5],
|
|
187
|
+
vary=[0.004, 0.004, 0.004, 15.0, 0.25],
|
|
188
|
+
names=['gx', 'gy', 'gz', 'Az', 'lwpp'],
|
|
189
|
+
options=FitOptions(method='global', max_iter=100))
|
|
190
|
+
FitPanel(session, x=B, xlabel='B (mT)').show()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Press **Start** to fit; **Stop** returns the best fit so far, and `session.result`
|
|
194
|
+
holds the result afterwards. The panel's `n_workers` box accepts `auto` for parallel
|
|
195
|
+
population fits. The full walkthrough is
|
|
196
|
+
[`examples/notebooks/09_fit_gui.ipynb`](examples/notebooks/09_fit_gui.ipynb).
|
|
156
197
|
|
|
157
198
|
### Differentiable spectrum
|
|
158
199
|
|
|
@@ -96,8 +96,49 @@ print('Best-fit g:', result.pfit)
|
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
`progress='text'` prints RMSD, evaluation count and ETA as it runs, and
|
|
99
|
-
interrupting the kernel returns the best fit so far.
|
|
100
|
-
|
|
99
|
+
interrupting the kernel returns the best fit so far.
|
|
100
|
+
|
|
101
|
+
Population methods (swarm, genetic, grid, montecarlo, global) can evaluate each
|
|
102
|
+
generation in parallel worker processes with `FitOptions(n_workers='auto')`. This
|
|
103
|
+
is off by default. Each worker is a separate Python process with its own torch
|
|
104
|
+
(about 0.3–0.5 GB), so `'auto'` uses the cores you were allocated, at most 32, and
|
|
105
|
+
no more workers than fit in half the free memory. A script that uses it needs the
|
|
106
|
+
usual `if __name__ == '__main__':` guard around the code that runs the fit.
|
|
107
|
+
|
|
108
|
+
### Interactive fitting panel (`torchspin.fitgui`)
|
|
109
|
+
|
|
110
|
+
In JupyterLab (`pip install "torchspin[gui]"`), the same kind of fit runs in a panel
|
|
111
|
+
with parameter sliders, fix boxes, method controls, Start/Stop and a live plot of the
|
|
112
|
+
fit, residual and RMSD:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
%matplotlib widget
|
|
116
|
+
import numpy as np
|
|
117
|
+
from torchspin import SpinSystem, Experiment, Options, pepper
|
|
118
|
+
from torchspin.esfit import FitOptions
|
|
119
|
+
from torchspin.fitgui import FitSession, FitPanel
|
|
120
|
+
|
|
121
|
+
exp = Experiment(mwFreq=9.5, Range=[330, 350], nPoints=1024, Harmonic=1)
|
|
122
|
+
|
|
123
|
+
def model(p): # p = [gx, gy, gz, Az (MHz), lwpp (mT)]
|
|
124
|
+
sys = SpinSystem(S=[0.5], g=p[:3], Nucs='14N', A=[[18, 18, p[3]]], lwpp=[p[4], 0])
|
|
125
|
+
return pepper(sys, exp, Options(GridSize=[31, 4], Verbosity=0))[1].numpy()
|
|
126
|
+
|
|
127
|
+
B = np.linspace(330, 350, 1024)
|
|
128
|
+
data = model([2.0085, 2.0060, 2.0025, 92.0, 0.35]) # or your measured spectrum
|
|
129
|
+
data = data + 0.02 * np.abs(data).max() * np.random.default_rng(1).standard_normal(data.size)
|
|
130
|
+
|
|
131
|
+
session = FitSession(data, model, p0=[2.011, 2.0085, 2.005, 101.0, 0.5],
|
|
132
|
+
vary=[0.004, 0.004, 0.004, 15.0, 0.25],
|
|
133
|
+
names=['gx', 'gy', 'gz', 'Az', 'lwpp'],
|
|
134
|
+
options=FitOptions(method='global', max_iter=100))
|
|
135
|
+
FitPanel(session, x=B, xlabel='B (mT)').show()
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Press **Start** to fit; **Stop** returns the best fit so far, and `session.result`
|
|
139
|
+
holds the result afterwards. The panel's `n_workers` box accepts `auto` for parallel
|
|
140
|
+
population fits. The full walkthrough is
|
|
141
|
+
[`examples/notebooks/09_fit_gui.ipynb`](examples/notebooks/09_fit_gui.ipynb).
|
|
101
142
|
|
|
102
143
|
### Differentiable spectrum
|
|
103
144
|
|
|
@@ -25,7 +25,7 @@ try:
|
|
|
25
25
|
__version__ = _pkg_version("torchspin")
|
|
26
26
|
except _PkgNotFound:
|
|
27
27
|
# Editable install during development, or version metadata unavailable.
|
|
28
|
-
__version__ = "0.6.
|
|
28
|
+
__version__ = "0.6.6+dev"
|
|
29
29
|
|
|
30
30
|
# Idle OpenMP threads must sleep, not spin-wait (see torchspin/_omp.py and
|
|
31
31
|
# docs/pitfalls.md, "Threads and performance"). This has to run before anything
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""How many worker processes this process can afford: cores and memory.
|
|
2
|
+
|
|
3
|
+
``n_workers='auto'`` used to mean one worker per core on the machine. Each worker
|
|
4
|
+
is a separate Python process with its own torch (about 0.3-0.4 GB before the model
|
|
5
|
+
allocates anything), so on a many-core workstation or cluster node 'auto' could ask
|
|
6
|
+
for tens of GB, and inside a SLURM allocation it ignored the cores actually granted.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
#: Memory planned per worker process (torch + torchspin + a small model).
|
|
14
|
+
WORKER_BYTES = 512 * 2**20
|
|
15
|
+
#: Fraction of the available memory the workers may take.
|
|
16
|
+
MEMORY_FRACTION = 0.5
|
|
17
|
+
#: Beyond this, more workers did not make fits faster in our measurements.
|
|
18
|
+
MAX_AUTO_WORKERS = 32
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def cpu_budget() -> int:
|
|
22
|
+
"""Cores this process may use: CPU affinity (Linux), then SLURM, then the machine."""
|
|
23
|
+
n = None
|
|
24
|
+
if hasattr(os, 'sched_getaffinity'):
|
|
25
|
+
try:
|
|
26
|
+
n = len(os.sched_getaffinity(0))
|
|
27
|
+
except OSError:
|
|
28
|
+
n = None
|
|
29
|
+
slurm = os.environ.get('SLURM_CPUS_PER_TASK')
|
|
30
|
+
if slurm and slurm.isdigit():
|
|
31
|
+
n = min(n, int(slurm)) if n else int(slurm)
|
|
32
|
+
return max(1, n or os.cpu_count() or 1)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _cgroup_limit() -> int | None:
|
|
36
|
+
"""Bytes left under the tightest cgroup v2 memory limit above this process.
|
|
37
|
+
|
|
38
|
+
SLURM and container runtimes set the limit on a parent of the process's own
|
|
39
|
+
cgroup (e.g. the job, not the step's task), so every ancestor is checked.
|
|
40
|
+
"""
|
|
41
|
+
try:
|
|
42
|
+
with open('/proc/self/cgroup') as f:
|
|
43
|
+
path = next((line.split('::', 1)[1].strip() for line in f if line.startswith('0::')), None)
|
|
44
|
+
except OSError:
|
|
45
|
+
return None
|
|
46
|
+
if path is None:
|
|
47
|
+
return None
|
|
48
|
+
best = None
|
|
49
|
+
parts = [p for p in path.split('/') if p]
|
|
50
|
+
for k in range(len(parts), -1, -1):
|
|
51
|
+
base = '/sys/fs/cgroup/' + '/'.join(parts[:k])
|
|
52
|
+
try:
|
|
53
|
+
with open(base + '/memory.max') as f:
|
|
54
|
+
limit = f.read().strip()
|
|
55
|
+
if limit == 'max':
|
|
56
|
+
continue
|
|
57
|
+
with open(base + '/memory.current') as f:
|
|
58
|
+
used = int(f.read().strip())
|
|
59
|
+
except (OSError, ValueError):
|
|
60
|
+
continue
|
|
61
|
+
left = max(0, int(limit) - used)
|
|
62
|
+
best = left if best is None else min(best, left)
|
|
63
|
+
return best
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _slurm_limit() -> int | None:
|
|
67
|
+
"""The job's memory request from SLURM's environment, in bytes (an upper bound)."""
|
|
68
|
+
try:
|
|
69
|
+
if os.environ.get('SLURM_MEM_PER_NODE'):
|
|
70
|
+
return int(os.environ['SLURM_MEM_PER_NODE']) * 2**20
|
|
71
|
+
if os.environ.get('SLURM_MEM_PER_CPU'):
|
|
72
|
+
cpus = int(os.environ.get('SLURM_CPUS_ON_NODE') or os.environ.get('SLURM_CPUS_PER_TASK') or 1)
|
|
73
|
+
return int(os.environ['SLURM_MEM_PER_CPU']) * cpus * 2**20
|
|
74
|
+
except ValueError:
|
|
75
|
+
pass
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def available_memory() -> int | None:
|
|
80
|
+
"""Bytes of memory new processes can use, or None if unknown."""
|
|
81
|
+
avail = None
|
|
82
|
+
if sys.platform.startswith('linux'):
|
|
83
|
+
try:
|
|
84
|
+
with open('/proc/meminfo') as f:
|
|
85
|
+
for line in f:
|
|
86
|
+
if line.startswith('MemAvailable:'):
|
|
87
|
+
avail = int(line.split()[1]) * 1024
|
|
88
|
+
break
|
|
89
|
+
except (OSError, ValueError):
|
|
90
|
+
pass
|
|
91
|
+
cg = _cgroup_limit()
|
|
92
|
+
if cg is not None:
|
|
93
|
+
avail = cg if avail is None else min(avail, cg)
|
|
94
|
+
slurm = _slurm_limit()
|
|
95
|
+
if slurm is not None:
|
|
96
|
+
avail = slurm if avail is None else min(avail, slurm)
|
|
97
|
+
if avail is None:
|
|
98
|
+
try:
|
|
99
|
+
import psutil # optional
|
|
100
|
+
avail = int(psutil.virtual_memory().available)
|
|
101
|
+
except Exception:
|
|
102
|
+
try: # macOS and other POSIX: physical memory as an upper bound
|
|
103
|
+
avail = os.sysconf('SC_PHYS_PAGES') * os.sysconf('SC_PAGE_SIZE')
|
|
104
|
+
except (ValueError, OSError, AttributeError):
|
|
105
|
+
avail = None
|
|
106
|
+
return avail
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def auto_workers(n_tasks: int) -> int:
|
|
110
|
+
"""Worker count for ``n_workers='auto'``: cores, memory and task count, at most 32."""
|
|
111
|
+
n = min(cpu_budget(), MAX_AUTO_WORKERS, max(1, int(n_tasks)))
|
|
112
|
+
mem = available_memory()
|
|
113
|
+
if mem is not None:
|
|
114
|
+
n = min(n, max(1, int(mem * MEMORY_FRACTION // WORKER_BYTES)))
|
|
115
|
+
return max(1, n)
|
|
@@ -97,11 +97,11 @@ def curry(
|
|
|
97
97
|
experiment.
|
|
98
98
|
|
|
99
99
|
Unit conventions:
|
|
100
|
-
* 1
|
|
101
|
-
* To convert to cm³/mol: multiply by 10⁶
|
|
100
|
+
* 1 cm³/mol (CGS-Gaussian, "emu/mol") = 4π × 10⁻⁶ m³/mol (SI)
|
|
101
|
+
* To convert to cm³/mol: multiply by 10⁶ / (4π) ≈ 79 577.5
|
|
102
102
|
|
|
103
103
|
Curie law check (S=1/2, g=2.0, T=300 K):
|
|
104
|
-
χ_mol ≈ 1.57 × 10⁻⁸ m³/mol = 1.
|
|
104
|
+
χ_mol ≈ 1.57 × 10⁻⁸ m³/mol = 1.25 × 10⁻³ cm³/mol (χT = 0.375 cm³·K/mol)
|
|
105
105
|
"""
|
|
106
106
|
# -----------------------------------------------------------------------
|
|
107
107
|
# Parse inputs
|
|
@@ -159,9 +159,11 @@ class FitOptions:
|
|
|
159
159
|
trf_x_scale: Union[str, float, np.ndarray] = 'jac'
|
|
160
160
|
trf_gradient_tol: float = 1e-7
|
|
161
161
|
# Parallelism for population-based methods (swarm, genetic, montecarlo,
|
|
162
|
-
# grid, global). n_workers=1 is sequential; an int > 1 or 'auto'
|
|
163
|
-
#
|
|
164
|
-
#
|
|
162
|
+
# grid, global). n_workers=1 is sequential; an int > 1 or 'auto' evaluates
|
|
163
|
+
# the population in a spawned process pool with one torch thread per worker.
|
|
164
|
+
# 'auto' = the cores this process may use (affinity / SLURM aware), at most
|
|
165
|
+
# 32, and no more than about 0.5 GB per worker within half the available
|
|
166
|
+
# memory: each worker is a separate Python process with its own torch (workers='auto'|'processes'; 'threads' forces a thread
|
|
165
167
|
# pool, which gains little because the forward model holds the GIL).
|
|
166
168
|
# Models defined in a notebook cell need cloudpickle (pip install
|
|
167
169
|
# cloudpickle) to reach the workers; otherwise a thread pool is used.
|
|
@@ -1575,10 +1577,12 @@ class _CloudPickled:
|
|
|
1575
1577
|
|
|
1576
1578
|
|
|
1577
1579
|
def _resolve_workers(n_workers, n: int) -> int:
|
|
1578
|
-
"""'auto' →
|
|
1580
|
+
"""'auto' → the cores this process may use (affinity / SLURM aware), capped by the
|
|
1581
|
+
available memory (about 0.5 GB per worker, half the free memory), by 32 and by
|
|
1582
|
+
the population size; ints pass through."""
|
|
1579
1583
|
if isinstance(n_workers, str):
|
|
1580
|
-
import
|
|
1581
|
-
return
|
|
1584
|
+
from torchspin._resources import auto_workers
|
|
1585
|
+
return auto_workers(n)
|
|
1582
1586
|
return int(n_workers or 1)
|
|
1583
1587
|
|
|
1584
1588
|
|
|
@@ -1590,7 +1594,7 @@ def _eval_population(
|
|
|
1590
1594
|
) -> np.ndarray:
|
|
1591
1595
|
"""Evaluate ``objective_fn`` on each row of ``X``.
|
|
1592
1596
|
|
|
1593
|
-
``n_workers > 1`` (or ``'auto'
|
|
1597
|
+
``n_workers > 1`` (or ``'auto'``, see :class:`FitOptions`) evaluates the rows
|
|
1594
1598
|
concurrently. ``workers='processes'`` uses a spawned process pool with one
|
|
1595
1599
|
torch thread per worker — the effective option for torchspin forward models,
|
|
1596
1600
|
whose Python-level work holds the GIL. The objective is pickled by
|
|
@@ -500,34 +500,34 @@ def unitconvert(
|
|
|
500
500
|
|
|
501
501
|
Examples
|
|
502
502
|
--------
|
|
503
|
-
>>> unitconvert(
|
|
503
|
+
>>> unitconvert(1.0, 'cm^-1->MHz') # 29979.2458 MHz
|
|
504
504
|
>>> unitconvert(340.0, 'mT->MHz', g=2.0) # ≈ 9520 MHz
|
|
505
505
|
"""
|
|
506
506
|
value = np.asarray(value, dtype=float)
|
|
507
507
|
|
|
508
508
|
# Conversion table (output in target unit)
|
|
509
509
|
conversions = {
|
|
510
|
-
'cm^-1->eV': lambda v: v *
|
|
511
|
-
'cm^-1->K': lambda v: v *
|
|
512
|
-
'cm^-1->mT': lambda v: v / g * (PLANCK / BMAGN / 1e-3) *
|
|
513
|
-
'cm^-1->MHz': lambda v: v *
|
|
510
|
+
'cm^-1->eV': lambda v: v * CLIGHT * PLANCK / EVOLT,
|
|
511
|
+
'cm^-1->K': lambda v: v * CLIGHT * PLANCK / BOLTZMANN,
|
|
512
|
+
'cm^-1->mT': lambda v: v / g * (PLANCK / BMAGN / 1e-3) * CLIGHT,
|
|
513
|
+
'cm^-1->MHz': lambda v: v * CLIGHT / 1e6,
|
|
514
514
|
|
|
515
|
-
'eV->cm^-1': lambda v: v * EVOLT / (
|
|
515
|
+
'eV->cm^-1': lambda v: v * EVOLT / (CLIGHT * PLANCK),
|
|
516
516
|
'eV->K': lambda v: v * EVOLT / BOLTZMANN,
|
|
517
517
|
'eV->mT': lambda v: v / g / BMAGN / 1e-3 * EVOLT,
|
|
518
518
|
'eV->MHz': lambda v: v * EVOLT / PLANCK / 1e6,
|
|
519
519
|
|
|
520
|
-
'K->cm^-1': lambda v: v * BOLTZMANN / (
|
|
520
|
+
'K->cm^-1': lambda v: v * BOLTZMANN / (CLIGHT * PLANCK),
|
|
521
521
|
'K->eV': lambda v: v * BOLTZMANN / EVOLT,
|
|
522
522
|
'K->mT': lambda v: v / g / BMAGN / 1e-3 * BOLTZMANN,
|
|
523
523
|
'K->MHz': lambda v: v * BOLTZMANN / PLANCK / 1e6,
|
|
524
524
|
|
|
525
|
-
'mT->cm^-1': lambda v: v * g / (PLANCK / BMAGN / 1e-3) /
|
|
525
|
+
'mT->cm^-1': lambda v: v * g / (PLANCK / BMAGN / 1e-3) / CLIGHT,
|
|
526
526
|
'mT->eV': lambda v: v * g * BMAGN * 1e-3 / EVOLT,
|
|
527
527
|
'mT->K': lambda v: v * g * BMAGN * 1e-3 / BOLTZMANN,
|
|
528
528
|
'mT->MHz': lambda v: v * g * (1e-3 * BMAGN / PLANCK / 1e6),
|
|
529
529
|
|
|
530
|
-
'MHz->cm^-1': lambda v: v * 1e6 /
|
|
530
|
+
'MHz->cm^-1': lambda v: v * 1e6 / CLIGHT,
|
|
531
531
|
'MHz->eV': lambda v: v * 1e6 * PLANCK / EVOLT,
|
|
532
532
|
'MHz->K': lambda v: v * 1e6 * PLANCK / BOLTZMANN,
|
|
533
533
|
'MHz->mT': lambda v: v / g * (PLANCK / BMAGN / 1e-3) * 1e6,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: torchspin
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.6
|
|
4
4
|
Summary: Differentiable PyTorch port of EasySpin - EPR/ESR spin Hamiltonian and spectrum simulation
|
|
5
5
|
Author-email: Alec Follmer <ahfollmer@ucdavis.edu>
|
|
6
6
|
Maintainer-email: Alec Follmer <ahfollmer@ucdavis.edu>
|
|
@@ -151,8 +151,49 @@ print('Best-fit g:', result.pfit)
|
|
|
151
151
|
```
|
|
152
152
|
|
|
153
153
|
`progress='text'` prints RMSD, evaluation count and ETA as it runs, and
|
|
154
|
-
interrupting the kernel returns the best fit so far.
|
|
155
|
-
|
|
154
|
+
interrupting the kernel returns the best fit so far.
|
|
155
|
+
|
|
156
|
+
Population methods (swarm, genetic, grid, montecarlo, global) can evaluate each
|
|
157
|
+
generation in parallel worker processes with `FitOptions(n_workers='auto')`. This
|
|
158
|
+
is off by default. Each worker is a separate Python process with its own torch
|
|
159
|
+
(about 0.3–0.5 GB), so `'auto'` uses the cores you were allocated, at most 32, and
|
|
160
|
+
no more workers than fit in half the free memory. A script that uses it needs the
|
|
161
|
+
usual `if __name__ == '__main__':` guard around the code that runs the fit.
|
|
162
|
+
|
|
163
|
+
### Interactive fitting panel (`torchspin.fitgui`)
|
|
164
|
+
|
|
165
|
+
In JupyterLab (`pip install "torchspin[gui]"`), the same kind of fit runs in a panel
|
|
166
|
+
with parameter sliders, fix boxes, method controls, Start/Stop and a live plot of the
|
|
167
|
+
fit, residual and RMSD:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
%matplotlib widget
|
|
171
|
+
import numpy as np
|
|
172
|
+
from torchspin import SpinSystem, Experiment, Options, pepper
|
|
173
|
+
from torchspin.esfit import FitOptions
|
|
174
|
+
from torchspin.fitgui import FitSession, FitPanel
|
|
175
|
+
|
|
176
|
+
exp = Experiment(mwFreq=9.5, Range=[330, 350], nPoints=1024, Harmonic=1)
|
|
177
|
+
|
|
178
|
+
def model(p): # p = [gx, gy, gz, Az (MHz), lwpp (mT)]
|
|
179
|
+
sys = SpinSystem(S=[0.5], g=p[:3], Nucs='14N', A=[[18, 18, p[3]]], lwpp=[p[4], 0])
|
|
180
|
+
return pepper(sys, exp, Options(GridSize=[31, 4], Verbosity=0))[1].numpy()
|
|
181
|
+
|
|
182
|
+
B = np.linspace(330, 350, 1024)
|
|
183
|
+
data = model([2.0085, 2.0060, 2.0025, 92.0, 0.35]) # or your measured spectrum
|
|
184
|
+
data = data + 0.02 * np.abs(data).max() * np.random.default_rng(1).standard_normal(data.size)
|
|
185
|
+
|
|
186
|
+
session = FitSession(data, model, p0=[2.011, 2.0085, 2.005, 101.0, 0.5],
|
|
187
|
+
vary=[0.004, 0.004, 0.004, 15.0, 0.25],
|
|
188
|
+
names=['gx', 'gy', 'gz', 'Az', 'lwpp'],
|
|
189
|
+
options=FitOptions(method='global', max_iter=100))
|
|
190
|
+
FitPanel(session, x=B, xlabel='B (mT)').show()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Press **Start** to fit; **Stop** returns the best fit so far, and `session.result`
|
|
194
|
+
holds the result afterwards. The panel's `n_workers` box accepts `auto` for parallel
|
|
195
|
+
population fits. The full walkthrough is
|
|
196
|
+
[`examples/notebooks/09_fit_gui.ipynb`](examples/notebooks/09_fit_gui.ipynb).
|
|
156
197
|
|
|
157
198
|
### Differentiable spectrum
|
|
158
199
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|