/
interpolate_dataframe.go
78 lines (63 loc) · 1.53 KB
/
interpolate_dataframe.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package interpolation
import (
"context"
"golang.org/x/sync/errgroup"
"sync"
dataframe "github.com/rocketlaunchr/dataframe-go"
)
func interpolateDataFrame(ctx context.Context, df *dataframe.DataFrame, opts InterpolateOptions) (map[interface{}]*dataframe.OrderedMapIntFloat64, error) {
if !opts.DontLock {
df.Lock()
defer df.Unlock()
}
var lock sync.Mutex
omaps := map[interface{}]*dataframe.OrderedMapIntFloat64{}
if opts.HorizAxis != nil {
switch s := opts.HorizAxis.(type) {
case int:
opts.HorizAxis = df.Series[s]
case string:
i, err := df.NameToColumn(s, dataframe.DontLock)
if err != nil {
return nil, err
}
opts.HorizAxis = df.Series[i]
case dataframe.Series:
default:
panic("HorizAxis option must be a SeriesFloat64/SeriesTime or convertable to a SeriesFloat64")
}
}
g, newCtx := errgroup.WithContext(ctx)
for i := range df.Series {
i := i
if df.Series[i] == opts.HorizAxis {
continue
}
fs, ok := df.Series[i].(*dataframe.SeriesFloat64)
if !ok {
continue
}
g.Go(func() error {
omap, err := Interpolate(newCtx, fs, opts)
if err != nil {
return err
}
if !opts.InPlace {
lock.Lock()
omaps[i] = omap.(*dataframe.OrderedMapIntFloat64)
omaps[df.Series[i].Name()] = omap.(*dataframe.OrderedMapIntFloat64)
lock.Unlock()
}
return nil
})
}
err := g.Wait()
if err != nil {
return nil, err
}
if opts.InPlace {
return nil, nil
}
return omaps, nil
}