freesurfer_test.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import os
  2. import re
  3. import typing
  4. import pandas
  5. import pytest
  6. from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  7. from conftest import SUBJECTS_DIR
  8. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  9. ('bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  10. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  11. ('bert/mri/lh.hippoSfVolumes-T1-T2.v10.txt',
  12. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': 'T2'}),
  13. ('bert/mri/lh.hippoSfVolumes-T2.v10.txt',
  14. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'T2'}),
  15. ('bert/mri/lh.hippoSfVolumes-T1-T2-high-res.v10.txt',
  16. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': 'T2-high-res'}),
  17. ('bert/mri/lh.hippoSfVolumes-T2-high-res.v10.txt',
  18. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'T2-high-res'}),
  19. ('bert/mri/lh.hippoSfVolumes-PD.v10.txt',
  20. {'subject': 'bert', 'hemisphere': 'left', 't1_input': False, 'analysis_id': 'PD'}),
  21. ('bert/mri/rh.hippoSfVolumes-T1.v10.txt',
  22. {'subject': 'bert', 'hemisphere': 'right', 't1_input': True, 'analysis_id': None}),
  23. ('bert/mri/rh.hippoSfVolumes-T1-T2.v10.txt',
  24. {'subject': 'bert', 'hemisphere': 'right', 't1_input': True, 'analysis_id': 'T2'}),
  25. ('freesurfer/subjects/bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  26. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  27. ('../../bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  28. {'subject': 'bert', 'hemisphere': 'left', 't1_input': True, 'analysis_id': None}),
  29. ])
  30. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  31. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  32. assert os.path.basename(volume_file_path) == os.path.basename(volume_file.absolute_path)
  33. for attr, value in expected_attrs.items():
  34. assert value == getattr(volume_file, attr)
  35. @pytest.mark.parametrize('volume_file_path', [
  36. 'bert/mri/lh.hippoSfLabels-T1.v10.mgz',
  37. 'bert/mri/lh.hippoSfVolumes-T1.v9.txt',
  38. 'bert/mri/lh.hippoSfVolumes.v10.txt',
  39. 'bert/mri/mh.hippoSfVolumes-T1.v10.txt',
  40. ])
  41. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  42. with pytest.raises(Exception):
  43. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  44. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  45. (os.path.join(SUBJECTS_DIR, 'bert/mri/lh.hippoSfVolumes-T1.v10.txt'),
  46. {'Hippocampal_tail': 123.456789,
  47. 'subiculum': 234.567891,
  48. 'CA1': 34.567891,
  49. 'hippocampal-fissure': 345.678912,
  50. 'presubiculum': 456.789123,
  51. 'parasubiculum': 45.678912,
  52. 'molecular_layer_HP': 56.789123,
  53. 'GC-ML-DG': 567.891234,
  54. 'CA3': 678.912345,
  55. 'CA4': 789.123456,
  56. 'fimbria': 89.123456,
  57. 'HATA': 91.234567,
  58. 'Whole_hippocampus': 1234.567899}),
  59. ])
  60. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  61. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  62. assert expected_volumes == volume_file.read_volumes_mm3()
  63. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  64. volume_file = HippocampalSubfieldsVolumeFile(
  65. path=os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
  66. with pytest.raises(FileNotFoundError):
  67. volume_file.read_volumes_mm3()
  68. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  69. (os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  70. pandas.DataFrame({
  71. 'subfield': ['Hippocampal_tail', 'subiculum', 'CA1', 'hippocampal-fissure',
  72. 'presubiculum', 'parasubiculum', 'molecular_layer_HP', 'GC-ML-DG',
  73. 'CA3', 'CA4', 'fimbria', 'HATA', 'Whole_hippocampus'],
  74. 'volume_mm^3': [173.456789, 734.567891, 34.567891, 345.678917, 456.789173, 45.678917,
  75. 56.789173, 567.891734, 678.917345, 789.173456, 89.173456, 91.734567,
  76. 1734.567899],
  77. 'subject': 'alice',
  78. 'hemisphere': 'left',
  79. 'T1_input': True,
  80. 'analysis_id': None,
  81. })),
  82. ])
  83. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  84. assert_volume_frames_equal: typing.Callable,
  85. volume_file_path: str, expected_dataframe: pandas.DataFrame):
  86. assert_volume_frames_equal(
  87. left=expected_dataframe,
  88. right=HippocampalSubfieldsVolumeFile(path=volume_file_path).read_volumes_dataframe(),
  89. )
  90. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  91. volume_file = HippocampalSubfieldsVolumeFile(
  92. path=os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
  93. with pytest.raises(FileNotFoundError):
  94. volume_file.read_volumes_dataframe()
  95. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  96. (SUBJECTS_DIR,
  97. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  98. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  99. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  100. (os.path.join(SUBJECTS_DIR, 'bert'),
  101. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  102. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  103. (os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
  104. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  105. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  106. ])
  107. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  108. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  109. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  110. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  111. (SUBJECTS_DIR,
  112. r'hippoSfVolumes-T1\.v10',
  113. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  114. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  115. (os.path.join(SUBJECTS_DIR, 'bert'),
  116. r'hippoSfVolumes-T1-T2',
  117. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt')}),
  118. ])
  119. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  120. expected_file_paths):
  121. assert expected_file_paths == set(
  122. f.absolute_path for f in HippocampalSubfieldsVolumeFile.find(
  123. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)))