Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Sep 1, 2024
1 parent f19bd73 commit e9d1599
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 25 deletions.
15 changes: 10 additions & 5 deletions datalist.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type IDataList interface {
FindFirst(interface{}) interface{}
FindLast(interface{}) interface{}
FindAll(interface{}) []int
Filter(func(interface{}) bool) []interface{}
Filter(func(interface{}) bool) *DataList
ReplaceFirst(interface{}, interface{})
ReplaceLast(interface{}, interface{})
ReplaceAll(interface{}, interface{})
Expand Down Expand Up @@ -87,9 +87,14 @@ func (dl *DataList) Data() []interface{} {
// NewDataList creates a new DataList, supporting both slice and variadic inputs,
// and flattens the input before storing it.
func NewDataList(values ...interface{}) *DataList {
flatData, _ := sliceutil.Flatten[interface{}](values)
var flatData []interface{}

flatData, _ = sliceutil.Flatten[interface{}](values)
LogDebug("NewDataList(): Flattened data:", flatData)

continuousMemData := make([]interface{}, len(flatData))
copy(continuousMemData, flatData)

dl := &DataList{
data: continuousMemData,
creationTimestamp: time.Now().Unix(),
Expand Down Expand Up @@ -869,8 +874,8 @@ func (dl *DataList) Mean() interface{} {
return nil
}
}

return sum / float64(len(dl.data))
mean := sum / float64(len(dl.data))
return mean
}

// GMean calculates the geometric mean of the DataList.
Expand Down Expand Up @@ -942,7 +947,7 @@ func (dl *DataList) Mode() interface{} {
}
}

return mode
return mode.(float64)
}

// Stdev calculates the standard deviation(sample) of the DataList.
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ module github.com/HazelnutParadise/insyra
go 1.21.6

require (
github.com/HazelnutParadise/Go-Utils v0.7.8
github.com/HazelnutParadise/Go-Utils v0.7.9
github.com/fatih/color v1.17.0
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
golang.org/x/text v0.17.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ github.com/HazelnutParadise/Go-Utils v0.7.7 h1:KSBcUuinXARkD06AvBBrLts64kraRWEP3
github.com/HazelnutParadise/Go-Utils v0.7.7/go.mod h1:JzuH5U+UNgAQzzotFf6k4zoJiOkBPyWPK7JTJrheYas=
github.com/HazelnutParadise/Go-Utils v0.7.8 h1:MKck/Yi68Y5nk8sgnHEz1AvNq41qeShFI/Ugv61L03A=
github.com/HazelnutParadise/Go-Utils v0.7.8/go.mod h1:JzuH5U+UNgAQzzotFf6k4zoJiOkBPyWPK7JTJrheYas=
github.com/HazelnutParadise/Go-Utils v0.7.9 h1:r4fSsuYa3gdShNV+gr8OUF/LsYFN++2OTKWPmht6gmk=
github.com/HazelnutParadise/Go-Utils v0.7.9/go.mod h1:JzuH5U+UNgAQzzotFf6k4zoJiOkBPyWPK7JTJrheYas=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
44 changes: 29 additions & 15 deletions stats/skew.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,77 @@
package stats

import (
"fmt"

"github.com/HazelnutParadise/insyra"
)

// Skew calculates the skewness(sample) of the DataList.
// Returns the skewness.
// Returns nil if the DataList is empty or the skewness cannot be calculated.
// 錯誤!
func Skew(data []interface{}, method ...string) interface{} {
d, dLen := insyra.ProcessData(data)
func Skew(sample interface{}, method ...string) interface{} {
d, dLen := insyra.ProcessData(sample)
d64 := insyra.SliceToF64(d)
insyra.LogDebug("stats.Skew(): d64: ", d64)
dl := insyra.NewDataList(d64)
insyra.LogDebug("stats.Skew(): dl: ", dl)

methodStr := "pearson"
if len(method) > 0 {
methodStr = method[0]
}
if len(method) > 1 {
fmt.Println("[insyra] DataList.Skew(): Too many arguments, returning nil.")
insyra.LogWarning("stats.Skew(): More than one method specified, using the first one.")
return nil
}
if dLen == 0 {
fmt.Println("[insyra] DataList.Skew(): DataList is empty, returning nil.")
insyra.LogWarning("stats.Skew(): DataList is empty, returning nil.")
return nil
}

var result interface{}
switch methodStr {
case "pearson":
result = calculateSkewPearson(d64)
result = calculateSkewPearson(dl)
case "moments":
result = calculateSkewMoments(d64)
result = calculateSkewMoments(dl)
default:
fmt.Println("[insyra] DataList.Skew(): Invalid method, returning nil.")
insyra.LogWarning("stats.Skew(): Invalid method, returning nil.")
return nil
}

if result == nil {
fmt.Println("[insyra] DataList.Skew(): Skewness calculation failed, returning nil.")
insyra.LogWarning("stats.Skew(): Skewness is nil, returning nil.")
return nil
}
resultFloat, ok := result.(float64)
if !ok {
fmt.Println("[insyra] DataList.Skew(): Skewness is not a float64, returning nil.")
insyra.LogWarning("stats.Skew(): Skewness is not a float64, returning nil.")
return nil
}
return resultFloat
}

// ======================== calculation functions ========================
func calculateSkewPearson(data []float64) interface{} {
// todo
var result float64
func calculateSkewPearson(sample insyra.IDataList) interface{} {
mean := sample.Mean()
median := sample.Median()
if mean == nil || median == nil {
insyra.LogWarning("DataList.Skew(): Mean or median is nil, returning nil.")
return nil
}
numerator := 3 * (mean.(float64) - median.(float64))
denominator := sample.Stdev().(float64)
if denominator == 0.0 {
insyra.LogWarning("DataList.Skew(): Denominator is 0, returning nil.")
return nil
}

result := numerator / denominator

return result
}

func calculateSkewMoments(data []float64) interface{} {
func calculateSkewMoments(sample insyra.IDataList) interface{} {
// todo
var result float64
return result
Expand Down
13 changes: 9 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package insyra

import (
"fmt"
"github.com/HazelnutParadise/Go-Utils/conv"
)

// ToFloat64 converts any numeric value to float64.
Expand Down Expand Up @@ -47,10 +47,15 @@ func ToFloat64Safe(v interface{}) (float64, bool) {
}

func SliceToF64(data []interface{}) []float64 {
defer func() {
if r := recover(); r != nil {
LogWarning("SliceToF64(): Failed to convert data to float64")
}
}()
var floatSlice []float64
for _, v := range data {
v = ToFloat64(v)
floatSlice = append(floatSlice, v.(float64)) // 將 interface{} 轉換為 float64
f64v := conv.ParseF64(v) // 將 interface{} 轉換為 float64
floatSlice = append(floatSlice, f64v) // 將 interface{} 轉換為 float64
}

return floatSlice
Expand All @@ -69,7 +74,7 @@ func ProcessData(input interface{}) ([]interface{}, int) {
case []interface{}:
data = v
default:
fmt.Println("Unsupported data type")
LogWarning("ProcessData(): Unsupported data type %T, returning nil.", input)
return nil, 0
}

Expand Down

0 comments on commit e9d1599

Please sign in to comment.