<commit_before>from __future__ import division, print_function, absolute_import
import six
range = six.moves.range

import networkx as nx
import numpy as np

def hierarchical(n, level):
    """ Generates hiearchical graph using method proposed of:
    <NAME>, <NAME>, Hierarchical organization in complex networks, PRE, 2003.

    Parameters
    ----------
    n : int
        Number of nodes
    level : int
        Number of hierarchical levels to create

    Returns
    -------
    networkx graph

    """

    if level == 0:
        return nx.complete_graph(n)
    else:
        fullG = nx.Graph()

        #get lower-order graphs
        for i in range(n):
            fullG = nx.union(hierarchical(n, level-1), fullG, rename=(str(i)+'.',''))
        
        edges = []
        suffix = ''
        for l in range(level-1):
            suffix += '.0'
            
        #connect outer nodes to the center
        center = '0.0'+suffix
        for node in fullG.nodes():
            if not '0' in node:
                edges.append((node, center))
        
        fullG.add_edges_from(edges)
        return fullG

def get_hierarchical_pos(net):
    """ Get x,y positions for plotting hierarchical graph.

    For example:

    >>> from graphy import graphgen
    >>> G = graphgen.hierarchical(3, 2)
    >>> pos = graphgen.get_hierarchical_pos(G)

    Parameters
    ----------
    net : networkx graph
        Graph whose nodes to layout

    Returns
    -------
    dict 
        Dictionary containing node:(x,y) entries

    """

    pos = {}

    for node in net.nodes():
        x, y = 0, 0
        xl = [0, -1, -1,  1,  1]
        yl = [0, -1,  1, -1,  1]
        for level_ndx, level in enumerate(map(int, node.title().split("."))):
            x += 0.5**level_ndx*(xl[level])
            y += 0.5**level_ndx*(yl[level])

        pos[node] = (x,y)

    return pos

def get_block_matrix(membership, intra_community_p, inter_community_p):
    """Generate binary block-structured matrix with with different
    probabilities of 1 entries for intra- and inter-community entries.

    For example:

    >>> from graphy import graphgen
    >>> cmx = graphgen.get_block_matrix([0,0,0,0,0,1,1,1,1,1], 0.5, 0.1)

    Parameters
    ----------
    membership : list or np.array of ints
        Array containing assignment of each node to communities
    intra_community_p : float
        Probability of having a 1 for intra-community connections
    inter_community_p : float
        Probability of having a 1 for inter-community connections

    Returns
    -------
    np.array matrix

    """

    membership = np.asarray(membership)
    N  = len(membership)
    mx = np.random.rand(N,N) > (1-inter_community_p)
    for comm in set(membership):
        ixs = np.flatnonzero(membership == comm)
        comm_size = len(ixs)
        for r in ixs:
            mx[r,ixs] = np.random.rand(comm_size) > (1-intra_community_p)
    np.fill_diagonal(mx, 0)
    return mx

def get_barbell_matrix(membership, num_conns=1):
    """Get a matrix corresponding to completely-connected communities
    connected by paths.

    For example:

    ..plot::
    >>> from graphy import graphgen
    >>> cmx = graphgen.get_barbell_matrix([0,0,0,0,0,1,1,1,1,1])
    >>> import matplotlib.pylab as plt
    >>> plt.imshow(cmx, interpolation='none') # doctest: +SKIP


    Parameters
    ----------
    membership : list or np.array of ints
        Array containing assignment of each node to communities
    num_conns : int
        Number of links that should run between communities 

    Returns
    -------
    np.array matrix

    """

    membership = np.asarray(membership)
    comms = list(set(membership))
    N  = len(membership)
    mx = np.zeros(shape=(N,N))
    for ndx1, comm in enumerate(comms):
        ixs = np.flatnonzero(membership == comm)
        for r in ixs:<commit_msg> Doc update<commit_after>from __future__ import division, print_function, absolute_import
import six
range = six.moves.range

