def transversal_cuts(img, cuts, cmap='gray', normalize=False, axes=None):
    if normalize:
        vmin, vmax = img.min(), img.max()
    else:
        vmin, vmax = 0, 255

    if axes is None:
        ax = plt.subplot(121)
    else:
        ax = axes[0]
    ax.imshow(img, vmin=vmin, vmax=vmax, cmap=cmap)
    for c in cuts:
        if c.orientation == 'v':
            lines = ax.plot([c.idx, c.idx], [0, img.shape[0] - 0.5])
        if c.orientation == 'h':
            lines = ax.plot([0, img.shape[1] - 0.5], [c.idx, c.idx])
        plt.setp(lines, color=c.color)
        plt.setp(lines, linewidth=1)
        ax.axis('off')

    with sns.axes_style('whitegrid'):
        if axes is None:
            ax = plt.subplot(122)
        else:
            ax = axes[1]

        def test(x):
            return (x * 0.5).sum()

        for c in cuts:
            if c.orientation == 'v':
                curve = img[:, c.idx]
            if c.orientation == 'h':
                curve = img[c.idx, :]
            ax.plot(curve, color=c.color, alpha=0.5)
def plot_elbow_kernel(
        values, 
        xlabel=r"$k$", 
        #ylabel=r"$\textnormal{Tr}(Y^\top G \, Y)$", 
        ylabel=r"$\log Q_{k+1} - \log Q_{k}$", 
        output="elbow.pdf",
        colors=['b', 'r', 'g'],
        legend=[r'$\widehat{\rho}_{\sqrt{7}}$', r'$\rho_{1}$'],
        marker=['o', 's']):
    fig = plt.figure(figsize=(fig_width, fig_height))
    ax = fig.add_subplot(111)
    for i, g in enumerate(values):
        xs = range(1, len(g)+1)
        ax.plot(xs, g, color=colors[i], linestyle='-', marker=marker[i],
                linewidth=1.5, markersize=5, label=legend[i], alpha=0.7)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_xlim([1, 12])
    ax.legend(loc=2, framealpha=.5, ncol=1)
    
    with sns.axes_style("whitegrid"):
        axins = inset_axes(ax, width="50%", height="50%", loc=1)
        for i, g in enumerate(values):
            axins.plot(xs[2:10], g[2:10], color=colors[i], linestyle='-', 
                    marker=marker[i], linewidth=1.5, markersize=5, alpha=0.7)
    #axins.set_xlim([5,12])
    #axins.set_ylim([19.9,20])
    #axins.set_xticks([])
    #axins.set_yticks([])
    fig.savefig(output, bbox_inches='tight')
示例#3
0
def parallel_coordinates(dataframe, hue, cols=None, palette=None, **subplot_kws):
    """ Produce a parallel coordinates plot from a dataframe.

    Parameters
    ----------
    dataframe : pandas.DataFrame
        The data to be plotted.
    hue : string
        The column used to the determine assign the lines' colors.
    cols : list of strings, optional
        The non-hue columns to include. If None, all other columns are
        used.
    palette : string, optional
        Name of the seaborn color palette to use.
    **subplot_kws : keyword arguments
        Options passed directly to plt.subplots()

    Returns
    -------
    fig : matplotlib Figure

    """

    # get the columsn to plot
    if cols is None:
        cols = dataframe.select(lambda c: c != hue, axis=1).columns.tolist()

    # subset the data
    final_cols = copy.copy(cols)
    final_cols.append(hue)
    data = dataframe[final_cols]

    # these plots look ridiculous in anything other than 'ticks'
    with seaborn.axes_style('ticks'):
        fig, axes = plt.subplots(ncols=len(cols), **subplot_kws)
        hue_vals = dataframe[hue].unique()
        colors = seaborn.color_palette(name=palette, n_colors=len(hue_vals))
        color_dict = dict(zip(hue_vals, colors))

        for col, ax in zip(cols, axes):
            data_limits =[(0, dataframe[col].min()), (0, dataframe[col].max())]
            ax.set_xticks([0])
            ax.update_datalim(data_limits)
            ax.set_xticklabels([col])
            ax.autoscale(axis='y')
            ax.tick_params(axis='y', direction='inout')
            ax.tick_params(axis='x', direction='in')

        for row in data.values:
            for n, (ax1, ax2) in enumerate(zip(axes[:-1], axes[1:])):
                line = _connect_spines(ax1, ax2, row[n], row[n+1], color=color_dict[row[-1]])


    fig.subplots_adjust(wspace=0)
    seaborn.despine(fig=fig, bottom=True, trim=True)
    return fig
