Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/utils.py @ 1204:a12d126346ff
merge
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 22 Mar 2023 22:01:12 -0400 |
| parents | 4356065ed3ca |
| children | 69b531c7a061 |
comparison
equal
deleted
inserted
replaced
| 1203:7b3384a8e409 | 1204:a12d126346ff |
|---|---|
| 429 upper_bound_check = min(len(cat_list)-1,point+halfWidth+1) | 429 upper_bound_check = min(len(cat_list)-1,point+halfWidth+1) |
| 430 window_values = cat_list[lower_bound_check:upper_bound_check] | 430 window_values = cat_list[lower_bound_check:upper_bound_check] |
| 431 smoothed[point] = max(set(window_values), key=window_values.count) | 431 smoothed[point] = max(set(window_values), key=window_values.count) |
| 432 return smoothed | 432 return smoothed |
| 433 | 433 |
| 434 def filterMovingWindow(inputSignal, halfWidth): | 434 def filterMovingWindow(inputSignal, halfWidth, mode = 'valid'): |
| 435 '''Returns an array obtained after the smoothing of the input by a moving average | 435 '''Returns an array obtained after the smoothing of the input by a moving average |
| 436 The first and last points are copied from the original.''' | 436 The size of the output depends on the mode: 'full', 'same', 'valid' |
| 437 width = float(halfWidth*2+1) | 437 See https://numpy.org/doc/stable/reference/generated/numpy.convolve.html.''' |
| 438 width = min(len(inputSignal), int(halfWidth*2+1)) | |
| 438 win = ones(width,'d') | 439 win = ones(width,'d') |
| 439 result = convolve(win/width,array(inputSignal),'same') | 440 return convolve(win/width, array(inputSignal), mode) |
| 440 result[:halfWidth] = inputSignal[:halfWidth] | |
| 441 result[-halfWidth:] = inputSignal[-halfWidth:] | |
| 442 return result | |
| 443 | 441 |
| 444 def linearRegression(x, y, deg = 1, plotData = False): | 442 def linearRegression(x, y, deg = 1, plotData = False): |
| 445 '''returns the least square estimation of the linear regression of y = ax+b | 443 '''returns the least square estimation of the linear regression of y = ax+b |
| 446 as well as the plot''' | 444 as well as the plot''' |
| 447 coef = polyfit(x, y, deg) | 445 coef = polyfit(x, y, deg) |
