test_polygonal_chain.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import pytest
  2. from freesurfer_surface import (
  3. LineSegment,
  4. PolygonalChain,
  5. PolygonalChainsNotOverlapingError,
  6. )
  7. def test_init():
  8. chain = PolygonalChain((0, 21, 42, 84))
  9. assert tuple(chain.vertex_indices) == (0, 21, 42, 84)
  10. def test_reassign_vertex_indices():
  11. chain = PolygonalChain((0, 21, 42, 84))
  12. chain.vertex_indices = (1, 2, 3, 4)
  13. assert tuple(chain.vertex_indices) == (1, 2, 3, 4)
  14. @pytest.mark.parametrize(
  15. ("indices_init", "indices_normalized"),
  16. (
  17. ([0, 3], [0, 3]),
  18. ([3, 0], [0, 3]),
  19. ([0, 3, 2, 4], [0, 3, 2, 4]),
  20. ([0, 4, 2, 3], [0, 3, 2, 4]),
  21. ([2, 3, 0, 4], [0, 3, 2, 4]),
  22. ([2, 4, 0, 3], [0, 3, 2, 4]),
  23. ([3, 0, 4, 2], [0, 3, 2, 4]),
  24. ([3, 2, 4, 0], [0, 3, 2, 4]),
  25. ([4, 0, 3, 2], [0, 3, 2, 4]),
  26. ([4, 2, 3, 0], [0, 3, 2, 4]),
  27. ),
  28. )
  29. def test_normalized(indices_init, indices_normalized):
  30. assert (
  31. list(PolygonalChain(indices_init).normalized().vertex_indices)
  32. == indices_normalized
  33. )
  34. def test_eq():
  35. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 2))
  36. # pylint: disable=unneeded-not
  37. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 4))
  38. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 4, 2))
  39. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((4, 1, 2))
  40. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((1, 2, 3, 4))
  41. def test_eq_normalized():
  42. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 2, 1))
  43. assert PolygonalChain((1, 0, 2)) == PolygonalChain((0, 2, 1))
  44. assert PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 4, 2))
  45. # pylint: disable=unneeded-not
  46. assert not PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 2, 4))
  47. def test_repr():
  48. assert repr(PolygonalChain([])) == "PolygonalChain(vertex_indices=())"
  49. assert repr(PolygonalChain((0, 2, 1))) == "PolygonalChain(vertex_indices=(0, 2, 1))"
  50. assert (
  51. repr(PolygonalChain((0, 2, 1, 4, 3)))
  52. == "PolygonalChain(vertex_indices=(0, 2, 1, 4, 3))"
  53. )
  54. @pytest.mark.parametrize(
  55. ("vertex_indices_a", "vertex_indices_b", "expected_vertex_indices"),
  56. [
  57. ((1, 2, 3), (3, 4), (1, 2, 3, 4)),
  58. ((1, 2, 3), (4, 3), (1, 2, 3, 4)),
  59. ((3, 2, 1), (3, 4), (4, 3, 2, 1)),
  60. ((3, 2, 1), (4, 3), (4, 3, 2, 1)),
  61. ((1,), (1,), (1,)),
  62. ((1, 2), (1,), (1, 2)),
  63. ((1, 2), (2,), (1, 2)),
  64. ((0, 3, 1, 5, 2), (3, 5, 2, 0), (3, 5, 2, 0, 3, 1, 5, 2)),
  65. ((98792, 98807, 98821), (98792, 98793), (98793, 98792, 98807, 98821)),
  66. ],
  67. )
  68. def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
  69. chain = PolygonalChain(vertex_indices_a)
  70. chain.connect(PolygonalChain(vertex_indices_b))
  71. assert PolygonalChain(expected_vertex_indices) == chain
  72. @pytest.mark.parametrize(
  73. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), (2, 4))]
  74. )
  75. def test_connect_fail(vertex_indices_a, vertex_indices_b):
  76. chain = PolygonalChain(vertex_indices_a)
  77. with pytest.raises(PolygonalChainsNotOverlapingError):
  78. chain.connect(PolygonalChain(vertex_indices_b))
  79. @pytest.mark.parametrize(
  80. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), ()), ((), (3, 4))]
  81. )
  82. def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
  83. chain = PolygonalChain(vertex_indices_a)
  84. with pytest.raises(Exception):
  85. chain.connect(PolygonalChain(vertex_indices_b))
  86. def test_adjacent_vertex_indices_1():
  87. chain = PolygonalChain((0, 1, 4, 8))
  88. pairs = list(chain.adjacent_vertex_indices(1))
  89. assert len(pairs) == 4
  90. assert pairs[0] == (0,)
  91. assert pairs[1] == (1,)
  92. assert pairs[2] == (4,)
  93. assert pairs[3] == (8,)
  94. def test_adjacent_vertex_indices_2():
  95. chain = PolygonalChain((0, 1, 4, 8))
  96. pairs = list(chain.adjacent_vertex_indices(2))
  97. assert len(pairs) == 3
  98. assert pairs[0] == (0, 1)
  99. assert pairs[1] == (1, 4)
  100. assert pairs[2] == (4, 8)
  101. def test_adjacent_vertex_indices_3():
  102. chain = PolygonalChain((0, 1, 4, 8))
  103. pairs = list(chain.adjacent_vertex_indices(3))
  104. assert len(pairs) == 2
  105. assert pairs[0] == (0, 1, 4)
  106. assert pairs[1] == (1, 4, 8)
  107. def test_segments():
  108. chain = PolygonalChain((0, 1, 4, 8))
  109. segments = list(chain.segments())
  110. assert len(segments) == 3
  111. assert segments[0] == LineSegment((0, 1))
  112. assert segments[1] == LineSegment((1, 4))
  113. assert segments[2] == LineSegment((4, 8))