-
Notifications
You must be signed in to change notification settings - Fork 7
/
table.go
77 lines (73 loc) · 1.61 KB
/
table.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
// Copyright (c) 2019, The Goki 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 agg
import (
"github.com/emer/etable/v2/etable"
"github.com/emer/etable/v2/etensor"
)
// MeanTables returns an etable.Table with the mean values across all float
// columns of the input tables, which must have the same columns but not
// necessarily the same number of rows.
func MeanTables(dts []*etable.Table) *etable.Table {
nt := len(dts)
if nt == 0 {
return nil
}
maxRows := 0
var maxdt *etable.Table
for _, dt := range dts {
if dt.Rows > maxRows {
maxRows = dt.Rows
maxdt = dt
}
}
if maxRows == 0 {
return nil
}
ot := maxdt.Clone()
// N samples per row
rns := make([]int, maxRows)
for _, dt := range dts {
dnr := dt.Rows
mx := min(dnr, maxRows)
for ri := 0; ri < mx; ri++ {
rns[ri]++
}
}
for ci, cl := range ot.Cols {
if cl.DataType() != etensor.FLOAT32 && cl.DataType() != etensor.FLOAT64 {
continue
}
_, cells := cl.RowCellSize()
for di, dt := range dts {
if di == 0 {
continue
}
dc := dt.Cols[ci]
dnr := dt.Rows
mx := min(dnr, maxRows)
for ri := 0; ri < mx; ri++ {
si := ri * cells
for j := 0; j < cells; j++ {
ci := si + j
cv := cl.FloatValue1D(ci)
cv += dc.FloatValue1D(ci)
cl.SetFloat1D(ci, cv)
}
}
}
for ri := 0; ri < maxRows; ri++ {
si := ri * cells
for j := 0; j < cells; j++ {
ci := si + j
cv := cl.FloatValue1D(ci)
if rns[ri] > 0 {
cv /= float64(rns[ri])
cl.SetFloat1D(ci, cv)
}
}
}
}
return ot
}