예제 #1
0
def test_pconnscalar():
    parcel_map = create_parcel_map((0, 1))
    scalar_map = create_scalar_map((2, ))

    matrix = ci.Cifti2Matrix()
    matrix.append(parcel_map)
    matrix.append(scalar_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(3, 3, 13)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_'
                                'PARCELLATED_SCALAR')

    with InTemporaryDirectory():
        ci.save(img, 'test.pconnscalar.nii')
        img2 = ci.load('test.pconnscalar.nii')
        assert_equal(img.nifti_header.get_intent()[0], 'ConnPPSc')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_true((img2.get_data() == data).all())
        assert_equal(img2.header.matrix.get_index_map(0),
                     img2.header.matrix.get_index_map(1))

        check_parcel_map(img2.header.matrix.get_index_map(0))
        check_scalar_map(img2.header.matrix.get_index_map(2))
        del img2
예제 #2
0
def test_wrong_shape():
    scalar_map = create_scalar_map((0, ))
    brain_model_map = create_geometry_map((1, ))

    matrix = ci.Cifti2Matrix()
    matrix.append(scalar_map)
    matrix.append(brain_model_map)
    hdr = ci.Cifti2Header(matrix)

    # correct shape is (2, 10)
    for data in (
            np.random.randn(1, 11),
            np.random.randn(2, 10, 1),
            np.random.randn(1, 2, 10),
            np.random.randn(3, 10),
            np.random.randn(2, 9),
    ):
        with clear_and_catch_warnings():
            with error_warnings():
                with pytest.raises(UserWarning):
                    ci.Cifti2Image(data, hdr)
        with suppress_warnings():
            img = ci.Cifti2Image(data, hdr)

        with pytest.raises(ValueError):
            img.to_file_map()
예제 #3
0
def to_header(axes):
    """
    Converts the axes describing the rows/columns of a CIFTI vector/matrix to a Cifti2Header

    Parameters
    ----------
    axes : iterable[Axis]
        one or more axes describing each dimension in turn

    Returns
    -------
    cifti2.Cifti2Header
    """
    axes = list(axes)
    mims_all = []
    matrix = cifti2.Cifti2Matrix()
    for dim, ax in enumerate(axes):
        if ax in axes[:dim]:
            dim_prev = axes.index(ax)
            mims_all[dim_prev].applies_to_matrix_dimension.append(dim)
            mims_all.append(mims_all[dim_prev])
        else:
            mim = ax.to_mapping(dim)
            mims_all.append(mim)
            matrix.append(mim)
    return cifti2.Cifti2Header(matrix)
예제 #4
0
def test_matrix():
    m = ci.Cifti2Matrix()
    assert_raises(TypeError, m, setattr, 'metadata', ci.Cifti2Parcel())
    assert_raises(TypeError, m.__setitem__, 0, ci.Cifti2Parcel())
    assert_raises(TypeError, m.insert, 0, ci.Cifti2Parcel())

    mim_none = ci.Cifti2MatrixIndicesMap(None, 'CIFTI_INDEX_TYPE_LABELS')
    mim_0 = ci.Cifti2MatrixIndicesMap(0, 'CIFTI_INDEX_TYPE_LABELS')
    mim_1 = ci.Cifti2MatrixIndicesMap(1, 'CIFTI_INDEX_TYPE_LABELS')
    mim_01 = ci.Cifti2MatrixIndicesMap([0, 1], 'CIFTI_INDEX_TYPE_LABELS')

    assert_raises(ci.Cifti2HeaderError, m.insert, 0, mim_none)
    assert_equal(m.mapped_indices, [])

    h = ci.Cifti2Header(matrix=m)
    assert_equal(m.mapped_indices, [])
    m.insert(0, mim_0)
    assert_equal(h.mapped_indices, [0])
    assert_equal(h.number_of_mapped_indices, 1)
    assert_raises(ci.Cifti2HeaderError, m.insert, 0, mim_0)
    assert_raises(ci.Cifti2HeaderError, m.insert, 0, mim_01)
    m[0] = mim_1
    assert_equal(list(m.mapped_indices), [1])
    m.insert(0, mim_0)
    assert_equal(list(sorted(m.mapped_indices)), [0, 1])
    assert_equal(h.number_of_mapped_indices, 2)
    assert_equal(h.get_index_map(0), mim_0)
    assert_equal(h.get_index_map(1), mim_1)
    assert_raises(ci.Cifti2HeaderError, h.get_index_map, 2)
