test_surface.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import datetime
  2. import os
  3. import pytest
  4. from freesurfer_surface import setlocale, Surface, Vertex
  5. SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
  6. SURFACE_FILE_PATH = os.path.join(SUBJECTS_DIR, 'fabian', 'surf', 'lh.pial')
  7. def test_read_triangular():
  8. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  9. assert surface.creator == b'fabianpeter'
  10. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  11. assert len(surface.vertices) == 155622
  12. assert len(surface.triangles_vertex_indices) == 311240
  13. assert not surface.using_old_real_ras
  14. assert surface.volume_geometry_info == (
  15. b'valid = 1 # volume info valid\n',
  16. b'filename = ../mri/filled-pretess255.mgz\n',
  17. b'volume = 256 256 256\n',
  18. b'voxelsize = 1.000000000000000e+00 1.000000000000000e+00 1.000000000000000e+00\n',
  19. b'xras = -1.000000000000000e+00 0.000000000000000e+00 1.862645149230957e-09\n',
  20. b'yras = 0.000000000000000e+00 -6.655682227574289e-09 -1.000000000000000e+00\n',
  21. b'zras = 0.000000000000000e+00 1.000000000000000e+00 -8.300048648379743e-09\n',
  22. b'cras = -2.773597717285156e+00 1.566547393798828e+01 -7.504364013671875e+00\n')
  23. assert surface.command_lines == [
  24. b'mris_remove_intersection ../surf/lh.orig ../surf/lh.orig'
  25. b' ProgramVersion: $Name: stable6 $'
  26. b' TimeStamp: 2019/05/09-17:42:36-GMT'
  27. b' BuildTimeStamp: Jan 18 2017 16:38:58'
  28. b' CVS: $Id: mris_remove_intersection.c,v 1.6 2011/03/02 00:04:32 nicks Exp $'
  29. b' User: fabianpeter'
  30. b' Machine: host12345'
  31. b' Platform: Linux'
  32. b' PlatformVersion: 4.15.0-46-generic'
  33. b' CompilerName: GCC'
  34. b' CompilerVersion: 40400'
  35. b' ',
  36. b'mris_make_surfaces -orig_white white.preaparc -orig_pial white.preaparc'
  37. b' -aseg ../mri/aseg.presurf -mgz -T1 brain.finalsurfs'
  38. b' fabian20190509 lh ProgramVersion: $Name: $'
  39. b' TimeStamp: 2019/05/09-20:27:28-GMT'
  40. b' BuildTimeStamp: Jan 18 2017 16:38:58'
  41. b' CVS: $Id: mris_make_surfaces.c,v 1.164.2.4 2016/12/13 22:26:32 zkaufman Exp $'
  42. b' User: fabianpeter'
  43. b' Machine: host12345'
  44. b' Platform: Linux'
  45. b' PlatformVersion: 4.15.0-46-generic'
  46. b' CompilerName: GCC'
  47. b' CompilerVersion: 40400'
  48. b' ']
  49. def test_read_triangular_locale():
  50. with setlocale('de_AT.utf8'):
  51. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  52. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  53. @pytest.mark.parametrize(('creation_datetime', 'expected_str'), [
  54. (datetime.datetime(2019, 5, 9, 22, 37, 41), b'Thu May 9 22:37:41 2019'),
  55. (datetime.datetime(2019, 4, 24, 23, 29, 22), b'Wed Apr 24 23:29:22 2019'),
  56. ])
  57. def test_triangular_creation_datetime_strftime(creation_datetime, expected_str):
  58. surface = Surface()
  59. surface.creation_datetime = creation_datetime
  60. # pylint: disable=protected-access
  61. assert expected_str == surface._triangular_creation_datetime_strftime()
  62. def test_read_write_triangular_same(tmpdir):
  63. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  64. output_path = tmpdir.join('surface')
  65. surface.write_triangular(output_path,
  66. creation_datetime=surface.creation_datetime)
  67. with open(output_path, 'rb') as output_file:
  68. with open(SURFACE_FILE_PATH, 'rb') as expected_file:
  69. assert expected_file.read() == output_file.read()
  70. def test_write_read_triangular_same(tmpdir):
  71. expected_surface = Surface()
  72. expected_surface.creator = b'pytest'
  73. expected_surface.creation_datetime = datetime.datetime.now().replace(microsecond=0)
  74. expected_surface.vertices = [Vertex(0.0, 0.0, 0.0),
  75. Vertex(1.0, 2.0, 3.0),
  76. Vertex(2.0, 4.0, 6.0),
  77. Vertex(3.0, 5.0, 7.0)]
  78. expected_surface.triangles_vertex_indices = [(0, 1, 2),
  79. (0, 1, 3),
  80. (3, 2, 1)]
  81. expected_surface.using_old_real_ras = False
  82. expected_surface.volume_geometry_info = tuple(b'?\n' for _ in range(8))
  83. expected_surface.command_lines = [b'?', b'!']
  84. output_path = tmpdir.join('surface')
  85. expected_surface.write_triangular(output_path,
  86. creation_datetime=expected_surface.creation_datetime)
  87. resulted_surface = Surface.read_triangular(output_path)
  88. assert vars(expected_surface) == vars(resulted_surface)
  89. def test_write_triangular_same_locale(tmpdir):
  90. surface = Surface()
  91. surface.creator = b'pytest'
  92. surface.volume_geometry_info = tuple(b'?' for _ in range(8))
  93. creation_datetime = datetime.datetime(2018, 12, 31, 21, 42)
  94. output_path = tmpdir.join('surface')
  95. with setlocale('de_AT.utf8'):
  96. surface.write_triangular(output_path, creation_datetime=creation_datetime)
  97. resulted_surface = Surface.read_triangular(output_path)
  98. assert resulted_surface.creation_datetime == creation_datetime
  99. with open(output_path, 'rb') as output_file:
  100. assert output_file.read().split(b' on ')[1].startswith(b'Mon Dec 31 21:42:00 2018\n')
  101. def test_load_annotation():
  102. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  103. assert not surface.vertex_annotation_values
  104. surface.load_annotation(os.path.join(SUBJECTS_DIR, 'fabian', 'label', 'lh.aparc.annot'))
  105. assert len(surface.vertex_annotation_values) == 155622
  106. assert surface.vertex_annotation_values[0] == (((100 << 8) + 20) << 8) + 220
  107. assert surface.vertex_annotation_values[1] == (((100 << 8) + 20) << 8) + 220
  108. assert surface.vertex_annotation_values[42] == (((140 << 8) + 30) << 8) + 20
  109. def test_add_vertex():
  110. surface = Surface()
  111. assert not surface.vertices
  112. assert surface.add_vertex(Vertex(1.0, 1.5, 2.0)) == 0
  113. assert len(surface.vertices) == 1
  114. assert surface.vertices[0].anterior == pytest.approx(1.5)
  115. assert surface.add_vertex(Vertex(-3.0, 0.0, 4.0)) == 1
  116. assert len(surface.vertices) == 2
  117. assert surface.vertices[1].right == pytest.approx(-3.0)