| 1 |
# =============================== |
| 2 |
# NEAR NEIGHBOUR FINDER |
| 3 |
# =============================== |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#'Nearest Neighbour Search |
| 8 |
#' |
| 9 |
#'Uses a kd-tree to find the p number of near neighbours for each point in an |
| 10 |
#'input/output dataset. The advantage of the kd-tree is that it runs in O(M log |
| 11 |
#'M) time. |
| 12 |
#' |
| 13 |
#'The \code{RANN} package utilizes the Approximate Near Neighbor (ANN) C++ |
| 14 |
#'library, which can give the exact near neighbours or (as the name suggests) |
| 15 |
#'approximate near neighbours to within a specified error bound. For more |
| 16 |
#'information on the ANN library please visit |
| 17 |
#'\url{https://www.cs.umd.edu/~mount/ANN/}. |
| 18 |
#' |
| 19 |
#'Search types: \code{priority} visits cells in increasing order of distance |
| 20 |
#'from the query point, and hence, should converge more rapidly on the true |
| 21 |
#'nearest neighbour, but standard is usually faster for exact searches. |
| 22 |
#'\code{radius} only searches for neighbours within a specified radius of the |
| 23 |
#'point. If there are no neighbours then nn.idx will contain 0 and nn.dists |
| 24 |
#'will contain 1.340781e+154 for that point. |
| 25 |
#' |
| 26 |
#'@param data An \bold{M} x \bold{d} data.frame or matrix, where each of the |
| 27 |
#' \bold{M} rows is a point or a (column) vector (where \bold{d=1}). |
| 28 |
#'@param query A set of \bold{N} x \bold{d} points that will be queried against |
| 29 |
#' \code{data}. \bold{d}, the number of columns, must be the same as |
| 30 |
#' \code{data}. If missing, defaults to \code{data}. |
| 31 |
#'@param k The maximum number of nearest neighbours to compute. The default |
| 32 |
#' value is set to the smaller of the number of columnns in data |
| 33 |
#'@param treetype Character vector specifying the standard \code{'kd'} tree or a |
| 34 |
#' \code{'bd'} (box-decomposition, AMNSW98) tree which may perform better for |
| 35 |
#' larger point sets |
| 36 |
#'@param searchtype See details |
| 37 |
#'@param radius Radius of search for searchtype='radius' |
| 38 |
#'@param eps Error bound: default of 0.0 implies exact nearest neighbour search |
| 39 |
#'@return A \code{list} of length 2 with elements: |
| 40 |
#' |
| 41 |
#' \item{nn.idx}{A \bold{N} x \bold{k} integer \code{matrix} returning the near |
| 42 |
#' neighbour indices.} |
| 43 |
#' |
| 44 |
#' \item{nn.dists}{A \bold{N} x \bold{k} \code{matrix} returning the near |
| 45 |
#' neighbour Euclidean distances.} |
| 46 |
#'@author Gregory Jefferis based on earlier code by Samuel E. Kemp (knnFinder |
| 47 |
#' package) |
| 48 |
#'@references Bentley J. L. (1975), Multidimensional binary search trees used |
| 49 |
#' for associative search. Communication ACM, 18:309-517. |
| 50 |
#' |
| 51 |
#' Arya S. and Mount D. M. (1993), Approximate nearest neighbor searching, |
| 52 |
#' Proc. 4th Ann. ACM-SIAM Symposium on Discrete Algorithms (SODA'93), 271-280. |
| 53 |
#' |
| 54 |
#' Arya S., Mount D. M., Netanyahu N. S., Silverman R. and Wu A. Y (1998), An |
| 55 |
#' optimal algorithm for approximate nearest neighbor searching, Journal of the |
| 56 |
#' ACM, 45, 891-923. |
| 57 |
#'@keywords nonparametric |
| 58 |
#'@examples |
| 59 |
#' |
| 60 |
#'x1 <- runif(100, 0, 2*pi) |
| 61 |
#'x2 <- runif(100, 0,3) |
| 62 |
#'DATA <- data.frame(x1, x2) |
| 63 |
#'nearest <- nn2(DATA,DATA) |
| 64 |
#'@export |
| 65 |
nn2 <- function(data, query=data, k=min(10,nrow(data)),treetype=c("kd","bd"), |
| 66 |
searchtype=c("standard","priority","radius"),radius=0.0,eps=0.0) |
| 67 |
{ |
| 68 |
dimension <- ncol(data) |
| 69 |
if(is.null(dimension)) dimension=1L |
| 70 |
query_dimension <- ncol(query) |
| 71 |
if(is.null(query_dimension)) query_dimension=1L |
| 72 |
|
| 73 |
ND <- nrow(data) |
| 74 |
if(is.null(ND)) ND=length(data) |
| 75 |
NQ <- nrow(query) |
| 76 |
if(is.null(NQ)) NQ=length(query) |
| 77 |
|
| 78 |
# Check that both datasets have same dimensionality |
| 79 |
if(dimension != query_dimension) |
| 80 |
stop("Query and data tables must have same dimensions") |
| 81 |
|
| 82 |
if(k>ND) |
| 83 |
stop("Cannot find more nearest neighbours than there are points") |
| 84 |
|
| 85 |
searchtypeInt=pmatch(searchtype[1],c("standard","priority","radius")) |
| 86 |
if(is.na(searchtypeInt)) stop(paste("Unknown search type",searchtype)) |
| 87 |
treetype=match.arg(treetype,c("kd","bd")) |
| 88 |
|
| 89 |
# Coerce to matrix form |
| 90 |
if(is.data.frame(data)) |
| 91 |
data <- unlist(data,use.names=FALSE) |
| 92 |
|
| 93 |
if(!length(data)) stop("no points in data!") |
| 94 |
|
| 95 |
# Coerce to matrix form |
| 96 |
if(!is.matrix(query)) |
| 97 |
query <- unlist(query,use.names=FALSE) |
| 98 |
|
| 99 |
if(!length(query)) stop("no points in query!") |
| 100 |
|
| 101 |
# void get_NN_2Set(double *data, double *query, int *D, int *ND, int *NQ, int *K, double *EPS, |
| 102 |
# int *nn_index, double *distances) |
| 103 |
|
| 104 |
results <- .C("get_NN_2Set", |
| 105 |
as.double(data), |
| 106 |
as.double(query), |
| 107 |
as.integer(dimension), |
| 108 |
as.integer(ND), |
| 109 |
as.integer(NQ), |
| 110 |
as.integer(k), |
| 111 |
as.double(eps), |
| 112 |
as.integer(searchtypeInt), |
| 113 |
as.integer(treetype=="bd"), |
| 114 |
as.double(radius*radius), |
| 115 |
nn.idx = integer(k*NQ), |
| 116 |
nn = double(k*NQ), PACKAGE="RANN") |
| 117 |
|
| 118 |
# now put the returned vectors into (nq x k) arrays |
| 119 |
nn.indexes=matrix(results$nn.idx,ncol=k,byrow=TRUE) |
| 120 |
nn.dist=matrix(results$nn,ncol=k,byrow=TRUE) |
| 121 |
|
| 122 |
return(list(nn.idx=nn.indexes, nn.dists=nn.dist)) |
| 123 |
} |