def test_loadVertexData_morph(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) with tempdir(): vdata1 = np.random.randint(1, 100, verts.shape[0]) vdata2 = np.random.randint(1, 100, verts.shape[0]) vdata3 = np.random.randint(1, 100, verts.shape[0]) gen_freesurfer_geometry('lh.pial', verts, tris) gen_freesurfer_morph('lh.curv', vdata1) gen_freesurfer_morph('lh.thickness', vdata2) np.savetxt( 'vdata.txt', vdata3) # bad gen_freesurfer_morph('rh.sulc', vdata2[:-1]) mesh = fslfs.FreesurferMesh('lh.pial') assert np.all(np.isclose(mesh.loadVertexData('lh.curv'), vdata1.reshape(-1, 1))) assert np.all(np.isclose(mesh.loadVertexData('lh.thickness'), vdata2.reshape(-1, 1))) assert np.all(np.isclose(mesh.loadVertexData('vdata.txt'), vdata3.reshape(-1, 1))) with pytest.raises(ValueError): mesh.loadVertexData('rh.sulc')
def asgifti(infile): mesh = fslfs.FreesurferMesh(infile) verts = nib.gifti.GiftiDataArray(mesh.vertices, intent='NIFTI_INTENT_POINTSET') tris = nib.gifti.GiftiDataArray(mesh.indices, intent='NIFTI_INTENT_TRIANGLE') gmesh = nib.gifti.GiftiImage(darrays=[verts, tris]) outfile = op.basename(infile) + '.surf.gii' gmesh.to_filename(outfile) return outfile
def test_FreesurferMesh_create(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) with tempdir(): gen_freesurfer_geometry('lh.pial', verts, tris) mesh = fslfs.FreesurferMesh('lh.pial') assert mesh.name == 'lh.pial' assert np.all(np.isclose(mesh.vertices, verts)) assert np.all(np.isclose(mesh.indices, tris))
def test_FreesurferMesh_create_loadall(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) with tempdir(): gen_freesurfer_geometry('lh.pial', verts, tris) vertSets = ['lh.orig', 'lh.white', 'lh.inflated'] for vs in vertSets: shutil.copy('lh.pial', vs) mesh = fslfs.FreesurferMesh('lh.pial', loadAll=True) assert list(sorted(mesh.vertexSets())) == \ list(sorted([op.abspath(p) for p in ['lh.pial'] + vertSets]))
def test_loadVertices(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) with tempdir(): gen_freesurfer_geometry('lh.pial', verts, tris) gen_freesurfer_geometry('rh.pial', verts * 2, tris) # bad gen_freesurfer_geometry('lh.orig', verts[:-1, :], tris) np.savetxt('verts.txt', verts * 3) mesh = fslfs.FreesurferMesh('lh.pial') assert np.all(np.isclose(mesh.loadVertices('rh.pial'), verts * 2)) assert np.all(np.isclose(mesh.loadVertices('verts.txt'), verts * 3)) with pytest.raises(ValueError): mesh.loadVertices('lh.orig')
def test_loadVertexData_mgh(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) nverts = verts.shape[0] with tempdir(): data = np.random.randint(1, 100, (nverts, 1, 1), dtype=np.int32) img = nib.freesurfer.mghformat.MGHImage(data, np.eye(4)) nib.save(img, 'lh.vdata.mgh') gen_freesurfer_geometry('lh.pial', verts, tris) mesh = fslfs.FreesurferMesh('lh.pial') assert np.all(np.isclose(mesh.loadVertexData('lh.vdata.mgh'), data.reshape(-1, 1))) del img del mesh img = None
def test_loadVertexData_label(): verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) nverts = verts.shape[0] with tempdir(): # Currently, vertex scalar data is # ignored by the FreesurferMesh class lverts = np.random.choice(np.arange(nverts), 4) vdata = np.random.randint(1, 100, 4) gen_freesurfer_geometry('lh.pial', verts, tris) gen_freesurfer_label('lh.vdata.label', lverts, vdata) mesh = fslfs.FreesurferMesh('lh.pial') exp = np.zeros((nverts, 1)) exp[lverts, :] = 1 assert np.all(np.isclose(mesh.loadVertexData('lh.vdata.label'), exp))
def test_loadVertexData_annot(): import nibabel.info as nibinfo # assume nibabel 2.* # nibabel 2.2.1 is broken w.r.t. .annot files. if nibinfo._version_minor == 2 and nibinfo._version_micro <= 1: return verts = np.array(CUBE_VERTICES) tris = np.array(CUBE_TRIANGLES_CCW) with tempdir(): nlabels = 3 names = ['label {}'.format(l) for l in range(1, nlabels + 1)] rgba = np.random.randint(0, 255, (nlabels, 4), dtype=np.int32) labels = list(range(nlabels)) + list(np.random.randint(0, nlabels, verts.shape[0] - nlabels)) labels = np.array(labels, dtype=np.int32) np.random.shuffle(labels) gen_freesurfer_geometry('lh.pial', verts, tris) gen_freesurfer_annot('lh.aparc.annot', labels, rgba, names) mesh = fslfs.FreesurferMesh('lh.pial') vdfile = op.abspath('lh.aparc.annot') loaded = mesh.loadVertexData(vdfile) ergbal, enames = mesh.getVertexDataColourTable(vdfile) enames = [n.decode() for n in enames] assert np.all(np.isclose(loaded, labels.reshape(-1, 1))) assert list(enames) == list(names) assert np.all(np.isclose(ergbal[:, :4], rgba))