Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/utils.py @ 1276:bae8de98406f
corrected bug in categorical value smoothing
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 25 Jun 2024 16:40:40 -0400 |
| parents | ad60e5adf084 |
| children | 76f5693b530c |
comparison
equal
deleted
inserted
replaced
| 1275:9f1711a85c56 | 1276:bae8de98406f |
|---|---|
| 418 return sqrt((x2-x1)**2+(y2-y1)**2) | 418 return sqrt((x2-x1)**2+(y2-y1)**2) |
| 419 | 419 |
| 420 def crossProduct(l1, l2): | 420 def crossProduct(l1, l2): |
| 421 return l1[0]*l2[1]-l1[1]*l2[0] | 421 return l1[0]*l2[1]-l1[1]*l2[0] |
| 422 | 422 |
| 423 def filterCategoricalMovingWindow(cat_list, halfWidth): | 423 def filterCategoricalMovingWindow(categoricalList, halfWidth): |
| 424 ''' Return a list of categories/values smoothed according to a window. | 424 ''' Return a list of categories/values smoothed according to a window. |
| 425 halfWidth is the search radius on either side''' | 425 halfWidth is the search radius on either side''' |
| 426 smoothed = deepcopy(cat_list) | 426 length = len(categoricalList) |
| 427 for point in range(len(cat_list)): | 427 smoothed = [0]*length |
| 428 lower_bound_check = max(0,point-halfWidth) | 428 for point in range(length): |
| 429 upper_bound_check = min(len(cat_list)-1,point+halfWidth+1) | 429 lowerBound = max(0,point-halfWidth) |
| 430 window_values = cat_list[lower_bound_check:upper_bound_check] | 430 upperBound = min(length,point+halfWidth+1) |
| 431 smoothed[point] = max(set(window_values), key=window_values.count) | 431 window = categoricalList[lowerBound:upperBound] |
| 432 smoothed[point] = max(set(window), key=window.count) | |
| 432 return smoothed | 433 return smoothed |
| 433 | 434 |
| 434 def filterMovingWindow(inputSignal, halfWidth): | 435 def filterMovingWindow(inputSignal, halfWidth): |
| 435 '''Returns an array obtained after the smoothing of the 1-D input by a moving average | 436 '''Returns an array obtained after the smoothing of the 1-D input by a moving average |
| 436 The size of the output depends on the mode: 'full', 'same', 'valid' | 437 The size of the output depends on the mode: 'full', 'same', 'valid' |
