-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaminginterpolators.py
122 lines (98 loc) · 3.43 KB
/
streaminginterpolators.py
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
"""
File: streaminginterpolators.py
Author: Evan Smith
Date Created: 10/22/15
Date Last Modified: 10/23/15
Python Version: 2.7.10
Description: Classes to efficiently interpolate streamed data
"""
from abc import ABCMeta, abstractmethod
from bintrees import AVLTree
class StreamingInterpolatorBase(object):
''' Abstract base class for streaming interpolators '''
__metaclass__ = ABCMeta
@abstractmethod
def insert(self, x, val):
''' Register a datapoint '''
return
@abstractmethod
def getInterpolatedVal(self, x):
''' Get the interpolated value for the given x '''
return
class LinearStreamingInterpolator(StreamingInterpolatorBase):
''' Linear streaming interpolator '''
def __init__(self):
self._history = AVLTree()
def insert(self, x, val):
'''
Register a datapoint
x (float) : the x coordinate of the datapoint
val (float): the rest of the datapoint
'''
self._history.insert(x, val)
def getInterpolatedVal(self, x):
'''
Get the interpolated value for the given x
x (float) : the x coordinate of the datapoint
returns (float) : interpolated value
'''
if self._history.is_empty():
return None
leftX, leftVal = None, None
rightX, rightVal = None, None
if self._history.min_key() <= x:
leftX, leftVal = self._history.floor_item(x)
if leftX == x:
return leftVal
if self._history.max_key() >= x:
rightX, rightVal = self._history.ceiling_item(x)
if rightX == x:
return rightVal
# check if on edge
if leftVal == None:
return rightVal
elif rightVal == None:
return leftVal
# find weighted average of the vals of the closest enclosing data
# points
intervalLength = abs(x - leftX) + abs(x - rightX)
value = float(abs(x - rightX) * leftVal + abs(x - leftX)
* rightVal) / intervalLength
return value
# FIXME: yea, so this is useless if restricted to 1 dimension
class NearestNeighborStreamingInterpolator(StreamingInterpolatorBase):
''' Nearest Neighbor 1D Streaming Interpolator '''
def __init__(self):
self._history = AVLTree()
def insert(self, x, val):
'''
Register a datapoint
x (float) : the x coordinate of the datapoint
val (float): the rest of the datapoint
'''
self._history.insert(x, val)
def getInterpolatedVal(self, x):
'''
Get the interpolated value for the given x
x (float) : the x coordinate of the datapoint
returns (float) : interpolated value
'''
if self._history.is_empty():
return None
# find the nearest point to the left and right
leftX, leftVal = None, None
rightX, rightVal = None, None
if self._history.min_key() <= x:
leftX, leftVal = self._history.floor_item(x)
if self._history.max_key() >= x:
rightX, rightVal = self._history.ceiling_item(x)
# if there is only one neighbor, return it
if leftVal == None:
return rightVal
elif rightVal == None:
return leftVal
# return the nearest neighbor
if abs(x - leftX) < abs(x - rightX):
return leftVal
else:
return rightVal