示例#1
0
def report_files_changed(
        results: Collection[RepoVersionDiffs], ax: plt.Axes, outliers: bool
) -> None:
    ax.set_title('#files changed between versions')
    ax.set_xlabel('#files changed')
    ax.set_ylabel('Type of bump')
    files_per_bump_type = get_diffs_attr(results, 'num_files_changed')
    ax.boxplot(  # type: ignore[attr-defined]
            files_per_bump_type.values(),
            labels=[lbl.value for lbl in files_per_bump_type.keys()],
            vert=False, showfliers=outliers, widths=0.5)
    ax.invert_yaxis()  # type: ignore[attr-defined]
示例#2
0
def report_lines_changed(results: Collection[RepoVersionDiffs], ax: plt.Axes,
                         outliers: bool) -> None:
    ax.set_title('#insertions and #deletions between versions')
    ax.set_xlabel('#lines')
    ax.set_ylabel('Type of bump')
    insertions_per_bump_type = get_diffs_attr(results, 'insertions')
    deletions_per_bump_type = get_diffs_attr(results, 'deletions')
    ticks: List[float] = []
    for (i, label) in enumerate(Label):
        ins_dels = [
            insertions_per_bump_type[label], deletions_per_bump_type[label]
        ]
        idx = i * 3
        ticks.append(idx + .5)
        bp = ax.boxplot(  # type: ignore[attr-defined]
            ins_dels,
            positions=[idx, idx + 1],
            vert=False,
            showfliers=outliers,
            widths=0.5,
            patch_artist=True)
        for box, color in zip(bp['boxes'], ('green', 'red')):
            box.set_facecolor(color)
    ax.set_yticks(ticks)
    ax.set_yticklabels([label.value for label in Label])

    # Dummy plots for legend
    dummy_insertions, = ax.plot([1, 1], 'g-',
                                label='Insertions')  # type: ignore
    dummy_deletions, = ax.plot([1, 1], 'r-', label='Deletions')  # type: ignore
    ax.legend()
    dummy_insertions.set_visible(False)
    dummy_deletions.set_visible(False)

    ax.invert_yaxis()  # type: ignore[attr-defined]
示例#3
0
def _plot_single_image_stats(image: np.ndarray, mask: np.ndarray, z_slice: int,
                             image_axes: Axes, hist_axes: Axes,
                             box_axes: Axes) -> None:
    data = image.flatten() if mask is None else image[mask > 0].flatten()
    box_axes.boxplot(data, notch=False, vert=False, sym=".", whis=[5, 95])
    hist_axes.hist(data, bins=30)
    image_axes.imshow(image[z_slice, :, :], cmap="Greys_r")
    image_axes.set_xticks([])
    image_axes.set_yticks([])
    # The histogram limits represent the full data range, set that also for the box plot
    # (box plot may show smaller range if no outliers are plotted)
    xlims = hist_axes.get_xlim()
    box_axes.set_xlim(left=xlims[0], right=xlims[1])
    box_axes.set_xticks([])  # Ticks match those of histogram anyway
    box_axes.set_yticks([])  # don't need that 1 tick mark
    hist_axes.set_yticks([])  # Number of voxels is not relevant
示例#4
0
def proximity(ax: plt.Axes):
    proximity_all, accuracy_all = [], []
    for pid in DataExp1.pids:
        proximity, accuracy = [], []
        data = DataExp1(pid).data
        for trial in data:
            if trial['ground_truth'] == 'C':
                x = np.array(trial['φ'])[:, :3]
                dx = np.abs(x[:, 0] - x[:, 1])
                proximity.append(np.minimum(dx, 2 * np.pi - dx).mean())
                accuracy.append(trial['choice'] == 'C')
        proximity_all += proximity
        accuracy_all += accuracy
        df = pd.DataFrame({'proximity': proximity, 'accuracy': accuracy})
        df['accuracy'] = df['accuracy'].astype(float)
        print(pearsonr(df['proximity'], df['accuracy']))
    proximity_all, accuracy_all = np.array(proximity_all), np.array(accuracy_all)
    ax.boxplot([proximity_all[~accuracy_all], proximity_all[accuracy_all]])
    ax.set_xticklabels(['Non-$C$', '$C$'])
    ax.set_xlabel('Human choice')
    ax.set_ylabel('Avg. angular distance b/w clustered dots')
