torch-fit-in-map
Overview
torch-fit-in-map is a PyTorch package for rigid-body volume alignment in
cryo-EM. It finds the rotation and translation that best superimposes a mobile
volume onto a reference volume using normalised cross-correlation (NCC).
The public API operates purely on torch.Tensor potential maps and
pandas.DataFrame atom tables — it does no file I/O. Reading/writing MRC and
PDB/mmCIF files and the command-line tools live in the companion package
torch-fit-in-map-cli.
Two alignment modes are supported:
- Map-to-map — align one map onto another (
fit_map_in_map). - Atoms <-> map — simulate an electrostatic potential from a table of
atoms and align it against a map (
fit_structure_in_map/fit_map_in_structure).
Features
- Exhaustive SO(3) grid search with per-rotation FFT-based optimal translation
- Symmetry-aware search (
C1,C4,D2,T,O,I, …) to restrict to the asymmetric unit - Gradient-based local refinement (L-BFGS or Adam) using PyTorch autograd
- Multi-start refinement to escape local minima
- Optional soft masking and multi-GPU support
- Atom-table transform: map input atomic coordinates into the reference frame
(
apply_alignment_to_structure)
Installation
pip install torch-fit-in-map
The atoms <-> map modes use the electrostatic-potential simulator
torch-calculate-electrostatic-potential (installed automatically as a
dependency).
Basic Usage
Map-to-map alignment
import torch
from torch_fit_in_map import fit_map_in_map, apply_alignment
# (d, h, w) float tensors at the same pixel size
reference = torch.load("reference.pt")
mobile = torch.load("mobile.pt")
result = fit_map_in_map(mobile, reference, pixel_size_angstroms=1.5)
print(f"NCC score: {result.score:.4f}")
print(f"Rotation (zyx):\n{result.rotation_matrix}")
print(f"Translation (px): {result.translation_pixels}")
aligned = apply_alignment(mobile, result)
If the two maps have different voxel sizes, resample first with
normalise_voxel_sizes(reference, mobile, ref_px, mob_px).
Atoms <-> map alignment
Atoms are passed as a DataFrame with columns x, y, z (Angstroms) and element —
exactly what mmdf produces:
import mmdf
from torch_fit_in_map import fit_structure_in_map
atoms = mmdf.read("model.pdb") # pandas DataFrame
result = fit_structure_in_map(
mobile_atoms=atoms,
reference_map=experimental, # (d, h, w) tensor
pixel_size_angstroms=1.5,
box_size=128,
)
fit_map_in_structure does the inverse (fit a map into the frame of an atomic
structure).
Both accept a custom simulator= implementing the PotentialSimulator protocol
and optional simulator_config= for the default electrostatic-potential backend.
Simulation contract
The default simulator (DEFAULT_POTENTIAL_SIMULATOR) delegates to
torch-calculate-electrostatic-potential:
- Atoms are centred at the cubic simulation-box centre
(
default_sublattice_radius(pixel_size)sets the per-atom stencil). - A
(box_size, box_size, box_size)potential in volts is returned (ZYX order). apply_alignment_to_structureinverts the same centre/crop geometry before applying the alignment transform.
Use PotentialSimulatorConfig to select Peng elemental vs bonded scattering
factors. Bonded factors require structure columns
chain, residue_id, residue, and atom (or set annotate_bonding=True).
The CLI wrappers torch-fit-in-map, torch-fit-in-atomic-model, and
torch-simulate-density in
torch-fit-in-map-cli
call the same default simulator path.
Transforming atoms into the reference frame
from torch_fit_in_map import apply_alignment_to_structure
moved = apply_alignment_to_structure(
atoms, result,
pixel_size=1.5,
box_shape=reference.shape, # (d, h, w)
) # returns a DataFrame with transformed x/y/z
Tuning the search
from torch_fit_in_map import (
fit_map_in_map,
ExhaustiveSearchConfig,
GradientRefinementConfig,
)
result = fit_map_in_map(
mobile,
reference,
exhaustive_config=ExhaustiveSearchConfig(
angular_step_degrees=7.5, # finer orientation sampling
symmetry="C4", # restrict to C4 asymmetric unit
n_start=5, # refine top-5 poses independently
devices=["cuda:0", "cuda:1"],
),
gradient_config=GradientRefinementConfig(optimizer="lbfgs", n_iterations=200),
pixel_size_angstroms=1.5,
)
Pass gradient_config=None to return the exhaustive-search result without refinement.
AlignmentResult
All alignment functions return an AlignmentResult:
| Field | Type | Description |
|---|---|---|
rotation_matrix |
(3, 3) tensor |
Rotation in zyx convention |
translation_pixels |
(3,) tensor |
Translation in zyx pixels |
score |
float |
Peak NCC score (higher is better, max 1.0) |
translation_angstroms |
(3,) tensor or None |
Translation in Angstroms (when pixel size is provided) |
simulated_potential |
(d, h, w) tensor or None |
Simulated potential when save_simulated=True |
Use apply_alignment(mobile, result) to produce the aligned volume.
Command-line tools
The torch-fit-in-map, torch-fit-in-atomic-model and torch-simulate-density
commands (with MRC/PDB file handling) live in
torch-fit-in-map-cli.
They use the same DEFAULT_POTENTIAL_SIMULATOR path as the Python API.
License
BSD 3-Clause — see the LICENSE file for details.
Metadata
Release files for torch-fit-in-map 0.6.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 |
|---|---|---|---|---|
| torch_fit_in_map-0.6.0.tar.gz | 28.5 kB | With provenance | Details for torch_fit_in_map-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_fit_in_map-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for torch_fit_in_map-0.6.0-py3-none-any.whl |
Total release size: 54.5 kB
Release files / torch_fit_in_map-0.6.0.tar.gz
| Download URL | torch_fit_in_map-0.6.0.tar.gz |
|---|---|
| Size | 28.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2edf8cc2d1e5bc680d06627515c6e82d251c710f39e7ab9d5d84c41c2daa1637
|
|
BLAKE2b-256 checksum How to use checksums |
84ef12fce6d009e0ea8ab9e9d6ba6bb8f05b1c703eadc9f741975a846f272b31
|
| 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 Sep 11, 2026.
Transparency logRelease files / torch_fit_in_map-0.6.0-py3-none-any.whl
| Download URL | torch_fit_in_map-0.6.0-py3-none-any.whl |
|---|---|
| Size | 26.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1aa72a9dcc558749533c6fe5b024a6f1c2e42f74a15bb7ac52a2213d320f0f35
|
|
BLAKE2b-256 checksum How to use checksums |
000b1c1c30228de3b3cb16ab78f78e77f553c0971f8ec049fcceebd2e2a078a9
|
| 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 Sep 11, 2026.
Transparency logSecurity for torch-fit-in-map 0.6.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 torch_fit_in_map-0.6.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 Sep 11, 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.