def test_MGHImage(): testfile = op.join(datadir, 'example.mgz') # Load from a file img = fslmgh.MGHImage(testfile) nbimg = nib.load(testfile) v2s = nbimg.header.get_vox2ras_tkr() w2s = affine.concat(v2s, affine.invert(nbimg.affine)) assert np.all(np.isclose(img[:], np.asanyarray(nbimg.dataobj))) assert np.all(np.isclose(img.voxToWorldMat, nbimg.affine)) assert np.all(np.isclose(img.voxToSurfMat, v2s)) assert np.all(np.isclose(img.surfToVoxMat, affine.invert(v2s))) assert np.all(np.isclose(img.worldToSurfMat, w2s)) assert np.all(np.isclose(img.surfToWorldMat, affine.invert(w2s))) assert img.name == op.basename(testfile) assert img.dataSource == testfile assert img.mghImageFile == testfile # Load from an in-memory nibabel object img = fslmgh.MGHImage(nbimg) assert np.all(np.isclose(img[:], np.asanyarray(nbimg.dataobj))) assert np.all(np.isclose(img.voxToWorldMat, nbimg.affine)) assert img.dataSource is None assert img.mghImageFile is None
def loadVertexDataFile(infile): """Loads the given Freesurfer vertex data, label, or annotation file. This function return different things depending on what ``infile`` is: - If ``infile`` is a vertex data file, a ``(nvertices,)`` array is returned, containing one value for each vertex in the mesh. - If ``infile`` is a ``mgh``/``mgz`` file, the image data is returned as-is, with dimensions of length 1 squeezed out (under the assumption that the image contains scalar vertex data). - If ``infile`` is a vertex label file, a tuple containing the following is returned: - a ``(n,)`` array, containing the indices of all vertices that are specified in the file. - a ``(n,)`` array, containing scalar value for each vertex - If ``infile`` is a vertex annotation file, a tuple containing the following is returned: - a ``(n,)`` array containing the indices of all ``n`` vertices that are specified in the file. - a ``(l, 5)`` array containing the RGBA colour, and the label value, for every label that is specified in the file. - A list of length ``l``, containing the names of every label that is specified in the file. """ if isVertexDataFile(infile): return nibfs.read_morph_data(infile) elif isVertexLabelFile(infile): return nibfs.read_label(infile, read_scalars=True) elif isVertexAnnotFile(infile): # nibabel 2.2.1 is broken w.r.t. .annot files. # raise ValueError('.annot files are not yet supported') labels, lut, names = nibfs.read_annot(infile, orig_ids=False) return labels, lut, names elif isVertexMGHFile(infile): return fslmgh.MGHImage(infile)[:].squeeze() else: raise ValueError('Unrecognised freesurfer ' 'file type: {}'.format(infile))
def test_MGHImage_save(): testfile = op.join(datadir, 'example.mgz') with tempdir.tempdir(): shutil.copy(testfile, 'example.mgz') testfile = 'example.mgz' img = fslmgh.MGHImage(testfile) img.save() expfile = op.abspath(fslimage.addExt('example', mustExist=False)) assert img.dataSource == op.abspath(expfile)
def test_voxToSurfMat(): testfile = op.join(datadir, 'example.mgz') img = fslmgh.MGHImage(testfile) assert np.all(np.isclose(img.voxToSurfMat, fslmgh.voxToSurfMat(img)))