def movie_plot(dir_name, files, box=None):
    with sns.axes_style("white"):
        fig = plt.figure(figsize=(9, 4))
        grid = ImageGrid(fig, rect=(0.05, 0, 0.9, 1),
                         nrows_ncols=(2, len(files)),
                         direction="row",
                         axes_pad=0.1,
                         add_all=True,
                         share_all=True)

        for k, fn in enumerate(files):
            movie = tifffile.imread(dir_name + fn)
            if box is None:
                movie = movie[:, ::2, :]
            else:
                movie = movie[:, box[0]:box[1]:2, box[2]:box[3]]

            print('Movie min:', movie.min(), 'max:', movie.max())

            grid.axes_row[0][k].imshow(movie[0], cmap='viridis')
            grid.axes_row[0][k].set_title('Movie {}'.format(k+1))
            grid.axes_row[0][k].grid(False)
            grid.axes_row[0][k].tick_params(axis='both',
                                       which='both',
                                       bottom='off', top='off',
                                       left='off', right='off',
                                       labelbottom='off', labelleft='off')
            grid.axes_row[0][k].spines['top'].set_visible(False)
            grid.axes_row[0][k].spines['right'].set_visible(False)
            grid.axes_row[0][k].spines['bottom'].set_visible(False)
            grid.axes_row[0][k].spines['left'].set_visible(False)

            grid.axes_row[1][k].imshow(np.mean(movie, axis=0), cmap='viridis')
            grid.axes_row[1][k].grid(False)
            grid.axes_row[1][k].tick_params(axis='both',
                                       which='both',
                                       bottom='off', top='off',
                                       left='off', right='off',
                                       labelbottom='off', labelleft='off')
            grid.axes_row[1][k].spines['top'].set_visible(False)
            grid.axes_row[1][k].spines['right'].set_visible(False)
            grid.axes_row[1][k].spines['bottom'].set_visible(False)
            grid.axes_row[1][k].spines['left'].set_visible(False)

        grid.axes_row[0][0].set_ylabel('Individual\nframe')
        grid.axes_row[1][0].set_ylabel('Temporal\nmean')
        # plt.tight_layout()
        plt.savefig('ground_truth_movie.pdf')
示例#5
0
def plot_elbow_kernel(
        values,
        xlabel=r"$k$",
        #ylabel=r"$\textnormal{Tr}(Y^\top G \, Y)$",
        ylabel=r"$\log Q_{k+1} - \log Q_{k}$",
        output="elbow.pdf",
        colors=['b', 'r', 'g'],
        legend=[r'$\widehat{\rho}_{\sqrt{7}}$', r'$\rho_{1}$'],
        marker=['o', 's']):
    fig = plt.figure(figsize=(fig_width, fig_height))
    ax = fig.add_subplot(111)
    for i, g in enumerate(values):
        xs = range(1, len(g) + 1)
        ax.plot(xs,
                g,
                color=colors[i],
                linestyle='-',
                marker=marker[i],
                linewidth=1.5,
                markersize=5,
                label=legend[i],
                alpha=0.7)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_xlim([1, 12])
    ax.legend(loc=2, framealpha=.5, ncol=1)

    with sns.axes_style("whitegrid"):
        axins = inset_axes(ax, width="50%", height="50%", loc=1)
        for i, g in enumerate(values):
            axins.plot(xs[2:10],
                       g[2:10],
                       color=colors[i],
                       linestyle='-',
                       marker=marker[i],
                       linewidth=1.5,
                       markersize=5,
                       alpha=0.7)
    #axins.set_xlim([5,12])
    #axins.set_ylim([19.9,20])
    #axins.set_xticks([])
    #axins.set_yticks([])
    fig.savefig(output, bbox_inches='tight')
