示例#1
0
def plot_surrogate_1D(
    ax: plt.Axes,
    surrogate: Surrogate,
    bound: Bound,
    n_points: int = 100,
) -> None:
    """Plot a 1D surrogate model.

    Parameters
    ----------
    ax: plt.Axes
        matplotlib axes object on which to plot the graph.
    surrogate: Surrogate
        A trained surrogate model.
    bound: Bound
        The bound on which to plot the graph. NB: doesn't have to be the
        same as the optimization bound.
    n_points: int
        The number of points with with to plot the graph, by default 100.
    """
    x = np.linspace(bound.lower, bound.upper, n_points).reshape(-1, 1)
    y_pred, sigma = surrogate.predict(x)
    std = np.sqrt(np.diag(sigma))
    lower = y_pred - 2 * std
    upper = y_pred + 2 * std

    ax.plot(x.flatten(), y_pred, label="mean")
    ax.fill_between(x.flatten(),
                    lower,
                    upper,
                    alpha=0.5,
                    label="confidence interval")
    ax.plot(surrogate.x.flatten(), surrogate.y, "k+", label="training data")
示例#2
0
def plot_throughput(df: pd.DataFrame, ax: plt.Axes) -> None:
    def outlier_throughput(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].mean() / 100000

    def outlier_throughput_std(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].std() / 100000

    # Draw throughput.
    variables = ('flexible', 'num_acceptor_groups', 'num_acceptors_per_group',
                 'num_replicas')
    grouped = df.groupby(variables)
    for (name, group) in grouped:
        (_, num_acceptor_groups, num_acceptors_per_group, num_replicas) = name
        label = (f'{num_acceptor_groups}x{num_acceptors_per_group} ' +
                  f'acceptors, {num_replicas} replicas')
        by_num_proxy_leaders = group.groupby('num_proxy_leaders')
        throughput = by_num_proxy_leaders.apply(outlier_throughput).sort_index()
        std = by_num_proxy_leaders.apply(outlier_throughput_std).sort_index()
        line = ax.plot(throughput.index, throughput,
                       '-', marker = next(MARKERS), label=label,
                       linewidth=1.5)[0]
        # Draw error bars.
        ax.fill_between(throughput.index,
                        throughput - std,
                        throughput + std,
                        color=line.get_color(),
                        alpha=0.3)
def roc_curve(y_true: list,
              y_score: list,
              ax: Axes,
              label: str,
              colour: str,
              plot_ci: bool = False):
    roc_metrics = [
        metrics.roc_curve(x.reshape(-1, 1), y)
        for x, y in zip(y_true, y_score)
    ]
    fpr = [x[0] for x in roc_metrics]
    tpr = [x[1] for x in roc_metrics]
    base_fpr = np.linspace(0, 1, 101)
    tprs = list()
    for fpr_, tpr_ in zip(fpr, tpr):
        tpr = np.interp(base_fpr, fpr_, tpr_)
        tpr[0] = 0.0
        tprs.append(tpr)
    tprs = np.array(tprs)
    mean_tprs = tprs.mean(axis=0)
    std = tprs.std(axis=0)
    tprs_upper = np.minimum(mean_tprs + std, 1)
    tprs_lower = mean_tprs - std
    ax.plot(base_fpr, mean_tprs, c=colour, label=label)
    ax.plot([0, 1], [0, 1], color='black', lw=1, linestyle='--')
    if plot_ci:
        ax.fill_between(base_fpr,
                        tprs_lower,
                        tprs_upper,
                        color=colour,
                        alpha=0.3)
    return ax
示例#4
0
def plot_throughput(df: pd.DataFrame, ax: plt.Axes) -> None:
    def outlier_throughput(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].mean() / 100000

    def outlier_throughput_std(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].std() / 100000

    # Draw throughput.
    grouped = df.groupby(
        ('leader_options.flush_phase2as_every_n', 'num_replicas'))
    for (name, group) in grouped:
        print(f'## {name}')
        print(group[['throughput', 'latency']])

        by_num_proxy_leaders = group.groupby('num_proxy_leaders')
        throughput = by_num_proxy_leaders.apply(
            outlier_throughput).sort_index()
        std = by_num_proxy_leaders.apply(outlier_throughput_std).sort_index()
        print(f'throughput = {throughput}')
        print(f'std = {std}')
        print()
        line = ax.plot(throughput.index,
                       throughput,
                       '-',
                       marker=next(MARKERS),
                       label=name,
                       linewidth=1.5)[0]
        # Draw error bars.
        ax.fill_between(throughput.index,
                        throughput - std,
                        throughput + std,
                        color=line.get_color(),
                        alpha=0.3)
示例#5
0
def plot_latency(ax: plt.Axes, n: int, before: pd.Series, after: pd.Series,
                 sample_every: int, marker: str) -> None:
    median_before = before.rolling('1000ms').median()
    p95_before = before.rolling('1000ms').quantile(0.95)
    line = ax.plot_date(before.index[::sample_every],
                        median_before[::sample_every],
                        label='_nolegend_',
                        fmt='-',
                        marker=marker,
                        markevery=0.1)[0]
    ax.fill_between(before.index[::sample_every],
                    median_before[::sample_every],
                    p95_before[::sample_every],
                    color=line.get_color(), alpha=0.25)

    median_after = after.rolling('1000ms').median()
    p95_after = after.rolling('1000ms').quantile(0.95)
    label = '1 client' if n == 1 else f'{n} clients'
    ax.plot_date(after.index[::sample_every],
                 median_after[::sample_every],
                 label=label,
                 color = line.get_color(),
                 fmt='-',
                 marker=marker,
                 markevery=0.1)
    ax.fill_between(after.index[::sample_every],
                    median_after[::sample_every],
                    p95_after[::sample_every],
                    color=line.get_color(), alpha=0.25)
