-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
72 lines (58 loc) · 1.54 KB
/
io.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
package olsgo
// resourced from: https://www.geeksforgeeks.org/how-to-read-a-csv-file-in-golang/
import (
"encoding/csv"
"fmt"
"os"
"strconv"
)
func (o *ols) Format() string {
fs := "OLSGO Output \n"
fs += fmt.Sprintf("Intercept: %-15.4f", o.intercept)
fs += fmt.Sprintf("B1: %-15.4f", o.b1)
fs += fmt.Sprintf("Pearson's r: %-15.4f", o.r)
fs += fmt.Sprintf("Residual variance: %-15.4f", o.R2)
return fs
}
func (o *ols) Save(n string) {
data := []byte(o.Format())
err := os.WriteFile(n+".txt", data, 0666)
if err != nil {
panic(err)
}
fmt.Println("Output saved.")
}
// read .csv file and returns a map where key-val pairs are header and observation data
func LoadCSV(filename string) (map[string][]float64, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("could not open file %s: %v", filename, err)
}
defer file.Close()
reader := csv.NewReader(file)
headers, err := reader.Read()
if err != nil {
return nil, fmt.Errorf("could not read headers: %v", err)
}
result := make(map[string][]float64)
for _, header := range headers {
result[header] = []float64{}
}
for {
record, err := reader.Read()
if err != nil {
if err.Error() == "EOF" {
break
}
return nil, fmt.Errorf("could not read record: %v", err)
}
for i, value := range record {
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, fmt.Errorf("could not parse value %s: %v", value, err)
}
result[headers[i]] = append(result[headers[i]], floatValue)
}
}
return result, nil
}