test_pajek.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. Pajek tests
  3. """
  4. import os
  5. import tempfile
  6. import networkx as nx
  7. from networkx.utils import edges_equal, nodes_equal
  8. class TestPajek:
  9. @classmethod
  10. def setup_class(cls):
  11. cls.data = """*network Tralala\n*vertices 4\n 1 "A1" 0.0938 0.0896 ellipse x_fact 1 y_fact 1\n 2 "Bb" 0.8188 0.2458 ellipse x_fact 1 y_fact 1\n 3 "C" 0.3688 0.7792 ellipse x_fact 1\n 4 "D2" 0.9583 0.8563 ellipse x_fact 1\n*arcs\n1 1 1 h2 0 w 3 c Blue s 3 a1 -130 k1 0.6 a2 -130 k2 0.6 ap 0.5 l "Bezier loop" lc BlueViolet fos 20 lr 58 lp 0.3 la 360\n2 1 1 h2 0 a1 120 k1 1.3 a2 -120 k2 0.3 ap 25 l "Bezier arc" lphi 270 la 180 lr 19 lp 0.5\n1 2 1 h2 0 a1 40 k1 2.8 a2 30 k2 0.8 ap 25 l "Bezier arc" lphi 90 la 0 lp 0.65\n4 2 -1 h2 0 w 1 k1 -2 k2 250 ap 25 l "Circular arc" c Red lc OrangeRed\n3 4 1 p Dashed h2 0 w 2 c OliveGreen ap 25 l "Straight arc" lc PineGreen\n1 3 1 p Dashed h2 0 w 5 k1 -1 k2 -20 ap 25 l "Oval arc" c Brown lc Black\n3 3 -1 h1 6 w 1 h2 12 k1 -2 k2 -15 ap 0.5 l "Circular loop" c Red lc OrangeRed lphi 270 la 180"""
  12. cls.G = nx.MultiDiGraph()
  13. cls.G.add_nodes_from(["A1", "Bb", "C", "D2"])
  14. cls.G.add_edges_from(
  15. [
  16. ("A1", "A1"),
  17. ("A1", "Bb"),
  18. ("A1", "C"),
  19. ("Bb", "A1"),
  20. ("C", "C"),
  21. ("C", "D2"),
  22. ("D2", "Bb"),
  23. ]
  24. )
  25. cls.G.graph["name"] = "Tralala"
  26. (fd, cls.fname) = tempfile.mkstemp()
  27. with os.fdopen(fd, "wb") as fh:
  28. fh.write(cls.data.encode("UTF-8"))
  29. @classmethod
  30. def teardown_class(cls):
  31. os.unlink(cls.fname)
  32. def test_parse_pajek_simple(self):
  33. # Example without node positions or shape
  34. data = """*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1"""
  35. G = nx.parse_pajek(data)
  36. assert sorted(G.nodes()) == ["1", "2"]
  37. assert edges_equal(G.edges(), [("1", "2"), ("1", "2")])
  38. def test_parse_pajek(self):
  39. G = nx.parse_pajek(self.data)
  40. assert sorted(G.nodes()) == ["A1", "Bb", "C", "D2"]
  41. assert edges_equal(
  42. G.edges(),
  43. [
  44. ("A1", "A1"),
  45. ("A1", "Bb"),
  46. ("A1", "C"),
  47. ("Bb", "A1"),
  48. ("C", "C"),
  49. ("C", "D2"),
  50. ("D2", "Bb"),
  51. ],
  52. )
  53. def test_parse_pajet_mat(self):
  54. data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n"""
  55. G = nx.parse_pajek(data)
  56. assert set(G.nodes()) == {"one", "two", "three"}
  57. assert G.nodes["two"] == {"id": "2"}
  58. assert edges_equal(
  59. set(G.edges()),
  60. {("one", "one"), ("two", "one"), ("two", "two"), ("two", "three")},
  61. )
  62. def test_read_pajek(self):
  63. G = nx.parse_pajek(self.data)
  64. Gin = nx.read_pajek(self.fname)
  65. assert sorted(G.nodes()) == sorted(Gin.nodes())
  66. assert edges_equal(G.edges(), Gin.edges())
  67. assert self.G.graph == Gin.graph
  68. for n in G:
  69. assert G.nodes[n] == Gin.nodes[n]
  70. def test_write_pajek(self):
  71. import io
  72. G = nx.parse_pajek(self.data)
  73. fh = io.BytesIO()
  74. nx.write_pajek(G, fh)
  75. fh.seek(0)
  76. H = nx.read_pajek(fh)
  77. assert nodes_equal(list(G), list(H))
  78. assert edges_equal(list(G.edges()), list(H.edges()))
  79. # Graph name is left out for now, therefore it is not tested.
  80. # assert_equal(G.graph, H.graph)
  81. def test_ignored_attribute(self):
  82. import io
  83. G = nx.Graph()
  84. fh = io.BytesIO()
  85. G.add_node(1, int_attr=1)
  86. G.add_node(2, empty_attr=" ")
  87. G.add_edge(1, 2, int_attr=2)
  88. G.add_edge(2, 3, empty_attr=" ")
  89. import warnings
  90. with warnings.catch_warnings(record=True) as w:
  91. nx.write_pajek(G, fh)
  92. assert len(w) == 4
  93. def test_noname(self):
  94. # Make sure we can parse a line such as: *network
  95. # Issue #952
  96. line = "*network\n"
  97. other_lines = self.data.split("\n")[1:]
  98. data = line + "\n".join(other_lines)
  99. G = nx.parse_pajek(data)
  100. def test_unicode(self):
  101. import io
  102. G = nx.Graph()
  103. name1 = chr(2344) + chr(123) + chr(6543)
  104. name2 = chr(5543) + chr(1543) + chr(324)
  105. G.add_edge(name1, "Radiohead", foo=name2)
  106. fh = io.BytesIO()
  107. nx.write_pajek(G, fh)
  108. fh.seek(0)
  109. H = nx.read_pajek(fh)
  110. assert nodes_equal(list(G), list(H))
  111. assert edges_equal(list(G.edges()), list(H.edges()))
  112. assert G.graph == H.graph