BiocNeighbors 1.0.0
Another application of the KMKNN algorithm is to identify all neighboring points within a certain (Euclidean) distance 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] 9683 1
##
## [[2]]
## [1] 5081 2
##
## [[3]]
## [1] 6654 5761 3
##
## [[4]]
## [1] 1297 4
##
## [[5]]
## [1] 6016 851 5 2987 3824
##
## [[6]]
## [1] 2798 2768 7733 8357 5223 6087 1123 5628 901 242 6 5366
head(fout$distance)
## [[1]]
## [1] 0.9581993 0.0000000
##
## [[2]]
## [1] 0.9711536 0.0000000
##
## [[3]]
## [1] 0.9069003 0.9263137 0.0000000
##
## [[4]]
## [1] 0.9829846 0.0000000
##
## [[5]]
## [1] 0.9738536 0.9320095 0.0000000 0.9778623 0.9370519
##
## [[6]]
## [1] 0.8952555 0.9503344 0.8737941 0.9848975 0.9932740 0.9187211 0.9914165
## [8] 0.9677364 0.9921727 0.9791581 0.0000000 0.9180528
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] 6654 5761 3
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.9069003 0.9263137 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 <- buildNNIndex(data, BNPARAM=KmknnParam())
fout.pre <- findNeighbors(precomputed=pre, threshold=1)
qout.pre <- queryNeighbors(precomputed=pre, query=query, threshold=1)
Users are referred to the documentation of each function for specific details.
sessionInfo()
## R version 3.5.1 Patched (2018-07-24 r75008)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2012 R2 x64 (build 9600)
##
## 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] BiocNeighbors_1.0.0 BiocParallel_1.16.0 knitr_1.20
## [4] BiocStyle_2.10.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.19 bookdown_0.7 digest_0.6.18
## [4] rprojroot_1.3-2 backports_1.1.2 stats4_3.5.1
## [7] magrittr_1.5 evaluate_0.12 stringi_1.2.4
## [10] S4Vectors_0.20.0 rmarkdown_1.10 tools_3.5.1
## [13] stringr_1.3.1 parallel_3.5.1 xfun_0.4
## [16] yaml_2.2.0 compiler_3.5.1 BiocGenerics_0.28.0
## [19] BiocManager_1.30.3 htmltools_0.3.6