示例#6
0
def plot_throughput(df: pd.DataFrame, ax: plt.Axes, label: str) -> None:
    def outlier_throughput(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].mean() / 100000

    def outlier_throughput_std(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].std() / 100000

    # Draw throughput.
    grouped = df.groupby('workload_label')
    vprint(f'# {label}')
    for (name, group) in grouped:
        vprint(f'## {name}')
        vprint(group[['throughput', 'latency']])
    throughput = grouped.apply(outlier_throughput).sort_index()
    std = grouped.apply(outlier_throughput_std).sort_index()
    vprint(f'throughput = {throughput}')
    vprint(f'std = {std}')
    vprint()
    line = ax.plot(throughput.index,
                   throughput,
                   '-',
                   marker=next(MARKERS),
                   label=label,
                   linewidth=1.5)[0]

    # Draw error bars.
    ax.fill_between(throughput.index,
                    throughput - std,
                    throughput + std,
                    color=line.get_color(),
                    alpha=0.3)
示例#7
0
def plot_1d_data(
    X_train: NDArray,
    y_train: NDArray,
    X_test: NDArray,
    y_test: NDArray,
    y_sigma: float,
    y_pred: NDArray,
    y_pred_low: NDArray,
    y_pred_up: NDArray,
    ax: plt.Axes,
    title: str,
) -> None:
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_xlim([-10, 10])
    ax.set_ylim([np.min(y_test) * 1.3, np.max(y_test) * 1.3])
    ax.fill_between(X_test, y_pred_low, y_pred_up, alpha=0.3)
    ax.scatter(X_train, y_train, color="red", alpha=0.3, label="Training data")
    ax.plot(X_test, y_test, color="gray", label="True confidence intervals")
    ax.plot(X_test, y_test - y_sigma, color="gray", ls="--")
    ax.plot(X_test, y_test + y_sigma, color="gray", ls="--")
    ax.plot(X_test, y_pred, color="b", alpha=0.5, label="Prediction intervals")
    if title is not None:
        ax.set_title(title)
    ax.legend()
