test_polygonal_chain.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_repr():
  42. assert repr(PolygonalChain([])) == "PolygonalChain(vertex_indices=())"
  43. assert repr(PolygonalChain((0, 2, 1))) == "PolygonalChain(vertex_indices=(0, 2, 1))"
  44. assert (
  45. repr(PolygonalChain((0, 2, 1, 4, 3)))
  46. == "PolygonalChain(vertex_indices=(0, 2, 1, 4, 3))"
  47. )
  48. @pytest.mark.parametrize(
  49. ("vertex_indices_a", "vertex_indices_b", "expected_vertex_indices"),
  50. [
  51. ((1, 2, 3), (3, 4), (1, 2, 3, 4)),
  52. ((1, 2, 3), (4, 3), (1, 2, 3, 4)),
  53. ((3, 2, 1), (3, 4), (4, 3, 2, 1)),
  54. ((3, 2, 1), (4, 3), (4, 3, 2, 1)),
  55. ((1,), (1,), (1,)),
  56. ((1, 2), (1,), (1, 2)),
  57. ((1, 2), (2,), (1, 2)),
  58. ((0, 3, 1, 5, 2), (3, 5, 2, 0), (3, 5, 2, 0, 3, 1, 5, 2)),
  59. ((98792, 98807, 98821), (98792, 98793), (98793, 98792, 98807, 98821)),
  60. ],
  61. )
  62. def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
  63. chain = PolygonalChain(vertex_indices_a)
  64. chain.connect(PolygonalChain(vertex_indices_b))
  65. assert PolygonalChain(expected_vertex_indices) == chain
  66. @pytest.mark.parametrize(
  67. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), (2, 4))]
  68. )
  69. def test_connect_fail(vertex_indices_a, vertex_indices_b):
  70. chain = PolygonalChain(vertex_indices_a)
  71. with pytest.raises(PolygonalChainsNotOverlapingError):
  72. chain.connect(PolygonalChain(vertex_indices_b))
  73. @pytest.mark.parametrize(
  74. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), ()), ((), (3, 4))]
  75. )
  76. def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
  77. chain = PolygonalChain(vertex_indices_a)
  78. with pytest.raises(Exception):
  79. chain.connect(PolygonalChain(vertex_indices_b))
  80. def test_adjacent_vertex_indices_1():
  81. chain = PolygonalChain((0, 1, 4, 8))
  82. pairs = list(chain.adjacent_vertex_indices(1))
  83. assert len(pairs) == 4
  84. assert pairs[0] == (0,)
  85. assert pairs[1] == (1,)
  86. assert pairs[2] == (4,)
  87. assert pairs[3] == (8,)
  88. def test_adjacent_vertex_indices_2():
  89. chain = PolygonalChain((0, 1, 4, 8))
  90. pairs = list(chain.adjacent_vertex_indices(2))
  91. assert len(pairs) == 3
  92. assert pairs[0] == (0, 1)
  93. assert pairs[1] == (1, 4)
  94. assert pairs[2] == (4, 8)
  95. def test_adjacent_vertex_indices_3():
  96. chain = PolygonalChain((0, 1, 4, 8))
  97. pairs = list(chain.adjacent_vertex_indices(3))
  98. assert len(pairs) == 2
  99. assert pairs[0] == (0, 1, 4)
  100. assert pairs[1] == (1, 4, 8)
  101. def test_segments():
  102. chain = PolygonalChain((0, 1, 4, 8))
  103. segments = list(chain.segments())
  104. assert len(segments) == 3
  105. assert segments[0] == LineSegment((0, 1))
  106. assert segments[1] == LineSegment((1, 4))
  107. assert segments[2] == LineSegment((4, 8))