예제 #1
0
 def test_no_self_loops(self):
     """Tests for forbidding self-loops."""
     n = 10
     k = 3
     alpha = 1
     G = random_k_out_graph(n, k, alpha, self_loops=False)
     assert_equal(nx.number_of_selfloops(G), 0)
예제 #2
0
def to_pydot_subgraph(N: nx.DiGraph, cluster_id: int) -> pydot.Subgraph:
    """from

    https://github.com/networkx/networkx/blob/networkx-2.3/networkx/drawing/nx_pydot.py#L174
    with a 'subgraph' graph_type
    """
    graph_defaults = N.graph.get("graph", {})
    strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()

    P = pydot.Subgraph("cluster{}".format(cluster_id),
                       strict=strict,
                       **graph_defaults)
    try:
        P.set_node_defaults(**N.graph["node"])
    except KeyError:
        pass
    try:
        P.set_edge_defaults(**N.graph["edge"])
    except KeyError:
        pass

    for n, nodedata in N.nodes(data=True):
        str_nodedata = dict((k, make_str(v)) for k, v in nodedata.items())
        p = pydot.Node(make_str(n), **str_nodedata)
        P.add_node(p)

    assert not N.is_multigraph()
    for u, v, edgedata in N.edges(data=True):
        str_edgedata = dict((k, make_str(v)) for k, v in edgedata.items())
        edge = pydot.Edge(make_str(u), make_str(v), **str_edgedata)
        P.add_edge(edge)

    return P
예제 #3
0
 def test_no_self_loops(self):
     """Tests for forbidding self-loops."""
     n = 10
     k = 3
     G = random_uniform_k_out_graph(n, k, self_loops=False)
     assert_equal(nx.number_of_selfloops(G), 0)
     assert_true(all(d == k for v, d in G.out_degree()))
def preprocess_Gnutella08():
    # Directed graph (each unordered pair of nodes is saved once): p2p-Gnutella08.txt
    # Directed Gnutella P2P network from August 8 2002
    # Nodes: 6301 Edges: 20777
    f0 = open('p2p-Gnutella08.txt')  # 打开图文件
    temp = [x.strip().split('\t') for x in f0.readlines()]  # 创建边元组的列表
    f0.close()  # 关闭文件
    DG = nx.DiGraph()
    DG.add_edges_from(temp)
    del temp  # 删除中间变量

    nodes_number = DG.number_of_nodes()
    edges_number = DG.number_of_edges()
    print('节点数量为:', nodes_number, '边总数为:', edges_number)
    print('图中的自环个数为', nx.number_of_selfloops(DG))  # 没有自环出现

    # 检查划分后的各个社区所包含的节点数量
    f0 = open('Gnutella08_communities.txt')  # 第一列是节点,第二列是节点所属的社区标号
    temp = [x.strip().split('\t') for x in f0.readlines()]
    f0.close()  # 关闭文件
    for i in temp:
        node_label_temp = i[0]
        community_label = i[1]
        DG.nodes[node_label_temp]['community_label'] = community_label

    all_community_label = set([x[1] for x in temp])  # 所有的社区标签
    community_dict = dict.fromkeys(list(all_community_label),
                                   0)  # 字典初始化,键为社区标签,值为该社区的总节点数
    for i in temp:
        community_dict[i[1]] += 1
    DG.graph[
        'community'] = community_dict  # 图的'community'属性存储了所有的社区标签和对应的社区节点数量
    activating_probability_init(DG)
    nx.write_gexf(DG, 'Gnutella08.gexf')
예제 #5
0
def _find_chordality_breaker(G, s=None, treewidth_bound=sys.maxsize):
    """Given a graph G, starts a max cardinality search
    (starting from s if s is given and from an arbitrary node otherwise)
    trying to find a non-chordal cycle.

    If it does find one, it returns (u,v,w) where u,v,w are the three
    nodes that together with s are involved in the cycle.
    """
    if nx.number_of_selfloops(G) > 0:
        raise nx.NetworkXError("Input graph is not chordal.")
    unnumbered = set(G)
    if s is None:
        s = arbitrary_element(G)
    unnumbered.remove(s)
    numbered = {s}
    current_treewidth = -1
    while unnumbered:  # and current_treewidth <= treewidth_bound:
        v = _max_cardinality_node(G, unnumbered, numbered)
        unnumbered.remove(v)
        numbered.add(v)
        clique_wanna_be = set(G[v]) & numbered
        sg = G.subgraph(clique_wanna_be)
        if _is_complete_graph(sg):
            # The graph seems to be chordal by now. We update the treewidth
            current_treewidth = max(current_treewidth, len(clique_wanna_be))
            if current_treewidth > treewidth_bound:
                raise nx.NetworkXTreewidthBoundExceeded(
                    f"treewidth_bound exceeded: {current_treewidth}"
                )
        else:
            # sg is not a clique,
            # look for an edge that is not included in sg
            (u, w) = _find_missing_edge(sg)
            return (u, v, w)
    return ()
