Skip to content

perf: annotate dense fragments without copying peaks per precursor - #470

Open
boopthesnoot wants to merge 2 commits into
mainfrom
perf/annotate-dense
Open

boopthesnoot wants to merge 2 commits into
mainfrom
perf/annotate-dense

Conversation

@boopthesnoot

@boopthesnoot boopthesnoot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

annotate_fragments_flat copies all peaks of a precursor's spectrum into a new DataFrame for every precursor and concatenates them. When many precursors share a spectrum, as de novo candidates do, memory grows with precursors × peaks per spectrum. calc_dense_fragments then maps every copied peak through a Python dict. For 20 de novo candidates per spectrum of one DDA run this needs about 96 GB, although the dense intensities and the precursor metrics fit in a few GB.

Change

  • annotate_fragments_dense(speclib, raw_data_peak_df, mass_error_ppm) matches each precursor's peaks against its rows of fragment_mz_df without copying them, with one precursor per parallel iteration. It writes the dense fragment_intensity_df, where a cell matched by several peaks keeps the last one as in calc_dense_fragments. It computes pif and mass_accuracy from the matched peaks and adds the other three precursor metrics from the dense intensities.
  • _match_peaks contains the peak matching. annotate_spectrum calls it, so both paths match peaks with the same code.
  • calculate_pif and _nan_median are compiled with numba_njit_or_python, so the parallel loop calls the same metric functions as apply_precursor_metrics. Without numba they stay plain Python.
  • apply_dense_precursor_metrics groups sequence_coverage, sequence_gini and normalized_count. apply_precursor_metrics calls it.

Behaviour change

numba sums the float32 intensities in calculate_pif in order, while numpy summed them pairwise. On the run below, pif from apply_precursor_metrics changed for 969,600 of 1,258,779 precursors by at most 9.5e-7. The pif column is now always float64. Before, it was float32 when every precursor had peaks.

Measurements

1,258,779 de novo candidates for 63,015 spectra of one Astral DDA run (858,195,268 copied peaks), 24 charged fragment types:

peak RSS time
flatten_fragments, annotate_fragments_flat, calc_dense_fragments, apply_precursor_metrics 95.7 GB 1,162 s
annotate_fragments_dense, 1 thread 6.4 GB 76 s
annotate_fragments_dense, 8 threads 6.5 GB 62 s

Times include the fragment m/z calculation and numba compilation. On this branch the dense path and the flat path give identical intensities and metrics for every candidate.

Tests

pytest tests/unit/spectral_library tests/test_fragment.py passes. test_annotate_fragments_dense checks hand-computed intensities, pif, mass_accuracy and sequence_coverage for two precursors that share a spectrum, a fragment matched by two peaks and a precursor without peaks.

annotate_fragments_flat copies every peak of a precursor's spectrum into a new
DataFrame for each precursor, so memory grows with precursors x peaks per spectrum
when many precursors share a spectrum, as de novo candidates do. The dense
conversion then maps every copied peak through a Python dict.

annotate_fragments_dense matches each precursor's peaks in place, in parallel, and
writes the dense intensity matrix and the peak-level metrics (pif, mass_accuracy)
directly. The peak matching is shared with annotate_spectrum, and the dense metrics
are shared with apply_precursor_metrics.

On 1,258,779 de novo candidates for 63,015 DDA spectra (858,195,268 copied peaks):
flat path (flatten, annotate_fragments_flat, calc_dense_fragments,
apply_precursor_metrics) 95.7 GB peak RSS and 1,162 s; dense path 6.4 GB and 62 s
(28 s in annotate_fragments_dense with 8 threads). Intensities, sequence_coverage,
sequence_gini, normalized_count and mass_accuracy are identical. pif differs by at
most 1.2e-7 because the dense path sums intensities in float64.
The dense annotation summed intensities and took the median of mass errors
with its own code. It now calls calculate_pif and _nan_median, which numba
compiles so that the parallel loop can call them, and passes each peak's
fragment series as its type. The annotation test checks hand-computed
intensities and metrics instead of comparing with the flat path.

This branch has not been deployed

No deployments
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