def plot_average_and_optimized_transfers():
    fig = plt.figure(facecolor="white")
    ax = fig.add_subplot(1, 2, 1)
    compare_transfers(ax, fmean_t1lab, opt_list_t1lab[4], "Iso-set mean", "Optimized", 1, "T1", "F[T1]")
    ax = fig.add_subplot(1, 2, 2)
    compare_transfers(ax, fmean_t2lab, opt_list_t2lab[4], "Iso-set mean", "Optimized", 1, "T2", "F[T2]")

    # Apply the transfer
    t2_lab = t2.astype(np.int32)
    means_t2t1, vars_t2t1 = get_mean_transfer(t2_lab, t1)
    ss_t1 = means_t2t1[t2_lab]

    t1_lab = t1.astype(np.int32)
    means_t1t2, vars_t1t2 = get_mean_transfer(t1_lab, t2)
    ss_t2 = means_t1t2[t1_lab]

    # Compute residuals of locally affine fit on T1 and T2
    radius = 4
    residuals_nb = ccr.compute_cc_residuals(t1, t2, radius, 1)
    residuals_nb = np.array(residuals_nb)

    # Compute residuals of locally affine fit on F(T1) and T2
    radius = 4
    residuals_t2 = ccr.compute_cc_residuals(ss_t2, t2, radius, 1)
    residuals_t2 = np.array(residuals_t2)

    # Compute residuals of locally affine fit on T1 and F[T2]
    radius = 4
    residuals_t1 = ccr.compute_cc_residuals(t1, ss_t1, radius, 1)
    residuals_t1 = np.array(residuals_t1)
def demo_near_dwi(idx=-1):
    # Plot local linear reconstruction error from the pair of images whose corresponding
    # diffusion encoding vectors' dot product rank `idx` from lowest to highest, e.g.
    # demo_near_dwi(0) shows the error for the "least coherent" pair, while
    # demo_near_dwi(-1) shows the error for the "most coherent" pair
    # Load data
    dwi_fname = "usmcuw_dipy.nii.gz"
    dwi_nib = nib.load(dwi_fname)
    dwi = dwi_nib.get_data().squeeze()
    B_name = "B_dipy.txt"
    B = np.loadtxt(B_name)
    n = B.shape[0]

    pp = []
    for i in range(1, n):
        for j in range(i + 1, n):
            p = np.abs(B[i, :3].dot(B[j, :3]))
            pp.append((p, (i, j)))
    pp.sort()
    sel = pp[idx][1]

    t1 = dwi[..., sel[0]]
    t1_lab = t1.astype(np.int32)
    t1 = t1.astype(np.float64)
    t1 = (t1.astype(np.float64) - t1.min()) / (t1.max() - t1.min())

    t2 = dwi[..., sel[1]]
    t2_lab = t2.astype(np.int32)
    t2 = t2.astype(np.float64)
    t2 = (t2.astype(np.float64) - t2.min()) / (t2.max() - t2.min())

    # Prepare interactive graphs
    global_figure = None
    global_map = None
    sel_x = None
    sel_y = None

    # Compute residuals of locally affine fit on T1 and T2
    radius = 4
    residuals_nb = ccr.compute_cc_residuals(t1, t2, radius, 1)
    residuals_nb = np.array(residuals_nb)

    # Apply the transfer
    means_t2t1, vars_t2t1 = get_mean_transfer(t2_lab, t1)
    ss_t1 = means_t2t1[t2_lab]

    means_t1t2, vars_t1t2 = get_mean_transfer(t1_lab, t2)
    ss_t2 = means_t1t2[t1_lab]

    # Compute residuals of locally affine fit on T1 and F[T2]
    radius = 4
    residuals_t1 = ccr.compute_cc_residuals(t1, ss_t1, radius, 1)
    residuals_t1 = np.array(residuals_t1)

    # Compute residuals of locally affine fit on F(T1) and T2
    radius = 4
    residuals_t2 = ccr.compute_cc_residuals(ss_t2, t2, radius, 1)
    residuals_t2 = np.array(residuals_t2)

    slice_type = 2
    slice_index = residuals_nb.shape[slice_type] // 2
    if slice_type == 0:
        max_val = np.max(
            [
                residuals_nb[slice_index, :, :].max(),
                residuals_t1[slice_index, :, :].max(),
                residuals_t2[slice_index, :, :].max(),
            ]
        )
    elif slice_type == 1:
        max_val = np.max(
            [
                residuals_nb[:, slice_index, :].max(),
                residuals_t1[:, slice_index, :].max(),
                residuals_t2[:, slice_index, :].max(),
            ]
        )
    else:
        max_val = np.max(
            [
                residuals_nb[:, :, slice_index].max(),
                residuals_t1[:, :, slice_index].max(),
                residuals_t2[:, :, slice_index].max(),
            ]
        )

    run_interactive_pair(t1, "T1", t2, "T2", residuals_nb, slice_type, slice_index, vmin=0, vmax=max_val)
