Mercurial > hg > nsaunier > traffic-intelligence
comparison python/events.py @ 731:b02431a8234c dev
made prototypecluster generic, in ml module, and added randominitialization
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 11 Aug 2015 11:38:05 -0400 |
| parents | 4e89341edd29 |
| children | 0e875a7f5759 |
comparison
equal
deleted
inserted
replaced
| 730:a850a4f92735 | 731:b02431a8234c |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Libraries for events | 2 '''Libraries for events |
| 3 Interactions, pedestrian crossing...''' | 3 Interactions, pedestrian crossing...''' |
| 4 | 4 |
| 5 import moving, prediction, indicators, utils, cvutils | 5 import moving, prediction, indicators, utils, cvutils, ml |
| 6 from base import VideoFilenameAddable | 6 from base import VideoFilenameAddable |
| 7 | 7 |
| 8 import numpy as np | 8 import numpy as np |
| 9 | 9 |
| 10 import multiprocessing | 10 import multiprocessing |
| 293 allPoints += points | 293 allPoints += points |
| 294 else: | 294 else: |
| 295 print('unknown type of point: '+pointType) | 295 print('unknown type of point: '+pointType) |
| 296 return allPoints | 296 return allPoints |
| 297 | 297 |
| 298 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity, minClusterSize = None): | 298 def prototypeCluster(interactions, similarityMatrix, indicatorName, minSimilarity, minClusterSize = None, randomInitialization = False): |
| 299 '''Finds exemplar indicator time series for all interactions | 299 return ml.prototypeCluster([inter.getIndicator(indicatorName) for inter in interactions], similarityMatrix, minSimilarity, minClusterSize, randomInitialization) |
| 300 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) | |
| 301 | |
| 302 if an indicator profile (time series) is different enough (<minSimilarity), | |
| 303 it will become a new prototype. | |
| 304 Non-prototype interactions will be assigned to an existing prototype | |
| 305 if minClusterSize is not None, the clusters will be refined by removing iteratively the smallest clusters | |
| 306 and reassigning all elements in the cluster until no cluster is smaller than minClusterSize''' | |
| 307 | |
| 308 # sort indicators based on length | |
| 309 indices = range(similarityMatrix.shape[0]) | |
| 310 def compare(i, j): | |
| 311 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)): | |
| 312 return -1 | |
| 313 elif len(interactions[i].getIndicator(indicatorName)) == len(interactions[j].getIndicator(indicatorName)): | |
| 314 return 0 | |
| 315 else: | |
| 316 return 1 | |
| 317 indices.sort(compare) | |
| 318 # go through all indicators | |
| 319 prototypeIndices = [indices[0]] | |
| 320 for i in indices[1:]: | |
| 321 if similarityMatrix[i][prototypeIndices].max() < minSimilarity: | |
| 322 prototypeIndices.append(i) | |
| 323 | |
| 324 # assignment | |
| 325 indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] | |
| 326 assign = True | |
| 327 while assign: | |
| 328 labels = [-1]*similarityMatrix.shape[0] | |
| 329 for i in prototypeIndices: | |
| 330 labels[i] = i | |
| 331 for i in indices: | |
| 332 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() | |
| 333 labels[i] = prototypeIndices[prototypeIndex] | |
| 334 clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices} | |
| 335 smallestClusterIndex = min(clusterSizes, key = clusterSizes.get) | |
| 336 assign = (clusterSizes[smallestClusterIndex] < minClusterSize) | |
| 337 print prototypeIndices, smallestClusterIndex, clusterSizes[smallestClusterIndex] | |
| 338 if assign: | |
| 339 prototypeIndices.remove(smallestClusterIndex) | |
| 340 indices.append(smallestClusterIndex) | |
| 341 | |
| 342 return prototypeIndices, labels | |
| 343 | |
| 344 def prototypeMultivariateCluster(interactions, similarityMatrics, indicatorNames, minSimilarities, minClusterSize): | |
| 345 '''Finds exmaple indicator time series (several indicators) for all interactions | |
| 346 | |
| 347 if any interaction indicator time series is different enough (<minSimilarity), | |
| 348 it will become a new prototype. | |
| 349 Non-prototype interactions will be assigned to an existing prototype if all indicators are similar enough''' | |
| 350 pass | |
| 351 | |
| 352 | 300 |
| 353 class Crossing(moving.STObject): | 301 class Crossing(moving.STObject): |
| 354 '''Class for the event of a street crossing | 302 '''Class for the event of a street crossing |
| 355 | 303 |
| 356 TODO: detecter passage sur la chaussee | 304 TODO: detecter passage sur la chaussee |
