test_polygonal_chain.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. def test_eq():
  15. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 2))
  16. # pylint: disable=unneeded-not
  17. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 4))
  18. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 4, 2))
  19. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((4, 1, 2))
  20. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((1, 2, 3, 4))
  21. def test_repr():
  22. assert repr(PolygonalChain([])) == "PolygonalChain(vertex_indices=())"
  23. assert repr(PolygonalChain((0, 2, 1))) == "PolygonalChain(vertex_indices=(0, 2, 1))"
  24. assert (
  25. repr(PolygonalChain((0, 2, 1, 4, 3)))
  26. == "PolygonalChain(vertex_indices=(0, 2, 1, 4, 3))"
  27. )
  28. @pytest.mark.parametrize(
  29. ("vertex_indices_a", "vertex_indices_b", "expected_vertex_indices"),
  30. [
  31. ((1, 2, 3), (3, 4), (1, 2, 3, 4)),
  32. ((1, 2, 3), (4, 3), (1, 2, 3, 4)),
  33. ((3, 2, 1), (3, 4), (4, 3, 2, 1)),
  34. ((3, 2, 1), (4, 3), (4, 3, 2, 1)),
  35. ((1,), (1,), (1,)),
  36. ((1, 2), (1,), (1, 2)),
  37. ((1, 2), (2,), (1, 2)),
  38. ((0, 3, 1, 5, 2), (3, 5, 2, 0), (3, 5, 2, 0, 3, 1, 5, 2)),
  39. ((98792, 98807, 98821), (98792, 98793), (98793, 98792, 98807, 98821)),
  40. ],
  41. )
  42. def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
  43. chain = PolygonalChain(vertex_indices_a)
  44. chain.connect(PolygonalChain(vertex_indices_b))
  45. assert PolygonalChain(expected_vertex_indices) == chain
  46. @pytest.mark.parametrize(
  47. ("vertex_indices_a", "vertex_indices_b"),
  48. [
  49. ((1, 2, 3), (2, 4)),
  50. ],
  51. )
  52. def test_connect_fail(vertex_indices_a, vertex_indices_b):
  53. chain = PolygonalChain(vertex_indices_a)
  54. with pytest.raises(PolygonalChainsNotOverlapingError):
  55. chain.connect(PolygonalChain(vertex_indices_b))
  56. @pytest.mark.parametrize(
  57. ("vertex_indices_a", "vertex_indices_b"),
  58. [
  59. ((1, 2, 3), ()),
  60. ((), (3, 4)),
  61. ],
  62. )
  63. def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
  64. chain = PolygonalChain(vertex_indices_a)
  65. with pytest.raises(Exception):
  66. chain.connect(PolygonalChain(vertex_indices_b))
  67. def test_adjacent_vertex_indices_1():
  68. chain = PolygonalChain((0, 1, 4, 8))
  69. pairs = list(chain.adjacent_vertex_indices(1))
  70. assert len(pairs) == 4
  71. assert pairs[0] == (0,)
  72. assert pairs[1] == (1,)
  73. assert pairs[2] == (4,)
  74. assert pairs[3] == (8,)
  75. def test_adjacent_vertex_indices_2():
  76. chain = PolygonalChain((0, 1, 4, 8))
  77. pairs = list(chain.adjacent_vertex_indices(2))
  78. assert len(pairs) == 3
  79. assert pairs[0] == (0, 1)
  80. assert pairs[1] == (1, 4)
  81. assert pairs[2] == (4, 8)
  82. def test_adjacent_vertex_indices_3():
  83. chain = PolygonalChain((0, 1, 4, 8))
  84. pairs = list(chain.adjacent_vertex_indices(3))
  85. assert len(pairs) == 2
  86. assert pairs[0] == (0, 1, 4)
  87. assert pairs[1] == (1, 4, 8)
  88. def test_segments():
  89. chain = PolygonalChain((0, 1, 4, 8))
  90. segments = list(chain.segments())
  91. assert len(segments) == 3
  92. assert segments[0] == LineSegment((0, 1))
  93. assert segments[1] == LineSegment((1, 4))
  94. assert segments[2] == LineSegment((4, 8))