diff 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
line wrap: on
line diff
--- a/python/indicators.py	Mon Aug 10 17:51:49 2015 -0400
+++ b/python/indicators.py	Mon Aug 10 17:52:19 2015 -0400
@@ -8,6 +8,9 @@
 from numpy import array, arange, mean, floor, mean
 
 
+def multivariateName(indicatorNames):
+    return '_'.join(indicatorNames)
+
 # need for a class representing the indicators, their units, how to print them in graphs...
 class TemporalIndicator(object):
     '''Class for temporal indicators
@@ -44,10 +47,7 @@
 
     def __getitem__(self, t):
         'Returns the value at time t'
-        if t in self.values.keys():
-            return self.values[t]
-        else:
-            return None
+        return self.values.get(t)
 
     def getIthValue(self, i):
         sortedKeys = sorted(self.values.keys())
@@ -86,18 +86,27 @@
         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
+
+    @classmethod
+    def createMultivariate(cls, indicators):
+        '''Creates a new temporal indicator where the value at each instant is a list 
+        of the indicator values at the instant, in the same order
+        the time interval will be the union of the time intervals of the indicators
+        name is concatenation of the indicator names'''
+        if len(indicators) < 2:
+            print('Error creating multivariate indicator with only {} indicator'.format(len(indicators)))
+            return None
 
+        timeInterval = moving.TimeInterval.unionIntervals([indic.getTimeInterval() for indic in indicators])
+        values = {}
+        for t in timeInterval:
+            tmpValues = [indic[t] for indic in indicators]
+            uniqueValues = set(tmpValues)
+            if len(uniqueValues) >= 2 or uniqueValues.pop() is not None:
+                values[t] = tmpValues
+        return cls(multivariateName([indic.name for indic in indicators]), values)
+
+# TODO static method avec class en parametre pour faire des indicateurs agrege, list par instant
 
 def l1Distance(x, y): # lambda x,y:abs(x-y)
     if x is None or y is None:
@@ -105,12 +114,20 @@
     else:
         return abs(x-y)
 
+def multiL1Matching(x, y, thresholds, proportionMatching=1.):
+    n = 0
+    nDimensions = len(x)
+    for i in range(nDimensions):
+        if l1Distance(x[i], y[i]) <= thresholds[i]:
+            n += 1
+    return n >= nDimensions*proportionMatching
+
 from utils import LCSS as utilsLCSS
 
 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)
+        utilsLCSS.__init__(self, similarityFunc = similarityFunc, delta = delta, aligned = aligned, lengthFunc = lengthFunc)
         self.minLength = minLength
 
     def checkIndicator(self, indicator):