changeset 321:a5e40bd04cf4

rearranged LCSS indicator functions
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 07 May 2013 02:02:55 +0200
parents 419f30491a4b
children 28661c5887d3
files python/events.py python/indicators.py
diffstat 2 files changed, 31 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/python/events.py	Mon May 06 17:20:07 2013 +0200
+++ b/python/events.py	Tue May 07 02:02:55 2013 +0200
@@ -42,10 +42,11 @@
         self.indicators = {}
 
     def getIndicator(self, indicatorName):
-        return self.indicators[indicatorName]
+        return self.indicators.get(indicatorName, None)
 
     def addIndicator(self, indicator):
-        self.indicators[indicator.name] = indicator
+        if indicator:
+            self.indicators[indicator.name] = indicator
 
     def computeIndicators(self):
         '''Computes the collision course cosine only if the cosine is positive'''
--- a/python/indicators.py	Mon May 06 17:20:07 2013 +0200
+++ b/python/indicators.py	Tue May 07 02:02:55 2013 +0200
@@ -96,7 +96,8 @@
 	
     def valueSorted(self):
 	''' return the values after sort the keys in the indicator
-    This should probably not be used: to delete''' 
+        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()
@@ -108,6 +109,7 @@
     def getDLCSS(indic1, indic2, threshold, delta = float('inf') , method ='min' ):
 	''' compute the distance between two indicators using LCSS
 	two common methods are used: min or mean of the indicators length'''
+        print('Deprecated: this is not appropriate method for indicator comparison')
         l1 = indic1.valueSorted
         l2 = indic2.valueSorted
         DLCSS = None
@@ -119,26 +121,39 @@
         return DLCSS
 		
 
-def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'):
-    ''' compute the distance between two indicators using LCSS
-    two common methods are used: min or mean of the indicators length'''
+def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')):
+    ''' compute the LCSS between two indicators using LCSS'''
     from utils import LCSS
 
     def distance(x, y): # lambda x,y:abs(x-y)
         if x == None or y == None:
             return float('inf')
-        else: 
+        else:
             return abs(x-y)
+    if indicator1 and indicator2:
+        return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta)
+    else:
+        return 0
+
+def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'):
+    ''' compute the normalized LCSS between two indicators using LCSS
+    ie, the LCSS divided by the min or mean of the indicator lengths'''
 
-    lcss = LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta)
-    if method == 'min':
-        denominator = min(len(indicator1), len(indicator2))
-    elif method == 'mean':
-        denominator = float(len(indicator1) + len(indicator2))/2
+    if indicator1 and indicator2:
+        if method == 'min':
+            denominator = min(len(indicator1), len(indicator2))
+        elif method == 'mean':
+            denominator = float(len(indicator1) + len(indicator2))/2
+        else:
+            print('Unknown denominator method name')
+            denominator = 1.
+        return float(computeLCSS(indicator1, indicator2, threshold, delta))/denominator
     else:
-        print('Unknown denominator method name')
-        denominator = 1.
-    return 1-float(lcss)/denominator
+        return 0.
+
+def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = 'min'):
+    ''' compute the LCSS distance between two indicators using LCSS'''
+    return 1-computeNormalizedLCSS(indicator1, indicator2, threshold, delta, method)
 
 class SeverityIndicator(TemporalIndicator):
     '''Class for severity indicators