示例#6
0
def corr_heatmap(x, mask_half=True, cmap='RdYlGn_r', vmin=-1, vmax=1,
                 linewidths=0.5, square=True, figsize=(10,10), **kwargs):
    """Wrapper around seaborn.heatmap for visualizing correlation matrix.

    Parameters
    ==========
    x : DataFrame
        Underlying data (not a correlation matrix)
    mask_half : bool, default True
        If True, mask (whiteout) the upper right triangle of the matrix
    All other parameters passed to seaborn.heatmap:
    https://seaborn.pydata.org/generated/seaborn.heatmap.html

    Example
    =======
    %matplotlib inline

    # Generate some correlated data
    k = 10
    size = 400
    mu = np.random.randint(0, 10, k).astype(float)
    r = np.random.ranf(k ** 2).reshape((k, k)) * 5
    df = pd.DataFrame(np.random.multivariate_normal(mu, r, size=size))

    corr_heatmap(df)
    """

    if mask_half:
        mask = np.zeros_like(x.corr().values)
        mask[np.triu_indices_from(mask)] = True
    else:
        mask = None

    with sns.axes_style('white'):
        return sns.heatmap(x.corr(), cmap=cmap, vmin=vmin, vmax=vmax,
                    linewidths=linewidths, square=square, mask=mask, **kwargs)
def ground_truth_patchwise(dir_name, files, gt_means_means, gt_vars_means,
                           box=None, block_size=8, stride=8):
    colors = sns.color_palette('Pastel1', len(files))
    colors_means = sns.color_palette('Set1', len(files))

    with sns.axes_style("white"):
        fig, axes = plt.subplots(1, 1, figsize=(8, 4))
        for k, fn in enumerate(files):
            movie = tifffile.imread(dir_name + fn)
            if box is None:
                movie = movie[:, ::2, :]
            else:
                movie = movie[:, box[0]:box[1]:2, box[2]:box[3]]
            movie = movie.astype(np.float32)

            means = []
            variances = []
            for i in range(0, len(movie), 10):
                blocks_i = im2col(movie[i], block_size, stride)
                means_i, vars_i = compute_mean_var(blocks_i)
                means.append(means_i)
                variances.append(vars_i)

            means = np.hstack(means)
            variances = np.hstack(variances)

            axes.plot(means, variances, '.', alpha=0.7, color=colors[k],
                      markeredgecolor='none', zorder=10 - k, rasterized=True)

            axes.scatter(means.mean(), variances.mean(), marker='x', s=1000,
                         linewidth=2, color=colors_means[k],
                         edgecolor=colors_means[k], zorder=1000)

            axes.scatter(gt_means_means[k], gt_vars_means[k], marker='+',
                         s=1000, linewidth=2, color=colors_means[k],
                         edgecolor=colors_means[k], zorder=1000)

        axes.yaxis.set_major_formatter(
            plt_tick.FuncFormatter(lambda t, pos: fix_scientific_notation(t)))

        axes.set_xlabel('Mean', fontsize='large')
        axes.set_ylabel('Variance', fontsize='large')

        markers1 = [plt_lines.Line2D([], [], marker='o', color=colors[k],
                                     linestyle='None')
                    for k in range(len(files))]
        label1 = ['Movie {} - patch'.format(k+1)
                  for k in range(len(files))]
        markers2 = [plt_lines.Line2D([], [], marker='x', color=colors_means[k],
                                     mec=colors_means[k], linestyle='None',
                                     markersize=10, markeredgewidth=2)
                    for k in range(len(files))]
        label2 = ['Movie {} - patch AVG'.format(k+1)
                  for k in range(len(files))]
        markers3 = [plt_lines.Line2D([], [], marker='+', color=colors_means[k],
                                     mec=colors_means[k], linestyle='None',
                                     markersize=10, markeredgewidth=2)
                    for k in range(len(files))]
        label3 = ['Movie {} - pixel AVG'.format(k+1)
                  for k in range(len(files))]
        plt.legend(markers1 + markers2 + markers3, label1 + label2 + label3,
                   bbox_to_anchor=(1.01, 1), loc='best', fontsize='large')
        fig.tight_layout(rect=(0, 0, 0.7, 1))
        plt.savefig('ground_truth_patchwise.pdf')
