def test_anisotropic_normalized(self, sphere3_msh, tensor): t = tensor cond_list = [None, None, 2, 7, 9] v = np.zeros((255, 255, 255, 6)) for i, j in enumerate([0, 1, 2, 4, 5, 8]): v[..., i] = t.reshape(-1)[j] affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127], [0, 0, 0, 1]]) elmcond = cond.cond2elmdata(sphere3_msh, cond_list, v, affine, aniso_tissues=3, normalize=True) tensor = elmcond.value[sphere3_msh.elm.tag1 == 3].reshape(-1, 3, 3) t = affine[:3, :3].dot(t).dot(affine[:3, :3]) assert np.allclose(np.linalg.det(tensor), 2**3) assert np.allclose( np.linalg.eig(tensor)[1][:, 0], np.linalg.eig(t)[1][0]) assert np.allclose( np.linalg.eig(tensor)[1][:, 1], np.linalg.eig(t)[1][1]) assert np.allclose( np.linalg.eig(tensor)[1][:, 2], np.linalg.eig(t)[1][2]) assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 4], [7, 0, 0, 0, 7, 0, 0, 0, 7]) assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 5], [9, 0, 0, 0, 9, 0, 0, 0, 9])
def main(): args = parseArguments(sys.argv[1:]) if not os.path.isfile(args.fn_nifti): raise IOError('Could not find file: {0}'.format(args.fn_nifti)) if not os.path.isfile(args.fn_mesh): raise IOError('Could not find file: {0}'.format(args.fn_mesh)) image = nibabel.load(args.fn_nifti) mesh = mesh_io.read_msh(args.fn_mesh) vol = image.dataobj affine = image.affine if args.ev: logger.info('Creating tensor visualization') mesh = mesh.crop_mesh([1, 2, 1001, 1002]) cond_list = [c.value for c in cond.standard_cond()] sigma = cond.cond2elmdata(mesh, cond_list, anisotropy_volume=vol, affine=affine, aniso_tissues=[1, 2]) views = cond.TensorVisualization(sigma, mesh) mesh.nodedata = [] mesh.elmdata = views mesh_io.write_msh(mesh, args.fn_out) else: logger.info('Interpolating data in NifTI file to mesh') if args.type == 'elements': ed = mesh_io.ElementData.from_data_grid(mesh, vol, affine, 'from_volume') mesh.elmdata.append(ed) if args.type == 'nodes': nd = mesh_io.NodeData.from_data_grid(mesh, vol, affine, 'from_volume') mesh.nodedata.append(nd) mesh_io.write_msh(mesh, args.fn_out)
def test_isotropic(self, sphere3_msh): cond_list = [None, None, 1, 2, 3] c = cond.cond2elmdata(sphere3_msh, cond_list).value assert np.all(c[sphere3_msh.elm.tag1 == 3] == 1) assert np.all(c[sphere3_msh.elm.tag1 == 1003] == 1) assert np.all(c[sphere3_msh.elm.tag1 == 4] == 2) assert np.all(c[sphere3_msh.elm.tag1 == 1004] == 2) assert np.all(c[sphere3_msh.elm.tag1 == 5] == 3) assert np.all(c[sphere3_msh.elm.tag1 == 1005] == 3)
def test_anisotropic(self, sphere3_msh, tensor): cond_list = [None, None, 1, 2, 3] v = np.zeros((255, 255, 255, 6)) for i, j in enumerate([0, 1, 2, 4, 5, 8]): v[:, :, :, i] = tensor.reshape(-1)[j] affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127], [0, 0, 0, 1]]) msh = sphere3_msh elmcond = cond.cond2elmdata(msh, cond_list, v, affine, aniso_tissues=3, max_cond=np.inf, max_ratio=np.inf, correct_intensity=False) t = affine[:3, :3].dot(tensor).dot(affine[:3, :3]) assert np.allclose(elmcond.value[msh.elm.tag1 == 3], t.reshape(-1)) assert np.allclose(elmcond.value[msh.elm.tag1 == 4], [2, 0, 0, 0, 2, 0, 0, 0, 2]) assert np.allclose(elmcond.value[msh.elm.tag1 == 5], [3, 0, 0, 0, 3, 0, 0, 0, 3]) vol = np.linalg.eig(tensor)[0].prod()**(1. / 3.) scaling = (vol * 1 + vol * 2) / (2 * (vol**2)) elmcond = cond.cond2elmdata(msh, cond_list, v, affine, aniso_tissues=[3, 4], max_cond=np.inf, max_ratio=np.inf, correct_intensity=True) assert np.allclose(elmcond.value[msh.elm.tag1 == 3], scaling * t.reshape(-1)) assert np.allclose(elmcond.value[msh.elm.tag1 == 4], scaling * t.reshape(-1)) assert np.allclose(elmcond.value[msh.elm.tag1 == 5], [3, 0, 0, 0, 3, 0, 0, 0, 3])
def test_anisotropic_zero_region(self, sphere3_msh, tensor): t = tensor cond_list = [None, None, 2, 7, 9] v = np.zeros((255, 255, 255, 6)) affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127], [0, 0, 0, 1]]) elmcond = cond.cond2elmdata(sphere3_msh, cond_list, v, affine, aniso_tissues=3, normalize=True) assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 3], [2, 0, 0, 0, 2, 0, 0, 0, 2]) '''
def test_anisotropic_scale_excentricity(self, sphere3_msh, tensor): t = tensor cond_list = [None, None, 2, 7, 9] v = np.zeros((255, 255, 255, 6)) for i, j in enumerate([0, 1, 2, 4, 5, 8]): v[..., i] = t.reshape(-1)[j] affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127], [0, 0, 0, 1]]) elmcond = cond.cond2elmdata(sphere3_msh, cond_list, v, affine, aniso_tissues=3, normalize=True, excentricity_scaling=0.) tensor = elmcond.value[sphere3_msh.elm.tag1 == 3].reshape(-1, 3, 3) t = affine[:3, :3].dot(t).dot(affine[:3, :3]) assert np.allclose(np.linalg.det(tensor), 2 ** 3) assert np.allclose(tensor, 2 * np.eye(3))
args = parseArguments(sys.argv[1:]) if not os.path.isfile(args.fn_nifti): raise IOError('Could not find file: {0}'.format(args.fn_nifti)) if not os.path.isfile(args.fn_mesh): raise IOError('Could not find file: {0}'.format(args.fn_mesh)) image = nibabel.load(args.fn_nifti) mesh = gmsh.read_msh(args.fn_mesh) vol = image.dataobj affine = image.affine if args.ev: logger.info('Creating tensor visualization') mesh = mesh.crop_mesh([1, 2, 1001, 1002]) cond_list = [c.value for c in cond.standard_cond()] sigma = cond.cond2elmdata(mesh, cond_list, anisotropy_volume=vol, affine=affine, aniso_tissues=[1, 2]) views = cond.TensorVisualization(sigma) mesh.nodedata = [] mesh.elmdata = views gmsh.write_msh(mesh, args.fn_out) else: logger.info('Interpolating data in NifTI file to mesh') ed = gmsh.ElementData.from_data_grid(mesh, vol, affine, 'from_volume', bounds_error=False, fill_value=0) mesh.nodedata = []