test_polygonal_circuit.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pytest
  2. from freesurfer_surface import PolygonalCircuit, Vertex
  3. def test_init_tuple():
  4. circuit = PolygonalCircuit((1, 2, 3, 5))
  5. assert circuit.vertex_indices == (1, 2, 3, 5)
  6. def test_init_list():
  7. circuit = PolygonalCircuit([1, 2, 3, 5])
  8. assert circuit.vertex_indices == (1, 2, 3, 5)
  9. def test_init_iterator():
  10. circuit = PolygonalCircuit(iter([1, 2, 3, 5]))
  11. assert circuit.vertex_indices == (1, 2, 3, 5)
  12. def test_init_invalid_type():
  13. with pytest.raises(Exception):
  14. PolygonalCircuit((0, 1, 2.0))
  15. with pytest.raises(Exception):
  16. PolygonalCircuit((Vertex(2, 3, 4),))
  17. with pytest.raises(Exception):
  18. PolygonalCircuit((0, 1, Vertex(2, 3, 4)))
  19. @pytest.mark.parametrize(
  20. ("source_vertex_indices", "expected_vertex_indices"),
  21. [
  22. ((1,), (1,)),
  23. ((1, 2), (1, 2)),
  24. ((2, 1), (1, 2)),
  25. ((1, 2, 3), (1, 2, 3)),
  26. ((2, 3, 1), (1, 2, 3)),
  27. ((3, 1, 2), (1, 2, 3)),
  28. ((1, 3, 2), (1, 2, 3)),
  29. ((2, 1, 3), (1, 2, 3)),
  30. ((3, 2, 1), (1, 2, 3)),
  31. ((1, 2, 3, 5), (1, 2, 3, 5)),
  32. ((2, 3, 5, 1), (1, 2, 3, 5)),
  33. ((3, 5, 1, 2), (1, 2, 3, 5)),
  34. ((2, 1, 5, 3), (1, 2, 3, 5)),
  35. ((5, 3, 2, 1), (1, 2, 3, 5)),
  36. ],
  37. )
  38. def test__normalize(source_vertex_indices, expected_vertex_indices):
  39. # pylint: disable=protected-access
  40. circuit = PolygonalCircuit(source_vertex_indices)
  41. assert expected_vertex_indices == circuit._normalize()._vertex_indices
  42. def test_eq():
  43. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((0, 1))
  44. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((1, 0))
  45. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 2))
  46. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((1, 2, 0))
  47. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 0, 1))
  48. # pylint: disable=unneeded-not
  49. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 4))
  50. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 4, 2))
  51. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((4, 1, 2))
  52. def test_eq_reverse():
  53. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 1, 0))
  54. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 2, 1))
  55. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((4, 2, 1, 0))
  56. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((1, 0, 4, 2))
  57. def test_hash():
  58. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((0, 1, 2)))
  59. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((1, 2, 0)))
  60. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((2, 0, 1)))
  61. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((0, 1, 4)))
  62. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((0, 4, 2)))
  63. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((4, 1, 2)))
  64. def test_hash_reverse():
  65. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((2, 1, 0)))
  66. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((0, 2, 1)))
  67. assert hash(PolygonalCircuit((0, 1, 2, 4))) == hash(PolygonalCircuit((4, 2, 1, 0)))
  68. assert hash(PolygonalCircuit((0, 1, 2, 4))) == hash(PolygonalCircuit((1, 0, 4, 2)))
  69. assert hash(PolygonalCircuit((0, 1, 2, 4))) != hash(PolygonalCircuit((1, 4, 0, 2)))
  70. def test_adjacent_vertex_indices_1():
  71. chain = PolygonalCircuit((0, 1, 4, 8))
  72. singles = list(chain.adjacent_vertex_indices(1))
  73. assert len(singles) == 4
  74. assert singles[0] == (0,)
  75. assert singles[1] == (1,)
  76. assert singles[2] == (4,)
  77. assert singles[3] == (8,)
  78. def test_adjacent_vertex_indices_2():
  79. chain = PolygonalCircuit((0, 1, 4, 8))
  80. pairs = list(chain.adjacent_vertex_indices(2))
  81. assert len(pairs) == 4
  82. assert pairs[0] == (0, 1)
  83. assert pairs[1] == (1, 4)
  84. assert pairs[2] == (4, 8)
  85. assert pairs[3] == (8, 0)
  86. def test_adjacent_vertex_indices_3():
  87. chain = PolygonalCircuit((0, 1, 4, 8))
  88. triplets = list(chain.adjacent_vertex_indices(3))
  89. assert len(triplets) == 4
  90. assert triplets[0] == (0, 1, 4)
  91. assert triplets[1] == (1, 4, 8)
  92. assert triplets[2] == (4, 8, 0)
  93. assert triplets[3] == (8, 0, 1)
  94. def test_adjacent_vertex_indices_4():
  95. chain = PolygonalCircuit((0, 1, 4, 8))
  96. quadruples = list(chain.adjacent_vertex_indices(4))
  97. assert len(quadruples) == 4
  98. assert quadruples[0] == (0, 1, 4, 8)
  99. assert quadruples[1] == (1, 4, 8, 0)
  100. assert quadruples[2] == (4, 8, 0, 1)
  101. assert quadruples[3] == (8, 0, 1, 4)
  102. def test_adjacent_vertex_indices_5():
  103. chain = PolygonalCircuit((0, 1, 4, 8))
  104. quintuples = list(chain.adjacent_vertex_indices(5))
  105. assert len(quintuples) == 4
  106. assert quintuples[0] == (0, 1, 4, 8, 0)
  107. assert quintuples[1] == (1, 4, 8, 0, 1)
  108. assert quintuples[2] == (4, 8, 0, 1, 4)
  109. assert quintuples[3] == (8, 0, 1, 4, 8)