Esempio n. 1
0
 def test_invalid(self, x):
     """
     Test that invalid value are formatted with empty string without
     raising exception.
     """
     formatter = mticker.LogitFormatter(use_overline=False)
     formatter.set_locs(self.decade_test)
     s = formatter(x)
     assert s == ""
Esempio n. 2
0
 def test_format_data_short(self, N):
     locs = np.linspace(0, 1, N)[1:-1]
     form = mticker.LogitFormatter()
     for x in locs:
         fx = form.format_data_short(x)
         if fx.startswith("1-"):
             x2 = 1 - float(fx[2:])
         else:
             x2 = float(fx)
         assert abs(x - x2) < 1 / N
Esempio n. 3
0
 def test_basic(self, x):
     """
     Test the formatted value correspond to the value for ideal ticks in
     logit space.
     """
     formatter = mticker.LogitFormatter(use_overline=False)
     formatter.set_locs(self.decade_test)
     s = formatter(x)
     x2 = TestLogitFormatter.logit_deformatter(s)
     assert _LogitHelper.isclose(x, x2)
Esempio n. 4
0
 def test_one_half(self):
     """
     Test the parameter one_half
     """
     form = mticker.LogitFormatter()
     assert r"\frac{1}{2}" in form(1/2)
     form.set_one_half("1/2")
     assert "1/2" in form(1/2)
     form.set_one_half("one half")
     assert "one half" in form(1/2)
Esempio n. 5
0
 def test_minor_number(self):
     """
     Test the parameter minor_number
     """
     min_loc = mticker.LogitLocator(minor=True)
     min_form = mticker.LogitFormatter(minor=True)
     ticks = min_loc.tick_values(5e-2, 1 - 5e-2)
     for minor_number in (2, 4, 8, 16):
         min_form.set_minor_number(minor_number)
         formatted = min_form.format_ticks(ticks)
         labelled = [f for f in formatted if len(f) > 0]
         assert len(labelled) == minor_number
Esempio n. 6
0
 def test_use_overline(self):
     """
     Test the parameter use_overline
     """
     x = 1 - 1e-2
     fx1 = r"$\mathdefault{1-10^{-2}}$"
     fx2 = r"$\mathdefault{\overline{10^{-2}}}$"
     form = mticker.LogitFormatter(use_overline=False)
     assert form(x) == fx1
     form.use_overline(True)
     assert form(x) == fx2
     form.use_overline(False)
     assert form(x) == fx1
Esempio n. 7
0
 def test_variablelength(self, x):
     """
     The format length should change depending on the neighbor labels.
     """
     formatter = mticker.LogitFormatter(use_overline=False)
     for N in (10, 20, 50, 100, 200, 1000, 2000, 5000, 10000):
         if x + 1 / N < 1:
             formatter.set_locs([x - 1 / N, x, x + 1 / N])
             sx = formatter(x)
             sx1 = formatter(x + 1 / N)
             d = (TestLogitFormatter.logit_deformatter(sx1) -
                  TestLogitFormatter.logit_deformatter(sx))
             assert 0 < d < 2 / N
Esempio n. 8
0
    def test_minor_vs_major(self, method, lims, cases):
        """
        Test minor/major displays.
        """

        if method:
            min_loc = mticker.LogitLocator(minor=True)
            ticks = min_loc.tick_values(*lims)
        else:
            ticks = np.array(lims)
        min_form = mticker.LogitFormatter(minor=True)
        for threshold, has_minor in cases:
            min_form.set_minor_threshold(threshold)
            formatted = min_form.format_ticks(ticks)
            labelled = [f for f in formatted if len(f) > 0]
            if has_minor:
                assert len(labelled) > 0, (threshold, has_minor)
            else:
                assert len(labelled) == 0, (threshold, has_minor)
