load.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package stats
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. // LoadRawData parses and converts a slice of mixed data types to floats
  7. func LoadRawData(raw interface{}) (f Float64Data) {
  8. var r []interface{}
  9. var s Float64Data
  10. switch t := raw.(type) {
  11. case []interface{}:
  12. r = t
  13. case []uint:
  14. for _, v := range t {
  15. s = append(s, float64(v))
  16. }
  17. return s
  18. case []uint8:
  19. for _, v := range t {
  20. s = append(s, float64(v))
  21. }
  22. return s
  23. case []uint16:
  24. for _, v := range t {
  25. s = append(s, float64(v))
  26. }
  27. return s
  28. case []uint32:
  29. for _, v := range t {
  30. s = append(s, float64(v))
  31. }
  32. return s
  33. case []uint64:
  34. for _, v := range t {
  35. s = append(s, float64(v))
  36. }
  37. return s
  38. case []bool:
  39. for _, v := range t {
  40. if v == true {
  41. s = append(s, 1.0)
  42. } else {
  43. s = append(s, 0.0)
  44. }
  45. }
  46. return s
  47. case []float64:
  48. return Float64Data(t)
  49. case []int:
  50. for _, v := range t {
  51. s = append(s, float64(v))
  52. }
  53. return s
  54. case []int8:
  55. for _, v := range t {
  56. s = append(s, float64(v))
  57. }
  58. return s
  59. case []int16:
  60. for _, v := range t {
  61. s = append(s, float64(v))
  62. }
  63. return s
  64. case []int32:
  65. for _, v := range t {
  66. s = append(s, float64(v))
  67. }
  68. return s
  69. case []int64:
  70. for _, v := range t {
  71. s = append(s, float64(v))
  72. }
  73. return s
  74. case []string:
  75. for _, v := range t {
  76. r = append(r, v)
  77. }
  78. case []time.Duration:
  79. for _, v := range t {
  80. r = append(r, v)
  81. }
  82. case map[int]int:
  83. for i := 0; i < len(t); i++ {
  84. s = append(s, float64(t[i]))
  85. }
  86. return s
  87. case map[int]int8:
  88. for i := 0; i < len(t); i++ {
  89. s = append(s, float64(t[i]))
  90. }
  91. return s
  92. case map[int]int16:
  93. for i := 0; i < len(t); i++ {
  94. s = append(s, float64(t[i]))
  95. }
  96. return s
  97. case map[int]int32:
  98. for i := 0; i < len(t); i++ {
  99. s = append(s, float64(t[i]))
  100. }
  101. return s
  102. case map[int]int64:
  103. for i := 0; i < len(t); i++ {
  104. s = append(s, float64(t[i]))
  105. }
  106. return s
  107. case map[int]string:
  108. for i := 0; i < len(t); i++ {
  109. r = append(r, t[i])
  110. }
  111. case map[int]uint:
  112. for i := 0; i < len(t); i++ {
  113. s = append(s, float64(t[i]))
  114. }
  115. return s
  116. case map[int]uint8:
  117. for i := 0; i < len(t); i++ {
  118. s = append(s, float64(t[i]))
  119. }
  120. return s
  121. case map[int]uint16:
  122. for i := 0; i < len(t); i++ {
  123. s = append(s, float64(t[i]))
  124. }
  125. return s
  126. case map[int]uint32:
  127. for i := 0; i < len(t); i++ {
  128. s = append(s, float64(t[i]))
  129. }
  130. return s
  131. case map[int]uint64:
  132. for i := 0; i < len(t); i++ {
  133. s = append(s, float64(t[i]))
  134. }
  135. return s
  136. case map[int]bool:
  137. for i := 0; i < len(t); i++ {
  138. if t[i] == true {
  139. s = append(s, 1.0)
  140. } else {
  141. s = append(s, 0.0)
  142. }
  143. }
  144. return s
  145. case map[int]float64:
  146. for i := 0; i < len(t); i++ {
  147. s = append(s, t[i])
  148. }
  149. return s
  150. case map[int]time.Duration:
  151. for i := 0; i < len(t); i++ {
  152. r = append(r, t[i])
  153. }
  154. }
  155. for _, v := range r {
  156. switch t := v.(type) {
  157. case int:
  158. a := float64(t)
  159. f = append(f, a)
  160. case uint:
  161. f = append(f, float64(t))
  162. case float64:
  163. f = append(f, t)
  164. case string:
  165. fl, err := strconv.ParseFloat(t, 64)
  166. if err == nil {
  167. f = append(f, fl)
  168. }
  169. case bool:
  170. if t == true {
  171. f = append(f, 1.0)
  172. } else {
  173. f = append(f, 0.0)
  174. }
  175. case time.Duration:
  176. f = append(f, float64(t))
  177. }
  178. }
  179. return f
  180. }