Mercurial > hg > nsaunier > traffic-intelligence
comparison python/events.py @ 430:fb3654a9127d
integrating indicator clustering code
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 12 Nov 2013 11:12:42 -0500 |
| parents | 365d8dee44f3 |
| children | b64ff7fe7b45 |
comparison
equal
deleted
inserted
replaced
| 429:2be846d36dec | 430:fb3654a9127d |
|---|---|
| 156 if not commonTimeInterval.empty(): | 156 if not commonTimeInterval.empty(): |
| 157 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) | 157 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) |
| 158 num += 1 | 158 num += 1 |
| 159 return interactions | 159 return interactions |
| 160 | 160 |
| 161 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): | |
| 162 '''Finds exemplar indicator time series for all interactions | |
| 163 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) | |
| 164 | |
| 165 if an indicator profile (time series) is different enough (<minSimilarity), | |
| 166 it will become a new prototype. Otherwise, it will be assigned to an existing prototypes''' | |
| 167 | |
| 168 # sort indicators based on length | |
| 169 indices = range(similarityMatrix.shape[0]) | |
| 170 def compare(i, j): | |
| 171 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)): | |
| 172 return -1 | |
| 173 elif len(interactions[i].getIndicator(indicatorName)) == len(interactions[j].getIndicator(indicatorName)): | |
| 174 return 0 | |
| 175 else: | |
| 176 return 1 | |
| 177 indices.sort(compare) | |
| 178 # go through all indicators | |
| 179 prototypeIndices = [indices[0]] | |
| 180 for i in indices[1:]: | |
| 181 if similarityMatrix[i][prototypeIndices].max() < minSimilarity: | |
| 182 prototypeIndices.append(i) | |
| 183 | |
| 184 # assignment | |
| 185 labels = [-1]*similarityMatrix.shape[0] | |
| 186 indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] | |
| 187 for i in prototypeIndices: | |
| 188 labels[i] = i | |
| 189 for i in indices[1:]: | |
| 190 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() | |
| 191 labels[i] = prototypeIndices[prototypeIndex] | |
| 192 | |
| 193 return prototypeIndices, labels | |
| 161 | 194 |
| 162 # TODO: | 195 # TODO: |
| 163 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class | 196 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class |
| 164 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 | 197 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 |
| 165 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): | 198 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): |