Esempio n. 9
0
def create_plot(all_data, raw, x_scale, y_scale, xn, yn, fn_out, linestyles,
                batch):
    xm, ym = (metrics[xn], metrics[yn])
    # Now generate each plot
    handles = []
    labels = []
    plt.figure(figsize=(12, 9))

    # Sorting by mean y-value helps aligning plots with labels
    def mean_y(algo):
        xs, ys, ls, axs, ays, als = create_pointset(all_data[algo], xn, yn)
        return -np.log(np.array(ys)).mean()

    # Find range for logit x-scale
    min_x, max_x = 1, 0
    for algo in sorted(all_data.keys(), key=mean_y):
        xs, ys, ls, axs, ays, als = create_pointset(all_data[algo], xn, yn)
        min_x = min([min_x] + [x for x in xs if x > 0])
        max_x = max([max_x] + [x for x in xs if x < 1])
        color, faded, linestyle, marker = linestyles[algo]
        handle, = plt.plot(xs,
                           ys,
                           '-',
                           label=algo,
                           color=color,
                           ms=7,
                           mew=3,
                           lw=3,
                           linestyle=linestyle,
                           marker=marker)
        handles.append(handle)
        if raw:
            handle2, = plt.plot(axs,
                                ays,
                                '-',
                                label=algo,
                                color=faded,
                                ms=5,
                                mew=2,
                                lw=2,
                                linestyle=linestyle,
                                marker=marker)
        labels.append(algo)

    ax = plt.gca()
    ax.set_ylabel(ym['description'])
    ax.set_xlabel(xm['description'])
    # Custom scales of the type --x-scale a3
    if x_scale[0] == 'a':
        alpha = int(x_scale[1:])
        fun = lambda x: 1 - (1 - x)**(1 / alpha)
        inv_fun = lambda x: 1 - (1 - x)**alpha
        ax.set_xscale('function', functions=(fun, inv_fun))
        if alpha <= 3:
            ticks = [inv_fun(x) for x in np.arange(0, 1.2, .2)]
            plt.xticks(ticks)
        if alpha > 3:
            from matplotlib import ticker
            ax.xaxis.set_major_formatter(ticker.LogitFormatter())
            #plt.xticks(ticker.LogitLocator().tick_values(min_x, max_x))
            plt.xticks([0, 1 / 2, 1 - 1e-1, 1 - 1e-2, 1 - 1e-3, 1 - 1e-4, 1])
    # Other x-scales
    else:
        ax.set_xscale(x_scale)
    ax.set_yscale(y_scale)
    ax.set_title(get_plot_label(xm, ym))
    box = plt.gca().get_position()
    # plt.gca().set_position([box.x0, box.y0, box.width * 0.8, box.height])
    ax.legend(handles,
              labels,
              loc='center left',
              bbox_to_anchor=(1, 0.5),
              prop={'size': 9})
    plt.grid(b=True, which='major', color='0.65', linestyle='-')
    plt.setp(ax.get_xminorticklabels(), visible=True)

    # Logit scale has to be a subset of (0,1)
    if 'lim' in xm and x_scale != 'logit':
        x0, x1 = xm['lim']
        plt.xlim(max(x0, 0), min(x1, 1))
    elif x_scale == 'logit':
        plt.xlim(min_x, max_x)
    if 'lim' in ym:
        plt.ylim(ym['lim'])

    # Workaround for bug https://github.com/matplotlib/matplotlib/issues/6789
    ax.spines['bottom']._adjust_location()

    plt.savefig(fn_out, bbox_inches='tight')
    plt.close()
    df = pd.read_csv(os.path.join(args.folder, 'correct_samples.csv'))

    data = [nopass[df['is_correct_1']].numpy(), nopass[~df['is_correct_1']].numpy(),
            nonaliased[df['is_correct_1']].numpy(), nonaliased[~df['is_correct_1']].numpy(),
            aliased[df['is_correct_1']].numpy(), aliased[~df['is_correct_1']].numpy(),
            tangled[df['is_correct_1']].numpy(), tangled[~df['is_correct_1']].numpy()]
    fig, ax = plt.subplots(figsize=(8, 3.3))
    a1, a2 = 0.5, 1.0
    colors = [[51 / 255, 51 / 255, 51 / 255, a1], [51 / 255, 51 / 255, 51 / 255, a2],
              [42/255, 42/255, 212/255, a1], [42/255, 42/255, 212/255, a2],
              [212/255, 212/255, 42 / 255, a1], [212/255, 212/255, 42 / 255, a2],
              [ 1, 0, 0, a1], [ 1, 0, 0, a2]]

    flierprops = dict(marker='.', markersize=5,)
    ax.set_yscale('logit')
    ax.yaxis.set_major_formatter(tk.LogitFormatter(one_half='0.5'))
    bplot = ax.boxplot(data, positions=[1, 1.6, 2.4, 3.0, 3.8, 4.4, 5.2, 5.8], patch_artist=True, flierprops=flierprops)
    for patch, whiskers, fliers, medians, color in zip(bplot['boxes'], bplot['whiskers'], bplot['fliers'], bplot['medians'],  colors):
        patch.set_facecolor(color)
        whiskers.set_color('black')
        fliers.set_markeredgecolor(color)
        medians.set_color('black')
    axtop = ax.secondary_xaxis('top')
    axtop.set_xticks([1.3, 2.7, 4.1, 5.5])  # only works with matplotlib==3.2.1 !
    axtop.set_xticklabels(['no pass', 'non-aliased', 'aliased', 'aliased-tangled'])  # only works with matplotlib==3.2.1 !
    ax.set_xticklabels(['$\checkmark $', '{\sffamily x}']*4)
    ax.set_yticks([0.1, 0.5])
    ax.set_yticklabels([0.1, 0.5])
    ax.set_ylabel('fraction')
    #plt.grid(which='both')
    if args.save:
 for i, n_intens in enumerate(noise_intensities):
     for j, tp in enumerate(tps):
         m = markers[j]
         ls = linestyles[j]
         filtered_results = results[(results['noise_intens'] == n_intens)
                                    & (results['type'] == tp)]
         n_params = filtered_results['n_params']
         acc = filtered_results['acc.']
         line, = ax[i // cols, i % cols].plot(n_params,
                                              acc / 100,
                                              marker=m,
                                              linestyle=ls,
                                              markersize=6)
         ax[i // cols, i % cols].set_yscale('logit')
         ax[i // cols, i % cols].yaxis.set_major_formatter(
             tk.LogitFormatter(one_half='0.5'))
         ax[i // cols, i % cols].set_xscale('log')
         ax[i // cols, i % cols].set_yticks([0.01, 0.1, 0.5, 0.9, 0.99])
         ax[i // cols,
            i % cols].set_yticklabels([0.01, 0.1, 0.5, 0.9, 0.99])
         ax[i // cols, i % cols].set_ylim([0.001, 0.997])
         ax[i // cols, i % cols].set_title(r'$A$ = {}'.format(n_intens))
         ax[i // cols, i % cols].grid(True, which='both')
         # x label
         if i // cols == 1:
             ax[i // cols, i % cols].set_xlabel('\# params')
         else:
             ax[i // cols, i % cols].set_xticklabels([''] * 2)
         # y label
         if i % cols == 0:
             ax[i // cols, i % cols].set_ylabel('accuracy')