forked from ryanbressler/CloudForest
/
featurematrix_test.go
148 lines (122 loc) · 4.53 KB
/
featurematrix_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
package CloudForest
import (
"bytes"
"strings"
"testing"
"github.com/bmizerany/assert"
"gonum.org/v1/gonum/mat"
)
//A toy feature matrix where either of the first
//two variables should be easilly predictible
//by the other by a single greedy tree.
var constantsfm = `. 0 1 2 3 4 5 6 7
C:CatTarget 0 0 0 0 0 1 1 1
N:GoodVals 0 0 0 0 0 1 1 1
C:Const1 0 0 0 0 0 0 0 1
C:Const2 0 0 0 0 0 0 0 1
C:Const3 0 0 0 0 0 0 0 1
N:Const4 0 0 0 0 0 0 0 1
N:Const5 0 0 0 0 0 0 0 1
N:Const6 0 0 0 0 0 0 0 1`
func TestBestSplitter(t *testing.T) {
fm := readFm()
target := fm.Data[0]
cases := &[]int{0, 1, 2, 3, 4, 5, 6}
candidates := []int{1, 2, 3, 4, 5, 6, 7}
allocs := NewBestSplitAllocs(len(*cases), target)
_, imp, constant := fm.Data[1].BestSplit(target, cases, 1, 1, false, allocs)
if imp <= minImp || constant == true {
t.Errorf("Good feature had imp %v and constant: %v", imp, constant)
}
_, imp, constant = fm.Data[2].BestSplit(target, cases, 1, 1, false, allocs)
if imp > minImp || constant == false {
t.Errorf("Constant cat feature had imp %v and constant: %v %v", imp, constant, fm.Data[2].(*DenseCatFeature).CatData)
}
_, imp, constant = fm.Data[7].BestSplit(target, cases, 1, 1, false, allocs)
if imp > minImp || constant == false {
t.Errorf("Constant num feature had imp %v and constant: %v", imp, constant)
}
fi, split, impDec, nconstants := fm.BestSplitter(target, cases, &candidates, len(candidates), nil, 1, true, false, false, false, allocs, 0)
if fi != 1 || split == nil || impDec == minImp || nconstants != 6 {
t.Errorf("BestSplitter couldn't find non constant feature and six constants fi: %v split: %v impDex: %v nconstants: %v ", fi, split, impDec, nconstants)
}
for i := 0; i < 7; i++ {
candidates = []int{1, 2, 3, 4, 5, 6, 7}
fi, split, impDec, nconstants = fm.BestSplitter(target, cases, &candidates, 1, nil, 1, true, false, false, false, allocs, i)
if fi != 1 || split == nil || impDec == minImp {
t.Errorf("BestSplitter couldn't find non constant feature with mTry=1 and %v known constants fi: %v split: %v impDex: %v nconstants: %v ", i, fi, split, impDec, nconstants)
}
candidates = []int{1, 2, 3, 4, 5, 6, 7}
fi, split, impDec, nconstants = fm.BestSplitter(target, cases, &candidates, len(candidates), nil, 1, true, false, false, false, allocs, i)
if fi != 1 || split == nil || impDec == minImp || nconstants != 6 {
t.Errorf("BestSplitter couldn't find non constant feature and six constants with %v known constants fi: %v split: %v impDex: %v nconstants: %v ", i, fi, split, impDec, nconstants)
}
}
}
func TestFmWrite(t *testing.T) {
fm := readFm()
header := true
writer := &bytes.Buffer{}
if err := fm.WriteFM(writer, "\t", header, true); err != nil {
t.Fatalf("could not write feature matrix: %v", err)
}
if writer.String() == "" {
t.Fatalf("could not write FM - buffer is empty")
}
firstLen := writer.Len()
writer = &bytes.Buffer{}
if err := fm.WriteFM(writer, "\t", header, false); err != nil {
t.Fatalf("could not write feature matrix: %v", err)
}
if writer.String() == "" {
t.Fatalf("could not write FM - buffer is empty")
}
secondLen := writer.Len()
if firstLen != secondLen {
t.Fatalf("expected buffers to have the same length: %v != %v", firstLen, secondLen)
}
}
func TestMat64(t *testing.T) {
fm := readFm()
dense := fm.Matrix(false, false)
compareCol := func(i int, exp []float64) {
col := mat.Col(nil, i, dense)
assert.Equal(t, len(col), len(exp))
for i := range exp {
assert.Equal(t, col[i], exp[i])
}
}
compareCol(1, []float64{0, 0, 0, 0, 0, 1, 1, 1})
compareCol(2, []float64{0, 0, 0, 0, 0, 0, 0, 1})
}
func readFm() *FeatureMatrix {
fmReader := strings.NewReader(constantsfm)
return ParseAFM(fmReader)
}
func TestFeatureMatrixCopy(t *testing.T) {
fm := readFm()
fmCopy := fm.Copy()
// check feature matrix copy
for k, v := range fm.Map {
assert.Equal(t, v, fmCopy.Map[k])
}
for i, v := range fm.CaseLabels {
assert.Equal(t, v, fmCopy.CaseLabels[i])
}
assert.Equal(t, len(fm.Data), len(fmCopy.Data))
for i, feature := range fm.Data {
rows := fm.Data[1].Length()
featureCopy := fmCopy.Data[i]
assert.Equal(t, rows, featureCopy.Length())
for r := 0; r < rows; r++ {
assert.Equal(t, feature.GetStr(r), featureCopy.GetStr(r))
}
}
// alter original feature matrix
fm.CaseLabels = fm.CaseLabels[0 : len(fm.CaseLabels)-1]
delete(fm.Map, "C:Const1")
fm.Data = fm.Data[0 : len(fm.CaseLabels)-1]
assert.NotEqual(t, len(fm.Data), len(fmCopy.Data))
assert.NotEqual(t, len(fm.Map), len(fmCopy.Map))
assert.NotEqual(t, len(fm.CaseLabels), len(fmCopy.CaseLabels))
}