test_main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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('sys.argv', ['', '--delimiter', ',', ANNOTATION_FILE_PATH]):
  24. annotation_labels()
  25. out, err = capsys.readouterr()
  26. assert not err
  27. check_rows(list(csv.reader(io.StringIO(out), delimiter=',')))
  28. def test_annotation_labels_script():
  29. proc_info = subprocess.run(['freesurfer-annotation-labels', ANNOTATION_FILE_PATH],
  30. check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  31. assert not proc_info.stderr
  32. check_rows(list(csv.reader(io.StringIO(proc_info.stdout.decode()),
  33. delimiter='\t')))
  34. def test_unite_surfaces_function(tmpdir, capsys):
  35. surface_b = Surface.read_triangular(SURFACE_FILE_PATH)
  36. surface_b.vertices = []
  37. for i in range(5):
  38. surface_b.add_vertex(Vertex(i, i, i))
  39. surface_b.triangles = [Triangle((0, 1, 3)), Triangle((1, 3, 4))]
  40. surface_b_path = tmpdir.join('b').strpath
  41. surface_b.write_triangular(surface_b_path)
  42. output_path = tmpdir.join('output_path').strpath
  43. with unittest.mock.patch('sys.argv', ['', '--output', output_path,
  44. SURFACE_FILE_PATH, surface_b_path]):
  45. unite_surfaces()
  46. out, err = capsys.readouterr()
  47. assert not out
  48. assert not err
  49. union = Surface.read_triangular(output_path)
  50. assert len(union.vertices) == 155627
  51. assert len(union.triangles) == 311242
  52. assert numpy.allclose(union.vertices[-5:], surface_b.vertices)
  53. def test_unite_surfaces_script(tmpdir):
  54. surface_b = Surface.read_triangular(SURFACE_FILE_PATH)
  55. surface_b.vertices = []
  56. for i in range(5):
  57. surface_b.add_vertex(Vertex(i, i, i))
  58. surface_b.triangles = [Triangle((0, 1, 3)), Triangle((1, 3, 4))]
  59. surface_b_path = tmpdir.join('b').strpath
  60. surface_b.write_triangular(surface_b_path)
  61. output_path = tmpdir.join('output_path').strpath
  62. proc_info = subprocess.run(['unite-freesurfer-surfaces',
  63. '--output', output_path,
  64. SURFACE_FILE_PATH, surface_b_path,
  65. surface_b_path],
  66. check=True,
  67. stdout=subprocess.PIPE,
  68. stderr=subprocess.PIPE)
  69. assert not proc_info.stdout
  70. assert not proc_info.stderr
  71. union = Surface.read_triangular(output_path)
  72. assert len(union.vertices) == 155622 + (5 * 2)
  73. assert len(union.triangles) == 311240 + (2 * 2)