diff --git a/Cargo.toml b/Cargo.toml index 61978cd..1ce10c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "smoll" version = "0.0.1" edition = "2024" -rust-version = "1.88.0" +rust-version = "1.85.1" authors = ["Wim Pomp "] license = "MIT OR Apache-2.0" description = "Single MOLecule Localization." @@ -18,7 +18,7 @@ ndarray = { version = "0.17", features = ["rayon"] } ndrustfft = "0.6" num = "0.4" numpy = { version = "0.30", optional = true } -pyo3 = { version = "0.30", features = ["abi3-py310", "anyhow", "generate-import-lib", "multiple-pymethods"], optional = true } +pyo3 = { version = "0.30", features = ["abi3-py311", "anyhow", "generate-import-lib", "multiple-pymethods"], optional = true } pyo3-stub-gen = { version = "0.23", optional = true } strum = { version = "0.28", features = ["derive"] } thiserror = "2" diff --git a/py/smoll/__init__.py b/py/smoll/__init__.py index f1ff8a3..71bbbd0 100644 --- a/py/smoll/__init__.py +++ b/py/smoll/__init__.py @@ -14,10 +14,11 @@ os.environ["RUST_BACKTRACE"] = "full" os.environ["COLORBT_SHOW_HIDDEN"] = "1" from .smoll_rs import generate_stub -from .smoll_rs import detection, filter +from .smoll_rs import detection, filter, gaussian sys.modules["smoll.detection"] = detection sys.modules["smoll.filter"] = filter +sys.modules["smoll.filter"] = gaussian try: __version__ = version(Path(__file__).parent.name) @@ -33,7 +34,7 @@ except Exception: # noqa __git_commit_hash__ = "unknown" -__all__ = ["detection", "filter"] +__all__ = ["detection", "filter", "gaussian"] def smoll_generate_stub(): diff --git a/py/py.typed b/py/smoll/py.typed similarity index 100% rename from py/py.typed rename to py/smoll/py.typed diff --git a/py/smoll/smoll_rs/__init__.pyi b/py/smoll/smoll_rs/__init__.pyi index 9103870..4930992 100644 --- a/py/smoll/smoll_rs/__init__.pyi +++ b/py/smoll/smoll_rs/__init__.pyi @@ -4,10 +4,12 @@ import builtins from . import detection from . import filter +from . import gaussian __all__ = [ "detection", "filter", + "gaussian", "generate_stub", ] diff --git a/py/smoll/smoll_rs/gaussian.pyi b/py/smoll/smoll_rs/gaussian.pyi new file mode 100644 index 0000000..5b4f6ba --- /dev/null +++ b/py/smoll/smoll_rs/gaussian.pyi @@ -0,0 +1,15 @@ +# This file is automatically generated by pyo3_stub_gen +# ruff: noqa: E501, F401, F403, F405 + +import numpy +import numpy.typing + +__all__ = [ + "gaussian", +] + +def gaussian(x: numpy.typing.ArrayLike, p: numpy.typing.ArrayLike) -> numpy.typing.NDArray[numpy.float64]: + r""" + x: n 1d arrays, defining coordinates in each dimension + p: I, x0, x1, ... xn, s00, s01, ..., s0n, s10, s11, ..., s1n, ..., sn0, sn1, ..., snn, o, o0, o1, ..., on + """ diff --git a/pyproject.toml b/pyproject.toml index bae92a9..fcc4344 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ dynamic = [ "keywords", "urls", ] -requires-python = ">=3.10" +requires-python = ">=3.11" classifiers = [ "License :: OSI Approved :: Apache Software License", "License :: OSI Approved :: MIT License", diff --git a/src/error.rs b/src/error.rs index 552a0d0..d2348ff 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,8 @@ use thiserror::Error; #[derive(Debug, Error, IntoStaticStr)] pub enum Error { + #[error("matrix not invertible")] + MatrixNotInvertible, #[error(transparent)] ShapeError(#[from] ndarray::ShapeError), } diff --git a/src/gaussian.rs b/src/gaussian.rs new file mode 100644 index 0000000..f3e2818 --- /dev/null +++ b/src/gaussian.rs @@ -0,0 +1,186 @@ +use crate::error::Error; +use itertools::Itertools; +use ndarray::{Array, Array2, ArrayView1, AsArray, Axis, Dimension, Ix1, Ix2}; + +const STP: f64 = 0.5 * std::f64::consts::FRAC_2_SQRT_PI * std::f64::consts::FRAC_1_SQRT_2; + +/// p: I, x0, x1, ... xn, s00, s01, ..., s0n, s10, s11, ..., s1n, ..., sn0, sn1, ..., snn, o, o0, o1, ..., on +/// x: n 1d arrays, defining coordinates in each dimension +pub fn gaussian<'a, A, D>(x: Vec, p: &[f64]) -> Result, Error> +where + A: AsArray<'a, f64, Ix1>, + D: Dimension, +{ + let n = x.len(); + let n2 = n * n; + debug_assert_eq!( + p.len(), + 2 + 2 * n + n2, + "number of parameters must be {}", + 2 + 2 * n + n2 + ); + let m = ArrayView1::from(&p[1..1 + n]); + let s = &p[1 + n..1 + n + n2]; + let o = p[1 + n + n2]; + let q = ArrayView1::from(&p[2 + n + n2..]); + let c = Array2::from_shape_vec((n, n), s.to_vec())?.powi(2); + let (lu, perm) = lu_factor(&c)?; + let c_inv = matrix_inverse(&lu, &perm); + let c_det = matrix_determinant(&lu, &perm); + let a = p[0] * STP.powi(n as i32) / c_det.sqrt(); + let x = x + .into_iter() + .map(|i| i.into()) + .collect::>>(); + let shape = x.iter().map(|i| i.len()).collect::>(); + Ok(Array::from_shape_vec( + shape, + x.into_iter() + .zip(m) + .map(|(xi, &mi)| (&xi - mi).to_vec()) + .multi_cartesian_product() + .map(|i| { + let xj = ArrayView1::from(&i).insert_axis(Axis(0)); + a * (-0.5 * xj.dot(&c_inv).dot(&xj.t())).sum().exp() + o + q.dot(&xj.t()).sum() + }) + .collect(), + )? + .into_dimensionality()?) +} + +/// PA = LU factorization (Doolittle with partial pivoting). +/// +/// Returns the packed LU matrix (lower unit triangular in columns +/// 0..k, upper triangular in rows k) and the row permutation, or `None` +/// if a pivot is singular or numerically rank-deficient. +fn lu_factor<'a, A>(matrix: A) -> Result<(Array2, Vec), Error> +where + A: AsArray<'a, f64, Ix2>, +{ + let matrix = matrix.into(); + let n = matrix.nrows(); + debug_assert_eq!(n, matrix.ncols(), "matrix must be square"); + + // pivot magnitudes must exceed this relative threshold to count as significant + let max_abs = matrix.iter().fold(0.0f64, |m, &x| m.max(x.abs())); + let threshold = max_abs * (n as f64) * f64::EPSILON; + + let mut lu = matrix.to_owned(); + let mut perm: Vec = (0..n).collect(); + + for k in 0..n { + let mut pivot_row = k; + for i in (k + 1)..n { + if lu[[i, k]].abs() > lu[[pivot_row, k]].abs() { + pivot_row = i; + } + } + if lu[[pivot_row, k]].abs() <= threshold || !lu[[pivot_row, k]].is_finite() { + return Err(Error::MatrixNotInvertible); + } + if pivot_row != k { + for j in 0..n { + lu.swap([k, j], [pivot_row, j]); + } + perm.swap(k, pivot_row); + } + + for i in (k + 1)..n { + let factor = lu[[i, k]] / lu[[k, k]]; + lu[[i, k]] = factor; + for j in (k + 1)..n { + lu[[i, j]] -= factor * lu[[k, j]]; + } + } + } + + Ok((lu, perm)) +} + +/// Calculate the inverse of a square matrix using LU factorization with partial +/// pivoting. +/// +/// Returns `None` if the matrix is singular, or numerically rank-deficient. +fn matrix_inverse(lu: &Array2, perm: &[usize]) -> Array2 { + let n = lu.nrows(); + + // Solve A * inv = I column by column: first L*y = P*e_col, then U*x = y. + let mut inv = Array2::zeros((n, n)); + for col in 0..n { + let mut y = vec![0.0; n]; + for i in 0..n { + let rhs = if perm[i] == col { 1.0 } else { 0.0 }; + let mut sum = rhs; + for j in 0..i { + sum -= lu[[i, j]] * y[j]; + } + y[i] = sum; + } + for i in (0..n).rev() { + let mut sum = y[i]; + for j in (i + 1)..n { + sum -= lu[[i, j]] * inv[[j, col]]; + } + inv[[i, col]] = sum / lu[[i, i]]; + } + } + inv +} + +/// Calculate the determinant of a square matrix using LU factorization +/// with partial pivoting. +/// +/// Returns `None` if the matrix is singular, or numerically rank-deficient. +fn matrix_determinant(lu: &Array2, perm: &[usize]) -> f64 { + // sign of the row permutation: parity of its inversion count + let inversions = (1..perm.len()) + .map(|i| (0..i).filter(|&j| perm[j] > perm[i]).count()) + .sum::(); + let sign = if inversions % 2 == 0 { 1.0 } else { -1.0 }; + sign * (0..lu.nrows()).map(|k| lu[[k, k]]).product::() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_matrix_det() -> Result<(), Error> { + // trivial 1x1 + let m = Array2::from_shape_vec((1, 1), vec![3.0])?; + let (lu, perm) = lu_factor(&m)?; + assert_eq!(matrix_determinant(&lu, &perm), 3.0); + // positive determinant, needs a row swap + let m = Array2::from_shape_vec((2, 2), vec![2.0, 1.0, 1.0, 3.0])?; + let (lu, perm) = lu_factor(&m)?; + assert!((matrix_determinant(&lu, &perm) - 5.0).abs() < 1e-14); + // negative determinant + let m = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 2.0, 1.0])?; + let (lu, perm) = lu_factor(&m)?; + assert!((matrix_determinant(&lu, &perm) + 3.0).abs() < 1e-14); + // 3x3 with det = 13 + let m = Array2::from_shape_vec((3, 3), vec![2.0, 0.0, 1.0, 1.0, 2.0, 0.0, 0.0, 1.0, 3.0])?; + let (lu, perm) = lu_factor(&m)?; + assert!((matrix_determinant(&lu, &perm) - 13.0).abs() < 1e-13); + // 3x3 requiring row swaps (zero in the leading column), det = 2 + let m = Array2::from_shape_vec((3, 3), vec![0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0])?; + let (lu, perm) = lu_factor(&m)?; + assert!((matrix_determinant(&lu, &perm) - 2.0).abs() < 1e-12); + // singular matrix + let m = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 2.0, 4.0])?; + assert!(lu_factor(&m).is_err()); + // det(A * B) == det(A) * det(B) + let a = Array2::from_shape_vec((3, 3), vec![1.0, 2.0, 3.0, 0.0, 1.0, 4.0, 5.0, 6.0, 0.0])?; + let b = + Array2::from_shape_vec((3, 3), vec![0.0, 1.0, 2.0, -1.0, 0.0, 3.0, 2.0, -1.0, 0.0])?; + let prod = a.dot(&b); + let (lu, perm) = lu_factor(&prod)?; + let ab = matrix_determinant(&lu, &perm); + let (lu, perm) = lu_factor(&a)?; + let a_det = matrix_determinant(&lu, &perm); + let (lu, perm) = lu_factor(&b)?; + let b_det = matrix_determinant(&lu, &perm); + assert!((ab - a_det * b_det).abs() < 1e-10); + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index bacb23b..2994bb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod detection; pub mod error; pub mod filter; +pub mod gaussian; #[cfg(feature = "python")] mod py; diff --git a/src/py.rs b/src/py.rs index 4beff70..d77672c 100644 --- a/src/py.rs +++ b/src/py.rs @@ -1,5 +1,6 @@ mod detection; mod filter; +mod gaussian; use crate::error::Error; use pyo3::exceptions::PyRuntimeError; @@ -84,4 +85,10 @@ mod smoll_rs { #[pymodule_export] use super::super::filter::gaussian_smooth; } + + #[pymodule] + mod gaussian { + #[pymodule_export] + use super::super::gaussian::gaussian; + } } diff --git a/src/py/gaussian.rs b/src/py/gaussian.rs new file mode 100644 index 0000000..4e6259a --- /dev/null +++ b/src/py/gaussian.rs @@ -0,0 +1,31 @@ +use crate::gaussian as gaussian_rs; +use numpy::{AllowTypeChange, IntoPyArray, PyArrayDyn, PyArrayLike1}; +use pyo3::prelude::*; +use pyo3_stub_gen::derive::*; + +/// x: n 1d arrays, defining coordinates in each dimension +/// p: I, x0, x1, ... xn, s00, s01, ..., s0n, s10, s11, ..., s1n, ..., sn0, sn1, ..., snn, o, o0, o1, ..., on +#[gen_stub_pyfunction(module = "smoll.gaussian")] +#[pyfunction] +pub(crate) fn gaussian<'py>( + py: Python<'py>, + #[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))] x: Vec< + PyArrayLike1, + >, + #[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))] + p: PyArrayLike1, +) -> PyResult>> { + let x = x + .into_iter() + .map(|i| i.as_array().to_owned()) + .collect::>(); + let xv = x.iter().map(|i| i.view()).collect::>(); + + Ok(if let Ok(p) = p.as_slice() { + gaussian_rs::gaussian(xv, p)? + } else { + let p = p.as_array().to_vec(); + gaussian_rs::gaussian(xv, &p)? + } + .into_pyarray(py)) +}