def plot_total_mass(
    replicates: List[RawData],
    ax: plt.Axes,
    label: str = '',
    color: str = 'black',
    fontsize: float = 36,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    '''Plot total mass data on an existing set of axes.

    Plots the median surrounded by a translucent band indicating the
    IQR. The median and IQR are computed over the replicates.

    Args:
        replicates: A list of the raw data dictionary for each
            replicate.
        ax: The axes to plot on.
        label: Label to associate with the curve showing the median
            total mass.
        color: Color of median curve and IQR band.
        fontsize: Size of all text in figure.

    Returns:
        A tuple of a numpy array for each quartile.
    '''
    times = sorted(replicates[0].keys())
    mass_timeseries = []
    for replicate in replicates:
        assert sorted(replicate.keys()) == times
        replicate_timeseries = get_total_mass_timeseries(replicate)
        mass_timeseries.append(replicate_timeseries)
    mass_matrix = np.array(mass_timeseries)
    median = np.median(mass_matrix, axis=0)
    q25, q75 = np.percentile(mass_matrix, [25, 75], axis=0)
    times_hours = tuple(time / 60 / 60 for time in times)
    if label:
        ax.semilogy(  # type: ignore
            times_hours, median, label=label, color=color)
        ax.fill_between(  # type: ignore
            times_hours,
            q25,
            q75,
            color=color,
            alpha=0.2,
            edgecolor='none')
        ax.legend(  # type: ignore
            prop={'size': fontsize}, frameon=False)
    else:
        ax.semilogy(  # type: ignore
            times_hours, mass_timeseries, color=color)
        ax.fill_between(  # type: ignore
            times_hours,
            q25,
            q75,
            color=color,
            alpha=0.2,
            edgecolor='none')
    for tick_type in ('major', 'minor'):
        ax.tick_params(  # type: ignore
            axis='both', which=tick_type, labelsize=fontsize)
    return cast(np.ndarray, q25), median, cast(np.ndarray, q75)
def plot_throughput(df: pd.DataFrame, ax: plt.Axes) -> None:
    def outlier_throughput(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].mean() / 100000

    def outlier_throughput_std(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].std() / 100000

    # Draw throughput.
    grouped = df.groupby('batcher_options.batch_size')
    for (name, group) in grouped:
        by_num_proxy_leaders = group.groupby('num_proxy_replicas')
        throughput = by_num_proxy_leaders.apply(
            outlier_throughput).sort_index()
        std = by_num_proxy_leaders.apply(outlier_throughput_std).sort_index()
        line = ax.plot(throughput.index,
                       throughput,
                       '-',
                       marker=next(MARKERS),
                       label=name,
                       linewidth=1.5)[0]
        # Draw error bars.
        ax.fill_between(throughput.index,
                        throughput - std,
                        throughput + std,
                        color=line.get_color(),
                        alpha=0.3)
示例#10
0
def draw_uncertainty_bands(
    uncertainty: tdub.root.TGraphAsymmErrors,
    total_mc: tdub.root.TH1,
    ax: plt.Axes,
    axr: plt.Axes,
    label: str = "Uncertainty",
    edgecolor: str | int = "mediumblue",
    zero_threshold: float = 0.25,
) -> None:
    """Draw uncertainty bands on both axes in stack plot with a ratio.

    Parameters
    ----------
    uncertainty : tdub.root.TGraphAsymmErrors
        ROOT TGraphAsymmErrors with full systematic uncertainty.
    total_mc : tdub.root.TH1
        ROOT TH1 providing the full Monte Carlo prediction.
    ax : matplotlib.axes.Axes
        Main axis (where histogram stack is painted)
    axr : matplotlib.axes.Axes
        Ratio axis
    label : str
        Legend label for the uncertainty.
    zero_threshold : float
        When total MC events are below threshold, zero contents and error.

    """
    lo = np.hstack([uncertainty.ylo, uncertainty.ylo[-1]])
    hi = np.hstack([uncertainty.yhi, uncertainty.yhi[-1]])
    mc = np.hstack([total_mc.counts, total_mc.counts[-1]])
    ratio_y1 = 1 - (lo / mc)
    ratio_y2 = 1 + (hi / mc)
    set_to_zero = mc < zero_threshold
    lo[set_to_zero] = 0.0
    hi[set_to_zero] = 0.0
    mc[set_to_zero] = 0.0
    ratio_y1[set_to_zero] = 0.0
    ratio_y2[set_to_zero] = 0.0
    ax.fill_between(
        x=total_mc.edges,
        y1=(mc - lo),
        y2=(mc + hi),
        step="post",
        facecolor="none",
        hatch="////",
        edgecolor=edgecolor,
        linewidth=0.0,
        label=label,
        zorder=50,
    )
    axr.fill_between(
        x=total_mc.edges,
        y1=ratio_y1,
        y2=ratio_y2,
        step="post",
        facecolor=(0, 0, 0, 0.33),
        linewidth=0.0,
        label=label,
        zorder=50,
    )
示例#11
0
def render_mean_std(
    ax: plt.Axes,
    x_grid: np.ndarray,
    mean: np.ndarray,
    std: np.ndarray,
    x_label: str,
    y_label: str,
    curve_label: str,
    num_stds: int = 1,
    alpha: float = 0.3,
    color: chr = None,
    show_legend: bool = True,
    show_legend_std: bool = False,
    title: str = None,
) -> plt.Figure:
    """
    Plot the given mean and the standard deviation over given x-axis data. 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 x_grid: data to plot on the x-axis
    :param mean: mean values to plot on the y-axis
    :param std: standard deviation values to plot on the y-axis
    :param x_label: label for the x-axis
    :param y_label: label for the y-axis
    :param curve_label: label for the mean curve
    :param num_stds: number of standard deviations to plot around the mean
    :param alpha: transparency (alpha-value) for the std area
    :param color: color (e.g. 'k'), None invokes the default behavior
    :param show_legend: flag if the legend entry should be printed, set to True when using multiple subplots
    :param show_legend_std: flag if a legend entry for the std area should be printed
    :param title: title displayed above the figure, set to None to suppress the title
    :param tight_layout: if True, the x and y axes will have no space to the plotted curves
    :return: handle to the resulting figure
    """
    ax.plot(x_grid, mean, label=curve_label, color=color)
    if show_legend_std:
        ax.fill_between(x_grid,
                        mean - num_stds * std,
                        mean + num_stds * std,
                        alpha=alpha,
                        label=f'$\pm {num_stds}$ std')
    else:
        ax.fill_between(x_grid,
                        mean - num_stds * std,
                        mean + num_stds * std,
                        alpha=alpha)
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    if show_legend:
        ax.legend()
    if title is not None:
        ax.set_title(title)
    return plt.gcf()
示例#12
0
文件: plotter.py 项目: techge/nemesys
    def fillDiffToCompare(ax: plt.Axes, analysisResult: List[float], compareValue: List[float]):
        """
        Fill the difference between analysisResults and compareValue in the plot.

        Call only after first plot into the figure has been done.

        :param ax: The ax to plot the difference to
        :param analysisResult: The first list of values of an analysis result
        :param compareValue: The second list of values of another analysis result
        """
        ax.fill_between(range(len(analysisResult)), analysisResult, compareValue, color='b', alpha=.4)
示例#13
0
def plot_expression_survival_boundary(ax: plt.Axes,
                                      boundary_x: Sequence[float],
                                      boundary_y: Sequence[float],
                                      boundary_error: Sequence[float],
                                      finals: Sequence[float],
                                      scaling: float = 1,
                                      boundary_color: str = 'black') -> None:
    '''Plot the life-death boundary on an expression-survival plot.

    Args:
        ax: Axes to plot on.
        boundary_x: X-values of the boundary identified
            numerically from the antibiotic model.
        boundary_y: Y-values of the boundary identified
            numerically from the antibiotic model.
        boundary_error: Precision of the Y-value
            predictions. This is the distance along the y axis between
            the known dead point and known live point closest to the
            prediction. The predicted y-value will be in the middle of
            this range, so an error band can be drawn of width
            boundary_error centered at boundary_y.
        finals: All final cell concentrations plotted on
            the figure. This is used to ensure the boundary line spans
            the figure.
        scaling: Coefficient to multiply all data by. This is
            intended to be used for changing the units plotted.
        boundary_color: Color of boundary.
    '''
    boundary_x_arr = np.array(boundary_x)
    boundary_y_arr = np.array(boundary_y)
    boundary_error_arr = np.array(boundary_error)
    mask = ((min(finals) <= boundary_x_arr) & (boundary_x_arr <= max(finals)))
    true_indices = np.where(mask)[0]
    if len(true_indices) > 0:
        min_true_idx = min(true_indices)
        max_true_idx = max(true_indices)
        # Make sure boundary spans entire figure
        if min_true_idx > 0:
            mask[min_true_idx - 1] = True
        if max_true_idx < len(mask) - 1:
            mask[max_true_idx + 1] = True
    boundary_x_arr = boundary_x_arr[mask]
    boundary_y_arr = boundary_y_arr[mask]
    ax.plot(  # type: ignore
        boundary_x_arr * scaling,
        boundary_y_arr * scaling,
        c=boundary_color,
        label='Boundary Predicted by Antibiotics Model')
    ax.fill_between(  # type: ignore
        boundary_x_arr * scaling,
        boundary_y_arr * scaling - boundary_error_arr * scaling / 2,
        boundary_y_arr * scaling + boundary_error_arr * scaling / 2,
        color=boundary_color,
        alpha=0.2)
示例#14
0
def plot_latency(ax: plt.Axes, label: str, color: str,
                 s: pd.Series, sample_every: int) -> None:
    median = s.rolling('500ms').median()
    max_latency = s.rolling('500ms').max()

    # ax.set_yscale('log')
    line = ax.plot_date(s.index[::sample_every], median[::sample_every],
                        fmt='-', color=color, markevery=0.2, label=label)[0]
    # ax.plot_date(s.index[::sample_every], max_latency[::sample_every],
    #              fmt='--', color=color, label='_nolegend_')
    ax.fill_between(s.index[::sample_every], median[::sample_every],
                    max_latency[::sample_every], color=line.get_color())
示例#15
0
def plot_latency(ax: plt.Axes, s: pd.Series, sample_every: int) -> None:
    median = s.rolling('1000ms').median()
    p95 = s.rolling('1000ms').quantile(0.95)
    line = ax.plot_date(s.index[::sample_every],
                        median[::sample_every],
                        label='100 clients',
                        fmt='-')[0]
    ax.fill_between(s.index[::sample_every],
                    median[::sample_every],
                    p95[::sample_every],
                    color=line.get_color(),
                    alpha=0.25)
示例#16
0
 def format_trace_plot(plot: plt.Axes, trace_forward: np.ndarray,
                       trace_reverse: np.ndarray):
     x = np.arange(n_trace + 1)[1:] * trace_spacing * 100
     plot.errorbar(x,
                   trace_forward[:, 0],
                   yerr=2 * trace_forward[:, 1],
                   ecolor='b',
                   elinewidth=0,
                   mec='none',
                   mew=0,
                   linestyle='None',
                   zorder=10)
     plot.plot(
         x,
         trace_forward[:, 0],
         'b-',
         marker='o',
         mec='b',
         mfc='w',
         label='Forward',
         zorder=20,
     )
     plot.errorbar(x,
                   trace_reverse[:, 0],
                   yerr=2 * trace_reverse[:, 1],
                   ecolor='r',
                   elinewidth=0,
                   mec='none',
                   mew=0,
                   linestyle='None',
                   zorder=10)
     plot.plot(x,
               trace_reverse[:, 0],
               'r-',
               marker='o',
               mec='r',
               mfc='w',
               label='Reverse',
               zorder=20)
     y_fill_upper = [trace_forward[-1, 0] + 2 * trace_forward[-1, 1]
                     ] * 2
     y_fill_lower = [trace_forward[-1, 0] - 2 * trace_forward[-1, 1]
                     ] * 2
     xlim = [0, 100]
     plot.fill_between(xlim,
                       y_fill_lower,
                       y_fill_upper,
                       color='orchid',
                       zorder=5)
     plot.set_xlim(xlim)
     plot.legend()
     plot.set_xlabel("% Samples Analyzed", fontsize=20)
     plot.set_ylabel(r"$\Delta G$ in kcal/mol", fontsize=20)
示例#17
0
    def _add_sd_area(self, element: str, ax: plt.Axes):
        """
        Draw red area around the fitting line to show confidence intervals

        :param element: Which variable is being plotted
        :param ax: axes on which to draw the area before returning to mother function
        """

        y1 = self.simulated_data_low_ci[element].to_numpy()
        y2 = self.simulated_data_high_ci[element].to_numpy()
        x = self.simulated_data.index
        ax.fill_between(x, y1, y2, alpha=.3, linewidth=0, color="red")
        return ax
示例#18
0
def render_lo_up_avg(
    ax: plt.Axes,
    x_grid: np.ndarray,
    lower: np.ndarray,
    upper: np.ndarray,
    average: np.ndarray,
    x_label: str,
    y_label: str,
    curve_label: str,
    alpha: float = 0.3,
    color: chr = None,
    show_legend: bool = True,
    area_label: [str, None] = r'min \& max',
    title: str = None,
):
    """
    Plot the given average, minimum, and maximum values over given x-axis data. 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 x_grid: data to plot on the x-axis
    :param lower: minimum values to plot on the y-axis
    :param upper: maximum values to plot on the y-axis
    :param average: average values to plot on the y-axis
    :param x_label: label for the x-axis
    :param y_label: label for the y-axis
    :param curve_label: label for the average curve
    :param alpha: transparency (alpha-value) for the std area
    :param color: color (e.g. 'k'), None invokes the default behavior
    :param show_legend: flag if the legend entry should be printed, set to True when using multiple subplots
    :param area_label: label for the shaded area, pass None to omit the label
    :param title: title displayed above the figure, set to None to suppress the title
    :param tight_layout: if True, the x and y axes will have no space to the plotted curves
    :return: handle to the resulting figure
    """
    if area_label is not None:
        ax.fill_between(x_grid, lower, upper, alpha=alpha, label=area_label)
    else:
        ax.fill_between(x_grid, lower, upper, alpha=alpha)
    ax.plot(x_grid, average, label=curve_label, color=color)

    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    if show_legend:
        ax.legend()
    if title is not None:
        ax.set_title(title)
    return plt.gcf()
示例#19
0
def plot_1d_data(
    X_train: NDArray,
    y_train: NDArray,
    X_test: NDArray,
    y_test: NDArray,
    y_test_sigma: float,
    y_pred: NDArray,
    y_pred_low: NDArray,
    y_pred_up: NDArray,
    ax: plt.Axes,
    title: str,
) -> None:
    """
    Generate a figure showing the training data and estimated
    prediction intervals on test data.

    Parameters
    ----------
    X_train : NDArray
        Training data.
    y_train : NDArray
        Training labels.
    X_test : NDArray
        Test data.
    y_test : NDArray
        True function values on test data.
    y_test_sigma : float
        True standard deviation.
    y_pred : NDArray
        Predictions on test data.
    y_pred_low : NDArray
        Predicted lower bounds on test data.
    y_pred_up : NDArray
        Predicted upper bounds on test data.
    ax : plt.Axes
        Axis to plot.
    title : str
        Title of the figure.
    """
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])
    ax.scatter(X_train, y_train, color="red", alpha=0.3, label="training")
    ax.plot(X_test, y_test, color="gray", label="True confidence intervals")
    ax.plot(X_test, y_test - y_test_sigma, color="gray", ls="--")
    ax.plot(X_test, y_test + y_test_sigma, color="gray", ls="--")
    ax.plot(X_test, y_pred, label="Prediction intervals")
    ax.fill_between(X_test, y_pred_low, y_pred_up, alpha=0.3)
    ax.set_title(title)
    ax.legend()
