Exemplo n.º 1
0
def hc(G, modelo):
    
    #G=nx.read_edgelist("test.net")
    # Extract largest connected component into graph H
    components = [comp for comp in nx.connected_component_subgraphs(G)]
    component_size = [len(comp)for comp in components]

    H=components[0]
    # Makes life easier to have consecutively labeled integer nodes
    H=nx.convert_node_labels_to_integers(H)
    # Create parititions with hierarchical clustering
    partitions=create_hc(H)

    partition = {}
    #Convertimos. partitions a partition como diccionario.
    cont = 0
    for i in partitions:
        for j in i:
            partition[j + 1] = cont # se le suma uno para no tener nodo cero sino desde nodo 1.
        cont += 1


    # Build blockmodel graph
    BM=nx.blockmodel(H,partitions)
    hc = 1
    comunidad(BM, partition, modelo, hc)
    hc = 0
Exemplo n.º 2
0
    def full_hc(self, G, hc = None, clusters = [], clear = True, method='complete', metric ='euclidean', clip = False, t=1.0):
        """ 
        All this function needs is G and it will do a full hierarchical clustering of a graph
        You could also seed it with an initial clustering of your own choosing by setting hc
        Note: hc is a list of node labels
        """
        #clears cached clusters so that clusters are not appended endlessly
        if clear:
            self.clear()
            clear = False

        if hc and len(hc) == 1:
            self.clusters.append(hc)
            clusters.append(hc)
            return clusters
        else:
            if not hc:
                hc = self.create_hc(G, method=method, metric=metric, t=t)
            
            bm = nx.blockmodel(G, hc)
            #append to local variable
            clusters.append(hc)
            #append to properties
            self.clusters.append(hc)
            self.blockmodels.append(bm)
            #grab next hc
            next_hc = self.next_labeled_hc(hc, bm, method = method, metric = metric, clip = clip, t=t)
            return self.full_hc(G, hc=next_hc, clusters=clusters, clear=clear, method=method, metric=metric, clip=clip, t=t)
Exemplo n.º 3
0
def pajek_group(G, Grupo, comp):
    #print("comienza reduccion de grafo")

    #elimino nodos con grado cero.

    #nuevo experimento, miraremos si es igual al arrojado por nuestro algoritmo.
    Grp = [Grupo[i] for i in Grupo] #guarda los grupos como arreglo para poder que la funcion blockmodel separe los que sobra.

    B = nx.blockmodel(G,Grp)

    #Re nombro los nodos para eliminar el nodo cero y que todo corresponda a los grupos encontrados en la funcion anterior.
    mapping = {} #variable que guardara el nuevo valor de nodos.
    cont = 0
    for i in B.nodes(): #creo diccionario que guarda el nuevo valor de nodos.
        mapping[cont] = cont + 1
        cont += 1

    A=nx.relabel_nodes(B,mapping) #asigno grafo con el nuevo valor de nodos.

   
    NodosBorrar = []
    cont_group = 1
    cont = 1
    for i in A.nodes():
        if A.degree(cont_group) != 0: 
           cont += 1
        if A.degree(cont_group) == 0: 
           NodosBorrar.append(cont_group)
        cont_group += 1
    

    for i in NodosBorrar: A.remove_node(i)
    Nodes = A.number_of_nodes()
 
    candidatos(G,A,comp, Grupo)
Exemplo n.º 4
0
    def blockmodel(self, save=True):
        goog_url = SEMANTIC_NETWORK_GOOG_URLS[self.model.name]
        cluster_ld = pytxt.tsv2ld(goog_url)
        cluster_id2d = pytxt.ld2dd(cluster_ld, 'ID')
        node_ld = pytxt.tsv2ld(
            self.fn.replace('.graphml', '.analysis-with-modularity.txt'))
        id2ld = pytxt.ld2dld(node_ld, 'partition_id')
        part_list = []
        for p_id, pld in sorted(id2ld.items()):
            nodes = [d['word'] for d in pld]
            part_list += [nodes]

        M = nx.blockmodel(self.g, part_list)
        M2 = nx.Graph()
        node2name = {}
        for node in M.nodes():
            cluster = cluster_id2d[str(node)]
            name = cluster['NAME']
            node2name[node] = name
            M2.add_node(name, **cluster)

        for a, b in M.edges():
            M2.add_edge(node2name[a], node2name[b])

        if save:
            nx.write_graphml(
                M2, self.fn.replace('.graphml', '.blockmodel.graphml'))
        return M2
Exemplo n.º 5
0
def inter_community_edges(G, partition):
    """Returns the number of inter-community edges according to the given
    partition of the nodes of ``G``.

    ``G`` must be a NetworkX graph.

    ``partition`` must be a partition of the nodes of ``G``.

    The *inter-community edges* are those edges joining a pair of nodes
    in different blocks of the partition.

    Implementation note: this function creates an intermediate graph
    that may require the same amount of memory as required to store
    ``G``.

    """
    # Alternate implementation that does not require constructing a new
    # graph object (but does require constructing an affiliation
    # dictionary):
    #
    #     aff = dict(chain.from_iterable(((v, block) for v in block)
    #                                    for block in partition))
    #     return sum(1 for u, v in G.edges() if aff[u] != aff[v])
    #
    return nx.blockmodel(G, partition, multigraph=True).size()
Exemplo n.º 6
0
    def propagate_constraints(self):
        """Propagate p=0 and p=1 probabilities edges
        """

        # propagated p=1 constraints
        p1 = []
        c = nx.Graph(self)
        c.remove_edges_from([(e, f) for e, f, d in self.edges_iter(data=True)
                             if d[PROBABILITY] != 1])
        components = nx.connected_components(c)
        for component in components:
            for i, n in enumerate(component):
                for m in component[i+1:]:
                    p1.append((n, m))

        # propagated p=0 constraints
        p0 = []
        c = nx.Graph(self)
        c.remove_edges_from([(e, f) for e, f, d in self.edges_iter(data=True)
                             if d[PROBABILITY] != 0])
        c = nx.blockmodel(c, components, multigraph=True)
        for e, f in c.edges_iter():
            for n in components[e]:
                for m in components[f]:
                    p0.append((n, m))

        for n, m in p1:
            self.add_edge(n, m, **{PROBABILITY: 1.})
        for n, m in p0:
            self.add_edge(n, m, **{PROBABILITY: 0.})

        return self
