-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-analysis.go
396 lines (364 loc) · 11 KB
/
csv-analysis.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
// This file is part of csv-analysis.
//
// Copyright (C) 2017 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
/*
Package main provides ways to analyse csv data from one or more files and generate basic statistic analysis.
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/DavidGamba/csv-analysis/csvutil"
"github.com/DavidGamba/csv-analysis/regression"
"github.com/DavidGamba/csv-analysis/stat"
"github.com/DavidGamba/go-getoptions"
"gonum.org/v1/gonum/mat"
// "github.com/gonum/optimize"
)
// Usage options
// noHeader - The csv file to be read has no header.
var noHeader bool
// filterZero - Ignore 0 value entries from statistical calculations.
var filterZero bool
// printError - prints the given error to STDERR.
func printError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
}
}
// printCSVColumnStats - Given a column and a set of csv files, it will print the statistical information for that column.
func printCSVColumnStats(files []string, column int) error {
var fieldSliceDataset []float64
for _, file := range files {
cf := csvutil.New(file)
cf.NoHeader = noHeader
cf.FilterZero = filterZero
fs, err := cf.GetFloat64Columns(column)
if err != nil {
return err
}
l := len(fs[0])
if l == 0 {
continue
}
fmt.Printf("Data: %d columns, %v\n", len(fs[0]), fs[0])
fieldSliceDataset = append(fieldSliceDataset, fs[0]...)
}
stat.PrintSliceStats(fieldSliceDataset)
return nil
}
func validateMinInt(min, value int) error {
if value < min {
return fmt.Errorf("can not be less than %d", min)
}
return nil
}
func validateMaxInt(max, value int) error {
if value > max {
return fmt.Errorf("can not be bigger than %d", max)
}
return nil
}
// trimSlice - It will trim the start and end of a slice.
func trimSlice(x []float64, trimStart, trimEnd int) ([]float64, error) {
n := len(x)
err := validateMinInt(0, trimStart)
if err != nil {
return nil, fmt.Errorf("trimStart %s", err)
}
err = validateMaxInt(n-1, trimStart)
if err != nil {
return nil, fmt.Errorf("trimStart %s", err)
}
err = validateMinInt(0, trimEnd)
if err != nil {
return nil, fmt.Errorf("trimEnd %s", err)
}
err = validateMaxInt(n-1, trimEnd)
if err != nil {
return nil, fmt.Errorf("trimEnd %s", err)
}
err = validateMaxInt(n-1, trimStart+trimEnd)
if err != nil {
return nil, fmt.Errorf("trimStart plus trimEnd %s", err)
}
return x[trimStart : n-trimEnd], nil
}
func synopsis() {
synopsis := `csv-analysis --column|-c <n> <csv-file>...
[--no-header|--nh] [--filter-zero|--fz]
# Regression analysis
csv-analysis -x <n> -y <n> <csv-file>...
[--no-header|--nh] [--filter-zero|--fz]
[--trim-start|--ts <n>] [--trim-end|--te <n>]
[--degree] [--regression] [--review]
[--plot-title <title>] [--plot-x-label <label>] [--plot-y-label <label>]
# Time plot
csv-analysis -x <n> -y <n> <csv-file>... -xtime <timeformat>
[--no-header|--nh] [--filter-zero|--fz]
[--trim-start|--ts <n>] [--trim-end|--te <n>]
[--plot-title <title>] [--plot-x-label <label>] [--plot-y-label <label>]
[--bold]
# Inspect data and exit
csv-analysis [--show-header|-s] [--show-data|--sd] <csv-file>...
csv-analysis [--help]
# --column: Column to use for statistical analysis. n starts at 1.
#
# --no-header: The csv file has no header.
# It is assumed that it does by default.
#
# --filter-zero: Ignore zeroes from statistical analysis.
#
# --x, --y: columns to use for X and Y when doing regression analysis.
#
# --trim-start, --trim-end: Trim fields from the CSV dataset.
#
# --degree: polynomial regression degree.
#
# --review: Show linear transformation graphs.
#
# --show-header: Show the header of the first csv file and exit.
#
# --show-data: Show the header and the first row of the first csv file and exit.
#
# --debug: Show debug output.
#
# --xtime: Mon Jan 2 15:04:05.000 MST 2006
# Examples:
# --xtime '2006/01/02 15:04:05.000'
# --xtime '2006-01-02 15:04:05.000'
`
fmt.Fprintln(os.Stderr, synopsis)
}
func main() {
var column, xColumn int // field to analize
var trimStart, trimEnd, degree int
var pTitle, pYLabel, pXLabel string
var xTimeFormat string
var review, bold bool
opt := getoptions.New()
// General options
opt.Bool("help", false)
opt.Bool("debug", false)
// CSV review options
opt.Bool("show-data", false, "sd")
opt.Bool("show-header", false, "s")
// CSV parsing options
opt.BoolVar(&noHeader, "no-header", false, "nh")
opt.BoolVar(&filterZero, "filter-zero", false, "fz")
opt.BoolVar(&review, "review", false)
// CSV data indicators
opt.IntVar(&column, "column", 1, "c")
opt.IntVar(&xColumn, "x", 1)
opt.StringVarOptional(&xTimeFormat, "xtime", time.RFC3339)
yColumns := opt.IntSlice("y", 1, 99)
// CSV data trimming
opt.IntVar(&trimStart, "trim-start", 0, "ts")
opt.IntVar(&trimEnd, "trim-end", 0, "te")
// Linear Regression degree
opt.IntVar(°ree, "degree", 1, "degree")
// Action
opt.Bool("regression", false, "r")
// Plot options
opt.StringVar(&pTitle, "plot-title", "Data", "pt")
opt.StringVar(&pXLabel, "plot-x-label", "", "px")
opt.StringVar(&pYLabel, "plot-y-label", "", "py")
opt.BoolVar(&bold, "bold", false)
remaining, err := opt.Parse(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
if opt.Called("help") {
synopsis()
os.Exit(1)
}
if opt.Called("debug") {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}
log.Println(remaining)
if len(remaining) < 1 {
fmt.Fprintf(os.Stderr, "ERROR: Missing file\n")
os.Exit(1)
}
// Inspect data and quit
if opt.Called("show-header") || opt.Called("show-data") {
var err error
cf := csvutil.New(remaining[0])
cf.NoHeader = noHeader
cf.FilterZero = filterZero
if opt.Called("show-data") {
err = cf.PrintCSVRows(1, 2)
} else {
err = cf.PrintCSVRows(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
os.Exit(1)
}
if opt.Called("x") && opt.Called("y") && opt.Called("xtime") {
cf := csvutil.New(remaining...)
cf.NoHeader = noHeader
cf.FilterZero = filterZero
sliceDatasetsString, err := cf.GetCSVColumns(xColumn)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
var xSliceDataset []float64
for _, e := range sliceDatasetsString[0] {
trimmed := strings.TrimSpace(e)
trimmedXTimeFormat := strings.TrimSpace(xTimeFormat)
t, err := time.Parse(trimmedXTimeFormat, trimmed)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: time format '%s': %s\n", xTimeFormat, err)
continue
}
xSliceDataset = append(xSliceDataset, float64(t.Unix()))
}
xTrimmed, err := trimSlice(xSliceDataset, trimStart, trimEnd)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
sliceDatasets, err := cf.GetFloat64Columns(*yColumns...)
// TODO: Add error to check for different column lenghts
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
var sYTrimmed [][]float64
for i, ySliceDataset := range sliceDatasets {
yTrimmed, _ := trimSlice(ySliceDataset, trimStart, trimEnd)
if len(yTrimmed) < 1 {
fmt.Fprintf(os.Stderr, "ERROR: column '%d' is empty. Removing it!\n", i)
} else {
sYTrimmed = append(sYTrimmed, yTrimmed)
}
}
// TODO: maybe show this only with verbose option
// fmt.Printf("Column X (%d): %v\n", xColumn, xTrimmed)
// fmt.Printf("Column Y (%v): %v\n", *yColumns, sYTrimmed)
fmt.Printf("Count: %d, Trim Start: %d, Trim End: %d\n", len(xTrimmed), trimStart, trimEnd)
regression.PlotTimeData(xTrimmed, sYTrimmed, regression.PlotSettings{
Title: pTitle,
XLabel: pXLabel,
YLabel: pYLabel,
Bold: bold,
})
err = printCSVColumnStats(remaining, (*yColumns)[0])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
} else if opt.Called("x") && opt.Called("y") {
cf := csvutil.New(remaining...)
cf.NoHeader = noHeader
cf.FilterZero = filterZero
query := []int{xColumn}
query = append(query, (*yColumns)...)
sliceDatasets, err := cf.GetFloat64Columns(query...)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
xTrimmed, err := trimSlice(sliceDatasets[0], trimStart, trimEnd)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
var sYTrimmed [][]float64
for _, ySliceDataset := range sliceDatasets[1:] {
yTrimmed, _ := trimSlice(ySliceDataset, trimStart, trimEnd)
sYTrimmed = append(sYTrimmed, yTrimmed)
}
// TODO: maybe show this only with verbose option
fmt.Printf("Column X (%d): %v\n", xColumn, xTrimmed)
fmt.Printf("Column Y (%v): %v\n", *yColumns, sYTrimmed)
fmt.Printf("Count: %d, Trim Start: %d, Trim End: %d\n", len(xTrimmed), trimStart, trimEnd)
regression.PlotRegression(xTrimmed, sYTrimmed, func(x float64) float64 { return x }, 0, 0, regression.PlotSettings{
Title: pTitle,
XLabel: pXLabel,
YLabel: pYLabel,
DataLabel: pTitle,
})
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
if !opt.Called("regression") {
os.Exit(0)
}
// Original data
solution, err := regression.SolveTransformation(xTrimmed, sYTrimmed[0], ®ression.None{})
if err == nil {
err = solution.Plot(®ression.None{})
printError(err)
} else {
printError(err)
}
ltList := []interface{}{
// Exp: y = aB^x | log y = log a + log B * x
®ression.Exponential{},
// Power: y = ax^b -> log y = log a + b * log x
®ression.Power{},
// y = ln(ax^b) | y = ln a + b * ln x
®ression.LnPower{},
// y = 1 / (a + bx) | 1/y = a + bx
®ression.OneOverX{},
// y = a + b / (1 + x) | y = a + b * 1/(1+x)
®ression.BOverX{},
// y = 1 / (a + bx)^2 | 1/sqrt(y) = a + bx
®ression.OneOverX2{},
// y = a + b * sqrt(x) | a + b * sqrt(x)
®ression.Sqrt{},
}
for _, lt := range ltList {
solution, err = regression.SolveTransformation(
xTrimmed, sYTrimmed[0], lt.(regression.LinearTransformation))
if err == nil {
if review {
err = solution.PlotLinearTransformation(lt.(regression.Plotter))
printError(err)
}
err = solution.Plot(lt.(regression.Plotter))
printError(err)
} else {
printError(err)
}
}
s, err := regression.SolvePolynomial(xTrimmed, sYTrimmed[0], degree)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
log.Printf("S:\n%3.3g\n", mat.Formatted(s.A, mat.Prefix(""), mat.Squeeze()))
// si, err := regression.SolvePolynomialReverseMatrix(xSliceDataset, ySliceDataset, degree)
// if err != nil {
// fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
// os.Exit(1)
// }
// log.Printf("S (matrix):\n%3.3g\n", mat.Formatted(si.A, mat.Prefix(""), mat.Squeeze()))
s.Plot()
} else {
// Get column stats
err := printCSVColumnStats(remaining, column)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
}