freesurfer_test.py 6.6 KB

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