# HG changeset patch # User Nicolas Saunier # Date 1719348040 14400 # Node ID bae8de98406fffa5942b4a034d38be04c8193d89 # Parent 9f1711a85c566301f854af5e13c4eddcfd1ad57f corrected bug in categorical value smoothing diff -r 9f1711a85c56 -r bae8de98406f trafficintelligence/tests/utils.txt --- 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) diff -r 9f1711a85c56 -r bae8de98406f trafficintelligence/utils.py --- 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):