/
rphash.go
95 lines (76 loc) · 1.78 KB
/
rphash.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
package main
import (
"encoding/gob"
"sync"
"flag"
"fmt"
_ "github.com/chrislusf/glow/driver"
"github.com/chrislusf/glow/flow"
"github.com/wilseypa/rphash-golang/parse"
"github.com/wilseypa/rphash-golang/reader"
"github.com/wilseypa/rphash-golang/stream"
"github.com/wilseypa/rphash-golang/itemset"
"github.com/wilseypa/rphash-golang/utils"
"os"
"time"
)
var (
f = flow.New()
expectedDimensions = -1
numClusters = 6
)
type Centroid struct {
C *itemset.Centroid
}
func goStart(wg *sync.WaitGroup, fn func()) {
wg.Add(1)
go func() {
defer wg.Done()
fn()
}()
}
func main() {
gob.Register(Centroid{})
gob.Register(itemset.Centroid{})
gob.Register(utils.Hash64Set{})
flag.Parse()
t1 := time.Now()
records := utils.ReadCSV("./dataset.csv")
Object := reader.NewStreamObject(len(records[0]), numClusters)
Stream := stream.NewStream(Object)
outChannel := make(chan Centroid)
ch := make(chan []float64)
source := f.Channel(ch)
f1 := source.Map(func(record []float64) Centroid {
return Centroid{C:Stream.AddVectorOnlineStep(record)}
}).AddOutput(outChannel)
flow.Ready()
var wg sync.WaitGroup
goStart(&wg, func() {
f1.Run()
})
goStart(&wg, func() {
for out := range outChannel {
Stream.CentroidCounter.Add(out.C)
}
})
for _, record := range records {
ch <- record
}
close(ch)
wg.Wait()
normalizedResults := Stream.GetCentroids()
ts := time.Since(t1)
file, err := os.OpenFile("./results.txt", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
defer file.Close()
for _, result := range normalizedResults {
for _, dimension := range result {
file.WriteString(fmt.Sprintf("%f ", parse.DeNormalize(dimension)))
}
file.WriteString("\n")
}
file.WriteString("Time: " + ts.String())
}