-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocketIO.go
More file actions
130 lines (124 loc) · 3.21 KB
/
rocketIO.go
File metadata and controls
130 lines (124 loc) · 3.21 KB
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
package rocketc
import (
"bufio"
"encoding/csv"
"fmt"
"os"
"strconv"
"strings"
)
// ReadCSVMatrix : Use this function to read CSV if you are sure that CSV
// file only contains parsable numerical values (float64).Takes a string filename
// and a boolean whether to drop first row or not. Returns a Matrix.
// Note : Drop first row if it contains name of columns.
func ReadCSVMatrix(fname string, dropFirst bool) (Matrix, error) {
var data Matrix
var f, err = os.Open(fname)
if err != nil {
return nil, err
}
defer f.Close()
var converter = func(s []string) []float32 {
var temp = make([]float32, len(s))
for index, value := range s {
t, err := strconv.ParseFloat(value, 64)
if err != nil {
fmt.Println(err.Error())
return nil
}
temp[index] = float32(t)
}
return temp
}
var scanner = bufio.NewScanner(f)
if dropFirst {
scanner.Scan()
_ = scanner.Text()
}
for scanner.Scan() {
row := strings.Split(scanner.Text(), ",")
list := converter(row)
data = append(data, list)
}
return data, nil
}
// ReadCSVDataFrame : Use this function to read CSV file completely.
// Takes a string filename. Returns DataFrame and not nil error value in case
// of any error occurred. Currently ReadCSVDataFrame can only read matrices
// which do not have multiple values in single column.
// Note : Use this function if data contains both numeric and string values.
func ReadCSVDataFrame(fname string) (DataFrame, error) {
var f, err = os.Open(fname)
if err != nil {
return nil, err
}
defer f.Close()
reader := csv.NewReader(f)
return reader.ReadAll()
}
// WriteCSVMatrix : Writes Matrix into a file, so that Matrix can be
// saved to disk for furthur. Takes a Matrix, slice of strings which are
// headers and filename as arguments. Returns an error value in case of
// any error occurred.
// Note : This function can only write Matrix to file.
func WriteCSVMatrix(m Matrix, fname string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
writerF := func(value []float32, writer *bufio.Writer) error {
var str string
for col, i := range value {
str = str + strconv.FormatFloat(float64(i), 'f', 6, 64)
if col != m.Cols()-1 {
str = str + ","
}
}
str = str + "\n"
_, err = writer.WriteString(str)
if err != nil {
return err
}
writer.Flush()
str = ""
return nil
}
var writer = bufio.NewWriter(f)
for _, value := range m {
err = writerF(value, writer) // start concatenation in empty string.
if err != nil {
return err
}
}
return err
}
// WriteCSVDataFrame : Writes DataFrame into a file, so that DataFrame can be
// saved to disk for furthur. Takes a DataFrame and filename as
// arguments. Returns an error value in case of any error occurred.
// Note : This function can only write DataFrame to file.
func WriteCSVDataFrame(d DataFrame, fname string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
var str string
var writer = bufio.NewWriter(f)
for _, value := range d {
for col, i := range value {
str = str + i
if col != d.Cols()-1 {
str = str + ","
}
}
str = str + "\n"
_, err = writer.WriteString(str)
if err != nil {
return err
}
writer.Flush()
str = ""
}
return err
}