Python networkx.OrderedGraph() Examples

The following are 22 code examples of networkx.OrderedGraph(). 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: test_graphs.py    From pygsp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_differential_operator(self, n_vertices=98):
        r"""The Laplacian must always be the divergence of the gradient,
        whether the Laplacian is combinatorial or normalized, and whether the
        graph is directed or weighted."""
        def test_incidence_nx(graph):
            r"""Test that the incidence matrix corresponds to NetworkX."""
            incidence_pg = np.sign(graph.D.toarray())
            G = nx.OrderedDiGraph if graph.is_directed() else nx.OrderedGraph
            graph_nx = nx.from_scipy_sparse_matrix(graph.W, create_using=G)
            incidence_nx = nx.incidence_matrix(graph_nx, oriented=True)
            np.testing.assert_equal(incidence_pg, incidence_nx.toarray())
        for graph in [graphs.Graph(np.zeros((n_vertices, n_vertices))),
                      graphs.Graph(np.identity(n_vertices)),
                      graphs.Graph([[0, 0.8], [0.8, 0]]),
                      graphs.Graph([[1.3, 0], [0.4, 0.5]]),
                      graphs.ErdosRenyi(n_vertices, directed=False, seed=42),
                      graphs.ErdosRenyi(n_vertices, directed=True, seed=42)]:
            for lap_type in ['combinatorial', 'normalized']:
                graph.compute_laplacian(lap_type)
                graph.compute_differential_operator()
                L = graph.D.dot(graph.D.T)
                np.testing.assert_allclose(L.toarray(), graph.L.toarray())
                test_incidence_nx(graph) 
Example #2
Source File: test_gml.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_tuplelabels(self):
        # https://github.com/networkx/networkx/pull/1048
        # Writing tuple labels to GML failed.
        G = nx.OrderedGraph()
        G.add_edge((0, 1), (1, 0))
        data = '\n'.join(nx.generate_gml(G, stringizer=literal_stringizer))
        answer = """graph [
  node [
    id 0
    label "(0,1)"
  ]
  node [
    id 1
    label "(1,0)"
  ]
  edge [
    source 0
    target 1
  ]
]"""
        assert_equal(data, answer) 
Example #3
Source File: test_gml.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_tuplelabels(self):
        # https://github.com/networkx/networkx/pull/1048
        # Writing tuple labels to GML failed.
        G = nx.OrderedGraph()
        G.add_edge((0, 1), (1, 0))
        data = '\n'.join(nx.generate_gml(G, stringizer=literal_stringizer))
        answer = """graph [
  node [
    id 0
    label "(0,1)"
  ]
  node [
    id 1
    label "(1,0)"
  ]
  edge [
    source 0
    target 1
  ]
]"""
        assert_equal(data, answer) 
Example #4
Source File: test_ordered.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph(self):
        G = nx.OrderedGraph() 
Example #5
Source File: test_ordered.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_graph():
        G = nx.OrderedGraph() 
Example #6
Source File: test_convert.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_attribute_dict_integrity(self):
        # we must not replace dict-like graph data structures with dicts
        G = nx.OrderedGraph()
        G.add_nodes_from("abc")
        H = to_networkx_graph(G, create_using=nx.OrderedGraph())
        assert_equal(list(H.nodes), list(G.nodes))
        H = nx.OrderedDiGraph(G)
        assert_equal(list(H.nodes), list(G.nodes)) 
Example #7
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_write_edgelist_4(self):
        fh=io.BytesIO()
        G=nx.OrderedGraph()
        G.add_edge(1,2,weight=2.0)
        G.add_edge(2,3,weight=3.0)
        nx.write_edgelist(G,fh,data=[('weight')])
        fh.seek(0)
        assert_equal(fh.read(),b"1 2 2.0\n2 3 3.0\n") 
Example #8
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_write_edgelist_3(self):
        fh=io.BytesIO()
        G=nx.OrderedGraph()
        G.add_edge(1,2,weight=2.0)
        G.add_edge(2,3,weight=3.0)
        nx.write_edgelist(G,fh,data=True)
        fh.seek(0)
        assert_equal(fh.read(),b"1 2 {'weight': 2.0}\n2 3 {'weight': 3.0}\n") 
Example #9
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_write_edgelist_2(self):
        fh=io.BytesIO()
        G=nx.OrderedGraph()
        G.add_edges_from([(1,2),(2,3)])
        nx.write_edgelist(G,fh,data=True)
        fh.seek(0)
        assert_equal(fh.read(),b"1 2 {}\n2 3 {}\n") 
Example #10
Source File: test_edgelist.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_write_edgelist_1(self):
        fh=io.BytesIO()
        G=nx.OrderedGraph()
        G.add_edges_from([(1,2),(2,3)])
        nx.write_edgelist(G,fh,data=False)
        fh.seek(0)
        assert_equal(fh.read(),b"1 2\n2 3\n") 
Example #11
Source File: test_planarity.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph2(self):
        G = nx.OrderedGraph([
            (1, 2), (4, 13), (0, 13), (4, 5), (7, 10), (1, 7), (0, 3), (2, 6),
            (5, 6), (7, 13), (4, 8), (0, 8), (0, 9), (2, 13), (6, 7), (3, 6),
            (2, 8)
        ])
        self.check_graph(G, is_planar=False) 
Example #12
Source File: test_planarity.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph1(self):
        G = nx.OrderedGraph([
            (3, 10), (2, 13), (1, 13), (7, 11), (0, 8), (8, 13), (0, 2),
            (0, 7), (0, 10), (1, 7)
        ])
        self.check_graph(G, is_planar=True) 
