-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot.go
198 lines (169 loc) · 4.99 KB
/
plot.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
package gambas
import (
"bytes"
"fmt"
"math/rand"
"os/exec"
"path/filepath"
"time"
)
// Plotting functionality uses gnuplot as its backend.
// A PlotData holds the data required for plotting.
//
// If you want to plot an arbitrary function, leave Df and Columns as nil.
// Otherwise, populate Df and Columns, and leave Function as "".
type PlotData struct {
// Df is the DataFrame object you would like to plot.
Df *DataFrame
// Columns are the columns in Df that you want to plot. Usually, it's a pair of columns [xcol, ycol].
// If you want to create a bar graph or a histogram, you can add more columns.
Columns []string
// Function is an arbitrary function such as sin(x) or an equation of the line of best fit.
Function string
// Opts are options such as `using` or `with`. `set` is passed in as an argument for other plotting functions.
Opts []GnuplotOpt
}
// Plot plots a set of data given by the PlotData object `pd`.
//
// Pass in any `set` options you need. Refer to the [gnuplot documentation] for `set` options.
//
// [gnuplot documentation]: http://gnuplot.info/docs_5.5/loc9418.html
func Plot(pd PlotData, setOpts ...GnuplotOpt) error {
path := ""
if pd.Function != "" && pd.Df == nil && pd.Columns == nil {
path = pd.Function
} else {
rand.Seed(time.Now().UnixNano())
newDf, err := pd.Df.LocCols(pd.Columns...)
if err != nil {
return err
}
path = filepath.Join("/", "tmp", fmt.Sprintf("%x.csv", rand.Intn(100000000)))
_, err = WriteCsv(newDf, path, true)
if err != nil {
return err
}
}
var setBuf bytes.Buffer
for _, setOpt := range setOpts {
str := setOpt.createCmdString()
setBuf.WriteString(str)
setBuf.WriteString("; ")
}
var usingBuf bytes.Buffer
var withBuf bytes.Buffer
for _, opt := range pd.Opts {
str := opt.createCmdString()
switch opt.getOption() {
case "using":
usingBuf.WriteString(str)
case "with":
withBuf.WriteString(str)
default:
return fmt.Errorf("this option is not supported yet")
}
}
cmdString := fmt.Sprintf(`%s %s "%s" %s %s`, setBuf.String(), "plot", path, usingBuf.String(), withBuf.String())
cmd := exec.Command("gnuplot", "-persist", "-e", cmdString)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf(fmt.Sprint(err, cmd.Stderr))
}
return nil
}
// PlotN plots several PlotData objects `pd` in one graph.
// Use PlotN when you want to compare two different datasets,
// or a dataset with a line of best fit.
//
// Refer to the [gnuplot documentation] for `set` options.
//
// [gnuplot documentation]: http://gnuplot.info/docs_5.5/loc9418.html
func PlotN(plotdata []PlotData, setOpts ...GnuplotOpt) error {
rand.Seed(time.Now().UnixNano())
var setBuf bytes.Buffer
for _, setOpt := range setOpts {
str := setOpt.createCmdString()
setBuf.WriteString(str)
setBuf.WriteString("; ")
}
cmdString := fmt.Sprintf(`%s %s `, setBuf.String(), "plot")
for _, pd := range plotdata {
path := ``
if pd.Function != "" && pd.Df == nil && pd.Columns == nil {
path = pd.Function
} else {
newDf, err := pd.Df.LocCols(pd.Columns...)
if err != nil {
return err
}
path = filepath.Join("/", "tmp", fmt.Sprintf("%x.csv", rand.Intn(100000000)))
_, err = WriteCsv(newDf, path, true)
if err != nil {
return err
}
path = fmt.Sprintf(`"%s"`, path)
}
var usingBuf bytes.Buffer
var withBuf bytes.Buffer
for _, opt := range pd.Opts {
str := opt.createCmdString()
switch opt.getOption() {
case "using":
usingBuf.WriteString(str)
case "with":
withBuf.WriteString(str)
default:
return fmt.Errorf("this option is not supported yet")
}
}
cmdStringPiece := fmt.Sprintf(`%s %s %s,`, path, usingBuf.String(), withBuf.String())
cmdString += cmdStringPiece
}
cmd := exec.Command("gnuplot", "-persist", "-e", cmdString)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf(fmt.Sprint(err, cmd.Stderr))
}
return nil
}
// Fit fits a user-defined function ff to data given in PlotData pd,
// and prints out the results.
//
// Pass options such as `using` in pd, but `via` in viaOpts.
func Fit(ff string, pd PlotData, viaOpts ...GnuplotOpt) error {
rand.Seed(time.Now().UnixNano())
newDf, err := pd.Df.LocCols(pd.Columns...)
if err != nil {
return err
}
path := filepath.Join("/", "tmp", fmt.Sprintf("%x.csv", rand.Intn(100000000)))
_, err = WriteCsv(newDf, path, true)
if err != nil {
return err
}
var usingBuf, viaBuf bytes.Buffer
for _, opt := range pd.Opts {
str := opt.createCmdString()
if opt.getOption() == "using" {
usingBuf.WriteString(str)
}
}
for _, opt := range viaOpts {
str := opt.createCmdString()
if opt.getOption() == "via" {
viaBuf.WriteString(str)
}
}
cmdString := fmt.Sprintf(`%s %s "%s" %s %s`, `set datafile sep ","; fit`, ff, path, usingBuf.String(), viaBuf.String())
cmd := exec.Command("gnuplot", "-persist", "-e", cmdString)
combOutput, err := cmd.CombinedOutput()
if err != nil {
return err
}
fmt.Printf("%s", combOutput)
return nil
}