def demo_t1_t2():
    # Load data
    t1_name = t1_name = get_brainweb("t1", "strip")
    t1_nib = nib.load(t1_name)
    t1 = t1_nib.get_data().squeeze()
    t1_lab = t1.astype(np.int32)
    t1 = t1.astype(np.float64)
    # t1 = (t1.astype(np.float64) - t1.min())/(t1.max()-t1.min())

    t2_name = t2_name = get_brainweb("t2", "strip")
    t2_nib = nib.load(t2_name)
    t2 = t2_nib.get_data().squeeze()
    t2_lab = t2.astype(np.int32)
    t2 = t2.astype(np.float64)
    # t2 = (t2.astype(np.float64) - t2.min())/(t2.max()-t2.min())

    # Prepare interactive graphs
    global_figure = None
    global_map = None
    sel_x = None
    sel_y = None

    # Compute residuals of locally affine fit on T1 and T2
    radius = 4
    residuals_nb = ccr.compute_cc_residuals(t1, t2, radius, 1)
    residuals_nb = np.array(residuals_nb)

    # Apply the transfer
    means_t2t1, vars_t2t1 = get_mean_transfer(t2_lab, t1)
    ss_t1 = means_t2t1[t2_lab]

    means_t1t2, vars_t1t2 = get_mean_transfer(t1_lab, t2)
    ss_t2 = means_t1t2[t1_lab]

    # Compute residuals of locally affine fit on T1 and F[T2]
    radius = 4
    residuals_t1 = ccr.compute_cc_residuals(t1, ss_t1, radius, 1)
    residuals_t1 = np.array(residuals_t1)

    # Compute residuals of locally affine fit on F(T1) and T2
    radius = 4
    residuals_t2 = ccr.compute_cc_residuals(ss_t2, t2, radius, 1)
    residuals_t2 = np.array(residuals_t2)

    slice_type = 1
    slice_index = residuals_nb.shape[slice_type] // 2
    if slice_type == 0:
        max_val = np.max(
            [
                residuals_nb[slice_index, :, :].max(),
                residuals_t1[slice_index, :, :].max(),
                residuals_t2[slice_index, :, :].max(),
            ]
        )
    elif slice_type == 1:
        max_val = np.max(
            [
                residuals_nb[:, slice_index, :].max(),
                residuals_t1[:, slice_index, :].max(),
                residuals_t2[:, slice_index, :].max(),
            ]
        )
    else:
        max_val = np.max(
            [
                residuals_nb[:, :, slice_index].max(),
                residuals_t1[:, :, slice_index].max(),
                residuals_t2[:, :, slice_index].max(),
            ]
        )

    run_interactive_pair(t1, "T1", t2, "T2", residuals_nb, slice_type, slice_index, vmin=0, vmax=max_val)
    run_interactive_pair(t1, "T1", ss_t1, "F[T2]", residuals_t1, slice_type, slice_index, vmin=0, vmax=max_val)
    run_interactive_pair(t2, "T2", ss_t2, "F[T1]", residuals_t2, slice_type, slice_index, vmin=0, vmax=max_val)