-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta_table.go
353 lines (339 loc) · 10.2 KB
/
meta_table.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
/* Copyright (C) 2016 Philipp Benner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gonetics
/* -------------------------------------------------------------------------- */
import "fmt"
import "bytes"
import "bufio"
import "math"
import "io"
import "io/ioutil"
import "strconv"
import "strings"
/* -------------------------------------------------------------------------- */
type OptionPrintScientific struct {
Value bool
}
/* -------------------------------------------------------------------------- */
func (meta Meta) WriteTable(writer io.Writer, header bool, args ...interface{}) error {
useScientific := false
for _, arg := range args {
switch a := arg.(type) {
case OptionPrintScientific:
useScientific = a.Value
default:
}
}
printCellSlice := func(writer io.Writer, widths []int, i, j int, data interface{}) (int, error) {
var tmpBuffer bytes.Buffer
tmpWriter := bufio.NewWriter(&tmpBuffer)
switch v := data.(type) {
case [][]string:
if len(v[i]) == 0 {
if _, err := fmt.Fprintf(tmpWriter, "nil"); err != nil {
return 0, err
}
}
for k := 0; k < len(v[i]); k++ {
if k != 0 {
fmt.Fprintf(tmpWriter, ",")
}
if _, err := fmt.Fprintf(tmpWriter, "%s", v[i][k]); err != nil {
return 0, err
}
}
case [][]float64:
if len(v[i]) == 0 {
if _, err := fmt.Fprintf(tmpWriter, "nil"); err != nil {
return 0, err
}
}
for k := 0; k < len(v[i]); k++ {
if k != 0 {
if _, err := fmt.Fprintf(tmpWriter, ","); err != nil {
return 0, err
}
}
if useScientific {
if _, err := fmt.Fprintf(tmpWriter, "%e", v[i][k]); err != nil {
return 0, err
}
} else {
if _, err := fmt.Fprintf(tmpWriter, "%f", v[i][k]); err != nil {
return 0, err
}
}
}
case [][]int:
if len(v[i]) == 0 {
if _, err := fmt.Fprintf(tmpWriter, "nil"); err != nil {
return 0, err
}
}
for k := 0; k < len(v[i]); k++ {
if k != 0 {
fmt.Fprintf(tmpWriter, ",")
}
if _, err := fmt.Fprintf(tmpWriter, "%d", v[i][k]); err != nil {
return 0, err
}
}
default:
panic("invalid meta data")
}
tmpWriter.Flush()
format := fmt.Sprintf(" %%%ds", widths[j]-1)
return fmt.Fprintf(writer, format, tmpBuffer.String())
}
printCell := func(writer io.Writer, widths []int, i, j int) (int, error) {
switch v := meta.MetaData[j].(type) {
case []string:
format := fmt.Sprintf(" %%%ds", widths[j]-1)
return fmt.Fprintf(writer, format, v[i])
case []float64:
if useScientific {
format := fmt.Sprintf(" %%%de", widths[j]-1)
return fmt.Fprintf(writer, format, v[i])
} else {
format := fmt.Sprintf(" %%%df", widths[j]-1)
return fmt.Fprintf(writer, format, v[i])
}
case []int:
format := fmt.Sprintf(" %%%dd", widths[j]-1)
return fmt.Fprintf(writer, format, v[i])
default:
return printCellSlice(writer, widths, i, j, v)
}
}
printRow := func(writer io.Writer, widths []int, i int) error {
if i != 0 {
if _, err := fmt.Fprintf(writer, "\n"); err != nil {
return err
}
}
for j := 0; j < meta.MetaLength(); j++ {
if _, err := printCell(writer, widths, i, j); err != nil {
return err
}
}
return nil
}
// compute widths of all cells in row i
updateMaxWidths := func(i int, widths []int) error {
for j := 0; j < meta.MetaLength(); j++ {
if width, err := printCell(ioutil.Discard, widths, i, j); err != nil {
return err
} else {
if width > widths[j] {
widths[j] = width
}
}
}
return nil
}
printHeader := func(writer io.Writer, widths []int) error {
for j := 0; j < meta.MetaLength(); j++ {
format := fmt.Sprintf(" %%%ds", widths[j]-1)
if _, err := fmt.Fprintf(writer, format, meta.MetaName[j]); err != nil {
return err
}
}
if _, err := fmt.Fprintf(writer, "\n"); err != nil {
return err
}
return nil
}
applyRows := func(f1 func(i int) error) error {
for i := 0; i < meta.Length(); i++ {
if err := f1(i); err != nil {
return err
}
}
return nil
}
// maximum column widths
widths := make([]int, meta.MetaLength())
for j := 0; j < meta.MetaLength(); j++ {
if width, err := fmt.Fprintf(ioutil.Discard, " %s", meta.MetaName[j]); err != nil {
return err
} else {
widths[j] = width
}
}
// determine column widths
if err := applyRows(func(i int) error { return updateMaxWidths(i, widths) }); err != nil {
return err
}
// pring header
if header {
if err := printHeader(writer, widths); err != nil {
return err
}
}
// print rows
if err := applyRows(
func(i int) error { return printRow(writer, widths, i) }); err != nil {
return err
}
return nil
}
/* -------------------------------------------------------------------------- */
func (meta *Meta) PrintTable(header bool, args ...interface{}) string {
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
if err := meta.WriteTable(writer, header, args...); err != nil {
return ""
}
writer.Flush()
return buffer.String()
}
/* -------------------------------------------------------------------------- */
func (meta *Meta) ReadTable(r io.Reader, names, types []string) error {
reader := bufio.NewReader(r)
if len(names) != len(types) {
panic("invalid arguments")
}
// header information
idxMap := make(map[string]int)
metaMap := make(map[string]interface{})
for i := 0; i < len(names); i++ {
idxMap[names[i]] = -1
switch types[i] {
case "[]string": metaMap[names[i]] = []string{}
case "[]int": metaMap[names[i]] = []int{}
case "[]float64": metaMap[names[i]] = []float64{}
case "[][]string": metaMap[names[i]] = [][]string{}
case "[][]int": metaMap[names[i]] = [][]int{}
case "[][]float64": metaMap[names[i]] = [][]float64{}
default: panic("invalid types argument")
}
}
// scan header
if l, err := bufioReadLine(reader); err != nil && err != io.EOF {
return err
} else {
fields := strings.Fields(l)
// get number of columns
if len(fields) < 4 {
return fmt.Errorf("invalid table")
}
for i := 0; i < len(fields); i++ {
if _, ok := idxMap[fields[i]]; ok {
idxMap[fields[i]] = i
}
}
}
// scan data
for i:= 2;; i++ {
l, err := bufioReadLine(reader)
if err == io.EOF {
break
}
if err != nil {
return err
}
if len(l) == 0 {
continue
}
fields := strings.Fields(l)
for name, idx := range idxMap {
if idx == -1 {
// column not found, skip
continue
}
if idx >= len(fields) {
return fmt.Errorf("invalid table")
}
switch entry := metaMap[name].(type) {
case []string:
entry = append(entry, fields[idx])
metaMap[name] = entry
case []int:
v, err := strconv.ParseInt(fields[idx], 10, 64)
if err != nil {
return fmt.Errorf("parsing meta information failed at line `%d': %v", i, err)
}
entry = append(entry, int(v))
metaMap[name] = entry
case []float64:
if fields[idx] == "NA" || fields[idx] == "NaN" {
entry = append(entry, math.NaN())
metaMap[name] = entry
} else {
v, err := strconv.ParseFloat(fields[idx], 64)
if err != nil {
return fmt.Errorf("parsing meta information failed at line `%d': %v", i, err)
}
entry = append(entry, v)
metaMap[name] = entry
}
case [][]int:
data := strings.FieldsFunc(fields[idx], func(x rune) bool { return x == ',' })
// parse counts
if len(data) == 1 && data[0] == "nil" {
entry = append(entry, []int{})
} else {
entry = append(entry, make([]int, len(data)))
// loop over count vector
for i := 0; i < len(data); i++ {
v, err := strconv.ParseInt(data[i], 10, 64)
if err != nil {
return fmt.Errorf("parsing meta information failed at line `%d': %v", i, err)
}
entry[len(entry)-1][i] = int(v)
}
}
metaMap[name] = entry
case [][]float64:
data := strings.FieldsFunc(fields[idx], func(x rune) bool { return x == ',' })
// parse counts
if len(data) == 1 && data[0] == "nil" {
entry = append(entry, []float64{})
} else {
entry = append(entry, make([]float64, len(data)))
// loop over count vector
for i := 0; i < len(data); i++ {
v, err := strconv.ParseFloat(data[i], 64)
if err != nil {
return fmt.Errorf("parsing meta information failed at line `%d': %v", i, err)
}
entry[len(entry)-1][i] = v
}
}
metaMap[name] = entry
case [][]string:
data := strings.FieldsFunc(fields[idx], func(x rune) bool { return x == ',' })
// parse counts
if len(data) == 1 && data[0] == "nil" {
entry = append(entry, []string{})
} else {
entry = append(entry, make([]string, len(data)))
// loop over count vector
for i := 0; i < len(data); i++ {
entry[len(entry)-1][i] = data[i]
}
}
metaMap[name] = entry
}
}
}
for name, idx := range idxMap {
if idx != -1 {
meta.AddMeta(name, metaMap[name])
}
}
return nil
}