A PyTorch-based crystallographic Refinement library
TorchRef is a crystallographic refinement package built entirely on PyTorch. Autograd and GPU acceleration make it composable with machine-learning workflows and cheap to extend with new targets.
Scope. TorchRef is mainly a library/framework to build and experiment with. It is not intended to replace mainline refinement programs for standard problems.
Benchmark
Refinement of Phaser-placed AlphaFold models against experimental data, on a conserved set of 723 PDB structures (1.40–3.00 Å). All engines start from the same placed models and are scored by one common validator (PHENIX).
(A) R-work and R-free per engine. Median R-free 0.3197 (TorchRef), 0.3165 (PHENIX), 0.3136 (REFMAC5); paired median difference, TorchRef minus reference, +0.0006 against PHENIX and +0.0047 against REFMAC. (B) Geometry RMS Z against REFMAC restraints, ideal 1.0: bond 0.58 (REFMAC 0.58, PHENIX 0.83), main-chain B 1.56 (REFMAC 0.90, PHENIX 1.03). (C) Wall-clock runtime per structure on 4 CPU cores: median 0.77 min (REFMAC 0.25, PHENIX 3.09). (D) Fraction of the total R-free improvement reached per macrocycle.
Key Features
-
Native PyTorch Integration: Built on PyTorch's
nn.Modulearchitecture, so TorchRef composes with PyTorch models, optimizers, and devices. -
Automatic Differentiation: No hand-written gradients. Define a new refinement target's forward pass and PyTorch supplies the derivatives.
-
Modular Architecture: Custom targets, restraints, and optimizers plug in without modifying core code.
-
GPU Acceleration: CUDA for structure factors, scaling, and optimization. Apple Silicon works through PyTorch's MPS backend — unsupported ops fall back to CPU automatically via
PYTORCH_ENABLE_MPS_FALLBACK=1, which TorchRef sets on import. -
FFT-based Structure Factors: F_calc via FFT, so large unit cells stay tractable.
Getting Started
Installation
pip install torchref
For development:
git clone --filter=blob:none --sparse https://github.com/HatPdotS/TorchRef.git
cd TorchRef
git sparse-checkout set torchref tests
pip install -e ".[dev]"
This fetches ~40 MB instead of ~436 MB; most of the repository is paper/ history. Files outside
the checkout are fetched on demand, so add paths later with git sparse-checkout add paper, or
git sparse-checkout disable for all of it. Requires Git ≥ 2.27.
Dependencies
Python ≥ 3.10, PyTorch ≥ 2.4, NumPy ≥ 2.0, Pandas ≥ 2.0, SciPy ≥ 1.10, Gemmi ≥ 0.5, reciprocalspaceship ≥ 0.9.18, Numba ≥ 0.59, Matplotlib ≥ 3.7. pyproject.toml carries the authoritative pinned ranges; upper bounds are set one minor version above the tested maximum, so a newer dependency will refuse to install rather than fail at runtime.
Testing
pytest tests/ # all tests
pytest tests/ --cov=torchref # with coverage
pytest tests/unit/ # fast unit tests only
Slow tests need --run-slow. Accelerator tests are not opt-in: they run wherever CUDA or MPS is available and are skipped when it is not.
Contributing
Contributions are welcome. Please use NumPy docstring style, add tests for new functionality, and make sure the suite passes before submitting.
License
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchref-0.6.3.tar.gz.
File metadata
- Download URL: torchref-0.6.3.tar.gz
- Upload date:
- Size: 3.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d999a25bcb6b587a64f0de3067175fe9479472110f1daef0d96f8562b8665040
|
|
| MD5 |
69606b3437780c6d1b20b7d7d00b3796
|
|
| BLAKE2b-256 |
75f56df4bdb782c1defe66dfb88dfa162916d1ca97fcbab2b473639d90637312
|
Provenance
The following attestation bundles were made for torchref-0.6.3.tar.gz:
Publisher:
publish.yml on HatPdotS/TorchRef
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchref-0.6.3.tar.gz -
Subject digest:
d999a25bcb6b587a64f0de3067175fe9479472110f1daef0d96f8562b8665040 - Sigstore transparency entry: 2451987786
- Sigstore integration time:
-
Permalink:
HatPdotS/TorchRef@00531496efb596bf77676e499664cb1955e2eb7b -
Branch / Tag:
refs/tags/0.6.3 - Owner: https://github.com/HatPdotS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00531496efb596bf77676e499664cb1955e2eb7b -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchref-0.6.3-py3-none-any.whl.
File metadata
- Download URL: torchref-0.6.3-py3-none-any.whl
- Upload date:
- Size: 3.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6d73e43b16de3b00c8a09d2f5068bb25570295258a96448aa17199bc41d0de6
|
|
| MD5 |
14bfb12985659832d108a9b58128123b
|
|
| BLAKE2b-256 |
5273bf114221d537ff84a6a2779e532db9b60f0a9b437926c59358fcd71f7110
|
Provenance
The following attestation bundles were made for torchref-0.6.3-py3-none-any.whl:
Publisher:
publish.yml on HatPdotS/TorchRef
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchref-0.6.3-py3-none-any.whl -
Subject digest:
f6d73e43b16de3b00c8a09d2f5068bb25570295258a96448aa17199bc41d0de6 - Sigstore transparency entry: 2451988465
- Sigstore integration time:
-
Permalink:
HatPdotS/TorchRef@00531496efb596bf77676e499664cb1955e2eb7b -
Branch / Tag:
refs/tags/0.6.3 - Owner: https://github.com/HatPdotS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00531496efb596bf77676e499664cb1955e2eb7b -
Trigger Event:
release
-
Statement type: