123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package stats
- import (
- "strconv"
- "time"
- )
- // LoadRawData parses and converts a slice of mixed data types to floats
- func LoadRawData(raw interface{}) (f Float64Data) {
- var r []interface{}
- var s Float64Data
- switch t := raw.(type) {
- case []interface{}:
- r = t
- case []uint:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []uint8:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []uint16:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []uint32:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []uint64:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []bool:
- for _, v := range t {
- if v == true {
- s = append(s, 1.0)
- } else {
- s = append(s, 0.0)
- }
- }
- return s
- case []float64:
- return Float64Data(t)
- case []int:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []int8:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []int16:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []int32:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []int64:
- for _, v := range t {
- s = append(s, float64(v))
- }
- return s
- case []string:
- for _, v := range t {
- r = append(r, v)
- }
- case []time.Duration:
- for _, v := range t {
- r = append(r, v)
- }
- case map[int]int:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]int8:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]int16:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]int32:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]int64:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]string:
- for i := 0; i < len(t); i++ {
- r = append(r, t[i])
- }
- case map[int]uint:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]uint8:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]uint16:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]uint32:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]uint64:
- for i := 0; i < len(t); i++ {
- s = append(s, float64(t[i]))
- }
- return s
- case map[int]bool:
- for i := 0; i < len(t); i++ {
- if t[i] == true {
- s = append(s, 1.0)
- } else {
- s = append(s, 0.0)
- }
- }
- return s
- case map[int]float64:
- for i := 0; i < len(t); i++ {
- s = append(s, t[i])
- }
- return s
- case map[int]time.Duration:
- for i := 0; i < len(t); i++ {
- r = append(r, t[i])
- }
- }
- for _, v := range r {
- switch t := v.(type) {
- case int:
- a := float64(t)
- f = append(f, a)
- case uint:
- f = append(f, float64(t))
- case float64:
- f = append(f, t)
- case string:
- fl, err := strconv.ParseFloat(t, 64)
- if err == nil {
- f = append(f, fl)
- }
- case bool:
- if t == true {
- f = append(f, 1.0)
- } else {
- f = append(f, 0.0)
- }
- case time.Duration:
- f = append(f, float64(t))
- }
- }
- return f
- }
|