Esempio n. 1
0
def test_cifti2_vertices():
    vs = ci.Cifti2Vertices()
    with pytest.raises(ci.Cifti2HeaderError):
        vs.to_xml()

    vs.brain_structure = 'CIFTI_STRUCTURE_OTHER'

    assert vs.to_xml().decode(
        'utf-8') == '<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER" />'

    assert len(vs) == 0
    vs.extend(np.array([0, 1, 2]))
    assert len(vs) == 3
    with pytest.raises(ValueError):
        vs[1] = 'a'
    with pytest.raises(ValueError):
        vs.insert(1, 'a')

    assert vs.to_xml().decode(
        'utf-8'
    ) == '<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER">0 1 2</Vertices>'

    vs[0] = 10
    assert vs[0] == 10
    assert len(vs) == 3
    vs = ci.Cifti2Vertices(vertices=[0, 1, 2])
    assert len(vs) == 3
Esempio n. 2
0
    def to_mapping(self, dim):
        """
        Converts the parcels 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_PARCELS')
        if self.affine is not None:
            affine = cifti2.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(-3, matrix=self.affine)
            mim.volume = cifti2.Cifti2Volume(self.volume_shape, affine)
        for name, nvertex in self.nvertices.items():
            mim.append(cifti2.Cifti2Surface(name, nvertex))
        for name, voxels, vertices in self.arr:
            cifti_voxels = cifti2.Cifti2VoxelIndicesIJK(voxels)
            element = cifti2.Cifti2Parcel(name, cifti_voxels)
            for name, idx_vertices in vertices.items():
                element.vertices.append(cifti2.Cifti2Vertices(name, idx_vertices))
            mim.append(element)
        return mim
Esempio n. 3
0
def test_cifti2_vertices():
    vs = ci.Cifti2Vertices()
    assert_raises(ci.Cifti2HeaderError, vs.to_xml)
    vs.brain_structure = 'CIFTI_STRUCTURE_OTHER'
    assert_equal(vs.to_xml().decode('utf-8'),
                 '<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER" />')
    assert_equal(len(vs), 0)
    vs.extend(np.array([0, 1, 2]))
    assert_equal(len(vs), 3)
    assert_raises(ValueError, vs.__setitem__, 1, 'a')
    assert_raises(ValueError, vs.insert, 1, 'a')
    assert_equal(
        vs.to_xml().decode('utf-8'),
        '<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER">0 1 2</Vertices>')

    vs[0] = 10
    assert_equal(vs[0], 10)
    assert_equal(len(vs), 3)
    vs = ci.Cifti2Vertices(vertices=[0, 1, 2])
    assert_equal(len(vs), 3)
Esempio n. 4
0
def test_cifti2_parcel():
    pl = ci.Cifti2Parcel()
    assert_raises(ci.Cifti2HeaderError, pl.to_xml)
    assert_raises(TypeError, pl.append_cifti_vertices, None)

    assert_raises(ValueError, ci.Cifti2Parcel, **{'vertices': [1, 2, 3]})
    pl = ci.Cifti2Parcel(name='region',
                         voxel_indices_ijk=ci.Cifti2VoxelIndicesIJK([[1, 2, 3]]),
                         vertices=[ci.Cifti2Vertices([0, 1, 2])])
    pl.pop_cifti2_vertices(0)
    assert_equal(len(pl.vertices), 0)
    assert_equal(
        pl.to_xml().decode('utf-8'),
        '<Parcel Name="region"><VoxelIndicesIJK>1 2 3</VoxelIndicesIJK></Parcel>'
    )
Esempio n. 5
0
def test_cifti2_parcel():
    pl = ci.Cifti2Parcel()
    with pytest.raises(ci.Cifti2HeaderError):
        pl.to_xml()

    with pytest.raises(TypeError):
        pl.append_cifti_vertices(None)

    with pytest.raises(ValueError):
        ci.Cifti2Parcel(vertices=[1, 2, 3])

    pl = ci.Cifti2Parcel(name='region',
                         voxel_indices_ijk=ci.Cifti2VoxelIndicesIJK([[1, 2,
                                                                      3]]),
                         vertices=[ci.Cifti2Vertices([0, 1, 2])])
    pl.pop_cifti2_vertices(0)

    assert len(pl.vertices) == 0
    assert pl.to_xml().decode(
        'utf-8'
    ) == '<Parcel Name="region"><VoxelIndicesIJK>1 2 3</VoxelIndicesIJK></Parcel>'
Esempio n. 6
0
def create_parcel_map(applies_to_matrix_dimension):
    mapping = ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension,
                                        'CIFTI_INDEX_TYPE_PARCELS')
    for name, elements in parcels:
        surfaces = []
        volume = None
        for element in elements:
            if isinstance(element[0], str):
                surfaces.append(ci.Cifti2Vertices(element[0], element[1]))
            else:
                volume = ci.Cifti2VoxelIndicesIJK(element)
        mapping.append(ci.Cifti2Parcel(name, volume, surfaces))

    mapping.extend([
        ci.Cifti2Surface('CIFTI_STRUCTURE_CORTEX_%s' % orientation,
                         number_of_vertices)
        for orientation in ['LEFT', 'RIGHT']
    ])
    mapping.volume = ci.Cifti2Volume(
        dimensions,
        ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(-3, affine))
    return mapping
Esempio n. 7
0
def create_parcel(info):
    """Create a list of Cifti2Parcels from a list of ParcelInfo"""
    allowed_brain_structures = (Structure.CORTEX_LEFT, Structure.CORTEX_RIGHT,
                                Structure.CEREBELLUM)
    surfaces = []
    volume = None

    has_surfaces = info.surfaces is not None
    has_ijk = info.voxel_ijk is not None

    if not has_vertices and not has_ijk:
        raise Exception
    if has_vertices:
        for surface in info.surfaces:
            if info.brain_structure not in allowed_brain_structure:
                raise Exception
            surfaces.append(
                ci.Cifti2Vertices(surface.brain_structure, surface.indices))
    if has_indices:
        volume = ci.Cifti2VoxelIndicesIJK(info.voxel_ijk)

    return ci.Cifti2Parcel(info.name, volume, surfaces)