-
Notifications
You must be signed in to change notification settings - Fork 2
/
annotator.go
73 lines (65 loc) · 1.48 KB
/
annotator.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
package treelite
// #cgo LDFLAGS: -ltreelite -ltreelite_runtime
// #include <stdlib.h>
// #include "treelite/c_api.h"
// #include "treelite/c_api_common.h"
// #include "treelite/c_api_runtime.h"
import "C"
import "errors"
var (
errCreateAnnotator = errors.New("create annotator failed")
errSaveAnnotator = errors.New("save annotator failed")
errFreeAnnotator = errors.New("free annotator failed")
)
// Annotator annotate branches in decision trees
// see https://treelite.readthedocs.io/en/latest/tutorials/optimize.html#annotate-conditional-branches
type Annotator struct {
pointer C.AnnotationHandle
}
// NewAnnotator creates an annotator of the given model and DMatrix
func NewAnnotator(
model *Model,
dmatrix *DMatrix,
numThread int,
verbose bool,
) (*Annotator, error) {
var handle C.AnnotationHandle
var verboseInt int32
if verbose {
verboseInt = 1
}
ret := C.TreeliteAnnotateBranch(
model.pointer,
dmatrix.pointer,
C.int(numThread),
C.int(verboseInt),
&handle,
)
if ret == -1 {
return nil, errCreateAnnotator
}
return &Annotator{
pointer: handle,
}, nil
}
// Save saves annotation result to the given path
func (a Annotator) Save(path string) error {
ret := C.TreeliteAnnotationSave(
a.pointer,
C.CString(path),
)
if ret == -1 {
return errSaveAnnotator
}
return nil
}
// Close frees internally allocated memory
func (a Annotator) Close() error {
ret := C.TreeliteAnnotationFree(
a.pointer,
)
if ret == -1 {
return errFreeAnnotator
}
return nil
}