Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 949:d6c1c05d11f5
modified multithreading at the interaction level for safety computations
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 21 Jul 2017 17:52:56 -0400 |
| parents | 584b9405e494 |
| children | 2a4f174879dd |
comparison
equal
deleted
inserted
replaced
| 948:584b9405e494 | 949:d6c1c05d11f5 |
|---|---|
| 54 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] | 54 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] |
| 55 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) | 55 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) |
| 56 else: | 56 else: |
| 57 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) | 57 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) |
| 58 | 58 |
| 59 if args.inputPrototypeDatabaseFilename is not None: | |
| 60 prototypeIndices, dbFilenames, trajectoryTypes, nMatchings, prototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) | |
| 61 | 59 |
| 62 trajectories = [o.getPositions().asArray().T for o in objects] | 60 trajectories = [o.getPositions().asArray().T for o in objects] |
| 61 if args.inputPrototypeDatabaseFilename is not None: | |
| 62 initialPrototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) | |
| 63 trajectories = [p.getMovingObject().getPositions().asArray().T for p in initialPrototypes]+trajectories | |
| 64 initialPrototypeIndices = range(len(initialPrototypes)) | |
| 65 else: | |
| 66 initialPrototypes = [] | |
| 67 initialPrototypeIndices = None | |
| 63 | 68 |
| 64 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 69 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
| 65 nTrajectories = len(trajectories) | 70 nTrajectories = len(trajectories) |
| 66 | 71 |
| 67 similarities = -np.ones((nTrajectories, nTrajectories)) | 72 similarities = -np.ones((nTrajectories, nTrajectories)) |
| 68 | 73 # the next line can be called again without reinitializing similarities |
| 69 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, None) # this line can be called again without reinitializing similarities | 74 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.optimizeCentroid, args.randomInitialization, args.inputPrototypeDatabaseFilename is not None, initialPrototypeIndices) # assignment is done only if working on the same database, otherwise the matchings will not compare and one has to to matchings on a large scale at once |
| 70 | 75 |
| 71 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 76 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
| 72 print(clusterSizes) | 77 print(clusterSizes) |
| 73 | 78 |
| 74 prototypes = [moving.Prototype(args.databaseFilename, objects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices] | 79 prototypes = [] |
| 80 for i in prototypeIndices: | |
| 81 if i<len(initialPrototypes): | |
| 82 initialPrototypes[i].nMatchings = 0 | |
| 83 prototypes.append(initialPrototypes[i]) | |
| 84 else: | |
| 85 if args.inputPrototypeDatabaseFilename is None: | |
| 86 nmatchings = clusterSizes[i] | |
| 87 else: | |
| 88 nmatchings = 0 | |
| 89 prototypes.append(moving.Prototype(args.databaseFilename, objects[i].getNum(), prototypeType, nmatchings) | |
| 75 if args.outputPrototypeDatabaseFilename is None: | 90 if args.outputPrototypeDatabaseFilename is None: |
| 76 outputPrototypeDatabaseFilename = args.databaseFilename | 91 outputPrototypeDatabaseFilename = args.databaseFilename |
| 77 else: | 92 else: |
| 78 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | 93 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
| 79 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) | 94 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) |
