コード例 #1
0
ファイル: cluster_bup.py プロジェクト: Chuphay/Clustering
def make_clusters(adjacency, alpha_breaks = [0,1,2], seed = 0):
    graph = np.array(adjacency)
    degrees = np.sum(graph, axis = 1) 
    n = len(graph)
    all_nodes = np.eye(n)
    clusters = [Cluster(all_nodes[i],graph[i].astype(float),degrees) for i in range(n)]
    #clustered = [0 for i in range(n)]

    my_alpha_clusters = alpha_clusters(alpha_breaks)
    binary_cluster = [t_node([i]) for i in range(n)]

    where_we_are = [seed]
    for i in range(2*n+2):
        here = where_we_are[-1]
        #print("here",here,clusters[here].readable_nodes())
        if(clusters[here].parent):
            print("this is probably a bug")

        if(sum(clusters[here].nodes)==n):
            print("Finished after", i, "iterations")
            break
        if(i == 2*n):
            print("bad break....")
            break


        next_node, not_using, not_using2, alpha = clusters[here].get_closest()
        #print("next_node", next_node, "alpha", alpha)


        while(clusters[next_node].parent):
            next_node = clusters[next_node].parentId

        next_node_best, not_using, not_using2, not_using3 = clusters[next_node].get_closest()

        if(clusters[here].nodes[next_node_best] == 1):
            #print("match!", next_node)
            where_we_are = [here]
            clusters[here].add_nodes(clusters[next_node].nodes,clusters[next_node].weights)
            clusters[next_node].parent = clusters[here]
            clusters[next_node].parentId = here
            #print(here, clusters[here].readable_nodes())
            binary_cluster[here].union(binary_cluster[next_node])
            #my_alpha_clusters.add_cluster(here,clusters[here].nodes.astype(float),alpha)


        else:
            if(next_node in where_we_are):
                #print("found a loop:", where_we_are, next_node)
                for i in range(len(where_we_are)-1):

                    spot = where_we_are[i]
                    while(clusters[spot].parent):                   
                        spot = clusters[spot].parentId

                    clusters[here].add_nodes(clusters[spot].nodes,clusters[spot].weights)
                    clusters[spot].parent = clusters[here]
                    clusters[spot].parentId = here
                    binary_cluster[here].union(binary_cluster[spot])
                where_we_are = [here]
            else:
                where_we_are.append(next_node)  

    #return my_alpha_clusters
    #print(clusters[here].nodes)
    return binary_cluster[0].get_parent()
コード例 #2
0
def make_clusters(adjacency, alpha_breaks=[0, 1, 2], seed=0):
    graph = np.array(adjacency)
    degrees = np.sum(graph, axis=1)
    n = len(graph)
    all_nodes = np.eye(n)
    clusters = [
        Cluster(all_nodes[i], graph[i].astype(float), degrees)
        for i in range(n)
    ]
    #clustered = [0 for i in range(n)]

    my_alpha_clusters = alpha_clusters(alpha_breaks)
    binary_cluster = [t_node([i]) for i in range(n)]

    where_we_are = [seed]
    for i in range(2 * n + 2):
        here = where_we_are[-1]
        #print("here",here,clusters[here].readable_nodes())
        if (clusters[here].parent):
            print("this is probably a bug")

        if (sum(clusters[here].nodes) == n):
            print("Finished after", i, "iterations")
            break
        if (i == 2 * n):
            print("bad break....")
            break

        next_node, not_using, not_using2, alpha = clusters[here].get_closest()
        #print("next_node", next_node, "alpha", alpha)

        while (clusters[next_node].parent):
            next_node = clusters[next_node].parentId

        next_node_best, not_using, not_using2, not_using3 = clusters[
            next_node].get_closest()

        if (clusters[here].nodes[next_node_best] == 1):
            #print("match!", next_node)
            where_we_are = [here]
            clusters[here].add_nodes(clusters[next_node].nodes,
                                     clusters[next_node].weights)
            clusters[next_node].parent = clusters[here]
            clusters[next_node].parentId = here
            #print(here, clusters[here].readable_nodes())
            binary_cluster[here].union(binary_cluster[next_node])
            #my_alpha_clusters.add_cluster(here,clusters[here].nodes.astype(float),alpha)

        else:
            if (next_node in where_we_are):
                #print("found a loop:", where_we_are, next_node)
                for i in range(len(where_we_are) - 1):

                    spot = where_we_are[i]
                    while (clusters[spot].parent):
                        spot = clusters[spot].parentId

                    clusters[here].add_nodes(clusters[spot].nodes,
                                             clusters[spot].weights)
                    clusters[spot].parent = clusters[here]
                    clusters[spot].parentId = here
                    binary_cluster[here].union(binary_cluster[spot])
                where_we_are = [here]
            else:
                where_we_are.append(next_node)

    #return my_alpha_clusters
    #print(clusters[here].nodes)
    return binary_cluster[0].get_parent()
