test_surface.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import datetime
  2. import pytest
  3. from freesurfer_surface import setlocale, Vertex, Triangle, _LineSegment, \
  4. Annotation, Surface
  5. from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
  6. def test_read_triangular():
  7. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  8. assert surface.creator == b'fabianpeter'
  9. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  10. assert len(surface.vertices) == 155622
  11. assert len(surface.triangles) == 311240
  12. assert not surface.using_old_real_ras
  13. assert surface.volume_geometry_info == (
  14. b'valid = 1 # volume info valid\n',
  15. b'filename = ../mri/filled-pretess255.mgz\n',
  16. b'volume = 256 256 256\n',
  17. b'voxelsize = 1.000000000000000e+00 1.000000000000000e+00 1.000000000000000e+00\n',
  18. b'xras = -1.000000000000000e+00 0.000000000000000e+00 1.862645149230957e-09\n',
  19. b'yras = 0.000000000000000e+00 -6.655682227574289e-09 -1.000000000000000e+00\n',
  20. b'zras = 0.000000000000000e+00 1.000000000000000e+00 -8.300048648379743e-09\n',
  21. b'cras = -2.773597717285156e+00 1.566547393798828e+01 -7.504364013671875e+00\n')
  22. assert surface.command_lines == [
  23. b'mris_remove_intersection ../surf/lh.orig ../surf/lh.orig'
  24. b' ProgramVersion: $Name: stable6 $'
  25. b' TimeStamp: 2019/05/09-17:42:36-GMT'
  26. b' BuildTimeStamp: Jan 18 2017 16:38:58'
  27. b' CVS: $Id: mris_remove_intersection.c,v 1.6 2011/03/02 00:04:32 nicks Exp $'
  28. b' User: fabianpeter'
  29. b' Machine: host12345'
  30. b' Platform: Linux'
  31. b' PlatformVersion: 4.15.0-46-generic'
  32. b' CompilerName: GCC'
  33. b' CompilerVersion: 40400'
  34. b' ',
  35. b'mris_make_surfaces -orig_white white.preaparc -orig_pial white.preaparc'
  36. b' -aseg ../mri/aseg.presurf -mgz -T1 brain.finalsurfs'
  37. b' fabian20190509 lh ProgramVersion: $Name: $'
  38. b' TimeStamp: 2019/05/09-20:27:28-GMT'
  39. b' BuildTimeStamp: Jan 18 2017 16:38:58'
  40. b' CVS: $Id: mris_make_surfaces.c,v 1.164.2.4 2016/12/13 22:26:32 zkaufman Exp $'
  41. b' User: fabianpeter'
  42. b' Machine: host12345'
  43. b' Platform: Linux'
  44. b' PlatformVersion: 4.15.0-46-generic'
  45. b' CompilerName: GCC'
  46. b' CompilerVersion: 40400'
  47. b' ']
  48. def test_read_triangular_locale():
  49. with setlocale('de_AT.utf8'):
  50. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  51. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  52. @pytest.mark.parametrize(('creation_datetime', 'expected_str'), [
  53. (datetime.datetime(2019, 5, 9, 22, 37, 41), b'Thu May 9 22:37:41 2019'),
  54. (datetime.datetime(2019, 4, 24, 23, 29, 22), b'Wed Apr 24 23:29:22 2019'),
  55. ])
  56. def test_triangular_creation_datetime_strftime(creation_datetime, expected_str):
  57. surface = Surface()
  58. surface.creation_datetime = creation_datetime
  59. # pylint: disable=protected-access
  60. assert expected_str == surface._triangular_creation_datetime_strftime()
  61. def test_read_write_triangular_same(tmpdir):
  62. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  63. output_path = tmpdir.join('surface')
  64. surface.write_triangular(output_path,
  65. creation_datetime=surface.creation_datetime)
  66. with open(output_path, 'rb') as output_file:
  67. with open(SURFACE_FILE_PATH, 'rb') as expected_file:
  68. assert expected_file.read() == output_file.read()
  69. def test_write_read_triangular_same(tmpdir):
  70. expected_surface = Surface()
  71. expected_surface.creator = b'pytest'
  72. expected_surface.creation_datetime = datetime.datetime.now().replace(microsecond=0)
  73. expected_surface.vertices = [Vertex(0.0, 0.0, 0.0),
  74. Vertex(1.0, 2.0, 3.0),
  75. Vertex(2.0, 4.0, 6.0),
  76. Vertex(3.0, 5.0, 7.0)]
  77. expected_surface.triangles = [Triangle((0, 1, 2)),
  78. Triangle((0, 1, 3)),
  79. Triangle((3, 2, 1))]
  80. expected_surface.using_old_real_ras = False
  81. expected_surface.volume_geometry_info = tuple(b'?\n' for _ in range(8))
  82. expected_surface.command_lines = [b'?', b'!']
  83. output_path = tmpdir.join('surface')
  84. expected_surface.write_triangular(output_path,
  85. creation_datetime=expected_surface.creation_datetime)
  86. resulted_surface = Surface.read_triangular(output_path)
  87. assert vars(expected_surface) == vars(resulted_surface)
  88. def test_write_triangular_same_locale(tmpdir):
  89. surface = Surface()
  90. surface.creator = b'pytest'
  91. surface.volume_geometry_info = tuple(b'?' for _ in range(8))
  92. creation_datetime = datetime.datetime(2018, 12, 31, 21, 42)
  93. output_path = tmpdir.join('surface')
  94. with setlocale('de_AT.utf8'):
  95. surface.write_triangular(output_path, creation_datetime=creation_datetime)
  96. resulted_surface = Surface.read_triangular(output_path)
  97. assert resulted_surface.creation_datetime == creation_datetime
  98. with open(output_path, 'rb') as output_file:
  99. assert output_file.read().split(b' on ')[1].startswith(b'Mon Dec 31 21:42:00 2018\n')
  100. def test_load_annotation():
  101. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  102. assert not surface.annotation
  103. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  104. assert isinstance(surface.annotation, Annotation)
  105. assert len(surface.annotation.vertex_label_index) == 155622
  106. assert surface.annotation.vertex_label_index[0] == 5
  107. def test_add_vertex():
  108. surface = Surface()
  109. assert not surface.vertices
  110. assert surface.add_vertex(Vertex(1.0, 1.5, 2.0)) == 0
  111. assert len(surface.vertices) == 1
  112. assert surface.vertices[0].anterior == pytest.approx(1.5)
  113. assert surface.add_vertex(Vertex(-3.0, 0.0, 4.0)) == 1
  114. assert len(surface.vertices) == 2
  115. assert surface.vertices[1].right == pytest.approx(-3.0)
  116. @pytest.mark.parametrize(('vertices_coords', 'expected_extra_vertex_coords'), [
  117. (((0, 0, 0), (2, 4, 0), (2, 4, 3)), (0, 0, 3)),
  118. (((1, 1, 0), (3, 5, 0), (3, 5, 3)), (1, 1, 3)),
  119. (((1, 1, 7), (3, 5, 7), (3, 5, 3)), (1, 1, 3)),
  120. (((1, 1, 1), (3, 5, 7), (3, 5, 9)), (1, 1, 3)),
  121. (((3, 5, 7), (1, 1, 1), (1, 1, 3)), (3, 5, 9)),
  122. ])
  123. def test_add_rectangle(vertices_coords, expected_extra_vertex_coords):
  124. surface = Surface()
  125. for vertex_coords in vertices_coords:
  126. surface.add_vertex(Vertex(*(float(c) for c in vertex_coords)))
  127. surface.add_rectangle(range(3))
  128. assert tuple(surface.vertices[3]) == pytest.approx(expected_extra_vertex_coords)
  129. assert len(surface.triangles) == 2
  130. assert surface.triangles[0].vertex_indices == (0, 1, 2)
  131. assert surface.triangles[1].vertex_indices == (2, 3, 0)
  132. def test__find_label_border_segments():
  133. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  134. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  135. precentral_label, = filter(lambda l: l.name == 'precentral',
  136. surface.annotation.labels.values())
  137. # pylint: disable=protected-access
  138. border_segments = set(surface._find_label_border_segments(precentral_label))
  139. assert len(border_segments) == 417
  140. assert _LineSegment((33450, 32065)) in border_segments
  141. assert _LineSegment((33454, 33450)) in border_segments
  142. for border_vertex_index in [33450, 33454, 32065]:
  143. assert surface.annotation.vertex_label_index[border_vertex_index] == precentral_label.index
  144. for other_vertex_index in [32064, 33449, 33455, 33449, 33455]:
  145. assert _LineSegment((other_vertex_index, border_vertex_index)) not in border_segments
  146. assert _LineSegment((border_vertex_index, other_vertex_index)) not in border_segments
  147. def test_find_label_border_polygonal_chains():
  148. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  149. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  150. precentral_label, = filter(lambda l: l.name == 'precentral',
  151. surface.annotation.labels.values())
  152. border_chain, = surface.find_label_border_polygonal_chains(precentral_label)
  153. vertex_indices = list(border_chain.vertex_indices)
  154. assert len(vertex_indices) == 418
  155. min_index = vertex_indices.index(min(vertex_indices))
  156. vertex_indices_normalized = vertex_indices[min_index:] + vertex_indices[:min_index]
  157. assert vertex_indices_normalized[:4] == [32065, 32072, 32073, 32080]
  158. assert vertex_indices_normalized[-4:] == [36281, 34870, 33454, 33450]