mioXpektron.denoise.denoise_main

ToF-SIMS 1D denoising & smoothing utilities

A focused toolkit for 1D spectra (e.g., ToF‑SIMS / MS) that implements wavelet‑shrinkage denoising and classical smoothing filters, plus resampling to a uniform grid when needed. The design emphasizes:

  • Peak preservation: track height, FWHM, area, and m/z boundaries.

  • Noise reduction: robust σ̂ via MAD (baseline regions) and support for variance‑stabilizing transforms (Anscombe) for count‑like noise.

  • Practical ergonomics: optional cycle‑spinning for translation invariance, TIC preservation, and shape‑preserving interpolation.

This module provides the primitive operations used by the higher‑level selection & evaluation code.

Functions

noise_filtering(intensities, *[, method, ...])

Apply 1D denoising/smoothing to ToF-SIMS spectra.

wavelet_denoise(intensities, *[, wavelet, ...])

Wavelet-shrinkage denoising for 1D ToF-SIMS/MS data, with optional translation invariance (cycle-spinning) and TIC preservation.

mioXpektron.denoise.denoise_main.wavelet_denoise(intensities, *, wavelet='sym8', level=None, threshold_mode='soft', threshold_strategy='universal', sigma=None, sigma_strategy='per_level', variance_stabilize='none', anscombe_negative_policy='warn_clip', cycle_spins=0, pywt_mode='periodization', clip_nonnegative=True, preserve_tic=False)[source]

Wavelet-shrinkage denoising for 1D ToF-SIMS/MS data, with optional translation invariance (cycle-spinning) and TIC preservation.

Parameters:
  • intensities (array-like) – 1D intensities (finite values recommended).

  • wavelet ({"db4","db8","sym5","sym8","coif2","coif3"}) – Discrete wavelet family used for the DWT.

  • level (int or None) – DWT level; if None, chosen conservatively via pywt.dwt_max_level (capped at 6).

  • threshold_mode ({"soft","hard"}) – Shrinkage mode (soft is default and recommended for denoising).

  • threshold_strategy ({"universal","bayes","sure","sure_opt"}) –

    Threshold selection per detail level:
    • ”universal”: √(2 log N) scaling (Donoho–Johnstone)

    • ”bayes”: BayesShrink (subband variance model)

    • ”sure”: SURE-threshold via full knot evaluation (fast, exact at knots)

    • ”sure_opt”: SURE with biased candidate subsampling + local refinement (faster)

  • sigma (float or None) – If provided, used as a global noise std at all levels (overrides sigma_strategy).

  • sigma_strategy ({"per_level","global"}) –

    How to estimate σ when sigma is None.
    • ”per_level”: σ_j via MAD on each detail subband (robust; recommended)

    • ”global”: a single σ via MAD on the finest detail of the unshifted input

  • variance_stabilize ({"none","anscombe"}) – Apply a variance-stabilizing transform prior to denoising. "anscombe" uses the classical Anscombe forward transform with the Mäkitalo-Foi unbiased inverse, appropriate for non-negative Poisson-like input.

  • anscombe_negative_policy ({"warn_clip","clip","raise"}) – How to handle negative values before the classical Anscombe transform. "warn_clip" preserves backward compatibility but emits a warning; "raise" is recommended when negative values are scientifically meaningful residuals rather than impossible counts.

  • cycle_spins ({0,4,8,16,32}) – Number of circular shifts to average (0 disables; try 8–16 for higher SNR if compute allows). If cycle_spins ≥ N on very long vectors, the implementation caps the number of shifts at 2048 for performance.

  • pywt_mode (str) – Wavelet signal extension mode. Common choices: ‘periodization’ (reduces edge artifacts), ‘symmetric’, ‘reflect’. See PyWavelets docs for full list.

  • clip_nonnegative (bool) – Clip negative outputs to zero (recommended for intensities).

  • preserve_tic (bool) – If True, rescale output so sum(out) == sum(input) (guarded to avoid division by zero).

Returns:

Denoised intensities, same length as input.

Return type:

np.ndarray

Raises:

