-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeseries.go
137 lines (105 loc) · 2.65 KB
/
timeseries.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
package models
import (
"errors"
"fmt"
"slices"
"time"
"github.com/montanaflynn/stats"
"github.com/rs/zerolog/log"
"github.com/soniakeys/cluster"
)
type TimeSeries struct {
Quantities []*Quantity `json:"quantities"`
}
func NewTimeSeries() *TimeSeries {
return &TimeSeries{
Quantities: []*Quantity{},
}
}
func (ts *TimeSeries) IsEmpty() bool {
for _, q := range ts.Quantities {
if !q.IsEmpty() {
return false
}
}
return true
}
func (ts *TimeSeries) SortByTime() bool {
for _, q := range ts.Quantities {
q.SortByTime()
}
return true
}
func (ts *TimeSeries) AddQuantity(q *Quantity) {
ts.Quantities = append(ts.Quantities, q)
}
func (ts *TimeSeries) FindQuantity(name string) (*Quantity, bool) {
for _, q := range ts.Quantities {
if q.Name == name {
return q, true
}
}
return nil, false
}
func (ts *TimeSeries) DropRecordsBefore(t time.Time) {
for _, q := range ts.Quantities {
q.DropRecordsBefore(t)
}
}
type MakePointFunc func(q *Quantity) (cluster.Point, error)
func (ts *TimeSeries) Cluster(k int, makePoint MakePointFunc) error {
if k < 1 {
return fmt.Errorf("k %d is not positive", k)
}
if k == 1 {
for _, q := range ts.Quantities {
q.Attributes[AttrCluster] = 0
}
return nil
}
points := []cluster.Point{}
qs := []*Quantity{}
for _, q := range ts.Quantities {
p, err := makePoint(q)
if err != nil {
log.Warn().Err(err).Str("name", q.Name).Msg("cannot make clustering points for quantity")
continue
}
qs = append(qs, q)
points = append(points, p)
}
if len(qs) == 0 {
return errors.New("failed to make clustering points for all quantities")
}
centers, cNums, _, _ := cluster.KMPP(points, k)
// clone and reverse sort center points
revSortedCenters := slices.Clone(centers)
slices.SortFunc(revSortedCenters, func(a, b cluster.Point) int {
return slices.Compare(a, b)
})
slices.Reverse(revSortedCenters)
for i, cNum := range cNums {
tgt := centers[cNum]
// find the sorted index
sortedIdx := slices.IndexFunc(revSortedCenters, func(c cluster.Point) bool {
return slices.Compare(c, tgt) == 0
})
qs[i].Attributes[AttrCluster] = sortedIdx
}
return nil
}
func QuantityMeanStddev(q *Quantity) (cluster.Point, error) {
vals := q.RecordValues()
if len(vals) == 0 {
return cluster.Point{}, fmt.Errorf("quantity %s has no values", q.Name)
}
mean, err := stats.Mean(vals)
if err != nil {
return cluster.Point{}, fmt.Errorf("failed to calc mean for quantity %s: %w", q.Name, err)
}
sd, err := stats.StandardDeviation(vals)
if err != nil {
return cluster.Point{}, fmt.Errorf("failed to calc stddev for quantity %s: %w", q.Name, err)
}
return cluster.Point{mean, sd}, nil
}