-
Notifications
You must be signed in to change notification settings - Fork 25
/
popcode_test.go
225 lines (183 loc) · 9.49 KB
/
popcode_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package popcode
import (
"testing"
"cogentcore.org/core/math32"
"cogentcore.org/core/tensor"
)
// difTol is the numerical difference tolerance for comparing vs. target values
const difTol = float32(1.0e-6)
const difTolWeak = float32(1.0e-4)
const difTolMulti = float32(1.0e-2)
func CmprFloats(out, cor []float32, msg string, t *testing.T) {
for i := range out {
dif := math32.Abs(out[i] - cor[i])
if dif > difTol { // allow for small numerical diffs
t.Errorf("%v err: out: %v, cor: %v, dif: %v\n", msg, out[i], cor[i], dif)
}
}
}
func TestPopCode1D(t *testing.T) {
pc := OneD{}
pc.Defaults()
var vals []float32
pc.Values(&vals, 11)
// fmt.Printf("vals: %v\n", vals)
corValues := []float32{-0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5}
CmprFloats(vals, corValues, "vals for 11 units", t)
var pat []float32
pc.Encode(&pat, 0.5, 11, Set)
// fmt.Printf("pat for 0.5: %v\n", pat)
corPat := []float32{0.0019304542, 0.018315637, 0.10539923, 0.3678795, 0.7788008, 1, 0.77880067, 0.3678795, 0.10539923, 0.01831562, 0.0019304542}
CmprFloats(pat, corPat, "pattern for 0.5 over 11 units", t)
val := pc.Decode(pat)
//fmt.Printf("decode pat for 0.5: %v\n", val)
if math32.Abs(val-0.5) > difTol {
t.Errorf("did not decode properly: val: %v != 0.5", val)
}
}
func TestPopCode1DMulti(t *testing.T) {
pc := OneD{}
pc.Defaults()
var pat []float32
// note: usually you'd use a larger pattern size for multiple values
pc.Encode(&pat, 0.1, 11, Set)
pc.Encode(&pat, 0.9, 11, Add)
// fmt.Printf("pat for 0.1, 0.9: %v\n", pat)
corPat := []float32{0.10540401, 0.36800286, 0.78073126, 1.0183157, 0.8842, 0.73575896, 0.8842002, 1.0183157, 0.78073114, 0.36800268, 0.10540401}
CmprFloats(pat, corPat, "pattern for 0.25, 0.75 over 11 units", t)
vals := pc.DecodeNPeaks(pat, 2, 1)
// fmt.Printf("decode pat for 0.25, 0.75: %v\n", vals)
for _, val := range vals {
if val > 0.5 {
if math32.Abs(val-0.9) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
} else {
if math32.Abs(val-0.1) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.1", val)
}
}
}
}
func TestPopCode2D(t *testing.T) {
pc := TwoD{}
pc.Defaults()
var valsX, valsY []float32
pc.Values(&valsX, &valsY, 11, 11)
// fmt.Printf("vals: %v\n", valsX)
corValues := []float32{-0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5}
CmprFloats(valsX, corValues, "valsX for 11 units", t)
CmprFloats(valsY, corValues, "valsY for 11 units", t)
var pat tensor.Float32
pat.SetShape([]int{11, 11})
pc.Encode(&pat, math32.Vec2(0.3, 0.9), Set)
// fmt.Printf("pat for 0.5: %v\n", pat)
corPat := []float32{8.7642576e-08, 5.0434767e-07, 1.7603463e-06, 3.7266532e-06, 4.7851167e-06, 3.7266532e-06, 1.7603463e-06, 5.0434767e-07, 8.7642576e-08, 9.237448e-09, 5.905302e-10, 2.2603292e-06, 1.3007299e-05, 4.5399953e-05, 9.611166e-05, 0.0001234098, 9.611166e-05, 4.5399953e-05, 1.3007299e-05, 2.2603292e-06, 2.3823696e-07, 1.5229979e-08, 3.53575e-05, 0.00020346837, 0.0007101748, 0.0015034394, 0.0019304542, 0.0015034394, 0.0007101748, 0.00020346837, 3.53575e-05, 3.7266532e-06, 2.3823696e-07, 0.00033546257, 0.0019304551, 0.0067379503, 0.014264241, 0.018315647, 0.014264241, 0.006737947, 0.001930456, 0.00033546257, 3.53575e-05, 2.2603292e-06, 0.0019304542, 0.011109002, 0.038774215, 0.08208501, 0.10539925, 0.08208501, 0.038774207, 0.011109007, 0.0019304542, 0.00020346837, 1.3007299e-05, 0.006737947, 0.038774207, 0.1353353, 0.28650483, 0.3678795, 0.28650483, 0.13533528, 0.038774226, 0.006737947, 0.0007101748, 4.5399953e-05, 0.014264233, 0.08208501, 0.28650486, 0.6065308, 0.77880096, 0.6065308, 0.2865048, 0.08208503, 0.014264233, 0.0015034394, 9.611166e-05, 0.018315637, 0.10539923, 0.36787945, 0.7788008, 1, 0.7788008, 0.3678794, 0.10539925, 0.018315637, 0.0019304542, 0.0001234098, 0.014264233, 0.08208499, 0.28650478, 0.6065306, 0.77880067, 0.6065306, 0.2865047, 0.08208499, 0.014264233, 0.0015034394, 9.611166e-05, 0.0067379437, 0.03877419, 0.13533522, 0.28650466, 0.36787927, 0.28650466, 0.13533519, 0.038774196, 0.0067379437, 0.0007101744, 4.5399953e-05, 0.0019304542, 0.011109002, 0.038774207, 0.08208499, 0.10539923, 0.08208499, 0.038774196, 0.011109002, 0.0019304542, 0.00020346837, 1.3007299e-05}
CmprFloats(pat.Values, corPat, "pattern for 0.3, 0.9 over 11x11 units", t)
val, err := pc.Decode(&pat)
// fmt.Printf("decode pat for 0.5: %v\n", val)
if err != nil {
t.Error(err)
}
if math32.Abs(val.X-0.3) > difTol {
t.Errorf("did not decode properly: val: %v != 0.3", val)
}
if math32.Abs(val.Y-0.9) > difTol {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
}
func TestPopCode2DMulti(t *testing.T) {
pc := TwoD{}
pc.Defaults()
var pat tensor.Float32
// note: usually you'd use a larger pattern size for multiple values
pat.SetShape([]int{11, 11})
pc.Encode(&pat, math32.Vec2(0.1, 0.9), Set)
pc.Encode(&pat, math32.Vec2(0.9, 0.1), Add)
// fmt.Printf("pat for 0.1, 0.9: %v\n", pat)
corPat := []float32{1.0086953e-06, 1.4767645e-05, 0.00020719503, 0.0019352402, 0.011112728, 0.03877597, 0.08208552, 0.10539932, 0.082085, 0.03877419, 0.011109002, 1.4767645e-05, 9.0799906e-05, 0.00080628647, 0.0068613603, 0.038870327, 0.1353807, 0.28651786, 0.36788172, 0.286505, 0.13533524, 0.038774207, 0.00020719503, 0.00080628647, 0.0030068788, 0.016194696, 0.08358845, 0.28721502, 0.6067343, 0.77883613, 0.60653436, 0.2865049, 0.082085, 0.0019352402, 0.0068613603, 0.016194696, 0.036631294, 0.11966349, 0.37461746, 0.78073144, 1.0003355, 0.778836, 0.36788154, 0.10539932, 0.011112728, 0.038870327, 0.08358845, 0.11966349, 0.16417003, 0.32527903, 0.6176397, 0.7807312, 0.60673404, 0.28651765, 0.0820855, 0.03877597, 0.1353807, 0.28721502, 0.37461746, 0.32527903, 0.2706706, 0.32527906, 0.3746174, 0.28721496, 0.13538063, 0.03877597, 0.08208552, 0.28651786, 0.6067343, 0.78073144, 0.6176397, 0.32527906, 0.16417003, 0.11966347, 0.08358843, 0.0388703, 0.011112728, 0.10539932, 0.36788172, 0.77883613, 1.0003355, 0.7807312, 0.3746174, 0.11966347, 0.036631294, 0.016194696, 0.006861357, 0.0019352402, 0.082085, 0.286505, 0.60653436, 0.778836, 0.60673404, 0.28721496, 0.08358843, 0.016194696, 0.0030068788, 0.00080628606, 0.00020719503, 0.03877419, 0.13533524, 0.2865049, 0.36788154, 0.28651765, 0.13538063, 0.0388703, 0.006861357, 0.00080628606, 9.0799906e-05, 1.4767645e-05, 0.011109002, 0.038774207, 0.082085, 0.10539932, 0.0820855, 0.03877597, 0.011112728, 0.0019352402, 0.00020719503, 1.4767645e-05, 1.0086953e-06}
CmprFloats(pat.Values, corPat, "pattern for 0.1, 0.9; 0.9; 0.1 over 11x11 units", t)
vals, err := pc.DecodeNPeaks(&pat, 2, 1)
if err != nil {
t.Error(err)
}
// fmt.Printf("decode pat for 0.1, 0.9; 0.9, 0.1: %v\n", vals)
for _, valv := range vals {
for d := 0; d < 2; d++ {
val := valv.Dim(math32.Dims(d))
if val > 0.5 {
if math32.Abs(val-0.9) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
} else {
if math32.Abs(val-0.1) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.1", val)
}
}
}
}
}
func TestRing(t *testing.T) {
pc := Ring{}
pc.Defaults()
pc.Min = 0
pc.Max = 360
pc.Sigma = .15 // a bit tighter
var vals []float32
pc.Values(&vals, 25)
// fmt.Printf("vals: %v\n", vals)
corValues := []float32{0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360}
CmprFloats(vals, corValues, "vals for 25 units", t)
var pat []float32
tvals := []float32{180, 330, 359, 300, 90, 30, 1}
for _, vl := range tvals {
pc.Encode(&pat, vl, 25)
// fmt.Printf("pat for 180: %v\n", pat)
val := pc.Decode(pat)
// fmt.Printf("decode pat for 180: %v\n", val)
if math32.Abs(val-vl) > 4 { // very bad tolerances
t.Errorf("did not decode properly: val: %v != %v", val, vl)
}
}
pc.Encode(&pat, 180, 25)
// fmt.Printf("pat for 180: %v\n", pat)
corPat := []float32{1.4945374e-05, 8.815463e-05, 0.00044561853, 0.001930456, 0.007166979, 0.022802992, 0.06217656, 0.1452917, 0.2909605, 0.49935186, 0.7344437, 0.92574126, 1, 0.92574126, 0.7344437, 0.49935186, 0.2909605, 0.1452917, 0.06217656, 0.022802992, 0.007166979, 0.001930456, 0.00044561853, 8.815463e-05, 1.4945374e-05}
CmprFloats(pat, corPat, "pattern for 180 over 25 units", t)
///////// 330
pc.Encode(&pat, 330, 25)
// fmt.Printf("pat for 330: %v\n", pat)
corPat = []float32{0.7344437, 0.49935186, 0.2909605, 0.1452917, 0.06217656, 0.022802992, 0.0071669817, 0.0019304849, 0.0004458889, 9.0326066e-05, 2.9890747e-05, 9.0326066e-05, 0.0004458889, 0.0019304849, 0.0071669817, 0.022802992, 0.06217656, 0.1452917, 0.2909605, 0.49935186, 0.7344437, 0.92574126, 1, 0.92574126, 0.7344437}
CmprFloats(pat, corPat, "pattern for 330 over 25 units", t)
}
func TestTwoDWrap(t *testing.T) {
pc := TwoD{}
pc.Defaults()
pc.Min.X = -180
pc.Max.X = 180
pc.WrapX = true
pc.Sigma.Set(0.15, 0.15)
pc.Clip = false
var pat tensor.Float32
pat.SetShape([]int{21, 21})
tangs := []float32{-179, -90, 0, 90, 179}
for _, ang := range tangs {
v := math32.Vec2(ang, .5)
pc.Encode(&pat, v, Set)
// fmt.Printf("ang: %g\n", ang)
// for i := 0; i < 21; i++ {
// fmt.Printf(" %g", pat.Value([]int{10, i}))
// }
// fmt.Printf("\n")
val, err := pc.Decode(&pat)
// fmt.Printf("decode: %v\n", val)
if err != nil {
t.Error(err)
}
if math32.Abs(val.X-ang) > 2 && !(val.X >= 179 && ang <= -179) { // very bad tolerances
t.Errorf("did not decode properly: val: %v != %v", val.X, ang)
}
}
}