示例#5
0
def render_boxplot(
    ax: plt.Axes,
    data: [Sequence[list], Sequence[np.ndarray]],
    x_labels: Sequence[str],
    y_label: str,
    vline_level: float = None,
    vline_label: str = 'approx. solved',
    alpha: float = 1.,
    colorize: bool = False,
    show_fliers: bool = False,
    show_legend: bool = True,
    legend_loc: str = 'best',
    title: str = None,
) -> plt.Figure:
    """
    Create a box plot for a list of data arrays. Every entry results in one column of the box plot.
    The plot is neither shown nor saved.

    .. note::
        If you want to have a tight layout, it is best to pass axes of a figure with `tight_layout=True` or
        `constrained_layout=True`.

    :param ax: axis of the figure to plot on
    :param data: list of data sets to plot as separate boxes
    :param x_labels: labels for the categories on the x-axis
    :param y_label: label for the y-axis
    :param vline_level: if not `None` (default) add a vertical line at the given level
    :param vline_label: label for the vertical line
    :param alpha: transparency (alpha-value) for boxes (including the border lines)
    :param colorize: colorize the core of the boxes
    :param show_fliers: show outliers (more the 1.5 of the inter quartial range) as circles
    :param show_legend: flag if the legend entry should be printed, set to True when using multiple subplots
    :param legend_loc: location of the legend, ignored if `show_legend = False`
    :param title: title displayed above the figure, set to None to suppress the title
    :return: handle to the resulting figure
    """
    medianprops = dict(linewidth=1., color='firebrick')
    meanprops = dict(marker='D',
                     markeredgecolor='black',
                     markerfacecolor='purple')
    boxprops = dict(linewidth=1.)
    whiskerprops = dict(linewidth=1.)
    capprops = dict(linewidth=1.)

    # Plot the data
    box = ax.boxplot(
        data,
        boxprops=boxprops,
        whiskerprops=whiskerprops,
        capprops=capprops,
        meanprops=meanprops,
        meanline=False,
        showmeans=False,
        medianprops=medianprops,
        showfliers=show_fliers,
        notch=False,
        patch_artist=colorize,  # necessary to colorize the boxes
        labels=x_labels,
        widths=0.7)

    if colorize:
        for i, patch in enumerate(box['boxes']):
            patch.set_facecolorf(f'C{i%10}')
            patch.set_alpha(alpha)

    # Add dashed line to mark the approx solved threshold
    if vline_level is not None:
        ax.axhline(vline_level, c='k', ls='--', lw=1., label=vline_label)

    ax.set_ylabel(y_label)
    if show_legend:
        ax.legend(loc=legend_loc)
    if title is not None:
        ax.set_title(title)
    return plt.gcf()
示例#6
0
def plot_box(axes: plt.Axes, data):
    """plot a box diagram
    """
    axes.boxplot(data)
