freesurfer_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import re
  3. import pytest
  4. from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  5. SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
  6. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  7. ('bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  8. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  9. ('bert/mri/lh.hippoSfVolumes-T1-T2.v10.txt',
  10. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': 'T2'}),
  11. ('bert/mri/lh.hippoSfVolumes-T2.v10.txt',
  12. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'T2'}),
  13. ('bert/mri/lh.hippoSfVolumes-T1-T2-high-res.v10.txt',
  14. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': 'T2-high-res'}),
  15. ('bert/mri/lh.hippoSfVolumes-T2-high-res.v10.txt',
  16. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'T2-high-res'}),
  17. ('bert/mri/lh.hippoSfVolumes-PD.v10.txt',
  18. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'PD'}),
  19. ('bert/mri/rh.hippoSfVolumes-T1.v10.txt',
  20. {'subject': 'bert', 'hemisphere': 'right', 't1_input': True, 'analysis_id': None}),
  21. ('bert/mri/rh.hippoSfVolumes-T1-T2.v10.txt',
  22. {'subject': 'bert', 'hemisphere': 'right', 't1_input': True, 'analysis_id': 'T2'}),
  23. ('freesurfer/subjects/bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  24. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  25. ('../../bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  26. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  27. ])
  28. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  29. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  30. assert os.path.basename(volume_file_path) == os.path.basename(volume_file.absolute_path)
  31. for attr, value in expected_attrs.items():
  32. assert value == getattr(volume_file, attr)
  33. @pytest.mark.parametrize('volume_file_path', [
  34. 'bert/mri/lh.hippoSfLabels-T1.v10.mgz',
  35. 'bert/mri/lh.hippoSfVolumes-T1.v9.txt',
  36. 'bert/mri/lh.hippoSfVolumes.v10.txt',
  37. 'bert/mri/mh.hippoSfVolumes-T1.v10.txt',
  38. ])
  39. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  40. with pytest.raises(Exception):
  41. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  42. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  43. (os.path.join(SUBJECTS_DIR, 'bert/mri/lh.hippoSfVolumes-T1.v10.txt'),
  44. {'Hippocampal_tail': 123.456789,
  45. 'subiculum': 234.567891,
  46. 'CA1': 34.567891,
  47. 'hippocampal-fissure': 345.678912,
  48. 'presubiculum': 456.789123,
  49. 'parasubiculum': 45.678912,
  50. 'molecular_layer_HP': 56.789123,
  51. 'GC-ML-DG': 567.891234,
  52. 'CA3': 678.912345,
  53. 'CA4': 789.123456,
  54. 'fimbria': 89.123456,
  55. 'HATA': 91.234567,
  56. 'Whole_hippocampus': 1234.567899}),
  57. ])
  58. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  59. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  60. assert expected_volumes == volume_file.read_volumes_mm3()
  61. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  62. volume_file = HippocampalSubfieldsVolumeFile(
  63. path=os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
  64. with pytest.raises(FileNotFoundError):
  65. volume_file.read_volumes_mm3()
  66. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  67. (SUBJECTS_DIR,
  68. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  69. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  70. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  71. (os.path.join(SUBJECTS_DIR, 'bert'),
  72. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  73. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  74. (os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
  75. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  76. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  77. ])
  78. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  79. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  80. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  81. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  82. (SUBJECTS_DIR,
  83. r'hippoSfVolumes-T1\.v10',
  84. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  85. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  86. (os.path.join(SUBJECTS_DIR, 'bert'),
  87. r'hippoSfVolumes-T1-T2',
  88. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt')}),
  89. ])
  90. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  91. expected_file_paths):
  92. assert expected_file_paths == set(
  93. f.absolute_path for f in HippocampalSubfieldsVolumeFile.find(
  94. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)))