Mercurial Hosting > traffic-intelligence
comparison python/indicators.py @ 368:2db4e76599a1
implemented subsequence extraction and rearranged arguments
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 15 Jul 2013 15:08:53 -0400 |
parents | 2f39c4ed0b62 |
children | 027e254f0b53 |
comparison
equal
deleted
inserted
replaced
367:d44eba0db517 | 368:2db4e76599a1 |
---|---|
94 keys.sort() | 94 keys.sort() |
95 for key in keys: | 95 for key in keys: |
96 values.append(self.values[key]) | 96 values.append(self.values[key]) |
97 return values | 97 return values |
98 | 98 |
99 @staticmethod | |
100 def getDLCSS(indic1, indic2, threshold, delta = float('inf') , method ='min' ): | |
101 ''' compute the distance between two indicators using LCSS | |
102 two common methods are used: min or mean of the indicators length''' | |
103 print('Deprecated: this is not appropriate method for indicator comparison') | |
104 l1 = indic1.valueSorted | |
105 l2 = indic2.valueSorted | |
106 DLCSS = None | |
107 if method == 'min': | |
108 DLCSS = 1- (LCSS(l1,l2, threshold, delta, distance))/min(len(l1),len(l2)) | |
109 if method == 'mean': | |
110 average = len(l1)+len(l2)/2 | |
111 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average) | |
112 return DLCSS | |
113 | |
114 | 99 |
115 def distanceForLCSS(x, y): # lambda x,y:abs(x-y) | 100 def distanceForLCSS(x, y): # lambda x,y:abs(x-y) |
116 if x == None or y == None: | 101 if x == None or y == None: |
117 return float('inf') | 102 return float('inf') |
118 else: | 103 else: |
121 # non-aligned LCSS computations, ok for delta = inf | 106 # non-aligned LCSS computations, ok for delta = inf |
122 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')): | 107 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')): |
123 ''' compute the LCSS between two indicators using LCSS''' | 108 ''' compute the LCSS between two indicators using LCSS''' |
124 from utils import LCSS | 109 from utils import LCSS |
125 if indicator1 and indicator2: | 110 if indicator1 and indicator2: |
126 return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) | 111 return LCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta) |
127 else: | 112 else: |
128 return 0 | 113 return 0 |
129 | 114 |
130 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): | 115 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): |
131 ''' compute the normalized LCSS between two indicators using LCSS | 116 ''' compute the normalized LCSS between two indicators using LCSS |
132 ie, the LCSS divided by the min or mean of the indicator lengths''' | 117 ie, the LCSS divided by the min or mean of the indicator lengths''' |
133 from utils import normalizedLCSS | 118 from utils import normalizedLCSS |
134 if indicator1 and indicator2: | 119 if indicator1 and indicator2: |
135 return normalizedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | 120 return normalizedLCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta, method) |
136 else: | 121 else: |
137 return 0. | 122 return 0. |
138 | 123 |
139 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): | 124 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): |
140 ''' compute the LCSS distance between two indicators using LCSS''' | 125 ''' compute the LCSS distance between two indicators using LCSS''' |
141 from utils import DLCSS | 126 from utils import DLCSS |
142 return DLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | 127 if indicator1 and indicator2: |
128 return DLCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta, method) | |
129 else: | |
130 return 1. | |
143 | 131 |
144 # aligned LCSS computations | 132 # aligned LCSS computations |
145 def computeAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf')): | 133 def computeAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf')): |
146 ''' compute the aligned LCSS between two indicators using LCSS''' | 134 ''' compute the aligned LCSS between two indicators using LCSS''' |
147 from utils import alignedLCSS | 135 from utils import alignedLCSS |
148 if indicator1 and indicator2: | 136 if indicator1 and indicator2: |
149 return alignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) | 137 return alignedLCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta) |
150 else: | 138 else: |
151 return 0 | 139 return 0 |
152 | 140 |
153 def computeNormalizedAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): | 141 def computeNormalizedAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): |
154 ''' compute the normalized aligned LCSS between two indicators using LCSS | 142 ''' compute the normalized aligned LCSS between two indicators using LCSS |
155 ie, the LCSS divided by the min or mean of the indicator lengths''' | 143 ie, the LCSS divided by the min or mean of the indicator lengths''' |
156 from utils import normalizedAlignedLCSS | 144 from utils import normalizedAlignedLCSS |
157 if indicator1 and indicator2: | 145 if indicator1 and indicator2: |
158 return normalizedAlignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | 146 return normalizedAlignedLCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta, method) |
159 else: | 147 else: |
160 return 0. | 148 return 0. |
161 | 149 |
162 def computeAlignedDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): | 150 def computeAlignedDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): |
163 ''' compute the aligned LCSS distance between two indicators using LCSS''' | 151 ''' compute the aligned LCSS distance between two indicators using LCSS''' |
164 from utils import alignedDLCSS | 152 from utils import alignedDLCSS |
165 return alignedDLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) | 153 if indicator1 and indicator2: |
154 return alignedDLCSS(indicator1.getValues(), indicator2.getValues(), lambda x,y: (distanceForLCSS(x,y) <= threshold), delta, method) | |
155 else: | |
156 return 1. | |
166 | 157 |
167 class SeverityIndicator(TemporalIndicator): | 158 class SeverityIndicator(TemporalIndicator): |
168 '''Class for severity indicators | 159 '''Class for severity indicators |
169 field mostSevereIsMax is True | 160 field mostSevereIsMax is True |
170 if the most severe value taken by the indicator is the maximum''' | 161 if the most severe value taken by the indicator is the maximum''' |