Skip to content

Commit 17c644e

Browse files
committed
[!] change uint32 -> int in API signatures
1 parent 7e8b4dc commit 17c644e

File tree

10 files changed

+61
-61
lines changed

10 files changed

+61
-61
lines changed

leaves.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const BatchSize = 16
55

66
// Ensemble is common interface that every model in leaves should implement
77
type Ensemble interface {
8-
PredictDense(vals []float64, nrows uint32, ncols uint32, predictions []float64, nTrees int, nThreads int) error
9-
PredictCSR(indptr []uint32, cols []uint32, vals []float64, predictions []float64, nTrees int, nThreads int)
8+
PredictDense(vals []float64, nrows int, ncols int, predictions []float64, nTrees int, nThreads int) error
9+
PredictCSR(indptr []int, cols []int, vals []float64, predictions []float64, nTrees int, nThreads int)
1010
Predict(fvals []float64, nTrees int) float64
1111
}

leaves_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func InnerTestHiggs(t *testing.T, model Ensemble, nThreads int, dense bool, true
111111
bufReader := bufio.NewReader(reader)
112112
var denseMat DenseMat
113113
var csrMat CSRMat
114-
var nRows uint32
114+
var nRows int
115115
if dense {
116116
denseMat, err = DenseMatFromLibsvm(bufReader, 0, true)
117117
if err != nil {
@@ -312,7 +312,7 @@ func InnerBenchmarkHiggs(b *testing.B, model Ensemble, nThreads int, dense bool)
312312
bufReader := bufio.NewReader(reader)
313313
var denseMat DenseMat
314314
var csrMat CSRMat
315-
var nRows uint32
315+
var nRows int
316316
if dense {
317317
denseMat, err = DenseMatFromLibsvm(bufReader, 0, true)
318318
if err != nil {

lgensemble.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// LGEnsemble is LightGBM model (ensemble of trees)
1111
type LGEnsemble struct {
1212
Trees []lgTree
13-
MaxFeatureIdx uint32
13+
MaxFeatureIdx int
1414
}
1515

1616
// NTrees returns number of trees in ensemble
@@ -23,7 +23,7 @@ func (e *LGEnsemble) NTrees() int {
2323
// return 0.0. Note, that result is a raw score (before sigmoid function
2424
// transformation and etc)
2525
func (e *LGEnsemble) Predict(fvals []float64, nTrees int) float64 {
26-
if e.MaxFeatureIdx+1 > uint32(len(fvals)) {
26+
if e.MaxFeatureIdx+1 > len(fvals) {
2727
return 0.0
2828
}
2929
ret := 0.0
@@ -45,7 +45,7 @@ func (e *LGEnsemble) Predict(fvals []float64, nTrees int) float64 {
4545
// threads that will be utilized (maximum is GO_MAX_PROCS)
4646
// Note, that result is a raw score (before sigmoid function transformation and etc).
4747
// Note, `predictions` slice should be properly allocated on call side
48-
func (e *LGEnsemble) PredictCSR(indptr []uint32, cols []uint32, vals []float64, predictions []float64, nTrees int, nThreads int) {
48+
func (e *LGEnsemble) PredictCSR(indptr []int, cols []int, vals []float64, predictions []float64, nTrees int, nThreads int) {
4949
nRows := len(indptr) - 1
5050
if nRows <= BatchSize || nThreads == 0 || nThreads == 1 {
5151
fvals := make([]float64, e.MaxFeatureIdx+1)
@@ -89,18 +89,18 @@ func (e *LGEnsemble) PredictCSR(indptr []uint32, cols []uint32, vals []float64,
8989
wg.Wait()
9090
}
9191

92-
func (e *LGEnsemble) predictCSRInner(indptr []uint32, cols []uint32, vals []float64, startIndex int, endIndex int, predictions []float64, nTrees int, fvals []float64) {
92+
func (e *LGEnsemble) predictCSRInner(indptr []int, cols []int, vals []float64, startIndex int, endIndex int, predictions []float64, nTrees int, fvals []float64) {
9393
for i := startIndex; i < endIndex; i++ {
9494
start := indptr[i]
9595
end := indptr[i+1]
9696
for j := start; j < end; j++ {
97-
if cols[j] < uint32(len(fvals)) {
97+
if cols[j] < len(fvals) {
9898
fvals[cols[j]] = vals[j]
9999
}
100100
}
101101
predictions[i] = e.Predict(fvals, nTrees)
102102
for j := start; j < end; j++ {
103-
if cols[j] < uint32(len(fvals)) {
103+
if cols[j] < len(fvals) {
104104
fvals[cols[j]] = 0.0
105105
}
106106
}
@@ -113,8 +113,8 @@ func (e *LGEnsemble) predictCSRInner(indptr []uint32, cols []uint32, vals []floa
113113
// threads that will be utilized (maximum is GO_MAX_PROCS)
114114
// Note, that result is a raw score (before sigmoid function transformation and etc).
115115
// Note, `predictions` slice should be properly allocated on call side
116-
func (e *LGEnsemble) PredictDense(vals []float64, nrows uint32, ncols uint32, predictions []float64, nTrees int, nThreads int) error {
117-
nRows := int(nrows)
116+
func (e *LGEnsemble) PredictDense(vals []float64, nrows int, ncols int, predictions []float64, nTrees int, nThreads int) error {
117+
nRows := nrows
118118
if ncols == 0 || e.MaxFeatureIdx > ncols-1 {
119119
return fmt.Errorf("incorrect number of columns")
120120
}

lgensemble_io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func LGEnsembleFromReader(reader *bufio.Reader) (*LGEnsemble, error) {
226226
if err != nil {
227227
return nil, err
228228
}
229-
e.MaxFeatureIdx = uint32(maxFeatureIdx)
229+
e.MaxFeatureIdx = maxFeatureIdx
230230

231231
treeSizesStr, isFound := params["tree_sizes"]
232232
if !isFound {

lgensemble_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ func TestLGEnsemble(t *testing.T) {
168168
math.NaN(), math.NaN(),
169169
}
170170

171-
denseRows := uint32(7)
172-
denseCols := uint32(2)
171+
denseRows := 7
172+
denseCols := 2
173173

174174
// check predictions
175175
predictions := make([]float64, denseRows)

mat.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
// DenseMat is dense matrix data structure
88
type DenseMat struct {
99
Values []float64
10-
Cols uint32
11-
Rows uint32
10+
Cols int
11+
Rows int
1212
}
1313

1414
// DenseMatFromArray converts arrays of `values` to DenseMat using shape
1515
// information `rows` and `cols`
16-
func DenseMatFromArray(values []float64, rows uint32, cols uint32) (DenseMat, error) {
16+
func DenseMatFromArray(values []float64, rows int, cols int) (DenseMat, error) {
1717
mat := DenseMat{}
18-
if uint32(len(values)) != cols*rows {
18+
if len(values) != cols*rows {
1919
return mat, fmt.Errorf("wrong dimensions")
2020
}
2121
mat.Values = append(mat.Values, values...)
@@ -26,36 +26,36 @@ func DenseMatFromArray(values []float64, rows uint32, cols uint32) (DenseMat, er
2626

2727
// CSRMat is Compressed Sparse Row matrix data structure
2828
type CSRMat struct {
29-
RowHeaders []uint32
30-
ColIndexes []uint32
29+
RowHeaders []int
30+
ColIndexes []int
3131
Values []float64
3232
}
3333

3434
// Rows returns number of rows in the matrix
35-
func (m *CSRMat) Rows() uint32 {
35+
func (m *CSRMat) Rows() int {
3636
if len(m.RowHeaders) == 0 {
3737
return 0
3838
}
39-
return uint32(len(m.RowHeaders)) - 1
39+
return len(m.RowHeaders) - 1
4040
}
4141

4242
// CSRMatFromArray converts arrays of `values` to CSRMat using shape information
4343
// `rows` and `cols`. See also DenseMatFromArray to store dense data in matrix
44-
func CSRMatFromArray(values []float64, rows uint32, cols uint32) (CSRMat, error) {
44+
func CSRMatFromArray(values []float64, rows int, cols int) (CSRMat, error) {
4545
mat := CSRMat{}
46-
if uint32(len(values)) != cols*rows {
46+
if len(values) != cols*rows {
4747
return mat, fmt.Errorf("wrong dimensions")
4848
}
4949
mat.Values = append(mat.Values, values...)
50-
mat.ColIndexes = make([]uint32, 0, len(values))
51-
mat.RowHeaders = make([]uint32, 0, rows+1)
50+
mat.ColIndexes = make([]int, 0, len(values))
51+
mat.RowHeaders = make([]int, 0, rows+1)
5252

53-
for i := uint32(0); i < rows; i++ {
54-
mat.RowHeaders = append(mat.RowHeaders, uint32(len(mat.ColIndexes)))
55-
for j := uint32(0); j < cols; j++ {
53+
for i := 0; i < rows; i++ {
54+
mat.RowHeaders = append(mat.RowHeaders, len(mat.ColIndexes))
55+
for j := 0; j < cols; j++ {
5656
mat.ColIndexes = append(mat.ColIndexes, j)
5757
}
5858
}
59-
mat.RowHeaders = append(mat.RowHeaders, uint32(len(mat.ColIndexes)))
59+
mat.RowHeaders = append(mat.RowHeaders, len(mat.ColIndexes))
6060
return mat, nil
6161
}

mat_io.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
// DenseMatFromLibsvm reads dense matrix from libsvm format from `reader`
1212
// stream. If `limit` > 0, reads only first limit `rows`. First colums is label,
1313
// and usually you should set `skipFirstColumn` = true
14-
func DenseMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool) (DenseMat, error) {
14+
func DenseMatFromLibsvm(reader *bufio.Reader, limit int, skipFirstColumn bool) (DenseMat, error) {
1515
mat := DenseMat{}
16-
startIndex := uint32(0)
16+
startIndex := 0
1717
if skipFirstColumn {
1818
startIndex = 1
1919
}
@@ -31,8 +31,8 @@ func DenseMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool
3131
return mat, fmt.Errorf("too few columns")
3232
}
3333

34-
var column uint32
35-
for col := startIndex; col < uint32(len(tokens)); col++ {
34+
var column int
35+
for col := startIndex; col < len(tokens); col++ {
3636
if len(tokens[col]) == 0 {
3737
break
3838
}
@@ -41,7 +41,7 @@ func DenseMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool
4141
return mat, fmt.Errorf("can't parse %s", tokens[col])
4242
}
4343
columnUint64, err := strconv.ParseUint(pair[0], 10, 32)
44-
column = uint32(columnUint64)
44+
column = int(columnUint64)
4545
if err != nil {
4646
return mat, fmt.Errorf("can't convert to float %s: %s", pair[0], err.Error())
4747
}
@@ -71,13 +71,13 @@ func DenseMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool
7171
// CSRMatFromLibsvm reads CSR (Compressed Sparse Row) matrix from libsvm format
7272
// from `reader` stream. If `limit` > 0, reads only first limit `rows`. First
7373
// colums is label, and usually you should set `skipFirstColumn` = true
74-
func CSRMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool) (CSRMat, error) {
74+
func CSRMatFromLibsvm(reader *bufio.Reader, limit int, skipFirstColumn bool) (CSRMat, error) {
7575
mat := CSRMat{}
76-
startIndex := uint32(0)
76+
startIndex := 0
7777
if skipFirstColumn {
7878
startIndex = 1
7979
}
80-
rows := uint32(0)
80+
rows := 0
8181
for {
8282
line, err := reader.ReadString('\n')
8383
if err != nil && err != io.EOF {
@@ -92,9 +92,9 @@ func CSRMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool)
9292
return mat, fmt.Errorf("too few columns")
9393
}
9494

95-
mat.RowHeaders = append(mat.RowHeaders, uint32(len(mat.Values)))
96-
var column uint32
97-
for col := startIndex; col < uint32(len(tokens)); col++ {
95+
mat.RowHeaders = append(mat.RowHeaders, len(mat.Values))
96+
var column int
97+
for col := startIndex; col < len(tokens); col++ {
9898
if len(tokens[col]) == 0 {
9999
break
100100
}
@@ -103,7 +103,7 @@ func CSRMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool)
103103
return mat, fmt.Errorf("can't parse %s", tokens[col])
104104
}
105105
columnUint64, err := strconv.ParseUint(pair[0], 10, 32)
106-
column = uint32(columnUint64)
106+
column = int(columnUint64)
107107
if err != nil {
108108
return mat, fmt.Errorf("can't convert to float %s: %s", pair[0], err.Error())
109109
}
@@ -120,7 +120,7 @@ func CSRMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool)
120120
break
121121
}
122122
}
123-
mat.RowHeaders = append(mat.RowHeaders, uint32(len(mat.Values)))
123+
mat.RowHeaders = append(mat.RowHeaders, len(mat.Values))
124124
return mat, nil
125125
}
126126

@@ -129,13 +129,13 @@ func CSRMatFromLibsvm(reader *bufio.Reader, limit uint32, skipFirstColumn bool)
129129
// is label, and usually you should set `skipFirstColumn` = true. If value is
130130
// absent `defValue` will be used instead
131131
func DenseMatFromCsv(reader *bufio.Reader,
132-
limit uint32,
132+
limit int,
133133
skipFirstColumn bool,
134134
delimiter string,
135135
defValue float64) (DenseMat, error) {
136136

137137
mat := DenseMat{}
138-
startIndex := uint32(0)
138+
startIndex := 0
139139
if skipFirstColumn {
140140
startIndex = 1
141141
}
@@ -150,8 +150,8 @@ func DenseMatFromCsv(reader *bufio.Reader,
150150
}
151151
tokens := strings.Split(line, delimiter)
152152

153-
var column uint32
154-
for col := startIndex; col < uint32(len(tokens)); col++ {
153+
var column int
154+
for col := startIndex; col < len(tokens); col++ {
155155
var value float64
156156
if len(tokens[col]) == 0 {
157157
value = defValue

mat_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ func TestCSRMatFromLibsvm(t *testing.T) {
8484
t.Errorf("mat.Values incorrect: %s", err.Error())
8585
}
8686

87-
trueRowHeaders := []uint32{0, 3, 5}
87+
trueRowHeaders := []int{0, 3, 5}
8888
if !reflect.DeepEqual(mat.RowHeaders, trueRowHeaders) {
8989
t.Error("mat.RowHeaders are incorrect")
9090
}
9191

92-
trueColIndexes := []uint32{0, 10, 12, 4, 5}
92+
trueColIndexes := []int{0, 10, 12, 4, 5}
9393
if !reflect.DeepEqual(mat.ColIndexes, trueColIndexes) {
9494
t.Error("mat.ColIndexes are incorrect")
9595
}
@@ -107,12 +107,12 @@ func TestCSRMatFromLibsvm(t *testing.T) {
107107
t.Errorf("mat.Values incorrect: %s", err.Error())
108108
}
109109

110-
trueRowHeaders = []uint32{0, 3}
110+
trueRowHeaders = []int{0, 3}
111111
if !reflect.DeepEqual(mat.RowHeaders, trueRowHeaders) {
112112
t.Error("mat.RowHeaders are incorrect")
113113
}
114114

115-
trueColIndexes = []uint32{0, 10, 12}
115+
trueColIndexes = []int{0, 10, 12}
116116
if !reflect.DeepEqual(mat.ColIndexes, trueColIndexes) {
117117
t.Error("mat.ColIndexes are incorrect")
118118
}

xgensemble.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// XGEnsemble is XGBoost model (ensemble of trees)
1111
type XGEnsemble struct {
1212
Trees []lgTree
13-
MaxFeatureIdx uint32
13+
MaxFeatureIdx int
1414
}
1515

1616
// NTrees returns number of trees in ensemble
@@ -24,7 +24,7 @@ func (e *XGEnsemble) NTrees() int {
2424
// Note, that result is a raw score (before sigmoid function transformation and etc).
2525
// Note, nan feature values treated as missing values
2626
func (e *XGEnsemble) Predict(fvals []float64, nTrees int) float64 {
27-
if e.MaxFeatureIdx+1 > uint32(len(fvals)) {
27+
if e.MaxFeatureIdx+1 > len(fvals) {
2828
return 0.0
2929
}
3030
ret := 0.0
@@ -46,7 +46,7 @@ func (e *XGEnsemble) Predict(fvals []float64, nTrees int) float64 {
4646
// threads that will be utilized (maximum is GO_MAX_PROCS)
4747
// Note, that result is a raw score (before sigmoid function transformation and etc).
4848
// Note, `predictions` slice should be properly allocated on call side
49-
func (e *XGEnsemble) PredictCSR(indptr []uint32, cols []uint32, vals []float64, predictions []float64, nTrees int, nThreads int) {
49+
func (e *XGEnsemble) PredictCSR(indptr []int, cols []int, vals []float64, predictions []float64, nTrees int, nThreads int) {
5050
nRows := len(indptr) - 1
5151
if nRows <= BatchSize || nThreads == 0 || nThreads == 1 {
5252
fvals := make([]float64, e.MaxFeatureIdx+1)
@@ -96,18 +96,18 @@ func (e *XGEnsemble) PredictCSR(indptr []uint32, cols []uint32, vals []float64,
9696
wg.Wait()
9797
}
9898

99-
func (e *XGEnsemble) predictCSRInner(indptr []uint32, cols []uint32, vals []float64, startIndex int, endIndex int, predictions []float64, nTrees int, fvals []float64) {
99+
func (e *XGEnsemble) predictCSRInner(indptr []int, cols []int, vals []float64, startIndex int, endIndex int, predictions []float64, nTrees int, fvals []float64) {
100100
for i := startIndex; i < endIndex; i++ {
101101
start := indptr[i]
102102
end := indptr[i+1]
103103
for j := start; j < end; j++ {
104-
if cols[j] < uint32(len(fvals)) {
104+
if cols[j] < len(fvals) {
105105
fvals[cols[j]] = vals[j]
106106
}
107107
}
108108
predictions[i] = e.Predict(fvals, nTrees)
109109
for j := start; j < end; j++ {
110-
if cols[j] < uint32(len(fvals)) {
110+
if cols[j] < len(fvals) {
111111
fvals[cols[j]] = math.NaN()
112112
}
113113
}
@@ -120,8 +120,8 @@ func (e *XGEnsemble) predictCSRInner(indptr []uint32, cols []uint32, vals []floa
120120
// threads that will be utilized (maximum is GO_MAX_PROCS)
121121
// Note, that result is a raw score (before sigmoid function transformation and etc).
122122
// Note, `predictions` slice should be properly allocated on call side
123-
func (e *XGEnsemble) PredictDense(vals []float64, nrows uint32, ncols uint32, predictions []float64, nTrees int, nThreads int) error {
124-
nRows := int(nrows)
123+
func (e *XGEnsemble) PredictDense(vals []float64, nrows int, ncols int, predictions []float64, nTrees int, nThreads int) error {
124+
nRows := nrows
125125
if ncols == 0 || e.MaxFeatureIdx > ncols-1 {
126126
return fmt.Errorf("incorrect number of columns")
127127
}

xgensemble_io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func XGEnsembleFromReader(reader *bufio.Reader) (*XGEnsemble, error) {
156156
if header.Param.NumFeatures == 0 {
157157
return nil, fmt.Errorf("zero number of features")
158158
}
159-
e.MaxFeatureIdx = header.Param.NumFeatures - 1
159+
e.MaxFeatureIdx = int(header.Param.NumFeatures) - 1
160160

161161
// reading gbtree
162162
origModel, err := xgbin.ReadGBTreeModel(reader)

0 commit comments

Comments
 (0)