コード例 #3
0
ファイル: cluster2.py プロジェクト: Chuphay/Clustering
def make_clusters(adjacency,  seed = 0):
    graph = np.array(adjacency)
    degrees = np.sum(graph, axis = 1) 
    n = len(graph)
    all_nodes = np.eye(n)
    clusters = [Cluster(i, all_nodes[i],graph[i].astype(float),degrees) for i in range(n)]
    binary_cluster = [t_node([i]) for i in range(n)]

    where_we_are = [seed]
    where_we_are_alpha = []
    for i in range(3*n+2):
        here = where_we_are[-1]
        #print("here",here,clusters[here].readable_nodes())
        if(clusters[here].parent):
            print("this is probably a bug")

        if(sum(clusters[here].nodes)==n):
            print("Finished after", i, "iterations")
            break
        if(i == 3*n):
            print("bad break....")
            break


        next_node, alpha = clusters[here].get_closest()
        #print("here", here, "next_node", next_node, "alpha", alpha)
        #print(clusters[next_node].get_nodes())
        #print(clusters[here].get_change(clusters[next_node].get_nodes()))


        while(clusters[next_node].parent):
            next_node = clusters[next_node].parentId

        next_node_best, next_node_alpha = clusters[next_node].get_closest()
        #print("alphas", alpha, next_node_alpha)
        if(clusters[here].nodes[next_node_best] == 1):
            #print("match!", next_node)
            #print(clusters[next_node].get_change(clusters[here].get_nodes()))
            where_we_are = [here]
            where_we_are_alpha = []
            clusters[here].add_nodes(clusters[next_node].nodes,clusters[next_node].weights)
            clusters[next_node].parent = clusters[here]
            clusters[next_node].parentId = here
            binary_cluster[here].union(binary_cluster[next_node], alpha, next_node_alpha)


        elif(alpha < next_node_alpha):
            print("Takeover!!!", alpha, next_node_alpha, here, next_node)
            #print(clusters[next_node].get_change(clusters[here].get_nodes()))
            where_we_are = [here]
            where_we_are_alpha = []
            clusters[here].add_nodes(clusters[next_node].nodes,clusters[next_node].weights)
            clusters[next_node].parent = clusters[here]
            clusters[next_node].parentId = here
            binary_cluster[here].union(binary_cluster[next_node], alpha, next_node_alpha)

        elif(next_node in where_we_are):

            
            where_we_are_alpha.append(alpha) #sort of messy, but we got to do it
            print("found a loop:", where_we_are, next_node, where_we_are_alpha,"here", here)
            
            backwards_alpha = []
            max_alpha =[] #actually we are doing the average
            for i in range(len(where_we_are)):
                first = where_we_are[i]
                second = where_we_are[(i+1)%len(where_we_are)]
                t_t_alpha, next_ve =  clusters[second].get_alpha(clusters[first].nodes)
                backwards_alpha.append(t_t_alpha)
                if(backwards_alpha[i] > where_we_are_alpha[i]):
                    max_alpha.append(t_t_alpha+where_we_are_alpha[i]) #average
                else:
                    max_alpha.append(where_we_are_alpha[i]+t_t_alpha) #average
            
            minimum_arg = np.argmin(np.array(max_alpha))
            #print(where_we_are[minimum_arg], where_we_are[(minimum_arg+1)%len(where_we_are)])
            here = where_we_are[minimum_arg]
            spot = where_we_are[(minimum_arg +1)%len(where_we_are)]
            if(clusters[here].parent or clusters[spot].parent):
                raise SyntaxError
            """
            size_of_cluster = []
            for x in where_we_are:
                size_of_cluster.append(sum(clusters[x].nodes))
            minimum_arg = np.argmin(np.array(size_of_cluster))
            here = where_we_are[minimum_arg]
            spot = where_we_are[(minimum_arg +1)%len(where_we_are)]
            """
        
            clusters[here].add_nodes(clusters[spot].nodes,clusters[spot].weights)
            clusters[spot].parent = clusters[here]
            clusters[spot].parentId = here
            binary_cluster[here].union(binary_cluster[spot],\
                                       where_we_are_alpha[minimum_arg],\
                                       backwards_alpha[minimum_arg]) 
            where_we_are = [here]
            where_we_are_alpha = []
        else:
            #print("moving on")
            #print(clusters[next_node].get_change(clusters[here].get_nodes()))
            where_we_are.append(next_node)
            where_we_are_alpha.append(alpha)

    return binary_cluster[0].get_parent()
