test_cortical_parcellation_stats.py 12 KB

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