def ground_truth_pixelwise(dir_name, files, box=None, plt_regression=True):
    colors = sns.color_palette('Pastel1', len(files))
    colors_means = sns.color_palette('Set1', len(files))

    with sns.axes_style("white"):
        fig, axes_scatter = plt.subplots(1, 1, figsize=(8, 4))
        means_means = []
        variances_means = []
        all_means = []
        all_variances = []
        for k, fn in enumerate(files):
            movie = tifffile.imread(dir_name + fn)
            if box is None:
                movie = movie[:, ::2, :]
            else:
                movie = movie[:, box[0]:box[1]:2, box[2]:box[3]]
            movie = movie.astype(np.float32)

            means, variances = compute_temporal_mean_var(movie)
            means = means.flatten()
            variances = variances.flatten()

            means_means.append(means.mean())
            variances_means.append(variances.mean())

            if plt_regression:
                all_means.extend(means)
                all_variances.extend(variances)

            axes_scatter.plot(means, variances, '.', alpha=0.7,
                              color=colors[k], markeredgecolor='none',
                              label='Movie {}'.format(k + 1), zorder=10 - k,
                              rasterized=True)

        if plt_regression:
            all_means = np.array(all_means)[:, np.newaxis]
            all_variances = np.array(all_variances)

            mod = LinearRegression()
            mod.fit(all_means, all_variances)
            x = np.array([all_means.min(), all_means.max()])[:, np.newaxis]
            axes_scatter.plot(x, mod.predict(x), 'k-', zorder=11)
            print('Linear fit:', mod.coef_[0], mod.intercept_)
            print('R2 score', r2_score(all_variances, mod.predict(all_means)))

        for k, (mean, variance) in enumerate(zip(means_means, variances_means)):
            axes_scatter.scatter(mean, variance, marker='+',
                                 s=1000, linewidth=2, color=colors_means[k],
                                 edgecolor='k', zorder=1000)

        axes_scatter.yaxis.set_major_formatter(
            plt_tick.FuncFormatter(lambda t, pos: fix_scientific_notation(t)))

        axes_scatter.set_xlabel('Mean', fontsize='large')
        axes_scatter.set_ylabel('Variance', fontsize='large')

        markers1 = [plt_lines.Line2D([], [], marker='o', color=colors[k],
                                     linestyle='None')
                    for k in range(len(files))]
        label1 = ['Movie {} - pixel'.format(k+1)
                  for k in range(len(files))]
        markers2 = [plt_lines.Line2D([], [], marker='+', color=colors_means[k],
                                     mec=colors_means[k], linestyle='None',
                                     markersize=10, markeredgewidth=2)
                    for k in range(len(files))]
        label2 = ['Movie {} - pixel AVG'.format(k+1)
                  for k in range(len(files))]
        plt.legend(markers1 + markers2, label1 + label2,
                   bbox_to_anchor=(1.01, 1), loc='best', fontsize='large')
        fig.tight_layout(rect=(0, 0, 0.73, 1))
        plt.savefig('ground_truth_pixelwise.pdf')

    return means_means, variances_means
