-
Notifications
You must be signed in to change notification settings - Fork 9
/
pmp.go
186 lines (164 loc) · 4.6 KB
/
pmp.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package matrixprofile
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"github.com/matrix-profile-foundation/go-matrixprofile/util"
)
// PMP represents the pan matrix profile
type PMP struct {
A []float64 `json:"a"` // query time series
B []float64 `json:"b"` // timeseries to perform full join with
SelfJoin bool `json:"self_join"` // indicates whether a self join is performed with an exclusion zone
PMP [][]float64 `json:"pmp"` // pan matrix profile
PIdx [][]int `json:"ppi"` // pan matrix profile index
PWindows []int `json:"windows"` // pan matrix windows used and is aligned with PMP and PIdx
Opts *PMPOpts `json:"options"` // options used for the computation
}
// NewPMP creates a new Pan matrix profile
func NewPMP(a, b []float64) (*PMP, error) {
if a == nil || len(a) == 0 {
return nil, fmt.Errorf("first slice is nil or has a length of 0")
}
if b != nil && len(b) == 0 {
return nil, fmt.Errorf("second slice must be nil for self-join operation or have a length greater than 0")
}
p := PMP{A: a}
if b == nil {
p.B = a
p.SelfJoin = true
} else {
p.B = b
}
return &p, nil
}
// Save will save the current matrix profile struct to disk
func (p PMP) Save(filepath, format string) error {
var err error
switch format {
case "json":
f, err := os.Open(filepath)
if err != nil {
f, err = os.Create(filepath)
if err != nil {
return err
}
}
defer f.Close()
out, err := json.Marshal(p)
if err != nil {
return err
}
_, err = f.Write(out)
default:
return fmt.Errorf("invalid save format, %s", format)
}
return err
}
// Load will attempt to load a matrix profile from a file for iterative use
func (p *PMP) Load(filepath, format string) error {
var err error
switch format {
case "json":
f, err := os.Open(filepath)
if err != nil {
return err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
err = json.Unmarshal(b, p)
default:
return fmt.Errorf("invalid load format, %s", format)
}
return err
}
// PMPOpts are parameters to vary the algorithm to compute the pan matrix profile.
type PMPOpts struct {
LowerM int `json:"lower_m"` // used for pan matrix profile
UpperM int `json:"upper_m"` // used for pan matrix profile
MPOpts *MPOpts `json:"mp_options"`
}
// NewPMPOpts returns a default PMPOpts
func NewPMPOpts(l, u int) *PMPOpts {
if l > u {
u = l
}
return &PMPOpts{
LowerM: l,
UpperM: u,
MPOpts: NewMPOpts(),
}
}
// Compute calculate the pan matrixprofile given a set of input options.
func (p *PMP) Compute(o *PMPOpts) error {
if o == nil {
return errors.New("Must provide PMP compute options")
}
p.Opts = o
return p.pmp()
}
func (p *PMP) pmp() error {
windows := util.BinarySplit(p.Opts.LowerM, p.Opts.UpperM)
windows = windows[:int(float64(len(windows))*p.Opts.MPOpts.SamplePct)]
if len(windows) < 1 {
return errors.New("Need more than one subsequence window for pmp")
}
p.PWindows = windows
p.PMP = make([][]float64, len(windows))
p.PIdx = make([][]int, len(windows))
for i := 0; i < len(windows); i++ {
lenA := len(p.A) - (i + p.Opts.LowerM) + 1
p.PMP[i] = make([]float64, lenA)
p.PIdx[i] = make([]int, lenA)
for j := 0; j < lenA; j++ {
p.PMP[i][j] = math.Inf(1)
p.PIdx[i][j] = math.MaxInt64
}
}
// need to create a new mp
var mp *MatrixProfile
var err error
if p.SelfJoin {
mp, err = New(p.A, nil, windows[0])
} else {
mp, err = New(p.A, p.B, windows[0])
}
if err != nil {
return err
}
for _, w := range windows {
mp.W = w
if err := mp.Compute(p.Opts.MPOpts); err != nil {
return err
}
copy(p.PMP[w-p.Opts.LowerM], mp.MP)
copy(p.PIdx[w-p.Opts.LowerM], mp.Idx)
}
return nil
}
// Analyze has not been implemented yet
func (p PMP) Analyze(co *MPOpts, ao *AnalyzeOpts) error {
return errors.New("Analyze for PMP has not been implemented yet.")
}
// DiscoverMotifs has not been implemented yet
func (p PMP) DiscoverMotifs(k int, r float64) ([]MotifGroup, error) {
return nil, errors.New("Motifs for PMP has not been implemented yet.")
}
// DiscoverDiscords has not been implemented yet
func (p PMP) DiscoverDiscords(k int, exclusionZone int) ([]int, error) {
return nil, errors.New("Discords for PMP has not been implemented yet.")
}
// DiscoverSegments has not been implemented yet
func (p PMP) DiscoverSegments() (int, float64, []float64) {
return 0, 0, nil
}
// Visualize has not been implemented yet
func (p PMP) Visualize(fn string, motifs []MotifGroup, discords []int, cac []float64) error {
return errors.New("Visualize for PMP has not been implemented yet.")
}