ashs_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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='intercranial_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='intercranial_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='intercranial_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(('root_dir_path', 'expected_file_paths'), [
  92. (os.path.join(SUBJECTS_DIR, 'bert'),
  93. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt')}),
  94. (os.path.join(SUBJECTS_DIR, 'alice'),
  95. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt')}),
  96. (SUBJECTS_DIR,
  97. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt'),
  98. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt')}),
  99. ])
  100. def test_intracranial_volume_file_find(root_dir_path, expected_file_paths):
  101. volume_files_iterator = IntracranialVolumeFile.find(
  102. root_dir_path=root_dir_path)
  103. assert expected_file_paths == set(
  104. f.absolute_path for f in volume_files_iterator)
  105. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  106. (SUBJECTS_DIR,
  107. r'^\w{4,6}_icv.txt$',
  108. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt'),
  109. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_icv.txt')}),
  110. (SUBJECTS_DIR,
  111. r'^\w{5,6}_icv.txt$',
  112. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_icv.txt')}),
  113. (SUBJECTS_DIR,
  114. r'^\w{7,}_icv.txt$',
  115. set()),
  116. ])
  117. def test_intracranial_volume_file_find_pattern(
  118. root_dir_path, filename_pattern, expected_file_paths):
  119. volume_files_iterator = IntracranialVolumeFile.find(
  120. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern))
  121. assert expected_file_paths == set(
  122. f.absolute_path for f in volume_files_iterator)
  123. @pytest.mark.parametrize(('volume_file_path', 'expected_attrs'), [
  124. ('ashs/final/bert_left_heur_volumes.txt',
  125. {'subject': 'bert', 'hemisphere': 'left', 'correction': None}),
  126. ('ashs/final/bert_left_corr_nogray_volumes.txt',
  127. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'nogray'}),
  128. ('ashs/final/bert_left_corr_usegray_volumes.txt',
  129. {'subject': 'bert', 'hemisphere': 'left', 'correction': 'usegray'}),
  130. ('ashs/final/bert_right_heur_volumes.txt',
  131. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  132. ('ashs/final/bert_right_corr_nogray_volumes.txt',
  133. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  134. ('ashs/final/bert_right_corr_usegray_volumes.txt',
  135. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'usegray'}),
  136. ('somewhere/else/bert_right_heur_volumes.txt',
  137. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  138. ('somewhere/else/bert_right_corr_nogray_volumes.txt',
  139. {'subject': 'bert', 'hemisphere': 'right', 'correction': 'nogray'}),
  140. ('bert_right_heur_volumes.txt',
  141. {'subject': 'bert', 'hemisphere': 'right', 'correction': None}),
  142. ('/foo/bar/alice_20190503_right_corr_nogray_volumes.txt',
  143. {'subject': 'alice_20190503', 'hemisphere': 'right', 'correction': 'nogray'}),
  144. ])
  145. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  146. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  147. assert os.path.abspath(volume_file_path) == volume_file.absolute_path
  148. for attr, value in expected_attrs.items():
  149. assert value == getattr(volume_file, attr)
  150. @pytest.mark.parametrize('volume_file_path', [
  151. 'bert_middle_heur_volumes.txt',
  152. 'bert_right_hear_volumes.txt',
  153. 'bert_right_heur_volumes.nii',
  154. 'bert_left_lfseg_corr_usegray.nii.gz',
  155. ])
  156. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  157. with pytest.raises(Exception):
  158. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  159. @pytest.mark.parametrize(('volume_file_path', 'expected_volumes'), [
  160. (os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  161. {'CA1': 678.901,
  162. 'CA2+3': 123.456,
  163. 'DG': 901.234,
  164. 'ERC': 789.012,
  165. 'PHC': 2345.876,
  166. 'PRC': 2345.678,
  167. 'SUB': 457.789}),
  168. ])
  169. def test_hippocampal_subfields_volume_file_read_volumes_mm3(volume_file_path, expected_volumes):
  170. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  171. assert expected_volumes == volume_file.read_volumes_mm3()
  172. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  173. volume_file = HippocampalSubfieldsVolumeFile(
  174. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  175. with pytest.raises(FileNotFoundError):
  176. volume_file.read_volumes_mm3()
  177. @pytest.mark.parametrize(('volume_file_path', 'expected_dataframe'), [
  178. (os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  179. pandas.DataFrame({
  180. 'subfield': ['CA1', 'CA2+3', 'DG', 'ERC', 'PHC', 'PRC', 'SUB'],
  181. 'volume_mm^3': [679.904, 124.459, 902.237, 789.012, 2346.879, 2346.671, 458.782],
  182. 'subject': 'alice',
  183. 'hemisphere': 'left',
  184. 'correction': None,
  185. })),
  186. ])
  187. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  188. volume_file_path: str, expected_dataframe: pandas.DataFrame):
  189. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  190. assert_volume_frames_equal(left=expected_dataframe,
  191. right=volume_file.read_volumes_dataframe())
  192. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  193. volume_file = HippocampalSubfieldsVolumeFile(
  194. path=os.path.join(SUBJECTS_DIR, 'nobert', 'final', 'bert_left_corr_nogray_volumes.txt'))
  195. with pytest.raises(FileNotFoundError):
  196. volume_file.read_volumes_dataframe()
  197. @pytest.mark.parametrize(('root_dir_path', 'expected_file_paths'), [
  198. (os.path.join(SUBJECTS_DIR, 'alice'),
  199. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  200. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt')}),
  201. (os.path.join(SUBJECTS_DIR, 'bert'),
  202. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  203. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  204. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  205. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  206. (SUBJECTS_DIR,
  207. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_heur_volumes.txt'),
  208. os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  209. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  210. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_usegray_volumes.txt'),
  211. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_heur_volumes.txt'),
  212. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  213. ])
  214. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  215. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  216. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  217. @pytest.mark.parametrize(('root_dir_path', 'filename_pattern', 'expected_file_paths'), [
  218. (SUBJECTS_DIR,
  219. r'^bert_right_',
  220. {os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  221. (SUBJECTS_DIR,
  222. r'_nogray_volumes.txt$',
  223. {os.path.join(SUBJECTS_DIR, 'alice', 'final', 'alice_left_corr_nogray_volumes.txt'),
  224. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_left_corr_nogray_volumes.txt'),
  225. os.path.join(SUBJECTS_DIR, 'bert', 'final', 'bert_right_corr_nogray_volumes.txt')}),
  226. ])
  227. def test_hippocampal_subfields_volume_file_find_pattern(root_dir_path, filename_pattern,
  228. expected_file_paths):
  229. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(
  230. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern))
  231. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)