ValueError – If intensities is not 1D, or an unknown threshold_strategy is provided.

See also

noise_filtering

High-level wrapper supporting classical smoothers and resampling.

mioXpektron.denoise.denoise_main.noise_filtering(intensities, *, method='wavelet', window_length=15, polyorder=3, deriv=0, gauss_sigma_pts=None, gaussian_order=0, wavelet='sym8', level=None, threshold_strategy='universal', threshold_mode='soft', sigma=None, sigma_strategy='per_level', variance_stabilize='none', anscombe_negative_policy='warn_clip', cycle_spins=0, pywt_mode='periodization', clip_nonnegative=True, preserve_tic=False, x=None, resample_to_uniform=False, target_dx=None, forward_interp='pchip')[source]

Apply 1D denoising/smoothing to ToF-SIMS spectra.

Notes

  • Savitzky–Golay / Gaussian / Median assume ~uniform sampling. If your m/z grid is nonuniform, pass x and set resample_to_uniform=True. The wavelet path can also resample when resample_to_uniform=True.

  • Wavelet shrinkage preserves narrow peaks; consider Bayes/SURE and cycle-spins.

Parameters:
  • intensities (np.ndarray) – 1D intensity array.

  • method ({'savitzky_golay','gaussian','median','wavelet','none'})

  • window_length (int) – Odd window for Savitzky–Golay or median; will be coerced to odd >=3.

  • polyorder (int) – For Savitzky–Golay, 0 ≤ polyorder < window_length.

  • deriv (int) – For Savitzky–Golay, derivative order (0 = smoothing; 1/2/… compute derivatives). Requires polyorder >= deriv.

  • gauss_sigma_pts (float or None) – If provided, overrides default sigma = window_length/6 for Gaussian filter.

  • gaussian_order (int) – For Gaussian filtering, derivative order for ndimage.gaussian_filter1d. 0 = smoothing; >0 computes derivatives.

  • wavelet (Literal['db4', 'db8', 'sym5', 'sym8', 'coif2', 'coif3']) – Passed to wavelet processing (see wavelet_denoise).

  • level (int | None) – Passed to wavelet processing (see wavelet_denoise).

  • threshold_strategy (Literal['universal', 'bayes', 'sure', 'sure_opt']) – Passed to wavelet processing (see wavelet_denoise).

  • threshold_mode (Literal['soft', 'hard']) – Passed to wavelet processing (see wavelet_denoise).

  • sigma (float | None) – Passed to wavelet processing (see wavelet_denoise).

  • cycle_spins (Literal[0, 4, 8, 16, 32]) – Passed to wavelet processing (see wavelet_denoise).

  • pywt_mode (str) – Passed to wavelet processing (see wavelet_denoise).

  • sigma_strategy ({"per_level","global"}) – Strategy if sigma is None. “per_level” = σ_j via MAD on each detail subband; “global” = one σ via MAD on the finest detail of the unshifted input.

  • variance_stabilize ({"none","anscombe"}) – Apply variance‑stabilizing transform before denoising. "anscombe" uses the classical Anscombe transform for non-negative Poisson-like input.

  • anscombe_negative_policy ({"warn_clip","clip","raise"}) – Handling policy for negative values before the classical Anscombe transform.

  • clip_nonnegative (bool) – Output behaviors.

  • preserve_tic (bool) – Output behaviors.

  • x (np.ndarray or None) – Optional m/z (or channel) axis aligned with intensities.

  • resample_to_uniform (bool) – If True and x is provided, internally resample to a uniform grid and back.

  • target_dx (float or None) – Target spacing for the uniform grid (if None, inferred).

  • forward_interp ({'pchip','linear'}) – Interpolant used when building the uniform-grid signal (PCHIP recommended).

Returns:

Filtered intensities aligned to the input grid/order.

Return type:

np.ndarray

Raises:

ValueError – If intensities or x have mismatched shapes or if intensities is not 1D. If Savitzky–Golay has polyorder < deriv after clamping, or if method is unknown.

See also

wavelet_denoise

Core wavelet denoising routine used when method=”wavelet”.