torch-2dtm
Overview
torch-2dtm is a Python package for efficient templating matching of 2D projections of a 3D template with a 2D image in PyTorch.
This is implemented for cryo-EM applications, see Rickgauer et al. 2017 eLife for details.
Features
- Fast 2D template matching using Fourier transforms
- Batch processing over orientations
- Batch processing over Fourier space filters (e.g. for defocus sweeps)
- GPU acceleration through PyTorch
Projections are calculated on-the-fly using torch-fourier-slice.
Installation
pip install torch-2dtm
Basic Usage
import torch
import torch_2dtm
from scipy.stats import special_ortho_group
# Create random test data
# 1. Create a random image and compute its FFT
image_size = (128, 128)
image = torch.randn(*image_size, dtype=torch.float32)
image_dft = torch.fft.rfftn(image, dim=(0, 1)) # Shape: (128, 65)
# 2. Create a random 3D template and compute its FFT
template_size = (64, 64, 64)
template = torch.randn(*template_size, dtype=torch.float32)
template_dft = torch.fft.rfftn(template, dim=(0, 1, 2)) # Shape: (64, 64, 33)
# 3. Create a batch of random rotation matrices with shape (b, 3, 3)
num_orientations = 10
rotation_matrices = torch.tensor(special_ortho_group.rvs(size=num_orientations, dim=3), dtype=torch.float32)
# 4. Create an arbitrary stack of Fourier space filters (identity filter in this example)
# These filters operate on rffts of the 2D projection images
# Filter shape: (..., h, w // 2 + 1)
filters = torch.ones(template_size[0], template_size[1] // 2 + 1, dtype=torch.complex64)
# Perform template matching
cross_correlation = torch_2dtm.match_template_dft_2d(
image_dft=image_dft,
template_dft=template_dft,
rotation_matrices=rotation_matrices,
filters=filters
)
# The result has shape (..., num_orientations, image_height, image_width)
License
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
Metadata
Release files for torch-2dtm 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 | |
|---|---|---|---|
| torch_2dtm-0.6.0.tar.gz | 7.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torch_2dtm-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.5 kB
Release files / torch_2dtm-0.6.0.tar.gz
| Download URL | torch_2dtm-0.6.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed57a9acb4c82330ddba781998e0f50f0ea71782df2023474367ad96832329ed
|
|
BLAKE2b-256 checksum How to use checksums |
0cd3e3763c532d6831b14b22fd7ee62b6fe79365663e14e835decd84a5d84fb6
|
| 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_2dtm-0.6.0-py3-none-any.whl
| Download URL | torch_2dtm-0.6.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce203b83c0bfeb8b49fc76462f52392afed239a0366f31d1211489255dd1709f
|
|
BLAKE2b-256 checksum How to use checksums |
f7761ba0be4f2cf8b65b5b7f1c7f632e0a8bef7b38d01f1c7df9f4afaf35d45c
|
| 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 log