Exemplo n.º 7
0
def hiclus_blockmodel(G):
    # Extract largest connected component into graph H
    H=nx.connected_component_subgraphs(G)[0]
    # Create parititions with hierarchical clustering
    partitions=hc.create_hc(H)
    # Build blockmodel graph
    BM=nx.blockmodel(H,partitions)


    # Draw original graph
    pos=nx.spring_layout(H,iterations=100)
    fig=plt.figure(1,figsize=(6,10))
    ax=fig.add_subplot(211)
    nx.draw(H,pos,with_labels=False,node_size=10)
    plt.xlim(0,1)
    plt.ylim(0,1)

    # Draw block model with weighted edges and nodes sized by number of internal nodes
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[(2*d['weight']) for (u,v,d) in BM.edges(data=True)]
    # Set positions to mean of positions of internal nodes from original graph
    posBM={}
    for n in BM:
        xy=numpy.array([pos[u] for u in BM.node[n]['graph']])
        posBM[n]=xy.mean(axis=0)
    ax=fig.add_subplot(212)
    nx.draw(BM,posBM,node_size=node_size,width=edge_width,with_labels=False)
    plt.xlim(0,1)
    plt.ylim(0,1)
    plt.axis('off')
Exemplo n.º 8
0
def hiclus_blockmodel(G):
    # Extract largest connected component into graph H
    H = nx.connected_component_subgraphs(G)[0]
    # Create parititions with hierarchical clustering
    partitions = hc.create_hc(H)
    # Build blockmodel graph
    BM = nx.blockmodel(H, partitions)

    # Draw original graph
    pos = nx.spring_layout(H, iterations=100)
    fig = plt.figure(1, figsize=(6, 10))
    ax = fig.add_subplot(211)
    nx.draw(H, pos, with_labels=False, node_size=10)
    plt.xlim(0, 1)
    plt.ylim(0, 1)

    # Draw block model with weighted edges and nodes sized by number of internal nodes
    node_size = [BM.node[x]['nnodes'] * 10 for x in BM.nodes()]
    edge_width = [(2 * d['weight']) for (u, v, d) in BM.edges(data=True)]
    # Set positions to mean of positions of internal nodes from original graph
    posBM = {}
    for n in BM:
        xy = numpy.array([pos[u] for u in BM.node[n]['graph']])
        posBM[n] = xy.mean(axis=0)
    ax = fig.add_subplot(212)
    nx.draw(BM,
            posBM,
            node_size=node_size,
            width=edge_width,
            with_labels=False)
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.axis('off')
Exemplo n.º 9
0
 def plot_additional(self, home_nodes, levels=0):
     """Plot new nodes"""
     
     new_nodes = self._neighbors(home_nodes, levels=levels)
     new_nodes = home_nodes.union(new_nodes)
     
     displayed_data_nodes = set([v['G_id'] for k,v in self.dispG.node.items()])
     
     current_num_islands = nx.number_connected_components(self.dispG)
     new_num_islands = nx.number_connected_components(self.G.subgraph(displayed_data_nodes.union(new_nodes)))
     if new_num_islands > current_num_islands:
         all_nodes = set(self.G.nodes())
         singleton_nodes = all_nodes - displayed_data_nodes - new_nodes
         singleton_nodes = map(lambda x: [x], singleton_nodes)
         partitions = [displayed_data_nodes, new_nodes] + list(singleton_nodes)
         B = nx.blockmodel(self.G, partitions, multigraph=True)
         
         try:
             path = nx.shortest_path(B, 0, 1)
         except nx.NetworkXNoPath:
             pass
         else:
             ans = tkm.askyesno("Plot path?", "A path exists between the current graph and"
                     "the nodes you have added. Would you like to plot that path?")
             if ans:
                 for u in path[1:-1]:
                     Gu = B.node[u]['graph'].nodes()
                     assert len(Gu) ==1; Gu=Gu[0]
                     new_nodes.add(Gu)
     self._plot_additional(new_nodes)
Exemplo n.º 10
0
    def _neighbors(self, node, levels=1, graph=None):
        """Return graph of neighbors around node in graph (default: self.dataG)
        to a certain number of levels"""

        if graph is None:
            graph = self.dataG

        if not isinstance(node, (list, tuple, set)):
            node = [
                node,
            ]

        neighbors = set(node)
        blocks = [[
            n,
        ] for n in node]
        for i in range(levels):
            for n in neighbors:
                new_neighbors = set(graph.neighbors(n)) - neighbors
                blocks.append(new_neighbors)
                neighbors = neighbors.union(new_neighbors)
        G = graph.subgraph(neighbors)

        if len(blocks) > 1:
            # Create a block repersentation of our graph and make sure we're plotting
            #  anything that connects the blocks too

            # Create blocks for each individual node not already in a block
            non_blocked = set(self.dataG.nodes()) - neighbors
            non_blocked = [[
                a,
            ] for a in non_blocked]

            partitions = blocks + non_blocked

            B = nx.blockmodel(graph, partitions)

            # The resulting graph will has nodes numbered according their index in partitions
            # We want to go through the partitions which are blocks and find the shortest path

            num_blocks = len(blocks)
            for frm_node, to_node in zip(range(num_blocks),
                                         range(1, num_blocks - 1)):
                try:
                    path = nx.shortest_path(B, frm_node, to_node)
                except nx.NetworkXNoPath as e:
                    pass  # In an island, which is permissible
                except nx.NetworkXError as e:
                    tkm.showerror("Node not in graph", str(e))
                    return
                else:
                    # Break path in B back down into path in G
                    path2 = []
                    for a in path[1:-1]:  # don't include end points
                        for n in partitions[a]:
                            neighbors.add(n)
            G = graph.subgraph(neighbors)

        return G
Exemplo n.º 11
0
def find_out_edges(list_of_edges, graph):

    NG = nx.blockmodel(graph, list_of_edges)
    eg = NG.edges_iter(data=True)
    count = 0
    for e in eg:
        count += e[2]['weight']
    return count
