test_main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import csv
  2. import io
  3. import subprocess
  4. import typing
  5. import unittest.mock
  6. import numpy
  7. from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
  8. from freesurfer_surface import Surface, Triangle, Vertex
  9. from freesurfer_surface.__main__ import annotation_labels, unite_surfaces
  10. def check_rows(csv_rows: typing.List[str]):
  11. assert len(csv_rows) == 36 + 1
  12. assert csv_rows[0] == ["index", "color", "name"]
  13. assert csv_rows[1] == ["0", "#190519", "unknown"]
  14. assert csv_rows[23] == ["22", "#dc1414", "postcentral"]
  15. assert csv_rows[25] == ["24", "#3c14dc", "precentral"]
  16. def test_annotation_labels_function(capsys):
  17. with unittest.mock.patch("sys.argv", ["", ANNOTATION_FILE_PATH]):
  18. annotation_labels()
  19. out, err = capsys.readouterr()
  20. assert not err
  21. check_rows(list(csv.reader(io.StringIO(out), delimiter="\t")))
  22. def test_annotation_labels_function_delimiter(capsys):
  23. with unittest.mock.patch(
  24. "sys.argv", ["", "--delimiter", ",", ANNOTATION_FILE_PATH]
  25. ):
  26. annotation_labels()
  27. out, err = capsys.readouterr()
  28. assert not err
  29. check_rows(list(csv.reader(io.StringIO(out), delimiter=",")))
  30. def test_annotation_labels_script():
  31. proc_info = subprocess.run(
  32. ["freesurfer-annotation-labels", ANNOTATION_FILE_PATH],
  33. check=True,
  34. stdout=subprocess.PIPE,
  35. stderr=subprocess.PIPE,
  36. )
  37. assert not proc_info.stderr
  38. check_rows(list(csv.reader(io.StringIO(proc_info.stdout.decode()), delimiter="\t")))
  39. def test_unite_surfaces_function(tmpdir, capsys):
  40. surface_b = Surface.read_triangular(SURFACE_FILE_PATH)
  41. surface_b.vertices = []
  42. for i in range(5):
  43. surface_b.add_vertex(Vertex(i, i, i))
  44. surface_b.triangles = [Triangle((0, 1, 3)), Triangle((1, 3, 4))]
  45. surface_b_path = tmpdir.join("b").strpath
  46. surface_b.write_triangular(surface_b_path)
  47. output_path = tmpdir.join("output_path").strpath
  48. with unittest.mock.patch(
  49. "sys.argv", ["", "--output", output_path, SURFACE_FILE_PATH, surface_b_path]
  50. ):
  51. unite_surfaces()
  52. out, err = capsys.readouterr()
  53. assert not out
  54. assert not err
  55. union = Surface.read_triangular(output_path)
  56. assert len(union.vertices) == 155627
  57. assert len(union.triangles) == 311242
  58. assert numpy.allclose(union.vertices[-5:], surface_b.vertices)
  59. def test_unite_surfaces_script(tmpdir):
  60. surface_b = Surface.read_triangular(SURFACE_FILE_PATH)
  61. surface_b.vertices = []
  62. for i in range(5):
  63. surface_b.add_vertex(Vertex(i, i, i))
  64. surface_b.triangles = [Triangle((0, 1, 3)), Triangle((1, 3, 4))]
  65. surface_b_path = tmpdir.join("b").strpath
  66. surface_b.write_triangular(surface_b_path)
  67. output_path = tmpdir.join("output_path").strpath
  68. proc_info = subprocess.run(
  69. [
  70. "unite-freesurfer-surfaces",
  71. "--output",
  72. output_path,
  73. SURFACE_FILE_PATH,
  74. surface_b_path,
  75. surface_b_path,
  76. ],
  77. check=True,
  78. stdout=subprocess.PIPE,
  79. stderr=subprocess.PIPE,
  80. )
  81. assert not proc_info.stdout
  82. assert not proc_info.stderr
  83. union = Surface.read_triangular(output_path)
  84. assert len(union.vertices) == 155622 + (5 * 2)
  85. assert len(union.triangles) == 311240 + (2 * 2)