comparison python/indicators.py @ 321:a5e40bd04cf4

rearranged LCSS indicator functions
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 07 May 2013 02:02:55 +0200
parents 43e62b9cb652
children 28661c5887d3
comparison
equal deleted inserted replaced
320:419f30491a4b 321:a5e40bd04cf4
94 if self.maxValue: 94 if self.maxValue:
95 ylim(ymax = self.maxValue) 95 ylim(ymax = self.maxValue)
96 96
97 def valueSorted(self): 97 def valueSorted(self):
98 ''' return the values after sort the keys in the indicator 98 ''' return the values after sort the keys in the indicator
99 This should probably not be used: to delete''' 99 This should probably not be used: to delete'''
100 print('Deprecated: values should not be accessed in this way')
100 values=[] 101 values=[]
101 keys = self.values.keys() 102 keys = self.values.keys()
102 keys.sort() 103 keys.sort()
103 for key in keys: 104 for key in keys:
104 values.append(self.values[key]) 105 values.append(self.values[key])
106 107
107 @staticmethod 108 @staticmethod
108 def getDLCSS(indic1, indic2, threshold, delta = float('inf') , method ='min' ): 109 def getDLCSS(indic1, indic2, threshold, delta = float('inf') , method ='min' ):
109 ''' compute the distance between two indicators using LCSS 110 ''' compute the distance between two indicators using LCSS
110 two common methods are used: min or mean of the indicators length''' 111 two common methods are used: min or mean of the indicators length'''
112 print('Deprecated: this is not appropriate method for indicator comparison')
111 l1 = indic1.valueSorted 113 l1 = indic1.valueSorted
112 l2 = indic2.valueSorted 114 l2 = indic2.valueSorted
113 DLCSS = None 115 DLCSS = None
114 if method == 'min': 116 if method == 'min':
115 DLCSS = 1- (LCSS(l1,l2, threshold, delta, distance))/min(len(l1),len(l2)) 117 DLCSS = 1- (LCSS(l1,l2, threshold, delta, distance))/min(len(l1),len(l2))
117 average = len(l1)+len(l2)/2 119 average = len(l1)+len(l2)/2
118 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average) 120 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average)
119 return DLCSS 121 return DLCSS
120 122
121 123
122 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'): 124 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')):
123 ''' compute the distance between two indicators using LCSS 125 ''' compute the LCSS between two indicators using LCSS'''
124 two common methods are used: min or mean of the indicators length'''
125 from utils import LCSS 126 from utils import LCSS
126 127
127 def distance(x, y): # lambda x,y:abs(x-y) 128 def distance(x, y): # lambda x,y:abs(x-y)
128 if x == None or y == None: 129 if x == None or y == None:
129 return float('inf') 130 return float('inf')
130 else: 131 else:
131 return abs(x-y) 132 return abs(x-y)
132 133 if indicator1 and indicator2:
133 lcss = LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta) 134 return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distance, delta)
134 if method == 'min':
135 denominator = min(len(indicator1), len(indicator2))
136 elif method == 'mean':
137 denominator = float(len(indicator1) + len(indicator2))/2
138 else: 135 else:
139 print('Unknown denominator method name') 136 return 0
140 denominator = 1. 137
141 return 1-float(lcss)/denominator 138 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= 'min'):
139 ''' compute the normalized LCSS between two indicators using LCSS
140 ie, the LCSS divided by the min or mean of the indicator lengths'''
141
142 if indicator1 and indicator2:
143 if method == 'min':
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:
152 return 0.
153
154 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = 'min'):
155 ''' compute the LCSS distance between two indicators using LCSS'''
156 return 1-computeNormalizedLCSS(indicator1, indicator2, threshold, delta, method)
142 157
143 class SeverityIndicator(TemporalIndicator): 158 class SeverityIndicator(TemporalIndicator):
144 '''Class for severity indicators 159 '''Class for severity indicators
145 field mostSevereIsMax is True 160 field mostSevereIsMax is True
146 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'''