main_test.py 3.4 KB

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