comparison 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
comparison
equal deleted inserted replaced
598:11f96bd08552 614:5e09583275a4
16 16
17 it should have more information like name, unit''' 17 it should have more information like name, unit'''
18 18
19 def __init__(self, name, values, timeInterval=None, maxValue = None): 19 def __init__(self, name, values, timeInterval=None, maxValue = None):
20 self.name = name 20 self.name = name
21 self.isCosine = (name.find('Cosine') >= 0)
22 if timeInterval: 21 if timeInterval:
23 assert len(values) == timeInterval.length() 22 assert len(values) == timeInterval.length()
24 self.timeInterval = timeInterval 23 self.timeInterval = timeInterval
25 self.values = {} 24 self.values = {}
26 for i in xrange(int(round(self.timeInterval.length()))): 25 for i in xrange(int(round(self.timeInterval.length()))):
67 return self.getIthValue(self.iterInstantNum-1) 66 return self.getIthValue(self.iterInstantNum-1)
68 67
69 def getTimeInterval(self): 68 def getTimeInterval(self):
70 return self.timeInterval 69 return self.timeInterval
71 70
71 def getName(self):
72 return self.name
73
72 def getValues(self): 74 def getValues(self):
73 return [self.__getitem__(t) for t in self.timeInterval] 75 return [self.__getitem__(t) for t in self.timeInterval]
74 76
75 def getAngleValues(self): 77 def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs):
76 '''if the indicator is a function of an angle,
77 transform it to an angle (eg cos)
78 (no transformation otherwise)'''
79 from numpy import arccos
80 values = self.getValues()
81 if self.isCosine:
82 return [arccos(c) for c in values]
83 else:
84 return values
85
86 def plot(self, options = '', xfactor = 1., **kwargs):
87 from matplotlib.pylab import plot,ylim 78 from matplotlib.pylab import plot,ylim
88 if self.getTimeInterval().length() == 1: 79 if self.getTimeInterval().length() == 1:
89 marker = 'o' 80 marker = 'o'
90 else: 81 else:
91 marker = '' 82 marker = ''
92 time = sorted(self.values.keys()) 83 time = sorted(self.values.keys())
93 plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) 84 plot([(x+timeShift)/xfactor for x in time], [self.values[i]/yfactor for i in time], options+marker, **kwargs)
94 if self.maxValue: 85 if self.maxValue:
95 ylim(ymax = self.maxValue) 86 ylim(ymax = self.maxValue)
96 87
97 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'): 88 def valueSorted(self):
98 ''' compute the distance between two indicators using LCSS 89 ''' return the values after sort the keys in the indicator
99 two common methods are used: min or mean of the indicators length''' 90 This should probably not be used: to delete'''
100 from utils import LCSS 91 print('Deprecated: values should not be accessed in this way')
101 92 values=[]
102 def distance(x, y): # lambda x,y:abs(x-y) 93 keys = self.values.keys()
103 if x == None or y == None: 94 keys.sort()
104 return float('inf') 95 for key in keys:
105 else: 96 values.append(self.values[key])
106 return abs(x-y) 97 return values
107 98
108 lcss = LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) 99
109 if method == 'min': 100 def l1Distance(x, y): # lambda x,y:abs(x-y)
110 denominator = min(len(indicator1), len(indicator2)) 101 if x == None or y == None:
111 elif method == 'mean': 102 return float('inf')
112 denominator = float(len(indicator1) + len(indicator2))/2
113 else: 103 else:
114 print('Unknown denominator method name') 104 return abs(x-y)
115 denominator = 1. 105
116 return 1-float(lcss)/denominator 106 from utils import LCSS as utilsLCSS
117 107
108 class LCSS(utilsLCSS):
109 '''Adapted LCSS class for indicators, same pattern'''
110 def __init__(self, similarityFunc, delta = float('inf'), minLength = 0, aligned = False, lengthFunc = min):
111 utilsLCSS.__init__(self, similarityFunc, delta, aligned, lengthFunc)
112 self.minLength = minLength
113
114 def checkIndicator(self, indicator):
115 return indicator != None and len(indicator) >= self.minLength
116
117 def compute(self, indicator1, indicator2, computeSubSequence = False):
118 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2):
119 return self._compute(indicator1.getValues(), indicator2.getValues(), computeSubSequence)
120 else:
121 return 0
122
123 def computeNormalized(self, indicator1, indicator2, computeSubSequence = False):
124 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2):
125 return self._computeNormalized(indicator1.getValues(), indicator2.getValues(), computeSubSequence)
126 else:
127 return 0.
128
129 def computeDistance(self, indicator1, indicator2, computeSubSequence = False):
130 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2):
131 return self._computeDistance(indicator1.getValues(), indicator2.getValues(), computeSubSequence)
132 else:
133 return 1.
134
118 class SeverityIndicator(TemporalIndicator): 135 class SeverityIndicator(TemporalIndicator):
119 '''Class for severity indicators 136 '''Class for severity indicators
120 field mostSevereIsMax is True 137 field mostSevereIsMax is True
121 if the most severe value taken by the indicator is the maximum''' 138 if the most severe value taken by the indicator is the maximum'''
122 139