ashs_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import os
  2. import re
  3. import pandas
  4. import pytest
  5. from freesurfer_volume_reader.ashs import HippocampalSubfieldsVolumeFile
  6. from conftest import SUBJECTS_DIR, assert_volume_frames_equal
  7. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  8. ('ashs/final/bert_left_heur_volumes.txt',
  9. {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),
  10. ('ashs/final/bert_left_corr_nogray_volumes.txt',
  11. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'nogray'}),
  12. ('ashs/final/bert_left_corr_usegray_volumes.txt',
  13. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'usegray'}),
  14. ('ashs/final/bert_right_heur_volumes.txt',
  15. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  16. ('ashs/final/bert_right_corr_nogray_volumes.txt',
  17. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  18. ('ashs/final/bert_right_corr_usegray_volumes.txt',
  19. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'usegray'}),
  20. ('somewhere/else/bert_right_heur_volumes.txt',
  21. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  22. ('somewhere/else/bert_right_corr_nogray_volumes.txt',
  23. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  24. ('bert_right_heur_volumes.txt',
  25. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  26. ('/foo/bar/alice_20190503_right_corr_nogray_volumes.txt',
  27. {'subject': 'alice_20190503', 'hemisphere': 'right', 'correction': 'nogray'}),
  28. ])
  29. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  30. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  31. assert os.path.abspath(volume_file_path) == volume_file.absolute_path
  32. for attr, value in expected_attrs.items():
  33. assert value == getattr(volume_file, attr)
  34. @pytest.mark.parametrize('volume_file_path', [
  35. 'bert_middle_heur_volumes.txt',
  36. 'bert_right_hear_volumes.txt',
  37. 'bert_right_heur_volumes.nii',
  38. 'bert_left_lfseg_corr_usegray.nii.gz',
  39. ])
  40. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  41. with pytest.raises(Exception):
  42. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  43. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  44. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  45. {'CA1': 678.901,
  46. 'CA2+3': 123.456,
  47. 'DG': 901.234,
  48. 'ERC': 678.901,
  49. 'PHC': 2345.876,
  50. 'PRC': 2345.678,
  51. 'SUB': 457.789}),
  52. ])
  53. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  54. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  55. assert expected_volumes == volume_file.read_volumes_mm3()
  56. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  57. volume_file = HippocampalSubfieldsVolumeFile(
  58. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  59. with pytest.raises(FileNotFoundError):
  60. volume_file.read_volumes_mm3()
  61. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  62. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  63. pandas.DataFrame({
  64. 'subfield': ['CA1', 'CA2+3', 'DG', 'ERC', 'PHC', 'PRC', 'SUB'],
  65. 'volume_mm^3': [679.904, 124.459, 902.237, 789.012, 2346.879, 2346.671, 458.782],
  66. 'subject': 'alice',
  67. 'hemisphere': 'left',
  68. 'correction': None,
  69. })),
  70. ])
  71. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  72. volume_file_path: str, expected_dataframe: pandas.DataFrame):
  73. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  74. assert_volume_frames_equal(left=expected_dataframe,
  75. right=volume_file.read_volumes_dataframe())
  76. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  77. volume_file = HippocampalSubfieldsVolumeFile(
  78. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  79. with pytest.raises(FileNotFoundError):
  80. volume_file.read_volumes_dataframe()
  81. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  82. (os.path.join(SUBJECTS_DIR, 'alice'),
  83. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  84. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt')}),
  85. (os.path.join(SUBJECTS_DIR, 'bert'),
  86. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  87. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  88. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  89. (SUBJECTS_DIR,
  90. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  91. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  92. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  93. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  94. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  95. ])
  96. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  97. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  98. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  99. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  100. (SUBJECTS_DIR,
  101. r'^bert_right_',
  102. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  103. (SUBJECTS_DIR,
  104. r'_nogray_volumes.txt$',
  105. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  106. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  107. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  108. ])
  109. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  110. expected_file_paths):
  111. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(
  112. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern))
  113. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)