Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/mzml_utils/xic.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@ def _window_intensity(mz: np.ndarray, intensity: np.ndarray, target_mz: float,
tol_da = tolerance
else:
raise ValueError(f"Unknown unit '{unit}'. Use 'ppm' or 'Da'.")
# The window edge is tested in float64. A SpectrumCache stores m/z as float32, and under
# NumPy 2's promotion rules (NEP 50) `float32_array - python_float` stays float32, so the
# TARGET was being rounded to float32 first (about 0.06 ppm at m/z 1000) and a peak at the
# edge could fall inside on an mzML and outside on the cache built from it. (NumPy 1.x
# promoted to float64 and did not show it; the cast makes the result the same under both.)
# The window edge is tested in float64. A SpectrumCache stores m/z as float32, and
# `float32_array - python_float` stays float32 under NumPy 1.x and 2.x alike (a Python
# float is a "weak" scalar in both), so the TARGET was being rounded to float32 first
# (about 0.06 ppm at m/z 1000) and a peak at the edge could fall inside on an mzML and
# outside on the cache built from it. Only a NumPy float64 SCALAR target behaves
# differently between the two (NEP 50 promotes it, value-based casting demoted it); the
# explicit float64 cast makes every caller and every NumPy agree.
mask = np.abs(np.asarray(mz, dtype=np.float64) - target_mz) <= tol_da
if not mask.any():
return 0.0
Expand Down
12 changes: 7 additions & 5 deletions tests/test_xic.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,13 @@ def test_no_peak_gives_empty_metrics(self):

class TestWindowEdgeIsFloat64:
def test_float32_mz_arrays_match_a_float64_reference(self):
"""A SpectrumCache stores m/z as float32. Under NumPy 2's promotion rules (NEP 50)
`float32_array - python_float` stays float32, so the target used to be rounded to float32
first (~0.06 ppm at m/z 1000) and a peak at the window edge could fall inside on the mzML
and outside on its cache. NumPy 1.x promoted the same expression to float64, which is why
the float32 reference below casts the target explicitly: the test must bite under either."""
"""A SpectrumCache stores m/z as float32, and `float32_array - python_float` stays
float32 under NumPy 1.x and 2.x alike, so the target used to be rounded to float32 first
(~0.06 ppm at m/z 1000) and a peak at the window edge could fall inside on the mzML and
outside on its cache. The float32 reference below rounds the target explicitly: an
earlier version subtracted the np.linspace float64 SCALAR, which NumPy 2 promotes to
float64 (NEP 50) and NumPy 1 demoted, so the reference agreed with float64 on CI and
the test recorded no disagreement there."""
from mzml_utils.xic import _window_intensity
rng = np.random.default_rng(7)
mz64 = np.sort(rng.uniform(1190.0, 1210.0, 20000))
Expand Down
Loading