test_pajek.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. Pajek tests
  3. """
  4. import networkx as nx
  5. from networkx.utils import edges_equal, nodes_equal
  6. class TestPajek:
  7. @classmethod
  8. def setup_class(cls):
  9. 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"""
  10. cls.G = nx.MultiDiGraph()
  11. cls.G.add_nodes_from(["A1", "Bb", "C", "D2"])
  12. cls.G.add_edges_from(
  13. [
  14. ("A1", "A1"),
  15. ("A1", "Bb"),
  16. ("A1", "C"),
  17. ("Bb", "A1"),
  18. ("C", "C"),
  19. ("C", "D2"),
  20. ("D2", "Bb"),
  21. ]
  22. )
  23. cls.G.graph["name"] = "Tralala"
  24. def test_parse_pajek_simple(self):
  25. # Example without node positions or shape
  26. data = """*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1"""
  27. G = nx.parse_pajek(data)
  28. assert sorted(G.nodes()) == ["1", "2"]
  29. assert edges_equal(G.edges(), [("1", "2"), ("1", "2")])
  30. def test_parse_pajek(self):
  31. G = nx.parse_pajek(self.data)
  32. assert sorted(G.nodes()) == ["A1", "Bb", "C", "D2"]
  33. assert edges_equal(
  34. G.edges(),
  35. [
  36. ("A1", "A1"),
  37. ("A1", "Bb"),
  38. ("A1", "C"),
  39. ("Bb", "A1"),
  40. ("C", "C"),
  41. ("C", "D2"),
  42. ("D2", "Bb"),
  43. ],
  44. directed=True,
  45. )
  46. def test_parse_pajek_mat(self):
  47. data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n"""
  48. G = nx.parse_pajek(data)
  49. assert set(G.nodes()) == {"one", "two", "three"}
  50. assert G.nodes["two"] == {"id": "2"}
  51. assert edges_equal(
  52. G.edges(),
  53. [("one", "one"), ("one", "two"), ("two", "two"), ("three", "two")],
  54. directed=True,
  55. )
  56. def test_read_pajek(self, tmp_path):
  57. G = nx.parse_pajek(self.data)
  58. # Read data from file
  59. fname = tmp_path / "test.pjk"
  60. with open(fname, "wb") as fh:
  61. fh.write(self.data.encode("UTF-8"))
  62. Gin = nx.read_pajek(fname)
  63. assert sorted(G.nodes()) == sorted(Gin.nodes())
  64. assert edges_equal(G.edges(), Gin.edges(), directed=True)
  65. assert self.G.graph == Gin.graph
  66. for n in G:
  67. assert G.nodes[n] == Gin.nodes[n]
  68. def test_write_pajek(self):
  69. import io
  70. G = nx.parse_pajek(self.data)
  71. fh = io.BytesIO()
  72. nx.write_pajek(G, fh)
  73. fh.seek(0)
  74. H = nx.read_pajek(fh)
  75. assert nodes_equal(list(G), list(H))
  76. assert edges_equal(G.edges(), H.edges(), directed=True)
  77. # Graph name is left out for now, therefore it is not tested.
  78. # assert_equal(G.graph, H.graph)
  79. def test_ignored_attribute(self):
  80. import io
  81. G = nx.Graph()
  82. fh = io.BytesIO()
  83. G.add_node(1, int_attr=1)
  84. G.add_node(2, empty_attr=" ")
  85. G.add_edge(1, 2, int_attr=2)
  86. G.add_edge(2, 3, empty_attr=" ")
  87. import warnings
  88. with warnings.catch_warnings(record=True) as w:
  89. nx.write_pajek(G, fh)
  90. assert len(w) == 4
  91. def test_noname(self):
  92. # Make sure we can parse a line such as: *network
  93. # Issue #952
  94. line = "*network\n"
  95. other_lines = self.data.split("\n")[1:]
  96. data = line + "\n".join(other_lines)
  97. G = nx.parse_pajek(data)
  98. def test_unicode(self):
  99. import io
  100. G = nx.Graph()
  101. name1 = chr(2344) + chr(123) + chr(6543)
  102. name2 = chr(5543) + chr(1543) + chr(324)
  103. G.add_edge(name1, "Radiohead", foo=name2)
  104. fh = io.BytesIO()
  105. nx.write_pajek(G, fh)
  106. fh.seek(0)
  107. H = nx.read_pajek(fh)
  108. assert nodes_equal(list(G), list(H))
  109. assert edges_equal(list(G.edges()), list(H.edges()))
  110. assert G.graph == H.graph