示例#7
0
def plot_f1_metrics(run_histories: List[Run],
                    experiment_name: str,
                    metric_names: Dict[str, str],
                    target_file_paths: Optional[List[str]] = None,
                    axis: plt.Axes = None,
                    y_lims: Tuple[float, float] = None,
                    mode='box',
                    add_legend=True,
                    color=None):
    standalone_mode = axis is None

    if color is None:
        # by default we use bright orange from map 'tab20c'
        color = plt.get_cmap('tab20c')(4)

    runs_data = []
    for run_history in run_histories:
        metrics_data = []
        for metric_name in metric_names.keys():
            metric_data = []
            for fold_history in run_history.fold_histories:
                # add the metric for the last epoch
                metric_data.append(fold_history.epochs[-1].metrics[metric_name])
            metric_data = np.array(metric_data)
            if mode == 'bar':
                metric_data = np.mean(metric_data)
            metrics_data.append(metric_data)
        runs_data.append(metrics_data)

    with plt.style.context('seaborn'):
        if axis is None:
            fig: plt.Figure = plt.figure()
            fig.suptitle(experiment_name, fontsize=24)
            axis = fig.add_subplot(111)
        else:
            axis.set_title(experiment_name, fontsize=22)
        axis.set_ylabel('Metric Value', fontsize=22)
        if y_lims is not None:
            print('limits', y_lims)
            axis.set_ylim(*y_lims)
        num_metrics = None
        num_runs = len(runs_data)
        for i, metrics_data in enumerate(runs_data):
            num_metrics = len(metrics_data)
            xs = range(i * len(metrics_data) + i, (i + 1) * len(metrics_data) + i)

            max_v = .9
            min_v = .6
            colors = []
            for idx in range(num_metrics):
                if num_metrics > 1:
                    norm = idx * (max_v - min_v) / (num_metrics - 1)
                else:
                    norm = 0
                fill_color = list(colorsys.rgb_to_hls(*mc.to_rgb(color)))
                fill_color[1] = min_v + norm
                colors.append((
                    color,
                    colorsys.hls_to_rgb(*fill_color)
                ))
            line_styles = ['-', '-.', ':', '--']

            if mode == 'box':
                boxplots = axis.boxplot(
                    metrics_data,
                    meanline=True,
                    showmeans=True,
                    positions=xs,
                    widths=0.6,
                    patch_artist=True
                )

                for plot_idx in range(num_metrics):
                    dark_color = colors[plot_idx][0]
                    light_color = colors[plot_idx][1]

                    plt.setp(boxplots['boxes'][plot_idx], color=dark_color)
                    plt.setp(boxplots['boxes'][plot_idx], facecolor=light_color)
                    plt.setp(boxplots['boxes'][plot_idx], linestyle=line_styles[plot_idx])

                    plt.setp(boxplots['whiskers'][plot_idx * 2], color=dark_color)
                    plt.setp(boxplots['whiskers'][plot_idx * 2 + 1], color=dark_color)
                    plt.setp(boxplots['whiskers'][plot_idx * 2], linestyle=line_styles[plot_idx])
                    plt.setp(boxplots['whiskers'][plot_idx * 2 + 1], linestyle=line_styles[plot_idx])

                    plt.setp(boxplots['caps'][plot_idx * 2], color=dark_color)
                    plt.setp(boxplots['caps'][plot_idx * 2 + 1], color=dark_color)

                    plt.setp(boxplots['fliers'][plot_idx], markeredgecolor=dark_color)
                    plt.setp(boxplots['fliers'][plot_idx], marker='x')

                    plt.setp(boxplots['medians'][plot_idx], color=dark_color)
                    plt.setp(boxplots['means'][plot_idx], color=dark_color)

                    legend_styles = [boxplots['boxes'][idx] for idx in range(num_metrics)]
            elif mode == 'bar':
                legend_styles = []
                for plot_idx in range(num_metrics):
                    ret = axis.bar(xs[plot_idx], metrics_data[plot_idx],
                                   color=colors[plot_idx][1],
                                   edgecolor=colors[plot_idx][0],
                                   width=0.6,
                                   linewidth=1.25,
                                   linestyle=line_styles[plot_idx], )
                    legend_styles.append(ret)

        tick_offset = num_metrics * 0.5 - 0.5
        ticks = np.arange(start=tick_offset, stop=num_runs * num_metrics + num_runs + tick_offset,
                          step=num_metrics + 1.0)
        axis.set_xticks(ticks)
        for yticklabel in axis.get_yticklabels():
            yticklabel.set_fontsize(20)
        axis.set_xticklabels([r.name for r in run_histories], fontsize=20, rotation=0)
        if add_legend:
            axis.legend(legend_styles, metric_names.values(),
                        loc='lower right', fontsize=16,
                        facecolor="white", frameon=True,
                        edgecolor="black")

        if standalone_mode:
            fig.show()
            if target_file_paths is not None:
                for target_file_path in target_file_paths:
                    fig.savefig(target_file_path)
        return legend_styles