Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 321:a5e40bd04cf4 | 322:28661c5887d3 |
|---|---|
| 119 average = len(l1)+len(l2)/2 | 119 average = len(l1)+len(l2)/2 |
| 120 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average) | 120 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average) |
| 121 return DLCSS | 121 return DLCSS |
| 122 | 122 |
| 123 | 123 |
| 124 def distanceForLCSS(x, y): # lambda x,y:abs(x-y) | |
| 125 if x == None or y == None: | |
| 126 return float('inf') | |
| 127 else: | |
| 128 return abs(x-y) | |
| 129 | |
| 130 # non-aligned LCSS computations, ok for delta = inf | |
| 124 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')): | 131 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')): |
| 125 ''' compute the LCSS between two indicators using LCSS''' | 132 ''' compute the LCSS between two indicators using LCSS''' |
| 126 from utils import LCSS | 133 from utils import LCSS |
| 127 | 134 if indicator1 and indicator2: |
| 128 def distance(x, y): # lambda x,y:abs(x-y) | 135 return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) |
| 129 if x == None or y == None: | |
| 130 return float('inf') | |
| 131 else: | |
| 132 return abs(x-y) | |
| 133 if indicator1 and indicator2: | |
| 134 return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) | |
| 135 else: | 136 else: |
| 136 return 0 | 137 return 0 |
| 137 | 138 |
| 138 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'): | 139 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): |
| 139 ''' compute the normalized LCSS between two indicators using LCSS | 140 ''' compute the normalized LCSS between two indicators using LCSS |
| 140 ie, the LCSS divided by the min or mean of the indicator lengths''' | 141 ie, the LCSS divided by the min or mean of the indicator lengths''' |
| 141 | 142 from utils import normalizedLCSS |
| 142 if indicator1 and indicator2: | 143 if indicator1 and indicator2: |
| 143 if method == 'min': | 144 return normalizedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) |
| 144 denominator = min(len(indicator1), len(indicator2)) | |
| 145 elif method == 'mean': | |
| 146 denominator = float(len(indicator1) + len(indicator2))/2 | |
| 147 else: | |
| 148 print('Unknown denominator method name') | |
| 149 denominator = 1. | |
| 150 return float(computeLCSS(indicator1, indicator2, threshold, delta))/denominator | |
| 151 else: | 145 else: |
| 152 return 0. | 146 return 0. |
| 153 | 147 |
| 154 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = 'min'): | 148 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): |
| 155 ''' compute the LCSS distance between two indicators using LCSS''' | 149 ''' compute the LCSS distance between two indicators using LCSS''' |
| 156 return 1-computeNormalizedLCSS(indicator1, indicator2, threshold, delta, method) | 150 from utils import DLCSS |
| 151 return DLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | |
| 152 | |
| 153 # aligned LCSS computations | |
| 154 def computeAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf')): | |
| 155 ''' compute the aligned LCSS between two indicators using LCSS''' | |
| 156 from utils import alignedLCSS | |
| 157 if indicator1 and indicator2: | |
| 158 return alignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) | |
| 159 else: | |
| 160 return 0 | |
| 161 | |
| 162 def computeNormalizedAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): | |
| 163 ''' compute the normalized aligned LCSS between two indicators using LCSS | |
| 164 ie, the LCSS divided by the min or mean of the indicator lengths''' | |
| 165 from utils import normalizedAlignedLCSS | |
| 166 if indicator1 and indicator2: | |
| 167 return normalizedAlignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | |
| 168 else: | |
| 169 return 0. | |
| 170 | |
| 171 def computeAlignedDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): | |
| 172 ''' compute the aligned LCSS distance between two indicators using LCSS''' | |
| 173 from utils import alignedDLCSS | |
| 174 return alignedDLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | |
| 157 | 175 |
| 158 class SeverityIndicator(TemporalIndicator): | 176 class SeverityIndicator(TemporalIndicator): |
| 159 '''Class for severity indicators | 177 '''Class for severity indicators |
| 160 field mostSevereIsMax is True | 178 field mostSevereIsMax is True |
| 161 if the most severe value taken by the indicator is the maximum''' | 179 if the most severe value taken by the indicator is the maximum''' |
