Repository navigation
perf: annotate dense fragments without copying peaks per precursor - #470
Open
boopthesnoot wants to merge 2 commits into
Open
boopthesnoot wants to merge 2 commits into
boopthesnoot wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
annotate_fragments_flatcopies 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_fragmentsthen 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 offragment_mz_dfwithout copying them, with one precursor per parallel iteration. It writes the densefragment_intensity_df, where a cell matched by several peaks keeps the last one as incalc_dense_fragments. It computespifandmass_accuracyfrom the matched peaks and adds the other three precursor metrics from the dense intensities._match_peakscontains the peak matching.annotate_spectrumcalls it, so both paths match peaks with the same code.calculate_pifand_nan_medianare compiled withnumba_njit_or_python, so the parallel loop calls the same metric functions asapply_precursor_metrics. Without numba they stay plain Python.apply_dense_precursor_metricsgroupssequence_coverage,sequence_giniandnormalized_count.apply_precursor_metricscalls it.Behaviour change
numba sums the float32 intensities in
calculate_pifin order, while numpy summed them pairwise. On the run below,piffromapply_precursor_metricschanged for 969,600 of 1,258,779 precursors by at most 9.5e-7. Thepifcolumn 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:
flatten_fragments,annotate_fragments_flat,calc_dense_fragments,apply_precursor_metricsannotate_fragments_dense, 1 threadannotate_fragments_dense, 8 threadsTimes 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.pypasses.test_annotate_fragments_densechecks hand-computed intensities,pif,mass_accuracyandsequence_coveragefor two precursors that share a spectrum, a fragment matched by two peaks and a precursor without peaks.