ashs_test.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_volume'), [
  27. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1234560),
  28. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1.23456e06),
  29. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), 1.23456e+06),
  30. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'), float('1.23456e+06')),
  31. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt'), 1543200),
  32. ])
  33. def test_intracranial_volume_file_read_volume_mm3(volume_file_path, expected_volume):
  34. volume_file = IntracranialVolumeFile(path=volume_file_path)
  35. assert expected_volume == pytest.approx(volume_file.read_volume_mm3())
  36. @pytest.mark.parametrize('volume_file_path', [
  37. os.path.join(SUBJECTS_DIR, 'noone', 'final', 'noone_icv.txt'),
  38. ])
  39. def test_intracranial_volume_file_read_volume_mm3_not_found(volume_file_path):
  40. volume_file = IntracranialVolumeFile(path=volume_file_path)
  41. with pytest.raises(FileNotFoundError):
  42. volume_file.read_volume_mm3()
  43. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  44. ('ashs/final/bert_left_heur_volumes.txt',
  45. {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),
  46. ('ashs/final/bert_left_corr_nogray_volumes.txt',
  47. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'nogray'}),
  48. ('ashs/final/bert_left_corr_usegray_volumes.txt',
  49. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'usegray'}),
  50. ('ashs/final/bert_right_heur_volumes.txt',
  51. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  52. ('ashs/final/bert_right_corr_nogray_volumes.txt',
  53. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  54. ('ashs/final/bert_right_corr_usegray_volumes.txt',
  55. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'usegray'}),
  56. ('somewhere/else/bert_right_heur_volumes.txt',
  57. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  58. ('somewhere/else/bert_right_corr_nogray_volumes.txt',
  59. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  60. ('bert_right_heur_volumes.txt',
  61. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  62. ('/foo/bar/alice_20190503_right_corr_nogray_volumes.txt',
  63. {'subject': 'alice_20190503', 'hemisphere': 'right', 'correction': 'nogray'}),
  64. ])
  65. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  66. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  67. assert os.path.abspath(volume_file_path) == volume_file.absolute_path
  68. for attr, value in expected_attrs.items():
  69. assert value == getattr(volume_file, attr)
  70. @pytest.mark.parametrize('volume_file_path', [
  71. 'bert_middle_heur_volumes.txt',
  72. 'bert_right_hear_volumes.txt',
  73. 'bert_right_heur_volumes.nii',
  74. 'bert_left_lfseg_corr_usegray.nii.gz',
  75. ])
  76. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  77. with pytest.raises(Exception):
  78. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  79. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  80. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  81. {'CA1': 678.901,
  82. 'CA2+3': 123.456,
  83. 'DG': 901.234,
  84. 'ERC': 789.012,
  85. 'PHC': 2345.876,
  86. 'PRC': 2345.678,
  87. 'SUB': 457.789}),
  88. ])
  89. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  90. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  91. assert expected_volumes == volume_file.read_volumes_mm3()
  92. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  93. volume_file = HippocampalSubfieldsVolumeFile(
  94. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  95. with pytest.raises(FileNotFoundError):
  96. volume_file.read_volumes_mm3()
  97. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  98. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  99. pandas.DataFrame({
  100. 'subfield': ['CA1', 'CA2+3', 'DG', 'ERC', 'PHC', 'PRC', 'SUB'],
  101. 'volume_mm^3': [679.904, 124.459, 902.237, 789.012, 2346.879, 2346.671, 458.782],
  102. 'subject': 'alice',
  103. 'hemisphere': 'left',
  104. 'correction': None,
  105. })),
  106. ])
  107. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  108. volume_file_path: str, expected_dataframe: pandas.DataFrame):
  109. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  110. assert_volume_frames_equal(left=expected_dataframe,
  111. right=volume_file.read_volumes_dataframe())
  112. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  113. volume_file = HippocampalSubfieldsVolumeFile(
  114. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  115. with pytest.raises(FileNotFoundError):
  116. volume_file.read_volumes_dataframe()
  117. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  118. (os.path.join(SUBJECTS_DIR, 'alice'),
  119. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  120. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt')}),
  121. (os.path.join(SUBJECTS_DIR, 'bert'),
  122. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  123. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  124. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  125. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  126. (SUBJECTS_DIR,
  127. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  128. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  129. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  130. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  131. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  132. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  133. ])
  134. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  135. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  136. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  137. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  138. (SUBJECTS_DIR,
  139. r'^bert_right_',
  140. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  141. (SUBJECTS_DIR,
  142. r'_nogray_volumes.txt$',
  143. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  144. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  145. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  146. ])
  147. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  148. expected_file_paths):
  149. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(
  150. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern))
  151. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)