test_vertex.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import numpy
  2. import pytest
  3. from freesurfer_surface import Vertex
  4. def test_init():
  5. vertex = Vertex(-4.0, 0.5, 21.42)
  6. assert isinstance(vertex, numpy.ndarray)
  7. assert vertex.right == pytest.approx(-4.0)
  8. assert vertex.anterior == pytest.approx(0.5)
  9. assert vertex.superior == pytest.approx(21.42)
  10. def test_init_kwargs():
  11. vertex = Vertex(right=-4.0, superior=21.42, anterior=0.5)
  12. assert vertex.right == pytest.approx(-4.0)
  13. assert vertex.anterior == pytest.approx(0.5)
  14. assert vertex.superior == pytest.approx(21.42)
  15. def test_repr():
  16. vertex = Vertex(right=-4.0, superior=21.42, anterior=0.5)
  17. assert repr(vertex) == "Vertex(right=-4.0, anterior=0.5, superior=21.42)"
  18. def test_add():
  19. assert Vertex(-1.5, 4, 2) + Vertex(2, -4.5, 3) == pytest.approx(
  20. Vertex(0.5, -0.5, 5)
  21. )
  22. def test_mult():
  23. assert Vertex(-1.5, 4, 2) * -3 == pytest.approx(Vertex(4.5, -12, -6))
  24. def test_vars():
  25. attrs = vars(Vertex(-1.5, 4, 2))
  26. assert len(attrs) == 3
  27. assert attrs["right"] == pytest.approx(-1.5)
  28. assert attrs["anterior"] == pytest.approx(4)
  29. assert attrs["superior"] == pytest.approx(2)
  30. @pytest.mark.parametrize(
  31. ("vertex_a", "vertex_b", "expected_distance_mm"),
  32. [
  33. (Vertex(0, 0, 0), Vertex(0, 0, 0), 0),
  34. (Vertex(0, 0, 0), Vertex(1, 0, 0), 1),
  35. (Vertex(0, 0, 0), Vertex(0, 1, 0), 1),
  36. (Vertex(0, 0, 0), Vertex(0, 0, 1), 1),
  37. (Vertex(0, 0, 0), Vertex(1, 1, 0), 2 ** (1 / 2)),
  38. (Vertex(0, 0, 0), Vertex(1, 1, 1), 3 ** (1 / 2)),
  39. (Vertex(1, 2, 3), Vertex(2, 3, 4), 3 ** (1 / 2)),
  40. (Vertex(1, 2, 3), Vertex(5, 8, -1), (16 + 36 + 16) ** (1 / 2)),
  41. (Vertex(0, 0, 0), [Vertex(0, 0, 1), Vertex(0, 0, 2)], [1, 2]),
  42. (Vertex(0, 0, 0), (Vertex(0, 0, 1), Vertex(0, 0, 2)), [1, 2]),
  43. (Vertex(0, 0, 0), numpy.vstack((Vertex(0, 0, 1), Vertex(0, 0, 2))), [1, 2]),
  44. (
  45. Vertex(1, 2, 3),
  46. (Vertex(2, 3, 4), Vertex(3, 4, 5)),
  47. [3 ** (1 / 2), 12 ** (1 / 2)],
  48. ),
  49. (
  50. Vertex(1, 2, 3),
  51. (Vertex(2, 3, 4), Vertex(3, 4, 5), Vertex(3, 4, 6)),
  52. [3 ** (1 / 2), 12 ** (1 / 2), 17 ** (1 / 2)],
  53. ),
  54. ],
  55. )
  56. def test_distance(vertex_a, vertex_b, expected_distance_mm):
  57. assert vertex_a.distance_mm(vertex_b) == pytest.approx(expected_distance_mm)