ashs_test.py 7.3 KB

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