Python networkx.isolates() Examples

The following are 30 code examples of networkx.isolates(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: utils.py    From GraphRNN with MIT License 7 votes vote down vote up
def decode_graph(adj, prefix):
    adj = np.asmatrix(adj)
    G = nx.from_numpy_matrix(adj)
    # G.remove_nodes_from(nx.isolates(G))
    print('num of nodes: {}'.format(G.number_of_nodes()))
    print('num of edges: {}'.format(G.number_of_edges()))
    G_deg = nx.degree_histogram(G)
    G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
    print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
    if nx.is_connected(G):
        print('average path length: {}'.format(nx.average_shortest_path_length(G)))
        print('average diameter: {}'.format(nx.diameter(G)))
    G_cluster = sorted(list(nx.clustering(G).values()))
    print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
    cycle_len = []
    cycle_all = nx.cycle_basis(G, 0)
    for item in cycle_all:
        cycle_len.append(len(item))
    print('cycles', cycle_len)
    print('cycle count', len(cycle_len))
    draw_graph(G, prefix=prefix) 
Example #2
Source File: graph.py    From postman_problems with MIT License 6 votes vote down vote up
def create_required_graph(graph):
    """
    Strip a graph down to just the required nodes and edges.  Used for RPP.  Expected edge attribute "required" with
     True/False or 0/1 values.

    Args:
        graph (networkx MultiGraph):

    Returns:
        networkx MultiGraph with optional nodes and edges deleted
    """

    graph_req = graph.copy()  # preserve original structure

    # remove optional edges
    for e in list(graph_req.edges(data=True, keys=True)):
        if not e[3]['required']:
            graph_req.remove_edge(e[0], e[1], key=e[2])

    # remove any nodes left isolated after optional edges are removed (no required incident edges)
    for n in list(nx.isolates(graph_req)):
        graph_req.remove_node(n)

    return graph_req 
Example #3
Source File: utils.py    From graph-generation with MIT License 6 votes vote down vote up
def decode_graph(adj, prefix):
    adj = np.asmatrix(adj)
    G = nx.from_numpy_matrix(adj)
    # G.remove_nodes_from(nx.isolates(G))
    print('num of nodes: {}'.format(G.number_of_nodes()))
    print('num of edges: {}'.format(G.number_of_edges()))
    G_deg = nx.degree_histogram(G)
    G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
    print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
    if nx.is_connected(G):
        print('average path length: {}'.format(nx.average_shortest_path_length(G)))
        print('average diameter: {}'.format(nx.diameter(G)))
    G_cluster = sorted(list(nx.clustering(G).values()))
    print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
    cycle_len = []
    cycle_all = nx.cycle_basis(G, 0)
    for item in cycle_all:
        cycle_len.append(len(item))
    print('cycles', cycle_len)
    print('cycle count', len(cycle_len))
    draw_graph(G, prefix=prefix) 
Example #4
Source File: isolate.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def number_of_isolates(G):
    """Returns the number of isolates in the graph.

    An *isolate* is a node with no neighbors (that is, with degree
    zero). For directed graphs, this means no in-neighbors and no
    out-neighbors.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    int
        The number of degree zero nodes in the graph `G`.

    """
    # TODO This can be parallelized.
    return sum(1 for v in isolates(G)) 
Example #5
Source File: test_edgelist.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_edgelist_integers(self):
        G=nx.convert_node_labels_to_integers(self.G)
        (fd,fname)=tempfile.mkstemp()
        bipartite.write_edgelist(G,fname)  
        H=bipartite.read_edgelist(fname,nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(nx.isolates(G))
        assert_nodes_equal(H.nodes(),G.nodes())
        assert_edges_equal(H.edges(),G.edges())
        os.close(fd)
        os.unlink(fname) 
Example #6
Source File: test_isolate.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_isolates():
    G = nx.Graph()
    G.add_edge(0, 1)
    G.add_nodes_from([2, 3])
    assert_equal(sorted(nx.isolates(G)), [2, 3]) 
Example #7
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_edgelist_integers(self):
        G=nx.convert_node_labels_to_integers(self.G)
        (fd,fname)=tempfile.mkstemp()
        bipartite.write_edgelist(G,fname)
        H=bipartite.read_edgelist(fname,nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(list(nx.isolates(G)))
        assert_nodes_equal(list(H), list(G))
        assert_edges_equal(list(H.edges()), list(G.edges()))
        os.close(fd)
        os.unlink(fname) 
Example #8
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_edgelist_integers(self):
        G=nx.convert_node_labels_to_integers(self.G)
        (fd,fname)=tempfile.mkstemp()
        nx.write_edgelist(G,fname)  
        H=nx.read_edgelist(fname,nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(list(nx.isolates(G)))
        assert_nodes_equal(list(H), list(G))
        assert_edges_equal(list(H.edges()), list(G.edges()))
        os.close(fd)
        os.unlink(fname) 
Example #9
Source File: test_isolate.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_isolates():
    G = nx.Graph()
    G.add_edge(0, 1)
    G.add_nodes_from([2, 3])
    assert_equal(sorted(nx.isolates(G)), [2, 3]) 
Example #10
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_edgelist_integers(self):
        G = nx.convert_node_labels_to_integers(self.G)
        (fd, fname) = tempfile.mkstemp()
        bipartite.write_edgelist(G, fname)
        H = bipartite.read_edgelist(fname, nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(list(nx.isolates(G)))
        assert_nodes_equal(list(H), list(G))
        assert_edges_equal(list(H.edges()), list(G.edges()))
        os.close(fd)
        os.unlink(fname) 
Example #11
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_edgelist_integers(self):
        G = nx.convert_node_labels_to_integers(self.G)
        (fd, fname) = tempfile.mkstemp()
        nx.write_edgelist(G, fname)
        H = nx.read_edgelist(fname, nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(list(nx.isolates(G)))
        assert_nodes_equal(list(H), list(G))
        assert_edges_equal(list(H.edges()), list(G.edges()))
        os.close(fd)
        os.unlink(fname) 
Example #12
Source File: utils.py    From BioNEV with MIT License 5 votes vote down vote up
def split_train_test_graph(input_edgelist, seed, testing_ratio=0.2, weighted=False):
    
    if (weighted):
        G = nx.read_weighted_edgelist(input_edgelist)
    else:
        G = nx.read_edgelist(input_edgelist)
    node_num1, edge_num1 = len(G.nodes), len(G.edges)
    print('Original Graph: nodes:', node_num1, 'edges:', edge_num1)
    testing_edges_num = int(len(G.edges) * testing_ratio)
    random.seed(seed)
    testing_pos_edges = random.sample(G.edges, testing_edges_num)
    G_train = copy.deepcopy(G)
    for edge in testing_pos_edges:
        node_u, node_v = edge
        if (G_train.degree(node_u) > 1 and G_train.degree(node_v) > 1):
            G_train.remove_edge(node_u, node_v)

    G_train.remove_nodes_from(nx.isolates(G_train))
    node_num2, edge_num2 = len(G_train.nodes), len(G_train.edges)
    assert node_num1 == node_num2
    train_graph_filename = 'graph_train.edgelist'
    if weighted:
        nx.write_edgelist(G_train, train_graph_filename, data=['weight'])
    else:
        nx.write_edgelist(G_train, train_graph_filename, data=False)

    node_num1, edge_num1 = len(G_train.nodes), len(G_train.edges)
    print('Training Graph: nodes:', node_num1, 'edges:', edge_num1)
    return G, G_train, testing_pos_edges, train_graph_filename 
Example #13
Source File: metaknowledgeCLI.py    From metaknowledge with GNU General Public License v2.0 5 votes vote down vote up
def getThresholds(clargs, grph):
    thresDict = collections.OrderedDict([
    ('0', "Continue"),
    ('1', "Drop isolates"),
    ('2', "Remove self loops"),
    ('3', "Remove edges below some weight"),
    ('4', "Remove edges above some weight"),
    ('5', "Remove nodes below some degree"),
    ('6', "Remove nodes above some degree"),
    ])
    print("The network contains {0} nodes and {1} edges, of which {2} are isolated and {3} are self loops.".format(len(list(grph.nodes())), len(list(grph.edges())), len(list(nx.isolates(grph))), len(list(grph.selfloop_edges()))))
    thresID = int(inputMenu(thresDict, header = "What type of filtering to you want? "))
    if thresID == 0:
        return grph
    elif thresID == 1:
        metaknowledge.dropNodesByDegree(grph, minDegree = 1)
        return getThresholds(clargs, grph)
    elif thresID == 2:
        metaknowledge.dropEdges(grph, dropSelfLoops = True)
        return getThresholds(clargs, grph)
    elif thresID == 3:
        metaknowledge.dropEdges(grph, minWeight = getNum("What is the minumum weight for an edge to be included? "))
        return getThresholds(clargs, grph)
    elif thresID == 4:
        metaknowledge.dropEdges(grph, minWeight = getNum("What is the maximum weight for an edge to be included? "))
        return getThresholds(clargs, grph)
    elif thresID == 5:
        metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the minumum degree for an edge to be included? "))
        return getThresholds(clargs, grph)
    else:
        metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the maximum degree for an edge to be included? "))
        return getThresholds(clargs, grph) 
Example #14
Source File: isolate.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def isolates(G):
    """Return list of isolates in the graph.

    Isolates are nodes with no neighbors (degree zero).

    Parameters
    ----------
    G : graph
        A networkx graph

    Returns
    -------
    isolates : list
       List of isolate nodes.
    
    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_edge(1,2)
    >>> G.add_node(3)
    >>> nx.isolates(G)
    [3]

    To remove all isolates in the graph use
    >>> G.remove_nodes_from(nx.isolates(G))
    >>> G.nodes()
    [1, 2]

    For digraphs isolates have zero in-degree and zero out_degre
    >>> G = nx.DiGraph([(0,1),(1,2)])
    >>> G.add_node(3)
    >>> nx.isolates(G)
    [3]
    """
    return [n for (n,d) in G.degree_iter() if d==0] 
Example #15
Source File: link_pred.py    From nodevectors with MIT License 5 votes vote down vote up
def split_train_test_graph(G, testing_ratio=0.5, seed=42):
    node_num1, edge_num1 = len(G.nodes), len(G.edges)
    testing_edges_num = int(len(G.edges) * testing_ratio)
    random.seed(seed)
    testing_pos_edges = random.sample(G.edges, testing_edges_num)
    G_train = copy.deepcopy(G)
    # for edge in testing_pos_edges:
    #     node_u, node_v = edge # unpack edge
    #     if (G_train.degree(node_u) > 1 and G_train.degree(node_v) > 1):
    #         G_train.remove_edge(node_u, node_v)
    G_train.remove_nodes_from(testing_pos_edges)
    G_train.remove_nodes_from(nx.isolates(G_train))
    node_num2, edge_num2 = len(G_train.nodes), len(G_train.edges)
    assert node_num1 == node_num2
    return G_train, testing_pos_edges 
Example #16
Source File: test_edgelist.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_edgelist_integers(self):
        G=nx.convert_node_labels_to_integers(self.G)
        (fd,fname)=tempfile.mkstemp()
        nx.write_edgelist(G,fname)  
        H=nx.read_edgelist(fname,nodetype=int)
        # isolated nodes are not written in edgelist
        G.remove_nodes_from(nx.isolates(G))
        assert_nodes_equal(H.nodes(),G.nodes())
        assert_edges_equal(H.edges(),G.edges())
        os.close(fd)
        os.unlink(fname) 
Example #17
Source File: algorithm.py    From Pixel-Art with GNU General Public License v3.0 5 votes vote down vote up
def get_boundaries(self):
        # Remove internal edges from a copy of our pixgrid graph and just get the boundaries
        self.outlines_graph = networkx.Graph(self.grid_graph)
        for pixel, attrs in self.pixel_graph.nodes(data=True):
            corners = attrs['corners']
            for neighbor in self.pixel_graph.neighbors(pixel):
                edge = corners & self.pixel_graph.nodes[neighbor]['corners']
                if len(edge) != 2:  # If the number of edges is not 2
                    print(edge)
                # Remove the internal edges in the outlines graph
                elif self.outlines_graph.has_edge(*edge):
                    self.outlines_graph.remove_edge(*edge)
        for node in list(networkx.isolates(self.outlines_graph)):
            # Remove the nodes from the outline graph too
            self.outlines_graph.remove_node(node) 
Example #18
Source File: data_loader.py    From Graph-U-Nets with GNU General Public License v3.0 5 votes vote down vote up
def gen_graph(self, f, i, label_dict, feat_dict, deg_as_tag):
        row = next(f).strip().split()
        n, label = [int(w) for w in row]
        if label not in label_dict:
            label_dict[label] = len(label_dict)
        g = nx.Graph()
        g.add_nodes_from(list(range(n)))
        node_tags = []
        for j in range(n):
            row = next(f).strip().split()
            tmp = int(row[1]) + 2
            row = [int(w) for w in row[:tmp]]
            if row[0] not in feat_dict:
                feat_dict[row[0]] = len(feat_dict)
            for k in range(2, len(row)):
                if j != row[k]:
                    g.add_edge(j, row[k])
            if len(row) > 2:
                node_tags.append(feat_dict[row[0]])
        g.label = label
        g.remove_nodes_from(list(nx.isolates(g)))
        if deg_as_tag:
            g.node_tags = list(dict(g.degree).values())
        else:
            g.node_tags = node_tags
        return g 
Example #19
Source File: utils.py    From pybel with MIT License 5 votes vote down vote up
def remove_isolated_nodes(graph):
    """Remove isolated nodes from the network, in place.

    :param pybel.BELGraph graph: A BEL graph
    """
    nodes = list(nx.isolates(graph))
    graph.remove_nodes_from(nodes) 
Example #20
Source File: utils.py    From pybel with MIT License 5 votes vote down vote up
def remove_isolated_nodes_op(graph):
    """Build a new graph excluding the isolated nodes.

    :param pybel.BELGraph graph: A BEL graph
    :rtype: pybel.BELGraph
    """
    rv = graph.copy()
    nodes = list(nx.isolates(rv))
    rv.remove_nodes_from(nodes)
    return rv 
Example #21
Source File: statistical.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def isolates_(self):
        """array-like of shape (n_isolates,): Indices of isolates.
        """

        import networkx as nx

        return np.array(list(nx.isolates(self.graphical_model_))) 
Example #22
Source File: conversion.py    From spektral with MIT License 4 votes vote down vote up
def numpy_to_nx(adj, node_features=None, edge_features=None, nf_name=None,
                ef_name=None):
    """
    Converts graphs in numpy format to a list of nx.Graphs.
    :param adj: adjacency matrices of shape `(num_samples, num_nodes, num_nodes)`.
    If there is only one sample, the first dimension can be dropped.
    :param node_features: optional node attributes matrices of shape `(num_samples, num_nodes, node_features_dim)`.
    If there is only one sample, the first dimension can be dropped.
    :param edge_features: optional edge attributes matrices of shape `(num_samples, num_nodes, num_nodes, edge_features_dim)`
    If there is only one sample, the first dimension can be dropped.
    :param nf_name: optional name to assign to node attributes in the nx.Graphs
    :param ef_name: optional name to assign to edge attributes in the nx.Graphs
    :return: a list of nx.Graphs (or a single nx.Graph is there is only one sample)
    """
    if adj.ndim == 2:
        adj = adj[None, ...]
        if node_features is not None:
            if nf_name is None:
                nf_name = 'node_features'
            node_features = node_features[None, ...]
            if node_features.ndim != 3:
                raise ValueError('node_features must have shape (batch, N, F) '
                                 'or (N, F).')
        if edge_features is not None:
            if ef_name is None:
                ef_name = 'edge_features'
            edge_features = edge_features[None, ...]
            if edge_features.ndim != 4:
                raise ValueError('edge_features must have shape (batch, N, N, S) '
                                 'or (N, N, S).')

    output = []
    for i in range(adj.shape[0]):
        g = nx.from_numpy_array(adj[i])
        g.remove_nodes_from(list(nx.isolates(g)))

        if node_features is not None:
            node_attrs = {n: {nf_name: node_features[i, n]} for n in g.nodes}
            nx.set_node_attributes(g, node_attrs, nf_name)
        if edge_features is not None:
            edge_attrs = {e: {ef_name: edge_features[i, e[0], e[1]]} for e in g.edges}
            nx.set_edge_attributes(g, edge_attrs, ef_name)
        output.append(g)

    if len(output) == 1:
        return output[0]
    else:
        return output 
Example #23
Source File: isolate.py    From aws-kube-codesuite with Apache License 2.0 4 votes vote down vote up
def isolates(G):
    """Iterator over isolates in the graph.

    An *isolate* is a node with no neighbors (that is, with degree
    zero). For directed graphs, this means no in-neighbors and no
    out-neighbors.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    iterator
        An iterator over the isolates of `G`.

    Examples
    --------
    To get a list of all isolates of a graph, use the :class:`list`
    constructor::

        >>> G = nx.Graph()
        >>> G.add_edge(1, 2)
        >>> G.add_node(3)
        >>> list(nx.isolates(G))
        [3]

    To remove all isolates in the graph, first create a list of the
    isolates, then use :meth:`Graph.remove_nodes_from`::

        >>> G.remove_nodes_from(list(nx.isolates(G)))
        >>> list(G)
        [1, 2]

    For digraphs, isolates have zero in-degree and zero out_degre::

        >>> G = nx.DiGraph([(0, 1), (1, 2)])
        >>> G.add_node(3)
        >>> list(nx.isolates(G))
        [3]

    """
    return (n for n, d in G.degree() if d == 0) 
Example #24
Source File: basic.py    From aws-kube-codesuite with Apache License 2.0 4 votes vote down vote up
def color(G):
    """Returns a two-coloring of the graph.

    Raises an exception if the graph is not bipartite.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    color : dictionary
       A dictionary keyed by node with a 1 or 0 as data for each node color.

    Raises
    ------
    exc:`NetworkXError` if the graph is not two-colorable.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> c = bipartite.color(G)
    >>> print(c)
    {0: 1, 1: 0, 2: 1, 3: 0}

    You can use this to set a node attribute indicating the biparite set:

    >>> nx.set_node_attributes(G, c, 'bipartite')
    >>> print(G.nodes[0]['bipartite'])
    1
    >>> print(G.nodes[1]['bipartite'])
    0
    """
    if G.is_directed():
        import itertools
        def neighbors(v):
            return itertools.chain.from_iterable([G.predecessors(v),
                                                  G.successors(v)])
    else:
        neighbors=G.neighbors

    color = {}
    for n in G: # handle disconnected graphs
        if n in color or len(G[n])==0: # skip isolates
            continue
        queue = [n]
        color[n] = 1 # nodes seen with color (1 or 0)
        while queue:
            v = queue.pop()
            c = 1 - color[v] # opposite color of node v
            for w in neighbors(v):
                if w in color:
                    if color[w] == color[v]:
                        raise nx.NetworkXError("Graph is not bipartite.")
                else:
                    color[w] = c
                    queue.append(w)
    # color isolates with 0
    color.update(dict.fromkeys(nx.isolates(G),0))
    return color 
Example #25
Source File: cli.py    From pybel with MIT License 4 votes vote down vote up
def main(directory: str):
    """Make hetionet exports."""
    path = os.path.join(directory, 'hetionet.bel.nodelink.json.gz')
    if not os.path.exists(path):
        graph = get_hetionet()
        to_nodelink_gz(graph, path)
    else:
        click.echo('loading pickle from {}'.format(path))
        graph = from_nodelink_gz(path)

    output_bel_gz_path = os.path.join(directory, 'hetionet.bel.gz')
    if not os.path.exists(output_bel_gz_path):
        click.echo('outputting whole hetionet as BEL GZ to {}'.format(output_bel_gz_path))
        to_bel_script_gz(graph, output_bel_gz_path, use_identifiers=True)

    output_graphdati_jsonl_gz_path = os.path.join(directory, 'hetionet.bel.graphdati.jsonl.gz')
    if not os.path.exists(output_graphdati_jsonl_gz_path):
        click.echo('outputting whole hetionet as BEL GraphDati JSONL GZ to {}'.format(output_graphdati_jsonl_gz_path))
        to_graphdati_jsonl_gz(graph, output_graphdati_jsonl_gz_path, use_identifiers=True)

    output_graphdati_gz_path = os.path.join(directory, 'hetionet.bel.graphdati.json.gz')
    if not os.path.exists(output_graphdati_gz_path):
        click.echo('outputting whole hetionet as BEL GraphDati JSON GZ to {}'.format(output_graphdati_gz_path))
        to_graphdati_gz(graph, output_graphdati_gz_path, use_identifiers=True)

    summary_tsv_path = os.path.join(directory, 'hetionet_summary.tsv')
    if not os.path.exists(summary_tsv_path):
        click.echo('getting metaedges')
        rows = []
        keep_keys = set()
        for value in get_metaedge_to_key(graph).values():
            u, v, key = choice(list(value))
            keep_keys.add(key)
            d = graph[u][v][key]
            bel = edge_to_bel(u, v, d, use_identifiers=True)
            rows.append((key[:8], bel))

        df = pd.DataFrame(rows, columns=['key', 'bel'])
        df.to_csv(summary_tsv_path, sep='\t', index=False)

        non_sample_edges = [
            (u, v, k, d)
            for u, v, k, d in tqdm(graph.edges(keys=True, data=True), desc='Getting non-sample edges to remove')
            if k not in keep_keys
        ]
        click.echo('Removing non-sample edges')
        graph.remove_edges_from(non_sample_edges)
        graph.remove_nodes_from(list(nx.isolates(graph)))

        sample_bel_path = os.path.join(directory, 'hetionet_sample.bel')
        click.echo('outputting sample hetionet in BEL to {}'.format(sample_bel_path))
        to_bel_script(graph, sample_bel_path, use_identifiers=True)

        sample_graphdati_path = os.path.join(directory, 'hetionet_sample.bel.graphdati.json')
        click.echo('outputting sample hetionet in BEL to {}'.format(sample_bel_path))
        to_graphdati_file(graph, sample_graphdati_path, use_identifiers=True, indent=2) 
Example #26
Source File: data.py    From graph-generation with MIT License 4 votes vote down vote up
def Graph_load_batch(min_num_nodes = 20, max_num_nodes = 1000, name = 'ENZYMES',node_attributes = True,graph_labels=True):
    '''
    load many graphs, e.g. enzymes
    :return: a list of graphs
    '''
    print('Loading graph dataset: '+str(name))
    G = nx.Graph()
    # load data
    path = 'dataset/'+name+'/'
    data_adj = np.loadtxt(path+name+'_A.txt', delimiter=',').astype(int)
    if node_attributes:
        data_node_att = np.loadtxt(path+name+'_node_attributes.txt', delimiter=',')
    data_node_label = np.loadtxt(path+name+'_node_labels.txt', delimiter=',').astype(int)
    data_graph_indicator = np.loadtxt(path+name+'_graph_indicator.txt', delimiter=',').astype(int)
    if graph_labels:
        data_graph_labels = np.loadtxt(path+name+'_graph_labels.txt', delimiter=',').astype(int)


    data_tuple = list(map(tuple, data_adj))
    # print(len(data_tuple))
    # print(data_tuple[0])

    # add edges
    G.add_edges_from(data_tuple)
    # add node attributes
    for i in range(data_node_label.shape[0]):
        if node_attributes:
            G.add_node(i+1, feature = data_node_att[i])
        G.add_node(i+1, label = data_node_label[i])
    G.remove_nodes_from(list(nx.isolates(G)))

    # print(G.number_of_nodes())
    # print(G.number_of_edges())

    # split into graphs
    graph_num = data_graph_indicator.max()
    node_list = np.arange(data_graph_indicator.shape[0])+1
    graphs = []
    max_nodes = 0
    for i in range(graph_num):
        # find the nodes for each graph
        nodes = node_list[data_graph_indicator==i+1]
        G_sub = G.subgraph(nodes)
        if graph_labels:
            G_sub.graph['label'] = data_graph_labels[i]
        # print('nodes', G_sub.number_of_nodes())
        # print('edges', G_sub.number_of_edges())
        # print('label', G_sub.graph)
        if G_sub.number_of_nodes()>=min_num_nodes and G_sub.number_of_nodes()<=max_num_nodes:
            graphs.append(G_sub)
            if G_sub.number_of_nodes() > max_nodes:
                max_nodes = G_sub.number_of_nodes()
            # print(G_sub.number_of_nodes(), 'i', i)
    # print('Graph dataset name: {}, total graph num: {}'.format(name, len(graphs)))
    # logging.warning('Graphs loaded, total num: {}'.format(len(graphs)))
    print('Loaded')
    return graphs 
Example #27
Source File: isolate.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def isolates(G):
    """Iterator over isolates in the graph.

    An *isolate* is a node with no neighbors (that is, with degree
    zero). For directed graphs, this means no in-neighbors and no
    out-neighbors.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    iterator
        An iterator over the isolates of `G`.

    Examples
    --------
    To get a list of all isolates of a graph, use the :class:`list`
    constructor::

        >>> G = nx.Graph()
        >>> G.add_edge(1, 2)
        >>> G.add_node(3)
        >>> list(nx.isolates(G))
        [3]

    To remove all isolates in the graph, first create a list of the
    isolates, then use :meth:`Graph.remove_nodes_from`::

        >>> G.remove_nodes_from(list(nx.isolates(G)))
        >>> list(G)
        [1, 2]

    For digraphs, isolates have zero in-degree and zero out_degre::

        >>> G = nx.DiGraph([(0, 1), (1, 2)])
        >>> G.add_node(3)
        >>> list(nx.isolates(G))
        [3]

    """
    return (n for n, d in G.degree() if d == 0) 
Example #28
Source File: data.py    From graph-generation with MIT License 4 votes vote down vote up
def Graph_synthetic(seed):
    G = nx.Graph()
    np.random.seed(seed)
    base = np.repeat(np.eye(5), 20, axis=0)
    rand = np.random.randn(100, 5) * 0.05
    node_features = base + rand

    # # print('node features')
    # for i in range(node_features.shape[0]):
    #     print(np.around(node_features[i], decimals=4))

    node_distance_l1 = np.ones((node_features.shape[0], node_features.shape[0]))
    node_distance_np = np.zeros((node_features.shape[0], node_features.shape[0]))
    for i in range(node_features.shape[0]):
        for j in range(node_features.shape[0]):
            if i != j:
                node_distance_l1[i,j] = np.sum(np.abs(node_features[i] - node_features[j]))
                # print('node distance', node_distance_l1[i,j])
                node_distance_np[i, j] = 1 / np.sum(np.abs(node_features[i] - node_features[j]) ** 2)

    print('node distance max', np.max(node_distance_l1))
    print('node distance min', np.min(node_distance_l1))
    node_distance_np_sum = np.sum(node_distance_np, axis=1, keepdims=True)
    embedding_dist = node_distance_np / node_distance_np_sum

    # generate the graph
    average_degree = 9
    for i in range(node_features.shape[0]):
        for j in range(i + 1, embedding_dist.shape[0]):
            p = np.random.rand()
            if p < embedding_dist[i, j] * average_degree:
                G.add_edge(i, j)

    G.remove_nodes_from(nx.isolates(G))
    print('num of nodes', G.number_of_nodes())
    print('num of edges', G.number_of_edges())

    G_deg = nx.degree_histogram(G)
    G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
    print('average degree', sum(G_deg_sum) / G.number_of_nodes())
    print('average path length', nx.average_shortest_path_length(G))
    print('diameter', nx.diameter(G))
    G_cluster = sorted(list(nx.clustering(G).values()))
    print('average clustering coefficient', sum(G_cluster) / len(G_cluster))
    print('Graph generation complete!')
    # node_features = np.concatenate((node_features, np.zeros((1,node_features.shape[1]))),axis=0)

    return G, node_features

# G = Graph_synthetic(10)



# return adj and features from a single graph 
Example #29
Source File: io_utils.py    From gnn-model-explainer with Apache License 2.0 4 votes vote down vote up
def denoise_graph(adj, node_idx, feat=None, label=None, threshold=None, threshold_num=None, max_component=True):
    """Cleaning a graph by thresholding its node values.

    Args:
        - adj               :  Adjacency matrix.
        - node_idx          :  Index of node to highlight (TODO ?)
        - feat              :  An array of node features.
        - label             :  A list of node labels.
        - threshold         :  The weight threshold.
        - theshold_num      :  The maximum number of nodes to threshold.
        - max_component     :  TODO
    """
    num_nodes = adj.shape[-1]
    G = nx.Graph()
    G.add_nodes_from(range(num_nodes))
    G.nodes[node_idx]["self"] = 1
    if feat is not None:
        for node in G.nodes():
            G.nodes[node]["feat"] = feat[node]
    if label is not None:
        for node in G.nodes():
            G.nodes[node]["label"] = label[node]

    if threshold_num is not None:
        # this is for symmetric graphs: edges are repeated twice in adj
        adj_threshold_num = threshold_num * 2
        #adj += np.random.rand(adj.shape[0], adj.shape[1]) * 1e-4
        neigh_size = len(adj[adj > 0])
        threshold_num = min(neigh_size, adj_threshold_num)
        threshold = np.sort(adj[adj > 0])[-threshold_num]

    if threshold is not None:
        weighted_edge_list = [
            (i, j, adj[i, j])
            for i in range(num_nodes)
            for j in range(num_nodes)
            if adj[i, j] >= threshold
        ]
    else:
        weighted_edge_list = [
            (i, j, adj[i, j])
            for i in range(num_nodes)
            for j in range(num_nodes)
            if adj[i, j] > 1e-6
        ]
    G.add_weighted_edges_from(weighted_edge_list)
    if max_component:
        largest_cc = max(nx.connected_components(G), key=len)
        G = G.subgraph(largest_cc).copy()
    else:
        # remove zero degree nodes
        G.remove_nodes_from(list(nx.isolates(G)))
    return G

# TODO: unify log_graph and log_graph2 
Example #30
Source File: data.py    From GraphRNN with MIT License 4 votes vote down vote up
def Graph_load_batch(min_num_nodes = 20, max_num_nodes = 1000, name = 'ENZYMES',node_attributes = True,graph_labels=True):
    '''
    load many graphs, e.g. enzymes
    :return: a list of graphs
    '''
    print('Loading graph dataset: '+str(name))
    G = nx.Graph()
    # load data
    path = 'dataset/'+name+'/'
    data_adj = np.loadtxt(path+name+'_A.txt', delimiter=',').astype(int)
    if node_attributes:
        data_node_att = np.loadtxt(path+name+'_node_attributes.txt', delimiter=',')
    data_node_label = np.loadtxt(path+name+'_node_labels.txt', delimiter=',').astype(int)
    data_graph_indicator = np.loadtxt(path+name+'_graph_indicator.txt', delimiter=',').astype(int)
    if graph_labels:
        data_graph_labels = np.loadtxt(path+name+'_graph_labels.txt', delimiter=',').astype(int)


    data_tuple = list(map(tuple, data_adj))
    # print(len(data_tuple))
    # print(data_tuple[0])

    # add edges
    G.add_edges_from(data_tuple)
    # add node attributes
    for i in range(data_node_label.shape[0]):
        if node_attributes:
            G.add_node(i+1, feature = data_node_att[i])
        G.add_node(i+1, label = data_node_label[i])
    G.remove_nodes_from(list(nx.isolates(G)))

    # print(G.number_of_nodes())
    # print(G.number_of_edges())

    # split into graphs
    graph_num = data_graph_indicator.max()
    node_list = np.arange(data_graph_indicator.shape[0])+1
    graphs = []
    max_nodes = 0
    for i in range(graph_num):
        # find the nodes for each graph
        nodes = node_list[data_graph_indicator==i+1]
        G_sub = G.subgraph(nodes)
        if graph_labels:
            G_sub.graph['label'] = data_graph_labels[i]
        # print('nodes', G_sub.number_of_nodes())
        # print('edges', G_sub.number_of_edges())
        # print('label', G_sub.graph)
        if G_sub.number_of_nodes()>=min_num_nodes and G_sub.number_of_nodes()<=max_num_nodes:
            graphs.append(G_sub)
            if G_sub.number_of_nodes() > max_nodes:
                max_nodes = G_sub.number_of_nodes()
            # print(G_sub.number_of_nodes(), 'i', i)
    # print('Graph dataset name: {}, total graph num: {}'.format(name, len(graphs)))
    # logging.warning('Graphs loaded, total num: {}'.format(len(graphs)))
    print('Loaded')
    return graphs