def ground_truth_estimate_vst(dir_name, files, box=None, block_size=8,
                              stride=8):
    variances_all_single_image = []
    variances_all_multi_image = []

    for k, fn in enumerate(files):
        print(k)

        movie = tifffile.imread(dir_name + fn)
        if box is None:
            movie = movie[:, ::2, :]
        else:
            movie = movie[:, box[0]:box[1]:2, box[2]:box[3]]
        movie = movie.astype(np.float32)

        img_gt = movie.mean(axis=0)
        img_noisy = movie[0]

        # single image estimation
        print('Single-image estimation')
        res = estimate_vst_image(img_noisy, block_size=block_size,
                                 stride=stride)

        movie_gat = compute_gat(movie, res.sigma_sq, res.alpha)
        means, variances = compute_temporal_mean_var(movie_gat)
        print('Temporal variance MEAN={}, STD={}'.format(variances.mean(),
                                                         variances.std(ddof=1)))

        variances = []
        for i in range(len(movie)):
            img_noisy = movie[i]
            v = assess_variance_stabilization(img_gt, img_noisy, res.sigma_sq,
                                              res.alpha, verbose=False,
                                              correct_noiseless=False)
            variances.append(v)
        variances = np.array(variances)
        print('variance AVG', variances.mean())
        variances_all_single_image.append(variances)

        # multi-image estimation
        print('Multi-image estimation')
        res = estimate_vst_movie(movie[::200])

        movie_gat = compute_gat(movie, res.sigma_sq, res.alpha)
        means, variances = compute_temporal_mean_var(movie_gat)
        print('Temporal variance MEAN={}, STD={}'.format(variances.mean(),
                                                         variances.std(ddof=1)))
        variances = []
        for i in range(len(movie)):
            img_noisy = movie[i]
            v = assess_variance_stabilization(img_gt, img_noisy, res.sigma_sq,
                                              res.alpha, verbose=False,
                                              correct_noiseless=False)
            variances.append(v)
        variances = np.array(variances)
        print('variance AVG', variances.mean())
        variances_all_multi_image.append(variances)

    with sns.axes_style('white'):
        plt.figure()
        plt.plot([0, 2 * len(files) - 1], [1, 1], color='k', alpha=0.2,
                 zorder=0)
        pos = np.arange(0, 2 * len(files), 2) + 0.2
        vio_single = plt.violinplot(variances_all_single_image, positions=pos)
        pos = np.arange(1, 2 * len(files), 2) - 0.2
        vio_multi = plt.violinplot(variances_all_multi_image, positions=pos)
        plt.xticks(2 * np.arange(len(files)) + 0.5,
                   ['Movie {}'.format(k+1) for k in range(len(files))],
                   fontsize='large')
        plt.ylabel('Stabilized noise variance per frame', fontsize='large')

        p_single = plt_patches.Patch(
            facecolor=vio_single['bodies'][0].get_facecolor().flatten(),
            edgecolor=vio_single['cbars'].get_edgecolor().flatten(),
            label='Single-image estimation')
        p_multi = plt_patches.Patch(
            facecolor=vio_multi['bodies'][0].get_facecolor().flatten(),
            edgecolor=vio_multi['cbars'].get_edgecolor().flatten(),
            label='Multi-image estimation')
        plt.legend(handles=[p_single, p_multi], loc='upper right',
                   fontsize='large')
        plt.savefig('ground_truth_single-multi.pdf')
示例#10
0
def plot_results(transformation):
    res_dir = '../results'

    _, dir_sigmas, _ = next(os.walk(res_dir))
    dir_sigmas = [ds for ds in dir_sigmas if ds.find(transformation) == 0]
    sigmas = [float(ds[len(transformation) + 1:]) for ds in dir_sigmas]
    idx_sigmas = np.argsort(sigmas)
    sigmas = [sigmas[i] for i in idx_sigmas]
    dir_sigmas = [dir_sigmas[i] for i in idx_sigmas]

    sigma_miss_err = {}
    sigma_times = {'PM': {}, 'NMU': {}, 'TOTAL': {}}
    example_miss_err = {}
    res_files = ['{}/{}/test.txt'.format(res_dir, ds) for ds in dir_sigmas]

    # Very crude parser, do not change console printing output
    # or this will break
    for s, rf in zip(sigmas, res_files):
        with open(rf, 'r') as file_contents:
            sigma_miss_err[s] = []
            sigma_times['PM'][s] = []
            sigma_times['NMU'][s] = []
            sigma_times['TOTAL'][s] = []
            for i, line in enumerate(file_contents):
                if line.find('Statistics') == 0:
                    break
                if i % 10 == 0:
                    example = line[:-5]
                if i % 10 == 3:
                    t = float(line.split()[4])
                    sigma_times['PM'][s].append(t)
                if i % 10 == 4:
                    t = float(line.split()[2])
                    sigma_times['NMU'][s].append(t)
                if i % 10 == 7:
                    t = float(line.split()[2])
                    sigma_times['TOTAL'][s].append(t)
                if i % 10 == 8:
                    pr = 100 * float(line.split()[3][:-1])
                    if example not in example_miss_err:
                        example_miss_err[example] = []
                    example_miss_err[example].append(pr)
                    sigma_miss_err[s].append(pr)

    def sort_dict(d):
        return collections.OrderedDict(sorted(d.items()))

    example_miss_err = sort_dict(example_miss_err)
    sigma_miss_err = sort_dict(sigma_miss_err)
    sigma_times['PM'] = sort_dict(sigma_times['PM'])
    sigma_times['NMU'] = sort_dict(sigma_times['NMU'])
    sigma_times['TOTAL'] = sort_dict(sigma_times['TOTAL'])

    def round2(vals, decimals=2):
        return np.round(vals, decimals=decimals)

    print('Misclassification error')
    for key in sigma_miss_err:
        values = np.array(sigma_miss_err[key])
        stats = (key, round2(np.mean(values)), round2(np.median(values)),
                 round2(np.std(values, ddof=1)))
        fmt_str = 'sigma: {}\tmean: {}\tmedian: {}\tstd: {}'
        print(fmt_str.format(*stats))
        # print('\t', values)

    with sns.axes_style("whitegrid"):
        values = np.array(list(sigma_miss_err.values())).T
        max_val = values.max()

        plt.figure()
        sns.boxplot(data=values, color='.95', whis=100)
        sns.stripplot(data=values, jitter=True)
        sigmas_text = ['{:.2f}'.format(s) for s in sigmas]
        plt.xticks(range(len(sigmas)), sigmas_text, size='x-large')
        yticks = [yt for yt in plt.yticks()[0] if yt >= 0]
        plt.yticks(yticks, size='x-large')
        plt.xlabel(r'$\sigma$', size='x-large')
        plt.ylabel('Misclassification error (%)', size='x-large')
        plt.ylim((-2, 10 * np.ceil(max_val / 10)))
        if transformation == 'homography':
            plt.title('Homographies', size='x-large')
        if transformation == 'fundamental':
            plt.title('Fundamental matrices', size='x-large')
        plt.tight_layout()
        plt.savefig('{}/{}_result.pdf'.format(res_dir, transformation),
                    bbox_inches='tight')

    print('Time')
    for key in sigma_miss_err:
        mean_PM = round2(np.mean(np.array(sigma_times['PM'][key])))
        mean_NMU = round2(np.mean((np.array(sigma_times['NMU'][key]))))
        mean_total = round2(np.mean((np.array(sigma_times['TOTAL'][key]))))
        stats = (key, mean_total, round2(mean_PM / mean_total),
                 round2(mean_NMU / mean_total))
        fmt_str = 'sigma: {}\tTOTAL: {}\tRATIO PM: {}\tRATIO NMU: {}'
        print(fmt_str.format(*stats))
