hippocampus_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. def assert_main_volume_frame_equals(capsys, assert_volume_frames_equal,
  20. argv: list, expected_frame: pandas.DataFrame,
  21. subjects_dir: typing.Optional[str] = None):
  22. if subjects_dir:
  23. os.environ['SUBJECTS_DIR'] = subjects_dir
  24. elif 'SUBJECTS_DIR' in os.environ:
  25. del os.environ['SUBJECTS_DIR']
  26. with unittest.mock.patch('sys.argv', [''] + argv):
  27. freesurfer_volume_reader.main()
  28. out, _ = capsys.readouterr()
  29. assert_volume_frames_equal(
  30. left=expected_frame,
  31. # pandas.DataFrame.drop(columns=[...], ...) >= pandas0.21.0
  32. right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
  33. )
  34. @pytest.mark.parametrize(('root_dir_paths', 'expected_csv_path'), [
  35. ([os.path.join(SUBJECTS_DIR, 'alice')],
  36. os.path.join(SUBJECTS_DIR, 'alice', 'hippocampal-volumes.csv')),
  37. ([os.path.join(SUBJECTS_DIR, 'bert')],
  38. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  39. ([os.path.join(SUBJECTS_DIR, 'alice'),
  40. os.path.join(SUBJECTS_DIR, 'bert')],
  41. os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
  42. ])
  43. def test_main_root_dir_param(capsys, assert_volume_frames_equal,
  44. root_dir_paths: list, expected_csv_path):
  45. assert_main_volume_frame_equals(
  46. argv=root_dir_paths,
  47. expected_frame=pandas.read_csv(expected_csv_path),
  48. capsys=capsys,
  49. assert_volume_frames_equal=assert_volume_frames_equal,
  50. )
  51. @pytest.mark.parametrize(('root_dir_path', 'expected_csv_path'), [
  52. (os.path.join(SUBJECTS_DIR, 'bert'),
  53. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  54. ])
  55. def test_main_root_dir_env(capsys, assert_volume_frames_equal,
  56. root_dir_path, expected_csv_path):
  57. assert_main_volume_frame_equals(
  58. argv=[],
  59. subjects_dir=root_dir_path,
  60. expected_frame=pandas.read_csv(expected_csv_path),
  61. capsys=capsys,
  62. assert_volume_frames_equal=assert_volume_frames_equal,
  63. )
  64. @pytest.mark.timeout(8)
  65. @pytest.mark.parametrize(('root_dir_path', 'subjects_dir', 'expected_csv_path'), [
  66. (os.path.join(SUBJECTS_DIR, 'bert'),
  67. os.path.join(SUBJECTS_DIR, 'alice'),
  68. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  69. (os.path.join(SUBJECTS_DIR, 'bert'),
  70. os.path.abspath(os.sep),
  71. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
  72. ])
  73. def test_main_root_dir_overwrite_env(capsys, assert_volume_frames_equal,
  74. root_dir_path, subjects_dir, expected_csv_path):
  75. assert_main_volume_frame_equals(
  76. argv=[root_dir_path],
  77. subjects_dir=subjects_dir,
  78. expected_frame=pandas.read_csv(expected_csv_path),
  79. capsys=capsys,
  80. assert_volume_frames_equal=assert_volume_frames_equal,
  81. )
  82. def test_main_root_dir_filename_regex(capsys, assert_volume_frames_equal):
  83. expected_volume_frame = pandas.read_csv(
  84. os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
  85. assert_main_volume_frame_equals(
  86. argv=['--filename-regex', r'^.*-T1-T2\.v10\.txt$',
  87. os.path.join(SUBJECTS_DIR, 'bert')],
  88. expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
  89. capsys=capsys,
  90. assert_volume_frames_equal=assert_volume_frames_equal,
  91. )