def get_network_characteristics(given_network, title):
    print(title)
    print('Number of distinct nodes: ', nx.number_of_nodes(given_network))
    print('Number of nodes with a self-loop: ',
          nx.number_of_selfloops(given_network))
    print('Number of undirected edges in the network: ',
          nx.number_of_edges(given_network))
    print('The min and max node degree: %d and %d' %
          (min(dict(given_network.degree).items(),
               key=operator.itemgetter(1))[1],
           max(dict(given_network.degree).items(),
               key=operator.itemgetter(1))[1]))
    print('The average degree: %f' %
          (sum(dict(given_network.degree).values()) /
           len(dict(given_network.degree))))
    try:
        print('Network diameter: ',
              nx.algorithms.distance_measures.diameter(given_network))
    except nx.exception.NetworkXError:
        print(
            'Found infinite path length because the digraph is not strongly connected'
        )
        print('I will find diameter for the largest connected subgraph')
        longest_connected_subgraph = max(
            nx.connected_components(given_network), key=len)
        new_smaller_graph = given_network.subgraph(
            list(longest_connected_subgraph))
        print('Network diameter: ',
              nx.algorithms.distance_measures.diameter(new_smaller_graph))
    plot_degrees(given_network, title)
    print('\n')
예제 #7
0
    def __init__(self, G: Graph, pos: POSITIONS = None):

        if nx.number_of_selfloops(G) != 0:
            raise OrthogonalException(
                'There can be no self loops in the graph')

        if nx.is_connected(G) is False:
            raise OrthogonalException(
                'The graph or parts of it are not connected.')

        self.logger: Logger = getLogger(__name__)
        if pos is None:
            is_planar, self.embedding = nx.check_planarity(G)
            assert is_planar
            pos = nx.combinatorial_embedding_to_pos(self.embedding)
        else:
            if self.numberOfCrossings(G, pos) != 0:
                raise OrthogonalException(
                    'The graph has edges that cross each other')
            self.embedding: nx.PlanarEmbedding = self.convert_pos_to_embedding(
                G, pos)

        self.G: Graph = G.copy()
        self.pos = pos  # is only used to find the ext_face now.
        self.dcel: Dcel = Dcel(G, self.embedding)
        self.ext_face = self.get_external_face()
예제 #8
0
def _make_translation_unit_ast_from_str(file_ast: FileAst,
                                        add_preamble: bool = True
                                        ) -> TranslationUnitAst:
    from tools.import_discovery import build_file_dependency_graph
    from sleepy.errors import CompilerError
    import networkx as nx
    dependency_graph = build_file_dependency_graph(root_ast=file_ast)
    if nx.number_of_selfloops(dependency_graph) > 0:
        raise CompilerError(
            f"Import error: Imports are cyclic. File {nx.nodes_with_selfloops(dependency_graph)} imports itself"
        )
    if not nx.is_directed_acyclic_graph(dependency_graph):
        sccs = [
            scc for scc in nx.strongly_connected_components(dependency_graph)
            if len(scc) > 1
        ]
        raise CompilerError(
            f"Import error: Imports are cyclic. The following cycles were found:\n{sccs}"
        )

    file_asts = [
        dependency_graph.nodes[node]["file_ast"]
        for node in nx.topological_sort(dependency_graph.reverse())
    ]
    if add_preamble:
        file_asts.insert(0, make_preamble_ast())
    return TranslationUnitAst.from_file_asts(file_asts)
예제 #9
0
 def test_no_self_loops(self):
     """Tests for forbidding self-loops."""
     n = 10
     k = 3
     G = random_uniform_k_out_graph(n, k, self_loops=False)
     assert nx.number_of_selfloops(G) == 0
     assert all(d == k for v, d in G.out_degree())
예제 #10
0
 def test_no_self_loops(self):
     """Tests for forbidding self-loops."""
     n = 10
     k = 3
     alpha = 1
     G = random_k_out_graph(n, k, alpha, self_loops=False)
     assert nx.number_of_selfloops(G) == 0
예제 #11
0
def test_selfloops(graph_type):
    G = nx.complete_graph(3, create_using=graph_type)
    G.add_edge(0, 0)
    assert nodes_equal(nx.nodes_with_selfloops(G), [0])
    assert edges_equal(nx.selfloop_edges(G), [(0, 0)])
    assert edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})])
    assert nx.number_of_selfloops(G) == 1
예제 #12
0
def get_network_info(city_str, verbose=False, draw=False, downloaded=False):
    if verbose == True:
        print("city:", city_str)
    start = datetime.now()
    #First we must get the graph from Open Street Maps. Either we already have
    # it on our system or we need to run getdata script:
    if downloaded == True:
        pass
    else:
        graph = getdata.get_graph(city_str)
    #Maybe there was a problem in finding the graph, in which case this city
    # is not good and we have to do something else with it.
    if graph == None:
        if verbose == True:
            print("We couldn't find a graph for this city.")
        return None, None, None, None, None
    if draw == True:
        ox.plot_graph(graph)
    if verbose == True:
        print("Took", datetime.now()-start, "seconds to get the graph")
    n = graph.order()
    m = graph.size()
    sl = nx.number_of_selfloops(graph)
    #Now we prepare the graph for our data through removing multiple edges
    # and self-loops:
    new_graph = nx.Graph(graph)
    new_graph.remove_edges_from(nx.selfloop_edges(new_graph))
    #We collect the new number of edges and the motif vector:
    m_simp = new_graph.size()
    motif_vector = mcount.get_motifvector(new_graph)
    if verbose == True:
        print("Took", datetime.now()-start, "seconds for everything")
    return n, m, m_simp, sl, motif_vector