예제 #5
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
예제 #6
0
def dlabel_to_dtseries(dlabel, out, n=10):
    """Create a mock .dtseries.nii from an .dlabel file

    All timepoints (rows) in .dtseries.nii are duplicates of the .dlabel array.
    This enables a way to verify that expected data is correctly extracted 
    (e.g., label 1 should extract a timeseries of all 1's, etc). 

    Parameters
    ----------
    dlabel : str
        File name of a .dlabel.nii file
    out : str
        File name of output .dtseries.nii
    n : int, optional
        Number of timepoints to generate, by default 100
    """

    dlabel = nib.load(dlabel)

    # imitate data with TR=2
    label_array = dlabel.get_fdata().ravel()
    tseries = np.tile(label_array, (n, 1))
    data_map = ci.Cifti2MatrixIndicesMap(
        applies_to_matrix_dimension=(0, ),
        indices_map_to_data_type='CIFTI_INDEX_TYPE_SERIES',
        number_of_series_points=tseries.shape[0],
        series_start=0,
        series_step=2,
        series_exponent=0,
        series_unit='SECOND')
    # take brain models from dlabel
    model_map = ci.Cifti2MatrixIndicesMap(
        applies_to_matrix_dimension=(1, ),
        indices_map_to_data_type='CIFTI_INDEX_TYPE_BRAIN_MODELS',
        maps=list(dlabel.header.get_index_map(1).brain_models))
    volume = dlabel.header.get_index_map(1).volume
    if volume is not None:
        model_map.volume = dlabel.header.get_index_map(1).volume

    # make header
    matrix = ci.Cifti2Matrix()
    matrix.append(data_map)
    matrix.append(model_map)
    hdr = ci.Cifti2Header(matrix)

    out_dtseries = ci.Cifti2Image(tseries, hdr)
    out_dtseries.to_filename(out)
    return out
예제 #7
0
def test_pconn():
    mapping = create_parcel_map((0, 1))
    matrix = ci.Cifti2Matrix()
    matrix.append(mapping)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(3, 3)
    img = ci.Cifti2Image(data, hdr)

    with InTemporaryDirectory():
        ci.save(img, 'test.pconn.nii')
        img2 = ci.load('test.pconn.nii')
        assert_true((img2.get_data() == data).all())
        assert_equal(img2.header.matrix.get_index_map(0),
                     img2.header.matrix.get_index_map(1))
        check_parcel_map(img2.header.matrix.get_index_map(0))
        del img2
예제 #8
0
def test_dscalar():
    scalar_map = create_scalar_map((0, ))
    geometry_map = create_geometry_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(scalar_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 9)
    img = ci.Cifti2Image(data, hdr)

    with InTemporaryDirectory():
        ci.save(img, 'test.dscalar.nii')
        img2 = ci.load('test.dscalar.nii')
        assert_true((img2.get_data() == data).all())
        check_scalar_map(img2.header.matrix.get_index_map(0))
        check_geometry_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #9
0
def test_plabel():
    label_map = create_label_map((0, ))
    parcel_map = create_parcel_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(label_map)
    matrix.append(parcel_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 3)
    img = ci.Cifti2Image(data, hdr)

    with InTemporaryDirectory():
        ci.save(img, 'test.plabel.nii')
        img2 = ci.load('test.plabel.nii')
        assert_true((img2.get_data() == data).all())
        check_label_map(img2.header.matrix.get_index_map(0))
        check_parcel_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #10
