Mercurial > hg > nsaunier > traffic-intelligence
diff python/indicators.py @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
| author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
|---|---|
| date | Fri, 05 Dec 2014 12:13:53 -0500 |
| parents | eb8baa080470 |
| children | 2d1d33ae1c69 |
line wrap: on
line diff
--- a/python/indicators.py Thu Apr 18 15:29:33 2013 -0400 +++ b/python/indicators.py Fri Dec 05 12:13:53 2014 -0500 @@ -18,7 +18,6 @@ def __init__(self, name, values, timeInterval=None, maxValue = None): self.name = name - self.isCosine = (name.find('Cosine') >= 0) if timeInterval: assert len(values) == timeInterval.length() self.timeInterval = timeInterval @@ -69,52 +68,70 @@ def getTimeInterval(self): return self.timeInterval + def getName(self): + return self.name + def getValues(self): return [self.__getitem__(t) for t in self.timeInterval] - def getAngleValues(self): - '''if the indicator is a function of an angle, - transform it to an angle (eg cos) - (no transformation otherwise)''' - from numpy import arccos - values = self.getValues() - if self.isCosine: - return [arccos(c) for c in values] - else: - return values - - def plot(self, options = '', xfactor = 1., **kwargs): + def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs): from matplotlib.pylab import plot,ylim if self.getTimeInterval().length() == 1: marker = 'o' else: marker = '' time = sorted(self.values.keys()) - plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) + plot([(x+timeShift)/xfactor for x in time], [self.values[i]/yfactor for i in time], options+marker, **kwargs) if self.maxValue: ylim(ymax = self.maxValue) + + def valueSorted(self): + ''' return the values after sort the keys in the indicator + This should probably not be used: to delete''' + print('Deprecated: values should not be accessed in this way') + values=[] + keys = self.values.keys() + keys.sort() + for key in keys: + values.append(self.values[key]) + return values -def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'): - ''' compute the distance between two indicators using LCSS - two common methods are used: min or mean of the indicators length''' - from utils import LCSS + +def l1Distance(x, y): # lambda x,y:abs(x-y) + if x == None or y == None: + return float('inf') + else: + return abs(x-y) + +from utils import LCSS as utilsLCSS - def distance(x, y): # lambda x,y:abs(x-y) - if x == None or y == None: - return float('inf') - else: - return abs(x-y) +class LCSS(utilsLCSS): + '''Adapted LCSS class for indicators, same pattern''' + def __init__(self, similarityFunc, delta = float('inf'), minLength = 0, aligned = False, lengthFunc = min): + utilsLCSS.__init__(self, similarityFunc, delta, aligned, lengthFunc) + self.minLength = minLength + + def checkIndicator(self, indicator): + return indicator != None and len(indicator) >= self.minLength - lcss = LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) - if method == 'min': - denominator = min(len(indicator1), len(indicator2)) - elif method == 'mean': - denominator = float(len(indicator1) + len(indicator2))/2 - else: - print('Unknown denominator method name') - denominator = 1. - return 1-float(lcss)/denominator + def compute(self, indicator1, indicator2, computeSubSequence = False): + if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): + return self._compute(indicator1.getValues(), indicator2.getValues(), computeSubSequence) + else: + return 0 + def computeNormalized(self, indicator1, indicator2, computeSubSequence = False): + if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): + return self._computeNormalized(indicator1.getValues(), indicator2.getValues(), computeSubSequence) + else: + return 0. + + def computeDistance(self, indicator1, indicator2, computeSubSequence = False): + if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): + return self._computeDistance(indicator1.getValues(), indicator2.getValues(), computeSubSequence) + else: + return 1. + class SeverityIndicator(TemporalIndicator): '''Class for severity indicators field mostSevereIsMax is True
