forked from rocketlaunchr/dataframe-go
/
interpolate_series.go
489 lines (429 loc) · 12.4 KB
/
interpolate_series.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package interpolation
import (
"context"
"golang.org/x/xerrors"
"math"
"github.com/DzananGanic/numericalgo/interpolate"
"github.com/DzananGanic/numericalgo/interpolate/lagrange"
"github.com/cnkei/gospline"
dataframe "github.com/mmavka/dataframe-go"
"github.com/mmavka/dataframe-go/utils"
)
func interpolateSeriesFloat64(ctx context.Context, fs *dataframe.SeriesFloat64, opts InterpolateOptions) (*dataframe.OrderedMapIntFloat64, error) {
if !opts.DontLock {
fs.Lock()
defer fs.Unlock()
}
var (
xaxisF *dataframe.SeriesFloat64
xaxisT *dataframe.SeriesTime
)
if opts.HorizAxis != nil {
switch s := opts.HorizAxis.(type) {
case *dataframe.SeriesFloat64:
xaxisF = s
case *dataframe.SeriesTime:
xaxisT = s
case dataframe.ToSeriesFloat64:
var err error
xaxisF, err = s.ToSeriesFloat64(ctx, false)
if err != nil {
return nil, err
}
default:
panic("HorizAxis option must be a SeriesFloat64/SeriesTime or convertable to a SeriesFloat64")
}
}
var omap *dataframe.OrderedMapIntFloat64
if !opts.InPlace {
omap = dataframe.NewOrderedMapIntFloat64()
}
r := &dataframe.Range{}
if opts.R != nil {
r = opts.R
}
start, end, err := r.Limits(len(fs.Values))
if err != nil {
return nil, err
}
if xaxisF != nil || xaxisT != nil {
subsetL := end - start + 1
if xaxisF != nil {
if len(xaxisF.Values) != len(fs.Values) && len(xaxisF.Values) != subsetL {
panic("HorizAxis must contain the same number of rows")
}
} else {
if len(xaxisT.Values) != len(fs.Values) && len(xaxisT.Values) != subsetL {
panic("HorizAxis must contain the same number of rows")
}
}
if opts.InPlace {
ncOpts := dataframe.NilCountOptions{
Ctx: ctx,
R: &dataframe.Range{Start: &start, End: &end},
DontLock: true,
StopAtOneNil: true,
}
var (
nc int
err error
)
if xaxisF != nil {
nc, err = xaxisF.NilCount(ncOpts)
if err != nil {
return nil, err
}
} else {
nc, err = xaxisT.NilCount(ncOpts)
if err != nil {
return nil, err
}
}
if nc > 0 {
panic("HorizAxis must contain no nil values")
}
}
}
// TODO: Check if there is only 1 non-nil value between start and end.
var (
startOfSeg int = start
firstRow *int // row of first known value
lastRow *int // row of last known value
)
//////// FOR ALGORITHM PREPARATION //////
var alg interface{}
switch method := opts.Method.(type) {
case Spline:
if method.Order == 3 {
// requires at least 2 known values
xVals := []float64{}
yVals := []float64{}
for x := start; x <= end; x++ {
y := fs.Values[x]
if !math.IsNaN(y) {
xVal := xVal(x, fs, xaxisF, xaxisT, start)
if math.IsNaN(xVal) {
panic("HorizAxis must contain no nil values")
}
xVals = append(xVals, xVal)
yVals = append(yVals, y)
}
}
alg = gospline.NewCubicSpline(xVals, yVals)
}
case Lagrange:
xVals := []float64{}
yVals := []float64{}
for x := start; x <= end; x++ {
y := fs.Values[x]
if !math.IsNaN(y) {
xVal := xVal(x, fs, xaxisF, xaxisT, start)
if math.IsNaN(xVal) {
panic("HorizAxis must contain no nil values")
}
xVals = append(xVals, xVal)
yVals = append(yVals, y)
}
}
li := lagrange.New()
li.Fit(xVals, yVals)
alg = li
}
////////////////////////////////////////
for {
if startOfSeg >= end-1 {
break
}
if err := ctx.Err(); err != nil {
return nil, err
}
var (
left *int
right *int
)
for i := startOfSeg; i <= end; i++ {
currentVal := fs.Values[i]
if !math.IsNaN(currentVal) {
if firstRow == nil {
firstRow = &[]int{i}[0]
}
if left == nil {
left = &[]int{i}[0]
} else {
right = &[]int{i}[0]
lastRow = &[]int{i}[0]
break
}
}
}
if left != nil && right != nil {
if opts.FillRegion == nil || opts.FillRegion.has(Interpolation) {
// Fill Inner range
switch method := opts.Method.(type) {
case nil, ForwardFill:
fillFn := func(row int) (float64, error) {
return fs.Values[*left], nil
}
err := fill(ctx, fillFn, fs, omap, *left, *right, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case BackwardFill:
fillFn := func(row int) (float64, error) {
return fs.Values[*right], nil
}
err := fill(ctx, fillFn, fs, omap, *left, *right, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Linear:
var fillFn func(int) (float64, error)
if xaxisF == nil && xaxisT == nil {
grad := (fs.Values[*right] - fs.Values[*left]) / float64(*right-*left)
c := fs.Values[*left] + grad
fillFn = func(row int) (float64, error) {
return grad*float64(row) + c, nil
}
} else {
xLeft := xVal(*left, fs, xaxisF, xaxisT, start)
if math.IsNaN(xLeft) {
panic("HorizAxis must contain no nil values")
}
xRight := xVal(*right, fs, xaxisF, xaxisT, start)
if math.IsNaN(xRight) {
panic("HorizAxis must contain no nil values")
}
grad := (fs.Values[*right] - fs.Values[*left]) / (xRight - xLeft)
fillFn = func(row int) (float64, error) {
xr := xVal(*left+row+1, fs, xaxisF, xaxisT, start)
if math.IsNaN(xr) {
panic("HorizAxis must contain no nil values")
}
Δx := xr - xLeft
return grad*Δx + fs.Values[*left], nil
}
}
err := fill(ctx, fillFn, fs, omap, *left, *right, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Spline:
if method.Order == 3 {
var fillFn func(int) (float64, error)
if xaxisF == nil && xaxisT == nil {
splineVals := alg.(gospline.Spline).Range(float64(*left), float64(*right), 1)
fillFn = func(row int) (float64, error) {
return splineVals[row+1], nil
}
} else {
fillFn = func(row int) (float64, error) {
xr := xVal(*left+row+1, fs, xaxisF, xaxisT, start)
return alg.(gospline.Spline).At(xr), nil
}
}
err := fill(ctx, fillFn, fs, omap, *left, *right, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
}
case Lagrange:
var fillFn func(int) (float64, error)
if xaxisF == nil && xaxisT == nil {
lagrangeXVals := utils.Float64Seq(float64(*left), float64(*right), 1)
lagrangeYVals, err := interpolate.WithMulti(alg.(*lagrange.Lagrange), lagrangeXVals)
if err != nil {
return nil, xerrors.Errorf("Lagrange method: %w", err)
}
fillFn = func(row int) (float64, error) {
return lagrangeYVals[row+1], nil
}
} else {
fillFn = func(row int) (float64, error) {
xr := xVal(*left+row+1, fs, xaxisF, xaxisT, start)
lagrangeYVal, err := interpolate.WithSingle(alg.(*lagrange.Lagrange), xr)
if err != nil {
return 0, xerrors.Errorf("Lagrange method: %w", err)
}
return lagrangeYVal, nil
}
}
err = fill(ctx, fillFn, fs, omap, *left, *right, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
}
}
startOfSeg = *right
} else {
break
}
}
// Extrapolation
if opts.FillRegion == nil || opts.FillRegion.has(Extrapolation) {
// Left side
if start != *firstRow {
switch method := opts.Method.(type) {
case nil, ForwardFill, BackwardFill:
fillFn := func(row int) (float64, error) {
return fs.Values[*firstRow], nil
}
err := fill(ctx, fillFn, fs, omap, start-1, *firstRow, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Linear:
var fillFn func(int) (float64, error)
if xaxisF == nil && xaxisT == nil {
var grad float64
if omap != nil {
y1 := fs.Values[*firstRow] // existing value
y2, exists := omap.Get(*firstRow + 1)
if !exists {
y2 = fs.Values[*firstRow+1]
}
grad = (y2 - y1) / 1.0
} else {
grad = (fs.Values[*firstRow+1] - fs.Values[*firstRow]) / 1.0
}
c := fs.Values[*firstRow] - grad*float64(*firstRow)
fillFn = func(row int) (float64, error) {
return grad*float64(row+start) + c, nil
}
} else {
// Calculate gradient
xFirstRow := xVal(*firstRow, fs, xaxisF, xaxisT, start)
if math.IsNaN(xFirstRow) {
panic("HorizAxis must contain no nil values")
}
xFirstRowPlusOne := xVal(*firstRow+1, fs, xaxisF, xaxisT, start)
if math.IsNaN(xFirstRowPlusOne) {
panic("HorizAxis must contain no nil values")
}
Δx := xFirstRowPlusOne - xFirstRow
var grad float64
if omap != nil {
y1 := fs.Values[*firstRow] // existing value
y2, exists := omap.Get(*firstRow + 1)
if !exists {
y2 = fs.Values[*firstRow+1]
}
grad = (y2 - y1) / Δx
} else {
grad = (fs.Values[*firstRow+1] - fs.Values[*firstRow]) / Δx
}
fillFn = func(row int) (float64, error) {
xr := xVal(start+row, fs, xaxisF, xaxisT, start)
if math.IsNaN(xr) {
panic("HorizAxis must contain no nil values")
}
Δx := xFirstRow - xr
return fs.Values[*firstRow] - grad*Δx, nil
}
}
err := fill(ctx, fillFn, fs, omap, start-1, *firstRow, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Spline:
if method.Order == 3 {
splineVals := alg.(gospline.Spline).Range(float64(start-1), float64(*firstRow), 1)
fillFn := func(row int) (float64, error) {
return splineVals[row+1], nil
}
err := fill(ctx, fillFn, fs, omap, start-1, *firstRow, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
}
case Lagrange:
// Can't be used to extrapolate.
// Package returns error: Value to interpolate is too small and not in range
}
}
// Right side
if end != *lastRow {
switch method := opts.Method.(type) {
case nil, ForwardFill, BackwardFill:
fillFn := func(row int) (float64, error) {
return fs.Values[*lastRow], nil
}
err := fill(ctx, fillFn, fs, omap, *lastRow, end+1, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Linear:
var fillFn func(int) (float64, error)
if xaxisF == nil && xaxisT == nil {
var grad float64
if omap != nil {
y2 := fs.Values[*lastRow] // existing value
y1, exists := omap.Get(*lastRow - 1)
if !exists {
y1 = fs.Values[*lastRow-1]
}
grad = (y2 - y1) / 1.0
} else {
grad = (fs.Values[*lastRow] - fs.Values[*lastRow-1]) / 1.0
}
c := fs.Values[*lastRow] - grad*float64(*lastRow)
fillFn = func(row int) (float64, error) {
return grad*float64(row+*lastRow+1) + c, nil
}
} else {
// Calculate gradient
xLastRow := xVal(*lastRow, fs, xaxisF, xaxisT, start)
if math.IsNaN(xLastRow) {
panic("HorizAxis must contain no nil values")
}
xLastRowMinusOne := xVal(*lastRow-1, fs, xaxisF, xaxisT, start)
if math.IsNaN(xLastRowMinusOne) {
panic("HorizAxis must contain no nil values")
}
Δx := xLastRow - xLastRowMinusOne
var grad float64
if omap != nil {
y2 := fs.Values[*lastRow] // existing value
y1, exists := omap.Get(*lastRow - 1)
if !exists {
y1 = fs.Values[*lastRow-1]
}
grad = (y2 - y1) / Δx
} else {
grad = (fs.Values[*lastRow] - fs.Values[*lastRow-1]) / Δx
}
fillFn = func(row int) (float64, error) {
xr := xVal(*lastRow+1+row, fs, xaxisF, xaxisT, start)
if math.IsNaN(xr) {
panic("HorizAxis must contain no nil values")
}
Δx := xr - xLastRow
return grad*Δx + fs.Values[*lastRow], nil
}
}
err := fill(ctx, fillFn, fs, omap, *lastRow, end+1, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
case Spline:
if method.Order == 3 {
splineVals := alg.(gospline.Spline).Range(float64(*lastRow), float64(end+1), 1)
fillFn := func(row int) (float64, error) {
return splineVals[row+1], nil
}
err := fill(ctx, fillFn, fs, omap, *lastRow, end+1, opts.FillDirection, opts.Limit)
if err != nil {
return nil, err
}
}
case Lagrange:
// Can't be used to extrapolate.
// Package returns error: Value to interpolate is too small and not in range
}
}
}
if opts.InPlace {
return nil, nil
}
return omap, nil
}