test_cortical_parcellation_stats.py 14 KB

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