def load_dwi_files_blinds(folder_name): from dipy.core.gradients import gradient_table from dipy.core.geometry import compose_matrix, decompose_matrix for file in os.listdir(folder_name): if file.endswith(".bvec"): bvec_file = os.path.join(folder_name, file) if file.endswith("labels.nii"): labels_file_name = os.path.join(folder_name, file) if file.endswith("WM.nii"): wm_file_name = os.path.join(folder_name, file) bval_file = bvec_file[:-4:]+'bval' nii_file = bvec_file[:-4:]+'nii' hardi_img = nib.load(nii_file) data = hardi_img.get_fdata() affine = hardi_img.affine scale, shear, ang, trans, pre = decompose_matrix(affine) shear = np.zeros(np.shape(shear)) affine = compose_matrix(scale, shear, ang, trans, pre) gtab = gradient_table(bval_file, bvec_file) #voxel_size = nib.affines.voxel_sizes(affine) #data, affine1 = reslice(data, affine, voxel_size, (3., 3., 3.)) labels_img = nib.load(labels_file_name) labels = labels_img.get_fdata() #labels,affine1=reslice(labels,affine,voxel_size,(3., 3., 3.)) wm_img = nib.load(wm_file_name) white_matter = wm_img.get_fdata() #white_matter,affine1=reslice(white_matter,affine,voxel_size,(3., 3., 3.)) return gtab,data,affine,labels,white_matter,nii_file,bvec_file
def test_compose_decompose_matrix(): for translate in permutations(40 * np.random.rand(3), 3): for angles in permutations(np.deg2rad(90 * np.random.rand(3)), 3): for shears in permutations(3 * np.random.rand(3), 3): for scale in permutations(3 * np.random.rand(3), 3): mat = compose_matrix(translate=translate, angles=angles, shear=shears, scale=scale) sc, sh, ang, trans, _ = decompose_matrix(mat) assert_array_almost_equal(translate, trans) assert_array_almost_equal(angles, ang) assert_array_almost_equal(shears, sh) assert_array_almost_equal(scale, sc)
def compose_matrix44(t, dtype=np.double): """ Compose a 4x4 transformation matrix Parameters ----------- t : ndarray This is a 1D vector of affine transformation parameters with size at least 3. If the size is 3, t is interpreted as translation. If the size is 6, t is interpreted as translation + rotation. If the size is 7, t is interpreted as translation + rotation + isotropic scaling. If the size is 9, t is interpreted as translation + rotation + anisotropic scaling. If size is 12, t is interpreted as translation + rotation + scaling + shearing. Returns ------- T : ndarray Homogeneous transformation matrix of size 4x4. """ if isinstance(t, list): t = np.array(t) size = t.size if size not in [3, 6, 7, 9, 12]: raise ValueError('Accepted number of parameters is 3, 6, 7, 9 and 12') MAX_DIST = 1e10 scale, shear, angles, translate = (None, ) * 4 translate = _threshold(t[0:3], MAX_DIST) if size in [6, 7, 9, 12]: angles = np.deg2rad(t[3:6]) if size == 7: scale = np.array((t[6], ) * 3) if size in [9, 12]: scale = t[6:9] if size == 12: shear = t[9:12] return compose_matrix(scale=scale, shear=shear, angles=angles, translate=translate)
def test_rigid_partial_real_bundles(): static = fornix_streamlines()[:20] moving = fornix_streamlines()[20:40] static_center, shift = center_streamlines(static) moving_center, shift2 = center_streamlines(moving) print(shift2) mat = compose_matrix(translate=np.array([0, 0, 0.]), angles=np.deg2rad([40, 0, 0.])) moved = transform_streamlines(moving_center, mat) srr = StreamlineLinearRegistration() srm = srr.optimize(static_center, moved) print(srm.fopt) print(srm.iterations) print(srm.funcs) moving_back = srm.transform(moved) print(srm.matrix) static_center = set_number_of_points(static_center, 100) moving_center = set_number_of_points(moving_back, 100) vol = np.zeros((100, 100, 100)) spts = np.concatenate(static_center, axis=0) spts = np.round(spts).astype(np.int) + np.array([50, 50, 50]) mpts = np.concatenate(moving_center, axis=0) mpts = np.round(mpts).astype(np.int) + np.array([50, 50, 50]) for index in spts: i, j, k = index vol[i, j, k] = 1 vol2 = np.zeros((100, 100, 100)) for index in mpts: i, j, k = index vol2[i, j, k] = 1 overlap = np.sum(np.logical_and(vol, vol2)) / float(np.sum(vol2)) assert_equal(overlap * 100 > 40, True)
def compose_matrix44(t, dtype=np.double): """ Compose a 4x4 transformation matrix Parameters ----------- t : ndarray This is a 1D vector of of affine transformation parameters with size at least 3. If size is 3, t is interpreted as translation. If size is 6, t is interpreted as translation + rotation. If size is 7, t is interpreted as translation + rotation + isotropic scaling. If size is 9, t is interpreted as translation + rotation + anisotropic scaling. If size is 12, t is interpreted as translation + rotation + scaling + shearing. Returns ------- T : ndarray Homogeneous transformation matrix of size 4x4. """ if isinstance(t, list): t = np.array(t) size = t.size if size not in [3, 6, 7, 9, 12]: raise ValueError('Accepted number of parameters is 3, 6, 7, 9 and 12') MAX_DIST = 1e10 scale, shear, angles, translate = (None, ) * 4 translate = _threshold(t[0:3], MAX_DIST) if size in [6, 7, 9, 12]: angles = np.deg2rad(t[3:6]) if size == 7: scale = np.array((t[6],) * 3) if size in [9, 12]: scale = t[6: 9] if size == 12: shear = t[9: 12] return compose_matrix(scale=scale, shear=shear, angles=angles, translate=translate)