def plot_learning_curve(data: pd.DataFrame,
                        features: list,
                        label: str,
                        ax: Axes,
                        n_folds: int = 5,
                        n_jobs: int = -1,
                        scoring: str = "balanced_accuracy",
                        train_sizes: np.array = np.linspace(0.3, 1.0, 10)):
    logreg = SMLogitWrapper()
    skf = StratifiedKFold(n_splits=n_folds, shuffle=False)
    train_sizes, train_scores, test_scores = \
        learning_curve(logreg,
                       data[features],
                       data[label],
                       cv=skf,
                       n_jobs=n_jobs,
                       scoring=scoring,
                       train_sizes=train_sizes,
                       return_times=False)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    ax.plot(train_sizes,
            train_scores_mean,
            'o-',
            color='r',
            label='Training score')
    ax.fill_between(train_sizes,
                    train_scores_mean - train_scores_std,
                    train_scores_mean + train_scores_std,
                    alpha=0.1,
                    color="r")
    ax.plot(train_sizes,
            test_scores_mean,
            'o-',
            color='g',
            label='Cross-validation score')
    ax.fill_between(train_sizes,
                    test_scores_mean - test_scores_std,
                    test_scores_mean + test_scores_std,
                    alpha=0.1,
                    color="g")
    ax.legend(loc="best")
    ax.set_xlabel("Training examples")
    ax.set_ylabel(scoring.replace("_", "").title())
    ax.set_ylim(0, 1)
    return ax
