示例#1
0
 def test_length(self):
     for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
         g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
         gstr = BytesIO()
         nx.write_graph6(g, gstr, header=False)
         # Strip the trailing newline.
         gstr = gstr.getvalue().rstrip()
         assert_equal(len(gstr),
                      ((i-1) * i // 2 + 5) // 6 + (1 if i < 63 else 4))
示例#2
0
 def test_roundtrip(self):
     for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
         G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
         f = BytesIO()
         nx.write_graph6(G, f)
         f.seek(0)
         H = nx.read_graph6(f)
         assert_nodes_equal(G.nodes(), H.nodes())
         assert_edges_equal(G.edges(), H.edges())
def generateRandomNetworks(randomSeed=622527):
    seed(randomSeed)
    # Network size will be 10^1, 10 ^2, 10^3, 10^4
    for exponent in range(1, 4): # 1 .. 4
        n = 10 ** exponent

        for p in [0.1, 0.3, 0.5, 0.7, 0.9]:
            m = round(n * p)

            # Generate erdos Renyi networks
            graph = nx.erdos_renyi_graph(n, p, randomNum())
            graphName = "erdos_renyi_n{}_p{}.graph6".format(n, p)
            nx.write_graph6(graph, directory + graphName)

            # Generate Barabasi Albert networks
            graph = nx.barabasi_albert_graph(n, m, randomNum())
            graphName = "barabasi_albert_n{}_m{}.graph6".format(n, m)
            nx.write_graph6(graph, directory + graphName)

            for k in [0.1, 0.3, 0.5, 0.7, 0.9]:
                k = round(n * k)
                # Generate Watts Strogatz networks
                graph = nx.watts_strogatz_graph(n, k, p, randomNum())
                graphName = "watts_strogatz_n{}_k{}_p{}.graph6".format(n, k, p)
                nx.write_graph6(graph, directory + graphName)
示例#4
0
import json

if __name__ == '__main__':
    graph = nx.read_edgelist('input/example_graph.edgelist', nodetype=int, data=(('weight', float),))
    assert isinstance(graph, nx.Graph)
    print 'edges:', graph.edges()

    # raw
    nx.write_adjlist(graph, 'output_raw/example_graph.adjlist')
    nx.write_multiline_adjlist(graph, 'output_raw/example_graph.multiline_adjlist')
    nx.write_edgelist(graph, 'output_raw/example_graph.edgelist')

    # better serialization
    nx.write_gpickle(graph, 'output_serialization/example_graph.pickle')
    nx.write_yaml(graph, 'output_serialization/example_graph.yaml')
    nx.write_graph6(graph, 'output_serialization/example_graph.graph6')

    # xml
    nx.write_gexf(graph, 'output_xml/example_graph.gexf')
    nx.write_graphml(graph, 'output_xml/example_graph.graphml')

    # json
    with open('output_json/node_link.json', 'w') as outfile:
        json.dump(json_graph.node_link_data(graph), outfile, indent=2)

    with open('output_json/adjacency.json', 'w') as outfile:
        json.dump(json_graph.adjacency_data(graph), outfile, indent=2)

    # other
    nx.write_gml(graph, 'output_other/example_graph.gml')
    nx.write_pajek(graph, 'output_other/example_graph.pajek')
示例#5
0
 def no_directed_graphs(self):
     with self.assertRaises(nx.NetworkXNotImplemented):
         nx.write_graph6(nx.DiGraph(), BytesIO())
示例#6
0
 def test_complete_bipartite_graph(self):
     result = BytesIO()
     G = nx.complete_bipartite_graph(6, 9)
     nx.write_graph6(G, result, header=False)
     # The expected encoding here was verified by Sage.
     self.assertEqual(result.getvalue(), b'N??F~z{~Fw^_~?~?^_?\n')
示例#7
0
 def test_no_header(self):
     result = BytesIO()
     nx.write_graph6(nx.complete_graph(4), result, header=False)
     self.assertEqual(result.getvalue(), b'C~\n')
示例#8
0
 def test_large_complete_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.complete_graph(67), result, header=False)
     self.assertEqual(result.getvalue(), b'[email protected]' + b'~' * 368 + b'w\n')
示例#9
0
 def test_complete_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.complete_graph(4), result)
     self.assertEqual(result.getvalue(), b'>>graph6<<C~\n')
示例#10
0
 def test_trivial_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.trivial_graph(), result)
     self.assertEqual(result.getvalue(), b'>>graph6<<@\n')
示例#11
0
 def test_null_graph(self):
     result = BytesIO()
     nx.write_graph6(nx.null_graph(), result)
     self.assertEqual(result.getvalue(), b'>>graph6<<?\n')
示例#12
0
 def test_write_graph6(self):
     fh = StringIO()
     nx.write_graph6(nx.complete_bipartite_graph(6,9), fh)
     fh.seek(0)
     assert_equal(fh.read(), '>>graph6<<N??F~z{~Fw^_~?~?^_?\n')