Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from soichih/master
Browse files Browse the repository at this point in the history
updated so that it can handle non-measure line in stats header (freesurfer 7)

#1
  • Loading branch information
fphammerle committed May 6, 2020
2 parents 79627eb + 5b1985b commit 7a3b034
Show file tree
Hide file tree
Showing 20 changed files with 2,039 additions and 9 deletions.
19 changes: 10 additions & 9 deletions freesurfer_stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,16 @@ def _read(self, stream: typing.TextIO) -> None:
self.whole_brain_measurements = pandas.DataFrame()
line = self._read_header_line(stream)
while not line.startswith('NTableCols'):
key, name, value, unit \
= self._GENERAL_MEASUREMENTS_REGEX.match(line).groups()
if key == 'SupraTentorialVolNotVent' and name.lower() == 'supratentorial volume':
name += ' Without Ventricles'
column_name = self._format_column_name(name, unit)
assert column_name not in self.whole_brain_measurements, \
(key, name, column_name, self.whole_brain_measurements)
self.whole_brain_measurements[column_name] \
= pandas.to_numeric([value], errors='raise')
match = self._GENERAL_MEASUREMENTS_REGEX.match(line)
if match:
key, name, value, unit = match.groups()
if key == 'SupraTentorialVolNotVent' and name.lower() == 'supratentorial volume':
name += ' Without Ventricles'
column_name = self._format_column_name(name, unit)
assert column_name not in self.whole_brain_measurements, \
(key, name, column_name, self.whole_brain_measurements)
self.whole_brain_measurements[column_name] \
= pandas.to_numeric([value], errors='raise')
line = self._read_header_line(stream)
columns = self._read_column_attributes(
int(line[len('NTableCols '):]), stream)
Expand Down
124 changes: 124 additions & 0 deletions tests/subjects/soichi/stats/aseg.stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Title Segmentation Statistics
#
# generating_program mri_segstats
# cvs_version 7.0.0
# cmdline mri_segstats --seed 1234 --seg mri/aseg.mgz --sum stats/aseg.stats --pv mri/norm.mgz --empty --brainmask mri/brainmask.mgz --brain-vol-from-seg --excludeid 0 --excl-ctxgmwm --supratent --subcortgray --in mri/norm.mgz --in-intensity-name norm --in-intensity-units MR --etiv --surf-wm-vol --surf-ctx-vol --totalgray --euler --ctab /usr/local/freesurfer/ASegStatsLUT.txt --subject output
# sysname Linux
# hostname nid00762
# machine x86_64
# user hayashis
# anatomy_type volume
#
# SUBJECTS_DIR /N/dc2/scratch/hayashis/bigred3-workflows/5eb0689676c10ead933d673c/5eb068b076c10e7b013d673f
# subjectname output
# BrainVolStatsFixed-NotNeeded because voxelvolume=1mm3
# Measure BrainSeg, BrainSegVol, Brain Segmentation Volume, 1169408.000000, mm^3
# Measure BrainSegNotVent, BrainSegVolNotVent, Brain Segmentation Volume Without Ventricles, 1157593.000000, mm^3
# Measure VentricleChoroidVol, VentricleChoroidVol, Volume of ventricles and choroid plexus, 9296.000000, mm^3
# Measure lhCortex, lhCortexVol, Left hemisphere cortical gray matter volume, 232345.220912, mm^3
# Measure rhCortex, rhCortexVol, Right hemisphere cortical gray matter volume, 222242.475246, mm^3
# Measure Cortex, CortexVol, Total cortical gray matter volume, 454587.696158, mm^3
# Measure lhCerebralWhiteMatter, lhCerebralWhiteMatterVol, Left hemisphere cerebral white matter volume, 251589.500000, mm^3
# Measure rhCerebralWhiteMatter, rhCerebralWhiteMatterVol, Right hemisphere cerebral white matter volume, 244895.500000, mm^3
# Measure CerebralWhiteMatter, CerebralWhiteMatterVol, Total cerebral white matter volume, 496485.000000, mm^3
# Measure SubCortGray, SubCortGrayVol, Subcortical gray matter volume, 62428.000000, mm^3
# Measure TotalGray, TotalGrayVol, Total gray matter volume, 625759.696158, mm^3
# Measure SupraTentorial, SupraTentorialVol, Supratentorial volume, 1023873.000000, mm^3
# Measure SupraTentorialNotVent, SupraTentorialVolNotVent, Supratentorial volume, 1012058.000000, mm^3
# Measure Mask, MaskVol, Mask Volume, 1686052.000000, mm^3
# Measure BrainSegVol-to-eTIV, BrainSegVol-to-eTIV, Ratio of BrainSegVol to eTIV, 0.823275, unitless
# Measure MaskVol-to-eTIV, MaskVol-to-eTIV, Ratio of MaskVol to eTIV, 1.186998, unitless
# Measure lhSurfaceHoles, lhSurfaceHoles, Number of defect holes in lh surfaces prior to fixing, 24, unitless
# Measure rhSurfaceHoles, rhSurfaceHoles, Number of defect holes in rh surfaces prior to fixing, 25, unitless
# Measure SurfaceHoles, SurfaceHoles, Total number of defect holes in surfaces prior to fixing, 49, unitless
# Measure EstimatedTotalIntraCranialVol, eTIV, Estimated Total Intracranial Volume, 1420434.160521, mm^3
# SegVolFile mri/aseg.mgz
# SegVolFileTimeStamp 2020/05/04 22:14:04
# ColorTable /usr/local/freesurfer/ASegStatsLUT.txt
# ColorTableTimeStamp 2020/04/29 22:19:19
# InVolFile mri/norm.mgz
# InVolFileTimeStamp 2020/05/04 19:28:46
# InVolFrame 0
# PVVolFile mri/norm.mgz
# PVVolFileTimeStamp 2020/05/04 19:28:46
# Excluding Cortical Gray and White Matter
# ExcludeSegId 0 2 3 41 42
# VoxelVolume_mm3 1
# TableCol 1 ColHeader Index
# TableCol 1 FieldName Index
# TableCol 1 Units NA
# TableCol 2 ColHeader SegId
# TableCol 2 FieldName Segmentation Id
# TableCol 2 Units NA
# TableCol 3 ColHeader NVoxels
# TableCol 3 FieldName Number of Voxels
# TableCol 3 Units unitless
# TableCol 4 ColHeader Volume_mm3
# TableCol 4 FieldName Volume
# TableCol 4 Units mm^3
# TableCol 5 ColHeader StructName
# TableCol 5 FieldName Structure Name
# TableCol 5 Units NA
# TableCol 6 ColHeader normMean
# TableCol 6 FieldName Intensity normMean
# TableCol 6 Units MR
# TableCol 7 ColHeader normStdDev
# TableCol 7 FieldName Itensity normStdDev
# TableCol 7 Units MR
# TableCol 8 ColHeader normMin
# TableCol 8 FieldName Intensity normMin
# TableCol 8 Units MR
# TableCol 9 ColHeader normMax
# TableCol 9 FieldName Intensity normMax
# TableCol 9 Units MR
# TableCol 10 ColHeader normRange
# TableCol 10 FieldName Intensity normRange
# TableCol 10 Units MR
# NRows 45
# NTableCols 10
# ColHeaders Index SegId NVoxels Volume_mm3 StructName normMean normStdDev normMin normMax normRange
1 4 5077 5581.1 Left-Lateral-Ventricle 27.3055 15.4437 0.0000 88.0000 88.0000
2 5 132 195.5 Left-Inf-Lat-Vent 53.1970 9.2625 37.0000 76.0000 39.0000
3 7 18074 18350.4 Left-Cerebellum-White-Matter 80.1782 9.0470 36.0000 106.0000 70.0000
4 8 54976 55868.4 Left-Cerebellum-Cortex 48.4004 10.9809 6.0000 98.0000 92.0000
5 10 8612 8178.8 Left-Thalamus 83.3560 10.4341 27.0000 114.0000 87.0000
6 11 3824 3708.7 Left-Caudate 70.4786 9.3153 30.0000 99.0000 69.0000
7 12 5776 5622.3 Left-Putamen 83.7332 7.1692 43.0000 102.0000 59.0000
8 13 2124 2091.0 Left-Pallidum 97.9812 3.8737 79.0000 110.0000 31.0000
9 14 679 741.0 3rd-Ventricle 37.1487 15.2063 0.0000 78.0000 78.0000
10 15 875 1019.4 4th-Ventricle 31.0811 14.2070 0.0000 76.0000 76.0000
11 16 21320 21011.7 Brain-Stem 84.7268 12.0079 25.0000 140.0000 115.0000
12 17 4357 4333.5 Left-Hippocampus 65.6167 8.2438 26.0000 108.0000 82.0000
13 18 1573 1549.1 Left-Amygdala 64.1742 6.3122 41.0000 83.0000 42.0000
14 24 965 1007.5 CSF 41.3772 12.8093 6.0000 68.0000 62.0000
15 26 731 718.7 Left-Accumbens-area 66.7989 5.7314 42.0000 86.0000 44.0000
16 28 4324 4125.1 Left-VentralDC 91.7333 12.2516 31.0000 132.0000 101.0000
17 30 0 0.0 Left-vessel 0.0000 0.0000 0.0000 0.0000 0.0000
18 31 419 356.7 Left-choroid-plexus 51.7375 15.3314 20.0000 80.0000 60.0000
19 43 3051 3370.2 Right-Lateral-Ventricle 35.5257 16.2240 0.0000 81.0000 81.0000
20 44 186 270.7 Right-Inf-Lat-Vent 46.6720 11.2473 14.0000 78.0000 64.0000
21 46 18717 19204.9 Right-Cerebellum-White-Matter 79.0736 9.2274 32.0000 106.0000 74.0000
22 47 53768 54076.3 Right-Cerebellum-Cortex 47.0445 10.8816 10.0000 92.0000 82.0000
23 49 7812 7387.3 Right-Thalamus 80.6749 9.9149 37.0000 103.0000 66.0000
24 50 3740 3580.3 Right-Caudate 73.5816 9.0473 37.0000 105.0000 68.0000
25 51 6114 5882.7 Right-Putamen 82.4882 7.7199 62.0000 122.0000 60.0000
26 52 2201 2163.7 Right-Pallidum 97.5766 3.9017 78.0000 108.0000 30.0000
27 53 4391 4328.9 Right-Hippocampus 65.4297 8.8955 25.0000 120.0000 95.0000
28 54 1715 1665.5 Right-Amygdala 63.4548 7.4180 31.0000 84.0000 53.0000
29 58 751 700.3 Right-Accumbens-area 68.9933 6.9871 49.0000 93.0000 44.0000
30 60 4383 4158.7 Right-VentralDC 88.2469 12.5069 35.0000 131.0000 96.0000
31 62 0 0.0 Right-vessel 0.0000 0.0000 0.0000 0.0000 0.0000
32 63 431 389.1 Right-choroid-plexus 54.3295 13.1272 26.0000 80.0000 54.0000
33 72 0 0.0 5th-Ventricle 0.0000 0.0000 0.0000 0.0000 0.0000
34 77 2244 1880.1 WM-hypointensities 69.7460 12.6019 33.0000 102.0000 69.0000
35 78 0 0.0 Left-WM-hypointensities 0.0000 0.0000 0.0000 0.0000 0.0000
36 79 0 0.0 Right-WM-hypointensities 0.0000 0.0000 0.0000 0.0000 0.0000
37 80 0 0.0 non-WM-hypointensities 0.0000 0.0000 0.0000 0.0000 0.0000
38 81 0 0.0 Left-non-WM-hypointensities 0.0000 0.0000 0.0000 0.0000 0.0000
39 82 0 0.0 Right-non-WM-hypointensities 0.0000 0.0000 0.0000 0.0000 0.0000
40 85 152 139.0 Optic-Chiasm 67.6645 10.3527 42.0000 107.0000 65.0000
41 251 1297 1197.2 CC_Posterior 98.0948 12.6156 45.0000 113.0000 68.0000
42 252 707 631.7 CC_Mid_Posterior 89.1471 17.0599 20.0000 115.0000 95.0000
43 253 886 789.6 CC_Central 90.2009 16.9791 24.0000 119.0000 95.0000
44 254 905 809.6 CC_Mid_Anterior 93.8497 17.3849 32.0000 127.0000 95.0000
45 255 1192 1124.7 CC_Anterior 102.1720 12.2017 47.0000 126.0000 79.0000
16 changes: 16 additions & 0 deletions tests/subjects/soichi/stats/brainvol.stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Measure BrainSeg, BrainSegVol, Brain Segmentation Volume, 1169408.000000000000, mm^3
# Measure BrainSegNotVent, BrainSegVolNotVent, Brain Segmentation Volume Without Ventricles, 1157593.000000000000, mm^3
# Measure SupraTentorial, SupraTentorialVol, Supratentorial volume, 1023873.000000000000, mm^3
# Measure SupraTentorialNotVent, SupraTentorialVolNotVent, Supratentorial volume, 1012058.000000000000, mm^3
# Measure SubCortGray, SubCortGrayVol, Subcortical gray matter volume, 62428.000000000000, mm^3
# Measure lhCortex, lhCortexVol, Left hemisphere cortical gray matter volume, 232345.220911918441, mm^3
# Measure rhCortex, rhCortexVol, Right hemisphere cortical gray matter volume, 222242.475245694164, mm^3
# Measure Cortex, CortexVol, Total cortical gray matter volume, 454587.696157612605, mm^3
# Measure TotalGray, TotalGrayVol, Total gray matter volume, 625759.696157612605, mm^3
# Measure lhCerebralWhiteMatter, lhCerebralWhiteMatterVol, Left hemisphere cerebral white matter volume, 251589.500000000000, mm^3
# Measure rhCerebralWhiteMatter, rhCerebralWhiteMatterVol, Right hemisphere cerebral white matter volume, 244895.500000000000, mm^3
# Measure CerebralWhiteMatter, CerebralWhiteMatterVol, Total cerebral white matter volume, 496485.000000000000, mm^3
# Measure Mask, MaskVol, Mask Volume, 1686052.000000000000, mm^3
# Measure SupraTentorialNotVentVox, SupraTentorialVolNotVentVox, Supratentorial volume voxel count, 1012058.000000000000, mm^3
# Measure BrainSegNotVentSurf, BrainSegVolNotVentSurf, Brain Segmentation Volume Without Ventricles from Surf, 1157593.000000000000, mm^3
# Measure VentricleChoroidVol, VentricleChoroidVol, Volume of ventricles and choroid plexus, 9296.000000000000, mm^3
74 changes: 74 additions & 0 deletions tests/subjects/soichi/stats/lh.BA_exvivo.stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Table of FreeSurfer cortical parcellation anatomical statistics
#
# CreationTime 2020/05/04-22:22:50-GMT
# generating_program mris_anatomical_stats
# cvs_version 7.0.0
# mrisurf.c-cvs_version 7.0.0
# cmdline mris_anatomical_stats -th3 -mgz -f ../stats/lh.BA_exvivo.stats -b -a ./lh.BA_exvivo.annot -c ./BA_exvivo.ctab output lh white
# sysname Linux
# hostname nid00762
# machine x86_64
# user hayashis
#
# SUBJECTS_DIR /N/dc2/scratch/hayashis/bigred3-workflows/5eb0689676c10ead933d673c/5eb068b076c10e7b013d673f
# anatomy_type surface
# subjectname output
# hemi lh
# AnnotationFile ./lh.BA_exvivo.annot
# AnnotationFileTimeStamp 2020/05/04 22:22:46
# Measure Cortex, NumVert, Number of Vertices, 127765, unitless
# Measure Cortex, WhiteSurfArea, White Surface Total Area, 90204.3, mm^2
# BrainVolStatsFixed-NotNeeded because voxelvolume=1mm3
# Measure BrainSeg, BrainSegVol, Brain Segmentation Volume, 1169408.000000, mm^3
# Measure BrainSegNotVent, BrainSegVolNotVent, Brain Segmentation Volume Without Ventricles, 1157593.000000, mm^3
# Measure BrainSegNotVentSurf, BrainSegVolNotVentSurf, Brain Segmentation Volume Without Ventricles from Surf, 1157593.000000, mm^3
# Measure Cortex, CortexVol, Total cortical gray matter volume, 454587.696158, mm^3
# Measure SupraTentorial, SupraTentorialVol, Supratentorial volume, 1023873.000000, mm^3
# Measure SupraTentorialNotVent, SupraTentorialVolNotVent, Supratentorial volume, 1012058.000000, mm^3
# Measure EstimatedTotalIntraCranialVol, eTIV, Estimated Total Intracranial Volume, 1420434.160521, mm^3
# NTableCols 10
# TableCol 1 ColHeader StructName
# TableCol 1 FieldName Structure Name
# TableCol 1 Units NA
# TableCol 2 ColHeader NumVert
# TableCol 2 FieldName Number of Vertices
# TableCol 2 Units unitless
# TableCol 3 ColHeader SurfArea
# TableCol 3 FieldName Surface Area
# TableCol 3 Units mm^2
# TableCol 4 ColHeader GrayVol
# TableCol 4 FieldName Gray Matter Volume
# TableCol 4 Units mm^3
# TableCol 5 ColHeader ThickAvg
# TableCol 5 FieldName Average Thickness
# TableCol 5 Units mm
# TableCol 6 ColHeader ThickStd
# TableCol 6 FieldName Thickness StdDev
# TableCol 6 Units mm
# TableCol 7 ColHeader MeanCurv
# TableCol 7 FieldName Integrated Rectified Mean Curvature
# TableCol 7 Units mm^-1
# TableCol 8 ColHeader GausCurv
# TableCol 8 FieldName Integrated Rectified Gaussian Curvature
# TableCol 8 Units mm^-2
# TableCol 9 ColHeader FoldInd
# TableCol 9 FieldName Folding Index
# TableCol 9 Units unitless
# TableCol 10 ColHeader CurvInd
# TableCol 10 FieldName Intrinsic Curvature Index
# TableCol 10 Units unitless
# ColHeaders StructName NumVert SurfArea GrayVol ThickAvg ThickStd MeanCurv GausCurv FoldInd CurvInd
BA1_exvivo 1091 779 2228 2.233 0.587 0.140 0.032 13 1.5
BA2_exvivo 3956 2761 6357 2.112 0.581 0.126 0.027 39 4.4
BA3a_exvivo 1132 726 973 1.785 0.396 0.129 0.028 10 1.3
BA3b_exvivo 2250 1572 3226 1.811 0.557 0.112 0.025 18 2.3
BA4a_exvivo 1849 1082 3167 2.671 0.626 0.099 0.022 11 1.7
BA4p_exvivo 1313 932 1786 2.033 0.504 0.116 0.026 8 1.5
BA6_exvivo 10247 6816 22620 2.873 0.634 0.111 0.023 82 9.7
BA44_exvivo 2571 1892 5119 2.394 0.596 0.121 0.024 25 2.8
BA45_exvivo 2876 2112 6879 2.597 0.807 0.131 0.033 30 4.0
V1_exvivo 3587 2681 4840 1.791 0.478 0.151 0.039 45 5.8
V2_exvivo 7987 5971 13181 2.067 0.540 0.155 0.039 108 13.2
MT_exvivo 1812 1342 3308 2.268 0.545 0.140 0.032 21 2.5
perirhinal_exvivo 572 418 1747 3.036 0.856 0.115 0.031 5 0.8
entorhinal_exvivo 544 350 1553 2.938 1.298 0.109 0.032 6 0.7
Loading

0 comments on commit 7a3b034

Please sign in to comment.