示例#11
0
    Image.open('../digits/digit8.png'),
    Image.open('../digits/digit9.png')
]
imgs = [np.array(im.convert('L'), dtype=np.float) / 255. for im in imgs]
img_size = imgs[0].shape

mat = 1 - np.stack([im.flatten() for im in imgs], axis=1)

t = timeit.default_timer()
factors = nmu.recursive_nmu(mat, r=10, init='svd')
t = timeit.default_timer() - t
print('time {:.2f}'.format(t))

recs = sum(u.dot(v) for u, v in factors)

with sns.axes_style("whitegrid"):
    plt.figure()
    for i, im in enumerate(imgs):
        plt.subplot(1, len(imgs), i + 1)
        plt.imshow(im, interpolation='nearest', cmap='gray')
        plt.tick_params(axis='both',
                        which='both',
                        bottom='off',
                        top='off',
                        labelbottom='off',
                        left='off',
                        right='off',
                        labelleft='off')
        plt.grid(b=False)
    plt.tight_layout()
    plt.savefig(dir_name + 'digits_original.pdf', dpi=150, bbox_inches='tight')
示例#12
0
文件: test_3d.py 项目: iamwx/arse
def plot_times(log_filenames, output_filename, relative=False, col_width=0.35):
    pattern_size = 'Preference matrix size: \((?P<m>.+), (?P<n>.+)\)'
    pref_sizes = []
    comp_time = []
    reg_time = []
    for fn in log_filenames:
        with open(fn, 'r') as f:
            s = str(f.read())
            m = re.search(pattern_size, s).groupdict()
            pref_sizes.append((int(m['m']), int(m['n'])))

            def retrieve_time(pat_type):
                pattern_time = 'Running {0} bi-clustering\nTime: (?P<time>.+)'
                m = re.search(pattern_time.format(pat_type), s)
                if m is None:
                    return np.nan
                else:
                    return float(m.groupdict()['time'])

            comp_time.append(retrieve_time('compressed'))
            reg_time.append(retrieve_time('regular'))

    idx = np.arange(len(log_filenames))
    rse_time = np.array(reg_time)
    arse_time = np.array(comp_time)
    if relative:
        ps = np.array([np.prod(s) for s in pref_sizes])
        rse_time = rse_time / ps
        arse_time = arse_time / ps

    def autolabel(rects):
        for rect in rects:
            height = rect.get_height()
            if np.isnan(height):
                continue
            str = '{:.2e}'.format(height)
            str = str.split('e')
            print(str)
            str = str[0] + 'e' + str[1][2:]
            plt.text(rect.get_x() + rect.get_width() / 2., 1.05 * height,
                     str, ha='center', va='bottom', fontsize='16')

    colors = sns.color_palette('Set1', n_colors=2)

    with sns.axes_style('whitegrid'):
        plt.figure()
        plt.yscale('log')

        bars1 = plt.bar(idx, rse_time, col_width, linewidth=0, color=colors[0])
        bars1.set_label('RSE')
        bars2 = plt.bar(idx + col_width, arse_time, col_width, linewidth=0,
                       color=colors[1])
        bars2.set_label('ARSE')
        if not relative:
            autolabel(bars1)
            autolabel(bars2)

        plt.xticks(idx + col_width, pref_sizes, horizontalalignment='center',
                   fontsize='16')
        _, labels = plt.yticks()
        for l in labels:
            l.set_fontsize(16)

        plt.xlabel('Preference matrix size', fontsize='16')
        if relative:
            plt.ylabel('Time / size', fontsize='16')
            loc = 'upper right'
        else:
            plt.ylabel('Time (s)', fontsize='16')
            loc = 'upper left'

        plt.legend(ncol=2, fontsize='16', loc=loc)

        plt.xlim(-col_width, idx.size + col_width / 2)
        plt.tight_layout()

        if relative:
            output_filename += '_relative'
        output_filename += '.pdf'
        plt.savefig(output_filename, dpi=600)