0
def test_dconn():
    mapping = create_geometry_map((0, 1))
    matrix = ci.Cifti2Matrix()
    matrix.append(mapping)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(9, 9)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE')

    with InTemporaryDirectory():
        ci.save(img, 'test.dconn.nii')
        img2 = nib.load('test.dconn.nii')
        assert_equal(img2.nifti_header.get_intent()[0], 'ConnDense')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_true((img2.get_data() == data).all())
        assert_equal(img2.header.matrix.get_index_map(0),
                     img2.header.matrix.get_index_map(1))
        check_geometry_map(img2.header.matrix.get_index_map(0))
        del img2
예제 #11
0
def test_pconn():
    mapping = create_parcel_map((0, 1))
    matrix = ci.Cifti2Matrix()
    matrix.append(mapping)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(4, 4)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED')

    with InTemporaryDirectory():
        ci.save(img, 'test.pconn.nii')
        img2 = ci.load('test.pconn.nii')
        assert img.nifti_header.get_intent()[0] == 'ConnParcels'
        assert isinstance(img2, ci.Cifti2Image)
        assert_array_equal(img2.get_fdata(), data)
        assert img2.header.matrix.get_index_map(
            0) == img2.header.matrix.get_index_map(1)
        check_parcel_map(img2.header.matrix.get_index_map(0))
        del img2
예제 #12
0
def test_plabel():
    label_map = create_label_map((0, ))
    parcel_map = create_parcel_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(label_map)
    matrix.append(parcel_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 4)
    img = ci.Cifti2Image(data, hdr)

    with InTemporaryDirectory():
        ci.save(img, 'test.plabel.nii')
        img2 = ci.load('test.plabel.nii')
        assert img.nifti_header.get_intent()[0] == 'ConnUnknown'
        assert isinstance(img2, ci.Cifti2Image)
        assert_array_equal(img2.get_fdata(), data)
        check_label_map(img2.header.matrix.get_index_map(0))
        check_parcel_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #13
0
def test_matrix():
    m = ci.Cifti2Matrix()

    with pytest.raises(ValueError):
        m.metadata = ci.Cifti2Parcel()

    with pytest.raises(TypeError):
        m[0] = ci.Cifti2Parcel()

    with pytest.raises(TypeError):
        m.insert(0, ci.Cifti2Parcel())

    mim_none = ci.Cifti2MatrixIndicesMap(None, 'CIFTI_INDEX_TYPE_LABELS')
    mim_0 = ci.Cifti2MatrixIndicesMap(0, 'CIFTI_INDEX_TYPE_LABELS')
    mim_1 = ci.Cifti2MatrixIndicesMap(1, 'CIFTI_INDEX_TYPE_LABELS')
    mim_01 = ci.Cifti2MatrixIndicesMap([0, 1], 'CIFTI_INDEX_TYPE_LABELS')

    with pytest.raises(ci.Cifti2HeaderError):
        m.insert(0, mim_none)

    assert m.mapped_indices == []

    h = ci.Cifti2Header(matrix=m)
    assert m.mapped_indices == []
    m.insert(0, mim_0)
    assert h.mapped_indices == [0]
    assert h.number_of_mapped_indices == 1
    with pytest.raises(ci.Cifti2HeaderError):
        m.insert(0, mim_0)

    with pytest.raises(ci.Cifti2HeaderError):
        m.insert(0, mim_01)

    m[0] = mim_1
    assert list(m.mapped_indices) == [1]
    m.insert(0, mim_0)
    assert list(sorted(m.mapped_indices)) == [0, 1]
    assert h.number_of_mapped_indices == 2
    assert h.get_index_map(0) == mim_0
    assert h.get_index_map(1) == mim_1
    with pytest.raises(ci.Cifti2HeaderError):
        h.get_index_map(2)
예제 #14
0
def test_dlabel():
    label_map = create_label_map((0, ))
    geometry_map = create_geometry_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(label_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 10)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_LABELS')

    with InTemporaryDirectory():
        ci.save(img, 'test.dlabel.nii')
        img2 = nib.load('test.dlabel.nii')
        assert img2.nifti_header.get_intent()[0] == 'ConnDenseLabel'
        assert isinstance(img2, ci.Cifti2Image)
        assert_array_equal(img2.get_fdata(), data)
        check_label_map(img2.header.matrix.get_index_map(0))
        check_geometry_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #15
0
def test_dpconn():
    parcel_map = create_parcel_map((0, ))
    geometry_map = create_geometry_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(parcel_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 3)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_PARCELLATED')

    with InTemporaryDirectory():
        ci.save(img, 'test.dpconn.nii')
        img2 = ci.load('test.dpconn.nii')
        assert_equal(img2.nifti_header.get_intent()[0], 'ConnDenseParcel')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_true((img2.get_data() == data).all())
        check_parcel_map(img2.header.matrix.get_index_map(0))
        check_geometry_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #16
0
def test_ptseries():
    series_map = create_series_map((0, ))
    parcel_map = create_parcel_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(series_map)
    matrix.append(parcel_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(13, 3)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_SERIES')

    with InTemporaryDirectory():
        ci.save(img, 'test.ptseries.nii')
        img2 = nib.load('test.ptseries.nii')
        assert_equal(img2.nifti_header.get_intent()[0], 'ConnParcelSries')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_true((img2.get_data() == data).all())
        check_series_map(img2.header.matrix.get_index_map(0))
        check_parcel_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #17
0
def test_dscalar():
    scalar_map = create_scalar_map((0, ))
    geometry_map = create_geometry_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(scalar_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 9)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SCALARS')

    with InTemporaryDirectory():
        ci.save(img, 'test.dscalar.nii')
        img2 = nib.load('test.dscalar.nii')
        assert_equal(img2.nifti_header.get_intent()[0], 'ConnDenseScalar')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_true((img2.get_data() == data).all())
        check_scalar_map(img2.header.matrix.get_index_map(0))
        check_geometry_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #18
0
def test_dtseries():
    series_map = create_series_map((0, ))
    geometry_map = create_geometry_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(series_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(13, 10)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES')

    with InTemporaryDirectory():
        ci.save(img, 'test.dtseries.nii')
        img2 = nib.load('test.dtseries.nii')
        assert_equal(img2.nifti_header.get_intent()[0], 'ConnDenseSeries')
        assert_true(isinstance(img2, ci.Cifti2Image))
        assert_array_equal(img2.get_fdata(), data)
        check_series_map(img2.header.matrix.get_index_map(0))
        check_geometry_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #19
0
def test_pdconn():
    geometry_map = create_geometry_map((0, ))
    parcel_map = create_parcel_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(geometry_map)
    matrix.append(parcel_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(10, 4)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_DENSE')

    with InTemporaryDirectory():
        ci.save(img, 'test.pdconn.nii')
        img2 = ci.load('test.pdconn.nii')
        assert img2.nifti_header.get_intent()[0] == 'ConnParcelDense'
        assert isinstance(img2, ci.Cifti2Image)
        assert_array_equal(img2.get_fdata(), data)
        check_geometry_map(img2.header.matrix.get_index_map(0))
        check_parcel_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #20
0
def test_pscalar():
    scalar_map = create_scalar_map((0, ))
    parcel_map = create_parcel_map((1, ))
    matrix = ci.Cifti2Matrix()
    matrix.append(scalar_map)
    matrix.append(parcel_map)
    hdr = ci.Cifti2Header(matrix)
    data = np.random.randn(2, 4)
    img = ci.Cifti2Image(data, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_SCALAR')

    with InTemporaryDirectory():
        ci.save(img, 'test.pscalar.nii')
        img2 = nib.load('test.pscalar.nii')
        assert img2.nifti_header.get_intent()[0] == 'ConnParcelScalr'
        assert isinstance(img2, ci.Cifti2Image)
        assert_array_equal(img2.get_fdata(), data)
        check_scalar_map(img2.header.matrix.get_index_map(0))
        check_parcel_map(img2.header.matrix.get_index_map(1))
        del img2
예제 #21
0
def create_img(maps, data):
    matrix = ci.Cifti2Matrix()
    matrix.extend(maps)
    hdr = ci.Cifti2Header(matrix)
    img = ci.Cifti2Image(data, hdr)
    return img
예제 #22
0
    def _create_cifti_image(bold_file, label_file, annotation_files, gii_files,
                            volume_target, surface_target, tr):
        """
        Generate CIFTI image in target space

        Parameters
            bold_file : 4D BOLD timeseries
            label_file : label atlas
            annotation_files : FreeSurfer annotations
            gii_files : 4D BOLD surface timeseries in GIFTI format
            volume_target : label atlas space
            surface_target : gii_files space
            tr : repetition timeseries

        Returns
            out_file : BOLD data as CIFTI dtseries
        """

        label_img = nb.load(label_file)
        bold_img = resample_to_img(bold_file, label_img)

        bold_data = bold_img.get_data()
        timepoints = bold_img.shape[3]
        label_data = label_img.get_data()

        # set up CIFTI information
        series_map = ci.Cifti2MatrixIndicesMap(
            (0, ),
            'CIFTI_INDEX_TYPE_SERIES',
            number_of_series_points=timepoints,
            series_exponent=0,
            series_start=0.0,
            series_step=tr,
            series_unit='SECOND')
        # Create CIFTI brain models
        idx_offset = 0
        brainmodels = []
        bm_ts = np.empty((timepoints, 0))

        for structure, labels in CIFTI_STRUCT_WITH_LABELS.items():
            if labels is None:  # surface model
                model_type = "CIFTI_MODEL_TYPE_SURFACE"
                # use the corresponding annotation
                hemi = structure.split('_')[-1]
                annot = nb.freesurfer.read_annot(
                    annotation_files[hemi == "RIGHT"])
                # currently only supports L/R cortex
                gii = nb.load(gii_files[hemi == "RIGHT"])
                # calculate total number of vertices
                surf_verts = len(annot[0])
                # remove medial wall for CIFTI format
                vert_idx = np.nonzero(
                    annot[0] != annot[2].index(b'unknown'))[0]
                # extract values across volumes
                ts = np.array([tsarr.data[vert_idx] for tsarr in gii.darrays])

                vert_idx = ci.Cifti2VertexIndices(vert_idx)
                bm = ci.Cifti2BrainModel(index_offset=idx_offset,
                                         index_count=len(vert_idx),
                                         model_type=model_type,
                                         brain_structure=structure,
                                         vertex_indices=vert_idx,
                                         n_surface_vertices=surf_verts)
                bm_ts = np.column_stack((bm_ts, ts))
                idx_offset += len(vert_idx)
                brainmodels.append(bm)
            else:
                model_type = "CIFTI_MODEL_TYPE_VOXELS"
                vox = []
                ts = None
                for label in labels:
                    ijk = np.nonzero(label_data == label)
                    ts = (bold_data[ijk] if ts is None else np.concatenate(
                        (ts, bold_data[ijk])))
                    vox += [[ijk[0][ix], ijk[1][ix], ijk[2][ix]]
                            for ix, row in enumerate(ts)]

                bm_ts = np.column_stack((bm_ts, ts.T))

                vox = ci.Cifti2VoxelIndicesIJK(vox)
                bm = ci.Cifti2BrainModel(index_offset=idx_offset,
                                         index_count=len(vox),
                                         model_type=model_type,
                                         brain_structure=structure,
                                         voxel_indices_ijk=vox)
                idx_offset += len(vox)
                brainmodels.append(bm)

        volume = ci.Cifti2Volume(
            bold_img.shape[:3],
            ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(
                -3, bold_img.affine))
        brainmodels.append(volume)

        # create CIFTI geometry based on brainmodels
        geometry_map = ci.Cifti2MatrixIndicesMap(
            (1, ), 'CIFTI_INDEX_TYPE_BRAIN_MODELS', maps=brainmodels)
        # provide some metadata to CIFTI matrix
        meta = {
            "target_surface": surface_target,
            "target_volume": volume_target,
        }
        # generate and save CIFTI image
        matrix = ci.Cifti2Matrix()
        matrix.append(series_map)
        matrix.append(geometry_map)
        matrix.metadata = ci.Cifti2MetaData(meta)
        hdr = ci.Cifti2Header(matrix)
        img = ci.Cifti2Image(bm_ts, hdr)
        img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES')

        _, out_base, _ = split_filename(bold_file)
        out_file = "{}.dtseries.nii".format(out_base)
        ci.save(img, out_file)
        return os.path.join(os.getcwd(), out_file)
예제 #23
0
def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files,
                        tr, targets):
    """
    Generate CIFTI image in target space.

    Parameters
    ----------
    bold_file : str
        BOLD volumetric timeseries
    label_file : str
        Subcortical label file
    bold_surfs : list
        BOLD surface timeseries [L,R]
    annotation_files : list
        Surface label files used to remove medial wall
    tr : float
        BOLD repetition time
    targets : tuple or list
        Surface and volumetric output spaces

    Returns
    -------
    out :
        BOLD data saved as CIFTI dtseries
    """
    bold_img = nb.load(bold_file)
    label_img = nb.load(label_file)
    if label_img.shape != bold_img.shape[:3]:
        warnings.warn("Resampling bold volume to match label dimensions")
        bold_img = resample_to_img(bold_img, label_img)

    bold_data = bold_img.get_fdata(dtype='float32')
    timepoints = bold_img.shape[3]
    label_data = np.asanyarray(label_img.dataobj).astype('int16')

    # Create brain models
    idx_offset = 0
    brainmodels = []
    bm_ts = np.empty((timepoints, 0))

    for structure, labels in CIFTI_STRUCT_WITH_LABELS.items():
        if labels is None:  # surface model
            model_type = "CIFTI_MODEL_TYPE_SURFACE"
            # use the corresponding annotation
            hemi = structure.split('_')[-1]
            # currently only supports L/R cortex
            surf = nb.load(bold_surfs[hemi == "RIGHT"])
            surf_verts = len(surf.darrays[0].data)
            if annotation_files[0].endswith('.annot'):
                annot = nb.freesurfer.read_annot(
                    annotation_files[hemi == "RIGHT"])
                # remove medial wall
                medial = np.nonzero(annot[0] != annot[2].index(b'unknown'))[0]
            else:
                annot = nb.load(annotation_files[hemi == "RIGHT"])
                medial = np.nonzero(annot.darrays[0].data)[0]
            # extract values across volumes
            ts = np.array([tsarr.data[medial] for tsarr in surf.darrays])

            vert_idx = ci.Cifti2VertexIndices(medial)
            bm = ci.Cifti2BrainModel(index_offset=idx_offset,
                                     index_count=len(vert_idx),
                                     model_type=model_type,
                                     brain_structure=structure,
                                     vertex_indices=vert_idx,
                                     n_surface_vertices=surf_verts)
            idx_offset += len(vert_idx)
            bm_ts = np.column_stack((bm_ts, ts))
        else:
            model_type = "CIFTI_MODEL_TYPE_VOXELS"
            vox = []
            ts = None
            for label in labels:
                ijk = np.nonzero(label_data == label)
                if ijk[0].size == 0:  # skip label if nothing matches
                    continue
                ts = (bold_data[ijk] if ts is None else np.concatenate(
                    (ts, bold_data[ijk])))
                vox += [[ijk[0][ix], ijk[1][ix], ijk[2][ix]]
                        for ix, row in enumerate(ts)]

            vox = ci.Cifti2VoxelIndicesIJK(vox)
            bm = ci.Cifti2BrainModel(index_offset=idx_offset,
                                     index_count=len(vox),
                                     model_type=model_type,
                                     brain_structure=structure,
                                     voxel_indices_ijk=vox)
            idx_offset += len(vox)
            bm_ts = np.column_stack((bm_ts, ts.T))
        # add each brain structure to list
        brainmodels.append(bm)

    # add volume information
    brainmodels.append(
        ci.Cifti2Volume(
            bold_img.shape[:3],
            ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(
                -3, bold_img.affine)))

    # generate Matrix information
    series_map = ci.Cifti2MatrixIndicesMap((0, ),
                                           'CIFTI_INDEX_TYPE_SERIES',
                                           number_of_series_points=timepoints,
                                           series_exponent=0,
                                           series_start=0.0,
                                           series_step=tr,
                                           series_unit='SECOND')
    geometry_map = ci.Cifti2MatrixIndicesMap((1, ),
                                             'CIFTI_INDEX_TYPE_BRAIN_MODELS',
                                             maps=brainmodels)
    # provide some metadata to CIFTI matrix
    meta = {
        "surface": targets[0],
        "volume": targets[1],
    }
    # generate and save CIFTI image
    matrix = ci.Cifti2Matrix()
    matrix.append(series_map)
    matrix.append(geometry_map)
    matrix.metadata = ci.Cifti2MetaData(meta)
    hdr = ci.Cifti2Header(matrix)
    img = ci.Cifti2Image(bm_ts, hdr)
    img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES')

    out_file = "{}.dtseries.nii".format(split_filename(bold_file)[1])
    ci.save(img, out_file)
    return os.path.join(os.getcwd(), out_file)
예제 #24
0
def _create_dtseries_cifti(timepoints, models):
    """Create a dense timeseries CIFTI-2 file"""
    import nibabel.cifti2 as ci

    def create_series_map():
        return ci.Cifti2MatrixIndicesMap((0, ),
                                         'CIFTI_INDEX_TYPE_SERIES',
                                         number_of_series_points=timepoints,
                                         series_exponent=0,
                                         series_start=0,
                                         series_step=1,
                                         series_unit='SECOND')

    def create_geometry_map():
        index_offset = 0
        brain_models = []
        timeseries = np.zeros((timepoints, 0))

        for name, data in models:
            if "CORTEX" in name:
                model_type = "CIFTI_MODEL_TYPE_SURFACE"
                attr = "vertex_indices"
                indices = ci.Cifti2VertexIndices(np.arange(len(data)))
            else:
                model_type = "CIFTI_MODEL_TYPE_VOXELS"
                attr = "voxel_indices_ijk"
                indices = ci.Cifti2VoxelIndicesIJK(np.arange(len(data)))
            bm = ci.Cifti2BrainModel(
                index_offset=index_offset,
                index_count=len(data),
                model_type=model_type,
                brain_structure=name,
            )
            setattr(bm, attr, indices)
            index_offset += len(data)
            brain_models.append(bm)
            timeseries = np.column_stack((timeseries, data.T))

        brain_models.append(
            ci.Cifti2Volume(
                (4, 4, 4),
                ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(
                    -3, np.eye(4)),
            ))

        return ci.Cifti2MatrixIndicesMap(
            (1, ),
            "CIFTI_INDEX_TYPE_BRAIN_MODELS",
            maps=brain_models,
        ), timeseries

    matrix = ci.Cifti2Matrix()
    series_map = create_series_map()
    geometry_map, ts = create_geometry_map()
    matrix.append(series_map)
    matrix.append(geometry_map)
    hdr = ci.Cifti2Header(matrix)
    img = ci.Cifti2Image(dataobj=ts, header=hdr)
    img.nifti_header.set_intent("NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES")

    out_file = Path("test.dtseries.nii").absolute()
    ci.save(img, out_file)
    return out_file