Mercurial > hg > nsaunier > traffic-intelligence
comparison python/events.py @ 746:e7ff0f60fef8
merged new developments (indicator and trajectory clustering)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 10 Sep 2015 15:52:45 -0400 |
| parents | 0e875a7f5759 |
| children | 8ba82b371eea |
comparison
equal
deleted
inserted
replaced
| 727:c6d4ea05a2d0 | 746:e7ff0f60fef8 |
|---|---|
| 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): | 298 def prototypeCluster(interactions, similarities, indicatorName, minSimilarity, similarityFunc = None, minClusterSize = None, randomInitialization = False): |
| 299 '''Finds exemplar indicator time series for all interactions | 299 return ml.prototypeCluster([inter.getIndicator(indicatorName) for inter in interactions], similarities, minSimilarity, similarityFunc, 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 | |
| 306 # sort indicators based on length | |
| 307 indices = range(similarityMatrix.shape[0]) | |
| 308 def compare(i, j): | |
| 309 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)): | |
| 310 return -1 | |
| 311 elif len(interactions[i].getIndicator(indicatorName)) == len(interactions[j].getIndicator(indicatorName)): | |
| 312 return 0 | |
| 313 else: | |
| 314 return 1 | |
| 315 indices.sort(compare) | |
| 316 # go through all indicators | |
| 317 prototypeIndices = [indices[0]] | |
| 318 for i in indices[1:]: | |
| 319 if similarityMatrix[i][prototypeIndices].max() < minSimilarity: | |
| 320 prototypeIndices.append(i) | |
| 321 | |
| 322 # assignment | |
| 323 labels = [-1]*similarityMatrix.shape[0] | |
| 324 indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] | |
| 325 for i in prototypeIndices: | |
| 326 labels[i] = i | |
| 327 for i in indices: | |
| 328 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() | |
| 329 labels[i] = prototypeIndices[prototypeIndex] | |
| 330 | |
| 331 return prototypeIndices, labels | |
| 332 | |
| 333 def prototypeMultivariateCluster(interactions, similarityMatrics, indicatorNames, minSimilarities, minClusterSize): | |
| 334 '''Finds exmaple indicator time series (several indicators) for all interactions | |
| 335 | |
| 336 if any interaction indicator time series is different enough (<minSimilarity), | |
| 337 it will become a new prototype. | |
| 338 Non-prototype interactions will be assigned to an existing prototype if all indicators are similar enough''' | |
| 339 pass | |
| 340 | |
| 341 | 300 |
| 342 class Crossing(moving.STObject): | 301 class Crossing(moving.STObject): |
| 343 '''Class for the event of a street crossing | 302 '''Class for the event of a street crossing |
| 344 | 303 |
| 345 TODO: detecter passage sur la chaussee | 304 TODO: detecter passage sur la chaussee |