예제 #13
0
def print_main_graph_attrs(graph):
	print("\n====== Total elements ======")
	print("# nodes: ",nx.number_of_nodes(graph)) 
	print("# edges: ",nx.number_of_edges(graph)) 
	if not nx.is_directed(graph):
		print("# connected components: ",nx.number_connected_components(graph)) 
	print("# self-loops: ",nx.number_of_selfloops(graph)) 
예제 #14
0
def calculate_stats(input_graphml, output_file):
    G = nx.read_graphml(input_graphml)
    G_nodes = G.nodes()
    no_nodes = nx.number_of_nodes(G)
    G_edges = G.edges()

    no_edges_try = nx.number_of_edges(G)
    nodes_str = str(no_nodes)
    edges_str = str(no_edges_try)

    self_loops = nx.number_of_selfloops(G)
    connected_comp = no_nodes - self_loops
    connected_comp_str = str(connected_comp)

    no_ann_nodes = nx.get_node_attributes(G, 'Compound_Name')
    len_no_ann_nodes = len(no_ann_nodes)
    no_ann_nodes_str = str(len_no_ann_nodes)

    with open(output_file, 'w', encoding='utf-8') as f:
        f.write("number of nodes=")
        f.write(nodes_str)
        f.write("\n")
        f.write("number of edges=")
        f.write(edges_str)
        f.write("\n")
        f.write("number of connected components=")
        f.write(connected_comp_str)
        f.write("\n")
        f.write("number of annotated nodes=")
        f.write(no_ann_nodes_str)
예제 #15
0
def is_tournament(G):
    """Returns True if and only if `G` is a tournament.

    A tournament is a directed graph, with neither self-loops nor
    multi-edges, in which there is exactly one directed edge joining
    each pair of distinct nodes.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    bool
        Whether the given graph is a tournament graph.

    Notes
    -----
    Some definitions require a self-loop on each node, but that is not
    the convention used here.

    """
    # In a tournament, there is exactly one directed edge joining each pair.
    return (all((v in G[u]) ^ (u in G[v]) for u, v in combinations(G, 2)) and
            nx.number_of_selfloops(G) == 0)
예제 #16
0
def is_tournament(G):
    """Returns True if and only if `G` is a tournament.

    A tournament is a directed graph, with neither self-loops nor
    multi-edges, in which there is exactly one directed edge joining
    each pair of distinct nodes.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    bool
        Whether the given graph is a tournament graph.

    Notes
    -----
    Some definitions require a self-loop on each node, but that is not
    the convention used here.

    """
    # In a tournament, there is exactly one directed edge joining each pair.
    return (all((v in G[u]) ^ (u in G[v]) for u, v in combinations(G, 2))
            and nx.number_of_selfloops(G) == 0)
예제 #17
0
def derives_networkx_graph_statistics(graph) -> str:
    """Derives statistics from an input knowledge graph and prints them to the console. Note that we are not
    converting each node to a string before deriving our counts. This is purposeful as the number of unique nodes is
    altered when you it converted to a string. For example, in the HPO when honoring the RDF type of each node there are
    406,717 unique nodes versus 406,331 unique nodes when ignoring the RDF type of each node.

    Args:
        graph: An networkx.MultiDiGraph object.

    Returns:
        stats: A formatted string containing descriptive statistics.
    """

    # derive statistics
    nx_graph_und = graph.to_undirected()
    nodes = networkx.number_of_nodes(graph); edges = networkx.number_of_edges(graph)
    self_loops = networkx.number_of_selfloops(graph)
    ce = sorted(Counter([str(x[2]) for x in graph.edges(keys=True)]).items(),  # type: ignore
                key=lambda x: x[1], reverse=1)[:6]  # type: ignore
    avg_degree = float(edges) / nodes
    n_deg = sorted([(str(x[0]), x[1]) for x in graph.degree()], key=lambda x: x[1], reverse=1)[:6]  # type: ignore
    density = networkx.density(graph)
    components = sorted(list(networkx.connected_components(nx_graph_und)), key=len, reverse=True)
    cc_sizes = {x: len(components[x]) for x in range(len(components))}
    x = '{} nodes, {} edges, {} self-loops, 5 most most common edges: {}, average degree {}, 5 highest degree '\
        'nodes: {}, density: {}, {} component(s) and size(s): {}'
    stats = 'Graph Stats: ' + x.format(nodes, edges, self_loops, ', '.join([x[0] + ':' + str(x[1]) for x in ce]),
                                       avg_degree, ', '.join([x[0] + ':' + str(x[1]) for x in n_deg]),
                                       density, len(components), cc_sizes)

    return stats
