freesurfer_test.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # pylint: disable=missing-module-docstring
  2. import os
  3. import re
  4. import pandas
  5. import pytest
  6. from freesurfer_volume_reader.freesurfer import HippocampalSubfieldsVolumeFile
  7. from conftest import SUBJECTS_DIR, assert_volume_frames_equal
  8. @pytest.mark.parametrize(
  9. ("volume_file_path", "expected_attrs"),
  10. [
  11. (
  12. "bert/mri/lh.hippoSfVolumes-T1.v10.txt",
  13. {
  14. "subject": "bert",
  15. "hemisphere": "left",
  16. "t1_input": True,
  17. "analysis_id": None,
  18. },
  19. ),
  20. (
  21. "bert/mri/lh.hippoSfVolumes-T1-T2.v10.txt",
  22. {
  23. "subject": "bert",
  24. "hemisphere": "left",
  25. "t1_input": True,
  26. "analysis_id": "T2",
  27. },
  28. ),
  29. (
  30. "bert/mri/lh.hippoSfVolumes-T2.v10.txt",
  31. {
  32. "subject": "bert",
  33. "hemisphere": "left",
  34. "t1_input": False,
  35. "analysis_id": "T2",
  36. },
  37. ),
  38. (
  39. "bert/mri/lh.hippoSfVolumes-T1-T2-high-res.v10.txt",
  40. {
  41. "subject": "bert",
  42. "hemisphere": "left",
  43. "t1_input": True,
  44. "analysis_id": "T2-high-res",
  45. },
  46. ),
  47. (
  48. "bert/mri/lh.hippoSfVolumes-T2-high-res.v10.txt",
  49. {
  50. "subject": "bert",
  51. "hemisphere": "left",
  52. "t1_input": False,
  53. "analysis_id": "T2-high-res",
  54. },
  55. ),
  56. (
  57. "bert/mri/lh.hippoSfVolumes-PD.v10.txt",
  58. {
  59. "subject": "bert",
  60. "hemisphere": "left",
  61. "t1_input": False,
  62. "analysis_id": "PD",
  63. },
  64. ),
  65. (
  66. "bert/mri/rh.hippoSfVolumes-T1.v10.txt",
  67. {
  68. "subject": "bert",
  69. "hemisphere": "right",
  70. "t1_input": True,
  71. "analysis_id": None,
  72. },
  73. ),
  74. (
  75. "bert/mri/rh.hippoSfVolumes-T1-T2.v10.txt",
  76. {
  77. "subject": "bert",
  78. "hemisphere": "right",
  79. "t1_input": True,
  80. "analysis_id": "T2",
  81. },
  82. ),
  83. (
  84. "freesurfer/subjects/bert/mri/lh.hippoSfVolumes-T1.v10.txt",
  85. {
  86. "subject": "bert",
  87. "hemisphere": "left",
  88. "t1_input": True,
  89. "analysis_id": None,
  90. },
  91. ),
  92. (
  93. "../../bert/mri/lh.hippoSfVolumes-T1.v10.txt",
  94. {
  95. "subject": "bert",
  96. "hemisphere": "left",
  97. "t1_input": True,
  98. "analysis_id": None,
  99. },
  100. ),
  101. ],
  102. )
  103. def test_hippocampal_subfields_volume_file_init(volume_file_path, expected_attrs):
  104. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  105. assert os.path.basename(volume_file_path) == os.path.basename(
  106. volume_file.absolute_path
  107. )
  108. for attr, value in expected_attrs.items():
  109. assert value == getattr(volume_file, attr)
  110. @pytest.mark.parametrize(
  111. "volume_file_path",
  112. [
  113. "bert/mri/lh.hippoSfLabels-T1.v10.mgz",
  114. "bert/mri/lh.hippoSfVolumes-T1.v9.txt",
  115. "bert/mri/lh.hippoSfVolumes.v10.txt",
  116. "bert/mri/mh.hippoSfVolumes-T1.v10.txt",
  117. ],
  118. )
  119. def test_hippocampal_subfields_volume_file_init_invalid(volume_file_path):
  120. with pytest.raises(Exception):
  121. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  122. @pytest.mark.parametrize(
  123. ("volume_file_path", "expected_volumes"),
  124. [
  125. (
  126. os.path.join(SUBJECTS_DIR, "bert/mri/lh.hippoSfVolumes-T1.v10.txt"),
  127. {
  128. "Hippocampal_tail": 123.456789,
  129. "subiculum": 234.567891,
  130. "CA1": 34.567891,
  131. "hippocampal-fissure": 345.678912,
  132. "presubiculum": 456.789123,
  133. "parasubiculum": 45.678912,
  134. "molecular_layer_HP": 56.789123,
  135. "GC-ML-DG": 567.891234,
  136. "CA3": 678.912345,
  137. "CA4": 789.123456,
  138. "fimbria": 89.123456,
  139. "HATA": 91.234567,
  140. "Whole_hippocampus": 1234.567899,
  141. },
  142. )
  143. ],
  144. )
  145. def test_hippocampal_subfields_volume_file_read_volumes_mm3(
  146. volume_file_path, expected_volumes
  147. ):
  148. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  149. assert expected_volumes == volume_file.read_volumes_mm3()
  150. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  151. volume_file = HippocampalSubfieldsVolumeFile(
  152. path=os.path.join(SUBJECTS_DIR, "non-existing", "lh.hippoSfVolumes-T1.v10.txt")
  153. )
  154. with pytest.raises(FileNotFoundError):
  155. volume_file.read_volumes_mm3()
  156. @pytest.mark.parametrize(
  157. ("volume_file_path", "expected_dataframe"),
  158. [
  159. (
  160. os.path.join(SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"),
  161. pandas.DataFrame(
  162. {
  163. "subfield": [
  164. "Hippocampal_tail",
  165. "subiculum",
  166. "CA1",
  167. "hippocampal-fissure",
  168. "presubiculum",
  169. "parasubiculum",
  170. "molecular_layer_HP",
  171. "GC-ML-DG",
  172. "CA3",
  173. "CA4",
  174. "fimbria",
  175. "HATA",
  176. "Whole_hippocampus",
  177. ],
  178. "volume_mm^3": [
  179. 173.456789,
  180. 734.567891,
  181. 34.567891,
  182. 345.678917,
  183. 456.789173,
  184. 45.678917,
  185. 56.789173,
  186. 567.891734,
  187. 678.917345,
  188. 789.173456,
  189. 89.173456,
  190. 91.734567,
  191. 1734.567899,
  192. ],
  193. "subject": "alice",
  194. "hemisphere": "left",
  195. "T1_input": True,
  196. "analysis_id": None,
  197. }
  198. ),
  199. )
  200. ],
  201. )
  202. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  203. volume_file_path: str, expected_dataframe: pandas.DataFrame
  204. ):
  205. assert_volume_frames_equal(
  206. left=expected_dataframe,
  207. right=HippocampalSubfieldsVolumeFile(
  208. path=volume_file_path
  209. ).read_volumes_dataframe(),
  210. )
  211. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  212. volume_file = HippocampalSubfieldsVolumeFile(
  213. path=os.path.join(SUBJECTS_DIR, "non-existing", "lh.hippoSfVolumes-T1.v10.txt")
  214. )
  215. with pytest.raises(FileNotFoundError):
  216. volume_file.read_volumes_dataframe()
  217. @pytest.mark.parametrize(
  218. ("root_dir_path", "expected_file_paths"),
  219. [
  220. (
  221. SUBJECTS_DIR,
  222. {
  223. os.path.join(
  224. SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  225. ),
  226. os.path.join(
  227. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  228. ),
  229. os.path.join(
  230. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  231. ),
  232. },
  233. ),
  234. (
  235. os.path.join(SUBJECTS_DIR, "bert"),
  236. {
  237. os.path.join(
  238. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  239. ),
  240. os.path.join(
  241. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  242. ),
  243. },
  244. ),
  245. (
  246. os.path.join(SUBJECTS_DIR, "bert", "mri"),
  247. {
  248. os.path.join(
  249. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  250. ),
  251. os.path.join(
  252. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  253. ),
  254. },
  255. ),
  256. ],
  257. )
  258. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  259. volume_files_iterator = HippocampalSubfieldsVolumeFile.find(
  260. root_dir_path=root_dir_path
  261. )
  262. assert expected_file_paths == set(f.absolute_path for f in volume_files_iterator)
  263. @pytest.mark.parametrize(
  264. ("root_dir_path", "filename_pattern", "expected_file_paths"),
  265. [
  266. (
  267. SUBJECTS_DIR,
  268. r"hippoSfVolumes-T1\.v10",
  269. {
  270. os.path.join(
  271. SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  272. ),
  273. os.path.join(
  274. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  275. ),
  276. },
  277. ),
  278. (
  279. os.path.join(SUBJECTS_DIR, "bert"),
  280. r"hippoSfVolumes-T1-T2",
  281. {
  282. os.path.join(
  283. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  284. )
  285. },
  286. ),
  287. ],
  288. )
  289. def test_hippocampal_subfields_volume_file_find_pattern(
  290. root_dir_path, filename_pattern, expected_file_paths
  291. ):
  292. assert expected_file_paths == set(
  293. f.absolute_path
  294. for f in HippocampalSubfieldsVolumeFile.find(
  295. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)
  296. )
  297. )