test_surface.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 \
  10. == datetime.datetime(2019, 5, 9, 22, 37, 41)
  11. assert len(surface.vertices) == 155622
  12. assert len(surface.triangles) == 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 \
  53. == datetime.datetime(2019, 5, 9, 22, 37, 41)
  54. @pytest.mark.parametrize(('creation_datetime', 'expected_str'), [
  55. (datetime.datetime(2019, 5, 9, 22, 37, 41), b'Thu May 9 22:37:41 2019'),
  56. (datetime.datetime(2019, 4, 24, 23, 29, 22), b'Wed Apr 24 23:29:22 2019'),
  57. ])
  58. def test_triangular_creation_datetime_strftime(creation_datetime, expected_str):
  59. surface = Surface()
  60. surface.creation_datetime = creation_datetime
  61. # pylint: disable=protected-access
  62. assert expected_str == surface._triangular_creation_datetime_strftime()
  63. def test_read_write_triangular_same(tmpdir):
  64. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  65. output_path = tmpdir.join('surface')
  66. surface.write_triangular(output_path,
  67. creation_datetime=surface.creation_datetime)
  68. with open(output_path, 'rb') as output_file:
  69. with open(SURFACE_FILE_PATH, 'rb') as expected_file:
  70. assert expected_file.read() == output_file.read()
  71. def test_write_read_triangular_same(tmpdir):
  72. expected_surface = Surface()
  73. expected_surface.creator = b'pytest'
  74. expected_surface.creation_datetime = datetime.datetime.now().replace(microsecond=0)
  75. expected_surface.vertices = [Vertex(0.0, 0.0, 0.0),
  76. Vertex(1.0, 2.0, 3.0),
  77. Vertex(2.0, 4.0, 6.0),
  78. Vertex(3.0, 5.0, 7.0)]
  79. expected_surface.triangles = [Triangle((0, 1, 2)),
  80. Triangle((0, 1, 3)),
  81. Triangle((3, 2, 1))]
  82. expected_surface.using_old_real_ras = False
  83. expected_surface.volume_geometry_info = tuple(b'?\n' for _ in range(8))
  84. expected_surface.command_lines = [b'?', b'!']
  85. output_path = tmpdir.join('surface')
  86. expected_surface.write_triangular(output_path,
  87. creation_datetime=expected_surface.creation_datetime)
  88. resulted_surface = Surface.read_triangular(output_path)
  89. assert vars(expected_surface) == vars(resulted_surface)
  90. def test_write_triangular_same_locale(tmpdir):
  91. surface = Surface()
  92. surface.creator = b'pytest'
  93. surface.volume_geometry_info = tuple(b'?' for _ in range(8))
  94. creation_datetime = datetime.datetime(2018, 12, 31, 21, 42)
  95. output_path = tmpdir.join('surface')
  96. with setlocale('de_AT.utf8'):
  97. surface.write_triangular(output_path,
  98. creation_datetime=creation_datetime)
  99. resulted_surface = Surface.read_triangular(output_path)
  100. assert resulted_surface.creation_datetime == creation_datetime
  101. with open(output_path, 'rb') as output_file:
  102. assert output_file.read().split(b' on ')[1] \
  103. .startswith(b'Mon Dec 31 21:42:00 2018\n')
  104. def test_load_annotation():
  105. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  106. assert not surface.annotation
  107. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  108. assert isinstance(surface.annotation, Annotation)
  109. assert len(surface.annotation.vertex_label_index) == 155622
  110. assert surface.annotation.vertex_label_index[0] == 5
  111. def test_add_vertex():
  112. surface = Surface()
  113. assert not surface.vertices
  114. assert surface.add_vertex(Vertex(1.0, 1.5, 2.0)) == 0
  115. assert len(surface.vertices) == 1
  116. assert surface.vertices[0].anterior == pytest.approx(1.5)
  117. assert surface.add_vertex(Vertex(-3.0, 0.0, 4.0)) == 1
  118. assert len(surface.vertices) == 2
  119. assert surface.vertices[1].right == pytest.approx(-3.0)
  120. @pytest.mark.parametrize(('vertices_coords', 'expected_extra_vertex_coords'), [
  121. (((0, 0, 0), (2, 4, 0), (2, 4, 3)), (0, 0, 3)),
  122. (((1, 1, 0), (3, 5, 0), (3, 5, 3)), (1, 1, 3)),
  123. (((1, 1, 7), (3, 5, 7), (3, 5, 3)), (1, 1, 3)),
  124. (((1, 1, 1), (3, 5, 7), (3, 5, 9)), (1, 1, 3)),
  125. (((3, 5, 7), (1, 1, 1), (1, 1, 3)), (3, 5, 9)),
  126. ])
  127. def test_add_rectangle(vertices_coords, expected_extra_vertex_coords):
  128. surface = Surface()
  129. for vertex_coords in vertices_coords:
  130. surface.add_vertex(Vertex(*(float(c) for c in vertex_coords)))
  131. surface.add_rectangle(range(3))
  132. assert tuple(surface.vertices[3]) \
  133. == pytest.approx(expected_extra_vertex_coords)
  134. assert len(surface.triangles) == 2
  135. assert surface.triangles[0].vertex_indices == (0, 1, 2)
  136. assert surface.triangles[1].vertex_indices == (2, 3, 0)
  137. def test__find_label_border_segments():
  138. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  139. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  140. precentral_label, = filter(lambda l: l.name == 'precentral',
  141. surface.annotation.labels.values())
  142. # pylint: disable=protected-access
  143. border_segments = set(
  144. surface._find_label_border_segments(precentral_label))
  145. assert len(border_segments) == 417
  146. assert _LineSegment((33450, 32065)) in border_segments
  147. assert _LineSegment((33454, 33450)) in border_segments
  148. for border_vertex_index in [33450, 33454, 32065]:
  149. assert surface.annotation.vertex_label_index[border_vertex_index] == precentral_label.index
  150. for other_vertex_index in [32064, 33449, 33455, 33449, 33455]:
  151. assert _LineSegment((other_vertex_index, border_vertex_index)) \
  152. not in border_segments
  153. assert _LineSegment((border_vertex_index, other_vertex_index)) \
  154. not in border_segments
  155. def test_find_label_border_polygonal_chains():
  156. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  157. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  158. precentral_label, = filter(lambda l: l.name == 'precentral',
  159. surface.annotation.labels.values())
  160. border_chain, = surface.find_label_border_polygonal_chains(precentral_label)
  161. vertex_indices = list(border_chain.vertex_indices)
  162. assert len(vertex_indices) == 418
  163. min_index = vertex_indices.index(min(vertex_indices))
  164. vertex_indices_normalized = vertex_indices[min_index:] + vertex_indices[:min_index]
  165. assert vertex_indices_normalized[:4] == [32065, 32072, 32073, 32080]
  166. assert vertex_indices_normalized[-4:] == [36281, 34870, 33454, 33450]