예제 #18
0
    def _get_stats(self):
        degree_sequence = sorted([d for n, d in self.G.degree()], reverse=True)
        degree_count = Counter(degree_sequence)

        with open(
                os.path.join(self.path_persistent, self.prefix + "-stats.txt"),
                "w") as fp:
            self._print(
                "Number of nodes in the graph: {}\n".format(
                    self.G.number_of_nodes()), fp)
            self._print(
                "Number of edges in the graph: {}\n".format(
                    self.G.number_of_edges()), fp)
            self._print(
                "The graph is connected: {}\n".format(nx.is_connected(self.G)),
                fp)
            self._print(
                "Number of connected components: {}\n".format(
                    nx.number_connected_components(self.G)), fp)
            self._print(
                "Number of self-loops: {}\n".format(
                    nx.number_of_selfloops(self.G)), fp)
            self._print("Maximum degree: {}\n".format(max(degree_count)), fp)
            self._print("Minimum degree: {}\n".format(min(degree_count)), fp)
            self._print(
                "Average degree: {}\n".format(
                    sum(degree_sequence) / len(self.G)), fp)
예제 #19
0
def info(G):
    """
  Compute and print out standard statistics of undirected multigraph G.
  """
    tic = time()
    print("{0:>15s} | '{1:s}'".format('Graph', G.name.replace('_', '-')))
    multi = False
    for edge in G.edges():
        if G.number_of_edges(edge[0], edge[1]) > 1:
            multi = True
            break
    print("{0:>15s} | '{1:s}'".format('Type', '===' if multi else '---'))
    print("{0:>15s} | {1:,d} ({2:,d})".format('Nodes', G.number_of_nodes(),
                                              nx.number_of_isolates(G)))
    print("{0:>15s} | {1:,d} ({2:,d})".format('Edges', G.number_of_edges(),
                                              nx.number_of_selfloops(G)))
    ks = [k for _, k in G.degree()]
    print("{0:>15s} | {1:.1f} ({2:,d}, {3:,d})".format(
        'Degree', 2.0 * G.number_of_edges() / G.number_of_nodes(), min(ks),
        max(ks)))
    print("{0:>15s} | {1:.8f}".format(
        'Density', 2.0 * G.number_of_edges() / G.number_of_nodes() /
        (G.number_of_nodes() - 1.0)))
    CCs = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)
    print("{0:>15s} | {1:.1f}% ({2:,d})".format(
        'Components', 100.0 * CCs[0].number_of_nodes() / G.number_of_nodes(),
        len(CCs)))
    d, D = dists(CCs[0])
    print("{0:>15s} | {1:.3f} ({2:,d})".format('Distances', d, D))
    print("{0:>15s} | {1:.6f}".format('Clustering',
                                      nx.average_clustering(nx.Graph(G))))
    print("{0:>15s} | {1:.1f} sec\n".format('Time', time() - tic))
def pajek_to_files(name, url, pajek_lines, dir_name):
    if pajek_lines:
        try:
            G = nx.parse_pajek(pajek_lines)
            if not nx.is_empty(G):
                old_attributes = list(G.nodes)
                G = nx.convert_node_labels_to_integers(G)
                id_mapping = []
                node_list = list(G.nodes)
                for i in range(len(node_list)):
                    id_mapping.append([old_attributes[i], str(node_list[i])])
                mapping_file = open('..' + dir_name +
                                    '/node_id_mappings/mapping_' +
                                    url.split('/')[-1] + '.csv',
                                    'w',
                                    newline='')
                mapping_file_writer = csv.writer(mapping_file)
                mapping_file_writer.writerow(['id', 'name'])
                for tup in id_mapping:
                    mapping_file_writer.writerow(list(tup))
                nx.write_edgelist(G,
                                  '..' + dir_name + '/edge_lists/' +
                                  url.split('/')[-1] + '.csv',
                                  delimiter=',')
                insert_into_db(
                    name, url,
                    dir_name + '/edge_lists/' + url.split('/')[-1] + '.csv',
                    dir_name + '/node_id_mappings/mapping_' +
                    url.split('/')[-1] + '.csv', G.is_directed(),
                    G.is_multigraph(), int(G.number_of_nodes()),
                    int(nx.number_of_selfloops(G)))
        except Exception as e:
            traceback.print_exc()
            print(e)
            print("Couldn't parse " + url)
예제 #21
0
def load_dataset(csv_file):
    df_edges = pd.read_csv(csv_file)
    G = nx.Graph()
    for row in tqdm(df_edges.iterrows()):
        row = row[1]
        G.add_edge(np.uint16(row["id_1"]), np.uint16(row["id_2"]))
    print("# of self loops: ", nx.number_of_selfloops(G))
    print("# nodes: ", len(G))
    return G