コード例 #4
0
ファイル: cluster.py プロジェクト: Chuphay/Clustering
def make_clusters(adjacency, seed=0):
    graph = np.array(adjacency)
    degrees = np.sum(graph, axis=1)
    n = len(graph)
    all_nodes = np.eye(n)
    clusters = [
        Cluster(i, all_nodes[i], graph[i].astype(float), degrees)
        for i in range(n)
    ]
    binary_cluster = [t_node([i]) for i in range(n)]

    where_we_are = [seed]
    where_we_are_alpha = []
    for i in range(3 * n + 2):
        here = where_we_are[-1]
        #print("here",here,clusters[here].readable_nodes())
        if (clusters[here].parent):
            print("this is probably a bug")

        if (sum(clusters[here].nodes) == n):
            print("Finished after", i, "iterations")
            break
        if (i == 3 * n):
            print("bad break....")
            break

        next_node, alpha = clusters[here].get_closest()
        #print("here", here, "next_node", next_node, "alpha", alpha)

        while (clusters[next_node].parent):
            next_node = clusters[next_node].parentId

        next_node_best, next_node_alpha = clusters[next_node].get_closest()

        if (clusters[here].nodes[next_node_best] == 1):
            #print("match!", next_node)
            where_we_are = [here]
            where_we_are_alpha = []
            clusters[here].add_nodes(clusters[next_node].nodes,
                                     clusters[next_node].weights)
            clusters[next_node].parent = clusters[here]
            clusters[next_node].parentId = here
            binary_cluster[here].union(binary_cluster[next_node], alpha,
                                       next_node_alpha)

        elif (next_node in where_we_are):

            where_we_are_alpha.append(
                alpha)  #sort of messy, but we got to do it
            #print("found a loop:", where_we_are, next_node, where_we_are_alpha,"here", here)

            backwards_alpha = []
            max_alpha = []  #actually we are doing the average
            for i in range(len(where_we_are)):
                first = where_we_are[i]
                second = where_we_are[(i + 1) % len(where_we_are)]
                t_t_alpha, next_ve = clusters[second].get_alpha(
                    clusters[first].nodes)
                backwards_alpha.append(t_t_alpha)
                if (backwards_alpha[i] > where_we_are_alpha[i]):
                    max_alpha.append(t_t_alpha +
                                     where_we_are_alpha[i])  #average
                else:
                    max_alpha.append(where_we_are_alpha[i] +
                                     t_t_alpha)  #average

            minimum_arg = np.argmin(np.array(max_alpha))
            #print(where_we_are[minimum_arg], where_we_are[(minimum_arg+1)%len(where_we_are)])
            here = where_we_are[minimum_arg]
            spot = where_we_are[(minimum_arg + 1) % len(where_we_are)]
            if (clusters[here].parent or clusters[spot].parent):
                raise SyntaxError
            """
            size_of_cluster = []
            for x in where_we_are:
                size_of_cluster.append(sum(clusters[x].nodes))
            minimum_arg = np.argmin(np.array(size_of_cluster))
            here = where_we_are[minimum_arg]
            spot = where_we_are[(minimum_arg +1)%len(where_we_are)]
            """

            clusters[here].add_nodes(clusters[spot].nodes,
                                     clusters[spot].weights)
            clusters[spot].parent = clusters[here]
            clusters[spot].parentId = here
            binary_cluster[here].union(binary_cluster[spot],\
                                       where_we_are_alpha[minimum_arg],\
                                       backwards_alpha[minimum_arg])
            where_we_are = [here]
            where_we_are_alpha = []
        else:
            #print("moving on")
            where_we_are.append(next_node)
            where_we_are_alpha.append(alpha)

    return binary_cluster[0].get_parent()