-
Notifications
You must be signed in to change notification settings - Fork 45
/
index_test.go
129 lines (110 loc) · 3.92 KB
/
index_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package nlp
import (
"sort"
"testing"
"github.com/james-bowman/nlp/measures/pairwise"
"github.com/james-bowman/sparse"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/mat"
)
func TestIndexerIndex(t *testing.T) {
m := sparse.Random(sparse.DenseFormat, 100, 10, 1.0)
tests := []struct {
index Indexer
}{
{index: NewLinearScanIndex(pairwise.CosineDistance)},
{index: NewLSHIndex(false, NewSimHash(1000, 100), NewClassicLSH(50, 20), pairwise.CosineDistance)},
{index: NewLSHIndex(true, NewSimHash(1000, 100), NewClassicLSH(50, 20), pairwise.HammingDistance)},
{index: NewLSHIndex(false, NewSimHash(1000, 100), NewLSHForest(50, 20), pairwise.CosineDistance)},
}
for ti, test := range tests {
ColDo(m, func(j int, v mat.Vector) {
test.index.Index(v, j)
})
ColDo(m, func(j int, v mat.Vector) {
matches := test.index.Search(v, 1)
if len(matches) != 1 {
t.Errorf("Test %d: Search expected 1 result but received %d", ti+1, len(matches))
}
if matches[0].ID != j {
t.Errorf("Test %d: Search expected to find %d but found %d", ti+1, j, matches[0].ID)
}
if matches[0].Distance < -0.0000001 || matches[0].Distance > 0.0000001 {
t.Errorf("Test %d: Search match distance expected 0.0 but received %f", ti+1, matches[0].Distance)
}
})
}
}
func TestIndexerSearch(t *testing.T) {
numCols := 10
m := sparse.Random(sparse.DenseFormat, 100, numCols, 1.0)
// build similarity matrix
similarityMatrix := make([]float64, numCols*numCols)
inds := make([][]int, numCols)
ColDo(m, func(j int, v1 mat.Vector) {
ColDo(m, func(i int, v2 mat.Vector) {
similarityMatrix[j*numCols+i] = pairwise.CosineDistance(v1, v2)
})
inds[j] = make([]int, numCols)
floats.Argsort(similarityMatrix[j*numCols:(j+1)*numCols], inds[j])
for left, right := 0, len(inds[j])-1; left < right; left, right = left+1, right-1 {
inds[j][left], inds[j][right] = inds[j][right], inds[j][left]
similarityMatrix[j*numCols+left], similarityMatrix[j*numCols+right] = similarityMatrix[j*numCols+right], similarityMatrix[j*numCols+left]
}
})
tests := []struct {
k int
index Indexer
}{
{k: numCols, index: NewLinearScanIndex(pairwise.CosineDistance)},
{k: numCols, index: NewLSHIndex(false, NewSimHash(700, 100), NewClassicLSH(7, 100), pairwise.CosineDistance)},
{k: numCols, index: NewLSHIndex(false, NewSimHash(1000, 100), NewLSHForest(50, 20), pairwise.CosineDistance)},
}
for ti, test := range tests {
ColDo(m, func(j int, v mat.Vector) {
test.index.Index(v, j)
})
ColDo(m, func(j int, v mat.Vector) {
matches := test.index.Search(v, test.k)
if len(matches) != test.k {
t.Errorf("Test %d: Search expected %d result but received %d", ti+1, test.k, len(matches))
}
heap := resultHeap{matches: matches}
sort.Sort(heap)
for i, match := range matches {
if match.ID != inds[j][i] {
t.Errorf("Test %d: For col #%d, Rank #%d - expected %v but found %v", ti+1, j, i, inds[j], matches)
return
}
}
})
}
}
func TestIndexerRemove(t *testing.T) {
m := sparse.Random(sparse.DenseFormat, 100, 10, 1.0)
tests := []struct {
index Indexer
}{
{index: NewLinearScanIndex(pairwise.CosineDistance)},
{index: NewLSHIndex(false, NewSimHash(1000, 100), NewClassicLSH(50, 20), pairwise.CosineDistance)},
{index: NewLSHIndex(true, NewSimHash(1000, 100), NewClassicLSH(50, 20), pairwise.HammingDistance)},
{index: NewLSHIndex(false, NewSimHash(1000, 100), NewLSHForest(50, 20), pairwise.CosineDistance)},
}
for ti, test := range tests {
ColDo(m, func(j int, v mat.Vector) {
test.index.Index(v, j)
})
ColDo(m, func(j int, v mat.Vector) {
test.index.Remove(j)
matches := test.index.Search(v, 1)
if len(matches) > 1 {
t.Errorf("Test %d: Search expected less than 1 result but received %d", ti+1, len(matches))
}
if len(matches) == 1 {
if matches[0].ID == j {
t.Errorf("Test %d: Search expected not to find %d but found %d", ti+1, j, matches[0].ID)
}
}
})
}
}