Example #13
Source File: test_canonicalization.py    From dwave_networkx with Apache License 2.0 5 votes vote down vote up
def test_reversed(self):
        C33 = nx.OrderedGraph()
        C33.add_nodes_from(reversed(range(3*3*4)))
        C33.add_edges_from(dnx.chimera_graph(3, 3, 4).edges)
        coord = chimera_coordinates(3, 3, 4)

        labels = canonical_chimera_labeling(C33)
        labels = {v: coord.chimera_to_linear(labels[v]) for v in labels}

        G = nx.relabel_nodes(C33, labels, copy=True)

        self.assertTrue(nx.is_isomorphic(G, C33)) 
Example #14
Source File: test_graphviews.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_subgraph_copy(self):
        for origG in self.graphs:
            G = nx.OrderedGraph(origG)
            SG = G.subgraph([4, 5, 6])
            H = SG.copy()
            assert_equal(type(G), type(H)) 
Example #15
Source File: test_convert.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_attribute_dict_integrity(self):
        # we must not replace dict-like graph data structures with dicts
        G = nx.OrderedGraph()
        G.add_nodes_from("abc")
        H = to_networkx_graph(G, create_using=nx.OrderedGraph)
        assert_equal(list(H.nodes), list(G.nodes))
        H = nx.OrderedDiGraph(G)
        assert_equal(list(H.nodes), list(G.nodes)) 
Example #16
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write_edgelist_4(self):
        fh = io.BytesIO()
        G = nx.OrderedGraph()
        G.add_edge(1, 2, weight=2.0)
        G.add_edge(2, 3, weight=3.0)
        nx.write_edgelist(G, fh, data=[('weight')])
        fh.seek(0)
        assert_equal(fh.read(), b"1 2 2.0\n2 3 3.0\n") 
Example #17
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write_edgelist_3(self):
        fh = io.BytesIO()
        G = nx.OrderedGraph()
        G.add_edge(1, 2, weight=2.0)
        G.add_edge(2, 3, weight=3.0)
        nx.write_edgelist(G, fh, data=True)
        fh.seek(0)
        assert_equal(fh.read(), b"1 2 {'weight': 2.0}\n2 3 {'weight': 3.0}\n") 
Example #18
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write_edgelist_2(self):
        fh = io.BytesIO()
        G = nx.OrderedGraph()
        G.add_edges_from([(1, 2), (2, 3)])
        nx.write_edgelist(G, fh, data=True)
        fh.seek(0)
        assert_equal(fh.read(), b"1 2 {}\n2 3 {}\n") 
Example #19
Source File: test_edgelist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write_edgelist_1(self):
        fh = io.BytesIO()
        G = nx.OrderedGraph()
        G.add_edges_from([(1, 2), (2, 3)])
        nx.write_edgelist(G, fh, data=False)
        fh.seek(0)
        assert_equal(fh.read(), b"1 2\n2 3\n") 
Example #20
Source File: test_ordered.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_graph():
        G = nx.OrderedGraph() 
Example #21
Source File: networkclustering.py    From PyPSA with GNU General Public License v3.0 5 votes vote down vote up
def busmap_by_linemask(network, mask):
    mask = network.lines.loc[:,['bus0', 'bus1']].assign(mask=mask).set_index(['bus0','bus1'])['mask']
    G = nx.OrderedGraph()
    G.add_nodes_from(network.buses.index)
    G.add_edges_from(mask.index[mask])
    return pd.Series(OrderedDict((n, str(i))
                                 for i, g in enumerate(nx.connected_components(G))
                                 for n in g),
                     name='name') 
Example #22
Source File: pf.py    From PyPSA with GNU General Public License v3.0 4 votes vote down vote up
def find_cycles(sub_network, weight='x_pu'):
    """
    Find all cycles in the sub_network and record them in sub_network.C.

    networkx collects the cycles with more than 2 edges; then the 2-edge cycles
    from the MultiGraph must be collected separately (for cases where there
    are multiple lines between the same pairs of buses).

    Cycles with infinite impedance are skipped.
    """
    branches_bus0 = sub_network.branches()["bus0"]
    branches_i = branches_bus0.index

    #reduce to a non-multi-graph for cycles with > 2 edges
    mgraph = sub_network.graph(weight=weight, inf_weight=False)
    graph = nx.OrderedGraph(mgraph)

    cycles = nx.cycle_basis(graph)

    #number of 2-edge cycles
    num_multi = len(mgraph.edges()) - len(graph.edges())

    sub_network.C = dok_matrix((len(branches_bus0),len(cycles)+num_multi))

    for j,cycle in enumerate(cycles):

        for i in range(len(cycle)):
            branch = next(iterkeys(mgraph[cycle[i]][cycle[(i+1)%len(cycle)]]))
            branch_i = branches_i.get_loc(branch)
            sign = +1 if branches_bus0.iat[branch_i] == cycle[i] else -1
            sub_network.C[branch_i,j] += sign

    #counter for multis
    c = len(cycles)

    #add multi-graph 2-edge cycles for multiple branches between same pairs of buses
    for u,v in graph.edges():
        bs = list(mgraph[u][v].keys())
        if len(bs) > 1:
            first = bs[0]
            first_i = branches_i.get_loc(first)
            for b in bs[1:]:
                b_i = branches_i.get_loc(b)
                sign = -1 if branches_bus0.iat[b_i] == branches_bus0.iat[first_i] else +1
                sub_network.C[first_i,c] = 1
                sub_network.C[b_i,c] = sign
                c+=1