Cross correlation with cosine stretching for cryo-EM data in PyTorch
Project description
torch-tiltxcorr
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
shifts = tiltxcorr(
tilt_series=tilt_series,
tilt_angles=tilt_angles,
tilt_axis_angle=tilt_axis_angle,
lowpass_angstroms=.5,
)
# 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:
- Sorting images by tilt angle
- Dividing the series into groups of positive and negative tilt angles
- 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
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_tiltxcorr-0.1.3.tar.gz.
File metadata
- Download URL: torch_tiltxcorr-0.1.3.tar.gz
- Upload date:
- Size: 195.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8101f411bd4db2c7dbc35fecf017a54ba8106a24a9956e29887e1e5c53c013c
|
|
| MD5 |
fae7dde8023c8fe51b399d40d550a65e
|
|
| BLAKE2b-256 |
e27acfa3293dae08b33651b58319891628fce84d827271670c29ed77d59f8178
|
Provenance
The following attestation bundles were made for torch_tiltxcorr-0.1.3.tar.gz:
Publisher:
ci.yml on teamtomo/torch-tiltxcorr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_tiltxcorr-0.1.3.tar.gz -
Subject digest:
e8101f411bd4db2c7dbc35fecf017a54ba8106a24a9956e29887e1e5c53c013c - Sigstore transparency entry: 769406569
- Sigstore integration time:
-
Permalink:
teamtomo/torch-tiltxcorr@2d3b3ce2b1a4e888c76c70bc76c9272aea3f0c90 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2d3b3ce2b1a4e888c76c70bc76c9272aea3f0c90 -
Trigger Event:
push
-
Statement type:
File details
Details for the file torch_tiltxcorr-0.1.3-py3-none-any.whl.
File metadata
- Download URL: torch_tiltxcorr-0.1.3-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93e179136d19fc002594e8077161239bc77996efdfe351594da7af3aa3f3e685
|
|
| MD5 |
148cd8ebeadbdfded1b8f768f536edf0
|
|
| BLAKE2b-256 |
1af5beda7e5fc1d3f650154134c4d705c9ef7acb047e62ec4763ae5c77b5622b
|
Provenance
The following attestation bundles were made for torch_tiltxcorr-0.1.3-py3-none-any.whl:
Publisher:
ci.yml on teamtomo/torch-tiltxcorr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_tiltxcorr-0.1.3-py3-none-any.whl -
Subject digest:
93e179136d19fc002594e8077161239bc77996efdfe351594da7af3aa3f3e685 - Sigstore transparency entry: 769406595
- Sigstore integration time:
-
Permalink:
teamtomo/torch-tiltxcorr@2d3b3ce2b1a4e888c76c70bc76c9272aea3f0c90 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2d3b3ce2b1a4e888c76c70bc76c9272aea3f0c90 -
Trigger Event:
push
-
Statement type: