test_cortical_parcellation_stats.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. """
  2. freesurfer-stats, a Python Library to Read FreeSurfer's Cortical Parcellation Anatomical Statistics
  3. Copyright (C) 2019 Fabian Peter Hammerle <fabian@hammerle.me>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. """
  15. import datetime
  16. import os
  17. import pathlib
  18. import numpy
  19. import pandas.util.testing
  20. import pytest
  21. from conftest import SUBJECTS_DIR
  22. from freesurfer_stats import CorticalParcellationStats
  23. # pylint: disable=too-many-arguments
  24. @pytest.mark.parametrize(
  25. (
  26. "path",
  27. "headers",
  28. "hemisphere",
  29. "whole_brain_measurements",
  30. "structural_measurements_length",
  31. "structural_measurements_subset",
  32. ),
  33. [
  34. (
  35. os.path.join(
  36. SUBJECTS_DIR, "fabian", "stats", "lh.aparc.DKTatlas.stats.short"
  37. ),
  38. {
  39. "CreationTime": datetime.datetime(
  40. 2019, 5, 9, 21, 5, 54, tzinfo=datetime.timezone.utc
  41. ),
  42. "generating_program": "mris_anatomical_stats",
  43. "cvs_version": "Id: mris_anatomical_stats.c,v 1.79"
  44. " 2016/03/14 15:15:34 greve Exp",
  45. "mrisurf.c-cvs_version": "Id: mrisurf.c,v 1.781.2.6"
  46. " 2016/12/27 16:47:14 zkaufman Exp",
  47. "cmdline": "mris_anatomical_stats -th3 -mgz -cortex ../label/lh.cortex.label"
  48. " -f ../stats/lh.aparc.DKTatlas.stats -b -a ../label/lh.aparc.DKTatlas.annot"
  49. " -c ../label/aparc.annot.DKTatlas.ctab fabian lh white",
  50. "sysname": "Linux",
  51. "hostname": "another-hostname",
  52. "machine": "x86_64",
  53. "user": "some-username",
  54. "SUBJECTS_DIR": "/home/some-username/freesurfer-subjects",
  55. "anatomy_type": "surface",
  56. "subjectname": "fabian",
  57. "hemi": "lh",
  58. "AnnotationFile": "../label/lh.aparc.DKTatlas.annot",
  59. "AnnotationFileTimeStamp": datetime.datetime(2019, 5, 9, 23, 5, 40),
  60. },
  61. "left",
  62. {
  63. "white_surface_total_area_mm^2": 98553,
  64. "mean_thickness_mm": 2.50462,
  65. "brain_segmentation_volume_mm^3": 1327432.000000,
  66. "brain_segmentation_volume_without_ventricles_mm^3": 1316285.000000,
  67. "brain_segmentation_volume_without_ventricles_from_surf_mm^3": 1315572.548920,
  68. "total_cortical_gray_matter_volume_mm^3": 553998.311189,
  69. "supratentorial_volume_mm^3": 1172669.548920,
  70. "supratentorial_volume_without_ventricles_mm^3": 1164180.548920,
  71. "estimated_total_intracranial_volume_mm^3": 1670487.274486,
  72. },
  73. 3,
  74. [
  75. {
  76. "structure_name": "caudalanteriorcingulate",
  77. "number_of_vertices": 2061,
  78. "surface_area_mm^2": 1472,
  79. "gray_matter_volume_mm^3": 4258,
  80. "average_thickness_mm": 2.653,
  81. "thickness_stddev_mm": 0.644,
  82. "integrated_rectified_mean_curvature_mm^-1": 0.135,
  83. "integrated_rectified_gaussian_curvature_mm^-2": 0.020,
  84. "folding_index": 27,
  85. "intrinsic_curvature_index": 1.6,
  86. },
  87. {
  88. "structure_name": "caudalmiddlefrontal",
  89. "number_of_vertices": 4451,
  90. "surface_area_mm^2": 3039,
  91. "gray_matter_volume_mm^3": 8239,
  92. "average_thickness_mm": 2.456,
  93. "thickness_stddev_mm": 0.486,
  94. "integrated_rectified_mean_curvature_mm^-1": 0.116,
  95. "integrated_rectified_gaussian_curvature_mm^-2": 0.020,
  96. "folding_index": 42,
  97. "intrinsic_curvature_index": 3.7,
  98. },
  99. {
  100. "structure_name": "insula",
  101. "number_of_vertices": 3439,
  102. "surface_area_mm^2": 2304,
  103. "gray_matter_volume_mm^3": 7594,
  104. "average_thickness_mm": 3.193,
  105. "thickness_stddev_mm": 0.620,
  106. "integrated_rectified_mean_curvature_mm^-1": 0.116,
  107. "integrated_rectified_gaussian_curvature_mm^-2": 0.027,
  108. "folding_index": 33,
  109. "intrinsic_curvature_index": 3.5,
  110. },
  111. ],
  112. ),
  113. (
  114. os.path.join(SUBJECTS_DIR, "fabian", "stats", "rh.aparc.pial.stats.short"),
  115. {
  116. "CreationTime": datetime.datetime(
  117. 2019, 5, 9, 21, 3, 42, tzinfo=datetime.timezone.utc
  118. ),
  119. "generating_program": "mris_anatomical_stats",
  120. "cvs_version": "Id: mris_anatomical_stats.c,v 1.79"
  121. " 2016/03/14 15:15:34 greve Exp",
  122. "mrisurf.c-cvs_version": "Id: mrisurf.c,v 1.781.2.6"
  123. " 2016/12/27 16:47:14 zkaufman Exp",
  124. "cmdline": "mris_anatomical_stats -th3 -mgz -cortex ../label/rh.cortex.label"
  125. " -f ../stats/rh.aparc.pial.stats -b -a ../label/rh.aparc.annot"
  126. " -c ../label/aparc.annot.ctab fabian rh pial",
  127. "sysname": "Linux",
  128. "hostname": "some-hostname",
  129. "machine": "x86_64",
  130. "user": "some-username",
  131. "SUBJECTS_DIR": "/home/some-username/freesurfer-subjects",
  132. "anatomy_type": "surface",
  133. "subjectname": "fabian",
  134. "hemi": "rh",
  135. "AnnotationFile": "../label/rh.aparc.annot",
  136. "AnnotationFileTimeStamp": datetime.datetime(2019, 5, 9, 22, 27, 28),
  137. },
  138. "right",
  139. {
  140. "pial_surface_total_area_mm^2": 121260,
  141. "mean_thickness_mm": 2.4817,
  142. "brain_segmentation_volume_mm^3": 1327432.000000,
  143. "brain_segmentation_volume_without_ventricles_mm^3": 1316285.000000,
  144. "brain_segmentation_volume_without_ventricles_from_surf_mm^3": 1315572.548920,
  145. "total_cortical_gray_matter_volume_mm^3": 553998.311189,
  146. "supratentorial_volume_mm^3": 1172669.548920,
  147. "supratentorial_volume_without_ventricles_mm^3": 1164180.548920,
  148. "estimated_total_intracranial_volume_mm^3": 1670487.274486,
  149. },
  150. 2,
  151. [
  152. {
  153. "structure_name": "bankssts",
  154. "number_of_vertices": 1344,
  155. "surface_area_mm^2": 825,
  156. "gray_matter_volume_mm^3": 2171,
  157. "average_thickness_mm": 2.436,
  158. "thickness_stddev_mm": 0.381,
  159. "integrated_rectified_mean_curvature_mm^-1": 0.115,
  160. "integrated_rectified_gaussian_curvature_mm^-2": 0.028,
  161. "folding_index": 19,
  162. "intrinsic_curvature_index": 1.7,
  163. },
  164. {
  165. "structure_name": "transversetemporal",
  166. "number_of_vertices": 651,
  167. "surface_area_mm^2": 545,
  168. "gray_matter_volume_mm^3": 1061,
  169. "average_thickness_mm": 2.251,
  170. "thickness_stddev_mm": 0.317,
  171. "integrated_rectified_mean_curvature_mm^-1": 0.110,
  172. "integrated_rectified_gaussian_curvature_mm^-2": 0.021,
  173. "folding_index": 3,
  174. "intrinsic_curvature_index": 0.6,
  175. },
  176. ],
  177. ),
  178. (
  179. os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.aparc.stats"),
  180. {
  181. "CreationTime": datetime.datetime(
  182. 2020, 5, 4, 22, 20, 26, tzinfo=datetime.timezone.utc
  183. ),
  184. "AnnotationFileTimeStamp": datetime.datetime(2020, 5, 4, 21, 58, 13),
  185. "AnnotationFile": "../label/rh.aparc.annot",
  186. "SUBJECTS_DIR": "/N/dc2/scratch/hayashis/bigred3-workflows"
  187. "/5eb0689676c10ead933d673c/5eb068b076c10e7b013d673f",
  188. "anatomy_type": "surface",
  189. "cmdline": "mris_anatomical_stats -th3 -mgz -cortex ../label/rh.cortex.label "
  190. "-f ../stats/rh.aparc.stats -b -a ../label/rh.aparc.annot -c "
  191. "../label/aparc.annot.ctab output rh white",
  192. "cvs_version": "7.0.0",
  193. "generating_program": "mris_anatomical_stats",
  194. "hemi": "rh",
  195. "hostname": "nid00762",
  196. "machine": "x86_64",
  197. "mrisurf.c-cvs_version": "7.0.0",
  198. "subjectname": "output",
  199. "sysname": "Linux",
  200. "user": "hayashis",
  201. "BrainVolStatsFixed": "NotNeeded because voxelvolume=1mm3",
  202. },
  203. "right",
  204. {
  205. "white_surface_total_area_mm^2": 83579.2,
  206. "mean_thickness_mm": 2.35815,
  207. "brain_segmentation_volume_mm^3": 1169408.0,
  208. "brain_segmentation_volume_without_ventricles_mm^3": 1157593.0,
  209. "brain_segmentation_volume_without_ventricles_from_surf_mm^3": 1157593.0,
  210. "total_cortical_gray_matter_volume_mm^3": 454587.696158,
  211. "supratentorial_volume_mm^3": 1023873.0,
  212. "supratentorial_volume_without_ventricles_mm^3": 1012058.0,
  213. "estimated_total_intracranial_volume_mm^3": 1420434.160521,
  214. },
  215. 34,
  216. [
  217. {
  218. "structure_name": "bankssts",
  219. "number_of_vertices": 1094,
  220. "surface_area_mm^2": 757,
  221. "gray_matter_volume_mm^3": 1725,
  222. "average_thickness_mm": 2.215,
  223. "thickness_stddev_mm": 0.544,
  224. "integrated_rectified_mean_curvature_mm^-1": 0.109,
  225. "integrated_rectified_gaussian_curvature_mm^-2": 0.025,
  226. "folding_index": 9,
  227. "intrinsic_curvature_index": 1.1,
  228. },
  229. {
  230. "structure_name": "caudalanteriorcingulate",
  231. "number_of_vertices": 1137,
  232. "surface_area_mm^2": 780,
  233. "gray_matter_volume_mm^3": 2327,
  234. "average_thickness_mm": 2.842,
  235. "thickness_stddev_mm": 0.667,
  236. "integrated_rectified_mean_curvature_mm^-1": 0.116,
  237. "integrated_rectified_gaussian_curvature_mm^-2": 0.021,
  238. "folding_index": 11,
  239. "intrinsic_curvature_index": 1.0,
  240. },
  241. {
  242. "structure_name": "caudalmiddlefrontal",
  243. "number_of_vertices": 3126,
  244. "surface_area_mm^2": 2218,
  245. "gray_matter_volume_mm^3": 5978,
  246. "average_thickness_mm": 2.447,
  247. "thickness_stddev_mm": 0.605,
  248. "integrated_rectified_mean_curvature_mm^-1": 0.122,
  249. "integrated_rectified_gaussian_curvature_mm^-2": 0.024,
  250. "folding_index": 28,
  251. "intrinsic_curvature_index": 3.1,
  252. },
  253. ],
  254. ),
  255. ],
  256. )
  257. def test_read(
  258. path,
  259. headers,
  260. hemisphere,
  261. whole_brain_measurements,
  262. structural_measurements_length,
  263. structural_measurements_subset,
  264. ):
  265. stats = CorticalParcellationStats.read(path)
  266. assert headers == stats.headers
  267. assert hemisphere == stats.hemisphere
  268. pandas.util.testing.assert_frame_equal(
  269. left=pandas.DataFrame([whole_brain_measurements]),
  270. right=stats.whole_brain_measurements,
  271. check_like=True, # ignore the order of index & columns
  272. check_dtype=True,
  273. check_names=True,
  274. )
  275. assert list(stats.structural_measurements.columns) == [
  276. "structure_name",
  277. "number_of_vertices",
  278. "surface_area_mm^2",
  279. "gray_matter_volume_mm^3",
  280. "average_thickness_mm",
  281. "thickness_stddev_mm",
  282. "integrated_rectified_mean_curvature_mm^-1",
  283. "integrated_rectified_gaussian_curvature_mm^-2",
  284. "folding_index",
  285. "intrinsic_curvature_index",
  286. ]
  287. assert len(stats.structural_measurements) == structural_measurements_length
  288. pandas.util.testing.assert_frame_equal(
  289. left=pandas.DataFrame(structural_measurements_subset),
  290. right=stats.structural_measurements.iloc[:3],
  291. check_like=True, # ignore the order of index & columns
  292. check_dtype=True,
  293. check_names=True,
  294. )
  295. @pytest.mark.parametrize(
  296. ("path", "structural_measurements_length"),
  297. [
  298. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.BA_exvivo.stats"), 14),
  299. (
  300. os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.BA_exvivo.thresh.stats"),
  301. 14,
  302. ),
  303. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.aparc.DKTatlas.stats"), 31),
  304. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.aparc.a2009s.stats"), 74),
  305. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.aparc.pial.stats"), 34),
  306. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "lh.aparc.stats"), 34),
  307. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.BA_exvivo.stats"), 14),
  308. (
  309. os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.BA_exvivo.thresh.stats"),
  310. 14,
  311. ),
  312. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.aparc.DKTatlas.stats"), 31),
  313. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.aparc.a2009s.stats"), 74),
  314. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.aparc.pial.stats"), 34),
  315. (os.path.join(SUBJECTS_DIR, "soichi", "stats", "rh.aparc.stats"), 34),
  316. ],
  317. )
  318. def test_read_structural_measurements_length(path, structural_measurements_length):
  319. # simple test to verify no exception gets raised, see test_read for comprehensive test
  320. stats = CorticalParcellationStats.read(path)
  321. assert len(stats.structural_measurements) == structural_measurements_length
  322. @pytest.mark.parametrize(
  323. ("line", "expected_column_name", "expected_value"),
  324. [
  325. (
  326. "Measure Cortex, CortexVol Total cortical gray matter volume, 553998.311189, mm^3",
  327. "total_cortical_gray_matter_volume_mm^3",
  328. 553998.311189,
  329. )
  330. ],
  331. )
  332. def test__parse_whole_brain_measurements_line(
  333. line, expected_column_name, expected_value
  334. ):
  335. # pylint: disable=protected-access
  336. column_name, value = CorticalParcellationStats._parse_whole_brain_measurements_line(
  337. line
  338. )
  339. assert column_name == expected_column_name
  340. assert numpy.allclose(value, [expected_value])
  341. @pytest.mark.parametrize(
  342. "line",
  343. ["Measure Cortex, CortexVol Total cortical gray matter volume, 553998.311189"],
  344. )
  345. def test__parse_whole_brain_measurements_line_parse_error(line):
  346. # pylint: disable=protected-access
  347. with pytest.raises(ValueError):
  348. CorticalParcellationStats._parse_whole_brain_measurements_line(line)
  349. @pytest.mark.parametrize(
  350. "path_str",
  351. [os.path.join(SUBJECTS_DIR, "fabian", "stats", "lh.aparc.DKTatlas.stats.short")],
  352. )
  353. def test_read_pathlib(path_str: str):
  354. stats_str = CorticalParcellationStats.read(path_str)
  355. stats_pathlib = CorticalParcellationStats.read(pathlib.Path(path_str))
  356. assert stats_str.headers == stats_pathlib.headers
  357. @pytest.mark.parametrize(
  358. "url",
  359. [
  360. "https://raw.githubusercontent.com/fphammerle/freesurfer-stats"
  361. "/master/tests/subjects/fabian/stats/rh.aparc.stats"
  362. ],
  363. )
  364. def test_read_https(url: str):
  365. stats = CorticalParcellationStats.read(url)
  366. assert stats.headers["generating_program"] == "mris_anatomical_stats"