hippocampus_test.py 7.2 KB

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