test_polygonal_circuit.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # freesurfer-surface - Read and Write Surface Files in Freesurfer’s TriangularSurface Format
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import pytest
  18. from freesurfer_surface import PolygonalCircuit, Vertex
  19. def test_init_tuple():
  20. circuit = PolygonalCircuit((1, 2, 3, 5))
  21. assert circuit.vertex_indices == (1, 2, 3, 5)
  22. def test_init_list():
  23. circuit = PolygonalCircuit([1, 2, 3, 5])
  24. assert circuit.vertex_indices == (1, 2, 3, 5)
  25. def test_init_iterator():
  26. circuit = PolygonalCircuit(iter([1, 2, 3, 5]))
  27. assert circuit.vertex_indices == (1, 2, 3, 5)
  28. def test_init_invalid_type():
  29. with pytest.raises(Exception):
  30. PolygonalCircuit((0, 1, 2.0))
  31. with pytest.raises(Exception):
  32. PolygonalCircuit((Vertex(2, 3, 4),))
  33. with pytest.raises(Exception):
  34. PolygonalCircuit((0, 1, Vertex(2, 3, 4)))
  35. @pytest.mark.parametrize(
  36. ("source_vertex_indices", "expected_vertex_indices"),
  37. [
  38. ((1,), (1,)),
  39. ((1, 2), (1, 2)),
  40. ((2, 1), (1, 2)),
  41. ((1, 2, 3), (1, 2, 3)),
  42. ((2, 3, 1), (1, 2, 3)),
  43. ((3, 1, 2), (1, 2, 3)),
  44. ((1, 3, 2), (1, 2, 3)),
  45. ((2, 1, 3), (1, 2, 3)),
  46. ((3, 2, 1), (1, 2, 3)),
  47. ((1, 2, 3, 5), (1, 2, 3, 5)),
  48. ((2, 3, 5, 1), (1, 2, 3, 5)),
  49. ((3, 5, 1, 2), (1, 2, 3, 5)),
  50. ((2, 1, 5, 3), (1, 2, 3, 5)),
  51. ((5, 3, 2, 1), (1, 2, 3, 5)),
  52. ],
  53. )
  54. def test__normalize(source_vertex_indices, expected_vertex_indices):
  55. # pylint: disable=protected-access
  56. circuit = PolygonalCircuit(source_vertex_indices)
  57. assert expected_vertex_indices == circuit._normalize()._vertex_indices
  58. def test_eq():
  59. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((0, 1))
  60. assert PolygonalCircuit((0, 1)) == PolygonalCircuit((1, 0))
  61. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 2))
  62. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((1, 2, 0))
  63. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 0, 1))
  64. # pylint: disable=unneeded-not
  65. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 1, 4))
  66. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 4, 2))
  67. assert not PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((4, 1, 2))
  68. def test_eq_reverse():
  69. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((2, 1, 0))
  70. assert PolygonalCircuit((0, 1, 2)) == PolygonalCircuit((0, 2, 1))
  71. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((4, 2, 1, 0))
  72. assert PolygonalCircuit((0, 1, 2, 4)) == PolygonalCircuit((1, 0, 4, 2))
  73. def test_hash():
  74. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((0, 1, 2)))
  75. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((1, 2, 0)))
  76. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((2, 0, 1)))
  77. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((0, 1, 4)))
  78. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((0, 4, 2)))
  79. assert hash(PolygonalCircuit((0, 1, 2))) != hash(PolygonalCircuit((4, 1, 2)))
  80. def test_hash_reverse():
  81. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((2, 1, 0)))
  82. assert hash(PolygonalCircuit((0, 1, 2))) == hash(PolygonalCircuit((0, 2, 1)))
  83. assert hash(PolygonalCircuit((0, 1, 2, 4))) == hash(PolygonalCircuit((4, 2, 1, 0)))
  84. assert hash(PolygonalCircuit((0, 1, 2, 4))) == hash(PolygonalCircuit((1, 0, 4, 2)))
  85. assert hash(PolygonalCircuit((0, 1, 2, 4))) != hash(PolygonalCircuit((1, 4, 0, 2)))
  86. def test_adjacent_vertex_indices_1():
  87. chain = PolygonalCircuit((0, 1, 4, 8))
  88. singles = list(chain.adjacent_vertex_indices(1))
  89. assert len(singles) == 4
  90. assert singles[0] == (0,)
  91. assert singles[1] == (1,)
  92. assert singles[2] == (4,)
  93. assert singles[3] == (8,)
  94. def test_adjacent_vertex_indices_2():
  95. chain = PolygonalCircuit((0, 1, 4, 8))
  96. pairs = list(chain.adjacent_vertex_indices(2))
  97. assert len(pairs) == 4
  98. assert pairs[0] == (0, 1)
  99. assert pairs[1] == (1, 4)
  100. assert pairs[2] == (4, 8)
  101. assert pairs[3] == (8, 0)
  102. def test_adjacent_vertex_indices_3():
  103. chain = PolygonalCircuit((0, 1, 4, 8))
  104. triplets = list(chain.adjacent_vertex_indices(3))
  105. assert len(triplets) == 4
  106. assert triplets[0] == (0, 1, 4)
  107. assert triplets[1] == (1, 4, 8)
  108. assert triplets[2] == (4, 8, 0)
  109. assert triplets[3] == (8, 0, 1)
  110. def test_adjacent_vertex_indices_4():
  111. chain = PolygonalCircuit((0, 1, 4, 8))
  112. quadruples = list(chain.adjacent_vertex_indices(4))
  113. assert len(quadruples) == 4
  114. assert quadruples[0] == (0, 1, 4, 8)
  115. assert quadruples[1] == (1, 4, 8, 0)
  116. assert quadruples[2] == (4, 8, 0, 1)
  117. assert quadruples[3] == (8, 0, 1, 4)
  118. def test_adjacent_vertex_indices_5():
  119. chain = PolygonalCircuit((0, 1, 4, 8))
  120. quintuples = list(chain.adjacent_vertex_indices(5))
  121. assert len(quintuples) == 4
  122. assert quintuples[0] == (0, 1, 4, 8, 0)
  123. assert quintuples[1] == (1, 4, 8, 0, 1)
  124. assert quintuples[2] == (4, 8, 0, 1, 4)
  125. assert quintuples[3] == (8, 0, 1, 4, 8)