test_cortical_parcellation_stats.py 13 KB

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