-
Notifications
You must be signed in to change notification settings - Fork 36
/
feature.go
175 lines (151 loc) · 3.79 KB
/
feature.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
package meta
import (
"fmt"
"sort"
"strings"
"time"
)
const (
featureStation = iota
featureLocation
featureSublocation
featureProperty
featureDescription
featureAspect
featureStart
featureEnd
)
var featureHeaders Header = map[string]int{
"Station": featureStation,
"Location": featureLocation,
"Sublocation": featureSublocation,
"Property": featureProperty,
"Description": featureDescription,
"Aspect": featureAspect,
"Start Date": featureStart,
"End Date": featureEnd,
}
type Feature struct {
Span
Station string
Location string
Sublocation string
Property string
Description string
Aspect string
}
// Id is a shorthand reference to the sample for debugging or testing.
func (f Feature) Id() string {
return fmt.Sprintf("%s_%s_%s:%s", f.Station, f.Location, f.Sublocation, f.Start.Format(DateTimeFormat))
}
// Less allows samples to be sorted.
func (f Feature) Less(feature Feature) bool {
switch {
case f.Station < feature.Station:
return true
case f.Station > feature.Station:
return false
case f.Location < feature.Location:
return true
case f.Location > feature.Location:
return false
case f.Sublocation < feature.Sublocation:
return true
case f.Sublocation > feature.Sublocation:
return false
case f.Property < feature.Property:
return true
case f.Property > feature.Property:
return false
case f.Description < feature.Description:
return true
case f.Description > feature.Description:
return false
case f.Aspect < feature.Aspect:
return true
case f.Aspect > feature.Aspect:
return false
case f.Start.Before(feature.Start):
return true
default:
return false
}
}
// Overlaps allows samples to be tested.
func (f Feature) Overlaps(feature Feature) bool {
switch {
case f.Station != feature.Station:
return false
case f.Location != feature.Location:
return false
case f.Sublocation != feature.Sublocation:
return false
case f.Property != feature.Property:
return false
case !f.Span.Overlaps(feature.Span):
return false
default:
return true
}
}
type FeatureList []Feature
func (f FeatureList) Len() int { return len(f) }
func (f FeatureList) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f FeatureList) Less(i, j int) bool { return f[i].Less(f[j]) }
func (f FeatureList) encode() [][]string {
var data [][]string
data = append(data, featureHeaders.Columns())
for _, row := range f {
data = append(data, []string{
strings.TrimSpace(row.Station),
strings.TrimSpace(row.Location),
strings.TrimSpace(row.Sublocation),
strings.TrimSpace(row.Property),
strings.TrimSpace(row.Description),
strings.TrimSpace(row.Aspect),
row.Start.Format(DateTimeFormat),
row.End.Format(DateTimeFormat),
})
}
return data
}
func (f *FeatureList) decode(data [][]string) error {
if !(len(data) > 1) {
return nil
}
var features []Feature
fields := featureHeaders.Fields(data[0])
for _, row := range data[1:] {
d := fields.Remap(row)
start, err := time.Parse(DateTimeFormat, d[featureStart])
if err != nil {
return err
}
end, err := time.Parse(DateTimeFormat, d[featureEnd])
if err != nil {
return err
}
features = append(features, Feature{
Span: Span{
Start: start,
End: end,
},
Station: strings.TrimSpace(d[featureStation]),
Location: strings.TrimSpace(d[featureLocation]),
Sublocation: strings.TrimSpace(d[featureSublocation]),
Property: strings.TrimSpace(d[featureProperty]),
Description: strings.TrimSpace(d[featureDescription]),
Aspect: strings.TrimSpace(d[featureAspect]),
})
}
*f = FeatureList(features)
return nil
}
func LoadFeatures(path string) ([]Feature, error) {
var f []Feature
if err := LoadList(path, (*FeatureList)(&f)); err != nil {
return nil, err
}
sort.Sort(FeatureList(f))
return f, nil
}