def test_GiftiMesh_create_loadAll(): testdir = op.join(op.dirname(__file__), 'testdata') testfile = op.join(testdir, 'example.surf.gii') with tempdir() as td: vertSets = [ op.join(td, 'prefix.L.1.space.surf.gii'), op.join(td, 'prefix.L.2.space.surf.gii'), op.join(td, 'prefix.L.3.space.surf.gii') ] for vs in vertSets: shutil.copy(testfile, vs) mesh = gifti.GiftiMesh(vertSets[0], loadAll=True) assert mesh.selectedVertices() == vertSets[0] mesh.vertices = vertSets[1] assert mesh.selectedVertices() == vertSets[1] mesh.vertices = vertSets[2] assert mesh.selectedVertices() == vertSets[2]
def test_GiftiMesh_loadVertexData(): testdir = op.join(op.dirname(__file__), 'testdata') surffile = op.join(testdir, 'example.surf.gii') shapefile = op.join(testdir, 'example.shape.gii') txtfile = op.join(testdir, 'test_mesh_data.txt') memdata = np.random.randint(1, 10, 642) # load from .gii file surf = gifti.GiftiMesh(surffile) assert surf.loadVertexData(shapefile).shape == (642, 1) # load from .txt file assert surf.loadVertexData(txtfile).shape == (642, 1) # add from memory assert np.all( np.isclose(surf.addVertexData('inmemdata', memdata), memdata.reshape(-1, 1))) # check cached assert surf.getVertexData(shapefile).shape == (642, 1) assert surf.getVertexData(txtfile).shape == (642, 1) assert surf.getVertexData('inmemdata').shape == (642, 1)
def Mesh(filename): """ Reads in a mesh from either a GIFTI (.surf.gii) or a VTK (.vtk) file :param filename: filename provided by the user :return: GIFTI or VTK sub-class of fsl.data.mesh.Mesh """ try: full_filename = path.addExt(filename, ['.surf.gii', '.vtk']) except path.PathError as e: raise argparse.ArgumentTypeError(*e.args) if path.hasExt(full_filename, '.surf.gii'): return gifti.GiftiMesh(full_filename) else: return vtk.VTKMesh(full_filename)
def test_GiftiMesh_needsFixing(): from . import test_mesh verts = test_mesh.CUBE_VERTICES idxs = test_mesh.CUBE_TRIANGLES_CW idxs_fixed = test_mesh.CUBE_TRIANGLES_CCW verts = nib.gifti.GiftiDataArray(verts, intent='NIFTI_INTENT_POINTSET') idxs = nib.gifti.GiftiDataArray(idxs, intent='NIFTI_INTENT_TRIANGLE') gimg = nib.gifti.GiftiImage(darrays=[verts, idxs]) with tempdir(): fname = op.abspath('test.gii') gimg.to_filename(fname) surf = gifti.GiftiMesh(fname, fixWinding=True) assert np.all(np.isclose(surf.indices, idxs_fixed))
def test_loadVertices(): testdir = op.join(op.dirname(__file__), 'testdata') testfile = op.join(testdir, 'example.surf.gii') with tempdir(): mesh = gifti.GiftiMesh(testfile) shutil.copy(testfile, 'example2.surf.gii') verts = mesh.vertices verts2 = verts * 2 np.savetxt('verts.txt', verts2) assert np.all(np.isclose(mesh.loadVertices('example2.surf.gii'), verts)) assert np.all(np.isclose(mesh.loadVertices('verts.txt'), verts2))
def test_GiftiMesh_create(): testdir = op.join(op.dirname(__file__), 'testdata') testfile = op.join(testdir, 'example.surf.gii') surf = gifti.GiftiMesh(testfile) minbounds = np.array([59.50759888, 88.43039703, 72.10890198]) maxbounds = np.array([77.72619629, 128.40600586, 94.82050323]) minb, maxb = surf.bounds assert surf.name == 'example' assert surf.dataSource == testfile assert tuple(surf.vertices.shape) == (642, 3) assert tuple(surf.indices.shape) == (1280, 3) assert isinstance(surf.getMeta(testfile), nib.gifti.GiftiImage) assert np.all(np.isclose(minbounds, minb)) assert np.all(np.isclose(maxbounds, maxb))
def test_GiftiMesh_surface_and_data(): data1 = np.random.randint(0, 10, len(TEST_VERTS)) data2 = np.random.randint(0, 10, len(TEST_VERTS)) expdata = np.vstack([data1, data2]).T verts = TEST_VERT_ARRAY tris = TEST_IDX_ARRAY data1 = nib.gifti.GiftiDataArray(data1, intent='NIFTI_INTENT_SHAPE') data2 = nib.gifti.GiftiDataArray(data2, intent='NIFTI_INTENT_SHAPE') gimg = nib.gifti.GiftiImage(darrays=[verts, tris, data1, data2]) with tempdir(): fname = op.abspath('test.gii') gimg.to_filename(fname) surf = gifti.GiftiMesh(fname) assert np.all(surf.vertices == TEST_VERTS) assert np.all(surf.indices == TEST_IDXS) assert surf.vertexDataSets() == [fname] assert np.all(surf.getVertexData(fname) == expdata)
def test_GiftiMesh_multiple_vertices(): tris = TEST_IDX_ARRAY verts1 = TEST_VERT_ARRAY verts2 = nib.gifti.GiftiDataArray(TEST_VERTS * 5, intent='NIFTI_INTENT_POINTSET') verts3 = nib.gifti.GiftiDataArray(TEST_VERTS * 10, intent='NIFTI_INTENT_POINTSET') gimg = nib.gifti.GiftiImage(darrays=[verts1, verts2, tris]) gimg2 = nib.gifti.GiftiImage(darrays=[verts3, tris]) with tempdir(): fname = op.abspath('test.gii') fname2 = op.abspath('test2.gii') gimg.to_filename(fname) gimg2.to_filename(fname2) surf = gifti.GiftiMesh(fname) expvsets = [fname, '{}_1'.format(fname)] expbounds1 = np.min(verts1.data, axis=0), np.max(verts1.data, axis=0) expbounds2 = np.min(verts2.data, axis=0), np.max(verts2.data, axis=0) expbounds3 = np.min(verts3.data, axis=0), np.max(verts3.data, axis=0) assert np.all(surf.vertices == TEST_VERTS) assert np.all(surf.indices == TEST_IDXS) assert surf.vertexSets() == expvsets assert np.all(np.isclose(surf.bounds, expbounds1)) surf.vertices = expvsets[1] assert np.all(surf.vertices == TEST_VERTS * 5) assert np.all(np.isclose(surf.bounds, expbounds2)) surf.loadVertices(fname2, select=True) assert np.all(surf.vertices == TEST_VERTS * 10) assert np.all(np.isclose(surf.bounds, expbounds3))