示例#21
0
def plot_latency(ax: plt.Axes, n: int, s: pd.Series, sample_every: int,
                 marker: str) -> None:
    median = s.rolling('1000ms').median()
    p95 = s.rolling('1000ms').quantile(0.95)
    label = '1 client' if n == 1 else f'{n} clients'
    line = ax.plot_date(s.index[::sample_every],
                        median[::sample_every],
                        label=label,
                        fmt='-',
                        marker=marker,
                        markevery=0.1)[0]
    ax.fill_between(s.index[::sample_every],
                    median[::sample_every],
                    p95[::sample_every],
                    color=line.get_color(),
                    alpha=0.25)
示例#22
0
    def _plot_smoothed_proportion(
        self,
        ax: plt.Axes,
        clusters: Sequence[Any],
        y_offset: Mapping[Any, float],
        alpha: float = 0.8,
    ) -> Tuple[Mapping[Any, np.ndarray], Mapping[Any, PolyCollection]]:
        start_t, end_t = self._cmat.columns.min(), self._cmat.columns.max()
        x = np.array(self._cmat.columns)  # fitting
        # extrapolation
        e = np.linspace(start_t, end_t, int(1 + (end_t - start_t) * 100))

        smoothed_proportion, handles = {}, {}
        for clust in clusters:
            y = self._cmat.loc[clust]
            f = interp1d(x, y)
            fe = f(e)
            lo = lowess(fe, e, frac=0.3, is_sorted=True, return_sorted=False)
            smoothed_proportion[clust] = lo

            handles[clust] = ax.fill_between(
                e,
                y_offset[clust] + lo,
                y_offset[clust] - lo,
                color=self.cmap[clust],
                label=clust,
                alpha=alpha,
                edgecolor=None,
            )

        return smoothed_proportion, handles
示例#23
0
def graph_frame(hist: history.DB, db_res_case: int,
                contour_key: history.ContourKey, ax: plt.Axes):
    row_skeleton = history.ColumnResult._all_nones()._replace(
        result_case_num=db_res_case)
    column_data = list(hist.get_all_matching(row_skeleton))

    col_data_graph = [
        cd for cd in column_data if cd.contour_key == contour_key
    ]

    ax.clear()
    graphed_something = False

    for yielded, base_col in (
        (False, 'tab:blue'),
        (True, 'tab:orange'),
    ):
        # Fall back to NaNs, only override with the real data.
        x_to_cd = {
            cd.x: history.ColumnResult._nans_at(cd.x)
            for cd in col_data_graph
        }

        # Override with the real thing
        for cd in col_data_graph:
            if cd.yielded == yielded:
                x_to_cd[cd.x] = cd
                graphed_something = True

        res_to_plot = [cd for x, cd in sorted(x_to_cd.items())]
        x = [cd.x for cd in res_to_plot]
        y_min = [cd.minimum for cd in res_to_plot]
        y_mean = [cd.mean for cd in res_to_plot]
        y_max = [cd.maximum for cd in res_to_plot]

        ax.fill_between(x, y_min, y_max, color=base_col, alpha=0.2)
        yield_text = "Dilated" if yielded else "Undilated"
        ax.plot(x,
                y_mean,
                color=base_col,
                label=f"{contour_key.name} ({yield_text})")

    ax.legend()

    return graphed_something
