def analyze_roi(p, delta, mod1, ss_mod1, mod1_name, mod2, ss_mod2, mod2_name, residuals, check_function): px = p[0] py = p[1] pz = p[2] roi = np.zeros_like(residuals) roi[(px - delta) : (px + delta + 1), (py - delta) : (py + delta + 1), (pz - delta) : (pz + delta + 1)] = 1 rt.plot_slices(residuals_nb, cmap=None) rt.plot_slices((roi + 0.1) * residuals_nb, cmap=None) samples = roi * residuals_nb samples = samples[roi != 0].reshape(-1) x = mod1[roi != 0].reshape(-1) y = mod2[roi != 0].reshape(-1) check_function(x, y, mod1_name, mod2_name, 0) check_function(x, y, mod1_name, mod2_name, 1) x_ss_t1 = mod1[roi != 0].reshape(-1) y_ss_t1 = ss_mod1[roi != 0].reshape(-1) check_function(x_ss_t1, y_ss_t1, mod1_name, "F[" + mod2_name + "]", 0) check_function(x_ss_t1, y_ss_t1, mod1_name, "F[" + mod2_name + "]", 1) x_ss_t2 = mod2[roi != 0].reshape(-1) y_ss_t2 = ss_mod2[roi != 0].reshape(-1) check_function(x_ss_t2, y_ss_t2, mod2_name, "F[" + mod1_name + "]", 0) check_function(x_ss_t2, y_ss_t2, mod2_name, "F[" + mod1_name + "]", 1)
def analyze_roi(p, delta, mod1, ss_mod1, mod1_name, mod2, ss_mod2, mod2_name, residuals, check_function): px = p[0] py = p[1] pz = p[2] roi = np.zeros_like(residuals) roi[(px-delta):(px+delta+1), (py-delta):(py+delta+1), (pz-delta):(pz+delta+1)] = 1 rt.plot_slices(residuals_nb, cmap=None) rt.plot_slices((roi+0.1)*residuals_nb, cmap=None) samples = roi*residuals_nb samples = samples[roi!=0].reshape(-1) x = mod1[roi!=0].reshape(-1) y = mod2[roi!=0].reshape(-1) check_function(x, y, mod1_name, mod2_name, 0) check_function(x, y, mod1_name, mod2_name, 1) x_ss_t1 = mod1[roi!=0].reshape(-1) y_ss_t1 = ss_mod1[roi!=0].reshape(-1) check_function(x_ss_t1, y_ss_t1, mod1_name, "F["+mod2_name+"]", 0) check_function(x_ss_t1, y_ss_t1, mod1_name, "F["+mod2_name+"]", 1) x_ss_t2 = mod2[roi!=0].reshape(-1) y_ss_t2 = ss_mod2[roi!=0].reshape(-1) check_function(x_ss_t2, y_ss_t2, mod2_name, "F["+mod1_name+"]", 0) check_function(x_ss_t2, y_ss_t2, mod2_name, "F["+mod1_name+"]", 1)
def quick_check(): img1_fname = "/home/omar/data/DATA_NeoBrainS12/T1.nii.gz" img2_fname = "/home/omar/data/DATA_NeoBrainS12/set2_i1_t1.nii.gz" img1_nib = nib.load(img1_fname) img1 = img1_nib.get_data().squeeze() img1_affine = img1_nib.get_affine() img2_nib = nib.load(img2_fname) img2 = img2_nib.get_data().squeeze() img2_affine = img2_nib.get_affine() # nib.aff2axcodes(img1_affine) #aff = AffineMap(None, img1.shape, img1_affine, img2.shape, img2_affine) #aff = transform_centers_of_mass(img1, img1_affine, img2, img2_affine) aff = dipy_align(img1, img1_affine, img2, img2_affine, np.eye(4)) img2_resampled = aff.transform(img2) rt.overlay_slices(img1, img2_resampled, slice_type=0) rt.overlay_slices(img1, img2_resampled, slice_type=1) rt.overlay_slices(img1, img2_resampled, slice_type=2) # Verify that original and RAS versions of neo1 describe the same object # Load original data neo1_fname = get_neobrain('train', 1, 'T1') neo1_old, neo1_old_affine, neo1_old_spacing, neo1_old_ori = load_from_raw(neo1_fname) # Load RAS version neo1_nib = nib.load(neo1_fname) neo1 = neo1_nib.get_data() neo1_affine = neo1_nib.get_affine() # Resample RAS on top of original aff = AffineMap(None, neo1_old.shape, neo1_old_affine, neo1.shape, neo1_affine) neo1_resampled = aff.transform(neo1) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=0) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=1) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=2) # Attempt to resample a test volume on top of training neo2_fname = get_neobrain('test', 1, 'i1_t1') neo2_nib = nib.load(neo2_fname) neo2 = neo2_nib.get_data() neo2_affine = neo2_nib.get_affine() aff = transform_centers_of_mass(neo1, neo1_affine, neo2, neo2_affine) #aff = dipy_align(neo1, neo1_affine, neo2, neo2_affine) neo2_resampled = aff.transform(neo2) rt.overlay_slices(neo1, neo2_resampled, slice_type=0) rt.overlay_slices(neo1, neo2_resampled, slice_type=1) rt.overlay_slices(neo1, neo2_resampled, slice_type=2) # Load atlas atlas_fname = get_neobrain('atlas', 'neo-withSkull', None) atlas_nib = nib.load(atlas_fname) atlas_affine = atlas_nib.get_affine() atlas = atlas_nib.get_data() rt.plot_slices(atlas) # Resample atlas on top of neo1 aff = AffineMap(None, neo1.shape, neo1_affine, atlas.shape, atlas_affine) atlas_resampled = aff.transform(atlas) rt.overlay_slices(neo1, atlas_resampled)
ibsr1 = ibsr1_nib.get_data().squeeze() ibsr1_affine = ibsr1_nib.get_affine() # Load brainweb (and match ibsr axes) brainweb_strip_name = info.get_brainweb("t1", "strip") brainweb_strip_nib = nib.load(brainweb_strip_name) brainweb_strip = brainweb_strip_nib.get_data().squeeze() brainweb_strip = brainweb_strip.transpose([0, 2, 1])[::-1, :, :] brainweb_mask = brainweb_strip > 0 brainweb_name = info.get_brainweb("t1", "raw") brainweb_nib = nib.load(brainweb_name) brainweb = brainweb_nib.get_data().squeeze() brainweb_affine = brainweb_nib.get_affine() brainweb = brainweb.transpose([0, 2, 1])[::-1, :, :] rt.plot_slices(brainweb) brainweb_affine = ibsr1_affine.copy() brainweb_affine[brainweb_affine != 0] = 1 brainweb_affine[0, 0] = -1 # Reslice Brainweb on IBSR1 ibsr_to_bw = AffineMap(None, ibsr1.shape, ibsr1_affine, brainweb.shape, brainweb_affine) bw_on_ibsr1 = ibsr_to_bw.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1) # misaligned c_of_mass = transform_centers_of_mass(ibsr1, ibsr1_affine, brainweb, brainweb_affine) bw_on_ibsr1 = c_of_mass.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1) # roughly aligned # Start affine alignment
ibsr1 = ibsr1_nib.get_data().squeeze() ibsr1_affine = ibsr1_nib.get_affine() # Load brainweb (and match ibsr axes) brainweb_strip_name = info.get_brainweb('t1', 'strip') brainweb_strip_nib = nib.load(brainweb_strip_name) brainweb_strip = brainweb_strip_nib.get_data().squeeze() brainweb_strip = brainweb_strip.transpose([0, 2, 1])[::-1, :, :] brainweb_mask = brainweb_strip > 0 brainweb_name = info.get_brainweb('t1', 'raw') brainweb_nib = nib.load(brainweb_name) brainweb = brainweb_nib.get_data().squeeze() brainweb_affine = brainweb_nib.get_affine() brainweb = brainweb.transpose([0, 2, 1])[::-1, :, :] rt.plot_slices(brainweb) brainweb_affine = ibsr1_affine.copy() brainweb_affine[brainweb_affine != 0] = 1 brainweb_affine[0, 0] = -1 # Reslice Brainweb on IBSR1 ibsr_to_bw = AffineMap(None, ibsr1.shape, ibsr1_affine, brainweb.shape, brainweb_affine) bw_on_ibsr1 = ibsr_to_bw.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1) # misaligned c_of_mass = transform_centers_of_mass(ibsr1, ibsr1_affine, brainweb, brainweb_affine) bw_on_ibsr1 = c_of_mass.transform(brainweb) rt.overlay_slices(ibsr1, bw_on_ibsr1) # roughly aligned
w_path_nib = nib.Nifti1Image(w_path, dwi_nib.get_affine()) w_path_nib.to_filename("w_path.nii.gz") P = np.random.permutation(range(1, 33)) for i in range(32): s = w_path.shape[2] // 2 figure(facecolor="white") subplot(1, 2, 1) imshow(w_path[:, :, s, P[i]].T) subplot(1, 2, 2) imshow(dwi[:, :, s, P[i]].T) for i in range(2, 4): rt.plot_slices(dwi[..., i]) def optimal_vs_average_experiment(): # Compute transfer for optimal local-linearity radius = 5 init_from_mean = True nlabels_t1 = 1 + np.max(t1_lab) nlabels_t2 = 1 + np.max(t2_lab) means_t1t2, vars_t1t2 = get_mean_transfer(t1_lab, t2) fmean_t1lab = np.array(means_t1t2) means_t2t1, vars_t2t1 = get_mean_transfer(t2_lab, t1) fmean_t2lab = np.array(means_t2t1)
def quick_check(): img1_fname = "/home/omar/data/DATA_NeoBrainS12/T1.nii.gz" img2_fname = "/home/omar/data/DATA_NeoBrainS12/set2_i1_t1.nii.gz" img1_nib = nib.load(img1_fname) img1 = img1_nib.get_data().squeeze() img1_affine = img1_nib.get_affine() img2_nib = nib.load(img2_fname) img2 = img2_nib.get_data().squeeze() img2_affine = img2_nib.get_affine() # nib.aff2axcodes(img1_affine) #aff = AffineMap(None, img1.shape, img1_affine, img2.shape, img2_affine) #aff = transform_centers_of_mass(img1, img1_affine, img2, img2_affine) aff = dipy_align(img1, img1_affine, img2, img2_affine, np.eye(4)) img2_resampled = aff.transform(img2) rt.overlay_slices(img1, img2_resampled, slice_type=0) rt.overlay_slices(img1, img2_resampled, slice_type=1) rt.overlay_slices(img1, img2_resampled, slice_type=2) # Verify that original and RAS versions of neo1 describe the same object # Load original data neo1_fname = get_neobrain('train', 1, 'T1') neo1_old, neo1_old_affine, neo1_old_spacing, neo1_old_ori = load_from_raw( neo1_fname) # Load RAS version neo1_nib = nib.load(neo1_fname) neo1 = neo1_nib.get_data() neo1_affine = neo1_nib.get_affine() # Resample RAS on top of original aff = AffineMap(None, neo1_old.shape, neo1_old_affine, neo1.shape, neo1_affine) neo1_resampled = aff.transform(neo1) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=0) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=1) rt.overlay_slices(neo1_old, neo1_resampled, slice_type=2) # Attempt to resample a test volume on top of training neo2_fname = get_neobrain('test', 1, 'i1_t1') neo2_nib = nib.load(neo2_fname) neo2 = neo2_nib.get_data() neo2_affine = neo2_nib.get_affine() aff = transform_centers_of_mass(neo1, neo1_affine, neo2, neo2_affine) #aff = dipy_align(neo1, neo1_affine, neo2, neo2_affine) neo2_resampled = aff.transform(neo2) rt.overlay_slices(neo1, neo2_resampled, slice_type=0) rt.overlay_slices(neo1, neo2_resampled, slice_type=1) rt.overlay_slices(neo1, neo2_resampled, slice_type=2) # Load atlas atlas_fname = get_neobrain('atlas', 'neo-withSkull', None) atlas_nib = nib.load(atlas_fname) atlas_affine = atlas_nib.get_affine() atlas = atlas_nib.get_data() rt.plot_slices(atlas) # Resample atlas on top of neo1 aff = AffineMap(None, neo1.shape, neo1_affine, atlas.shape, atlas_affine) atlas_resampled = aff.transform(atlas) rt.overlay_slices(neo1, atlas_resampled)