b0 = hardi_data[..., gtab.b0s_mask] mean_b0 = np.mean(b0, -1) ni_b0 = nib.Nifti1Image(mean_b0, hardi_affine) ni_b0.to_filename('mean_b0.nii') plt.matshow(mean_b0[:, :, mean_b0.shape[-1] // 2], cmap=cm.bone) MNI_T2 = dpd.read_mni_template() MNI_T2_data = MNI_T2.get_data() MNI_T2_affine = MNI_T2.get_affine() level_iters = [10, 10, 5] dim = 3 metric = CCMetric(dim) sdr = SymmetricDiffeomorphicRegistration(metric, level_iters, step_length=0.25) sdr.verbosity = VerbosityLevels.DIAGNOSE mapping = sdr.optimize(MNI_T2_data, mean_b0, MNI_T2_affine, hardi_affine) warped_b0 = mapping.transform(mean_b0) plt.matshow(warped_b0[:, :, warped_b0.shape[-1] // 2], cmap=cm.bone) plt.matshow(MNI_T2_data[:, :, MNI_T2_data.shape[-1] // 2], cmap=cm.bone) new_ni = nib.Nifti1Image(warped_b0, MNI_T2_affine) new_ni.to_filename('./warped_b0.nii.gz') afqpath = 'D:/opt/AFQ/' LOCC_ni = nib.load( os.path.join(afqpath, 'templates/callosum2/L_Occipital.nii.gz')) ROCC_ni = nib.load( os.path.join(afqpath, 'templates/callosum2/R_Occipital.nii.gz')) midsag_ni = nib.load( os.path.join(afqpath, 'templates/callosum2/Callosum_midsag.nii.gz'))
import nibabel as nib from dipy.align.imwarp import SymmetricDiffeomorphicRegistration from dipy.align.metrics import CCMetric f_t1 = '/home/eleftherios/Data/reg/t1_fa.nii.gz' f_fa = '/home/eleftherios/Data/reg/fa.nii.gz' t1 = nib.load(f_t1).get_data() img = nib.load(f_fa) fa = img.get_data() fa_aff = img.get_affine() cc = CCMetric(3) sdr = SymmetricDiffeomorphicRegistration(metric=cc) sdr.verbosity = 2 sdm = sdr.optimize(static=fa, moving=t1) t1_warped = sdm.transform(t1) f_t1_warped = '/home/eleftherios/Data/reg/t1_fa_warped.nii.gz' nib.save(nib.Nifti1Image(t1_warped, fa_aff), f_t1_warped)
with open(config_fname,'r') as f: options = [tuple(line.strip().split()) for line in f.readlines()] options = {opt[0]:opt[1] for opt in options} else: options = {} step_length = float(options.get('step_length', '0.25')) inv_tol = float(options.get('inv_tol', '1e-3')) inv_iter = int(options.get('inv_iter', '20')) experiment_name = options.get('experiment_name') level_iters = [400, 200, 100, 50, 25] dim = 3 metric = SSDMetric(dim, smooth=3) sdr = SymmetricDiffeomorphicRegistration(metric, level_iters, inv_iter=inv_iter, inv_tol=inv_tol, opt_tol=1e-6, step_length=step_length, ss_sigma_factor=1.0) sdr.verbosity = VerbosityLevels.DIAGNOSE start = time.time() mapping = sdr.optimize(cup, sphere) end = time.time() elapsed = end - start print('Elapsed: %e'%(elapsed,)) fwd = np.array(mapping.forward) bwd = np.array(mapping.backward) # Get deformed mid slices z0 = fwd.shape[2]//2 whlines, wvlines = get_deformed_grid(fwd, [z0]) fwd_fname = 'fwd_lines_%s.p'%(experiment_name,) pickle.dump((whlines, wvlines), open(fwd_fname,'wb'))
ibsr_bw_affmap = dipy_align(ibsr1, ibsr1_affine, brainweb, brainweb_affine) pickle.dump(ibsr_bw_affmap, open(aff_name, "w")) bw_on_ibsr1 = ibsr_bw_affmap.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=0) # aligned (sagital view) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=1) # aligned (axial view) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=2) # aligned (coronal view) # Start diffeomorphic registration diff_name = "ibsr1_to_brainweb_diff.p" if os.path.isfile(diff_name): ibsr_bw_diffmap = pickle.load(open(diff_name, "r")) else: metric = CCMetric(3) level_iters = [50, 10] sdr = SymmetricDiffeomorphicRegistration(metric, level_iters) sdr.verbosity = VerbosityLevels.DEBUG ibsr_bw_diffmap = sdr.optimize(ibsr1, brainweb, ibsr1_affine, brainweb_affine, ibsr_bw_affmap.affine) pickle.dump(ibsr_bw_diffmap, open(diff_name, "w")) bw_warped_ibsr1 = ibsr_bw_diffmap.transform(brainweb) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=0) # warped (sagital view) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=1) # warped (axial view) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=2) # warped (coronal view) # Now the initial segmentation bw_mask_ibsr1 = ibsr_bw_diffmap.transform(brainweb_mask) bw_mask_ibsr1 = bw_mask_ibsr1 > 0 # Dilate structure = np.ones((5, 5, 5)) dilated_mask = binary_dilation(bw_mask_ibsr1, structure)
ibsr_bw_affmap = dipy_align(ibsr1, ibsr1_affine, brainweb, brainweb_affine) pickle.dump(ibsr_bw_affmap, open(aff_name, 'w')) bw_on_ibsr1 = ibsr_bw_affmap.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=0) # aligned (sagital view) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=1) # aligned (axial view) rt.overlay_slices(ibsr1, bw_on_ibsr1, slice_type=2) # aligned (coronal view) # Start diffeomorphic registration diff_name = 'ibsr1_to_brainweb_diff.p' if os.path.isfile(diff_name): ibsr_bw_diffmap = pickle.load(open(diff_name, 'r')) else: metric = CCMetric(3) level_iters = [50, 10] sdr = SymmetricDiffeomorphicRegistration(metric, level_iters) sdr.verbosity = VerbosityLevels.DEBUG ibsr_bw_diffmap = sdr.optimize(ibsr1, brainweb, ibsr1_affine, brainweb_affine, ibsr_bw_affmap.affine) pickle.dump(ibsr_bw_diffmap, open(diff_name, 'w')) bw_warped_ibsr1 = ibsr_bw_diffmap.transform(brainweb) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=0) # warped (sagital view) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=1) # warped (axial view) rt.overlay_slices(ibsr1, bw_warped_ibsr1, slice_type=2) # warped (coronal view) # Now the initial segmentation bw_mask_ibsr1 = ibsr_bw_diffmap.transform(brainweb_mask) bw_mask_ibsr1 = bw_mask_ibsr1 > 0