Fix 870: Significant memory and runtime improvements for Ripley's L#1236
Open
jberg5 wants to merge 3 commits into
Open
Fix 870: Significant memory and runtime improvements for Ripley's L#1236jberg5 wants to merge 3 commits into
jberg5 wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1236 +/- ##
==========================================
+ Coverage 75.32% 76.81% +1.49%
==========================================
Files 56 63 +7
Lines 7936 9270 +1334
Branches 1295 1566 +271
==========================================
+ Hits 5978 7121 +1143
- Misses 1447 1547 +100
- Partials 511 602 +91
🚀 New features to boost your workflow:
|
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.
Description
Headline number: at 250,000 cells in a single cluster, memory usage drops from ~1.8tb to 0.2gb, and runtime drops from probably something like 40 minutes (extrapolated because I don't have 1.8tb of ram lol) to 6 minutes on a GCP
n2-highmem-8.Peak memory
main(pdist)branch(KDTree)Runtime
main(pdist)branch(KDTree)(* for Claude extrapolated because the process OOMed on main)
Previously, ripley's L calculation materialized O(n^2) pairwise distances (via
pdist) and then broadcast that across the number of steps insupport. In issue #870, at 250,000 cells, this isn * (n - 1) / 2 = 31,249,875,000unordered pairs, multiplied by 50 steps meansdistances < support.reshape(-1, 1)would be a 2D bool array of50 * 31,249,875,000bytes, so roughly 1.5tb of memory (excluding thepdistintermediate, which would still exist and add another ~250gb on top of that), assuming all cells are in the same cluster. This would OOM on pretty much any reasonable hardware.Fortunately, we can skip all of that by using a KDTree. Long story short once we build the binary tree once, using O(n) memory, and then two_point_correlation traverses this structure to find the number of points within each radius without materializing every pairwise distance. This gives us O(n) memory usage instead of O(n^2).
One thing to note: this narrows the list of valid metrics down to:
whereas previously
pdistwould have accepted any ofbut I don't think any of the dropped ones were valid / sensical metrics for the kind of spatial stats that are happening here.
How has this been tested?
main.Closes
closes #870