# HG changeset patch # User Nicolas Saunier # Date 1384272762 18000 # Node ID fb3654a9127d89f1be68765ef186228617058751 # Parent 2be846d36dec28f2ab543a1435211b78753b7b37 integrating indicator clustering code diff -r 2be846d36dec -r fb3654a9127d python/events.py --- a/python/events.py Tue Nov 12 00:38:47 2013 -0500 +++ b/python/events.py Tue Nov 12 11:12:42 2013 -0500 @@ -158,6 +158,39 @@ num += 1 return interactions +def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): + '''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 + labels = [-1]*similarityMatrix.shape[0] + indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] + for i in prototypeIndices: + labels[i] = i + for i in indices[1:]: + prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() + labels[i] = prototypeIndices[prototypeIndex] + + return prototypeIndices, labels # TODO: #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class