Mercurial Hosting > traffic-intelligence
diff python/indicators.py @ 322:28661c5887d3
Corrected a major bug for LCSS
Added functions to test all alignments when computing the LCSS with a finite delta
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 07 May 2013 18:43:40 +0200 |
parents | a5e40bd04cf4 |
children | 1046b7346886 |
line wrap: on
line diff
--- a/python/indicators.py Tue May 07 02:02:55 2013 +0200 +++ b/python/indicators.py Tue May 07 18:43:40 2013 +0200 @@ -121,39 +121,57 @@ return DLCSS +def distanceForLCSS(x, y): # lambda x,y:abs(x-y) + if x == None or y == None: + return float('inf') + else: + return abs(x-y) + +# non-aligned LCSS computations, ok for delta = inf 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: - return abs(x-y) if indicator1 and indicator2: - return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) + return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) else: return 0 -def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'): +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''' - + from utils import normalizedLCSS 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 + return normalizedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) else: return 0. -def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = 'min'): +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) + from utils import DLCSS + return DLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) + +# aligned LCSS computations +def computeAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf')): + ''' compute the aligned LCSS between two indicators using LCSS''' + from utils import alignedLCSS + if indicator1 and indicator2: + return alignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) + else: + return 0 + +def computeNormalizedAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): + ''' compute the normalized aligned LCSS between two indicators using LCSS + ie, the LCSS divided by the min or mean of the indicator lengths''' + from utils import normalizedAlignedLCSS + if indicator1 and indicator2: + return normalizedAlignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) + else: + return 0. + +def computeAlignedDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): + ''' compute the aligned LCSS distance between two indicators using LCSS''' + from utils import alignedDLCSS + return alignedDLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) class SeverityIndicator(TemporalIndicator): '''Class for severity indicators