示例#24
0
 def format_trace_plot(plot: plt.Axes, trace_forward: np.ndarray, trace_reverse: np.ndarray):
     x = np.arange(n_trace + 1)[1:] * trace_spacing * 100
     plot.errorbar(x, trace_forward[:, 0], yerr=2 * trace_forward[:, 1], ecolor='b',
                   elinewidth=0, mec='none', mew=0, linestyle='None',
                   zorder=10)
     plot.plot(x, trace_forward[:, 0], 'b-', marker='o', mec='b', mfc='w', label='Forward', zorder=20,)
     plot.errorbar(x, trace_reverse[:, 0], yerr=2 * trace_reverse[:, 1], ecolor='r',
                   elinewidth=0, mec='none', mew=0, linestyle='None',
                   zorder=10)
     plot.plot(x, trace_reverse[:, 0], 'r-', marker='o', mec='r', mfc='w', label='Reverse', zorder=20)
     y_fill_upper = [trace_forward[-1, 0] + 2 * trace_forward[-1, 1]] * 2
     y_fill_lower = [trace_forward[-1, 0] - 2 * trace_forward[-1, 1]] * 2
     xlim = [0, 100]
     plot.fill_between(xlim, y_fill_lower, y_fill_upper, color='orchid', zorder=5)
     plot.set_xlim(xlim)
     plot.legend()
     plot.set_xlabel("% Samples Analyzed", fontsize=20)
     plot.set_ylabel(r"$\Delta G$ in kcal/mol", fontsize=20)
示例#25
0
def plot_throughput(df: pd.DataFrame, ax: plt.Axes,
                    grouping_columns: Tuple[str, ...], x_column: str,
                    y_columns: List[str]) -> None:
    def outlier_mean(c: str):
        def f(g: pd.DataFrame) -> float:
            cutoff = 0.5 * g[c].max()
            return g[g[c] >= cutoff][c].mean() / 100000

        return f

    def outlier_std(c: str):
        def f(g: pd.DataFrame):
            cutoff = 0.5 * g[c].max()
            return g[g[c] >= cutoff][c].std() / 100000

        return f

    # Draw throughput.
    grouped = df.groupby(grouping_columns)
    for (name, group) in grouped:
        name = [name] if len(grouping_columns) == 1 else name
        label = ','.join(f'{f}={x}' for (f, x) in zip(grouping_columns, name))
        by_acceptors = group.groupby(x_column)
        for c in y_columns:
            throughput = by_acceptors.apply(outlier_mean(c)).sort_index()
            std = by_acceptors.apply(outlier_std(c)).sort_index()
            line = ax.plot(throughput.index,
                           throughput,
                           '-',
                           marker=next(MARKERS),
                           label=f'{c} {label}',
                           linewidth=1.5)[0]
            # Draw error bars.
            ax.fill_between(throughput.index,
                            throughput - std,
                            throughput + std,
                            color=line.get_color(),
                            alpha=0.3)

    ax.set_xlabel(x_column)
    ax.set_ylabel('Throughput\n(100,000 commands per second)')
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    ax.grid()
def plot_1d_data(X_train: np.ndarray, y_train: np.ndarray, X_test: np.ndarray,
                 y_test: np.ndarray, y_test_sigma: float, y_pred: np.ndarray,
                 y_pred_low: np.ndarray, y_pred_up: np.ndarray, ax: plt.Axes,
                 title: str) -> None:
    """
    Generate a figure showing the training data and estimated
    prediction intervals on test data.

    Parameters
    ----------
    X_train : np.ndarray
        Training data.
    y_train : np.ndarray
        Training labels.
    X_test : np.ndarray
        Test data.
    y_test : np.ndarray
        True function values on test data.
    y_test_sigma : float
        True standard deviation.
    y_pred : np.ndarray
        Predictions on test data.
    y_pred_low : np.ndarray
        Predicted lower bounds on test data.
    y_pred_up : np.ndarray
        Predicted upper bounds on test data.
    ax : plt.Axes
        Axis to plot.
    title : str
        Title of the figure.
    """
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_xlim([0, 1.1])
    ax.set_ylim([0, 1])
    ax.scatter(X_train, y_train, color='red', alpha=0.3, label='training')
    ax.plot(X_test, y_test, color='gray', label='True confidence intervals')
    ax.plot(X_test, y_test - y_test_sigma, color='gray', ls='--')
    ax.plot(X_test, y_test + y_test_sigma, color='gray', ls='--')
    ax.plot(X_test, y_pred, label='Prediction intervals')
    ax.fill_between(X_test, y_pred_low, y_pred_up, alpha=0.3)
    ax.set_title(title)
    ax.legend()
示例#27
0
 def show(self, ax: Axes, plot: "plt.Plot"):
     xvalues = self.xvalues
     yvalues = self.yvalues
     ax.plot(xvalues,
             yvalues,
             self.fmt if self.fmt else "-",
             color=self.color,
             label=self.label,
             **self.plot_kwargs)
     yerr = self.yerr
     if yerr.size > 0 and plot.plot_settings[lit.ERROR_BAR]:
         max_vals = yvalues + yerr
         min_vals = yvalues - yerr
         ax.fill_between(xvalues,
                         min_vals,
                         max_vals,
                         edgecolor='none',
                         color=self.color,
                         alpha=0.3,
                         interpolate=True,
                         zorder=0)
示例#28
0
def plot_throughput(df: pd.DataFrame, ax: plt.Axes, n: int, label: str) -> None:
    def outlier_throughput(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].mean() / 100000

    def outlier_throughput_std(g: pd.DataFrame) -> float:
        cutoff = 0.5 * g['throughput'].max()
        return g[g['throughput'] >= cutoff]['throughput'].std() / 100000

    # Draw throughput.
    grouped = df.groupby('workload_label')
    vprint(f'# {label}')
    for (name, group) in grouped:
        vprint(f'## {name}')
        vprint(group[['throughput', 'latency']])
    throughput = grouped.apply(outlier_throughput).sort_index()
    std = grouped.apply(outlier_throughput_std).sort_index()
    vprint(f'throughput = {throughput}')
    vprint(f'std = {std}')
    vprint()
    line = ax.plot(throughput.index, throughput,
                   '-', marker = next(MARKERS), label=label, linewidth=1.5)[0]

    # Draw ideal throughput.
    # frs = throughput.index.to_series()
    # ideal = n * ALPHA / ((n * (1 - frs)) + (frs * 1))
    # ax.plot(frs,
    #         ideal,
    #         '--',
    #         linewidth=1.5,
    #         color=line.get_color(),
    #         alpha = 0.75,
    #         label='_nolegend_')

    # Draw error bars.
    ax.fill_between(throughput.index,
                    throughput - std,
                    throughput + std,
                    color=line.get_color(),
                    alpha=0.3)
