comparison trafficintelligence/utils.py @ 1250:77fbd0e2ba7d

dltrack works with moving average filtering
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 15 Feb 2024 22:04:35 -0500
parents 69b531c7a061
children ad60e5adf084
comparison
equal deleted inserted replaced
1249:2aa56b101041 1250:77fbd0e2ba7d
9 from collections import Counter 9 from collections import Counter
10 10
11 from scipy.stats import rv_continuous, kruskal, shapiro, lognorm, norm, t, chi2_contingency 11 from scipy.stats import rv_continuous, kruskal, shapiro, lognorm, norm, t, chi2_contingency
12 from scipy.spatial import distance 12 from scipy.spatial import distance
13 from scipy.sparse import dok_matrix 13 from scipy.sparse import dok_matrix
14 from numpy import zeros, array, exp, sum as npsum, int64 as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit 14 from numpy import zeros, array, exp, sum as npsum, int64 as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit, float64
15 from numpy.random import random_sample, permutation as nppermutation 15 from numpy.random import random_sample, permutation as nppermutation
16 from pandas import DataFrame, concat, crosstab 16 from pandas import DataFrame, concat, crosstab
17 import matplotlib.pyplot as plt 17 import matplotlib.pyplot as plt
18 18
19 datetimeFormat = "%Y-%m-%d %H:%M:%S" 19 datetimeFormat = "%Y-%m-%d %H:%M:%S"
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, mode = 'valid'): 434 def filterMovingWindow(inputSignal, halfWidth):
435 '''Returns an array obtained after the smoothing of the 1-D input by a moving average 435 '''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' 436 The size of the output depends on the mode: 'full', 'same', 'valid'
437 See https://numpy.org/doc/stable/reference/generated/numpy.convolve.html.''' 437 See https://numpy.org/doc/stable/reference/generated/numpy.convolve.html.'''
438 width = min(len(inputSignal), int(halfWidth*2+1)) 438 halfWidth = min(floor((len(inputSignal)-1)/2.), halfWidth)
439 win = ones(width,'d') 439 win = ones(2*halfWidth+1)/(2*halfWidth+1)
440 return convolve(win/width, array(inputSignal), mode) 440 filtered = array(inputSignal, dtype=float64)
441 filtered[halfWidth:-halfWidth] = convolve(inputSignal, win, 'valid') # .ravel()
442 for i in range(halfWidth-1):
443 filtered[i] = sum(inputSignal[:2*i+1])/(2*i+1)
444 filtered[-1-i] = sum(inputSignal[-1-2*i:])/(2*i+1)
445 return filtered
441 446
442 def linearRegression(x, y, deg = 1, plotData = False): 447 def linearRegression(x, y, deg = 1, plotData = False):
443 '''returns the least square estimation of the linear regression of y = ax+b 448 '''returns the least square estimation of the linear regression of y = ax+b
444 as well as the plot''' 449 as well as the plot'''
445 coef = polyfit(x, y, deg) 450 coef = polyfit(x, y, deg)