예제 #22
0
 def save_stats_txt(self, save_results=False):
     if save_results:
         print("Name: " + Walktrap.G.name)
         print("No. of Nodes: " + str(Walktrap.G.number_of_nodes()))
         print("No. of Edges: " + str(Walktrap.G.number_of_edges()))
         print("No. of self-Loops: " +
               str(nx.number_of_selfloops(Walktrap.G)))
         print("\nDegrees:\n" + str(Walktrap.G.degree()))
     else:
         f = open(self.output_path + "stats.txt", "w+")
         f.write("Name: " + Walktrap.G.name)
         f.write("\nNo. of Nodes: " + str(Walktrap.G.number_of_nodes()))
         f.write("\nNo. of Edges: " + str(Walktrap.G.number_of_edges()))
         f.write("\nNo. of self-Loops: " +
                 str(nx.number_of_selfloops(Walktrap.G)))
         f.write("\n\nDegrees:\n" + str(Walktrap.G.degree()))
         f.close()
         print(f"Statistics file saved in: {f.name}")
예제 #23
0
def _is_complete_graph(G):
    """Returns True if G is a complete graph."""
    if nx.number_of_selfloops(G) > 0:
        raise nx.NetworkXError("Self loop found in _is_complete_graph()")
    n = G.number_of_nodes()
    if n < 2:
        return True
    e = G.number_of_edges()
    max_edges = ((n * (n - 1)) / 2)
    return e == max_edges
예제 #24
0
파일: chordal.py 프로젝트: zcf900/networkx
def _is_complete_graph(G):
    """Returns True if G is a complete graph."""
    if nx.number_of_selfloops(G) > 0:
        raise nx.NetworkXError("Self loop found in _is_complete_graph()")
    n = G.number_of_nodes()
    if n < 2:
        return True
    e = G.number_of_edges()
    max_edges = (n * (n - 1)) / 2
    return e == max_edges
예제 #25
0
def without_selfloops(G):
    """return copy of G without selfloop edges"""
    H = G.copy()
    num_loops = nx.number_of_selfloops(G)

    if num_loops:
        log.warning("Network contains {} self-loops. "
                    "Removing...".format(num_loops))
        H.remove_edges_from(nx.selfloop_edges(G))

    return H
예제 #26
0
def without_selfloops(G):
    """return copy of G without selfloop edges"""
    H = G.copy()
    num_loops = nx.number_of_selfloops(G)

    if num_loops:
        log.warning("Network contains {} self-loops. "
                    "Removing...".format(num_loops))
        H.remove_edges_from(nx.selfloop_edges(G))

    return H
예제 #27
0
    def __init__(self, definition_str: str):
        """
        Initializes the network using the comma separated list of nodes and weights
        :param definition_str: a string like "AB2, BC4, AC1"
        """
        self.graph = nx.DiGraph()
        self.graph.add_weighted_edges_from(
            self._parse_definition_str(definition_str))
        self.duplicate_graph = self._clone_with_duplicate_nodes()

        if nx.number_of_selfloops(self.graph) > 0:
            raise GraphException("loops are not allowed")
예제 #28
0
def to_agraph(N):
    """Return a pygraphviz graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)

    Notes
    -----
    If N has an dict N.graph_attr an attempt will be made first
    to copy properties attached to the graph (see from_agraph)
    and then updated with the calling arguments if any.

    """
    try:
        import pygraphviz
    except ImportError:
        raise ImportError('requires pygraphviz ',
                          'http://pygraphviz.github.io/')
    directed = N.is_directed()
    strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()
    A = pygraphviz.AGraph(name=N.name, strict=strict, directed=directed)

    # default graph attributes
    A.graph_attr.update(N.graph.get('graph', {}))
    A.node_attr.update(N.graph.get('node', {}))
    A.edge_attr.update(N.graph.get('edge', {}))

    A.graph_attr.update(N.graph)

    # add nodes
    for n, nodedata in N.nodes(data=True):
        A.add_node(n, **nodedata)

    # loop over edges

    if N.is_multigraph():
        for u, v, key, edgedata in N.edges(data=True, keys=True):
            str_edata = {k: str(v) for k, v in edgedata.items() if k != 'key'}
            A.add_edge(u, v, key=str(key), **str_edata)
    else:
        for u, v, edgedata in N.edges(data=True):
            str_edgedata = {k: str(v) for k, v in edgedata.items()}
            A.add_edge(u, v, **str_edgedata)

    return A
