hippocampus_test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import io
  2. import os
  3. import typing
  4. import unittest.mock
  5. import pandas
  6. import pandas.util.testing
  7. import pytest
  8. import freesurfer_volume_reader
  9. import freesurfer_volume_reader.freesurfer
  10. SUBJECTS_DIR = os.path.join(os.path.dirname(__file__), 'subjects')
  11. @pytest.mark.parametrize(('source_pattern', 'expected_pattern'), [
  12. (r'^(?P<h>[lr])h\.hippoSfVolumes', r'^([lr])h\.hippoSfVolumes'),
  13. (r'(?P<a>a(?P<b>b))', r'(a(b))'),
  14. ])
  15. def test_remove_group_names_from_regex(source_pattern, expected_pattern):
  16. assert expected_pattern == freesurfer_volume_reader.remove_group_names_from_regex(
  17. regex_pattern=source_pattern,
  18. )
  19. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  20. (os.path.join(SUBJECTS_DIR, 'bert/mri/lh.hippoSfVolumes-T1.v10.txt'),
  21. {'Hippocampal_tail': 123.456789,
  22. 'subiculum': 234.567891,
  23. 'CA1': 34.567891,
  24. 'hippocampal-fissure': 345.678912,
  25. 'presubiculum': 456.789123,
  26. 'parasubiculum': 45.678912,
  27. 'molecular_layer_HP': 56.789123,
  28. 'GC-ML-DG': 567.891234,
  29. 'CA3': 678.912345,
  30. 'CA4': 789.123456,
  31. 'fimbria': 89.123456,
  32. 'HATA': 91.234567,
  33. 'Whole_hippocampus': 1234.567899}),
  34. ])
  35. def test_read_hippocampal_volumes_mm3(volume_file_path, expected_volumes):
  36. assert expected_volumes == freesurfer_volume_reader.read_hippocampal_volumes_mm3(
  37. volume_file_path)
  38. def test_read_hippocampal_volumes_mm3_not_found():
  39. with pytest.raises(FileNotFoundError):
  40. freesurfer_volume_reader.read_hippocampal_volumes_mm3(
  41. os.path.join(SUBJECTS_DIR, 'non-existing', 'lh.hippoSfVolumes-T1.v10.txt'))
  42. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  43. (os.path.join(SUBJECTS_DIR, 'alice', 'mri', 'lh.hippoSfVolumes-T1.v10.txt'),
  44. pandas.DataFrame({
  45. 'subfield': ['Hippocampal_tail', 'subiculum', 'CA1', 'hippocampal-fissure',
  46. 'presubiculum', 'parasubiculum', 'molecular_layer_HP', 'GC-ML-DG',
  47. 'CA3', 'CA4', 'fimbria', 'HATA', 'Whole_hippocampus'],
  48. 'volume_mm^3': [173.456789, 734.567891, 34.567891, 345.678917, 456.789173, 45.678917,
  49. 56.789173, 567.891734, 678.917345, 789.173456, 89.173456, 91.734567,
  50. 1734.567899],
  51. 'subject': 'alice',
  52. 'hemisphere': 'left',
  53. 'T1_input': True,
  54. 'analysis_id': None,
  55. })),
  56. ])
  57. def test_read_hippocampal_volume_file_dataframe(volume_file_path, expected_dataframe):
  58. volume_file = freesurfer_volume_reader.freesurfer.HippocampalSubfieldsVolumeFile(
  59. path=volume_file_path)
  60. assert_volume_frames_equal(
  61. left=expected_dataframe,
  62. right=freesurfer_volume_reader.read_hippocampal_volume_file_dataframe(
  63. volume_file=volume_file),
  64. )
  65. def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
  66. sort_by = ['volume_mm^3', 'analysis_id']
  67. left.sort_values(sort_by, inplace=True)
  68. right.sort_values(sort_by, inplace=True)
  69. left.reset_index(inplace=True, drop=True)
  70. right.reset_index(inplace=True, drop=True)
  71. pandas.util.testing.assert_frame_equal(
  72. left=left,
  73. right=right,
  74. # ignore the order of index & columns
  75. check_like=True,
  76. )
  77. def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
  78. subjects_dir: typing.Optional[str] = None):
  79. if subjects_dir:
  80. os.environ['SUBJECTS_DIR'] = subjects_dir
  81. elif 'SUBJECTS_DIR' in os.environ:
  82. del os.environ['SUBJECTS_DIR']
  83. with unittest.mock.patch('sys.argv', [''] + argv):
  84. freesurfer_volume_reader.main()
  85. out, _ = capsys.readouterr()
  86. assert_volume_frames_equal(
  87. left=expected_frame,
  88. # pandas.DataFrame.drop(columns=[...], ...) >= pandas0.21.0
  89. right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
  90. )
  91. @pytest.mark.parametrize(('root_dir_paths', 'expected_csv_path'), [
  92. ([os.path.join(SUBJECTS_DIR, 'alice')],
  93. os.path.join(SUBJECTS_DIR, 'alice', 'hippocampal-volumes.csv')),
  94. ([os.path.join(SUBJECTS_DIR, 'bert')],
  95. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  96. ([os.path.join(SUBJECTS_DIR, 'alice'),
  97. os.path.join(SUBJECTS_DIR, 'bert')],
  98. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  99. ])
  100. def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
  101. assert_main_volume_frame_equals(
  102. argv=root_dir_paths,
  103. expected_frame=pandas.read_csv(expected_csv_path),
  104. capsys=capsys,
  105. )
  106. @pytest.mark.parametrize(('root_dir_path', 'expected_csv_path'), [
  107. (os.path.join(SUBJECTS_DIR, 'bert'),
  108. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  109. ])
  110. def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
  111. assert_main_volume_frame_equals(
  112. argv=[],
  113. subjects_dir=root_dir_path,
  114. expected_frame=pandas.read_csv(expected_csv_path),
  115. capsys=capsys,
  116. )
  117. @pytest.mark.timeout(8)
  118. @pytest.mark.parametrize(('root_dir_path', 'subjects_dir', 'expected_csv_path'), [
  119. (os.path.join(SUBJECTS_DIR, 'bert'),
  120. os.path.join(SUBJECTS_DIR, 'alice'),
  121. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  122. (os.path.join(SUBJECTS_DIR, 'bert'),
  123. os.path.abspath(os.sep),
  124. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  125. ])
  126. def test_main_root_dir_overwrite_env(capsys, root_dir_path, subjects_dir, expected_csv_path):
  127. assert_main_volume_frame_equals(
  128. argv=[root_dir_path],
  129. subjects_dir=subjects_dir,
  130. expected_frame=pandas.read_csv(expected_csv_path),
  131. capsys=capsys,
  132. )
  133. def test_main_root_dir_filename_regex(capsys):
  134. expected_volume_frame = pandas.read_csv(
  135. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
  136. assert_main_volume_frame_equals(
  137. argv=['--filename-regex', r'^.*-T1-T2\.v10\.txt$',
  138. os.path.join(SUBJECTS_DIR, 'bert')],
  139. expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
  140. capsys=capsys,
  141. )