示例#29
0
def subplot_result(ax: plt.Axes, name: str, minimize: bool,
                   opt_settings: OptimizerSettings) -> None:

    results = opt_settings.results
    if len(results.shape) != 2:
        """
        The shape of results must be (n_trials, n_iterations).
        results[i][j] := the performance metric at
                         the j-th iteration of the i-th trial
        """
        raise ValueError(
            "The shape of results must be (n_trials, n_iterations). "
            f"But got the shape {results.shape}.")
    if not isinstance(results, np.ndarray):
        raise ValueError("results must be np.ndarray. "
                         f"But got {type(results)}.")

    iterations = np.arange(1, results.shape[1] + 1)

    if minimize:
        cumulative = np.minimum.accumulate(results, axis=-1)
    else:
        cumulative = np.maximum.accumulate(results, axis=-1)

    mean = cumulative.mean(axis=0)
    std = cumulative.std(axis=0)

    ax.plot(iterations,
            mean,
            color=opt_settings.color,
            marker=opt_settings.marker,
            label=f"{opt_settings.name}: $\\mu \\pm \\sigma$")
    ax.fill_between(iterations,
                    mean - std,
                    mean + std,
                    color=opt_settings.color,
                    alpha=0.2)
示例#30
0
    def plot(self,
             x_label: str = '1 - Specificity',
             y_label: str = 'Sensitivity',
             title: str = 'ROC Curve',
             label: Optional[str] = None,
             color: str = 'blue',
             bootstrap: bool = False,
             num_bootstraps: int = 1000,
             num_bootstrap_jobs: int = 1,
             seed: Optional[int] = None,
             p_value: float = 0.05,
             mean_roc: bool = False,
             show_min_max: bool = False,
             plot_roc_curve: bool = True,
             ax: plt.Axes = None) -> plt.Axes:
        """Plot ROC curve.

        Parameters
        ----------
        x_label
            Label for x-axis in ROC curve plot.
        y_label
            Label for y-axis in ROC curve plot.
        title
            Title for ROC curve plot.
        label
            If set, this will be the label displayed in the legend.
        color
            Color of the ROC curve and the confidence interval, if required.
        bootstrap
            If set to True the ROC curve will be plotted with a confidence
            interval using bootstrapping.
        num_bootstraps
            Number of bootstraps to apply on the ROC curve. The larger the
            value the more accurate the confidence interval. But larger values
            for num_bootstrap will lead to longer computation time and memory
            usage.
        num_bootstrap_jobs
            Number of jobs used to compute the bootstraps for the ROC curve in
            parallel. If n_jobs is set negative all available cpu threads will
            be used.
        seed
            Seed used for bootstrapping the ROC curve. If no seed is set the
            seed will be set randomly, generating non-deterministic output.
        p_value
            Value between 0 and 1. This value shows the confidence area of the
            ROC curve.
        mean_roc
            If set to True all bootstrapped ROC curves are used to create an
            averaged ROC curve. Usually this ROC curve looks more smooth than
            the original ROC curve, and therefore can be used for smoothing the
            original ROC curve.
        show_min_max
            If set to True the ROC curve will be plotted with the smallest and
            largest true positive rate values obtained from bootstrapping. This
            parameter is mainly used for debug purposes.
        plot_roc_curve
            If set to False the ROC curve will not be plotted. But the
            confidence interval, obtained with bootstrapping, and averaged ROC
            curve will be plotted. This option could be usefull for plotting
            the average ROC curve without the original ROC curve.
        ax
            If ax is given the ROC plot will be plotted into this Matplotlib
            axis.

        Raises
        ------
        RuntimeError
            If mean_roc is set to True, but bootstrap is set to False.

        Returns
        -------
        ax
            Matplotlib axis plot with the ROC curve plot, optionally with
            confidence interval.

        """
        if not ax:
            ax = plt.gca()

        if bootstrap:
            bsp = self.bootstrap_confidence(
                num_bootstraps=num_bootstraps,
                seed=seed,
                num_bootstrap_jobs=num_bootstrap_jobs,
                show_min_max=show_min_max,
                mean_roc=mean_roc,
                p_value=p_value)

            ax.fill_between(bsp.xrange,
                            bsp.min_quantile,
                            bsp.max_quantile,
                            alpha=0.2,
                            color=color)
            if show_min_max:
                ax.fill_between(bsp.xrange,
                                bsp.min,
                                bsp.max,
                                alpha=0.1,
                                color=color)
            ax.plot(bsp.xrange, bsp.min_quantile, color=color, alpha=0.3)
            ax.plot(bsp.xrange, bsp.max_quantile, color=color, alpha=0.3)

        if mean_roc:
            if not bootstrap:
                raise RuntimeError(
                    'Cannot plot mean ROC curve without bootstrapping.')
            if label:
                ax.plot(bsp.xrange, bsp.mean, label=label, color=color)
            else:
                ax.plot(bsp.xrange, bsp.mean, color=color)
        elif plot_roc_curve:
            fps, tps, _ = self.roc()
            if label:
                ax.plot(fps, tps, label=label, color=color)
            else:
                ax.plot(fps, tps, color=color)

        ax.set_xlim(0, 1)
        ax.set_ylim(0, 1)
        ax.set_title(title)
        ax.set_xlabel(x_label)
        ax.set_ylabel(y_label)

        return ax
