Skip to main content

Cross correlation with cosine stretching for cryo-EM data in PyTorch

Project description

torch-tiltxcorr

License PyPI Python Version CI codecov

Cross correlation with image stretching for coarse alignment of cryo-EM tilt series data in PyTorch.

Overview

torch-tiltxcorr reimplements the IMOD program tiltxcorr in PyTorch.

Installation

pip install torch-tiltxcorr

Usage

import torch
from torch_fourier_shift import fourier_shift_image_2d
from torch_tiltxcorr import tiltxcorr

# Load or create your tilt series
# tilt_series shape: (batch, height, width) - batch is number of tilt images
# Example: tilt_series with shape (61, 512, 512) - 61 tilt images of 512x512 pixels
tilt_series = torch.randn(61, 512, 512)

# Define tilt angles (in degrees)
# Shape: (batch,) - one angle per tilt image
tilt_angles = torch.linspace(-60, 60, steps=61)

# Define tilt axis angle (in degrees)
tilt_axis_angle = 45

# Run tiltxcorr
# (for noisy cryo-EM data, apply a lowpass to ~4x pixel size as a starting point)
shifts = tiltxcorr(
   tilt_series=tilt_series,
   tilt_angles=tilt_angles,
   tilt_axis_angle=tilt_axis_angle,
   pixel_spacing_angstroms=10,
   lowpass_angstroms=40,
)
# shifts shape: (batch, 2) - (dy, dx) shifts which center each tilt image

# Apply shifts to align the tilt series
aligned_tilt_series = fourier_shift_image_2d(tilt_series, shifts=shifts)
# aligned_tilt_series shape: (batch, height, width)

Use uv to run an example with simulated data and visualize the results.

uv run examples/tiltxcorr_example_simulated_data.py

How It Works

torch-tiltxcorr performs coarse tilt series alignment by:

  1. Sorting images by tilt angle
  2. Dividing the series into groups of positive and negative tilt angles
  3. For each adjacent pair of images in each group:
    • Applying a stretch perpendicular to the tilt axis on the image with the larger tilt angle
    • Calculating cross-correlation between the images
    • Extracting the shift from the position of the correlation peak
    • Transforming the shift to account for the stretch applied to the image
  4. Accumulating shifts to align the entire series

With Sample Tilt Estimation

If a sample is physically rotated +5° around the microscope stage tilt axis, then at nominal 0° stage tilt the beam sees the sample at +5°.

This offset affects the correlations measured by tiltxcorr. By finding the offset value that maximizes total correlation, we can estimate the true physical pre-tilt of the sample around the microscope tilt axis.

import torch
from torch_fourier_shift import fourier_shift_image_2d
from torch_tiltxcorr import tiltxcorr_with_sample_tilt_estimation

# Load or create your tilt series
tilt_series = torch.randn(61, 512, 512)
tilt_angles = torch.linspace(-60, 60, steps=61)
tilt_axis_angle = 45

# Run tiltxcorr with sample tilt estimation
# (for noisy cryo-EM data, apply a lowpass to ~4x pixel size as a starting point)
shifts, sample_tilt = tiltxcorr_with_sample_tilt_estimation(
   tilt_series=tilt_series,
   tilt_angles=tilt_angles,
   tilt_axis_angle=tilt_axis_angle,
   pixel_spacing_angstroms=10,
   lowpass_angstroms=40,
   sample_tilt_range=(-30.0, 30.0),  # search range in degrees
)
# shifts shape: (batch, 2) tensor of (dy, dx) shifts which center each tilt image
# sample_tilt: float value for component of sample tilt about the stage in degrees

# Apply shifts to align the tilt series
aligned_tilt_series = fourier_shift_image_2d(tilt_series, shifts=shifts)

License

This package is distributed under the BSD 3-Clause License.

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_tiltxcorr-0.5.2rc1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

torch_tiltxcorr-0.5.2rc1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file torch_tiltxcorr-0.5.2rc1.tar.gz.

File metadata

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

File hashes

Hashes for torch_tiltxcorr-0.5.2rc1.tar.gz
Algorithm Hash digest
SHA256 2da4ff8403a317e8e92f7f296277aed2dec88b5019129658e017e291da7f4064
MD5 0f0bf8f3d3255a2121cc6190982f5072
BLAKE2b-256 417ad6a75ce99aa10cdaf54ef4322983f018c66396c4f90bd818c1bca298a081

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_tiltxcorr-0.5.2rc1.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_tiltxcorr-0.5.2rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_tiltxcorr-0.5.2rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 1713fa5a006a40832b12f419b5646c7bfc061e771ef7004b2e313bd69865cb38
MD5 0b2a79c182dd9b780f34fc505d55f7ff
BLAKE2b-256 6c33fc8b92ef4e128f7814a23d78cd7b7eb9d4aba56d959f2cf4ef51df961e24

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_tiltxcorr-0.5.2rc1-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