Exemplo n.º 12
0
def GenerateBlockSequenceRepresentatives(G) :
    """Synopsis:

    For the graph G, this function does the following:

    1. Generates all indecomposable partitions of V(G), denoted by P.

    2. For each partition p of P, it generates the graph G \ p and its
    update sequence representatives translated back to the blocks of p.

    3. It returns a double list where each element is the list of
    representatives for G \ p as p varies over P. This list can be
    flatten as in the example contained at the end of this file.

    The function assumes that the vertices are labeled 0, 1, ...,
    n-1. The collection returned is canonical in the sense that each
    representative has a maximal number of blocks. Some blocks may be
    combined, but this will generally not lead to a canonical set of
    representatives.

    """

    partitionCollection = GenerateIrreducibleBlockSequences(G)

    globalCollection = []

    for partition in partitionCollection :

        # print "Partition: ", partition

        # Generate G \ partition with vertex set range(0, len(partition) )

        gMod = nx.blockmodel(G, partition)
        # print "Gmod - vertices: ", gMod.number_of_nodes()

        # Generate update sequence transversal

        linExt = equivalence.LinearExtensions( gMod )

        # print "Linear extensions: ", len(linExt)
        # for e in linExt :
        #     print "\t", e

        # Generate corresponding block sequences for this partition
        
        blockSequenceCollection = []

        for gModSeq in linExt :
            blockSequence = []
            for index in gModSeq :
                blockSequence.append( partition[index] )

            blockSequenceCollection.append( blockSequence )

        globalCollection.append( blockSequenceCollection )


    return globalCollection
Exemplo n.º 13
0
 def test_barbell(self):
     G = networkx.barbell_graph(3, 0)
     partition = [[0, 1, 2], [3, 4, 5]]
     M = networkx.blockmodel(G, partition)
     assert_equal(sorted(M.nodes()), [0, 1])
     assert_equal(sorted(M.edges()), [(0, 1)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 3)
         assert_equal(M.node[n]['nnodes'], 3)
         assert_equal(M.node[n]['density'], 1.0)
Exemplo n.º 14
0
 def test_path(self):
     G=networkx.path_graph(6)
     partition=[[0,1],[2,3],[4,5]]
     M=networkx.blockmodel(G,partition)
     assert_equal(sorted(M.nodes()),[0,1,2])
     assert_equal(sorted(M.edges()),[(0,1),(1,2)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'],1)
         assert_equal(M.node[n]['nnodes'],2)
         assert_equal(M.node[n]['density'],1.0)
Exemplo n.º 15
0
 def test_path(self):
     G = networkx.path_graph(6)
     partition = [[0, 1], [2, 3], [4, 5]]
     M = networkx.blockmodel(G, partition)
     assert_equal(sorted(M.nodes()), [0, 1, 2])
     assert_equal(sorted(M.edges()), [(0, 1), (1, 2)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 1)
         assert_equal(M.node[n]['nnodes'], 2)
         assert_equal(M.node[n]['density'], 1.0)
Exemplo n.º 16
0
 def test_barbell(self):
     G=networkx.barbell_graph(3,0)
     partition=[[0,1,2],[3,4,5]]
     M=networkx.blockmodel(G,partition)
     assert_equal(sorted(M.nodes()),[0,1])
     assert_equal(sorted(M.edges()),[(0,1)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'],3)
         assert_equal(M.node[n]['nnodes'],3)
         assert_equal(M.node[n]['density'],1.0)
Exemplo n.º 17
0
 def test_multigraph_blockmodel(self):
     G = nx.MultiGraph(nx.path_graph(6))
     partition = [[0, 1], [2, 3], [4, 5]]
     M = nx.blockmodel(G, partition, multigraph=True)
     assert_equal(sorted(M.nodes()), [0, 1, 2])
     assert_equal(sorted(M.edges()), [(0, 1), (1, 2)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 1)
         assert_equal(M.node[n]['nnodes'], 2)
         assert_equal(M.node[n]['density'], 1.0)
Exemplo n.º 18
0
 def test_directed_multigraph_path(self):
     G = networkx.MultiDiGraph()
     G.add_path(list(range(6)))
     partition = [[0, 1], [2, 3], [4, 5]]
     M = networkx.blockmodel(G, partition, multigraph=True)
     assert_equal(sorted(M.nodes()), [0, 1, 2])
     assert_equal(sorted(M.edges()), [(0, 1), (1, 2)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 1)
         assert_equal(M.node[n]['nnodes'], 2)
         assert_equal(M.node[n]['density'], 0.5)
Exemplo n.º 19
0
 def test_directed_multigraph_path(self):
     G = networkx.MultiDiGraph()
     G.add_path(list(range(6)))
     partition=[[0,1],[2,3],[4,5]]
     M=networkx.blockmodel(G,partition,multigraph=True)
     assert_equal(sorted(M.nodes()),[0,1,2])
     assert_equal(sorted(M.edges()),[(0,1),(1,2)])
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'],1)
         assert_equal(M.node[n]['nnodes'],2)
         assert_equal(M.node[n]['density'],0.5)
Exemplo n.º 20
0
 def test_barbell_plus(self):
     G=networkx.barbell_graph(3,0)
     G.add_edge(0,5) # add extra edge between bells
     partition=[[0,1,2],[3,4,5]]
     M=networkx.blockmodel(G,partition)
     assert_equal(sorted(M.nodes()),[0,1])
     assert_equal(sorted(M.edges()),[(0,1)])
     assert_equal(M[0][1]['weight'],2)
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'],3)
         assert_equal(M.node[n]['nnodes'],3)
         assert_equal(M.node[n]['density'],1.0)
Exemplo n.º 21
0
def pajek_group(G, Grupos):
    print("comienza reduccion de grafo")

    #elimino nodos con grado cero.

    #nuevo experimento, miraremos si es igual al arrojado por nuestro algoritmo.

    B = nx.blockmodel(G,Grupos)

    #Re nombro los nodos para eliminar el nodo cero y que todo corresponda a los grupos encontrados en la funcion anterior.
    mapping = {} #variable que guardara el nuevo valor de nodos.
    cont = 0
    for i in B.nodes(): #creo diccionario que guarda el nuevo valor de nodos.
        mapping[cont] = cont + 1
        cont += 1

    A=nx.relabel_nodes(B,mapping) #asigno grafo con el nuevo valor de nodos.

   
    NodosBorrar = []
    cont_group = 1
    cont = 1
    for i in A.nodes():
        if A.degree(cont_group) != 0: 
           cont += 1
        if A.degree(cont_group) == 0: 
           NodosBorrar.append(cont_group)
        cont_group += 1
    

    for i in NodosBorrar: A.remove_node(i)
    Nodes = A.number_of_nodes()
    
    print "*vertices ", A.number_of_nodes()
    cont = 1
    for i in A.nodes():
		print cont,'"Grupo', i, '"'
		cont += 1 
    print "*Arcs"
    for i in A.edges(): print i[0], i[1], int(A[i[0]][i[1]]['weight'])
 
    pos = nx.spring_layout(A)
    labels={}
    cont = 1
    for i in A.nodes():
        colr = float(cont)/Nodes
        nx.draw_networkx_nodes(A, pos, [i] , node_size = 250, node_color = str(colr), with_labels=True)
        labels[i] = i
        cont += 1

    nx.draw_networkx_labels(A,pos,labels,font_size=5)        
    nx.draw_networkx_edges(A,pos, alpha=0.5)
    plt.show()
Exemplo n.º 22
0
    def _neighbors(self, node, levels=1, graph=None):
        """Return graph of neighbors around node in graph (default: self.dataG)
        to a certain number of levels"""

        if graph is None:
            graph = self.dataG

        if not isinstance(node, (list, tuple, set)):
            node = [node,]

        neighbors = set(node)
        blocks = [[n,] for n in node]
        for i in range(levels):
            for n in neighbors:
                new_neighbors = set(graph.neighbors(n)) - neighbors
                blocks.append(new_neighbors)
                neighbors = neighbors.union(new_neighbors)
        G = graph.subgraph(neighbors)

        if len(blocks) > 1:
            # Create a block repersentation of our graph and make sure we're plotting
            #  anything that connects the blocks too

            # Create blocks for each individual node not already in a block
            non_blocked = set(self.dataG.nodes()) - neighbors
            non_blocked = [[a,] for a in non_blocked]

            partitions = blocks + non_blocked

            B = nx.blockmodel(graph, partitions)

            # The resulting graph will has nodes numbered according their index in partitions
            # We want to go through the partitions which are blocks and find the shortest path

            num_blocks = len(blocks)
            for frm_node, to_node in zip(range(num_blocks), range(1,num_blocks-1)):
                try:
                    path = nx.shortest_path(B, frm_node, to_node)
                except nx.NetworkXNoPath as e:
                    pass # In an island, which is permissible
                except nx.NetworkXError as e:
                    tkm.showerror("Node not in graph", str(e))
                    return
                else:
                    # Break path in B back down into path in G
                    path2 = []
                    for a in path[1:-1]: # don't include end points
                        for n in partitions[a]:
                            neighbors.add(n)
            G = graph.subgraph(neighbors)


        return G
Exemplo n.º 23
0
 def test_barbell_plus(self):
     G = networkx.barbell_graph(3, 0)
     G.add_edge(0, 5)  # add extra edge between bells
     partition = [[0, 1, 2], [3, 4, 5]]
     M = networkx.blockmodel(G, partition)
     assert_equal(sorted(M.nodes()), [0, 1])
     assert_equal(sorted(M.edges()), [(0, 1)])
     assert_equal(M[0][1]['weight'], 2)
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 3)
         assert_equal(M.node[n]['nnodes'], 3)
         assert_equal(M.node[n]['density'], 1.0)
Exemplo n.º 24
0
    def plot_additional(self, home_nodes, levels=0):
        """Add nodes to existing plot.  Prompt to include link to existing
        if possible.  home_nodes are the nodes to add to the graph"""

        new_nodes = self._neighbors(home_nodes, levels=levels)
        new_nodes = home_nodes.union(new_nodes)

        displayed_data_nodes = set(
            [v['dataG_id'] for k, v in self.dispG.node.items()])

        # It is possible the new nodes create a connection with the existing
        #  nodes; in such a case, we don't need to try to find the shortest
        #  path between the two blocks
        current_num_islands = nx.number_connected_components(self.dispG)
        new_num_islands = nx.number_connected_components(
            self.dataG.subgraph(displayed_data_nodes.union(new_nodes)))
        if new_num_islands > current_num_islands:
            # Find shortest path between two blocks graph and, if it exists,
            #  ask the user if they'd like to include those nodes in the
            #  display as well.
            # First, create a block model of our data graph where what is
            #  current displayed is a block, the new nodes are a a block
            all_nodes = set(self.dataG.nodes())
            singleton_nodes = all_nodes - displayed_data_nodes - new_nodes
            singleton_nodes = map(lambda x: [x], singleton_nodes)
            partitions = [displayed_data_nodes, new_nodes] + \
                         list(singleton_nodes)
            B = nx.blockmodel(self.dataG, partitions, multigraph=True)

            # Find shortest path between existing display (node 0) and
            #  new display island (node 1)
            try:
                path = nx.shortest_path(B, 0, 1)
            except nx.NetworkXNoPath:
                pass
            else:
                ans = tkm.askyesno(
                    "Plot path?", "A path exists between the "
                    "currently graph and the nodes you've asked to be added "
                    "to the display.  Would you like to plot that path?")
                if ans:  # Yes to prompt
                    # Add the nodes from the source graph which are part of
                    #  the path to the new_nodes set
                    # Don't include end points because they are the two islands
                    for u in path[1:-1]:
                        Gu = B.node[u]['graph'].nodes()
                        assert len(Gu) == 1
                        Gu = Gu[0]
                        new_nodes.add(Gu)

        # Plot the new nodes
        self._plot_additional(new_nodes)
Exemplo n.º 25
0
def labeled_blockmodel(g,partition):
    """
    Perform blockmodel transformation on graph *g*
    and partition represented by dictionary *partition*.
    Values of *partition* are used to partition the graph.
    Keys of *partition* are used to label the nodes of the
    new graph.
    """
    new_g = nx.blockmodel(g,partition.values())
    labels = dict(enumerate(partition.keys()))
    new_g = nx.relabel_nodes(new_g,labels)
    
    return new_g
Exemplo n.º 26
0
def plot_dhc(PG, part, labels, lvl, pos):
    fig = plt.figure(figsize=(9,6))
    BM=nx.blockmodel(PG, part)
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[1 for (u,v,d) in BM.edges(data=True)]
    BM_pos = nx.spring_layout(BM,iterations=200)
    plt.axis("off")
    plt.title("Block Model Clusters at level: {}".format(lvl))
    nx.draw(BM, BM_pos,node_size=node_size,width=edge_width,with_labels=False)
    fig = plt.figure(figsize=(9,6))
    plt.axis("off")
    plt.title("Node Clusters at level: {}".format(lvl))
    nx.draw_networkx(PG, pos, node_size=45, cmap = plt.get_cmap("jet"), node_color=labels, with_labels = False)
Exemplo n.º 27
0
def labeled_blockmodel(g, partition):
    """
    Perform blockmodel transformation on graph *g*
    and partition represented by dictionary *partition*.
    Values of *partition* are used to partition the graph.
    Keys of *partition* are used to label the nodes of the
    new graph.
    """
    new_g = nx.blockmodel(g, partition.values())
    labels = dict(enumerate(partition.keys()))
    new_g = nx.relabel_nodes(new_g, labels)

    return new_g
Exemplo n.º 28
0
def plot_dhc(PG, part, labels, lvl, pos):
    fig = plt.figure(figsize=(9,6))
    BM=nx.blockmodel(PG, part)
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[1 for (u,v,d) in BM.edges(data=True)]
    BM_pos = nx.spring_layout(BM,iterations=200)
    plt.axis("off")
    plt.title("Block Model Clusters at level: {}".format(lvl))
    nx.draw(BM, BM_pos,node_size=node_size,width=edge_width,with_labels=False)
    fig = plt.figure(figsize=(9,6))
    plt.axis("off")
    plt.title("Node Clusters at level: {}".format(lvl))
    nx.draw_networkx(PG, pos, node_size=45, cmap = plt.get_cmap("jet"), node_color=labels, with_labels = False)
Exemplo n.º 29
0
def meta_mpg(g):
    """Meta Multimodal Probability Graph

    Parameters
    ----------
    g : nx.Graph
        Multimodal probability graph

    Returns
    -------
    G : nx.Graph
        Multimodal probability graph where hard-linked nodes (p=1) are
        grouped into meta-nodes
    groups : list of lists
        Groups of nodes
    """

    # Group of hard-linked nodes
    # (ie. nodes connected with probability p=1)
    hard = nx.Graph()
    hard.add_nodes_from(g)
    hard.add_edges_from([(e,f) for e,f,d in g.edges_iter(data=True)
                               if d[PROBABILITY] == 1.])
    groups = nx.connected_components(hard)

    # meta graph with one node per group
    G = nx.blockmodel(g, groups, multigraph=True)

    meta = nx.Graph()
    for n in range(len(groups)):
        meta.add_node(n)
        for m in range(n+1, len(groups)):

            # do not do anything in case there is no edge
            # between those two meta-nodes
            if not G.has_edge(n, m):
                continue

            # obtain probabilities of all edges between n & m
            probabilities = [data[PROBABILITY] for data in G[n][m].values()]

            # raise an error in case of conflict (p=0 vs. p>0)
            if len(set(probabilities)) > 1 and 0 in probabilities:
                raise ValueError('conflict in meta-edges between %r and %r:' \
                                 'probabilities = %r' % (groups[n],
                                                         groups[m],
                                                         probabilities))

            meta.add_edge(n, m, {PROBABILITY: np.mean(probabilities)})

    return meta, groups
Exemplo n.º 30
0
    def plot_additional(self, home_nodes, levels=0):
        """Add nodes to existing plot.  Prompt to include link to existing
        if possible.  home_nodes are the nodes to add to the graph"""

        new_nodes = self._neighbors(home_nodes, levels=levels)
        new_nodes = home_nodes.union(new_nodes)

        displayed_data_nodes = set([ v['dataG_id']
                            for k,v in self.dispG.node.items() ])

        # It is possible the new nodes create a connection with the existing
        #  nodes; in such a case, we don't need to try to find the shortest
        #  path between the two blocks
        current_num_islands = nx.number_connected_components(self.dispG)
        new_num_islands = nx.number_connected_components(
            self.dataG.subgraph(displayed_data_nodes.union(new_nodes)))
        if new_num_islands > current_num_islands:
            # Find shortest path between two blocks graph and, if it exists,
            #  ask the user if they'd like to include those nodes in the
            #  display as well.
            # First, create a block model of our data graph where what is
            #  current displayed is a block, the new nodes are a a block
            all_nodes = set(self.dataG.nodes())
            singleton_nodes = all_nodes - displayed_data_nodes - new_nodes
            singleton_nodes = map(lambda x: [x], singleton_nodes)
            partitions = [displayed_data_nodes, new_nodes] + \
                         list(singleton_nodes)
            B = nx.blockmodel(self.dataG, partitions, multigraph=True)

            # Find shortest path between existing display (node 0) and
            #  new display island (node 1)
            try:
                path = nx.shortest_path(B, 0, 1)
            except nx.NetworkXNoPath:
                pass
            else:
                ans = tkm.askyesno("Plot path?", "A path exists between the "
                  "currently graph and the nodes you've asked to be added "
                  "to the display.  Would you like to plot that path?")
                if ans: # Yes to prompt
                    # Add the nodes from the source graph which are part of
                    #  the path to the new_nodes set
                    # Don't include end points because they are the two islands
                    for u in path[1:-1]:
                        Gu = B.node[u]['graph'].nodes()
                        assert len(Gu) == 1; Gu = Gu[0]
                        new_nodes.add(Gu)

        # Plot the new nodes
        self._plot_additional(new_nodes)
Exemplo n.º 31
0
def draw_hc(G, lvl):
    plt.close()
    plt.ion()
    fig=plt.figure(figsize=(9,6))
    plt.axis("off")
    G=nx.convert_node_labels_to_integers(G)
    partitions=create_hc(G)
    BM=nx.blockmodel(G,partitions)
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[1 for (u,v,d) in BM.edges(data=True)]
    pos = nx.spring_layout(BM,iterations=200)
    nx.draw(BM,pos,node_size=node_size,width=edge_width,with_labels=False)
    plt.title('Agglomerative Hierarchical Clustering for Citibike Network, lvl {}'.format(lvl))
    plt.show()
    return BM, partitions
Exemplo n.º 32
0
def draw_hc(G, lvl):
    plt.close()
    plt.ion()
    fig=plt.figure(figsize=(9,6))
    plt.axis("off")
    G=nx.convert_node_labels_to_integers(G)
    partitions=create_hc(G)
    BM=nx.blockmodel(G,partitions)
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[1 for (u,v,d) in BM.edges(data=True)]
    pos = nx.spring_layout(BM,iterations=200)
    nx.draw(BM,pos,node_size=node_size,width=edge_width,with_labels=False)
    plt.title('Agglomerative Hierarchical Clustering for Citibike Network, lvl {}'.format(lvl))
    plt.show()
    return BM, partitions
Exemplo n.º 33
0
def draw_graph(G, membership, filename=None):
    partition = { i : [] for i in range(max(membership) + 1) }
    for n,p in zip(list(range(len(G))),membership):
        partition[p].append(n)
    clustering = list(partition.values())
    # print clustering

    BM = nx.blockmodel(G, clustering)
    # print BM.number_of_nodes(), G.number_of_nodes()

    pyplot.clf()
    posBM = nx.spring_layout(BM)
    nx.draw(BM, posBM, with_labels=True)
    # pyplot.show()
    if filename:
        pyplot.savefig(filename)
Exemplo n.º 34
0
 def test_weighted_path(self):
     G=networkx.path_graph(6)
     G[0][1]['weight']=1
     G[1][2]['weight']=2
     G[2][3]['weight']=3
     G[3][4]['weight']=4
     G[4][5]['weight']=5
     partition=[[0,1],[2,3],[4,5]]
     M=networkx.blockmodel(G,partition)
     assert_equal(sorted(M.nodes()),[0,1,2])
     assert_equal(sorted(M.edges()),[(0,1),(1,2)])
     assert_equal(M[0][1]['weight'],2)
     assert_equal(M[1][2]['weight'],4)
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'],1)
         assert_equal(M.node[n]['nnodes'],2)
         assert_equal(M.node[n]['density'],1.0)
Exemplo n.º 35
0
 def test_weighted_path(self):
     G = networkx.path_graph(6)
     G[0][1]['weight'] = 1
     G[1][2]['weight'] = 2
     G[2][3]['weight'] = 3
     G[3][4]['weight'] = 4
     G[4][5]['weight'] = 5
     partition = [[0, 1], [2, 3], [4, 5]]
     M = networkx.blockmodel(G, partition)
     assert_equal(sorted(M.nodes()), [0, 1, 2])
     assert_equal(sorted(M.edges()), [(0, 1), (1, 2)])
     assert_equal(M[0][1]['weight'], 2)
     assert_equal(M[1][2]['weight'], 4)
     for n in M.nodes():
         assert_equal(M.node[n]['nedges'], 1)
         assert_equal(M.node[n]['nnodes'], 2)
         assert_equal(M.node[n]['density'], 1.0)
Exemplo n.º 36
0
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout
import matplotlib.pyplot as plt


def visualize_a_graph(graph):
    pos = graphviz_layout(graph)
    nx.draw(graph, pos, width=4.0, alpha=0.5, edge_color='grey',
            node_color='white', node_size=500, with_labels=True)

    nx.draw_networkx_edge_labels(graph, pos)
    nx.draw_networkx_labels(graph, pos)
    plt.axis('off')
    plt.show()


if __name__ == '__main__':
    G = nx.path_graph(6)
    visualize_a_graph(G)

    partition = [[0, 1], [2, 3], [4, 5]]
    graph = nx.Graph(nx.blockmodel(G, partition, multigraph=True))
    visualize_a_graph(graph)
Exemplo n.º 37
0
    for n, p in zip(list(range(len(G))), membership):
        partition[p].append(n)
    return list(partition.values())


if __name__ == '__main__':
    G = nx.read_edgelist("hartford_drug.edgelist")

    # Extract largest connected component into graph H
    H = nx.connected_component_subgraphs(G)[0]
    # Makes life easier to have consecutively labeled integer nodes
    H = nx.convert_node_labels_to_integers(H)
    # Create parititions with hierarchical clustering
    partitions = create_hc(H)
    # Build blockmodel graph
    BM = nx.blockmodel(H, partitions)

    # Draw original graph
    pos = nx.spring_layout(H, iterations=100)
    fig = plt.figure(1, figsize=(6, 10))
    ax = fig.add_subplot(211)
    nx.draw(H, pos, with_labels=False, node_size=10)
    plt.xlim(0, 1)
    plt.ylim(0, 1)

    # Draw block model with weighted edges and nodes sized by number of internal nodes
    node_size = [BM.node[x]['nnodes'] * 10 for x in BM.nodes()]
    edge_width = [(2 * d['weight']) for (u, v, d) in BM.edges(data=True)]
    # Set positions to mean of positions of internal nodes from original graph
    posBM = {}
    for n in BM:
Exemplo n.º 38
0
def create_block_diagram(graph, member_dict):
    BM = nx.blockmodel(graph, member_dict.values())
    return BM
Exemplo n.º 39
0
        xy_list_dict[label] = [line_to_xy_dict[xy_list[i]]]

nodes_chunks = []
for key in xy_list_dict.keys():
    nodes_chunks.append(xy_list_dict[key])

new_line_xy_dict = {}
cluster_dict = {}
for i, chunk in enumerate(nodes_chunks):
    coordinates = [line_xy_dict[ident] for ident in chunk]
    com = numpy.sum(coordinates, 0) / len(coordinates)
    new_line_xy_dict[i] = com
    cluster_dict[i] = chunk

print('Performing blockmodels')
blocks_graph = nx.blockmodel(g, nodes_chunks, multigraph=True)
g = blocks_graph
#nx.draw(g, new_line_xy_dict, node_shape='.', alpha=0.002)
#plt.show()

edges = g.edges()
edges_weigth = {}
for e in edges:
    try:
        edges_weigth[e] += 1
    except:
        edges_weigth[e] = 1

new_edges = []
for e, weigth in edges_weigth.items():
    if weigth < WEIGTH_THRESHOLD:
Exemplo n.º 40
0
 def test_overlapping(self):
     G=networkx.path_graph(6)
     partition=[[0,1,2],[2,3],[4,5]]
     M=networkx.blockmodel(G,partition)
Exemplo n.º 41
0
 def test_overlapping(self):
     G = networkx.path_graph(6)
     partition = [[0, 1, 2], [2, 3], [4, 5]]
     M = networkx.blockmodel(G, partition)
Exemplo n.º 42
0
#H=nx.Graph()

ynode = []
for com in listsort:
    xnode = []
    for comnodes in com:
        for node in G.nodes(data=True):
            if int(node[1]['label']) == int(comnodes):
                xnode.append(int(node[1]['id']))
                xnode.sort()
                bunch = [int(node[1]['id'])] + G.neighbors(int(node[1]['id']))
                Gprime = G.subgraph(bunch)
                H.add_edges_from(Gprime.edges())
    ynode.append(xnode)

BM = nx.blockmodel(H, ynode)
#pos=nx.circular_layout(H)
pos = nx.graphviz_layout(H, prog='twopi')
#pos=nx.spring_layout(H, iterations = 50, weighted = False)

fig = plt.figure(figsize=(10, 10))
ax = fig.add_axes((0.0, 0.0, 1.0, 1.0))

nshells = len(ynode)

listcomattr = []
weightcom = []
comact = []
bmicom = []
for s in range(nshells):
Exemplo n.º 43
0
def main(argv):
    #Standardvalues
    partitionfile = "data/partitions/final_partitions_p100_200_0.2.csv"
    project = "584"
    to_pajek = False
    try:
      opts, args = getopt.getopt(argv,"p:s:o")
    except getopt.GetoptError:
      print 'group_bridging.py -p <project_name> -s <partitionfile> -o [if you want pajek output]'
      sys.exit(2)
    for opt, arg in opts:
        if opt in ("-p"):
            project = arg
        elif opt in ("-s"):
            partitionfile = arg
        elif opt in ("-o"):
             to_pajek = True
        else:
            print 'group_bridging.py -p <project_name> -s <partitionfile> -o [if you want pajek output]'
    
    print "##################### GROUP BRIDGING ########################"
    print "Project %s " % project
    print "Partition %s" % partitionfile
    
    ff_edges_writer = csv.writer(open("results/%s_ff_bridging_edges.csv" % project, "wb"))
    at_edges_writer = csv.writer(open("results/%s_at_bridging_edges.csv" % project, "wb"))
    rt_edges_writer = csv.writer(open("results/%s_rt_bridging_edges.csv" % project, "wb"))
    
    csv_bridging_writer = csv.writer(open('results/spss/group bridging/%s_group_bridging.csv' % project , 'wb'))
    
    csv_bridging_writer.writerow(["Project", "Name", "Member_count", "Competing_Lists",
                                "FF_bin_degree", "FF_bin_in_degree", "FF_bin_out_degree",
                                "FF_volume_in","FF_volume_out",
                                "FF_bin_betweeness","FF_bin_closeness", "FF_bin_pagerank", #"FF_bin_eigenvector",
                                "FF_bin_c_size","FF_bin_c_density","FF_bin_c_hierarchy","FF_bin_c_index",
                                "AT_bin_degree", "AT_bin_in_degree", "AT_bin_out_degree",
                                "AT_bin_betweeness", "AT_bin_closeness", "AT_bin_pagerank", #"AT_bin_eigenvector",                            
                                "AT_bin_c_size","AT_bin_c_density","AT_bin_c_hierarchy","AT_bin_c_index",
                                "AT_volume_in", "AT_volume_out",
                                "RT_volume_in", "RT_volume_out",
                                "FF_rec", "AT_rec", "AT_avg", "FF_avg"])    
    
    # Get the overall network from disk    
    FF = nx.read_edgelist('data/networks/%s_FF.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph()) 
    AT = nx.read_edgelist('data/networks/%s_solr_AT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph()) 
    RT = nx.read_edgelist('data/networks/%s_solr_RT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph())
        
    # Read in the partition
    tmp = hp.get_partition(partitionfile)
    partitions = tmp[0]
    groups = tmp[1]
    
    #Read in members count for each project
    reader = csv.reader(open("results/stats/%s_lists_stats.csv" % project, "rb"), delimiter=",")
    temp  = {}
    reader.next() # Skip first row
    for row in reader:        
            temp[row[0]] = {"name":row[0],"member_count":int(row[3])}
    
    #Read in the list-listings for individuals
    listings = {}
    indiv_reader = csv.reader(open(partitionfile))
    for row in indiv_reader:                
            if listings.has_key(row[1]):
                listings[row[1]]["competing_lists"] += int(row[3])
            else:
                listings[row[1]] = {"competing_lists": int(row[3])}                            
           
    # Add dummy nodes if they are missing in the networks
    for partition in partitions:
            for node in partition:
                FF.add_node(node)
                AT.add_node(node)
                RT.add_node(node)
            
    #Blockmodel the networks into groups according to the partition
    P_FF = nx.blockmodel(FF,partitions)
    P_AT = nx.blockmodel(AT,partitions)
    P_RT = nx.blockmodel(RT,partitions)
    
    #Name the nodes in the network
    #TODO check: How do I know that the names really match?
    mapping = {}
    mapping_pajek = {}
    i = 0
    for group in groups:
        mapping_pajek[i] = "\"%s\"" % group # mapping for pajek
        mapping[i] = "%s" % group 
        i += 1
    
    H_FF = nx.relabel_nodes(P_FF,mapping)
    H_AT = nx.relabel_nodes(P_AT,mapping)
    H_RT = nx.relabel_nodes(P_RT,mapping)
    
    #Outpt the networks to pajek if needed
    if to_pajek:
        OUT_FF = nx.relabel_nodes(P_FF,mapping_pajek)
        OUT_AT = nx.relabel_nodes(P_AT,mapping_pajek)
        OUT_RT = nx.relabel_nodes(P_RT,mapping_pajek)
        
        #Write the blocked network out to disk
        nx.write_pajek(OUT_FF,"results/networks/%s_grouped_FF.net" % project)
        nx.write_pajek(OUT_AT,"results/networks/%s_grouped_AT.net" % project)
        nx.write_pajek(OUT_RT,"results/networks/%s_grouped_RT.net" % project)
    
    ########## Output the Edges between groups to csv ##############
    # Needed for the computation of individual bridging
    # Edges in both directions between the groups are addded up
    
    processed_edges = []
    for (u,v,attrib) in H_FF.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_FF.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                ff_edges_writer.writerow([u,v,attrib["weight"]+H_FF[v][u]["weight"]])
            else:
                ff_edges_writer.writerow([u,v,attrib["weight"]])
                
    processed_edges = []
    for (u,v,attrib) in H_AT.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_AT.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                at_edges_writer.writerow([u,v,attrib["weight"]+H_AT[v][u]["weight"]])
            else:
                at_edges_writer.writerow([u,v,attrib["weight"]])
                    
    processed_edges = []
    for (u,v,attrib) in H_RT.edges(data=True):
        if "%s%s" %(u,v)  not in processed_edges:
            processed_edges.append("%s%s" % (u,v))            
            if H_RT.has_edge(v,u):
                processed_edges.append("%s%s" % (v,u))
                rt_edges_writer.writerow([u,v,attrib["weight"]+H_RT[v][u]["weight"]])
            else:
                rt_edges_writer.writerow([u,v,attrib["weight"]])                

    
    ########## TRIM EDGES ################
    # For meaningfull results we have to trim edges in the AT and FF network so the whole network just doesnt look like a blob            
    # It is chosen this way so the network remains as one component
    
    THRESHOLD = min([hp.min_threshold(H_AT),hp.min_threshold(H_FF)])-1    
    H_FF = hp.trim_edges(H_FF, THRESHOLD)
    H_AT = hp.trim_edges(H_AT, THRESHOLD)    

    ########## MEASURES ##############
    
    #Get the number of nodes in the aggregated networks
    #FF_nodes = {}
    #for node in H_FF.nodes(data=True):
    #        FF_nodes[node[0]] = node[1]["nnodes"]
    
    
    #Get the FF network measures of the nodes
    # Works fine on binarized Data
    FF_bin_degree = nx.degree_centrality(H_FF) 
    FF_bin_in_degree = nx.in_degree_centrality(H_FF) # The attention paid towards this group
    FF_bin_out_degree = nx.out_degree_centrality(H_FF) # The attention that this group pays towards other people
    FF_bin_betweenness = nx.betweenness_centrality(H_FF,weight="weight") # How often is the group between other groups
    FF_bin_closeness = nx.closeness_centrality(H_FF) #FF_bin_eigenvector = nx.eigenvector_centrality(H_FF)
    FF_bin_pagerank = nx.pagerank(H_FF)        
    FF_bin_struc = sx.structural_holes(H_FF)
    
    # AT network measures of the nodes
    AT_bin_degree = nx.degree_centrality(H_AT)
    AT_bin_in_degree = nx.in_degree_centrality(H_AT)
    AT_bin_out_degree = nx.out_degree_centrality(H_AT)
    AT_bin_betweenness = nx.betweenness_centrality(H_AT,weight="weight") 
    AT_bin_closeness = nx.closeness_centrality(H_AT) #AT_bin_eigenvector = nx.eigenvector_centrality(H_AT)
    AT_bin_pagerank = nx.pagerank(H_AT)        
    AT_bin_struc = sx.structural_holes(H_AT)
    
    # Tie strengths
    dAT_avg_tie = hp.individual_average_tie_strength(H_AT)
    dFF_avg_tie = hp.individual_average_tie_strength(H_FF)
    dAT_rec = hp.individual_reciprocity(H_AT)    
    dFF_rec = hp.individual_reciprocity(H_FF)
    
    # Dependent Variable see csv
    # TODO A measure that calculates how often Tweets travel through this group: Eventually betweeness in the RT graph
    
    #Arrange it in a list and output
    for node in FF_bin_degree.keys():                
                csv_bridging_writer.writerow([project, node, int(temp[node]["member_count"]), listings[node]["competing_lists"],
                                                FF_bin_degree[node], FF_bin_in_degree[node], FF_bin_out_degree[node],
                                                H_FF.in_degree(node,weight="weight"), H_FF.out_degree(node,weight="weight"),
                                                FF_bin_betweenness[node],FF_bin_closeness[node],FF_bin_pagerank[node], #FF_bin_eigenvector[node],
                                                FF_bin_struc[node]['C-Size'],FF_bin_struc[node]['C-Density'],FF_bin_struc[node]['C-Hierarchy'],FF_bin_struc[node]['C-Index'],
                                                AT_bin_degree[node], AT_bin_in_degree[node], AT_bin_out_degree[node],
                                                AT_bin_betweenness[node], AT_bin_closeness[node], AT_bin_pagerank[node], #AT_bin_eigenvector[node],
                                                AT_bin_struc[node]['C-Size'],AT_bin_struc[node]['C-Density'],AT_bin_struc[node]['C-Hierarchy'],AT_bin_struc[node]['C-Index'],
                                                H_AT.in_degree(node,weight="weight"), H_AT.out_degree(node,weight="weight"),
                                                H_RT.in_degree(node,weight="weight"), H_RT.out_degree(node,weight="weight"),
                                                dFF_rec[node],dAT_rec[node],dAT_avg_tie[node],dFF_avg_tie[node]
                                            ])        
Exemplo n.º 44
0
    partition=defaultdict(list)
    for n,p in zip(list(range(len(G))),membership):
        partition[p].append(n)
    return list(partition.values())

if __name__ == '__main__':
    G=nx.read_edgelist("hartford_drug.edgelist")

    # Extract largest connected component into graph H
    H=nx.connected_component_subgraphs(G)[0]
    # Makes life easier to have consecutively labeled integer nodes
    H=nx.convert_node_labels_to_integers(H)
    # Create parititions with hierarchical clustering
    partitions=create_hc(H)
    # Build blockmodel graph
    BM=nx.blockmodel(H,partitions)


    # Draw original graph
    pos=nx.spring_layout(H,iterations=100)
    fig=plt.figure(1,figsize=(6,10))
    ax=fig.add_subplot(211)
    nx.draw(H,pos,with_labels=False,node_size=10)
    plt.xlim(0,1)
    plt.ylim(0,1)

    # Draw block model with weighted edges and nodes sized by number of internal nodes
    node_size=[BM.node[x]['nnodes']*10 for x in BM.nodes()]
    edge_width=[(2*d['weight']) for (u,v,d) in BM.edges(data=True)]
    # Set positions to mean of positions of internal nodes from original graph
    posBM={}