示例#31
0
def plot_equilibrium(eq: pleque.Equilibrium,
                     ax: plt.Axes = None,
                     colorbar=False,
                     **kwargs):
    if ax is None:
        ax = plt.gca()

    if eq._first_wall is not None and len(eq._first_wall) > 2:
        ax.fill_between(eq._first_wall[:, 0],
                        eq._first_wall[:, 1],
                        color='lightgrey')
        ax.plot(eq._first_wall[:, 0],
                eq._first_wall[:, 1],
                color='k',
                lw=2,
                label='First wall')
        sep = eq.separatrix
        in_fw = eq.in_first_wall(sep)
        ax.plot(sep.R[in_fw], sep.Z[in_fw], color='C3', lw=2, alpha=0.5)

    # separatrix
    ax.plot(eq.lcfs.R, eq.lcfs.Z, color='C1', lw=2, ls='--')

    coords = eq.grid((400, 600), 'size')
    rs, zs = coords.mesh()

    psi = eq.psi(coords)

    mask_inlcfs = eq.in_lcfs(coords)
    mask_inlimiter = eq.in_first_wall(coords)
    mask_insol = mask_inlcfs ^ mask_inlimiter

    psi_in = np.ma.masked_array(psi, np.logical_not(mask_inlcfs))
    psi_out = np.ma.masked_array(psi, mask_inlcfs)
    # ax.pcolormesh(coords.R, coords.Z, psi_in, shading='gouraud')

    contour_out = eq.coordinates(r=eq.lcfs.r_mid[0] + 2e-3 * np.arange(1, 11),
                                 theta=np.zeros(10),
                                 grid=False)

    cl = ax.contour(coords.R, coords.Z, psi_in, 20, **kwargs)
    plt.colorbar(cl, ax=ax)

    # todo: psi should be 1-d (!) resolve this
    ax.contour(coords.R,
               coords.Z,
               psi,
               np.sort(np.squeeze(contour_out.psi)),
               colors='C0')

    #    contact = eq.strike_points
    #    ax.plot(contact.R, contact.Z, "C3+")

    op = eq.magnetic_axis
    ax.plot(op.R, op.Z, "C0o")

    psi_lcfs = eq._psi_lcfs
    z0 = eq._mg_axis[1]

    # ax.pcolormesh(coords.R, coords.Z, mask_in)
    # ax.pcolormesh(rs, zs, mask_inlimiter)
    # ax.pcolormesh(coords.R, coords.Z, mask_insol)

    ax.set_xlabel('R [m]')
    ax.set_ylabel('Z [m]')

    if len(eq._first_wall) > 2:
        rlim = [np.min(eq.first_wall.R), np.max(eq.first_wall.R)]
        zlim = [np.min(eq.first_wall.Z), np.max(eq.first_wall.Z)]

        size = rlim[1] - rlim[0]
        rlim[0] -= size / 12
        rlim[1] += size / 12

        size = zlim[1] - zlim[0]
        zlim[0] -= size / 12
        zlim[1] += size / 12
        ax.set_xlim(*rlim)
        ax.set_ylim(*zlim)
    ax.set_aspect('equal')

    return ax
示例#32
0
def plot_calibration_weight_traces(
    dataset: Union[netCDF4.Dataset, List[netCDF4.Dataset]],
    ax: plt.Axes = None,
    bar: bool = True,
    error: str = "stdev",
    num_bootstrap_samples: int = 1000,
    zerobased: bool = False,
):
    """Plot the results of a calibration netCDF dataset.

    Parameters
    ----------
    dataset - netCDF4 dataset with `Protons` data
    ax - matplotlib axes object, if None creates a new figure and axes
    bar - bool, plot BAR estimates
    error - str, if bar is True 'stdev' will plot the BAR error as 2 times standard deviation returned from BAR, 'bootstrap' will plot standard error from bootstrap.
    num_bootstrap_samples - Used if 'bootstrap' is used as error method. Number of bootstrap samples to draw.
    zerobased - if True, label states in zero based fashion in the plots. Defaults to 1-based


    Returns
    -------
    plt.Axes containing the plot

    """
    if ax is None:
        ax = plt.gca()

    # Two methods of calculating the error
    if error == "stdev":
        # Use 2 * standard deviation, as returned by bar
        bootstrap = False
        err_multiply = 2
        err_label = r"BAR estimate $\pm$ $2\sigma$, State {}"

    elif error == "bootstrap":
        # Use the standard error from bootstrap sampling of reverse and forward trajectories
        bootstrap = True
        err_multiply = 1
        err_label = r"BAR estimate $\pm$ SEM, State {}"
    else:
        raise ValueError("Unsupported error method: {}.".format(error))

    if zerobased:
        label_offset = 0
    else:
        label_offset = 1

    # Make the y-axis label, ticks and tick labels match the line color.
    ax.set_xlabel("Update", fontsize=18)

    ax.set_ylabel(r"$g_k/RT$ (unitless) relative to state 1", fontsize=18)
    if type(dataset) == netCDF4.Dataset:
        n_states, gk = calibration_dataset_to_arrays(dataset)[-2:]
    elif type(dataset) == list:
        n_states, gk = stitch_calibrations(dataset)[-2:]
    cp = sns.color_palette("husl", n_states)

    if bar:
        bar_data = bar_all_states(
            dataset, bootstrap=bootstrap, num_bootstrap_samples=num_bootstrap_samples
        )
    else:
        bar_data = None

    xaxis_points = list(range(gk.shape[0]))
    for state in range(1, n_states):
        state_color = cp[state]
        ax.plot(
            gk[:, state],
            label="State {}".format(state + label_offset),
            color=state_color,
        )

        if bar:
            estimate, err = bar_data["0->{}".format(state)]
            err *= err_multiply
            ax.fill_between(
                xaxis_points,
                -estimate - err,
                -estimate + err,
                alpha=0.3,
                color=state_color,
                label=err_label.format(state + label_offset),
            )
    sns.despine(ax=ax)
    return ax