comparison python/indicators.py @ 727:c6d4ea05a2d0

adding ability to deal with multivariate indicators
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 10 Aug 2015 01:06:59 -0400
parents b75d0c258ca9
children 933670761a57
comparison
equal deleted inserted replaced
726:43ae3a1af290 727:c6d4ea05a2d0
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
81 marker = '' 84 marker = ''
82 time = sorted(self.values.keys()) 85 time = sorted(self.values.keys())
83 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)
84 if self.maxValue: 87 if self.maxValue:
85 ylim(ymax = self.maxValue) 88 ylim(ymax = self.maxValue)
86 89
90 @classmethod
91 def createMultivariate(cls, indicators):
92 '''Creates a new temporal indicator where the value at each instant is a list
93 of the indicator values at the instant, in the same order
94 the time interval will be the union of the time intervals of the indicators
95 name is concatenation of the indicator names'''
96 if len(indicators) < 2:
97 print('Error creating multivariate indicator with only {} indicator'.format(len(indicators)))
98 return None
99
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
87 110
88 def l1Distance(x, y): # lambda x,y:abs(x-y) 111 def l1Distance(x, y): # lambda x,y:abs(x-y)
89 if x is None or y is None: 112 if x is None or y is None:
90 return float('inf') 113 return float('inf')
91 else: 114 else:
92 return abs(x-y) 115 return abs(x-y)
93 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
94 from utils import LCSS as utilsLCSS 125 from utils import LCSS as utilsLCSS
95 126
96 class LCSS(utilsLCSS): 127 class LCSS(utilsLCSS):
97 '''Adapted LCSS class for indicators, same pattern''' 128 '''Adapted LCSS class for indicators, same pattern'''
98 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):
99 utilsLCSS.__init__(self, similarityFunc, delta, aligned, lengthFunc) 130 utilsLCSS.__init__(self, similarityFunc = similarityFunc, delta = delta, aligned = aligned, lengthFunc = lengthFunc)
100 self.minLength = minLength 131 self.minLength = minLength
101 132
102 def checkIndicator(self, indicator): 133 def checkIndicator(self, indicator):
103 return indicator is not None and len(indicator) >= self.minLength 134 return indicator is not None and len(indicator) >= self.minLength
104 135