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.6.3rc3.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.6.3rc3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file torch_2dtm-0.6.3rc3.tar.gz.

File metadata

  • Download URL: torch_2dtm-0.6.3rc3.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.6.3rc3.tar.gz
Algorithm Hash digest
SHA256 2ccf86b688d7fcf4046db8294b9be2e6ac53a3eb517528cf80c72970dafd2ed8
MD5 25578616faca8b7cd64cb8df2411d5f1
BLAKE2b-256 0f4d0df44bc412af76fdade905da4c3603fb39a7b3b541657e62a6f2a7a4e1ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_2dtm-0.6.3rc3.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.6.3rc3-py3-none-any.whl.

File metadata

  • Download URL: torch_2dtm-0.6.3rc3-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.6.3rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 7d09f2d4151e61c7741a720d193568d38a6c97f1573eb4ac53d85c94134a73c8
MD5 38d52c2d7dd423ab791421c33cb77efc
BLAKE2b-256 c1f56a4fffd8c3d684b00fbdfe2860609d3caac07ed47c8306fa3a55e0c13a86

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_2dtm-0.6.3rc3-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