예제 #29
0
def handle_mtx(path, name, simplify=False):
    """
    Handle a file that is in Matrix Market matrix format (http://math.nist.gov/MatrixMarket/formats.html#MMformat)

    We only load graphs that have the format '%%MatrixMarket matrix coordinate pattern symmetric'
    (first line in the file). I.e. unweighted and symmetric/undirected

    :param path: Path to the unzipped graph data
    :param name: Filename of the .mtx-file
    :param simplify: (Optional) Simplify the graph
    :return: A graph or None on error
    :rtype: :class:`networkx.Graph` | None
    """
    filepath = os.path.join(path, name)

    # Some data has wrong format: The first line starts with '%MatrixMarket' instead of '%%MatrixMarket'
    # -> Fix this in advance
    _fix_mtx(filepath)

    try:
        rows, cols, entries, matrix_format, field, symm = scipy.io.mminfo(filepath)
    except ValueError as e:
        logging.error('{}: {}'.format(name, e))
        return None

    if not simplify and matrix_format == 'coordinate' and field == 'pattern' and symm == 'symmetric':
        matrix = scipy.io.mmread(filepath)

        g = nx.Graph(matrix, name=name)
        del matrix
        if nx.number_of_selfloops(g):
            logging.error('{}: Graph has loops'.format(name))
            return None

        return g
    elif simplify and matrix_format == 'coordinate' and field in ('pattern', 'real', 'integer', 'complex'):
        logging.info('{} has format ({}, {}, {}). Simplify.'.format(name, matrix_format, field, symm))
        matrix = scipy.io.mmread(filepath)  # Scipy (sparse) matrix

        if matrix.shape[0] != matrix.shape[1]:
            logging.error('{}: Data matrix is not symmetric {})'.format(name, matrix.shape))
            return None

        # SimpleGraph is undirected, neglects edge weights and removes loops
        g = SimpleGraph(matrix, name=name)
        del matrix

        return g
    else:
        logging.info('{} is of the wrong format ({}, {}, {}). Skip.'.format(name, matrix_format, field, symm))
        return None
예제 #30
0
 def test_selfloops(self):
     G = self.K3.copy()
     G.add_edge(0, 0)
     assert_nodes_equal(nx.nodes_with_selfloops(G), [0])
     assert_edges_equal(nx.selfloop_edges(G), [(0, 0)])
     assert_equal(nx.number_of_selfloops(G), 1)
     G.remove_edge(0, 0)
     G.add_edge(0, 0)
     G.remove_edges_from([(0, 0)])
     G.add_edge(1, 1)
     G.remove_node(1)
     G.add_edge(0, 0)
     G.add_edge(1, 1)
     G.remove_nodes_from([0, 1])
예제 #31
0
def graph_test_train_split_folds(G, nfolds):
    print('Using folds')
    self_loop_edges = nx.selfloop_edges(G)
    print('Removing %d edges because of self loops' %
          nx.number_of_selfloops(G))
    G.remove_edges_from(self_loop_edges)

    edges = np.array(G.edges())
    neg_G = nx.complement(G)
    kf = KFold(n_splits=nfolds, shuffle=True)
    fgpt_attr = nx.get_node_attributes(G, name='fingerprint')
    edge_attr = nx.get_edge_attributes(G, name='edge_attr')
    for i, (train_idx, test_idx) in enumerate(kf.split(edges)):
        test_G = nx.Graph()
        test_G.add_edges_from(edges[test_idx])
        test_G.name = 'test_G_%d' % i

        train_G = nx.Graph()
        train_G.add_edges_from(edges[train_idx])
        train_G.name = 'train_G_%d' % i
        remove_nodes = [n for n, d in G.degree() if d == 0]
        print('Removing %d nodes from train_G due to no neighbors' %
              len(remove_nodes))
        train_G.remove_nodes_from(remove_nodes)

        nx.set_node_attributes(train_G, values=fgpt_attr, name='fingerprint')
        nx.set_node_attributes(test_G, values=fgpt_attr, name='fingerprint')

        nx.set_edge_attributes(train_G, values=edge_attr, name='edge_attr')
        nx.set_edge_attributes(test_G, values=edge_attr, name='edge_attr')

        nodelist = train_G.nodes()
        nodelistMap = dict(zip(nodelist, range(len(nodelist))))

        train_G = nx.relabel_nodes(train_G, nodelistMap)

        test_G = test_G.subgraph(nodelist).copy()
        test_G = nx.relabel_nodes(test_G, nodelistMap)

        molecule_names = {v: k for k, v in nodelistMap.items()}
        nx.set_node_attributes(train_G, values=molecule_names, name='molecule')
        nx.set_node_attributes(test_G, values=molecule_names, name='molecule')

        neg_G_ = neg_G.subgraph(nodelist).copy()
        neg_G_ = nx.relabel_nodes(neg_G_, nodelistMap)

        print(nx.info(train_G))
        print(nx.info(test_G))

        yield {'train_G': train_G, 'test_G': test_G, 'neg_G': neg_G_}
예제 #32
0
def test_selfloops():
    graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
    for graph in graphs:
        G = nx.complete_graph(3, create_using=graph)
        G.add_edge(0, 0)
        assert_nodes_equal(nx.nodes_with_selfloops(G), [0])
        assert_edges_equal(nx.selfloop_edges(G), [(0, 0)])
        assert_edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})])
        assert_equal(nx.number_of_selfloops(G), 1)
        # test selfloop attr
        G.add_edge(1, 1, weight=2)
        assert_edges_equal(nx.selfloop_edges(G, data=True),
                           [(0, 0, {}), (1, 1, {'weight': 2})])
        assert_edges_equal(nx.selfloop_edges(G, data='weight'),
                           [(0, 0, None), (1, 1, 2)])
