nsaunier/traffic-intelligence
corrected bug in categorical value smoothing
Commit bae8de98406f · Nicolas Saunier · 2024-06-25 16:40 -0400
Comments
No comments yet.
Diff
diff --git a/trafficintelligence/tests/utils.txt b/trafficintelligence/tests/utils.txt
--- a/trafficintelligence/tests/utils.txt
+++ b/trafficintelligence/tests/utils.txt
@@ -47,6 +47,15 @@
>>> values[-1]
6.0
+>>> filterCategoricalMovingWindow([3]*3 + [4]*4, 2)
+[3, 3, 3, 4, 4, 4, 4]
+>>> filterCategoricalMovingWindow([3]*6 + [4], 2)
+[3, 3, 3, 3, 3, 3, 3]
+>>> filterCategoricalMovingWindow(['a']*3 + ['c'] + ['b']*3, 2)
+['a', 'a', 'a', 'b', 'b', 'b', 'b']
+>>> filterCategoricalMovingWindow([3], 2)
+[3]
+
>>> filterMovingWindow(arange(10), 3)
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> filterMovingWindow(list(range(10)), 3)
diff --git a/trafficintelligence/utils.py b/trafficintelligence/utils.py
--- a/trafficintelligence/utils.py
+++ b/trafficintelligence/utils.py
@@ -420,15 +420,16 @@
def crossProduct(l1, l2):
return l1[0]*l2[1]-l1[1]*l2[0]
-def filterCategoricalMovingWindow(cat_list, halfWidth):
+def filterCategoricalMovingWindow(categoricalList, halfWidth):
''' Return a list of categories/values smoothed according to a window.
halfWidth is the search radius on either side'''
- smoothed = deepcopy(cat_list)
- for point in range(len(cat_list)):
- lower_bound_check = max(0,point-halfWidth)
- upper_bound_check = min(len(cat_list)-1,point+halfWidth+1)
- window_values = cat_list[lower_bound_check:upper_bound_check]
- smoothed[point] = max(set(window_values), key=window_values.count)
+ length = len(categoricalList)
+ smoothed = [0]*length
+ for point in range(length):
+ lowerBound = max(0,point-halfWidth)
+ upperBound = min(length,point+halfWidth+1)
+ window = categoricalList[lowerBound:upperBound]
+ smoothed[point] = max(set(window), key=window.count)
return smoothed
def filterMovingWindow(inputSignal, halfWidth):