comparison python/indicators.py @ 729:dad99b86a104 dev

merge with default
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 10 Aug 2015 17:52:19 -0400
parents c6d4ea05a2d0
children 933670761a57
comparison
equal deleted inserted replaced
728:4e89341edd29 729:dad99b86a104
5 #import matplotlib.nxutils as nx 5 #import matplotlib.nxutils as nx
6 from matplotlib.pyplot import plot, ylim 6 from matplotlib.pyplot import plot, ylim
7 from matplotlib.pylab import find 7 from matplotlib.pylab import find
8 from numpy import array, arange, mean, floor, mean 8 from numpy import array, arange, mean, floor, mean
9 9
10
11 def multivariateName(indicatorNames):
12 return '_'.join(indicatorNames)
10 13
11 # need for a class representing the indicators, their units, how to print them in graphs... 14 # need for a class representing the indicators, their units, how to print them in graphs...
12 class TemporalIndicator(object): 15 class TemporalIndicator(object):
13 '''Class for temporal indicators 16 '''Class for temporal indicators
14 i.e. indicators that take a value at specific instants 17 i.e. indicators that take a value at specific instants
42 def empty(self): 45 def empty(self):
43 return len(self.values) == 0 46 return len(self.values) == 0
44 47
45 def __getitem__(self, t): 48 def __getitem__(self, t):
46 'Returns the value at time t' 49 'Returns the value at time t'
47 if t in self.values.keys(): 50 return self.values.get(t)
48 return self.values[t]
49 else:
50 return None
51 51
52 def getIthValue(self, i): 52 def getIthValue(self, i):
53 sortedKeys = sorted(self.values.keys()) 53 sortedKeys = sorted(self.values.keys())
54 if 0<=i<len(sortedKeys): 54 if 0<=i<len(sortedKeys):
55 return self.values[sortedKeys[i]] 55 return self.values[sortedKeys[i]]
84 marker = '' 84 marker = ''
85 time = sorted(self.values.keys()) 85 time = sorted(self.values.keys())
86 plot([(x+timeShift)/xfactor for x in time], [self.values[i]/yfactor for i in time], options+marker, **kwargs) 86 plot([(x+timeShift)/xfactor for x in time], [self.values[i]/yfactor for i in time], options+marker, **kwargs)
87 if self.maxValue: 87 if self.maxValue:
88 ylim(ymax = self.maxValue) 88 ylim(ymax = self.maxValue)
89 89
90 def valueSorted(self): 90 @classmethod
91 ''' return the values after sort the keys in the indicator 91 def createMultivariate(cls, indicators):
92 This should probably not be used: to delete''' 92 '''Creates a new temporal indicator where the value at each instant is a list
93 print('Deprecated: values should not be accessed in this way') 93 of the indicator values at the instant, in the same order
94 values=[] 94 the time interval will be the union of the time intervals of the indicators
95 keys = self.values.keys() 95 name is concatenation of the indicator names'''
96 keys.sort() 96 if len(indicators) < 2:
97 for key in keys: 97 print('Error creating multivariate indicator with only {} indicator'.format(len(indicators)))
98 values.append(self.values[key]) 98 return None
99 return values 99
100 100 timeInterval = moving.TimeInterval.unionIntervals([indic.getTimeInterval() for indic in indicators])
101 values = {}
102 for t in timeInterval:
103 tmpValues = [indic[t] for indic in indicators]
104 uniqueValues = set(tmpValues)
105 if len(uniqueValues) >= 2 or uniqueValues.pop() is not None:
106 values[t] = tmpValues
107 return cls(multivariateName([indic.name for indic in indicators]), values)
108
109 # TODO static method avec class en parametre pour faire des indicateurs agrege, list par instant
101 110
102 def l1Distance(x, y): # lambda x,y:abs(x-y) 111 def l1Distance(x, y): # lambda x,y:abs(x-y)
103 if x is None or y is None: 112 if x is None or y is None:
104 return float('inf') 113 return float('inf')
105 else: 114 else:
106 return abs(x-y) 115 return abs(x-y)
107 116
117 def multiL1Matching(x, y, thresholds, proportionMatching=1.):
118 n = 0
119 nDimensions = len(x)
120 for i in range(nDimensions):
121 if l1Distance(x[i], y[i]) <= thresholds[i]:
122 n += 1
123 return n >= nDimensions*proportionMatching
124
108 from utils import LCSS as utilsLCSS 125 from utils import LCSS as utilsLCSS
109 126
110 class LCSS(utilsLCSS): 127 class LCSS(utilsLCSS):
111 '''Adapted LCSS class for indicators, same pattern''' 128 '''Adapted LCSS class for indicators, same pattern'''
112 def __init__(self, similarityFunc, delta = float('inf'), minLength = 0, aligned = False, lengthFunc = min): 129 def __init__(self, similarityFunc, delta = float('inf'), minLength = 0, aligned = False, lengthFunc = min):
113 utilsLCSS.__init__(self, similarityFunc, delta, aligned, lengthFunc) 130 utilsLCSS.__init__(self, similarityFunc = similarityFunc, delta = delta, aligned = aligned, lengthFunc = lengthFunc)
114 self.minLength = minLength 131 self.minLength = minLength
115 132
116 def checkIndicator(self, indicator): 133 def checkIndicator(self, indicator):
117 return indicator is not None and len(indicator) >= self.minLength 134 return indicator is not None and len(indicator) >= self.minLength
118 135