mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-25 13:48:49 +08:00
go fmt ./...
This commit is contained in:
parent
45f0be7607
commit
94e5843bcf
@ -2,8 +2,8 @@ package edf
|
||||
|
||||
import (
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"testing"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestThreadDeserialize(T *testing.T) {
|
||||
@ -34,20 +34,20 @@ func TestThreadSerialize(T *testing.T) {
|
||||
|
||||
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")
|
||||
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() {
|
||||
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)
|
||||
|
@ -1,28 +1,28 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// FloatAttribute is an implementation which stores floating point
|
||||
// representations of numbers.
|
||||
type FloatAttribute struct {
|
||||
Name string
|
||||
Precision int
|
||||
Name string
|
||||
Precision int
|
||||
}
|
||||
|
||||
// NewFloatAttribute returns a new FloatAttribute with a default
|
||||
// precision of 2 decimal places
|
||||
func NewFloatAttribute(name string) *FloatAttribute {
|
||||
return &FloatAttribute{name, 2}
|
||||
return &FloatAttribute{name, 2}
|
||||
}
|
||||
|
||||
// Compatable checks whether this FloatAttribute can be ponded with another
|
||||
// Attribute (checks if they're both FloatAttributes)
|
||||
func (Attr *FloatAttribute) Compatable(other Attribute) bool {
|
||||
_, ok := other.(*FloatAttribute)
|
||||
return ok
|
||||
_, ok := other.(*FloatAttribute)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Equals tests a FloatAttribute for equality with another Attribute.
|
||||
@ -30,50 +30,50 @@ func (Attr *FloatAttribute) Compatable(other Attribute) bool {
|
||||
// Returns false if the other Attribute has a different name
|
||||
// or if the other Attribute is not a FloatAttribute.
|
||||
func (Attr *FloatAttribute) Equals(other Attribute) bool {
|
||||
// Check whether this FloatAttribute is equal to another
|
||||
_, ok := other.(*FloatAttribute)
|
||||
if !ok {
|
||||
// Not the same type, so can't be equal
|
||||
return false
|
||||
}
|
||||
if Attr.GetName() != other.GetName() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
// Check whether this FloatAttribute is equal to another
|
||||
_, ok := other.(*FloatAttribute)
|
||||
if !ok {
|
||||
// Not the same type, so can't be equal
|
||||
return false
|
||||
}
|
||||
if Attr.GetName() != other.GetName() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetName returns this FloatAttribute's human-readable name.
|
||||
func (Attr *FloatAttribute) GetName() string {
|
||||
return Attr.Name
|
||||
return Attr.Name
|
||||
}
|
||||
|
||||
// SetName sets this FloatAttribute's human-readable name.
|
||||
func (Attr *FloatAttribute) SetName(name string) {
|
||||
Attr.Name = name
|
||||
Attr.Name = name
|
||||
}
|
||||
|
||||
// GetType returns Float64Type.
|
||||
func (Attr *FloatAttribute) GetType() int {
|
||||
return Float64Type
|
||||
return Float64Type
|
||||
}
|
||||
|
||||
// String returns a human-readable summary of this Attribute.
|
||||
// e.g. "FloatAttribute(Sepal Width)"
|
||||
func (Attr *FloatAttribute) String() string {
|
||||
return fmt.Sprintf("FloatAttribute(%s)", Attr.Name)
|
||||
return fmt.Sprintf("FloatAttribute(%s)", Attr.Name)
|
||||
}
|
||||
|
||||
// CheckSysValFromString confirms whether a given rawVal can
|
||||
// be converted into a valid system representation. If it can't,
|
||||
// the returned value is nil.
|
||||
func (Attr *FloatAttribute) CheckSysValFromString(rawVal string) ([]byte, error) {
|
||||
f, err := strconv.ParseFloat(rawVal, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := strconv.ParseFloat(rawVal, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := PackFloatToBytes(f)
|
||||
return ret, nil
|
||||
ret := PackFloatToBytes(f)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// GetSysValFromString parses the given rawVal string to a float64 and returns it.
|
||||
@ -82,22 +82,22 @@ func (Attr *FloatAttribute) CheckSysValFromString(rawVal string) ([]byte, error)
|
||||
// IMPORTANT: This function panic()s if rawVal is not a valid float.
|
||||
// Use CheckSysValFromString to confirm.
|
||||
func (Attr *FloatAttribute) GetSysValFromString(rawVal string) []byte {
|
||||
f, err := Attr.CheckSysValFromString(rawVal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
f, err := Attr.CheckSysValFromString(rawVal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// GetFloatFromSysVal converts a given system value to a float
|
||||
func (Attr *FloatAttribute) GetFloatFromSysVal(rawVal []byte) float64 {
|
||||
return UnpackBytesToFloat(rawVal)
|
||||
return UnpackBytesToFloat(rawVal)
|
||||
}
|
||||
|
||||
// GetStringFromSysVal converts a given system value to to a string with two decimal
|
||||
// places of precision.
|
||||
func (Attr *FloatAttribute) GetStringFromSysVal(rawVal []byte) string {
|
||||
f := UnpackBytesToFloat(rawVal)
|
||||
formatString := fmt.Sprintf("%%.%df", Attr.Precision)
|
||||
return fmt.Sprintf(formatString, f)
|
||||
f := UnpackBytesToFloat(rawVal)
|
||||
formatString := fmt.Sprintf("%%.%df", Attr.Precision)
|
||||
return fmt.Sprintf(formatString, f)
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
os.Mkdir("lib", os.ModeDir | 0777)
|
||||
os.Mkdir("lib", os.ModeDir|0777)
|
||||
|
||||
log.Println("Installing libs")
|
||||
if runtime.GOOS == "windows" {
|
||||
|
@ -11,4 +11,4 @@ type FrequencyTableEntry struct {
|
||||
|
||||
func (t *FrequencyTableEntry) String() string {
|
||||
return fmt.Sprintf("%.2f %s", t.Value, t.Frequency)
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package filters
|
||||
|
||||
import (
|
||||
"github.com/sjwhitworth/golearn/base"
|
||||
"fmt"
|
||||
"github.com/sjwhitworth/golearn/base"
|
||||
"math"
|
||||
)
|
||||
|
||||
|
182
filters/float.go
182
filters/float.go
@ -1,8 +1,8 @@
|
||||
package filters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sjwhitworth/golearn/base"
|
||||
"fmt"
|
||||
"github.com/sjwhitworth/golearn/base"
|
||||
)
|
||||
|
||||
// FloatConvertFilters convert a given DataGrid into one which
|
||||
@ -14,84 +14,84 @@ import (
|
||||
// CategoricalAttributes are discretised into one or more new
|
||||
// BinaryAttributes.
|
||||
type FloatConvertFilter struct {
|
||||
attrs []base.Attribute
|
||||
converted []base.FilteredAttribute
|
||||
twoValuedCategoricalAttributes map[base.Attribute]bool // Two-valued categorical Attributes
|
||||
nValuedCategoricalAttributeMap map[base.Attribute]map[uint64]base.Attribute
|
||||
attrs []base.Attribute
|
||||
converted []base.FilteredAttribute
|
||||
twoValuedCategoricalAttributes map[base.Attribute]bool // Two-valued categorical Attributes
|
||||
nValuedCategoricalAttributeMap map[base.Attribute]map[uint64]base.Attribute
|
||||
}
|
||||
|
||||
// NewFloatConvertFilter creates a blank FloatConvertFilter
|
||||
func NewFloatConvertFilter() *FloatConvertFilter {
|
||||
ret := &FloatConvertFilter{
|
||||
make([]base.Attribute, 0),
|
||||
make([]base.FilteredAttribute, 0),
|
||||
make(map[base.Attribute]bool),
|
||||
make(map[base.Attribute]map[uint64]base.Attribute),
|
||||
}
|
||||
return ret
|
||||
ret := &FloatConvertFilter{
|
||||
make([]base.Attribute, 0),
|
||||
make([]base.FilteredAttribute, 0),
|
||||
make(map[base.Attribute]bool),
|
||||
make(map[base.Attribute]map[uint64]base.Attribute),
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// AddAttribute adds a new Attribute to this Filter
|
||||
func (f *FloatConvertFilter) AddAttribute(a base.Attribute) error {
|
||||
f.attrs = append(f.attrs, a)
|
||||
return nil
|
||||
f.attrs = append(f.attrs, a)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAttributesAfterFiltering returns the Attributes previously computed via Train()
|
||||
func (f *FloatConvertFilter) GetAttributesAfterFiltering() []base.FilteredAttribute {
|
||||
return f.converted
|
||||
return f.converted
|
||||
}
|
||||
|
||||
// String gets a human-readable string
|
||||
func (f *FloatConvertFilter) String() string {
|
||||
return fmt.Sprintf("FloatConvertFilter(%d Attribute(s))", len(f.attrs))
|
||||
return fmt.Sprintf("FloatConvertFilter(%d Attribute(s))", len(f.attrs))
|
||||
}
|
||||
|
||||
// Transform converts the given byte sequence using the old Attribute into the new
|
||||
// byte sequence.
|
||||
|
||||
func (f *FloatConvertFilter) Transform(a base.Attribute, n base.Attribute, attrBytes []byte) []byte {
|
||||
ret := make([]byte, 8)
|
||||
// Check for CategoricalAttribute
|
||||
if _, ok := a.(*base.CategoricalAttribute); ok {
|
||||
// Unpack byte value
|
||||
val := base.UnpackBytesToU64(attrBytes)
|
||||
// If it's a two-valued one, check for non-zero
|
||||
if f.twoValuedCategoricalAttributes[a] {
|
||||
if val > 0 {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else if an, ok := f.nValuedCategoricalAttributeMap[a]; ok {
|
||||
// If it's an n-valued one, check the new Attribute maps onto
|
||||
// the unpacked value
|
||||
if af, ok := an[val]; ok {
|
||||
if af.Equals(n) {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else {
|
||||
panic("Categorical value not defined!")
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf("Not a recognised Attribute %v", a))
|
||||
}
|
||||
} else if _, ok := a.(*base.FloatAttribute); ok {
|
||||
// Binary: just return the original value
|
||||
ret = attrBytes
|
||||
} else if _, ok := a.(*base.BinaryAttribute); ok {
|
||||
// Float: check for non-zero
|
||||
if attrBytes[0] > 0 {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf("Unrecognised Attribute: %v", a))
|
||||
}
|
||||
return ret
|
||||
ret := make([]byte, 8)
|
||||
// Check for CategoricalAttribute
|
||||
if _, ok := a.(*base.CategoricalAttribute); ok {
|
||||
// Unpack byte value
|
||||
val := base.UnpackBytesToU64(attrBytes)
|
||||
// If it's a two-valued one, check for non-zero
|
||||
if f.twoValuedCategoricalAttributes[a] {
|
||||
if val > 0 {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else if an, ok := f.nValuedCategoricalAttributeMap[a]; ok {
|
||||
// If it's an n-valued one, check the new Attribute maps onto
|
||||
// the unpacked value
|
||||
if af, ok := an[val]; ok {
|
||||
if af.Equals(n) {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else {
|
||||
panic("Categorical value not defined!")
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf("Not a recognised Attribute %v", a))
|
||||
}
|
||||
} else if _, ok := a.(*base.FloatAttribute); ok {
|
||||
// Binary: just return the original value
|
||||
ret = attrBytes
|
||||
} else if _, ok := a.(*base.BinaryAttribute); ok {
|
||||
// Float: check for non-zero
|
||||
if attrBytes[0] > 0 {
|
||||
ret = base.PackFloatToBytes(1.0)
|
||||
} else {
|
||||
ret = base.PackFloatToBytes(0.0)
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf("Unrecognised Attribute: %v", a))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Train converts the Attributes into equivalently named FloatAttributes,
|
||||
@ -105,37 +105,37 @@ func (f *FloatConvertFilter) Transform(a base.Attribute, n base.Attribute, attrB
|
||||
// If the CategoricalAttribute has more than two (n) values, the Filter
|
||||
// generates n FloatAttributes and sets each of them if the value's observed.
|
||||
func (f *FloatConvertFilter) Train() error {
|
||||
for _, a := range f.attrs {
|
||||
if ac, ok := a.(*base.CategoricalAttribute); ok {
|
||||
vals := ac.GetValues()
|
||||
if len(vals) <= 2 {
|
||||
nAttr := base.NewFloatAttribute(ac.GetName())
|
||||
fAttr := base.FilteredAttribute{ac, nAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
f.twoValuedCategoricalAttributes[a] = true
|
||||
} else {
|
||||
if _, ok := f.nValuedCategoricalAttributeMap[a]; !ok {
|
||||
f.nValuedCategoricalAttributeMap[a] = make(map[uint64]base.Attribute)
|
||||
}
|
||||
for i := uint64(0); i < uint64(len(vals)); i++ {
|
||||
v := vals[i]
|
||||
newName := fmt.Sprintf("%s_%s", ac.GetName(), v)
|
||||
newAttr := base.NewFloatAttribute(newName)
|
||||
fAttr := base.FilteredAttribute{ac, newAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
f.nValuedCategoricalAttributeMap[a][i] = newAttr
|
||||
}
|
||||
}
|
||||
} else if ab, ok := a.(*base.FloatAttribute); ok {
|
||||
fAttr := base.FilteredAttribute{ab, ab}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
} else if af, ok := a.(*base.BinaryAttribute); ok {
|
||||
newAttr := base.NewFloatAttribute(af.GetName())
|
||||
fAttr := base.FilteredAttribute{af, newAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
} else {
|
||||
return fmt.Errorf("Unsupported Attribute type: %v", a)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
for _, a := range f.attrs {
|
||||
if ac, ok := a.(*base.CategoricalAttribute); ok {
|
||||
vals := ac.GetValues()
|
||||
if len(vals) <= 2 {
|
||||
nAttr := base.NewFloatAttribute(ac.GetName())
|
||||
fAttr := base.FilteredAttribute{ac, nAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
f.twoValuedCategoricalAttributes[a] = true
|
||||
} else {
|
||||
if _, ok := f.nValuedCategoricalAttributeMap[a]; !ok {
|
||||
f.nValuedCategoricalAttributeMap[a] = make(map[uint64]base.Attribute)
|
||||
}
|
||||
for i := uint64(0); i < uint64(len(vals)); i++ {
|
||||
v := vals[i]
|
||||
newName := fmt.Sprintf("%s_%s", ac.GetName(), v)
|
||||
newAttr := base.NewFloatAttribute(newName)
|
||||
fAttr := base.FilteredAttribute{ac, newAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
f.nValuedCategoricalAttributeMap[a][i] = newAttr
|
||||
}
|
||||
}
|
||||
} else if ab, ok := a.(*base.FloatAttribute); ok {
|
||||
fAttr := base.FilteredAttribute{ab, ab}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
} else if af, ok := a.(*base.BinaryAttribute); ok {
|
||||
newAttr := base.NewFloatAttribute(af.GetName())
|
||||
fAttr := base.FilteredAttribute{af, newAttr}
|
||||
f.converted = append(f.converted, fAttr)
|
||||
} else {
|
||||
return fmt.Errorf("Unsupported Attribute type: %v", a)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -10,4 +10,4 @@
|
||||
are generated via majority voting.
|
||||
*/
|
||||
|
||||
package meta
|
||||
package meta
|
||||
|
Loading…
x
Reference in New Issue
Block a user