test_vertex.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 numpy
  18. import pytest
  19. from freesurfer_surface import Vertex
  20. def test_init():
  21. vertex = Vertex(-4.0, 0.5, 21.42)
  22. assert isinstance(vertex, numpy.ndarray)
  23. assert vertex.right == pytest.approx(-4.0)
  24. assert vertex.anterior == pytest.approx(0.5)
  25. assert vertex.superior == pytest.approx(21.42)
  26. def test_init_kwargs():
  27. vertex = Vertex(right=-4.0, superior=21.42, anterior=0.5)
  28. assert vertex.right == pytest.approx(-4.0)
  29. assert vertex.anterior == pytest.approx(0.5)
  30. assert vertex.superior == pytest.approx(21.42)
  31. def test_repr():
  32. vertex = Vertex(right=-4.0, superior=21.42, anterior=0.5)
  33. assert repr(vertex) == "Vertex(right=-4.0, anterior=0.5, superior=21.42)"
  34. def test_add():
  35. assert Vertex(-1.5, 4, 2) + Vertex(2, -4.5, 3) == pytest.approx(
  36. Vertex(0.5, -0.5, 5)
  37. )
  38. def test_mult():
  39. assert Vertex(-1.5, 4, 2) * -3 == pytest.approx(Vertex(4.5, -12, -6))
  40. def test_vars():
  41. attrs = vars(Vertex(-1.5, 4, 2))
  42. assert len(attrs) == 3
  43. assert attrs["right"] == pytest.approx(-1.5)
  44. assert attrs["anterior"] == pytest.approx(4)
  45. assert attrs["superior"] == pytest.approx(2)
  46. @pytest.mark.parametrize(
  47. ("vertex_a", "vertex_b", "expected_distance_mm"),
  48. [
  49. (Vertex(0, 0, 0), Vertex(0, 0, 0), 0),
  50. (Vertex(0, 0, 0), Vertex(1, 0, 0), 1),
  51. (Vertex(0, 0, 0), Vertex(0, 1, 0), 1),
  52. (Vertex(0, 0, 0), Vertex(0, 0, 1), 1),
  53. (Vertex(0, 0, 0), Vertex(1, 1, 0), 2 ** (1 / 2)),
  54. (Vertex(0, 0, 0), Vertex(1, 1, 1), 3 ** (1 / 2)),
  55. (Vertex(1, 2, 3), Vertex(2, 3, 4), 3 ** (1 / 2)),
  56. (Vertex(1, 2, 3), Vertex(5, 8, -1), (16 + 36 + 16) ** (1 / 2)),
  57. (Vertex(0, 0, 0), [Vertex(0, 0, 1), Vertex(0, 0, 2)], [1, 2]),
  58. (Vertex(0, 0, 0), (Vertex(0, 0, 1), Vertex(0, 0, 2)), [1, 2]),
  59. (Vertex(0, 0, 0), numpy.vstack((Vertex(0, 0, 1), Vertex(0, 0, 2))), [1, 2]),
  60. (
  61. Vertex(1, 2, 3),
  62. (Vertex(2, 3, 4), Vertex(3, 4, 5)),
  63. [3 ** (1 / 2), 12 ** (1 / 2)],
  64. ),
  65. (
  66. Vertex(1, 2, 3),
  67. (Vertex(2, 3, 4), Vertex(3, 4, 5), Vertex(3, 4, 6)),
  68. [3 ** (1 / 2), 12 ** (1 / 2), 17 ** (1 / 2)],
  69. ),
  70. ],
  71. )
  72. def test_distance(vertex_a, vertex_b, expected_distance_mm):
  73. assert vertex_a.distance_mm(vertex_b) == pytest.approx(expected_distance_mm)