changeset 430:fb3654a9127d

integrating indicator clustering code
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 12 Nov 2013 11:12:42 -0500
parents 2be846d36dec
children 0bcfa49d179a
files python/events.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 (<minSimilarity), 
+    it will become a new prototype. Otherwise, it will be assigned to an existing prototypes'''
+
+    # sort indicators based on length
+    indices = range(similarityMatrix.shape[0])
+    def compare(i, j):
+        if len(interactions[i].getIndicator(indicatorName)) > 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