Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

NPCloud Python

中文 | English

Python implementation of NP-Cloud (Nearest Paired Cloud) for fast and robust drift correction in single-molecule localization microscopy (SMLM).

Overview

This package is a complete Python port of the original MATLAB implementation from:

"Fast and robust drift correction for single-molecule localization microscopy"

The Python implementation produces numerically identical results to the MATLAB version, verified through comprehensive comparison tests.

Features

  • 2D Drift Correction: NP-Cloud and RR-NP-Cloud algorithms
  • 3D Drift Correction: NP-Cloud-3D and RR-NP-Cloud-3D algorithms
  • MATLAB Comparison Tests: Verify Python output matches MATLAB exactly
  • Visualization Tools: Compare drift curves and rendered SMLM images

Comparison Results

The Python implementation has been validated against the original MATLAB code:

Metric2D (High Density)3D (High Density)
Max X Drift Diff~1e-2 pixels~1e-2 pixels
Max Y Drift Diff~1e-2 pixels~1e-2 pixels
Max Z Drift Diff-~1e-1 nm

2D Drift Correction Comparison

Drift Curves Comparison

2D Drift Curves2D drift curves: Python vs MATLAB

SMLM Images Comparison

2D Images2D drift correction comparison: Original vs Python vs MATLAB corrected images

High Density Data Comparison

2D High Density2D high density data drift curves comparison

Python vs MATLAB Difference

2D Difference2D corrected images difference (Python - MATLAB)

3D Drift Correction Comparison

Drift Curves Comparison

3D Drift Curves3D drift curves: Python vs MATLAB (X, Y, Z dimensions)

SMLM Images Comparison (XY Projection)

3D Images3D drift correction comparison: Original vs Python vs MATLAB corrected images (XY projection)

High Density Data Comparison

3D High Density3D high density data drift curves comparison

Installation

cd npcloud_python
pip install -r requirements.txt

Requirements

  • Python 3.6+
  • NumPy >= 1.19.0
  • SciPy >= 1.5.0
  • Matplotlib >= 3.3.0
  • pytest >= 6.0.0 (for testing)

Usage

2D Drift Correction

fromnpcloud.npc_rrnpc_2dimportnpc_rrnpc_call_functionfromnpcloud.utilsimportload_smlm_data# Load data (columns: Frame, X, Y)smlm_data=load_smlm_data('your_data.mat')
array_frames=smlm_data[:, 0].astype('int32')
array_x=smlm_data[:, 1].astype('float32')
array_y=smlm_data[:, 2].astype('float32')
# Run drift correctionxc, yc, xd1, yd1, xd2, yd2, npc_time, total_time=npc_rrnpc_call_function(
array_x, array_y, array_frames,
dc_segment_size=70, # Frames per segmentmax_search_radius_pix1=0.3, # Pass 1 search radius (pixels)max_search_radius_pix2=0.225, # Pass 2 search radius (pixels)resample_fold=12# Resampling factor
)
# xc, yc: corrected positions# xd2, yd2: final drift curves (RR-NPC)

3D Drift Correction

fromnpcloud.npc_rrnpc_3dimportnpc_rrnpc_3d_call_function# Load 3D data (columns: Frame, X, Y, Z)# X, Y in pixels; Z in nmresult=npc_rrnpc_3d_call_function(
array_x, array_y, array_z, array_frames,
dc_segment_size=100, # Frames per segmentmax_search_radius_pix1=0.35, # Pass 1 XY search radiusmax_search_radius_pix2=0.28, # Pass 2 XY search radiustol_z_nm=110, # Pass 1 Z tolerance (nm)tol_z_nm2=90, # Pass 2 Z tolerance (nm)resample_fold=12
)
xc, yc, zc, xd1, yd1, zd1, xd2, yd2, zd2, npc_time, total_time=result

Command Line Scripts

# 2D drift correction
python scripts/run_npc_rrnpc.py --data your_data.mat --name output_folder
# 3D drift correction 
python scripts/run_npc_rrnpc_3d.py --data your_3d_data.mat --name output_folder
# Compare Python vs MATLAB (requires MATLAB engine)
python scripts/compare_matlab_python.py

Parameters

Parameter2D Default3D DefaultDescription
dc_segment_size70100Frames per segment
max_search_radius_pix10.30.35XY search radius Pass 1 (pixels)
max_search_radius_pix20.2250.28XY search radius Pass 2 (pixels)
tol_z_nm-110Z tolerance Pass 1 (nm)
tol_z_nm2-90Z tolerance Pass 2 (nm)
resample_fold1212Resampling factor for RR-NPC

Data Format

  • 2D: 3 columns [Frame, X, Y] - X,Y in pixels
  • 3D: 4 columns [Frame, X, Y, Z] - X,Y in pixels, Z in nm
  • Frame numbers must be increasing (sorted by frame)

Project Structure

npcloud_python/
├── npcloud/
│ ├── core/
│ │ ├── np_cloud_2d.py # 2D NP-Cloud algorithm
│ │ ├── np_cloud_3d_pass1.py # 3D Pass 1 (Z by averaging)
│ │ └── np_cloud_3d_pass2.py # 3D Pass 2 (Z iterative)
│ ├── npc_rrnpc_2d.py # 2D two-pass drift correction
│ ├── npc_rrnpc_3d.py # 3D two-pass drift correction
│ └── utils.py # Utility functions
├── scripts/
│ ├── run_npc_rrnpc.py # 2D command line script
│ ├── run_npc_rrnpc_3d.py # 3D command line script
│ └── compare_matlab_python.py # Visualization comparison
├── tests/ # MATLAB comparison tests
├── requirements.txt
└── README.md

File Correspondence (MATLAB to Python)

MATLAB FilePython File
NP_Cloud_NoSort.mnpcloud/core/np_cloud_2d.py
NP_Cloud_NoSort_3D_Pass1.mnpcloud/core/np_cloud_3d_pass1.py
NP_Cloud_NoSort_3D_Pass2.mnpcloud/core/np_cloud_3d_pass2.py
NPC_RRNPC_CallFunction.mnpcloud/npc_rrnpc_2d.py
NPC_RRNPC_3D_CallFunction.mnpcloud/npc_rrnpc_3d.py
Run_NPC_RRNPC.mscripts/run_npc_rrnpc.py
Run_NPC_RRNPC_3D.mscripts/run_npc_rrnpc_3d.py

Testing

Run tests comparing Python vs MATLAB (requires MATLAB engine for Python):

# Activate matlab environment
conda activate matlab_py36
# Run all tests
pytest tests/ -v
# Run specific test
pytest tests/test_np_cloud_2d.py -v

Algorithm Overview

NP-Cloud (Nearest Paired Cloud)

  1. Build Displacement Cloud: For each molecule in the comparing segment, find all molecules in the reference segment within a search radius and record displacement vectors.

  2. Iterative Convergence: Shift the cloud center iteratively until convergence to find the true drift.

  3. Nearest Neighbor Filtering: Keep only the nearest neighbor pair for each molecule to improve accuracy.

RR-NP-Cloud (Resampled Reference NP-Cloud)

A two-pass approach:

  • Pass 1 (NPC): Use the first segment as reference
  • Pass 2 (RR-NPC): Resample from the entire drift-corrected dataset as reference for refinement

Citation

If you use this code, please cite the original paper:

@article{NPCloud,
title={Fast and robust drift correction for single-molecule localization microscopy},
author={...},
journal={...},
year={...}
}

License

See original MATLAB code license.

Acknowledgments

This Python implementation is based on the original MATLAB code from the NPCloud project.

About

Python implementation of NP-Cloud drift correction for SMLM

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages