/
utils.go
436 lines (392 loc) · 9.68 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package gambas
import (
"encoding/csv"
"fmt"
"math"
"os"
"sort"
"strconv"
)
func checkTypeIntegrity(data []interface{}) (string, error) {
determinant := 0
isBool := 0
isInt := 0
isFloat64 := 0
isString := 0
dtype := ""
emptyValLocations := make([]int, 0)
for i, d := range data {
if d == "" {
emptyValLocations = append(emptyValLocations, i)
continue
}
switch d.(type) {
case bool:
isBool = 1
case int:
isInt = 2
case float64:
isFloat64 = 4
case string:
isString = 8
}
}
determinant = isBool + isInt + isFloat64 + isString
switch determinant {
case 1:
dtype = "bool"
case 2:
if len(emptyValLocations) > 0 {
dtype = "float64"
} else {
dtype = "int"
}
case 4:
dtype = "float64"
case 8:
dtype = "string"
case 6:
dtype = "float64"
case 0:
if len(emptyValLocations) == len(data) {
dtype = "string"
} else {
return "", fmt.Errorf("invalid data type; data type should be either bool, int, float64, or string")
}
default:
dtype = "string"
}
return dtype, nil
// isFloat64 := 0
// isString := 0
// isNil := 0
// for _, v := range data {
// switch t := v.(type) {
// case float64:
// if math.IsNaN(t) {
// continue
// }
// isFloat64 = 1
// case string:
// isString = 1
// case nil:
// isNil = 1
// default:
// _, err := i2f(v)
// if err != nil {
// return false, fmt.Errorf("invalid type: %T", t)
// } else {
// isFloat64 = 1
// }
// }
// if isFloat64+isString+isNil > 1 {
// return false, nil
// } else if isFloat64+isString+isNil == 0 {
// panic("type not detected")
// }
// }
// return true, nil
}
func i2f(data interface{}) (float64, error) {
var x float64
switch v := data.(type) {
case int:
x = float64(v)
case int8:
x = float64(v)
case int16:
x = float64(v)
case int32:
x = float64(v)
case int64:
x = float64(v)
case uint:
x = float64(v)
case uint8:
x = float64(v)
case uint16:
x = float64(v)
case uint32:
x = float64(v)
case uint64:
x = float64(v)
case float32:
x = float64(v)
case float64:
x = v
default:
return 0.0, fmt.Errorf("%v is not a number", data)
}
return x, nil
}
// tryBool checks if a string can be converted into bool.
// tryBool only accepts "TRUE", "True", "true", and "FALSE", "False", "false".
func tryBool(data string) (bool, error) {
ignored := []string{"1", "t", "T", "0", "f", "F"}
if containsString(ignored, data) {
return false, fmt.Errorf("ignored string")
}
b, err := strconv.ParseBool(data)
if err != nil {
return false, err
}
return b, nil
}
// tryInt checks if a string can be converted into int.
func tryInt(data string) (int, error) {
s, err := strconv.Atoi(data)
if err != nil {
return 0, err
}
return s, nil
}
// tryFloat64 checks if a string can be converted into float64.
func tryFloat64(data string) (float64, error) {
if data == "" || data == "NaN" {
return math.NaN(), nil
}
f, err := strconv.ParseFloat(data, 64)
if err != nil {
return 0, err
}
return f, nil
}
// tryDataType accepts a string and tries to convert it to the correct data type.
// It will try to convert the data into a bool, then int, then float64, and finally string.
func tryDataType(data string) interface{} {
b, err := tryBool(data)
if err != nil {
i, err := tryInt(data)
if err != nil {
f, err := tryFloat64(data)
if err != nil {
return data
}
return f
}
return i
}
return b
}
// checkType checks to see if the data can be represented as a float64.
// Because CSV is read as an array of strings, there has to be a way to check the type.
func checkCSVDataType(data string) interface{} {
if data == "" {
return math.NaN()
}
v, ok := strconv.ParseFloat(data, 64)
switch ok {
case nil:
return v
default:
return data
}
}
// checkJsonDataType checks for JSON null data, and converts it into math.NaN().
func checkJsonDataType(data interface{}) interface{} {
if data == nil {
return math.NaN()
}
return data
}
// consolidateToFloat64 consolidates all data in an []interface{} to float64.
// This is necessary to convert empty string values into math.NaN().
// In order to stay compatible with Series.data,
// the data type of the slice is still an empty interface.
func consolidateToFloat64(data []interface{}) []interface{} {
result := make([]interface{}, len(data))
for i, d := range data {
switch dd := d.(type) {
case float64:
result[i] = dd
case int:
result[i] = float64(dd)
case string:
if dd == "" || dd == "NaN" {
result[i] = math.NaN()
} else {
f, err := strconv.ParseFloat(fmt.Sprint(d), 64)
if err != nil {
panic("consolidateToFloat64 should be called only when data is in the form of float64")
}
result[i] = f
}
}
}
return result
}
// consolidateToString consolidates all data in an []interface{} to string.
// In order to stay compatible with Series.data,
// the data type of the slice is still an empty interface.
func consolidateToString(data []interface{}) []interface{} {
result := make([]interface{}, len(data))
for i, d := range data {
if d == "" || d == "NaN" {
result[i] = math.NaN()
} else if conv, ok := d.(float64); ok && math.IsNaN(conv) {
result[i] = math.NaN()
} else {
result[i] = fmt.Sprint(d)
}
}
return result
}
// interface2F64Data() converts a slice of interface{} into F64Data.
func interface2F64Slice(data []interface{}) ([]float64, error) {
fd := make([]float64, 0)
for _, v := range data {
switch converted := v.(type) {
case float64:
if math.IsNaN(converted) {
continue
}
fd = append(fd, converted)
// case int:
// fd = append(fd, float64(converted))
default:
return nil, fmt.Errorf("data is not a float64: %v", v)
}
}
return fd, nil
}
// interface2StringData() converts a slice of interface{} into StringData.
func interface2StringSlice(data []interface{}) ([]string, error) {
sd := make([]string, 0)
for _, v := range data {
switch converted := v.(type) {
case string:
sd = append(sd, converted)
default:
return nil, fmt.Errorf("data is not a string: %v", v)
}
}
return sd, nil
}
// slicesAreEqual checks whether two slices are equal.
func slicesAreEqual(slice1, slice2 []interface{}) bool {
if len(slice1) != len(slice2) {
return false
}
for i, v := range slice1 {
if v != slice2[i] {
return false
}
}
return true
}
// containsString checks whether a string exists in a slice of strings.
func containsString(strSlice []string, str string) bool {
for _, data := range strSlice {
if data == str {
return true
}
}
return false
}
// containsIndex checks whether an Index object exists in a slice of Index objects.
// For a lenient comaprison that checks for values only, use containsIndexWithoutId.
func containsIndex(indexSlice []Index, index Index) bool {
for _, data := range indexSlice {
if slicesAreEqual(data.value, index.value) && (data.id == index.id) {
return true
}
}
return false
}
// containsIndex checks whether an Index object exists in a slice of Index objects.
// For a strict comaprison that checks for id as well, use containsIndex.
func containsIndexWithoutId(indexSlice []Index, index Index) bool {
for _, data := range indexSlice {
if slicesAreEqual(data.value, index.value) {
return true
}
}
return false
}
// containsSlice checks whether a slice of interface{} exists in a slice of []interface{}.
func containsSlice(s1 [][]interface{}, s2 []interface{}) bool {
for _, data := range s1 {
if slicesAreEqual(data, s2) {
return true
}
}
return false
}
// Summary statistics functions (internal use only)
// median() returns the median of the elements in an array.
func median(data []float64) (float64, error) {
median := 0.0
sort.Float64s(data)
total := len(data)
if total == 0 {
return math.NaN(), fmt.Errorf("no elements in this column")
}
if total%2 == 0 {
lower := data[total/2-1]
upper := data[total/2]
median = (lower + upper) / 2
} else {
median := data[(total+1)/2-1]
return median, nil
}
return median, nil
}
// copyDf takes a source DataFrame and returns a copy of it with different memory address.
func copyDf(src *DataFrame) DataFrame {
newDf := new(DataFrame)
newDf.series = make([]Series, len(src.series))
for i, ser := range src.series {
newDf.series[i].data = append(newDf.series[i].data, ser.data...)
newDf.series[i].index.index = append(newDf.series[i].index.index, ser.index.index...)
newDf.series[i].index.names = append(newDf.series[i].index.names, ser.index.names...)
newDf.series[i].name = ser.name
newDf.series[i].dtype = ser.dtype
}
newDf.index.index = append(newDf.index.index, src.index.index...)
newDf.index.names = append(newDf.index.names, src.index.names...)
newDf.columns = append(newDf.columns, src.columns...)
return *newDf
}
// readCsvColIntoData extracts a column in a CSV file to a [][]interface{}.
func readCsvColIntoData(filepath string, col string) ([][]interface{}, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
csvr := csv.NewReader(f)
rows, err := csvr.ReadAll()
if err != nil {
return nil, err
}
res := make([][]interface{}, 0)
colIndex := 0
for i, colLabel := range rows[0] {
if colLabel == col {
colIndex = i
break
}
}
for i, row := range rows {
if i == 0 {
continue
}
res = append(res, []interface{}{row[colIndex]})
}
return res, nil
}
// generateAlphabets will generate alphabets based on i.
// This is mostly used to dynamically create column labels for excel sheets.
// For example, if i=1 then it will return A.
// i=25, Z. i=26, AA. And vice versa...
func generateAlphabets(i int) string {
i--
result := ""
if first := i / 26; first > 0 {
result += generateAlphabets(first)
result += string(rune('A' + i%26))
} else {
result += string(rune('A' + i))
}
return result
}