BiocNeighbors 1.18.0
Another application of the KMKNN or VP tree algorithms is to identify all neighboring points within a certain distance1 The default here is Euclidean, but again, we can set distance="Manhattan"
in the BNPARAM
object if so desired. of the current point.
We first mock up some data:
nobs <- 10000
ndim <- 20
data <- matrix(runif(nobs*ndim), ncol=ndim)
We apply the findNeighbors()
function to data
:
fout <- findNeighbors(data, threshold=1)
head(fout$index)
## [[1]]
## [1] 1 4182 9054
##
## [[2]]
## [1] 4380 2
##
## [[3]]
## [1] 2226 2472 1655 4582 9200 2241 7142 6315 9298 3025 8107 3 7848 6500 8109
## [16] 4086 1116 285
##
## [[4]]
## [1] 4 8202 5588 9723
##
## [[5]]
## [1] 5 8636
##
## [[6]]
## [1] 3851 6902 6171 6148 7109 8719 5151 4214 5961 6 5083 1848 1494 8259 944
head(fout$distance)
## [[1]]
## [1] 0.0000000 0.9967663 0.9871965
##
## [[2]]
## [1] 0.6841379 0.0000000
##
## [[3]]
## [1] 0.9818217 0.9862802 0.9295874 0.9866842 0.8949421 0.9928102 0.9110744
## [8] 0.9688307 0.8311477 0.9671546 0.9184399 0.0000000 0.9743137 0.8797036
## [15] 0.9858389 0.9353820 0.9417429 0.9180806
##
## [[4]]
## [1] 0.0000000 0.8876210 0.9382543 0.9665364
##
## [[5]]
## [1] 0.0000000 0.9917996
##
## [[6]]
## [1] 0.9287338 0.9053904 0.9951980 0.9902664 0.9944217 0.9184797 0.9683886
## [8] 0.9939547 0.9231684 0.0000000 0.8725382 0.8820620 0.8729779 0.9794740
## [15] 0.9538635
Each entry of the index
list corresponds to a point in data
and contains the row indices in data
that are within threshold
.
For example, the 3rd point in data
has the following neighbors:
fout$index[[3]]
## [1] 2226 2472 1655 4582 9200 2241 7142 6315 9298 3025 8107 3 7848 6500 8109
## [16] 4086 1116 285
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.9818217 0.9862802 0.9295874 0.9866842 0.8949421 0.9928102 0.9110744
## [8] 0.9688307 0.8311477 0.9671546 0.9184399 0.0000000 0.9743137 0.8797036
## [15] 0.9858389 0.9353820 0.9417429 0.9180806
Note that, for this function, the reported neighbors are not sorted by distance. The order of the output is completely arbitrary and will vary depending on the random seed. However, the identity of the neighbors is fully deterministic.
The queryNeighbors()
function is also provided for identifying all points within a certain distance of a query point.
Given a query data set:
nquery <- 1000
ndim <- 20
query <- matrix(runif(nquery*ndim), ncol=ndim)
… we apply the queryNeighbors()
function:
qout <- queryNeighbors(data, query, threshold=1)
length(qout$index)
## [1] 1000
… where each entry of qout$index
corresponds to a row of query
and contains its neighbors in data
.
Again, the order of the output is arbitrary but the identity of the neighbors is deterministic.
Most of the options described for findKNN()
are also applicable here.
For example:
subset
to identify neighbors for a subset of points.get.distance
to avoid retrieving distances when unnecessary.BPPARAM
to parallelize the calculations across multiple workers.raw.index
to return the raw indices from a precomputed index.Note that the argument for a precomputed index is precomputed
:
pre <- buildIndex(data, BNPARAM=KmknnParam())
fout.pre <- findNeighbors(BNINDEX=pre, threshold=1)
qout.pre <- queryNeighbors(BNINDEX=pre, query=query, threshold=1)
Users are referred to the documentation of each function for specific details.
sessionInfo()
## R version 4.3.0 RC (2023-04-13 r84266)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.34.1 BiocNeighbors_1.18.0 knitr_1.42
## [4] BiocStyle_2.28.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.1 rlang_1.1.0 xfun_0.38
## [4] jsonlite_1.8.4 S4Vectors_0.38.1 htmltools_0.5.5
## [7] stats4_4.3.0 sass_0.4.5 rmarkdown_2.21
## [10] grid_4.3.0 evaluate_0.20 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.7 bookdown_0.33
## [16] BiocManager_1.30.20 compiler_4.3.0 codetools_0.2-19
## [19] Rcpp_1.0.10 lattice_0.21-8 digest_0.6.31
## [22] R6_2.5.1 parallel_4.3.0 bslib_0.4.2
## [25] Matrix_1.5-4 tools_4.3.0 BiocGenerics_0.46.0
## [28] cachem_1.0.7