Skip to content

FIX: Keep IPSA regions aligned with unsorted input coordinates - #99

Open
antonsoo wants to merge 1 commit into
derb12:developmentfrom
antonsoo:fix/ipsa-region-order
Open

antonsoo wants to merge 1 commit into
derb12:developmentfrom
antonsoo:fix/ipsa-region-order

Conversation

@antonsoo

Copy link
Copy Markdown

Description

IPSA sorts the data by x_data, but uses roi in that sorted array without translating its input indices. With original_criteria=True, reversing or shuffling the same spectrum and region can change the tolerance history and when smoothing stops.

import numpy as np
from pybaselines import Baseline

x = np.linspace(1, 100, 100)
y = .004*x**2 + 3*np.exp(-((x-18)/3)**2) + 10*np.exp(-((x-80)/3)**2)
roi = (x > 10) & (x < 25)
order = np.random.default_rng(10).permutation(len(x))
options = dict(half_window=5, original_criteria=True, tol=.02, max_iter=30)
expected, a = Baseline(x).ipsa(y, roi=roi, **options)
actual, b = Baseline(x[order]).ipsa(y[order], roi=roi[order], **options)
print(len(a['tol_history']), len(b['tol_history']))
print(np.max(np.abs(expected - actual[np.argsort(order)])))

Before: 27 versus 31 iterations, with maximum baseline difference about 0.27165. After: both use 27 iterations and the baselines are identical.

Translate the selected original indices into sorted positions before calculating either the default tolerance or the original stopping criterion. Preserve slice, integer-index and Boolean-mask selections. The ordinary stopping criterion and roi=None path remain unchanged.

Twelve coordinate-order regressions fail before and pass after. On eight recorded Raman spectra, a separate implementation derives quadratic smoothing weights from a Vandermonde pseudoinverse and uses direct convolution. All 384 cases now match it: 192 stopping-history mismatches and 76 wrong baselines are corrected, with maximum relative baseline error below 4.9e-13. All 192 ordered/all-point controls and 96 ordinary-criterion controls retain identical baseline and history hashes. This checks region selection and smoothing, not spectral identification quality.

Type of Pull Request

  • Bug Fix
  • New Feature
  • Miscellaneous Changes (refactor, code improvements, etc.)
  • Documentation or Example Programs

Pull Request Checklist

  • New code and/or documentation is valid for use with the BSD 3-Clause license
  • New code follows the style and docstring guidelines (ruff check . passes)
  • Added/updated tests and ensured they pass locally (7,090 passed, 73 skipped; unrelated 2D tests excluded as permitted by the guide; 13 doctests pass)
  • Verified that documentation builds locally (n/a: no documentation, docstring or public API change)

Validated on Linux/Python 3.12. The correction is limited to the original criterion's ROI mapping; other algorithm choices and parallel behavior were not changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant