Experimental Port: This project is an experiment in porting the R package
ptwto Rust. It leverages Rust's type safety and performance features (including parallel execution viarayon) while maintaining full API compatibility with the original R package.
This package implements Parametric Time Warping (PTW), a technique used to align signal patterns (e.g., chromatograms, spectra) by non-linearly warping the time axis. The core computational logic, including the warping algorithms and optimization routines (Nelder-Mead), has been rewritten in Rust to assess the feasibility and benefits of a hybrid R/Rust architecture.
- Full Feature Parity: Supports all standard
ptwoptions, including:- Warping Modes:
global(one warp for all samples) andindividual(per-sample warping). - Optimization Criteria:
RMS(Root Mean Square) andWCC(Weighted Cross Correlation). - Initialization: Support for custom initial coefficients and automatic restart strategies (
try = TRUE).
- Warping Modes:
- High Performance:
- Parallelization: Multithreaded processing for multiple samples using
rayon, automatically utilizing available CPU cores. - Numerical Stability: Implements advanced parameter scaling to ensure robust optimization convergence even for large datasets (N > 10,000) where polynomial warping typically faces conditioning issues.
- Parallelization: Multithreaded processing for multiple samples using
- Rust Backend:
- Custom Nelder-Mead Simplex Optimizer implemented in pure Rust.
- Seamless R integration via
extendr.
If you do not have the Rust toolchain installed, you can install the pre-compiled binary packages from the GitHub Releases page.
- Download the appropriate file for your OS:
- Windows:
ptwRust_*.zip - macOS:
ptwRust_*.tgz
- Windows:
- Install in R:
# Replace path with the actual location of the downloaded file
install.packages("~/Downloads/ptwRust_0.1.0.zip", repos = NULL)Ensure you have the Rust toolchain installed (including cargo).
# Install directly from the repository or local source
devtools::install()The API mirrors the original ptw package, but the package name is ptwRust.
library(ptwRust)
# Generate synthetic data
time <- seq(0, 100, length.out = 100)
ref <- exp(-0.5 * (time - 50)^2 / 10) # Reference peak at 50
samp <- exp(-0.5 * (time - 60)^2 / 10) # Sample peak at 60 (shifted)
# Align sample to reference using Global Warping and RMS
model <- ptw(ref, samp, warp.type = "global", optim.crit = "RMS")
# Inspect coefficients (Expect intercept ~10.0)
print(model$warp.coef)
# Access warped signal
plot(time, ref, type = "l", col = "black", main = "PTW Alignment")
lines(time, samp, col = "red", lty = 2)
lines(time, model$warped.sample, col = "blue", lwd = 2)
legend("topright", legend = c("Reference", "Original", "Warped"),
col = c("black", "red", "blue"), lty = c(1, 2, 1))R/ptw.R: R wrapper function that prepares data matrices and calls the Rust backend.src/rust/: The Rust crate containing the logic.src/lib.rs:extendrbindings exposingptw_fitandptw_predict.src/model.rs:PtwModelstruct, warping logic, and WCC implementation. Usesrayonfor parallelizing individual warping tasks.src/optim.rs: Generic Nelder-Mead optimizer with robust parameter handling.
Unit tests are implemented in both Rust (via cargo test) and R (via testthat) to ensure numerical correctness against the behavior of the original package.
MIT