Skip to content

Use KDTree spatial indexing for PrecursorCorrection overlap checks - #8694

Closed
timosachsenberg with Copilot wants to merge 3 commits into
developfrom
copilot/update-kdtree-overlap-checks
Closed

timosachsenberg with Copilot wants to merge 3 commits into
developfrom
copilot/update-kdtree-overlap-checks

Conversation

Copilot AI commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor

The correctToNearestFeature method performs 812M+ overlap checks on standard datasets due to O(n×m) nested loops over all MS2 spectra and features.

Changes

  • Build KDTreeFeatureMaps spatial index from features (one-time O(m log m) cost)
  • Pre-compute maximum feature m/z extent for KDTree query window sizing
  • Replace nested loop with queryRegion() spatial queries (O(n log m))
  • Only check features within RT-MZ window against each precursor

Impact

For typical datasets (~15K precursors, ~54K features):

  • Before: 810M overlap checks
  • After: 750K checks (assuming ~50 features per window)
  • 99.9% reduction

The overlaps_() and compatible_() logic remain unchanged. Results are identical to the original implementation.

// Build spatial index once
KDTreeFeatureMaps kd_tree;
kd_tree.addMaps({features});

// Query only nearby features per precursor
vector<Size> candidate_indices;
kd_tree.queryRegion(rt - rt_tol, rt + rt_tol, 
                   pc_mz - max_mz_extent, pc_mz + max_mz_extent,
                   candidate_indices);
Original prompt

This section details on the original issue you should resolve

<issue_title>PrecursorCorrection use our kdtree or other structure for overlap checks</issue_title>
<issue_description>While currently not a bottleneck it just doesn't feel right to perform 812631750 overlap on a standard dataset ;)</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Update PrecursorCorrection to use kdtree for overlap checks Use KDTree spatial indexing for PrecursorCorrection overlap checks Jan 26, 2026
Copilot AI requested a review from timosachsenberg January 26, 2026 07:02
@timosachsenberg

Copy link
Copy Markdown
Contributor

bullshit

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.

PrecursorCorrection use our kdtree or other structure for overlap checks

2 participants