test_surface.py 9.0 KB

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