Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dbscan bug of task dispatch #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions csv_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func TestImportedLoadCorrectData(t *testing.T) {
f = "data/test.csv"
i = CsvImporter()
s = [][]float64{
[]float64{0.1, 0.2, 0.3},
[]float64{0.4, 0.5, 0.6},
[]float64{0.7, 0.8, 0.9},
{0.1, 0.2, 0.3},
{0.4, 0.5, 0.6},
{0.7, 0.8, 0.9},
}
)

Expand All @@ -41,7 +41,7 @@ func TestImportedLoadCorrectData(t *testing.T) {
}

if !fsliceEqual(d, s) {
t.Error("Imported data mismatch: %v vs %v\n", d, s)
t.Errorf("Imported data mismatch: %v vs %v\n", d, s)
}
}

Expand Down
31 changes: 19 additions & 12 deletions dbscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ type dbscanClusterer struct {
distance DistanceFunc

// slices holding the cluster mapping and sizes. Access is synchronized to avoid read during computation.
mu sync.RWMutex
a, b []int
mu sync.RWMutex
// groups for dateset
a []int
b []int

// variables used for concurrent computation of nearest neighbours
l, s, o, f int
j chan *rangeJob
m *sync.Mutex
w *sync.WaitGroup
r *[]int
p []float64
// dataset len
l int
// worker number
s int
// work number for per worker
f int
j chan *rangeJob
m *sync.Mutex
w *sync.WaitGroup
// current point near
r *[]int
// current point
p []float64

// visited points
v []bool
Expand Down Expand Up @@ -78,7 +87,6 @@ func (c *dbscanClusterer) Learn(data [][]float64) error {

c.l = len(data)
c.s = c.numWorkers()
c.o = c.s - 1
c.f = c.l / c.s

c.d = data
Expand Down Expand Up @@ -198,15 +206,14 @@ func (c *dbscanClusterer) nearest(p int, l *int, r *[]int) {
c.p = c.d[p]
c.r = r

c.w.Add(c.s)

for i := 0; i < c.l; i += c.f {
if c.l-i <= c.f {
b = c.l - 1
b = c.l
} else {
b = i + c.f
}

c.w.Add(1)
c.j <- &rangeJob{
a: i,
b: b,
Expand Down
72 changes: 72 additions & 0 deletions dbscan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package clusters

import (
"reflect"
"testing"
)

func TestDBSCANCluster(t *testing.T) {
tests := []struct {
MinPts int
Eps float64
Points [][]float64
Expected []int
}{
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}},
Expected: []int{1},
},
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}, {1.5}},
Expected: []int{1, 1},
},
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}, {1}},
Expected: []int{1, 1},
},
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}, {1}, {1}},
Expected: []int{1, 1, 1},
},
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}, {1.5}, {2}},
Expected: []int{1, 1, 1},
},
{
MinPts: 1,
Eps: 1,
Points: [][]float64{{1}, {1.5}, {3}},
Expected: []int{1, 1, 2},
},
{
MinPts: 2,
Eps: 1,
Points: [][]float64{{1}, {3}},
Expected: []int{-1, -1},
},
}
for _, test := range tests {
c, e := DBSCAN(test.MinPts, test.Eps, 0, EuclideanDistance)
if e != nil {
t.Errorf("Error initializing kmeans clusterer: %s\n", e.Error())
}

if e = c.Learn(test.Points); e != nil {
t.Errorf("Error learning data: %s\n", e.Error())
}

if !reflect.DeepEqual(c.Guesses(), test.Expected) {
t.Errorf("guesses does not match: %d vs %d\n", c.Guesses(), test.Expected)
}
}
}
2 changes: 1 addition & 1 deletion kmeans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func TestKmeansClusterNumerMatches(t *testing.T) {
func TestKmeansClusterNumberMatches(t *testing.T) {
const (
C = 8
)
Expand Down