hippocampus_test.py 6.0 KB

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