Mercurial Hosting > traffic-intelligence
changeset 287:66691c06928c
first version of indicator LCSS
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 27 Jan 2013 00:51:48 -0500 |
parents | fa95796a76b3 |
children | e0d41c7f53d4 |
files | python/indicators.py python/tests/indicators.txt |
diffstat | 2 files changed, 28 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/python/indicators.py Sun Jan 27 00:22:55 2013 -0500 +++ b/python/indicators.py Sun Jan 27 00:51:48 2013 -0500 @@ -34,26 +34,26 @@ self.timeInterval = moving.TimeInterval() self.maxValue = maxValue + def __len__(self): + return len(self.values) + def empty(self): return len(self.values) == 0 def __getitem__(self, i): - 'Returns ith value' + 'Returns ith value in time interval' + if i in self.values.keys(): + return self.values[i] + else: + return None + + def getIthValue(self, i): sortedKeys = sorted(self.values.keys()) if 0<=i<len(sortedKeys): return self.values[sortedKeys[i]] else: return None - def valueAtInstant(self, i): - if i in self.values.keys(): - return self.values[i] - else: - return None - - def __len__(self): - return len(self.values) - def __iter__(self): self.iterInstantNum = 0 # index in the interval or keys of the dict return self @@ -64,13 +64,13 @@ raise StopIteration else: self.iterInstantNum += 1 - return self.__getitem__(self.iterInstantNum-1) + return self.getIthValue(self.iterInstantNum-1) def getTimeInterval(self): return self.timeInterval def getValues(self): - return self.values.values() + return [self.__getitem__(t) for t in self.timeInterval] def getAngleValues(self): '''if the indicator is a function of an angle, @@ -98,7 +98,14 @@ ''' compute the distance between two indicators using LCSS two common methods are used: min or mean of the indicators length''' from utils import LCSS - lcss = LCSS(indicator1, indicator2, threshold, lambda x,y:abs(x-y), delta) + + def distance(x, y): # lambda x,y:abs(x-y) + if x == None or y == None: + return float('inf') + else: + return abs(x-y) + + lcss = LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) if method == 'min': denominator = min(len(indicator1), len(indicator2)) elif method == 'mean':
--- a/python/tests/indicators.txt Sun Jan 27 00:22:55 2013 -0500 +++ b/python/tests/indicators.txt Sun Jan 27 00:51:48 2013 -0500 @@ -4,20 +4,20 @@ >>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6)) >>> indic1.empty() False ->>> indic1.valueAtInstant(5) +>>> indic1.getIthValue(1) 3 ->>> indic1.valueAtInstant(3) ->>> indic1[1] -3 ->>> indic1[5] +>>> indic1.getIthValue(3) +>>> indic1[6] +-4 +>>> indic1[7] >>> [v for v in indic1] [0, 3, -4] >>> indic1 = TemporalIndicator('bla', {2:0,4:3,5:-5}) ->>> indic1.valueAtInstant(4) +>>> indic1.getIthValue(1) 3 ->>> indic1.valueAtInstant(3) +>>> indic1.getIthValue(3) >>> indic1[2] --5 +0 >>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]]) >>> indicatorMap([1,2,3], t1, 1)