Exemple #1
0
def bad_cifti():
    import nibabel.cifti2.cifti2 as ncif
    import nibabel.cifti2.cifti2_axes as ncax

    np.random.seed(seed=1)
    data = np.random.uniform(size=(1, 50))

    ser_ax = ncax.ScalarAxis(name=["Data"])
    h = ncif.Cifti2Header.from_axes((ser_ax, ser_ax))
    c = ncif.Cifti2Image(data, h)
    return c
Exemple #2
0
def cifti():
    import nibabel.cifti2.cifti2 as ncif
    import nibabel.cifti2.cifti2_axes as ncax

    np.random.seed(seed=1)
    verts = np.arange(0, 100, 2)
    data = np.random.uniform(size=(1, 50))

    ser_ax = ncax.ScalarAxis(name=["Data"])
    bm_ax = ncax.BrainModelAxis(name="CORTEX_LEFT",
                                vertex=verts,
                                affine=np.eye(4),
                                nvertices={"CORTEX_LEFT": verts.shape[0]})
    h = ncif.Cifti2Header.from_axes((ser_ax, bm_ax))
    c = ncif.Cifti2Image(data, h)
    return c
Exemple #3
0
def check_rewrite(arr, axes, extension='.nii'):
    """
    Checks wheter writing the Cifti2 array to disc and reading it back in gives the same object

    Parameters
    ----------
    arr : array
        N-dimensional array of data
    axes : Sequence[cifti2_axes.Axis]
        sequence of length N with the meaning of the rows/columns along each dimension
    extension : str
        custom extension to use
    """
    (fd, name) = tempfile.mkstemp(extension)
    cifti2.Cifti2Image(arr, header=axes).to_filename(name)
    img = nib.load(name)
    arr2 = img.get_data()
    assert (arr == arr2).all()
    for idx in range(len(img.shape)):
        assert (axes[idx] == img.header.get_axis(idx))
    return img
Exemple #4
0
def save2cifti(file_path,
               data,
               brain_models,
               map_names=None,
               volume=None,
               label_tables=None):
    """
    Save data as a cifti file
    If you just want to simply save pure data without extra information,
    you can just supply the first three parameters.

    NOTE!!!!!!
        The result is a Nifti2Image instead of Cifti2Image, when nibabel-2.2.1 is used.
        Nibabel-2.3.0 can support for Cifti2Image indeed.
        And the header will be regard as Nifti2Header when loading cifti file by nibabel earlier than 2.3.0.

    Parameters:
    ----------
    file_path: str
        the output filename
    data: numpy array
        An array with shape (maps, values), each row is a map.
    brain_models: sequence of Cifti2BrainModel
        Each brain model is a specification of a part of the data.
        We can always get them from another cifti file header.
    map_names: sequence of str
        The sequence's indices correspond to data's row indices and label_tables.
        And its elements are maps' names.
    volume: Cifti2Volume
        The volume contains some information about subcortical voxels,
        such as volume dimensions and transformation matrix.
        If your data doesn't contain any subcortical voxel, set the parameter as None.
    label_tables: sequence of Cifti2LableTable
        Cifti2LableTable is a mapper to map label number to Cifti2Label.
        Cifti2Lable is a specification of the label, including rgba, label name and label number.
        If your data is a label data, it would be useful.
    """
    if file_path.endswith('.dlabel.nii'):
        assert label_tables is not None
        idx_type0 = 'CIFTI_INDEX_TYPE_LABELS'
    elif file_path.endswith('.dscalar.nii'):
        idx_type0 = 'CIFTI_INDEX_TYPE_SCALARS'
    else:
        raise TypeError('Unsupported File Format')

    if map_names is None:
        map_names = [None] * data.shape[0]
    else:
        assert data.shape[0] == len(
            map_names), "Map_names are mismatched with the data"

    if label_tables is None:
        label_tables = [None] * data.shape[0]
    else:
        assert data.shape[0] == len(
            label_tables), "Label_tables are mismatched with the data"

    # CIFTI_INDEX_TYPE_SCALARS always corresponds to Cifti2Image.header.get_index_map(0),
    # and this index_map always contains some scalar information, such as named_maps.
    # We can get label_table and map_name and metadata from named_map.
    mat_idx_map0 = cifti2.Cifti2MatrixIndicesMap([0], idx_type0)
    for mn, lbt in zip(map_names, label_tables):
        named_map = cifti2.Cifti2NamedMap(mn, label_table=lbt)
        mat_idx_map0.append(named_map)

    # CIFTI_INDEX_TYPE_BRAIN_MODELS always corresponds to Cifti2Image.header.get_index_map(1),
    # and this index_map always contains some brain_structure information, such as brain_models and volume.
    mat_idx_map1 = cifti2.Cifti2MatrixIndicesMap(
        [1], 'CIFTI_INDEX_TYPE_BRAIN_MODELS')
    for bm in brain_models:
        mat_idx_map1.append(bm)
    if volume is not None:
        mat_idx_map1.append(volume)

    matrix = cifti2.Cifti2Matrix()
    matrix.append(mat_idx_map0)
    matrix.append(mat_idx_map1)
    header = cifti2.Cifti2Header(matrix)
    img = cifti2.Cifti2Image(data, header)
    cifti2.save(img, file_path)