示例#13
0
def plot_vst_estimation(movie,
                        blocks,
                        sigma_sq_init,
                        alpha_init,
                        res,
                        idx,
                        gt=None,
                        filename=None):
    img = movie[idx]
    if gt is not None:
        img_gt = gt.movie[idx]

    means, variances = est.compute_mean_var(blocks)

    if gt is not None:
        movie_gat = compute_gat(movie, sigma_sq_init, alpha=alpha_init)
        _, temp_vars = compute_temporal_mean_var(movie_gat)
        print(
            '---> Temporal variance',
            'MEAN={}, STD={}'.format(temp_vars.mean(), temp_vars.std(ddof=1)))

        compare_variance_stabilization(img_gt, img, gt.sigma_sq, gt.alpha,
                                       sigma_sq_init, alpha_init)

        gt_movie_gat = compute_gat(movie, res.sigma_sq, alpha=res.alpha)
        _, temp_vars = compute_temporal_mean_var(gt_movie_gat)
        print(
            '---> Temporal variance',
            'MEAN={}, STD={}'.format(temp_vars.mean(), temp_vars.std(ddof=1)))

        compare_variance_stabilization(img_gt, img, gt.sigma_sq, gt.alpha,
                                       res.sigma_sq, res.alpha)

    line_cmap = ['#377eb8', '#e41a1c']

    with sns.axes_style('white'):
        if gt is None:
            plt.figure(figsize=(24, 5))
            gs = gridspec.GridSpec(1,
                                   5,
                                   width_ratios=[1, 3, 3, 3, 3],
                                   left=0.02,
                                   right=0.98,
                                   wspace=0.3)

            axes0 = plt.subplot(gs[0, 0])
            axes1 = plt.subplot(gs[0, 1])
            axes2 = plt.subplot(gs[0, 2])
            axes3 = plt.subplot(gs[0, 3])
            axes4 = plt.subplot(gs[0, 4])

            axes0.imshow(img, cmap='viridis')
            axes0.axis('off')
            axes0.set_title('Input image', fontsize='xx-large')
        else:
            plt.figure(figsize=(24, 5))
            gs = gridspec.GridSpec(2,
                                   5,
                                   width_ratios=[2, 2, 3, 3, 3],
                                   left=0.02,
                                   right=0.98,
                                   wspace=0.3)

            axes00 = plt.subplot(gs[0, 0])
            axes10 = plt.subplot(gs[1, 0])
            axes1 = plt.subplot(gs[:, 1])
            axes2 = plt.subplot(gs[:, 2])
            axes3 = plt.subplot(gs[:, 3])
            axes4 = plt.subplot(gs[:, 4])

            axes00.imshow(img_gt, cmap='viridis')
            axes00.axis('off')
            axes00.set_title('Noiseless image', fontsize='xx-large')
            axes10.imshow(img, cmap='viridis')
            axes10.axis('off')
            axes10.set_title('Noisy image', fontsize='xx-large')

        scatter_color = '#a6cee3'

        # Using rasterized=True drastically reduces the file size
        axes1.plot(means,
                   variances,
                   '.',
                   alpha=0.2,
                   color=scatter_color,
                   markeredgecolor='none',
                   rasterized=True,
                   label='Patch')

        x = np.array([[means.min()], [means.max()]])
        axes1.plot(x,
                   alpha_init * x + sigma_sq_init,
                   color=line_cmap[0],
                   label='Initial estimation')

        axes1.plot(x,
                   res.alpha * x + res.sigma_sq,
                   color=line_cmap[1],
                   label='Refined estimation')

        if gt is not None:
            axes1.plot(x,
                       gt.alpha * x + gt.sigma_sq,
                       color='k',
                       label='Ground truth')

        set_tick_label_size(axes1)

        axes1.set_xlabel('Mean', fontsize='xx-large')
        axes1.set_ylabel('Variance', fontsize='xx-large')

        # lgnd = axes1.legend(markerscale=5, fontsize='xx-large')
        # for h in lgnd.legendHandles:
        #     h._sizes = [600]

        xdiff = np.percentile(means, 99) - means.min()
        ydiff = np.percentile(variances, 99) - variances.min()
        axes1.set_xlim((means.min() - 0.1 * xdiff,
                        np.percentile(means, 99) + 0.1 * xdiff))
        axes1.set_ylim((variances.min() - 0.1 * ydiff,
                        np.percentile(variances, 99) + 0.1 * ydiff))

        axes1.set_title('Patch mean vs patch variance', fontsize='xx-large')

        # Accumulator space
        plot_vst_accumulator_space(res.acc_space_init,
                                   ax=axes2,
                                   plot_focus=True)
        set_tick_label_size(axes2)
        axes2.set_title('Coarse accumulator space', fontsize='xx-large')

        # Zoom-in on the accumulator space
        plot_vst_accumulator_space(res.acc_space,
                                   ax=axes3,
                                   plot_estimates=True)
        set_tick_label_size(axes3)
        axes3.set_title('Focused accumulator space', fontsize='xx-large')

        # Density plot of block variances
        blocks_gat = compute_gat(blocks, sigma_sq_init, alpha=alpha_init)
        _, variances_init = est.compute_mean_var(blocks_gat)

        blocks_gat = compute_gat(blocks, res.sigma_sq, alpha=res.alpha)
        _, variances = est.compute_mean_var(blocks_gat)

        data_range = (np.minimum(variances_init.min(), variances.min()),
                      np.maximum(variances_init.max(), variances.max()))
        samples = np.linspace(*data_range, num=1000)

        kde = KernelDensity(kernel='gaussian', bandwidth=0.05)
        probas_both = []
        for vs, color, label in zip([variances_init, variances], line_cmap,
                                    ['Initial', 'Refined']):
            kde.fit(vs[:, np.newaxis])
            probas = np.exp(kde.score_samples(samples[:, np.newaxis]))
            probas /= probas.sum()
            probas_both.append(probas)
            axes4.fill_between(samples,
                               probas,
                               color=color,
                               alpha=0.3,
                               label=label + ' estimation')
            loc = np.argmax(probas)
            axes4.plot([samples[loc], samples[loc]], [0, probas[loc]],
                       '-',
                       color=color)
            axes4.plot([1, 1], [0, probas[loc] * 1.05], 'k:')

        probas_both = np.maximum(*probas_both)
        idx_nnz = np.where(probas_both > 1e-4)[0]
        axes4.set_xlim(samples[0], samples[idx_nnz[-1]])

        set_tick_label_size(axes4)

        axes4.legend(fontsize='xx-large', loc='upper right')
        axes4.set_xlabel('Patch variance', fontsize='xx-large')
        axes4.set_title('Patch variance density', fontsize='xx-large')

        if filename is not None:
            if 'cai-1' in filename:
                filename = filename.replace('/raw_data/', '_')
                filename = filename.replace('/', '_')
            plt.savefig(filename + '_estimation.pdf')