# HG changeset patch # User Nicolas Saunier # Date 1439307485 14400 # Node ID b02431a8234cb9c8be584d6bddad72c7cd2b3bdd # Parent a850a4f9273578d433085c21ca1a480a3d968629 made prototypecluster generic, in ml module, and added randominitialization diff -r a850a4f92735 -r b02431a8234c python/events.py --- a/python/events.py Tue Aug 11 10:52:04 2015 -0400 +++ b/python/events.py Tue Aug 11 11:38:05 2015 -0400 @@ -2,7 +2,7 @@ '''Libraries for events Interactions, pedestrian crossing...''' -import moving, prediction, indicators, utils, cvutils +import moving, prediction, indicators, utils, cvutils, ml from base import VideoFilenameAddable import numpy as np @@ -295,60 +295,8 @@ print('unknown type of point: '+pointType) return allPoints -def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity, minClusterSize = None): - '''Finds exemplar indicator time series for all interactions - Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) - - if an indicator profile (time series) is different enough ( len(interactions[j].getIndicator(indicatorName)): - return -1 - elif len(interactions[i].getIndicator(indicatorName)) == len(interactions[j].getIndicator(indicatorName)): - return 0 - else: - return 1 - indices.sort(compare) - # go through all indicators - prototypeIndices = [indices[0]] - for i in indices[1:]: - if similarityMatrix[i][prototypeIndices].max() < minSimilarity: - prototypeIndices.append(i) - - # assignment - indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] - assign = True - while assign: - labels = [-1]*similarityMatrix.shape[0] - for i in prototypeIndices: - labels[i] = i - for i in indices: - prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() - labels[i] = prototypeIndices[prototypeIndex] - clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices} - smallestClusterIndex = min(clusterSizes, key = clusterSizes.get) - assign = (clusterSizes[smallestClusterIndex] < minClusterSize) - print prototypeIndices, smallestClusterIndex, clusterSizes[smallestClusterIndex] - if assign: - prototypeIndices.remove(smallestClusterIndex) - indices.append(smallestClusterIndex) - - return prototypeIndices, labels - -def prototypeMultivariateCluster(interactions, similarityMatrics, indicatorNames, minSimilarities, minClusterSize): - '''Finds exmaple indicator time series (several indicators) for all interactions - - if any interaction indicator time series is different enough ( len(instances[j]): + return -1 + elif len(instances[i]) == len(instances[j]): + return 0 + else: + return 1 + indices.sort(compare) + # go through all instances + prototypeIndices = [indices[0]] + for i in indices[1:]: + if similarityMatrix[i][prototypeIndices].max() < minSimilarity: + prototypeIndices.append(i) + + # assignment + indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] + assign = True + while assign: + labels = [-1]*similarityMatrix.shape[0] + for i in prototypeIndices: + labels[i] = i + for i in indices: + prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() + labels[i] = prototypeIndices[prototypeIndex] + clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices} + smallestClusterIndex = min(clusterSizes, key = clusterSizes.get) + assign = (clusterSizes[smallestClusterIndex] < minClusterSize) + if assign: + prototypeIndices.remove(smallestClusterIndex) + indices.append(smallestClusterIndex) + + return prototypeIndices, labels + +def motionPatternLearning(objects, maxDistance): ''' Option to use only the (n?) longest features per object instead of all for speed up TODO''' pass - -def prototypeCluster(): - ''' - TODO''' - pass