import networkx as nx
import numpy as np

def hierarchical(n, level):
    """ Generates hiearchical graph using method proposed of:
    <NAME>, <NAME>, Hierarchical organization in complex networks, PRE, 2003.

    Parameters
    ----------
    n : int
        Number of nodes
    level : int
        Number of hierarchical levels to create

    Returns
    -------
    networkx graph

    """

    if level == 0:
        return nx.complete_graph(n)
    else:
        fullG = nx.Graph()

        #get lower-order graphs
        for i in range(n):
            fullG = nx.union(hierarchical(n, level-1), fullG, rename=(str(i)+'.',''))
        
        edges = []
        suffix = ''
        for l in range(level-1):
            suffix += '.0'
            
        #connect outer nodes to the center
        center = '0.0'+suffix
        for node in fullG.nodes():
            if not '0' in node:
                edges.append((node, center))
        
        fullG.add_edges_from(edges)
        return fullG

def get_hierarchical_pos(net):
    """ Get x,y positions for plotting hierarchical graph.

    For example:

    >>> from graphy import graphgen
    >>> G = graphgen.hierarchical(3, 2)
    >>> pos = graphgen.get_hierarchical_pos(G)

    Parameters
    ----------
    net : networkx graph
        Graph whose nodes to layout

    Returns
    -------
    dict 
        Dictionary containing node:(x,y) entries

    """

    pos = {}

    for node in net.nodes():
        x, y = 0, 0
        xl = [0, -1, -1,  1,  1]
        yl = [0, -1,  1, -1,  1]
        for level_ndx, level in enumerate(map(int, node.title().split("."))):
            x += 0.5**level_ndx*(xl[level])
            y += 0.5**level_ndx*(yl[level])

        pos[node] = (x,y)

    return pos

def get_block_matrix(membership, intra_community_p, inter_community_p):
    """Generate binary block-structured matrix with with different
    probabilities of 1 entries for intra- and inter-community entries.

    For example:

    >>> from graphy import graphgen
    >>> cmx = graphgen.get_block_matrix([0,0,0,0,0,1,1,1,1,1], 0.5, 0.1)

    Parameters
    ----------
    membership : list or np.array of ints
        Array containing assignment of each node to communities
    intra_community_p : float
        Probability of having a 1 for intra-community connections
    inter_community_p : float
        Probability of having a 1 for inter-community connections

    Returns
    -------
    np.array matrix

    """

    membership = np.asarray(membership)
    N  = len(membership)
    mx = np.random.rand(N,N) > (1-inter_community_p)
    for comm in set(membership):
        ixs = np.flatnonzero(membership == comm)
        comm_size = len(ixs)
        for r in ixs:
            mx[r,ixs] = np.random.rand(comm_size) > (1-intra_community_p)
    np.fill_diagonal(mx, 0)
    return mx

def get_barbell_matrix(membership, num_conns=1):
    """Get a matrix corresponding to completely-connected communities
    connected by paths.

    For example:

    ..plot::
    
    >>> from graphy import graphgen
    >>> cmx = graphgen.get_barbell_matrix([0,0,0,0,0,1,1,1,1,1])
    >>> import matplotlib.pylab as plt
    >>> plt.imshow(cmx, interpolation='none') # doctest: +SKIP


    Parameters
    ----------
    membership : list or np.array of ints
        Array containing assignment of each node to communities
    num_conns : int
        Number of links that should run between communities 

    Returns
    -------
    np.array matrix

    """

    membership = np.asarray(membership)
    comms = list(set(membership))
    N  = len(membership)
    mx = np.zeros(shape=(N,N))
    for ndx1, comm in enumerate(comms):
        ixs = np.flatnonzero(membership == comm)
        for r in ixs:
{'fraction_non_alphanumeric': 0.11949929023099755, 'fraction_numerical': 0.01574396696347916, 'mean_word_length': 2.16068515497553, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3260837', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2389, 'n_words': 695}