test_main.py 4.0 KB

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