ashs_test.py 5.2 KB

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