<p>How do you programmatically in Python open and read segmentation data in an aparc+aseg.nii.gz file programmatically?</p>
<p>Is this correct?</p>
<pre><code>import numpy as np
from pathlib import Path
import torchio as tio

dataset_dir_name = '/home/shared/projects/BrainSegNet3D/data'
dataset_dir = Path(dataset_dir_name)
labels_dir = dataset_dir / 'label'
label_paths = sorted(labels_dir.glob('*.nii.gz'))
label_path = label_paths[0]
abcd_free_surfer_labels = tio.LabelMap(label_path)
free_surfer_labels = abcd_free_surfer_labels.data[0].numpy()
np.unique(free_surfer_labels.astype('int'))

array([   0,    2,    4,    5,    7,    8,   10,   11,   12,   13,   14,
         15,   16,   17,   18,   24,   26,   28,   30,   31,   41,   43,
         44,   46,   47,   49,   50,   51,   52,   53,   54,   58,   60,
         62,   63,   72,   77,   80,   85,  251,  252,  253,  254,  255,
       1000, 1001, 1002, 1003, 1005, 1006, 1007, 1008, 1009, 1010, 1011,
       1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022,
       1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033,
       1034, 1035, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009,
       2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020,
       2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031,
       2032, 2033, 2034, 2035])
</code></pre>
<p>Why are some of the numbers so large?  Does FreeSurfer really distinguish between over 2000 regions of the brain?</p>

CC BY-SA 4.0
2021-03-29T21:16:28.173Z
I am not sure you can open a nii.gz file with nibabel.freesurfer.io.read_annot method. From the documentation, it is for opening the *.annot files. To read nii.gz files, you may simply be able to use, nibabel.load method.
ssm
CC BY-SA 4.0
2021-03-29T21:21:50.440Z
And then what?  It's the gory details that I need.
Paul Reiners
CC BY-SA 4.0
2021-03-29T21:49:06.167Z
https://nipy.org/nibabel/gettingstarted.html might help accessing data in the file.
ssm
<neuroimaging>
Opening an aparc+aseg.nii.gz file programmatically in Python
Paul Reiners
# Opening an aparc+aseg.nii.gz file programmatically in Python

(Asked by: username_0 on 2021-03-29)

How do you programmatically in Python open and read segmentation data in an aparc+aseg.nii.gz file programmatically?
Is this correct?
import numpy as np
from pathlib import Path
import torchio as tio

dataset_dir_name = '/home/shared/projects/BrainSegNet3D/data'
dataset_dir = Path(dataset_dir_name)
labels_dir = dataset_dir / 'label'
label_paths = sorted(labels_dir.glob('*.nii.gz'))
label_path = label_paths[0]
abcd_free_surfer_labels = tio.LabelMap(label_path)
free_surfer_labels = abcd_free_surfer_labels.data[0].numpy()
np.unique(free_surfer_labels.astype('int'))

array([   0,    2,    4,    5,    7,    8,   10,   11,   12,   13,   14,
         15,   16,   17,   18,   24,   26,   28,   30,   31,   41,   43,
         44,   46,   47,   49,   50,   51,   52,   53,   54,   58,   60,
         62,   63,   72,   77,   80,   85,  251,  252,  253,  254,  255,
       1000, 1001, 1002, 1003, 1005, 1006, 1007, 1008, 1009, 1010, 1011,
       1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022,
       1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033,
       1034, 1035, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009,
       2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020,
       2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031,
       2032, 2033, 2034, 2035])

Why are some of the numbers so large?  Does FreeSurfer really distinguish between over 2000 regions of the brain?

**Tags:** neuroimaging

> **Comments:**
> **username_1**: I am not sure you can open a nii.gz file with nibabel.freesurfer.io.read_annot method. From the documentation, it is for opening the *.annot files. To read nii.gz files, you may simply be able to use, nibabel.load method.  
> **username_0**: And then what?  It's the gory details that I need.  
> **username_1**: https://nipy.org/nibabel/gettingstarted.html might help accessing data in the file.  

---

## 0 answers

