test_surface.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. def test__find_label_border_segments():
  117. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  118. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  119. precentral_label, = filter(lambda l: l.name == 'precentral',
  120. surface.annotation.labels.values())
  121. # pylint: disable=protected-access
  122. border_segments = set(surface._find_label_border_segments(precentral_label))
  123. assert len(border_segments) == 417
  124. assert _LineSegment((33450, 32065)) in border_segments
  125. assert _LineSegment((33454, 33450)) in border_segments
  126. for border_vertex_index in [33450, 33454, 32065]:
  127. assert surface.annotation.vertex_label_index[border_vertex_index] == precentral_label.index
  128. for other_vertex_index in [32064, 33449, 33455, 33449, 33455]:
  129. assert _LineSegment((other_vertex_index, border_vertex_index)) not in border_segments
  130. assert _LineSegment((border_vertex_index, other_vertex_index)) not in border_segments