freesurfer_test.py 9.5 KB

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