-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta.go
548 lines (502 loc) · 13.9 KB
/
meta.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* 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 "errors"
import "regexp"
import "sort"
/* -------------------------------------------------------------------------- */
type Meta struct {
MetaName []string
MetaData []interface{}
rows int
}
/* constructors
* -------------------------------------------------------------------------- */
func NewMeta(names []string, data []interface{}) Meta {
meta := Meta{}
if len(names) != len(data) {
panic("NewMeta(): invalid parameters!")
}
for i := 0; i < len(names); i++ {
meta.AddMeta(names[i], data[i])
}
return meta
}
// Deep copy the Meta object.
func (m *Meta) Clone() Meta {
// empty meta data
result := NewMeta([]string{}, []interface{}{})
// loop over meta data and clone elements
for i := 0; i < m.MetaLength(); i++ {
switch v := m.MetaData[i].(type) {
case [][]string:
r := make([][]string, len(v))
for j := 0; j < len(v); j++ {
r[j] = make([]string, len(v[j]))
copy(r[j], v[j])
}
result.AddMeta(m.MetaName[i], r)
case []string:
r := make([]string, len(v))
copy(r, v)
result.AddMeta(m.MetaName[i], r)
case [][]float64:
r := make([][]float64, len(v))
for j := 0; j < len(v); j++ {
r[j] = make([]float64, len(v[j]))
copy(r[j], v[j])
}
result.AddMeta(m.MetaName[i], r)
case []float64:
r := make([]float64, len(v))
copy(r, v)
result.AddMeta(m.MetaName[i], r)
case [][]int:
r := make([][]int, len(v))
for j := 0; j < len(v); j++ {
r[j] = make([]int, len(v[j]))
copy(r[j], v[j])
}
result.AddMeta(m.MetaName[i], r)
case []int:
r := make([]int, len(v))
copy(r, v)
result.AddMeta(m.MetaName[i], r)
case []Range:
r := make([]Range, len(v))
copy(r, v)
result.AddMeta(m.MetaName[i], r)
default: panic("AddMeta(): invalid type!")
}
}
return result
}
/* -------------------------------------------------------------------------- */
// Returns the number of rows.
func (m Meta) Length() int {
return m.rows
}
// Returns the number of columns.
func (m Meta) MetaLength() int {
return len(m.MetaName)
}
func (m Meta) Row(i int) MetaRow {
return NewMetaRow(m, i)
}
func (m *Meta) AddMeta(name string, meta interface{}) {
n := -1
// determine length
switch v := meta.(type) {
case [][]string: n = len(v)
case []string: n = len(v)
case [][]float64: n = len(v)
case []float64: n = len(v)
case [][]int: n = len(v)
case []int: n = len(v)
case []Range: n = len(v)
default: panic("AddMeta(): invalid type!")
}
if m.MetaLength() > 0 {
// this is not the first column added; check length
if n != m.rows {
panic(fmt.Sprintf("AddMeta(): column `%s' has invalid length: expected length of `%d' but column has length `%d'", name, m.rows, n))
}
} else {
// this is the first column, set length
m.rows = n
}
// if a meta column with this name already exists delete it
m.DeleteMeta(name)
m.MetaData = append(m.MetaData, meta)
m.MetaName = append(m.MetaName, name)
}
func (m *Meta) DeleteMeta(name string) {
for i := 0; i < m.MetaLength(); i++ {
if m.MetaName[i] == name {
m.MetaName = append(m.MetaName[:i], m.MetaName[i+1:]...)
m.MetaData = append(m.MetaData[:i], m.MetaData[i+1:]...)
}
}
}
func (m *Meta) RenameMeta(nameOld, nameNew string) {
if nameOld == nameNew {
return
}
// if a meta column with this name already exists delete it
m.DeleteMeta(nameNew)
for i := 0; i < m.MetaLength(); i++ {
if m.MetaName[i] == nameOld {
m.MetaName[i] = nameNew
}
}
}
func (m Meta) GetMeta(name string) interface{} {
re := regexp.MustCompile("^"+name+"$")
for i := 0; i < m.MetaLength(); i++ {
if re.MatchString(m.MetaName[i]) {
return m.MetaData[i]
}
}
return nil
}
func (m Meta) GetMetaStr(name string) []string {
r := m.GetMeta(name)
if r != nil {
return r.([]string)
}
return []string{}
}
func (m Meta) GetMetaFloat(name string) []float64 {
r := m.GetMeta(name)
if r != nil {
return r.([]float64)
}
return []float64{}
}
func (m Meta) GetMetaInt(name string) []int {
r := m.GetMeta(name)
if r != nil {
return r.([]int)
}
return []int{}
}
func (m Meta) GetMetaRange(name string) []Range {
r := m.GetMeta(name)
if r != nil {
return r.([]Range)
}
return []Range{}
}
func (meta1 Meta) Append(meta2 Meta) Meta {
result := Meta{}
// clone data so we do not have to deep copy
// two-dimensional slices
m1 := meta1.Clone()
m2 := meta2.Clone()
for j := 0; j < m2.MetaLength(); j++ {
var t interface{}
name := m2.MetaName[j]
dat1 := m1.GetMeta(name)
dat2 := m2.MetaData[j]
switch v := dat1.(type) {
case [][]string: t = append(v, dat2.([][]string)...)
case [][]int: t = append(v, dat2.([][]int)...)
case [][]float64: t = append(v, dat2.([][]float64)...)
case []string: t = append(v, dat2.( []string)...)
case []int: t = append(v, dat2.( []int)...)
case []float64: t = append(v, dat2.( []float64)...)
case []Range: t = append(v, dat2.( []Range)...)
default:
t = dat2
}
result.AddMeta(name, t)
}
return result
}
func (meta Meta) Remove(indices []int) Meta {
if len(indices) == 0 {
return meta.Clone()
}
indices = removeDuplicatesInt(indices)
sort.Ints(indices)
n := meta.Length()
m := n - len(indices)
// convert indices to subset indices
idx := make([]int, m)
for i, j, k := 0, 0, 0; i < meta.Length(); i++ {
for k < len(indices)-1 && i > indices[k] {
k++
}
if i != indices[k] {
idx[j] = i
j++
}
}
return meta.Subset(idx)
}
// Return a new Meta object with a subset of the rows from
// this object.
func (meta Meta) Subset(indices []int) Meta {
n := len(indices)
m := meta.MetaLength()
data := []interface{}{}
for j := 0; j < m; j++ {
switch v := meta.MetaData[j].(type) {
case [][]string:
l := make([][]string, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case []string:
l := make([]string, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case []float64:
l := make([]float64, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case [][]float64:
l := make([][]float64, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case []int:
l := make([]int, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case [][]int:
l := make([][]int, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
case []Range:
l := make([]Range, n)
for i := 0; i < n; i++ {
l[i] = v[indices[i]]
}
data = append(data, l)
}
}
return NewMeta(meta.MetaName, data)
}
// Return a new Meta object containing rows given by the range
// [ifrom, ito).
func (meta Meta) Slice(ifrom, ito int) Meta {
n := ito-ifrom
m := meta.MetaLength()
data := []interface{}{}
for j := 0; j < m; j++ {
switch v := meta.MetaData[j].(type) {
case [][]string:
l := make([][]string, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case []string:
l := make([]string, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case []float64:
l := make([]float64, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case [][]float64:
l := make([][]float64, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case []int:
l := make([]int, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case [][]int:
l := make([][]int, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
case []Range:
l := make([]Range, n)
for i := ifrom; i < ito; i++ {
l[i-ifrom] = v[i]
}
data = append(data, l)
}
}
return NewMeta(meta.MetaName, data)
}
// Return a new Meta object where a given set of rows has been merged. The argument indices
// should assign the same target index to all rows that should be merged, i.e.
// indices := []int{0, 1, 1, 2, 3}
// would merge rows 1 and 2 but leave all rows 0, 3 and 4 as they are. Rows are merged by
// replacing one-dimensional slices by two-dimensional slices. The Reduce{String,Float64,Int}
// methods may be used afterwards to apply a function to the merged data. A Meta object that
// already contains two-dimensional slices cannot be merged.
func (meta Meta) Merge(indices []int) Meta {
sliceMax := func(s []int) int {
max := 0
for _, v := range s {
if v > max {
max = v
}
}
return max
}
n := sliceMax(indices)+1
m := meta.MetaLength()
data := []interface{}{}
for j := 0; j < m; j++ {
switch v := meta.MetaData[j].(type) {
case []string:
l := make([][]string, n)
for i := 0; i < len(v); i++ {
l[indices[i]] = append(l[indices[i]], v[i])
}
data = append(data, l)
case []float64:
l := make([][]float64, n)
for i := 0; i < len(v); i++ {
l[indices[i]] = append(l[indices[i]], v[i])
}
data = append(data, l)
case []int:
l := make([][]int, n)
for i := 0; i < len(v); i++ {
l[indices[i]] = append(l[indices[i]], v[i])
}
data = append(data, l)
case [][]string : panic("cannot merge [][]string")
case [][]int : panic("cannot merge [][]int")
case [][]float64: panic("cannot merge [][]float64")
case []Range : panic("cannot merge []Range")
}
}
return NewMeta(meta.MetaName, data)
}
// Reduce a column with a two-dimensional string slice by applying
// the given function f. If nameNew != "" the old meta column
// is kept.
func (meta *Meta) ReduceString(name, nameNew string, f func([]string) string) {
t := meta.GetMeta(name).([][]string)
r := make([]string, len(t))
for i := 0; i < len(t); i++ {
r[i] = f(t[i])
}
if nameNew != "" && name != nameNew {
meta.AddMeta(nameNew, r)
} else {
meta.DeleteMeta(name)
meta.AddMeta(name, r)
}
}
// Reduce a column with a two-dimensional float64 slice by applying
// the given function f. If nameNew != "" the old meta column
// is kept.
func (meta *Meta) ReduceFloat(name, nameNew string, f func([]float64) float64) {
t := meta.GetMeta(name).([][]float64)
r := make([]float64, len(t))
for i := 0; i < len(t); i++ {
r[i] = f(t[i])
}
if nameNew != "" && name != nameNew {
meta.AddMeta(nameNew, r)
} else {
meta.DeleteMeta(name)
meta.AddMeta(name, r)
}
}
// Reduce a column with a two-dimensional int slice by applying
// the given function f. If nameNew != "" the old meta column
// is kept.
func (meta *Meta) ReduceInt(name, nameNew string, f func([]int) int) {
t := meta.GetMeta(name).([][]int)
r := make([]int, len(t))
for i := 0; i < len(t); i++ {
r[i] = f(t[i])
}
if nameNew != "" && name != nameNew {
meta.AddMeta(nameNew, r)
} else {
meta.DeleteMeta(name)
meta.AddMeta(name, r)
}
}
/* sorting
* -------------------------------------------------------------------------- */
type metaPair struct {
Key int
Value interface{}
}
type metaPairList []metaPair
func (p metaPairList) Len() int {
return len(p)
}
func (p metaPairList) Less(i, j int) bool {
switch p[i].Value.(type) {
case float64: return p[i].Value.(float64) < p[j].Value.(float64)
case int : return p[i].Value.(int) < p[j].Value.(int)
case string : return p[i].Value.(string) < p[j].Value.(string)
default:
panic("Invalid type for sorting!")
}
}
func (p metaPairList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (meta *Meta) sortedIndices(name string, reverse bool) ([]int, error) {
var l metaPairList
if t := meta.GetMeta(name); t != nil {
switch s := t.(type) {
case []float64: for i, v := range s { l = append(l, metaPair{i, v}) }
case []int : for i, v := range s { l = append(l, metaPair{i, v}) }
case []string : for i, v := range s { l = append(l, metaPair{i, v}) }
default:
panic("Invalid type for sorting!")
}
} else {
return []int{}, errors.New("Meta column not found!")
}
if reverse {
sort.Sort(sort.Reverse(l))
} else {
sort.Sort(l)
}
// slice of indices
j := make([]int, len(l))
// extract indices
for i := 0; i < len(l); i++ {
j[i] = l[i].Key
}
return j, nil
}
func (meta *Meta) Sort(name string, reverse bool) (Meta, error) {
j, err := meta.sortedIndices(name, reverse)
if err != nil {
return Meta{}, err
}
return meta.Subset(j), nil
}
/* -------------------------------------------------------------------------- */
func (meta *Meta) String() string {
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
if err := meta.WritePretty(writer, 10); err != nil {
return ""
}
writer.Flush()
return buffer.String()
}