changeset 1276:bae8de98406f default tip

corrected bug in categorical value smoothing
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 25 Jun 2024 16:40:40 -0400
parents 9f1711a85c56
children
files trafficintelligence/tests/utils.txt trafficintelligence/utils.py
diffstat 2 files changed, 17 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/tests/utils.txt	Wed Jun 19 16:11:35 2024 -0400
+++ b/trafficintelligence/tests/utils.txt	Tue Jun 25 16:40:40 2024 -0400
@@ -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)
--- a/trafficintelligence/utils.py	Wed Jun 19 16:11:35 2024 -0400
+++ b/trafficintelligence/utils.py	Tue Jun 25 16:40:40 2024 -0400
@@ -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):