예제 #33
0
def read_graph(graph_f=None):
    if graph_f.endswith(".pkl"):
        G = nx.read_gpickle(graph_f)
    else:
        G = nx.read_edgelist(graph_f)
    print('Original G from', graph_f)
    print('Removing %d edges because of self loops' % nx.number_of_selfloops(G))
    self_loop_edges = nx.selfloop_edges(G)
    G.remove_edges_from(self_loop_edges)
    G.name = 'G'
    print(nx.info(G))
    G = load_fingerprints(G, 'pubchem')
    G = load_fingerprints(G, 'maccs')
    print(nx.info(G))
    return G
예제 #34
0
def print_graph_detail(graph):
    """
    格式化显示Graph参数
    :param graph:
    :return:
    """
    import networkx as nx
    dst = {
        "nodes": nx.number_of_nodes(graph),
        "edges": nx.number_of_edges(graph),
        "selfloops": nx.number_of_selfloops(graph),
        "isolates": nx.number_of_isolates(graph),
        "覆盖度": 1 - nx.number_of_isolates(graph) / nx.number_of_nodes(graph),
    }
    print_table(dst)
예제 #35
0
def test_selfloops():
    graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
    for graph in graphs:
        G = nx.complete_graph(3, create_using=graph)
        G.add_edge(0, 0)
        assert_nodes_equal(nx.nodes_with_selfloops(G), [0])
        assert_edges_equal(nx.selfloop_edges(G), [(0, 0)])
        assert_edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})])
        assert nx.number_of_selfloops(G) == 1
        # test selfloop attr
        G.add_edge(1, 1, weight=2)
        assert_edges_equal(nx.selfloop_edges(G, data=True),
                           [(0, 0, {}), (1, 1, {'weight': 2})])
        assert_edges_equal(nx.selfloop_edges(G, data='weight'),
                           [(0, 0, None), (1, 1, 2)])
    def __init__(self, G, pos=None):
        assert nx.number_of_selfloops(G) == 0
        assert nx.is_connected(G)
        if pos is None:
            is_planar, self.embedding = nx.check_planarity(G)
            assert is_planar
            pos = nx.combinatorial_embedding_to_pos(self.embedding)
        else:
            assert number_of_cross(G, pos) == 0
            self.embedding = convert_pos_to_embdeding(G, pos)

        self.G = G.copy()
        self.pos = pos # is only used to find the ext_face now.
        self.dcel = DCEL.Dcel(G, self.embedding)
        self.ext_face = self.get_external_face()
예제 #37
0
파일: core.py 프로젝트: networkx/networkx
def core_number(G):
    """Returns the core number for each vertex.

    A k-core is a maximal subgraph that contains nodes of degree k or more.

    The core number of a node is the largest value k of a k-core containing
    that node.

    Parameters
    ----------
    G : NetworkX graph
       A graph or directed graph

    Returns
    -------
    core_number : dictionary
       A dictionary keyed by node to the core number.

    Raises
    ------
    NetworkXError
        The k-core is not implemented for graphs with self loops
        or parallel edges.

    Notes
    -----
    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    References
    ----------
    .. [1] An O(m) Algorithm for Cores Decomposition of Networks
       Vladimir Batagelj and Matjaz Zaversnik, 2003.
       https://arxiv.org/abs/cs.DS/0310049
    """
    if nx.number_of_selfloops(G) > 0:
        msg = ('Input graph has self loops which is not permitted; '
               'Consider using G.remove_edges_from(nx.selfloop_edges(G)).')
        raise NetworkXError(msg)
    degrees = dict(G.degree())
    # Sort nodes by degree.
    nodes = sorted(degrees, key=degrees.get)
    bin_boundaries = [0]
    curr_degree = 0
    for i, v in enumerate(nodes):
        if degrees[v] > curr_degree:
            bin_boundaries.extend([i] * (degrees[v] - curr_degree))
            curr_degree = degrees[v]
    node_pos = {v: pos for pos, v in enumerate(nodes)}
    # The initial guess for the core number of a node is its degree.
    core = degrees
    nbrs = {v: list(nx.all_neighbors(G, v)) for v in G}
    for v in nodes:
        for u in nbrs[v]:
            if core[u] > core[v]:
                nbrs[u].remove(v)
                pos = node_pos[u]
                bin_start = bin_boundaries[core[u]]
                node_pos[u] = bin_start
                node_pos[nodes[bin_start]] = pos
                nodes[bin_start], nodes[pos] = nodes[pos], nodes[bin_start]
                bin_boundaries[core[u]] += 1
                core[u] -= 1
    return core
