hippocampus_test.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import io
  2. import os
  3. import re
  4. import typing
  5. import unittest.mock
  6. import pandas
  7. import pandas.util.testing
  8. import pytest
  9. import freesurfer_volume_reader
  10. SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
  11. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  12. (SUBJECTS_DIR,
  13. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  14. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  15. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  16. (os.path.join(SUBJECTS_DIR, 'bert'),
  17. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  18. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  19. (os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
  20. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt'),
  21. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  22. ])
  23. def test_find_hippocampal_volume_files(root_dir_path, expected_file_paths):
  24. assert expected_file_paths == set(
  25. freesurfer_volume_reader.find_hippocampal_volume_files(root_dir_path=root_dir_path))
  26. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  27. (SUBJECTS_DIR,
  28. r'hippoSfVolumes-T1\.v10',
  29. {os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  30. os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1.v10.txt')}),
  31. (os.path.join(SUBJECTS_DIR, 'bert'),
  32. r'hippoSfVolumes-T1-T2',
  33. {os.path.join(SUBJECTS_DIR, 'bert', 'mri', 'lh.hippoSfVolumes-T1-T2.v10.txt')}),
  34. ])
  35. def test_find_hippocampal_volume_files_pattern(root_dir_path, filename_pattern,
  36. expected_file_paths):
  37. assert expected_file_paths == set(freesurfer_volume_reader.find_hippocampal_volume_files(
  38. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)))
  39. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  40. (os.path.join(SUBJECTS_DIR, 'bert/mri/lh.hippoSfVolumes-T1.v10.txt'),
  41. {'Hippocampal_tail': 123.456789,
  42. 'subiculum': 234.567891,
  43. 'CA1': 34.567891,
  44. 'hippocampal-fissure': 345.678912,
  45. 'presubiculum': 456.789123,
  46. 'parasubiculum': 45.678912,
  47. 'molecular_layer_HP': 56.789123,
  48. 'GC-ML-DG': 567.891234,
  49. 'CA3': 678.912345,
  50. 'CA4': 789.123456,
  51. 'fimbria': 89.123456,
  52. 'HATA': 91.234567,
  53. 'Whole_hippocampus': 1234.567899}),
  54. ])
  55. def test_read_hippocampal_volumes_mm3(volume_file_path, expected_volumes):
  56. assert expected_volumes == freesurfer_volume_reader.read_hippocampal_volumes_mm3(
  57. volume_file_path)
  58. def test_read_hippocampal_volumes_mm3_not_found():
  59. with pytest.raises(FileNotFoundError):
  60. freesurfer_volume_reader.read_hippocampal_volumes_mm3(
  61. os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
  62. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  63. ('bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  64. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': True, 'analysis_id': None}),
  65. ('bert/mri/lh.hippoSfVolumes-T1-T2.v10.txt',
  66. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': True, 'analysis_id': 'T2'}),
  67. ('bert/mri/lh.hippoSfVolumes-T2.v10.txt',
  68. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': False, 'analysis_id': 'T2'}),
  69. ('bert/mri/lh.hippoSfVolumes-T1-T2-high-res.v10.txt',
  70. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': True, 'analysis_id': 'T2-high-res'}),
  71. ('bert/mri/lh.hippoSfVolumes-T2-high-res.v10.txt',
  72. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': False, 'analysis_id': 'T2-high-res'}),
  73. ('bert/mri/lh.hippoSfVolumes-PD.v10.txt',
  74. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': False, 'analysis_id': 'PD'}),
  75. ('bert/mri/rh.hippoSfVolumes-T1.v10.txt',
  76. {'subject': 'bert', 'hemisphere': 'right', 'T1_input': True, 'analysis_id': None}),
  77. ('bert/mri/rh.hippoSfVolumes-T1-T2.v10.txt',
  78. {'subject': 'bert', 'hemisphere': 'right', 'T1_input': True, 'analysis_id': 'T2'}),
  79. ('freesurfer/subjects/bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  80. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': True, 'analysis_id': None}),
  81. ('../../bert/mri/lh.hippoSfVolumes-T1.v10.txt',
  82. {'subject': 'bert', 'hemisphere': 'left', 'T1_input': True, 'analysis_id': None}),
  83. ])
  84. def test_parse_hippocampal_volume_file_path(volume_file_path, expected_attrs):
  85. assert expected_attrs == freesurfer_volume_reader.parse_hippocampal_volume_file_path(
  86. volume_file_path=volume_file_path)
  87. @pytest.mark.parametrize('volume_file_path', [
  88. 'bert/mri/lh.hippoSfLabels-T1.v10.mgz',
  89. 'bert/mri/lh.hippoSfVolumes-T1.v9.txt',
  90. 'bert/mri/lh.hippoSfVolumes.v10.txt',
  91. 'bert/mri/mh.hippoSfVolumes-T1.v10.txt',
  92. ])
  93. def test_parse_hippocampal_volume_file_path_invalid(volume_file_path):
  94. with pytest.raises(Exception):
  95. freesurfer_volume_reader.parse_hippocampal_volume_file_path(
  96. volume_file_path=volume_file_path)
  97. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  98. (os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  99. pandas.DataFrame({
  100. 'subfield': ['Hippocampal_tail', 'subiculum', 'CA1', 'hippocampal-fissure',
  101. 'presubiculum', 'parasubiculum', 'molecular_layer_HP', 'GC-ML-DG',
  102. 'CA3', 'CA4', 'fimbria', 'HATA', 'Whole_hippocampus'],
  103. 'volume_mm^3': [173.456789, 734.567891, 34.567891, 345.678917, 456.789173, 45.678917,
  104. 56.789173, 567.891734, 678.917345, 789.173456, 89.173456, 91.734567,
  105. 1734.567899],
  106. 'subject': 'alice',
  107. 'hemisphere': 'left',
  108. 'T1_input': True,
  109. 'analysis_id': None,
  110. })),
  111. ])
  112. def test_read_hippocampal_volume_file_dataframe(volume_file_path, expected_dataframe):
  113. assert_volume_frames_equal(
  114. left=expected_dataframe,
  115. right=freesurfer_volume_reader.read_hippocampal_volume_file_dataframe(
  116. volume_file_path=volume_file_path),
  117. )
  118. def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
  119. sort_by = ['volume_mm^3', 'analysis_id']
  120. left.sort_values(sort_by, inplace=True)
  121. right.sort_values(sort_by, inplace=True)
  122. left.reset_index(inplace=True, drop=True)
  123. right.reset_index(inplace=True, drop=True)
  124. pandas.util.testing.assert_frame_equal(
  125. left=left,
  126. right=right,
  127. # ignore the order of index & columns
  128. check_like=True,
  129. )
  130. def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
  131. subjects_dir: typing.Optional[str] = None):
  132. if subjects_dir:
  133. os.environ['SUBJECTS_DIR'] = subjects_dir
  134. elif 'SUBJECTS_DIR' in os.environ:
  135. del os.environ['SUBJECTS_DIR']
  136. with unittest.mock.patch('sys.argv', [''] + argv):
  137. freesurfer_volume_reader.main()
  138. out, _ = capsys.readouterr()
  139. assert_volume_frames_equal(
  140. left=expected_frame,
  141. # pandas.DataFrame.drop(columns=[...], ...) >= pandas0.21.0
  142. right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
  143. )
  144. @pytest.mark.parametrize(('root_dir_paths', 'expected_csv_path'), [
  145. ([os.path.join(SUBJECTS_DIR, 'alice')],
  146. os.path.join(SUBJECTS_DIR, 'alice', 'hippocampal-volumes.csv')),
  147. ([os.path.join(SUBJECTS_DIR, 'bert')],
  148. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  149. ([os.path.join(SUBJECTS_DIR, 'alice'),
  150. os.path.join(SUBJECTS_DIR, 'bert')],
  151. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  152. ])
  153. def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
  154. assert_main_volume_frame_equals(
  155. argv=root_dir_paths,
  156. expected_frame=pandas.read_csv(expected_csv_path),
  157. capsys=capsys,
  158. )
  159. @pytest.mark.parametrize(('root_dir_path', 'expected_csv_path'), [
  160. (os.path.join(SUBJECTS_DIR, 'bert'),
  161. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  162. ])
  163. def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
  164. assert_main_volume_frame_equals(
  165. argv=[],
  166. subjects_dir=root_dir_path,
  167. expected_frame=pandas.read_csv(expected_csv_path),
  168. capsys=capsys,
  169. )
  170. @pytest.mark.timeout(8)
  171. @pytest.mark.parametrize(('root_dir_path', 'subjects_dir', 'expected_csv_path'), [
  172. (os.path.join(SUBJECTS_DIR, 'bert'),
  173. os.path.join(SUBJECTS_DIR, 'alice'),
  174. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  175. (os.path.join(SUBJECTS_DIR, 'bert'),
  176. os.path.abspath(os.sep),
  177. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  178. ])
  179. def test_main_root_dir_overwrite_env(capsys, root_dir_path, subjects_dir, expected_csv_path):
  180. assert_main_volume_frame_equals(
  181. argv=[root_dir_path],
  182. subjects_dir=subjects_dir,
  183. expected_frame=pandas.read_csv(expected_csv_path),
  184. capsys=capsys,
  185. )
  186. def test_main_root_dir_filename_regex(capsys):
  187. expected_volume_frame = pandas.read_csv(
  188. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
  189. assert_main_volume_frame_equals(
  190. argv=['--filename-regex', r'^.*-T1-T2\.v10\.txt$',
  191. os.path.join(SUBJECTS_DIR, 'bert')],
  192. expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
  193. capsys=capsys,
  194. )