Skip to content

Commit af4e046

Browse files
committed
basic implementation of aparc.stats reader (returning everything)
1 parent e4825f4 commit af4e046

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

nibabel/freesurfer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"""
33

44
from .io import read_geometry, read_morph_data, write_morph_data, \
5-
read_annot, read_label, write_geometry, write_annot, read_aseg_stats
5+
read_annot, read_label, write_geometry, write_annot, read_aseg_stats, read_aparc_stats
66
from .mghformat import load, save, MGHImage

nibabel/freesurfer/io.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,38 @@ def read_aseg_stats(seg_stats_file,
132132
return out_data
133133

134134

135+
def read_aparc_stats(file_path):
136+
"""Read statistics on cortical features (such as thickness, curvature etc) produced by Freesurfer.
137+
138+
file_path would contain whether it is from the right or left hemisphere.
139+
140+
"""
141+
142+
# ColHeaders StructName NumVert SurfArea GrayVol ThickAvg ThickStd MeanCurv GausCurv FoldInd CurvInd
143+
aparc_roi_dtype = [('StructName', 'S50'), ('NumVert', '<i4'), ('SurfArea', '<i4'), ('GrayVol', '<i4'),
144+
('ThickAvg', '<f4'), ('ThickStd', '<f4'), ('MeanCurv', '<f4'), ('GausCurv', '<f4'),
145+
('FoldInd', '<f4'), ('CurvInd', '<f4')]
146+
roi_stats = np.genfromtxt(file_path, dtype=aparc_roi_dtype, filling_values=np.NaN)
147+
subset = ['SurfArea', 'GrayVol', 'ThickAvg', 'ThickStd', 'MeanCurv', 'GausCurv', 'FoldInd', 'CurvInd']
148+
roi_stats_values = np.full((len(roi_stats), len(subset)), np.NaN)
149+
for idx, stat in enumerate(roi_stats):
150+
roi_stats_values[idx,:] = [ stat[feat] for feat in subset ]
151+
152+
# whole cortex
153+
# Measure Cortex, NumVert, Number of Vertices, 120233, unitless
154+
# Measure Cortex, WhiteSurfArea, White Surface Total Area, 85633.5, mm^2
155+
# Measure Cortex, MeanThickness, Mean Thickness, 2.59632, mm
156+
wb_regex_pattern = r'# Measure Cortex, ([\w/+_\- ]+), ([\w/+_\- ]+), ([\d\.]+), ([\w/+_\-^]+)'
157+
wb_aparc_dtype = np.dtype('U100,U100,f8,U10')
158+
# wb_aparc_dtype = [('f0', '<U100'), ('f1', '<U100'), ('f2', '<f8'), ('f3', '<U10')]
159+
wb_stats = np.fromregex(file_path, wb_regex_pattern, dtype=wb_aparc_dtype)
160+
161+
# concatenating while surf total area and global mean thickness
162+
stats = np.hstack((roi_stats_values.flatten(), (wb_stats[1][2], wb_stats[2][2])))
163+
164+
return stats
165+
166+
135167
def _read_volume_info(fobj):
136168
"""Helper for reading the footer from a surface file."""
137169
volume_info = OrderedDict()

0 commit comments

Comments
 (0)