예제 #38
0
def to_agraph(N):
    """Return a pygraphviz graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)

    Notes
    -----
    If N has an dict N.graph_attr an attempt will be made first
    to copy properties attached to the graph (see from_agraph)
    and then updated with the calling arguments if any.

    """
    try:
        import pygraphviz
    except ImportError:
        raise ImportError('requires pygraphviz ',
                          'http://pygraphviz.github.io/')
    directed = N.is_directed()
    strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()
    A = pygraphviz.AGraph(name=N.name, strict=strict, directed=directed)

    # default graph attributes
    A.graph_attr.update(N.graph.get('graph', {}))
    A.node_attr.update(N.graph.get('node', {}))
    A.edge_attr.update(N.graph.get('edge', {}))

    A.graph_attr.update((k, v) for k, v in N.graph.items()
                        if k not in ('graph', 'node', 'edge'))

    # add nodes
    for n, nodedata in N.nodes(data=True):
        A.add_node(n)
        if nodedata is not None:
            a = A.get_node(n)
            a.attr.update({k: str(v) for k, v in nodedata.items()})

    # loop over edges
    if N.is_multigraph():
        for u, v, key, edgedata in N.edges(data=True, keys=True):
            str_edgedata = {k: str(v) for k, v in edgedata.items()
                            if k != 'key'}
            A.add_edge(u, v, key=str(key))
            if edgedata is not None:
                a = A.get_edge(u, v)
                a.attr.update(str_edgedata)

    else:
        for u, v, edgedata in N.edges(data=True):
            str_edgedata = {k: str(v) for k, v in edgedata.items()}
            A.add_edge(u, v)
            if edgedata is not None:
                a = A.get_edge(u, v)
                a.attr.update(str_edgedata)

    return A
예제 #39
0
def to_pydot(N):
    """Return a pydot graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> P = nx.nx_pydot.to_pydot(K5)

    Notes
    -----

    """
    pydot = _import_pydot()

    # set Graphviz graph type
    if N.is_directed():
        graph_type = 'digraph'
    else:
        graph_type = 'graph'
    strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()

    name = N.name
    graph_defaults = N.graph.get('graph', {})
    if name is '':
        P = pydot.Dot('', graph_type=graph_type, strict=strict,
                      **graph_defaults)
    else:
        P = pydot.Dot('"%s"' % name, graph_type=graph_type, strict=strict,
                      **graph_defaults)
    try:
        P.set_node_defaults(**N.graph['node'])
    except KeyError:
        pass
    try:
        P.set_edge_defaults(**N.graph['edge'])
    except KeyError:
        pass

    for n, nodedata in N.nodes(data=True):
        str_nodedata = dict((k, make_str(v)) for k, v in nodedata.items())
        p = pydot.Node(make_str(n), **str_nodedata)
        P.add_node(p)

    if N.is_multigraph():
        for u, v, key, edgedata in N.edges(data=True, keys=True):
            str_edgedata = dict((k, make_str(v)) for k, v in edgedata.items()
                                if k != 'key')
            edge = pydot.Edge(make_str(u), make_str(v),
                              key=make_str(key), **str_edgedata)
            P.add_edge(edge)

    else:
        for u, v, edgedata in N.edges(data=True):
            str_edgedata = dict((k, make_str(v)) for k, v in edgedata.items())
            edge = pydot.Edge(make_str(u), make_str(v), **str_edgedata)
            P.add_edge(edge)
    return P
예제 #40
0
def rich_club_coefficient(G, normalized=True, Q=100, seed=None):
    r"""Returns the rich-club coefficient of the graph `G`.

    For each degree *k*, the *rich-club coefficient* is the ratio of the
    number of actual to the number of potential edges for nodes with
    degree greater than *k*:

    .. math::

        \phi(k) = \frac{2 E_k}{N_k (N_k - 1)}

    where `N_k` is the number of nodes with degree larger than *k*, and
    `E_k` is the number of edges among those nodes.

    Parameters
    ----------
    G : NetworkX graph
        Undirected graph with neither parallel edges nor self-loops.
    normalized : bool (optional)
        Normalize using randomized network as in [1]_
    Q : float (optional, default=100)
        If `normalized` is True, perform `Q * m` double-edge
        swaps, where `m` is the number of edges in `G`, to use as a
        null-model for normalization.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    rc : dictionary
       A dictionary, keyed by degree, with rich-club coefficient values.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)])
    >>> rc = nx.rich_club_coefficient(G, normalized=False)
    >>> rc[0] # doctest: +SKIP
    0.4

    Notes
    -----
    The rich club definition and algorithm are found in [1]_.  This
    algorithm ignores any edge weights and is not defined for directed
    graphs or graphs with parallel edges or self loops.

    Estimates for appropriate values of `Q` are found in [2]_.

    References
    ----------
    .. [1] Julian J. McAuley, Luciano da Fontoura Costa,
       and Tibério S. Caetano,
       "The rich-club phenomenon across complex network hierarchies",
       Applied Physics Letters Vol 91 Issue 8, August 2007.
       https://arxiv.org/abs/physics/0701290
    .. [2] R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon,
       "Uniform generation of random graphs with arbitrary degree
       sequences", 2006. https://arxiv.org/abs/cond-mat/0312028
    """
    if nx.number_of_selfloops(G) > 0:
        raise Exception('rich_club_coefficient is not implemented for '
                        'graphs with self loops.')
    rc = _compute_rc(G)
    if normalized:
        # make R a copy of G, randomize with Q*|E| double edge swaps
        # and use rich_club coefficient of R to normalize
        R = G.copy()
        E = R.number_of_edges()
        nx.double_edge_swap(R, Q * E, max_tries=Q * E * 10, seed=seed)
        rcran = _compute_rc(R)
        rc = {k: v / rcran[k] for k, v in rc.items()}
    return rc