Skip to main content

2D template matching in pytorch

Project description

torch-2dtm

License PyPI Python Version CI codecov

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.

Project details


Download files

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

Source Distribution

torch_2dtm-0.5.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

torch_2dtm-0.5.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file torch_2dtm-0.5.2.tar.gz.

File metadata

  • Download URL: torch_2dtm-0.5.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torch_2dtm-0.5.2.tar.gz
Algorithm Hash digest
SHA256 6c512d89c72ce1dd6a6ed38a8b655ec69684ed29e3882069ba07855915e6ac2b
MD5 0cee5c906acad3df8e9af5c0259b42cb
BLAKE2b-256 d9d0d381455fa7b90c4e733e66c27ab1a5c15e9fdf33a28f1c0a9de587153045

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_2dtm-0.5.2.tar.gz:

Publisher: deploy.yml on teamtomo/teamtomo

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

File details

Details for the file torch_2dtm-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: torch_2dtm-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torch_2dtm-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 709ad7dd94f4a92ffda8ba478e4dac8631d75e6cb8225d1e283f060b905f76ef
MD5 85cbd7524f6e2a68ec28ed1ccdf86ce3
BLAKE2b-256 2811c0bb1d35bd3e4ce088305c5f62fe58d6e3d5eddbb99c48b4c2b98d8a1f4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_2dtm-0.5.2-py3-none-any.whl:

Publisher: deploy.yml on teamtomo/teamtomo

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 Pingdom Monitoring Sentry Error logging StatusPage Status page