test_cortical_parcellation_stats.py 16 KB

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