-
Notifications
You must be signed in to change notification settings - Fork 29
/
stats.go
121 lines (100 loc) · 2.17 KB
/
stats.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
package main
import (
"encoding/csv"
"fmt"
"math"
"os"
"slices"
"sort"
"strconv"
)
func readFile(filepath string) ([]float64, error) {
_, err := os.Stat(filepath)
if err != nil {
return nil, err
}
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
lines, err := csv.NewReader(f).ReadAll()
if err != nil {
return nil, err
}
values := make([]float64, 0)
for _, line := range lines {
tmp, err := strconv.ParseFloat(line[0], 64)
if err != nil {
fmt.Println("Error reading:", line[0], err)
continue
}
values = append(values, tmp)
}
return values, nil
}
func stdDev(x []float64) (float64, float64) {
sum := float64(0)
for _, val := range x {
sum = sum + val
}
meanValue := sum / float64(len(x))
fmt.Printf("Mean value: %.5f\n", meanValue)
// Standard deviation
var squared float64
for i := 0; i < len(x); i++ {
squared = squared + math.Pow((x[i]-meanValue), 2)
}
standardDeviation := math.Sqrt(squared / float64(len(x)))
return meanValue, standardDeviation
}
type DataFile struct {
Filename string
Len int
Minimum float64
Maximum float64
Mean float64
StdDev float64
}
type DFslice []DataFile
// Implement sort.Interface
func (a DFslice) Len() int {
return len(a)
}
func (a DFslice) Less(i, j int) bool {
return a[i].Mean < a[j].Mean
}
func (a DFslice) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func main() {
if len(os.Args) == 1 {
fmt.Println("Need one or more file paths!")
return
}
// Slice of DataFile structures
files := DFslice{}
for i := 1; i < len(os.Args); i++ {
file := os.Args[i]
currentFile := DataFile{}
currentFile.Filename = file
values, err := readFile(file)
if err != nil {
fmt.Println("Error reading:", file, err)
os.Exit(0)
}
currentFile.Len = len(values)
currentFile.Minimum = slices.Min(values)
currentFile.Maximum = slices.Max(values)
meanValue, standardDeviation := stdDev(values)
currentFile.Mean = meanValue
currentFile.StdDev = standardDeviation
files = append(files, currentFile)
}
// Now sort files
sort.Sort(files)
for _, val := range files {
f := val.Filename
fmt.Println(f,":",val.Len,val.Mean,val.Maximum,val.Minimum)
}
}