freesurfer_test.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. "bert_left_corr_nogray_volumes.txt",
  118. ],
  119. )
  120. def test_hippocampal_subfields_volume_file_init_invalid_path(volume_file_path):
  121. with pytest.raises(Exception):
  122. HippocampalSubfieldsVolumeFile(path=volume_file_path)
  123. @pytest.mark.parametrize(
  124. ("volume_file_path", "expected_volumes"),
  125. [
  126. (
  127. os.path.join(SUBJECTS_DIR, "bert/mri/lh.hippoSfVolumes-T1.v10.txt"),
  128. {
  129. "Hippocampal_tail": 123.456789,
  130. "subiculum": 234.567891,
  131. "CA1": 34.567891,
  132. "hippocampal-fissure": 345.678912,
  133. "presubiculum": 456.789123,
  134. "parasubiculum": 45.678912,
  135. "molecular_layer_HP": 56.789123,
  136. "GC-ML-DG": 567.891234,
  137. "CA3": 678.912345,
  138. "CA4": 789.123456,
  139. "fimbria": 89.123456,
  140. "HATA": 91.234567,
  141. "Whole_hippocampus": 1234.567899,
  142. },
  143. )
  144. ],
  145. )
  146. def test_hippocampal_subfields_volume_file_read_volumes_mm3(
  147. volume_file_path, expected_volumes
  148. ):
  149. volume_file = HippocampalSubfieldsVolumeFile(path=volume_file_path)
  150. assert volume_file.t1_input
  151. assert expected_volumes == volume_file.read_volumes_mm3()
  152. def test_hippocampal_subfields_volume_file_read_volumes_mm3_not_found():
  153. volume_file = HippocampalSubfieldsVolumeFile(
  154. path=os.path.join(SUBJECTS_DIR, "non-existing", "lh.hippoSfVolumes-T1.v10.txt")
  155. )
  156. with pytest.raises(FileNotFoundError):
  157. volume_file.read_volumes_mm3()
  158. @pytest.mark.parametrize(
  159. ("volume_file_path", "expected_dataframe"),
  160. [
  161. (
  162. os.path.join(SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"),
  163. pandas.DataFrame(
  164. {
  165. "subfield": [
  166. "Hippocampal_tail",
  167. "subiculum",
  168. "CA1",
  169. "hippocampal-fissure",
  170. "presubiculum",
  171. "parasubiculum",
  172. "molecular_layer_HP",
  173. "GC-ML-DG",
  174. "CA3",
  175. "CA4",
  176. "fimbria",
  177. "HATA",
  178. "Whole_hippocampus",
  179. ],
  180. "volume_mm^3": [
  181. 173.456789,
  182. 734.567891,
  183. 34.567891,
  184. 345.678917,
  185. 456.789173,
  186. 45.678917,
  187. 56.789173,
  188. 567.891734,
  189. 678.917345,
  190. 789.173456,
  191. 89.173456,
  192. 91.734567,
  193. 1734.567899,
  194. ],
  195. "subject": "alice",
  196. "hemisphere": "left",
  197. "T1_input": True,
  198. "analysis_id": None,
  199. }
  200. ),
  201. )
  202. ],
  203. )
  204. def test_hippocampal_subfields_volume_file_read_volumes_dataframe(
  205. volume_file_path: str, expected_dataframe: pandas.DataFrame
  206. ):
  207. assert_volume_frames_equal(
  208. left=expected_dataframe,
  209. right=HippocampalSubfieldsVolumeFile(
  210. path=volume_file_path
  211. ).read_volumes_dataframe(),
  212. )
  213. def test_hippocampal_subfields_volume_file_read_volumes_dataframe_not_found():
  214. volume_file = HippocampalSubfieldsVolumeFile(
  215. path=os.path.join(SUBJECTS_DIR, "non-existing", "lh.hippoSfVolumes-T1.v10.txt")
  216. )
  217. with pytest.raises(FileNotFoundError):
  218. volume_file.read_volumes_dataframe()
  219. @pytest.mark.parametrize(
  220. ("root_dir_path", "expected_file_paths"),
  221. [
  222. (
  223. SUBJECTS_DIR,
  224. {
  225. os.path.join(
  226. SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  227. ),
  228. os.path.join(
  229. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  230. ),
  231. os.path.join(
  232. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  233. ),
  234. },
  235. ),
  236. (
  237. os.path.join(SUBJECTS_DIR, "bert"),
  238. {
  239. os.path.join(
  240. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  241. ),
  242. os.path.join(
  243. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  244. ),
  245. },
  246. ),
  247. (
  248. os.path.join(SUBJECTS_DIR, "bert", "mri"),
  249. {
  250. os.path.join(
  251. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  252. ),
  253. os.path.join(
  254. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  255. ),
  256. },
  257. ),
  258. ],
  259. )
  260. def test_hippocampal_subfields_volume_file_find(root_dir_path, expected_file_paths):
  261. volume_files = list(
  262. HippocampalSubfieldsVolumeFile.find(root_dir_path=root_dir_path)
  263. )
  264. assert all(
  265. "hippoSfVolumes" in os.path.basename(f.absolute_path) for f in volume_files
  266. )
  267. assert expected_file_paths == set(f.absolute_path for f in volume_files)
  268. @pytest.mark.parametrize(
  269. ("root_dir_path", "filename_pattern", "expected_file_paths"),
  270. [
  271. (
  272. SUBJECTS_DIR,
  273. r"hippoSfVolumes-T1\.v10",
  274. {
  275. os.path.join(
  276. SUBJECTS_DIR, "alice", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  277. ),
  278. os.path.join(
  279. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1.v10.txt"
  280. ),
  281. },
  282. ),
  283. (
  284. os.path.join(SUBJECTS_DIR, "bert"),
  285. r"hippoSfVolumes-T1-T2",
  286. {
  287. os.path.join(
  288. SUBJECTS_DIR, "bert", "mri", "lh.hippoSfVolumes-T1-T2.v10.txt"
  289. )
  290. },
  291. ),
  292. ],
  293. )
  294. def test_hippocampal_subfields_volume_file_find_pattern(
  295. root_dir_path, filename_pattern, expected_file_paths
  296. ):
  297. assert expected_file_paths == set(
  298. f.absolute_path
  299. for f in HippocampalSubfieldsVolumeFile.find(
  300. root_dir_path=root_dir_path, filename_regex=re.compile(filename_pattern)
  301. )
  302. )