| 1 |
context("nn2") |
| 2 |
|
| 3 |
test_that("basic use of nn2", { |
| 4 |
|
| 5 |
res<-structure(list(nn.idx = structure(c(1L, 2L, 2L), |
| 6 |
.Dim = c(3L, 1L)), |
| 7 |
nn.dists = structure(c(0.01, 1, 2), .Dim = c(3L, 1L))), |
| 8 |
.Names = c("nn.idx", "nn.dists")) |
| 9 |
|
| 10 |
res.func<-nn2(rbind(c(1,0),c(2,0)),rbind(c(1.01,0),c(3,0),c(4.0,0)),k=1) |
| 11 |
expect_equal(res,res.func) |
| 12 |
|
| 13 |
a=structure(c(-0.626453810742332, 0.183643324222082, -0.835628612410047, |
| 14 |
1.59528080213779, 0.329507771815361, -0.820468384118015, 0.487429052428485, |
| 15 |
0.738324705129217, 0.575781351653492, -0.305388387156356, 1.51178116845085, |
| 16 |
0.389843236411431, -0.621240580541804, -2.2146998871775, 1.12493091814311, |
| 17 |
-0.0449336090152309, -0.0161902630989461, 0.94383621068530, 0.821221195098089, |
| 18 |
0.593901321217509, 0.918977371608218, 0.782136300731067, 0.0745649833651906, |
| 19 |
-1.98935169586337, 0.61982574789471, -0.0561287395290008, -0.155795506705329, |
| 20 |
-1.47075238389927, -0.47815005510862, 0.417941560199702), .Dim = c(10L, |
| 21 |
3L)) |
| 22 |
|
| 23 |
b=structure(c(1.35867955152904, -0.102787727342996, 0.387671611559369, |
| 24 |
-0.0538050405829051, -1.37705955682861, -0.41499456329968, -0.394289953710349, |
| 25 |
-0.0593133967111857, 1.10002537198388, 0.763175748457544, -0.164523596253587, |
| 26 |
-0.253361680136508, 0.696963375404737, 0.556663198673657, -0.68875569454952, |
| 27 |
-0.70749515696212, 0.36458196213683, 0.768532924515416, -0.112346212150228, |
| 28 |
0.881107726454215, 0.398105880367068, -0.612026393250771, 0.341119691424425, |
| 29 |
-1.12936309608079, 1.43302370170104, 1.98039989850586, -0.367221476466509, |
| 30 |
-1.04413462631653, 0.569719627442413, -0.135054603880824), .Dim = c(10L, |
| 31 |
3L)) |
| 32 |
|
| 33 |
nearest=structure(list( |
| 34 |
nn.idx = structure(c(7L, 7L, 5L, 9L, 3L, 2L, 6L, |
| 35 |
9L, 7L, 9L, 2L, 6L, 2L, 8L, 6L, 3L, 10L, 8L, 2L, 5L, 9L, 3L, |
| 36 |
10L, 7L, 10L, 10L, 7L, 7L, 5L, 7L, 5L, 9L, 9L, 6L, 2L, 6L, 9L, |
| 37 |
6L, 9L, 2L, 10L, 10L, 7L, 10L, 1L, 5L, 3L, 10L, 10L, 10L), |
| 38 |
.Dim = c(10L, 5L)), |
| 39 |
nn.dists = structure(c(1.04301819567468, 0.782785251535478, |
| 40 |
0.514019762075842, 0.943635726058299, 1.46393847125353, 1.73157573316917, |
| 41 |
0.66791447696244, 0.852326186446708, 0.954406845715277, 0.395496580572611, |
| 42 |
1.35481071903339, 0.931412898425681, 0.574844301957827, 0.945472961596115, |
| 43 |
1.71518967014802, 1.95360681279363, 0.822782991518318, 0.921390522381496, |
| 44 |
1.06633538007411, 0.904080753178077, 1.53376883835267, 1.06948509416105, |
| 45 |
0.704879811283639, 1.25256907321914, 1.95552547157996, 2.03639936314308, |
| 46 |
0.983420985126367, 1.30532148465522, 1.45844529983923, 0.938940949941509, |
| 47 |
1.66464626768377, 1.27793083718641, 0.849722423887674, 1.44966319723756, |
| 48 |
2.00569777023645, 2.179643484087, 1.07789743302845, 1.4890400761415, |
| 49 |
1.49813576432115, 1.19098185772077, 1.82885863701822, 1.3489757618116, |
| 50 |
0.874908208472254, 1.56806650285981, 2.38117935086023, 2.40067304459157, |
| 51 |
1.16696237017602, 1.49288843286122, 1.58019302443234, 1.23698083599426), |
| 52 |
.Dim = c(10L, 5L))), |
| 53 |
.Names = c("nn.idx", "nn.dists")) |
| 54 |
|
| 55 |
expect_equal(nn2(a, b, k=5), nearest, tol=1e-6) |
| 56 |
}) |
| 57 |
|
| 58 |
# NB this fails with the version of ANN distributed with knnFinder v1.0 |
| 59 |
test_that("nn2 with identical point", { |
| 60 |
res<-structure(list(nn.idx = structure(c(1L, 2L, 2L), .Dim = c(3L, |
| 61 |
1L)), nn.dists = structure(c(0.00, 1, 2), .Dim = c(3L, 1L))), .Names = c("nn.idx", |
| 62 |
"nn.dists")) |
| 63 |
|
| 64 |
res.func<-nn2(rbind(c(1,0),c(2,0)),rbind(c(1.0,0),c(3,0),c(4.0,0)),k=1) |
| 65 |
expect_equal(res,res.func) |
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
test_that("nn2 with different search / tree types", { |
| 70 |
set.seed(1) |
| 71 |
a=matrix(rnorm(3000),ncol=3) |
| 72 |
b=matrix(rnorm(3000),ncol=3) |
| 73 |
n.standard<-nn2(a,b,k=5,searchtype='standard') |
| 74 |
n.priority<-nn2(a,b,k=5,searchtype='priority') |
| 75 |
n.bd.standard<-nn2(a,b,k=5,searchtype='standard',treetype='bd') |
| 76 |
n.bd.priority<-nn2(a,b,k=5,searchtype='priority',treetype='bd') |
| 77 |
|
| 78 |
expect_equal(n.standard,n.priority) |
| 79 |
expect_equal(n.standard,n.bd.standard) |
| 80 |
expect_equal(n.standard,n.bd.priority) |
| 81 |
}) |
| 82 |
|
| 83 |
test_that("nn2 fixed radius with large radius", { |
| 84 |
set.seed(1) |
| 85 |
a=matrix(rnorm(3000),ncol=3) |
| 86 |
b=matrix(rnorm(3000),ncol=3) |
| 87 |
n.standard<-nn2(a,b,k=5,searchtype='standard') |
| 88 |
n.rad<-nn2(a,b,k=5,searchtype='radius',radius=20.0) |
| 89 |
n.bd.rad<-nn2(a,b,k=5,searchtype='radius',radius=20.0,treetype='bd') |
| 90 |
|
| 91 |
expect_equal(n.standard,n.rad) |
| 92 |
expect_equal(n.standard,n.bd.rad) |
| 93 |
}) |
| 94 |
|
| 95 |
test_that("matrix with 0 columns", { |
| 96 |
d=matrix(ncol=0,nrow=90) |
| 97 |
expect_error(nn2(d)) |
| 98 |
}) |
| 99 |
|
| 100 |
test_that("all NA", { |
| 101 |
data=matrix(rnorm(10), ncol=2) |
| 102 |
query=matrix(rep(NA_real_,10), ncol=2) |
| 103 |
expect_error(nn2(data = data, query = query, k=1)) |
| 104 |
}) |
| 105 |
|
| 106 |
test_that("mixture of matrix and vector inputs", { |
| 107 |
mat=matrix(rnorm(10), ncol=1) |
| 108 |
vec=as.numeric(mat) |
| 109 |
expect_is(res<-nn2(data = mat, query = vec, k=1), 'list') |
| 110 |
expect_equal(nn2(data = vec, query = mat, k=1), res) |
| 111 |
expect_equal(nn2(data = vec, query = vec, k=1), res) |
| 112 |
}) |
| 113 |
|
| 114 |
test_that("vector inputs give outputs of appropriate length", { |
| 115 |
expect_is(res<-nn2(data = rnorm(10), query = rnorm(5), k=1), 'list') |
| 116 |
expect_equal(length(res$nn.idx), 5L) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("inputs with different dimensions", { |
| 120 |
mat=matrix(rnorm(20), ncol=2) |
| 121 |
vec=as.numeric(mat[,1]) |
| 122 |
expect_error(nn2(data = mat, query = vec, k=1), 'same dimensions') |
| 123 |
}) |