Skip to main content

TorchRef

A PyTorch-based crystallographic refinement library

Tests Python 3.10+ PyTorch License: MIT Documentation CUDA Apple Silicon MPS

TorchRef is a crystallographic refinement package built entirely on PyTorch. By leveraging PyTorch's automatic differentiation and GPU acceleration, TorchRef enables seamless integration with machine learning workflows and provides a flexible, extensible framework for crystallographic structure refinement.

**Scope TorchRef is a mainly a library/framework to build and experiment with. It is not intended to replace mainline refinement programs for standard problems.

Benchmark

TorchRef AlphaFold-start refinement benchmark

Refinement of Phaser-placed AlphaFold models against experimental data, benchmarked on a conserved set of ~720 PDB structures (1.4–3.0 Å) with all engines starting from the same placed models and scored by a single common validator.

  • (A) R-factors (PHENIX-validated). Starting from the AlphaFold prediction (green), TorchRef (red) drives R-work/R-free down to essentially the same cluster as REFMAC (purple) and PHENIX (blue). Median R-free is 0.3167 (TorchRef) vs 0.3166 (PHENIX) and 0.3161 (REFMAC5)
  • (B) Geometry (RMSZ vs REFMAC restraints). Bond, angle, chiral and main-chain B-factor RMS Z-scores. TorchRef produces valid, physically reasonable geometry; its restraints run slightly looser than PHENIX/REFMAC (bond RMSZ ≈ 1.3)
  • (C) Wall-clock runtime. Median runtime per structure (4 CPU cores). TorchRef (1.65 min) sits between REFMAC (0.53 min) and PHENIX (4.63 min) — ~2.8× faster than PHENIX, ~3× slower than REFMAC.
  • (D) Convergence speed (normalized). Fraction of the total R-free improvement achieved per macrocycle. The different programs show differing convergence behavior.

Key Features

  • Native PyTorch Integration: Built on PyTorch's nn.Module architecture, TorchRef integrates naturally with the PyTorch ecosystem, including machine learning models, optimizers, and GPU acceleration.

  • Automatic Differentiation: Dynamic computational graphs eliminate the need for manually implemented gradient calculations. Define new refinement targets directly—PyTorch handles the derivatives automatically.

  • Modular Architecture: Following PyTorch's module pattern, components are easily composable and extensible. Add custom targets, restraints, or optimizers without modifying core code.

  • GPU Acceleration: Leverage CUDA for structure factor calculations, scaling, and optimization—achieving significant speedups for large structures. Apple Silicon GPUs are also supported 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: Efficient structure factor calculation using Fast Fourier Transform (FFT) methods, enabling rapid F_calc computation even for large unit cells.

Getting Started

Notebook Description
Open In Colab Quickstart — MTZ + PDB to refined structure, refined MTZ and CCP4 map; selection- and parameter-type-based refinement
Open In Colab Structure factors — one-liner, FFT class, and manual voxel pipeline; standalone scaling; autograd
Open In Colab Targets and weighting — standard targets, target-offset weighting, X-ray mode comparison, custom targets, driving an optimizer from a LossState

Installation

pip install torchref

Local installation for development

clone the repository

git clone https://github.com/HatPdotS/TorchRef.git cd torchref

Install with pip

pip install -e .

Or install with development dependencies

pip install -e ".[dev]"

Dependencies

  • Python ≥ 3.10
  • PyTorch ≥ 2.4
  • NumPy ≥ 2.0
  • Gemmi ≥ 0.5
  • reciprocalspaceship ≥ 0.9.18
  • SciPy ≥ 1.10

Testing

# Run all tests
pytest tests/

# Run with coverage
pytest tests/ --cov=torchref

# Run specific test categories
pytest tests/unit/           # Fast unit tests
pytest tests/integration/    # Integration tests
pytest tests/functional/     # Full workflow tests

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Follow the NumPy docstring style
  2. Add tests for new functionality
  3. Ensure all tests pass before submitting

License

This project is licensed under the MIT License - see the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchref-0.6.2.tar.gz (3.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchref-0.6.2-py3-none-any.whl (3.2 MB view details)

Uploaded Python 3

File details

Details for the file torchref-0.6.2.tar.gz.

File metadata

  • Download URL: torchref-0.6.2.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

Hashes for torchref-0.6.2.tar.gz
Algorithm Hash digest
SHA256 a76a96934bdd3de2933e4a64fb6c4f7d79afde35745fedc53afd57123d7b95d0
MD5 28a8d12cadbf23fa0f0fb20032fb1024
BLAKE2b-256 daa342283c563a9d752a76a2e7b79d1fd2dabee510bb24808aaf82900e5982d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchref-0.6.2.tar.gz:

Publisher: publish.yml on HatPdotS/TorchRef

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchref-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: torchref-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 3.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for torchref-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f163a1873316b9541257d8b3996842ed6d9f1532318e036299713d378c2c957
MD5 afc3c140b19b0700fa82cb1ec5f75581
BLAKE2b-256 7c9747f0472df62b80311080cbed8dc99cf1d65bfb57933a6e58ab2b8db1feda

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchref-0.6.2-py3-none-any.whl:

Publisher: publish.yml on HatPdotS/TorchRef

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page