main_test.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.__main__
  9. from conftest import SUBJECTS_DIR, assert_volume_frames_equal
  10. def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
  11. subjects_dir: typing.Optional[str] = None):
  12. if subjects_dir:
  13. os.environ['SUBJECTS_DIR'] = subjects_dir
  14. elif 'SUBJECTS_DIR' in os.environ:
  15. del os.environ['SUBJECTS_DIR']
  16. with unittest.mock.patch('sys.argv', [''] + argv):
  17. freesurfer_volume_reader.__main__.main()
  18. out, _ = capsys.readouterr()
  19. assert_volume_frames_equal(
  20. left=expected_frame,
  21. # pandas.DataFrame.drop(columns=[...], ...) >= pandas0.21.0
  22. right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
  23. )
  24. @pytest.mark.parametrize(('args', 'root_dir_paths', 'expected_csv_path'), [
  25. ([],
  26. [os.path.join(SUBJECTS_DIR, 'alice')],
  27. os.path.join(SUBJECTS_DIR, 'alice', 'freesurfer-hippocampal-volumes.csv')),
  28. ([],
  29. [os.path.join(SUBJECTS_DIR, 'bert')],
  30. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  31. ([],
  32. [os.path.join(SUBJECTS_DIR, 'alice'),
  33. os.path.join(SUBJECTS_DIR, 'bert')],
  34. os.path.join(SUBJECTS_DIR, 'freesurfer-hippocampal-volumes.csv')),
  35. ([],
  36. [SUBJECTS_DIR],
  37. os.path.join(SUBJECTS_DIR, 'freesurfer-hippocampal-volumes.csv')),
  38. (['--source-types', 'freesurfer'],
  39. [os.path.join(SUBJECTS_DIR, 'alice')],
  40. os.path.join(SUBJECTS_DIR, 'alice', 'freesurfer-hippocampal-volumes.csv')),
  41. (['--source-types', 'freesurfer'],
  42. [SUBJECTS_DIR],
  43. os.path.join(SUBJECTS_DIR, 'freesurfer-hippocampal-volumes.csv')),
  44. (['--source-types', 'ashs'],
  45. [os.path.join(SUBJECTS_DIR, 'alice')],
  46. os.path.join(SUBJECTS_DIR, 'alice', 'ashs-hippocampal-volumes.csv')),
  47. (['--source-types', 'ashs'],
  48. [os.path.join(SUBJECTS_DIR, 'bert')],
  49. os.path.join(SUBJECTS_DIR, 'bert', 'ashs-hippocampal-volumes.csv')),
  50. (['--source-types', 'ashs'],
  51. [os.path.join(SUBJECTS_DIR, 'alice'),
  52. os.path.join(SUBJECTS_DIR, 'bert')],
  53. os.path.join(SUBJECTS_DIR, 'ashs-hippocampal-volumes.csv')),
  54. (['--source-types', 'ashs'],
  55. [SUBJECTS_DIR],
  56. os.path.join(SUBJECTS_DIR, 'ashs-hippocampal-volumes.csv')),
  57. (['--source-types', 'ashs', 'freesurfer'],
  58. [os.path.join(SUBJECTS_DIR, 'alice')],
  59. os.path.join(SUBJECTS_DIR, 'alice', 'all-hippocampal-volumes.csv')),
  60. (['--source-types', 'freesurfer', 'ashs'],
  61. [os.path.join(SUBJECTS_DIR, 'alice')],
  62. os.path.join(SUBJECTS_DIR, 'alice', 'all-hippocampal-volumes.csv')),
  63. (['--source-types', 'ashs', 'freesurfer'],
  64. [os.path.join(SUBJECTS_DIR, 'alice'),
  65. os.path.join(SUBJECTS_DIR, 'bert')],
  66. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  67. (['--source-types', 'ashs', 'freesurfer'],
  68. [SUBJECTS_DIR],
  69. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  70. ])
  71. def test_main_root_dir_param(capsys, args, root_dir_paths: list, expected_csv_path):
  72. assert_main_volume_frame_equals(
  73. argv=args + ['--'] + root_dir_paths,
  74. expected_frame=pandas.read_csv(expected_csv_path),
  75. capsys=capsys,
  76. )
  77. @pytest.mark.parametrize(('args', 'root_dir_path', 'expected_csv_path'), [
  78. ([],
  79. SUBJECTS_DIR,
  80. os.path.join(SUBJECTS_DIR, 'freesurfer-hippocampal-volumes.csv')),
  81. ([],
  82. os.path.join(SUBJECTS_DIR, 'bert'),
  83. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  84. (['--source-types', 'freesurfer'],
  85. SUBJECTS_DIR,
  86. os.path.join(SUBJECTS_DIR, 'freesurfer-hippocampal-volumes.csv')),
  87. (['--source-types', 'freesurfer'],
  88. os.path.join(SUBJECTS_DIR, 'bert'),
  89. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  90. (['--source-types', 'freesurfer'],
  91. os.path.join(SUBJECTS_DIR, 'bert', 'mri'),
  92. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  93. (['--source-types', 'ashs'],
  94. SUBJECTS_DIR,
  95. os.path.join(SUBJECTS_DIR, 'ashs-hippocampal-volumes.csv')),
  96. (['--source-types', 'ashs'],
  97. os.path.join(SUBJECTS_DIR, 'bert'),
  98. os.path.join(SUBJECTS_DIR, 'bert', 'ashs-hippocampal-volumes.csv')),
  99. (['--source-types', 'ashs'],
  100. os.path.join(SUBJECTS_DIR, 'bert', 'final'),
  101. os.path.join(SUBJECTS_DIR, 'bert', 'ashs-hippocampal-volumes.csv')),
  102. (['--source-types', 'ashs'],
  103. os.path.join(SUBJECTS_DIR, 'alice'),
  104. os.path.join(SUBJECTS_DIR, 'alice', 'ashs-hippocampal-volumes.csv')),
  105. (['--source-types', 'ashs', 'freesurfer'],
  106. os.path.join(SUBJECTS_DIR, 'alice'),
  107. os.path.join(SUBJECTS_DIR, 'alice', 'all-hippocampal-volumes.csv')),
  108. (['--source-types', 'freesurfer', 'ashs'],
  109. os.path.join(SUBJECTS_DIR, 'alice'),
  110. os.path.join(SUBJECTS_DIR, 'alice', 'all-hippocampal-volumes.csv')),
  111. (['--source-types', 'freesurfer', 'ashs'],
  112. SUBJECTS_DIR,
  113. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  114. ])
  115. def test_main_root_dir_env(capsys, args, root_dir_path, expected_csv_path):
  116. assert_main_volume_frame_equals(
  117. argv=args,
  118. subjects_dir=root_dir_path,
  119. expected_frame=pandas.read_csv(expected_csv_path),
  120. capsys=capsys,
  121. )
  122. @pytest.mark.timeout(8)
  123. @pytest.mark.parametrize(('args', 'root_dir_path', 'subjects_dir', 'expected_csv_path'), [
  124. ([],
  125. os.path.join(SUBJECTS_DIR, 'bert'),
  126. os.path.join(SUBJECTS_DIR, 'alice'),
  127. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  128. (['--source-types', 'ashs'],
  129. os.path.join(SUBJECTS_DIR, 'bert'),
  130. os.path.join(SUBJECTS_DIR, 'alice'),
  131. os.path.join(SUBJECTS_DIR, 'bert', 'ashs-hippocampal-volumes.csv')),
  132. ([],
  133. os.path.join(SUBJECTS_DIR, 'bert'),
  134. os.path.abspath(os.sep),
  135. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv')),
  136. ])
  137. def test_main_root_dir_overwrite_env(capsys, args, root_dir_path, subjects_dir, expected_csv_path):
  138. assert_main_volume_frame_equals(
  139. argv=args + ['--', root_dir_path],
  140. subjects_dir=subjects_dir,
  141. expected_frame=pandas.read_csv(expected_csv_path),
  142. capsys=capsys,
  143. )
  144. def test_main_root_dir_filename_regex_freesurfer(capsys):
  145. expected_volume_frame = pandas.read_csv(
  146. os.path.join(SUBJECTS_DIR, 'bert', 'freesurfer-hippocampal-volumes.csv'))
  147. assert_main_volume_frame_equals(
  148. argv=['--freesurfer-filename-regex', r'^.*-T1-T2\.v10\.txt$',
  149. os.path.join(SUBJECTS_DIR, 'bert')],
  150. expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
  151. capsys=capsys,
  152. )
  153. def test_main_root_dir_filename_regex_ashs(capsys):
  154. expected_volume_frame = pandas.read_csv(
  155. os.path.join(SUBJECTS_DIR, 'bert', 'ashs-hippocampal-volumes.csv'))
  156. assert_main_volume_frame_equals(
  157. argv=['--ashs-filename-regex', r'_nogray_volumes.txt$',
  158. '--source-types', 'ashs', '--',
  159. os.path.join(SUBJECTS_DIR, 'bert')],
  160. expected_frame=expected_volume_frame[expected_volume_frame['correction']
  161. == 'nogray'].copy(),
  162. capsys=capsys,
  163. )