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.0.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.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torch_2dtm-0.5.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c56b716511105299e622af3b6225d9b13f8b6cf4e621d48cff5b7fe217504864
MD5 5157103e67c2c80764a3c76372c0f03d
BLAKE2b-256 f91a81998d3470adf022f29076b5a44a1444737fc1c1a3b21edd37e25547e047

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: torch_2dtm-0.5.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11f3d8f4fe9a31aa6c6b1247d0e5057056d7042c5c62adaa75c9466537208042
MD5 21c830c4a629e6db71b75e539bf4310c
BLAKE2b-256 2dea590e1e4f0457123701da4a1f1fa4a332069c80fd1f8d71dc01c98a4ccc45

See more details on using hashes here.

Provenance

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