Skip to content

Commit

Permalink
Merge pull request sjwhitworth#76 from amitkgupta/master
Browse files Browse the repository at this point in the history
Improve ConfusionMatrix GetSummary formatting, and several tiny improvements
  • Loading branch information
sjwhitworth committed Aug 22, 2014
2 parents 45f0be7 + 14aad31 commit c8bf178
Show file tree
Hide file tree
Showing 41 changed files with 457 additions and 511 deletions.
4 changes: 2 additions & 2 deletions base/bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBAGSimple(t *testing.T) {
} else if name == "2" {
attrSpecs[2] = a
} else {
panic(name)
t.Fatalf("Unexpected attribute name '%s'", name)
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func TestBAG(t *testing.T) {
} else if name == "2" {
attrSpecs[2] = a
} else {
panic(name)
t.Fatalf("Unexpected attribute name '%s'", name)
}
}

Expand Down
18 changes: 11 additions & 7 deletions base/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

// ParseCSVGetRows returns the number of rows in a given file.
func ParseCSVGetRows(filepath string) int {
func ParseCSVGetRows(filepath string) (int, error) {
file, err := os.Open(filepath)
if err != nil {
panic(err)
return 0, err
}
defer file.Close()

Expand All @@ -25,11 +25,11 @@ func ParseCSVGetRows(filepath string) int {
if err == io.EOF {
break
} else if err != nil {
panic(err)
return 0, err
}
counter++
}
return counter
return counter, nil
}

// ParseCSVGetAttributes returns an ordered slice of appropriate-ly typed
Expand Down Expand Up @@ -157,7 +157,11 @@ func ParseCSVBuildInstances(filepath string, hasHeaders bool, u UpdatableDataGri
func ParseCSVToInstances(filepath string, hasHeaders bool) (instances *DenseInstances, err error) {

// Read the number of rows in the file
rowCount := ParseCSVGetRows(filepath)
rowCount, err := ParseCSVGetRows(filepath)
if err != nil {
return nil, err
}

if hasHeaders {
rowCount--
}
Expand All @@ -176,7 +180,7 @@ func ParseCSVToInstances(filepath string, hasHeaders bool) (instances *DenseInst
// Read the input
file, err := os.Open(filepath)
if err != nil {
panic(err)
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
Expand All @@ -188,7 +192,7 @@ func ParseCSVToInstances(filepath string, hasHeaders bool) (instances *DenseInst
if err == io.EOF {
break
} else if err != nil {
panic(err)
return nil, err
}
if rowCounter == 0 {
if hasHeaders {
Expand Down
83 changes: 52 additions & 31 deletions base/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,127 @@ import (
"testing"
)

func TestParseCSVGetRows(testEnv *testing.T) {
lineCount := ParseCSVGetRows("../examples/datasets/iris.csv")
func TestParseCSVGetRows(t *testing.T) {
lineCount, err := ParseCSVGetRows("../examples/datasets/iris.csv")
if err != nil {
t.Fatalf("Unable to parse CSV to get number of rows: %s", err.Error())
}
if lineCount != 150 {
testEnv.Errorf("Should have %d lines, has %d", 150, lineCount)
t.Errorf("Should have %d lines, has %d", 150, lineCount)
}

lineCount, err = ParseCSVGetRows("../examples/datasets/iris_headers.csv")
if err != nil {
t.Fatalf("Unable to parse CSV to get number of rows: %s", err.Error())
}

lineCount = ParseCSVGetRows("../examples/datasets/iris_headers.csv")
if lineCount != 151 {
testEnv.Errorf("Should have %d lines, has %d", 151, lineCount)
t.Errorf("Should have %d lines, has %d", 151, lineCount)
}

}

func TestParseCCSVGetAttributes(testEnv *testing.T) {
func TestParseCSVGetRowsWithMissingFile(t *testing.T) {
_, err := ParseCSVGetRows("../examples/datasets/non-existent.csv")
if err == nil {
t.Fatal("Expected ParseCSVGetRows to return error when given path to non-existent file")
}
}

func TestParseCCSVGetAttributes(t *testing.T) {
attrs := ParseCSVGetAttributes("../examples/datasets/iris_headers.csv", true)
if attrs[0].GetType() != Float64Type {
testEnv.Errorf("First attribute should be a float, %s", attrs[0])
t.Errorf("First attribute should be a float, %s", attrs[0])
}
if attrs[0].GetName() != "Sepal length" {
testEnv.Errorf(attrs[0].GetName())
t.Errorf(attrs[0].GetName())
}

if attrs[4].GetType() != CategoricalType {
testEnv.Errorf("Final attribute should be categorical, %s", attrs[4])
t.Errorf("Final attribute should be categorical, %s", attrs[4])
}
if attrs[4].GetName() != "Species" {
testEnv.Error(attrs[4])
t.Error(attrs[4])
}
}

func TestParseCsvSniffAttributeTypes(testEnv *testing.T) {
func TestParseCsvSniffAttributeTypes(t *testing.T) {
attrs := ParseCSVSniffAttributeTypes("../examples/datasets/iris_headers.csv", true)
if attrs[0].GetType() != Float64Type {
testEnv.Errorf("First attribute should be a float, %s", attrs[0])
t.Errorf("First attribute should be a float, %s", attrs[0])
}
if attrs[1].GetType() != Float64Type {
testEnv.Errorf("Second attribute should be a float, %s", attrs[1])
t.Errorf("Second attribute should be a float, %s", attrs[1])
}
if attrs[2].GetType() != Float64Type {
testEnv.Errorf("Third attribute should be a float, %s", attrs[2])
t.Errorf("Third attribute should be a float, %s", attrs[2])
}
if attrs[3].GetType() != Float64Type {
testEnv.Errorf("Fourth attribute should be a float, %s", attrs[3])
t.Errorf("Fourth attribute should be a float, %s", attrs[3])
}
if attrs[4].GetType() != CategoricalType {
testEnv.Errorf("Final attribute should be categorical, %s", attrs[4])
t.Errorf("Final attribute should be categorical, %s", attrs[4])
}
}

func TestParseCSVSniffAttributeNamesWithHeaders(testEnv *testing.T) {
func TestParseCSVSniffAttributeNamesWithHeaders(t *testing.T) {
attrs := ParseCSVSniffAttributeNames("../examples/datasets/iris_headers.csv", true)
if attrs[0] != "Sepal length" {
testEnv.Error(attrs[0])
t.Error(attrs[0])
}
if attrs[1] != "Sepal width" {
testEnv.Error(attrs[1])
t.Error(attrs[1])
}
if attrs[2] != "Petal length" {
testEnv.Error(attrs[2])
t.Error(attrs[2])
}
if attrs[3] != "Petal width" {
testEnv.Error(attrs[3])
t.Error(attrs[3])
}
if attrs[4] != "Species" {
testEnv.Error(attrs[4])
t.Error(attrs[4])
}
}

func TestReadInstances(testEnv *testing.T) {
func TestParseCSVToInstances(t *testing.T) {
inst, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
testEnv.Error(err)
t.Error(err)
return
}
row1 := inst.RowString(0)
row2 := inst.RowString(50)
row3 := inst.RowString(100)

if row1 != "5.10 3.50 1.40 0.20 Iris-setosa" {
testEnv.Error(row1)
t.Error(row1)
}
if row2 != "7.00 3.20 4.70 1.40 Iris-versicolor" {
testEnv.Error(row2)
t.Error(row2)
}
if row3 != "6.30 3.30 6.00 2.50 Iris-virginica" {
testEnv.Error(row3)
t.Error(row3)
}
}

func TestParseCSVToInstancesWithMissingFile(t *testing.T) {
_, err := ParseCSVToInstances("../examples/datasets/non-existent.csv", true)
if err == nil {
t.Fatal("Expected ParseCSVToInstances to return error when given path to non-existent file")
}
}

func TestReadAwkwardInsatnces(testEnv *testing.T) {
func TestReadAwkwardInsatnces(t *testing.T) {
inst, err := ParseCSVToInstances("../examples/datasets/chim.csv", true)
if err != nil {
testEnv.Error(err)
t.Error(err)
return
}
attrs := inst.AllAttributes()
if attrs[0].GetType() != Float64Type {
testEnv.Error("Should be float!")
t.Error("Should be float!")
}
if attrs[1].GetType() != CategoricalType {
testEnv.Error("Should be discrete!")
t.Error("Should be discrete!")
}
}
16 changes: 8 additions & 8 deletions base/edf/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ func TestAllocFixed(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
So(err, ShouldEqual, nil)
Convey("Mapping the file should suceed", func() {
Convey("Mapping the file should succeed", func() {
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
Convey("Allocation should suceed", func() {
Convey("Allocation should succeed", func() {
r, err := mapping.AllocPages(1, 2)
So(err, ShouldEqual, nil)
So(r.Start.Byte, ShouldEqual, 4*os.Getpagesize())
So(r.Start.Segment, ShouldEqual, 0)
Convey("Unmapping the file should suceed", func() {
Convey("Unmapping the file should succeed", func() {
err = mapping.Unmap(EDF_UNMAP_SYNC)
So(err, ShouldEqual, nil)
Convey("Remapping the file should suceed", func() {
Convey("Remapping the file should succeed", func() {
mapping, err = EdfMap(tempFile, EDF_READ_ONLY)
Convey("Should get the same allocations back", func() {
rr, err := mapping.GetThreadBlocks(2)
Expand All @@ -41,20 +41,20 @@ func TestAllocWithExtraContentsBlock(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
So(err, ShouldEqual, nil)
Convey("Mapping the file should suceed", func() {
Convey("Mapping the file should succeed", func() {
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
Convey("Allocation of 10 pages should suceed", func() {
Convey("Allocation of 10 pages should succeed", func() {
allocated := make([]EdfRange, 10)
for i := 0; i < 10; i++ {
r, err := mapping.AllocPages(1, 2)
So(err, ShouldEqual, nil)
allocated[i] = r
}
Convey("Unmapping the file should suceed", func() {
Convey("Unmapping the file should succeed", func() {
err = mapping.Unmap(EDF_UNMAP_SYNC)
So(err, ShouldEqual, nil)
Convey("Remapping the file should suceed", func() {
Convey("Remapping the file should succeed", func() {
mapping, err = EdfMap(tempFile, EDF_READ_ONLY)
Convey("Should get the same allocations back", func() {
rr, err := mapping.GetThreadBlocks(2)
Expand Down
8 changes: 4 additions & 4 deletions base/edf/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAnonMap(t *testing.T) {
Convey("Anonymous mapping should suceed", t, func() {
Convey("Anonymous mapping should succeed", t, func() {
mapping, err := EdfAnonMap()
So(err, ShouldEqual, nil)
bytes := mapping.m[0]
Expand Down Expand Up @@ -39,10 +39,10 @@ func TestFileCreate(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
So(err, ShouldEqual, nil)
Convey("Mapping the file should suceed", func() {
Convey("Mapping the file should succeed", func() {
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
Convey("Unmapping the file should suceed", func() {
Convey("Unmapping the file should succeed", func() {
err = mapping.Unmap(EDF_UNMAP_SYNC)
So(err, ShouldEqual, nil)
})
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestFileThreadCounter(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
So(err, ShouldEqual, nil)
Convey("Mapping the file should suceed", func() {
Convey("Mapping the file should succeed", func() {
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
Convey("The file should have two threads to start with", func() {
Expand Down
38 changes: 19 additions & 19 deletions base/edf/thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,52 @@ package edf

import (
. "github.com/smartystreets/goconvey/convey"
"testing"
"os"
"testing"
)

func TestThreadDeserialize(T *testing.T) {
func TestThreadDeserialize(t *testing.T) {
bytes := []byte{0, 0, 0, 6, 83, 89, 83, 84, 69, 77, 0, 0, 0, 1}
Convey("Given a byte slice", T, func() {
var t Thread
size := t.Deserialize(bytes)
Convey("Given a byte slice", t, func() {
var thread Thread
size := thread.Deserialize(bytes)
Convey("Decoded name should be SYSTEM", func() {
So(t.name, ShouldEqual, "SYSTEM")
So(thread.name, ShouldEqual, "SYSTEM")
})
Convey("Size should be the same as the array", func() {
So(size, ShouldEqual, len(bytes))
})
})
}

func TestThreadSerialize(T *testing.T) {
var t Thread
func TestThreadSerialize(t *testing.T) {
var thread Thread
refBytes := []byte{0, 0, 0, 6, 83, 89, 83, 84, 69, 77, 0, 0, 0, 1}
t.name = "SYSTEM"
t.id = 1
thread.name = "SYSTEM"
thread.id = 1
toBytes := make([]byte, len(refBytes))
Convey("Should serialize correctly", T, func() {
t.Serialize(toBytes)
Convey("Should serialize correctly", t, func() {
thread.Serialize(toBytes)
So(toBytes, ShouldResemble, refBytes)
})
}

func TestThreadFindAndWrite(T *testing.T) {
Convey("Creating a non-existent file should succeed", T, func() {
tempFile, err := os.OpenFile("hello.db", os.O_RDWR | os.O_TRUNC | os.O_CREATE, 0700) //ioutil.TempFile(os.TempDir(), "TestFileCreate")
func TestThreadFindAndWrite(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
tempFile, err := os.OpenFile("hello.db", os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0700) //ioutil.TempFile(os.TempDir(), "TestFileCreate")
So(err, ShouldEqual, nil)
Convey("Mapping the file should suceed", func() {
Convey("Mapping the file should succeed", func() {
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
Convey("Writing the thread should succeed", func () {
Convey("Writing the thread should succeed", func() {
t := NewThread(mapping, "MyNameISWhat")
Convey("Thread number should be 3", func () {
Convey("Thread number should be 3", func() {
So(t.id, ShouldEqual, 3)
})
Convey("Writing the thread should succeed", func() {
err := mapping.WriteThread(t)
So(err, ShouldEqual, nil)
Convey("Should be able to find the thread again later", func() {
Convey("Should be able to find the thread again later", func() {
id, err := mapping.FindThread("MyNameISWhat")
So(err, ShouldEqual, nil)
So(id, ShouldEqual, 3)
Expand Down
Loading

0 comments on commit c8bf178

Please sign in to comment.