FastSurferCNN: segstats.py¶
segstats.py is a script that is equivalent to FreeSurfer’s mri_segstats. However, it is faster and (automatically) scales very well to multi-processing scenarios.
Full commandline interface of FastSurferCNN/segstats.py¶
Script to calculate partial volumes and other segmentation statistics of a segmentation file.
usage: python segstats.py (-norm|-pv) <input_norm> -i <input_seg>
-o <output_seg_stats> [optional arguments]
[{measures,mri_segstats} ...]
Named Arguments¶
- --pvfile, -pv
Path to image used to compute the partial volume effects (default: the file passed as normfile). This file is required, either directly or indirectly via normfile.
- -norm, --normfile
Path to biasfield-corrected image (the same image space as segmentation). This file is used to calculate intensity values. Also, if no pvfile is defined, it is used as pvfile. One of normfile or pvfile is required.
- -i, --segfile
Segmentation file to read and use for evaluation (required).
- -o, --segstatsfile
Path to output segstats file.
- --excludeid
List of segmentation ids (integers) to exclude in analysis, e.g. –excludeid 0 1 10 (default: None).
Default:
[]- --ids
List of exclusive segmentation ids (integers) to use (default: all ids in –lut or all ids in image).
- --merged_label
Add a ‘virtual’ label (first value) that is the combination of all following values, e.g. –merged_label 100 3 4 8 will compute the statistics for label 100 by aggregating labels 3, 4 and 8. With –robust, the values are dropped from the union of these labels.
Default:
[]- --robust
Whether to calculate robust segmentation metrics. This parameter expects the fraction of values to keep, e.g. –robust 0.95 will ignore the 2.5% smallest and the 2.5% largest values in the segmentation when calculating the statistics (default: no robust statistics == –robust 1.0). With –legacy_freesurfer, one value less is ignored at the top, like mri_segstats does.
- --measure_only
Only calculate the Measures in the header, no PV table.
Default:
False
Suboptions¶
- subparser
Possible choices: measures
Advanced options (not shown in -h)¶
- --threads
Number of threads to use (defaults to number of hardware threads: 4)
Default:
4- --patch_size
Patch size to use in calculating the partial volumes (default: 32).
Default:
32- --empty
Keep ids for the table that do not exist in the segmentation (default: drop).
Default:
False- --device
Device to run inference on: auto (default), cpu, cuda (NVIDIA, or AMD with ROCm), a specific gpu (e.g. cuda:1), or mps (Apple silicon gpu). auto uses cuda if available, else mps, else cpu.
Default:
'auto'- --sid
The subject id to use, if not passed we try to extract the subject id from the path passed to –t1. For multi-subject processing, use –remove_suffix if sid is not the second to last element of input file passed to –t1.
- --sd
Directory in which evaluation results should be written. Will be created if it does not exist.
- --lut
Path and name of LUT to use.
- --legacy_freesurfer
Reproduce FreeSurfer mri_segstats numbers (default: off). Please note, that exact agreement of numbers cannot be guaranteed, because the condition number of FreeSurfers algorithm (mri_segstats) combined with the fact that mri_segstats uses ‘float’ to measure the partial volume corrected volume. This yields differences of more than 60mm3 or 0.1% in large structures. This uniquely impacts highres images with more voxels (on the boundary) and smaller voxel sizes (volume per voxel).
Default:
False- --mixing_coeff
Save the mixing coefficients (default: off).
Default:
do not save the file- --alternate_labels
Save the alternate labels (default: off).
Default:
do not save the file- --alternate_mixing_coeff
Save mixing coefficients of alternate labels (default: off).
Default:
do not save the file- --seg_means
Save means of segmentation labels (default: off).
Default:
do not save the file- --alternate_means
Save means of alternate labels (default: off).
Default:
do not save the file- --volume_precision
Number of digits after dot in summary stats file (default: 3). Use 1 for maximum FreeSurfer compatibility).
Default:
3- --norm_name
Option to change the name of the in volume (default: norm).
Default:
'norm'- --norm_unit
Option to change the unit of the in volume (default: MR).
Default:
'MR'
Sub-commands¶
measures¶
Options to configure measures
python segstats.py (...) measures [optional arguments]
Named Arguments¶
- --compute
Additional Measures to compute based on imported/computed measures:<br>Cortex, CerebralWhiteMatter, SubCortGray, TotalGray, BrainSegVol-to-eTIV, MaskVol-to-eTIV, SurfaceHoles, EstimatedTotalIntraCranialVol
Default:
[]- --import
Additional Measures to import from the measurefile.<br>Example measures (‘all’ to import all measures in the measurefile):<br>BrainSeg, BrainSegNotVent, SupraTentorial, SupraTentorialNotVent, SubCortGray, lhCortex, rhCortex, Cortex, TotalGray, lhCerebralWhiteMatter, rhCerebralWhiteMatter, CerebralWhiteMatter, Mask, SupraTentorialNotVentVox, BrainSegNotVentSurf, VentricleChoroidVol, BrainSegVol-to-eTIV, MaskVol-to-eTIV, lhSurfaceHoles, rhSurfaceHoles, SurfaceHoles, EstimatedTotalIntraCranialVol<br>Note, ‘all’ will always be overwritten by any explicitly mentioned measures.
Default:
[]- --file
Default file to read measures (–import …) from. If the path is relative, it is interpreted as relative to <subjects_dir>/<subject_id> from –sd and –sid.
Default:
brainvol.stats- --from_seg
Replace the default segfile to compute measures from by -i/–segfile. This will default to ‘mri/aseg.mgz’ for –legacy_freesurfer and to the value of -i/–segfile otherwise.
Dependencies:
Python 3.12
Numpy http://www.numpy.org
Nibabel to read images http://nipy.org/nibabel/
Pandas to read/write stats files etc. https://pandas.pydata.org/
FreeSurfer-compatible interfaces: mri_segstats.py and mri_brainvol_stats.py¶
For scripts written for FreeSurfer, FastSurferCNN/mri_segstats.py and FastSurferCNN/mri_brainvol_stats.py
accept the options of FreeSurfer’s mri_segstats and mri_brainvol_stats and run segstats.py with the
equivalent options. Options that have no equivalent in segstats.py are not listed; --print shows the
equivalent segstats.py call.
Translates mri_segstats options for segstats.py. Options not listed here have no equivalent representation in segstats.py. <br> IMPORTANT NOTES mri_segstats uses a legacy version for the computation of measures (from FreeSurfer 6). But mri_segstats.py implements the behavior if first mri_brainvol_stats and then mri_segstats is run (which uses the stats/brainvol.stats generated by mri_brainvol_stats). This reflects the output of stats files as created by FreeSurfer’s recon-all.
usage: python mri_segstats.py --seg segvol [optional arguments]
Named Arguments¶
Print the equivalent native segstats.py options and exit.
Default:
[(1, <function make_arguments.<locals>.add_etiv_measures at 0x7f6181cf5d00>), (10, <function make_arguments.<locals>._update_what_to_import at 0x7f6181cf5da0>), (20, <function make_arguments.<locals>._no_global_stats at 0x7f6181caca40>)]- --version
Print the version of the mri_segstats.py script
- --seg
Specify the segmentation file.
- --o, --sum
Specify the output summary statistics file.
- --pv
Use pvvol to compensate for partial volume effects. Without –pv, –i is used (mri_segstats reports the number of voxels as the volume instead).
- --i, --in
Input volume from which to compute the intensity statistics (Mean, StdDev, Min, Max and Range).
- --robust
Compute stats after excluding percent (0 <= percent < 50) from high and low values (the volume reported is still the full volume). Like mri_segstats, this excludes one voxel less from the high values than from the low values, unless –no_legacy is passed.
- --sqr
Compute the square of the partial volume image (–pv, or –i without –pv). mri_segstats applies this to –i instead.
- --sqrt
Compute the square root of the partial volume image (–pv, or –i without –pv). mri_segstats applies this to –i instead.
- --mul
Multiply the partial volume image (–pv, or –i without –pv) by val, before –abs, –sqr and –sqrt; multiple –mul and –div combine. mri_segstats applies this to –i instead.
- --div
Divide the partial volume image (–pv, or –i without –pv) by val, before –abs, –sqr and –sqrt; multiple –mul and –div combine. mri_segstats applies this to –i instead.
- --abs
Compute the absolute value of the partial volume image (–pv, or –i without –pv). mri_segstats applies this to –i instead.
- --ctab
load the Color Lookup Table.
- --ctab-default
load default Color Lookup Table (from FREESURFER_HOME or FASTSURFER_HOME).
- --id
Specify the segmentation ids to report on. Multiple ids can be given after a single –id or with multiple –id.
Default:
[]- --excludeid
Exclude the given segmentation ids from the report. Multiple ids can be given after a single –excludeid or with multiple –excludeid.
- --no-cached
Do not try to load stats/brainvol.stats.
Default:
False- --excl-ctxgmwm
Exclude cortical gray and white matter (ids 2, 3, 41 and 42) from the report.
- --surf-wm-vol
Compute cortical white matter based on the surface:<br>- rhCerebralWhiteMatter<br>- lhCerebralWhiteMatter<br>- CerebralWhiteMatter
Default:
[]- --surf-ctx-vol
compute cortical gray matter based on the surface:<br>- rhCortex<br>- lhCortex<br>- Cortex
Default:
[]- --no-global-stats, --no_global_stats
Turn off the computation of global stats (the measures in the header, e.g. BrainSeg, eTIV or SupraTentorial), wherever this option is on the command line.
Default:
False- --empty
Report all segmentation labels in ctab, even if they are not in seg.
Default:
False- --brain-vol-from-seg
Compute measures BrainSeg measures:<br>- BrainSeg<br>- BrainSegNotVent
Default:
[]- --brainmask
Report the volume of the non-zero voxels in brainmask.
Default:
[]- --supratent
Compute supratentorial measures:<br>- SupraTentorial<br>- SupraTentorialNotVent
Default:
[]- --subcortgray
Compute measure SubCortGray:<br>- SubCortGray
Default:
[]- --totalgray
Compute measure TotalGray:<br>- TotalGray
Default:
[]- --etiv
Compute eTIV:<br>- EstimatedTotalIntraCranialVol<br>- BrainSegVol-to-eTIV (if also –brain-vol-from-seg)<br>- MaskVol-to-eTIV (if also –brainmask)
Default:
[]- --euler
Compute surface holes measures:<br>- rhSurfaceHoles<br>- lhSurfaceHoles<br>- SurfaceHoles
Default:
[]- --sd
set SUBJECTS_DIR, defaults to environment SUBJECTS_DIR, required to find several files used by measures, e.g. surfaces.
- --subject
set subject_id, required to find several files used by measures, e.g. surfaces.
- --seed
The seed has no effect
- --in-intensity-name
name of the intensity image
Default:
''- --in-intensity-units
unit of the intensity image
Default:
''- --no_legacy
Use the FastSurfer algorithms instead of reproducing mri_segstats (partial volume correction and –robust).
Default:
True
<br>Dependencies:<br><br> Python 3.12<br><br> Numpy<br> http://www.numpy.org<br><br> Nibabel to read images<br> http://nipy.org/nibabel/<br><br> Pandas to read/write stats files etc.<br> https://pandas.pydata.org/<br><br><br>
Translates mri_brainvol_stats options for segstats.py. Options not listed here have no equivalent representation in segstats.py.
usage: python mri_brainvol_stats.py -s <subject>
Named Arguments¶
Print the equivalent native segstats.py options and exit.
Default:
[]- --sd
set SUBJECTS_DIR, defaults to environment SUBJECTS_DIR, required to find several files used by measures, e.g. surfaces.
- -s, --subject, --sid
set subject_id, required to find several files used by measures, e.g. surfaces.
- -o, --segstatsfile
Where to save the brainvol.stats, if relative path, this will be relative to the subject directory.
Default:
stats/brainvol.stats
FastSurfer options (no equivalence with FreeSurfer’s mri_brainvol_stats)¶
- --no_legacy
use FastSurfer algorithms instead of FastSurfer.
Default:
True- --pvfile, -pv
Path to image used to compute the partial volume effects. This file is only used in the FastSurfer algorithms (–no_legacy).
<br>Dependencies:<br><br> Python 3.12<br><br> Numpy<br> http://www.numpy.org<br><br> Nibabel to read images<br> http://nipy.org/nibabel/<br><br> Pandas to read/write stats files etc.<br> https://pandas.pydata.org/<br><br>