Skip to content

Commit 335a875

Browse files
committed
Add some tests
1 parent 5937cba commit 335a875

File tree

3 files changed

+96
-36
lines changed

3 files changed

+96
-36
lines changed

tests/lib/model/c2p.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ describe('clustering', () => {
4444
const ri = randIndex(y, t)
4545
expect(ri).toBeGreaterThan(0.9)
4646
})
47+
48+
test('near distance clusters', () => {
49+
const model = new C2P(10, 50)
50+
const x = [
51+
[0, 0],
52+
[1, 1],
53+
[0, 1],
54+
]
55+
56+
model.fit(x)
57+
const y = model.predict(3)
58+
expect(y).toHaveLength(x.length)
59+
60+
const t = [0, 1, 2]
61+
const ri = randIndex(y, t)
62+
expect(ri).toBeGreaterThan(0.9)
63+
})
4764
})
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import Matrix from '../../../lib/util/matrix.js'
22
import GeneralizedESD from '../../../lib/model/generalized_esd.js'
33

4-
test.each([1, 2, 3])('anomaly detection %d', k => {
5-
const model = new GeneralizedESD(1, k)
6-
const x = Matrix.random(100, 2, 0, 0.2).toArray()
7-
x.push([10, 10])
8-
const y = model.predict(x, 0.1)
9-
let c = 0
10-
for (let i = 0; i < y.length - 1; i++) {
11-
if (c < k - 1) {
12-
if (y[i]) {
13-
c++
4+
describe('anomaly detection', () => {
5+
test.each([1, 2, 3])('r: %d', k => {
6+
const model = new GeneralizedESD(1, k)
7+
const x = Matrix.random(100, 2, 0, 0.2).toArray()
8+
x.push([10, 10])
9+
const y = model.predict(x, 0.1)
10+
let c = 0
11+
for (let i = 0; i < y.length - 1; i++) {
12+
if (c < k - 1) {
13+
if (y[i]) {
14+
c++
15+
}
16+
continue
1417
}
15-
continue
18+
expect(y[i]).toBe(false)
1619
}
17-
expect(y[i]).toBe(false)
18-
}
19-
expect(y[y.length - 1]).toBe(true)
20+
expect(y[y.length - 1]).toBe(true)
21+
})
22+
23+
test('oidx correction', () => {
24+
const model = new GeneralizedESD(1, 3)
25+
const x = [
26+
[0.06, 0.13],
27+
[0.01, 0.06],
28+
[0.1, 0],
29+
[0.07, 0.01],
30+
[0.12, 0.15],
31+
[10, 10],
32+
]
33+
const y = model.predict(x, 0.1)
34+
expect(y[5]).toBe(true)
35+
})
2036
})

tests/lib/model/vbgmm.test.js

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,55 @@ import VBGMM from '../../../lib/model/vbgmm.js'
66

77
import { randIndex } from '../../../lib/evaluate/clustering.js'
88

9-
test('clustering', () => {
10-
const model = new VBGMM(0.001, 0.001, 5)
11-
const n = 20
12-
const x = Matrix.concat(Matrix.randn(n, 2, 0, 0.2), Matrix.randn(n, 2, 5, 0.2)).toArray()
9+
describe('clustering', () => {
10+
test('many data', () => {
11+
const model = new VBGMM(0.001, 0.001, 5)
12+
const n = 20
13+
const x = Matrix.concat(Matrix.randn(n, 2, 0, 0.2), Matrix.randn(n, 2, 5, 0.2)).toArray()
1314

14-
model.init(x)
15-
for (let i = 0; i < 2; i++) {
15+
model.init(x)
16+
for (let i = 0; i < 2; i++) {
17+
model.fit()
18+
}
19+
expect(model.means.sizes).toEqual([5, 2])
20+
expect(model.covs).toHaveLength(5)
21+
for (let i = 0; i < model.covs.length; i++) {
22+
expect(model.covs[i].sizes).toEqual([2, 2])
23+
}
24+
expect(model.effectivity).toHaveLength(5)
25+
const y = model.predict(x)
26+
expect(y).toHaveLength(x.length)
27+
28+
const t = []
29+
for (let i = 0; i < x.length; i++) {
30+
t[i] = Math.floor(i / n)
31+
}
32+
const ri = randIndex(y, t)
33+
expect(ri).toBeGreaterThan(0.9)
34+
})
35+
36+
test('few data', () => {
37+
const model = new VBGMM(0.001, 0.001, 1)
38+
const x = [
39+
[0, 0],
40+
[1, 1],
41+
]
42+
43+
model.init(x)
44+
model.fit()
45+
46+
const y = model.predict(x)
47+
expect(y).toEqual([0, 0])
48+
})
49+
50+
test('single data', () => {
51+
const model = new VBGMM(0.001, 0.001, 1)
52+
const x = [[0, 0]]
53+
54+
model.init(x)
1655
model.fit()
17-
}
18-
expect(model.means.sizes).toEqual([5, 2])
19-
expect(model.covs).toHaveLength(5)
20-
for (let i = 0; i < model.covs.length; i++) {
21-
expect(model.covs[i].sizes).toEqual([2, 2])
22-
}
23-
expect(model.effectivity).toHaveLength(5)
24-
const y = model.predict(x)
25-
expect(y).toHaveLength(x.length)
26-
27-
const t = []
28-
for (let i = 0; i < x.length; i++) {
29-
t[i] = Math.floor(i / n)
30-
}
31-
const ri = randIndex(y, t)
32-
expect(ri).toBeGreaterThan(0.9)
56+
57+
const y = model.predict(x)
58+
expect(y).toEqual([0])
59+
})
3360
})

0 commit comments

Comments
 (0)