BiocNeighbors 1.11.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 6545 740 1980 1249
##
## [[2]]
## [1] 2 9908 1216
##
## [[3]]
## [1] 8169 37 3
##
## [[4]]
## [1] 9969 524 9743 465 7593 4 8862 9481 9244 7339 5968 2598 1125 717 567
## [16] 103 3843 6207 618 3183 1463 1986 7759
##
## [[5]]
## [1] 5
##
## [[6]]
## [1] 1818 5413 8562 6034 6
head(fout$distance)
## [[1]]
## [1] 0.0000000 0.7667693 0.9986509 0.9494600 0.9828091
##
## [[2]]
## [1] 0.0000000 0.9465021 0.9106771
##
## [[3]]
## [1] 0.8407744 0.8260629 0.0000000
##
## [[4]]
## [1] 0.9273443 0.8527587 0.9738556 0.9458984 0.9873350 0.0000000 0.9780729
## [8] 0.8906146 0.9735840 0.9892468 0.8625085 0.9966137 0.9164379 0.9565362
## [15] 0.9457568 0.9794949 0.9904697 0.9079275 0.9692205 0.9206782 0.9943601
## [22] 0.9956507 0.9950893
##
## [[5]]
## [1] 0
##
## [[6]]
## [1] 0.9822839 0.9526688 0.9738099 0.9492343 0.0000000
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] 8169 37 3
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.8407744 0.8260629 0.0000000
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.1.0 (2021-05-18)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 17763)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.27.0 BiocNeighbors_1.11.0 knitr_1.33
## [4] BiocStyle_2.21.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 magrittr_2.0.1 BiocGenerics_0.39.0
## [4] lattice_0.20-44 R6_2.5.0 rlang_0.4.11
## [7] stringr_1.4.0 tools_4.1.0 parallel_4.1.0
## [10] grid_4.1.0 xfun_0.23 jquerylib_0.1.4
## [13] htmltools_0.5.1.1 yaml_2.2.1 digest_0.6.27
## [16] bookdown_0.22 Matrix_1.3-3 BiocManager_1.30.15
## [19] S4Vectors_0.31.0 sass_0.4.0 evaluate_0.14
## [22] rmarkdown_2.8 stringi_1.6.2 compiler_4.1.0
## [25] bslib_0.2.5.1 stats4_4.1.0 jsonlite_1.7.2