Created
August 10, 2022 20:40
-
-
Save LTLA/0cad5ae0b42bbc2bae73315676b6f17f to your computer and use it in GitHub Desktop.
Downsample code based on the neighbors
This file contains 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
mat <- matrix(rnorm(100000), ncol=10) | |
library(BiocNeighbors) | |
res <- findKNN(mat, k=20) | |
d <- res$distance[,ncol(res$distance)] | |
processed <- logical(nrow(mat)) | |
candidates <- logical(nrow(mat)) | |
while (!all(processed)) { | |
keep <- which(!processed) | |
coverage <- vapply(keep, function(x) sum(processed[res$index[x,]]), 0) | |
o <- order(coverage, d[keep]) | |
limit <- min(coverage) | |
print(limit) | |
for (i in o) { | |
pt <- keep[i] | |
if (processed[pt]) { | |
next | |
} else if (coverage[i] > limit) { | |
break | |
} | |
neighbors <- res$index[pt,] | |
updated.coverage <- sum(processed[neighbors]) | |
if (updated.coverage == limit) { | |
candidates[pt] <- TRUE | |
processed[pt] <- TRUE | |
processed[neighbors] <- TRUE | |
} | |
} | |
} | |
mean(candidates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment