예제 #1
0
	def test_lpa(self):
		out_label_dict = lpa.lpa(self.g0, opt.tolerance, 1,self.label_dict1)  	# all are in the same community => no change
		assert out_label_dict == self.label_dict1 
		out_label_dict = lpa.lpa(self.g0, opt.tolerance, 1,self.label_dict2)	# clique with 1 in 'b', others in 'a'
		assert out_label_dict == self.label_dict1 
		# a---a---b    a---a---b    
		# | x | \ | => | x |   | 
		# a---a---b    a---a---b  
		out_label_dict = lpa.lpa(self.g1, opt.tolerance, 1,self.label_dict3)  
		assert out_label_dict == self.label_dict4  
예제 #2
0
 def test_lpa(self):
     out_label_dict = lpa.lpa(
         self.g0, opt.tolerance, 1,
         self.label_dict1)  # all are in the same community => no change
     assert out_label_dict == self.label_dict1
     out_label_dict = lpa.lpa(
         self.g0, opt.tolerance, 1,
         self.label_dict2)  # clique with 1 in 'b', others in 'a'
     assert out_label_dict == self.label_dict1
     # a---a---b    a---a---b
     # | x | \ | => | x |   |
     # a---a---b    a---a---b
     out_label_dict = lpa.lpa(self.g1, opt.tolerance, 1, self.label_dict3)
     assert out_label_dict == self.label_dict4
예제 #3
0
def generate_dendogram(graph, delta, tolerance, lambduh, Zgraph, part_init=None):

    """Function to find communities in a graph and return the associated dendogram.

    A dendogram is a tree and each level is a partition of the graph nodes.  
    Level 0 is the first partition, which contains the smallest communities, 
    and the best is len(dendogram) - 1. The higher the level, the bigger 
    the size of the communities. At each step, nodes (which are the communities
    from the previous step) are merged into aggregate communities such that modularity
    is maximized, subject to the estrangement constraint. For more details on the 
    basic agglomerative procedure, see: [Blondel08]_.

    Parameters
    ----------
    graph : networkx.Graph
        The networkx graph representing the network.
    delta : float
        The temporal divergence. Smaller values imply greater emphasis on temporal
        contiguity whereas larger values place greater emphasis on finding better
        instanteous communities.
    tolerance: float
        For a label to be considered a dominant label, it must be within this much of the maximum
        value found for the quality function. The smaller it is, the fewer dominant labels there 
        will be.     
    lambduh: float
        The Lagrange multiplier.
    Zgraph : networkx.Graph
        A graph containing edges between nodes of the same community in all previous snapshots
        The current labelling of the nodes in the graph
    part_init : dictiionary, optional {node:community}
        The algorithm will start using this partition of the nodes. 
        It is a dictionary where keys are their nodes and values the communities.



    Returns
    -------
    dendogram : list of dictionaries
        A list of partitions, ie dictionnaries where keys of the i+1 are the values of the i, 
        and where keys of the first are the nodes of graph.
    
    Raises
    ------
    TypeError:
        If the graph is not a networkx.Graph

    See Also
    --------
    best_partition

    Notes
    -----
    Uses Louvain algorithm: [Blondel08]_

    Examples
    --------
    >>> G=nx.erdos_renyi_graph(100, 0.01)
    >>> dendo = generate_dendogram(G)
    >>> for level in range(len(dendo) - 1) :
    >>>     print "partition at level", level, "is", partition_at_level(dendo, level)
    """

    if type(graph) != nx.Graph:
        raise TypeError("Bad graph type, use only non directed graph")
    current_graph = graph.copy()
    current_Zgraph = Zgraph.copy()

    partition_list = []
    F = -99999999.0

    while True:
        partition = lpa.lpa(current_graph, tolerance, lambduh, Z=current_Zgraph)

        mod = modularity(partition, current_graph)
        E = utils.Estrangement(current_graph, partition, current_Zgraph)
        new_F = mod - lambduh * E + lambduh * delta
        logging.info("level=%d, Q=%f, E=%f, F=%f -----------", len(partition_list), mod, E, new_F)

        if new_F - F < __MIN:
            break
        partition_list.append(partition)
        F = new_F
        current_graph, current_Zgraph = induced_graph(partition, current_graph, current_Zgraph)
    return partition_list
예제 #4
0
def generate_dendogram(graph,
                       delta,
                       tolerance,
                       lambduh,
                       Zgraph,
                       part_init=None):
    """Function to find communities in a graph and return the associated dendogram.

    A dendogram is a tree and each level is a partition of the graph nodes.  
    Level 0 is the first partition, which contains the smallest communities, 
    and the best is len(dendogram) - 1. The higher the level, the bigger 
    the size of the communities. At each step, nodes (which are the communities
    from the previous step) are merged into aggregate communities such that modularity
    is maximized, subject to the estrangement constraint. For more details on the 
    basic agglomerative procedure, see: [Blondel08]_.

    Parameters
    ----------
    graph : networkx.Graph
        The networkx graph representing the network.
    delta : float
        The temporal divergence. Smaller values imply greater emphasis on temporal
        contiguity whereas larger values place greater emphasis on finding better
        instanteous communities.
    tolerance: float
        For a label to be considered a dominant label, it must be within this much of the maximum
        value found for the quality function. The smaller it is, the fewer dominant labels there 
        will be.     
    lambduh: float
        The Lagrange multiplier.
    Zgraph : networkx.Graph
        A graph containing edges between nodes of the same community in all previous snapshots
        The current labelling of the nodes in the graph
    part_init : dictiionary, optional {node:community}
        The algorithm will start using this partition of the nodes. 
        It is a dictionary where keys are their nodes and values the communities.



    Returns
    -------
    dendogram : list of dictionaries
        A list of partitions, ie dictionnaries where keys of the i+1 are the values of the i, 
        and where keys of the first are the nodes of graph.
    
    Raises
    ------
    TypeError:
        If the graph is not a networkx.Graph

    See Also
    --------
    best_partition

    Notes
    -----
    Uses Louvain algorithm: [Blondel08]_

    Examples
    --------
    >>> G=nx.erdos_renyi_graph(100, 0.01)
    >>> dendo = generate_dendogram(G)
    >>> for level in range(len(dendo) - 1) :
    >>>     print "partition at level", level, "is", partition_at_level(dendo, level)
    """

    if type(graph) != nx.Graph:
        raise TypeError("Bad graph type, use only non directed graph")
    current_graph = graph.copy()
    current_Zgraph = Zgraph.copy()

    partition_list = []
    F = -99999999.0

    while True:
        partition = lpa.lpa(current_graph,
                            tolerance,
                            lambduh,
                            Z=current_Zgraph)

        mod = modularity(partition, current_graph)
        E = utils.Estrangement(current_graph, partition, current_Zgraph)
        new_F = mod - lambduh * E + lambduh * delta
        logging.info("level=%d, Q=%f, E=%f, F=%f -----------",
                     len(partition_list), mod, E, new_F)

        if new_F - F < __MIN:
            break
        partition_list.append(partition)
        F = new_F
        current_graph, current_Zgraph = induced_graph(partition, current_graph,
                                                      current_Zgraph)
    return partition_list