Exemplo n.º 1
0
def yeo_to_91k(dlabel, medial_wall, reference, out):
    """Convert Yeo-style dlabels (Yeo and Schaefer parcellations) to 91k 
    grayordinate space
    
    The Yeo lab generates dlabel's inclusive of medial wall vertices and only 
    for the cortical surfaces. This is different from how typical dlabels are 
    formatted, which exclude medial wall vertices and include voxels from all 
    subcortical and cerebellar structures (i.e. the full 91k grayordinate 
    space). This function corrects Yeo dlabels to proper 91k grayordinates.  

    Parameters
    ----------
    dlabel : str
        A Yeo-style .dlabel.nii atlas
    medial_wall : str
        HCP medial wall mask (.dlabel.nii)
    reference : str
        A reference .dlabel.nii file with 91k grayordinates and all brain 
        models included
    out : str
        Output 91k grayordinate .dlabel.nii file
    """
    dlabel = nib.load(dlabel)
    medial_wall = nib.load(medial_wall)
    ref = nib.load(reference)

    # remove medial wall vertices
    array = dlabel.get_fdata()
    corrected_array = array[np.logical_not(medial_wall.get_fdata())]

    # expand to 91k
    grayordinates = np.zeros(ref.shape)
    grayordinates[0, :corrected_array.shape[0]] = corrected_array

    # make header
    labels = dlabel.header.get_axis(index=0).label[0]
    label_table = ci.Cifti2LabelTable()
    for key, (tag, rgba) in labels.items():
        label_table[key] = ci.Cifti2Label(key, tag, *rgba)

    maps = [ci.Cifti2NamedMap('labels', ci.Cifti2MetaData({}), label_table)]
    label_map = ci.Cifti2MatrixIndicesMap(
        applies_to_matrix_dimension=(0, ),
        indices_map_to_data_type='CIFTI_INDEX_TYPE_LABELS',
        maps=maps)
    model_map = ci.Cifti2MatrixIndicesMap(
        applies_to_matrix_dimension=(1, ),
        indices_map_to_data_type='CIFTI_INDEX_TYPE_BRAIN_MODELS',
        maps=list(ref.header.get_index_map(1).brain_models))
    model_map.volume = ref.header.get_index_map(1).volume

    matrix = ci.Cifti2Matrix()
    matrix.append(label_map)
    matrix.append(model_map)
    hdr = ci.Cifti2Header(matrix)

    out_dtseries = ci.Cifti2Image(grayordinates, hdr)
    out_dtseries.to_filename(out)
    return out
Exemplo n.º 2
0
 def make_imaker(self, arr, header=None, ni_header=None):
     for idx, sz in enumerate(arr.shape):
         maps = [ci.Cifti2NamedMap(str(value)) for value in range(sz)]
         mim = ci.Cifti2MatrixIndicesMap((idx, ),
                                         'CIFTI_INDEX_TYPE_SCALARS',
                                         maps=maps)
         header.matrix.append(mim)
     return lambda: self.image_maker(arr.copy(), header, ni_header)
Exemplo n.º 3
0
def create_scalar_map(applies_to_matrix_dimension):
    maps = [
        ci.Cifti2NamedMap(name, ci.Cifti2MetaData(meta))
        for name, meta in scalars
    ]
    return ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension,
                                     'CIFTI_INDEX_TYPE_SCALARS',
                                     maps=maps)
Exemplo n.º 4
0
def create_label_map(applies_to_matrix_dimension):
    maps = []
    for name, meta, label in labels:
        label_table = ci.Cifti2LabelTable()
        for key, (tag, rgba) in label.items():
            label_table[key] = ci.Cifti2Label(key, tag, *rgba)
        maps.append(
            ci.Cifti2NamedMap(name, ci.Cifti2MetaData(meta), label_table))
    return ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension,
                                     'CIFTI_INDEX_TYPE_LABELS',
                                     maps=maps)
Exemplo n.º 5
0
    def to_mapping(self, dim):
        """
        Converts the hcp_labels to a MatrixIndicesMap for storage in CIFTI format

        Parameters
        ----------
        dim : int
            which dimension of the CIFTI vector/matrix is described by this dataset (zero-based)

        Returns
        -------
        cifti2.Cifti2MatrixIndicesMap
        """
        mim = cifti2.Cifti2MatrixIndicesMap([dim], 'CIFTI_INDEX_TYPE_SCALARS')
        for elem in self.arr:
            meta = None if len(elem['meta']) == 0 else elem['meta']
            named_map = cifti2.Cifti2NamedMap(elem['name'], cifti2.Cifti2MetaData(meta))
            mim.append(named_map)
        return mim
Exemplo n.º 6
0
    def to_mapping(self, dim):
        """
        Converts the hcp_labels to a MatrixIndicesMap for storage in CIFTI format

        Parameters
        ----------
        dim : int
            which dimension of the CIFTI vector/matrix is described by this dataset (zero-based)

        Returns
        -------
        cifti2.Cifti2MatrixIndicesMap
        """
        mim = cifti2.Cifti2MatrixIndicesMap([dim], 'CIFTI_INDEX_TYPE_LABELS')
        for elem in self.arr:
            label_table = cifti2.Cifti2LabelTable()
            for key, value in elem['label'].items():
                label_table[key] = (value[0],) + tuple(value[1])
            meta = None if len(elem['meta']) == 0 else elem['meta']
            named_map = cifti2.Cifti2NamedMap(elem['name'], cifti2.Cifti2MetaData(meta),
                                              label_table)
            mim.append(named_map)
        return mim
Exemplo n.º 7
0
def create_scalar_map(applies_to_matrix_dimension, info):
    """Creates a scalar map form a list of NamedMapInfo"""
    maps = [ci.Cifti2NamedMap(i.name, ci.Cifti2MetaData(i.meta)) for i in info]
    return ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension,
                                     Map.SCALARS,
                                     maps=maps)