ashs_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_series'), [
  44. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'),
  45. pandas.Series(
  46. data=[1234560.0],
  47. name='volume_mm^3',
  48. index=pandas.Index(data=['bert'], name='subject'),
  49. )),
  50. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt'),
  51. pandas.Series(
  52. data=[1543200.0],
  53. name='volume_mm^3',
  54. index=pandas.Index(data=['alice'], name='subject'),
  55. )),
  56. ])
  57. def test_intracranial_volume_file_read_volume_series_single(volume_file_path, expected_series):
  58. volume_file = IntracranialVolumeFile(path=volume_file_path)
  59. pandas.testing.assert_series_equal(
  60. left=expected_series,
  61. right=volume_file.read_volume_series(),
  62. check_dtype=True,
  63. check_names=True,
  64. )
  65. @pytest.mark.parametrize(('volume_file_paths', 'expected_series'), [
  66. ([os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt'),
  67. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt')],
  68. pandas.Series(
  69. data=[1234560.0, 1543200.0],
  70. name='volume_mm^3',
  71. index=pandas.Index(data=['bert', 'alice'], name='subject'),
  72. )),
  73. ])
  74. def test_intracranial_volume_file_read_volume_series_concat(volume_file_paths, expected_series):
  75. volume_series = pandas.concat(
  76. IntracranialVolumeFile(path=p).read_volume_series()
  77. for p in volume_file_paths)
  78. pandas.testing.assert_series_equal(
  79. left=expected_series,
  80. right=volume_series,
  81. check_dtype=True,
  82. check_names=True,
  83. )
  84. @pytest.mark.parametrize('volume_file_path', [
  85. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'BERT_icv.txt'),
  86. ])
  87. def test_intracranial_volume_file_read_volume_series_not_found(volume_file_path):
  88. volume_file = IntracranialVolumeFile(path=volume_file_path)
  89. with pytest.raises(FileNotFoundError):
  90. volume_file.read_volume_series()
  91. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  92. ('ashs/final/bert_left_heur_volumes.txt',
  93. {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),
  94. ('ashs/final/bert_left_corr_nogray_volumes.txt',
  95. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'nogray'}),
  96. ('ashs/final/bert_left_corr_usegray_volumes.txt',
  97. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'usegray'}),
  98. ('ashs/final/bert_right_heur_volumes.txt',
  99. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  100. ('ashs/final/bert_right_corr_nogray_volumes.txt',
  101. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  102. ('ashs/final/bert_right_corr_usegray_volumes.txt',
  103. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'usegray'}),
  104. ('somewhere/else/bert_right_heur_volumes.txt',
  105. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  106. ('somewhere/else/bert_right_corr_nogray_volumes.txt',
  107. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  108. ('bert_right_heur_volumes.txt',
  109. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  110. ('/foo/bar/alice_20190503_right_corr_nogray_volumes.txt',
  111. {'subject': 'alice_20190503', 'hemisphere': 'right', 'correction': 'nogray'}),
  112. ])
  113. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  114. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  115. assert os.path.abspath(volume_file_path) == volume_file.absolute_path
  116. for attr, value in expected_attrs.items():
  117. assert value == getattr(volume_file, attr)
  118. @pytest.mark.parametrize('volume_file_path', [
  119. 'bert_middle_heur_volumes.txt',
  120. 'bert_right_hear_volumes.txt',
  121. 'bert_right_heur_volumes.nii',
  122. 'bert_left_lfseg_corr_usegray.nii.gz',
  123. ])
  124. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  125. with pytest.raises(Exception):
  126. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  127. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  128. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  129. {'CA1': 678.901,
  130. 'CA2+3': 123.456,
  131. 'DG': 901.234,
  132. 'ERC': 789.012,
  133. 'PHC': 2345.876,
  134. 'PRC': 2345.678,
  135. 'SUB': 457.789}),
  136. ])
  137. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  138. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  139. assert expected_volumes == volume_file.read_volumes_mm3()
  140. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  141. volume_file = HippocampalSubfieldsVolumeFile(
  142. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  143. with pytest.raises(FileNotFoundError):
  144. volume_file.read_volumes_mm3()
  145. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  146. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  147. pandas.DataFrame({
  148. 'subfield': ['CA1', 'CA2+3', 'DG', 'ERC', 'PHC', 'PRC', 'SUB'],
  149. 'volume_mm^3': [679.904, 124.459, 902.237, 789.012, 2346.879, 2346.671, 458.782],
  150. 'subject': 'alice',
  151. 'hemisphere': 'left',
  152. 'correction': None,
  153. })),
  154. ])
  155. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  156. volume_file_path: str, expected_dataframe: pandas.DataFrame):
  157. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  158. assert_volume_frames_equal(left=expected_dataframe,
  159. right=volume_file.read_volumes_dataframe())
  160. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  161. volume_file = HippocampalSubfieldsVolumeFile(
  162. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  163. with pytest.raises(FileNotFoundError):
  164. volume_file.read_volumes_dataframe()
  165. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  166. (os.path.join(SUBJECTS_DIR, 'alice'),
  167. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  168. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt')}),
  169. (os.path.join(SUBJECTS_DIR, 'bert'),
  170. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  171. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  172. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  173. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  174. (SUBJECTS_DIR,
  175. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  176. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  177. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  178. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  179. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  180. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  181. ])
  182. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  183. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  184. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  185. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  186. (SUBJECTS_DIR,
  187. r'^bert_right_',
  188. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  189. (SUBJECTS_DIR,
  190. r'_nogray_volumes.txt$',
  191. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  192. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  193. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  194. ])
  195. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  196. expected_file_paths):
  197. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(
  198. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern))
  199. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)