test_surface.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import datetime
  2. import os
  3. import pytest
  4. from freesurfer_surface import 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. @pytest.mark.parametrize(('creation_datetime', 'expected_str'), [
  50. (datetime.datetime(2019, 5, 9, 22, 37, 41), b'Thu May 9 22:37:41 2019'),
  51. (datetime.datetime(2019, 4, 24, 23, 29, 22), b'Wed Apr 24 23:29:22 2019'),
  52. ])
  53. def test_triangular_creation_datetime_strftime(creation_datetime, expected_str):
  54. surface = Surface()
  55. surface.creation_datetime = creation_datetime
  56. # pylint: disable=protected-access
  57. assert expected_str == surface._triangular_creation_datetime_strftime()
  58. def test_read_write_triangular_same(tmpdir):
  59. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  60. output_path = tmpdir.join('surface')
  61. surface.write_triangular(output_path,
  62. creation_datetime=surface.creation_datetime)
  63. with open(output_path, 'rb') as output_file:
  64. with open(SURFACE_FILE_PATH, 'rb') as expected_file:
  65. assert expected_file.read() == output_file.read()
  66. def test_write_read_triangular_same(tmpdir):
  67. expected_surface = Surface()
  68. expected_surface.creator = b'pytest'
  69. expected_surface.creation_datetime = datetime.datetime.now().replace(microsecond=0)
  70. expected_surface.vertices = [Vertex(0.0, 0.0, 0.0),
  71. Vertex(1.0, 2.0, 3.0),
  72. Vertex(2.0, 4.0, 6.0),
  73. Vertex(3.0, 5.0, 7.0)]
  74. expected_surface.triangles_vertex_indices = [(0, 1, 2),
  75. (0, 1, 3),
  76. (3, 2, 1)]
  77. expected_surface.using_old_real_ras = False
  78. expected_surface.volume_geometry_info = [b'?\n'] * 8
  79. expected_surface.command_lines = [b'?', b'!']
  80. output_path = tmpdir.join('surface')
  81. expected_surface.write_triangular(output_path,
  82. creation_datetime=expected_surface.creation_datetime)
  83. resulted_surface = Surface.read_triangular(output_path)
  84. assert vars(expected_surface) == vars(resulted_surface)