Esempio n. 1
0
def build_convfreqs(res, saveto=None, savefig=None):
    '''
    Build plot with simulation descriptives
    ...

    Arguments
    ---------
    res
    saveto
    savefig

    Returns
    -------
    out
    ts
    '''
    g = res.groupby(level=['prop_mix'])
    f, axes = plt.subplots(g.ngroups, 1, figsize=(6, 10))
    color = 'k'
    backcolor = '0.5'
    '''
    if type(axes) != list:
        axes = [axes]
    '''
    out = []
    ts = []
    for i, block in enumerate(zip(axes, g)):
        ax, pack = block
        id, s = pack
        g1 = s.index.get_level_values('group').unique()[0]
        plotblock = s.dropna().groupby(level=['tau', 'group'])\
                .size().unstack()[g1]
        plotblock = pd.DataFrame({id: plotblock})
        plotblock.plot(kind='line', ax=ax, color=color, grid=False, \
                alpha=1.0)
        ax.legend().set_visible(False)
        ax.set_ylim((0, 550))
        ax.set_xlabel('Tau', size=10, color=backcolor)
        ax.set_ylabel('Draws', size=10, color=backcolor)
        ax.tick_params(axis='both', which='major', labelsize=10,
                labelcolor=backcolor, color=backcolor)
        plt.setp(ax.spines.values(), color=backcolor)
        out.append(plotblock)
        ticks = s.dropna().groupby(level=['tau', 'group'])\
                .mean()['ticks'].unstack()[g1]
        ticks.name = id
        axT = ax.twinx()
        ticks.plot(kind='line', style='--', ax=axT, color=color, grid=False, \
                alpha=1.0)
        axT.set_xlim((0, 0.7))
        axT.set_ylabel('Iterations', size=10, color=backcolor)
        axT.tick_params(axis='both', which='major', labelsize=10,
                labelcolor=backcolor, color=backcolor)
        ts.append(ticks)
        title = id + ' | ' + walk[id]
        plt.title(title)
    d = plt.Line2D((0, 0), (1, 1), linestyle='-', c=color)
    t = plt.Line2D((0, 0), (1, 1), linestyle='--', c=color)
    plt.legend([d, t], ["Draws", "Iterations"], loc=0, frameon=False)
    f.tight_layout()
    if savefig:
        plt.savefig(savefig)
    else:
        plt.show()
    out = pd.concat(out, axis=1).fillna(0)
    ts = pd.concat(ts, axis=1)
    if saveto:
        fo = open(saveto, 'w')
        fo.write('\n\\vspace{2cm}\n')
        t0 = out.ix[:, :3].to_latex()
        fo.write(t0)
        fo.write('\n\\vspace{2cm}\n')
        t1 = out.ix[:, 3:].to_latex()
        fo.write(t1)
        fo.close()
    return out, ts
    print 'Sod shock NOT work'

#######################################################
if (showPlot):
    fig, ax = plt.subplots(3, sharex=True)
    x = []
    y = []
    yp = []
    yv = []
    for i in range(len(detector)):
        x.append(float(
            detector[i][0]))  #+0.5)#In this test case the origin is in -0.5
        y.append(float(FSrho[i][0]))
        yp.append(float(FSP[i][0]))
        yv.append(float(FSV[i][0]))
    line = plt.Line2D(x, y, color='red', linewidth=2)
    line2 = plt.Line2D(Analytical_X,
                       Analytical_Yrho,
                       color='blue',
                       linewidth=2)
    #line.text.set_color('red')
    #line.text.set_fontsize(16)
    ax[0].add_line(line)
    ax[0].add_line(line2)

    line3 = plt.Line2D(x, yp, color='red', linewidth=2)
    line4 = plt.Line2D(Analytical_X, Analytical_Yp, color='blue', linewidth=2)
    ax[1].add_line(line3)
    ax[1].add_line(line4)

    line5 = plt.Line2D(x, yv, color='red', linewidth=2)
Esempio n. 3
0
 def line2d(cls, nodesx, nodesy, color='', ls='-', lw=1):
     return plt.Line2D(nodesx, nodesy, color=color, ls=ls, lw=lw)
Esempio n. 4
0
def plot_tire_eval(xy, radius, pts, vehicle=None, path=None):
    """Plot tire configuration and evaluation points.
    Args:
        xy [n x 2]: tire locations.
        radiums [n x 1]: tire radii.
        pts [n x 2]: evaluation point locations.
        vehicle [str]: vehicle name.
        path [str]: path for saving figure. None if plot only.
    """
    xmin, ymin = xy.min(axis=0)[0], xy.min(axis=0)[1]
    xmax, ymax = xy.max(axis=0)[0], xy.max(axis=0)[1]
    xlen, ylen = xy.ptp(axis=0)[0], xy.ptp(axis=0)[1]
    xlim = [xmin - xlen, xmax + xlen]
    ylim = [ymin - ylen / 2, ymax + ylen / 2]

    fig = plt.figure()
    ax = fig.subplots()
    plt.xlim(xlim)
    plt.ylim(ylim)
    ax.set_axisbelow(True)
    ax.minorticks_on()
    plt.grid(which='major', linestyle='-', color='gray')
    plt.grid(which='minor', linestyle='--')
    ax.set_aspect(1)

    # plot tires
    for i in range(len(xy)):
        ax.add_artist(plt.Circle((xy[i, 0], xy[i, 1]), radius[i], color='b'))
        ax.add_artist(
            plt.Text(xy[i, 0],
                     xy[i, 1],
                     str(i),
                     color='white',
                     va='center',
                     ha='center'))

    # plot eval points
    ax.scatter(pts[:, 0], pts[:, 1], marker='*', color='red', zorder=5)
    for i in range(len(pts)):
        ax.add_artist(
            plt.Text(pts[i, 0],
                     pts[i, 1] - 2,
                     str(i),
                     color='black',
                     va='top',
                     ha='center'))

    # legend
    custom_legend = [
        plt.Line2D([], [], marker='o', color='blue', linestyle='None'),
        plt.Line2D([], [], marker='*', color='red', linestyle='None')
    ]
    ax.legend(custom_legend, ['Tire Location', 'Evaluation Point'],
              loc='upper right',
              framealpha=1)

    if vehicle != None:
        plt.title("Configuration of {}".format(vehicle))
    plt.xlabel("X Coordinate (in.)")
    plt.ylabel("Y Coordinate (in.)")

    if path != None:
        plt.savefig(path, dpi=300, bbox_inches='tight')
        plt.close(fig)
    else:
        plt.show()
Esempio n. 5
0
            x.append(int(row[0]))#num cluster
            y.append(int(row[1]))#iterazion
            z.append(float(row[2]))#ssim
        if int(row[1])==5:#con 5 iter
            x1.append(int(row[0]))#num cluster
            y1.append(int(row[1]))#iterazion
            z1.append(float(row[2]))#ssim
        if int(row[1])==25:#con 25 iter
            x2.append(int(row[0]))#num cluster
            y2.append(int(row[1]))#iterazion
            z2.append(float(row[2]))#ssim


plt.plot(x, z, label= "stars", color= "blue",marker='o') 
plt.plot(x1, z1, label= "stars", color= "red",marker='+') 
plt.plot(x2, z2, label= "stars", color= "green",marker='*') 


iter50 = plt.Line2D([], [], color='blue', marker='o',markersize=5, label='40 iterazioni')
iter5 = plt.Line2D([], [], color='red', marker='+',markersize=15, label='5 iterazioni')
iter25 = plt.Line2D([], [], color='green', marker='*',markersize=15, label='25 iterazioni')
plt.legend(handles=[iter5,iter25,iter50])

plt.title('Dati SSIM')
plt.xlabel('Cluster')
plt.ylabel('SSIM')

plt.xticks(np.arange(0, 72, 8)) 
plt.yticks(np.arange(0.6, 1, 0.02)) 

plt.show()
Esempio n. 6
0
                linewidth=2)
        ax.set_title(testQNames[idx])
        ax.set_xlabel(xaxes[idx])
        ax.set_ylabel(yaxes[idx])
        ax.axis([-1, 1, 0, yMaxs[idx]])
        ax.set_yticks([0.0, 0.001, 0.002, 0.003], minor=False)
        ax.yaxis.set_major_formatter(
            mtick.FixedFormatter(
                [r"$0.0$", r"$1.0$", r"$2.0$", r"$\times\ 10^{-3}$"]))
        ax.set_xticks([-1.0, -0.5, 0.0, 0.5, 1.0], minor=False)
        ax.xaxis.set_major_formatter(
            mtick.FixedFormatter(
                [r"$\minus1.0$", r"$\minus0.5$", r"$0.0$", r"$0.5$",
                 r"$1.0$"]))
    pyplot.suptitle(supTitle, fontsize="16")
    line1 = pyplot.Line2D((0, 1), (0, 0), color="blue", linewidth=2)
    line2 = pyplot.Line2D((0, 1), (0, 0), color="red", linewidth=2)
    lines, figStrs = [line1, line2], [r"$\rm{Generated}$", r"$\rm{Expected}$"]
    figure.legend(lines,
                  figStrs,
                  bbox_to_anchor=[0.5, 0.05],
                  loc='center',
                  ncol=2)
    pyplot.tight_layout()
    ##Space main title out to prevent overlapping and allow space for legend below:
    pyplot.subplots_adjust(top=0.85, bottom=0.17)
    assertions.show_graph()
    print "\nFinished testing get_quarks_theta."
    assertions.pause(__name__)

    ##Test get_quark_weights and get_quark_code:##
Esempio n. 7
0
def make_biaxial(train_feat,
                 valid_feat,
                 test_feat,
                 train_y,
                 valid_y,
                 test_y,
                 figpath,
                 xlabel=None,
                 ylabel=None,
                 add_legend=False):
    # make the biaxial figure
    sns.set_style('white')
    palette = np.array(sns.color_palette("Set2", 3))
    plt.figure(figsize=(3, 3))
    ax = plt.subplot(aspect='equal')

    # the training samples
    ax.scatter(train_feat[:, 0],
               train_feat[:, 1],
               s=30,
               alpha=.5,
               c=palette[train_y],
               marker='>',
               edgecolors='face')

    # the validation samples
    ax.scatter(valid_feat[:, 0],
               valid_feat[:, 1],
               s=30,
               alpha=.5,
               c=palette[valid_y],
               marker=(5, 1),
               edgecolors='face')

    # the test samples
    ax.scatter(test_feat[:, 0],
               test_feat[:, 1],
               s=30,
               alpha=.5,
               c=palette[test_y],
               marker='o',
               edgecolors='face')

    # http://stackoverflow.com/questions/13303928/how-to-make-custom-legend-in-matplotlib
    a1 = plt.Line2D((0, 1), (0, 0), color=palette[0])
    a2 = plt.Line2D((0, 1), (0, 0), color=palette[1])
    a3 = plt.Line2D((0, 1), (0, 0), color=palette[2])

    a4 = plt.Line2D((0, 1), (0, 0),
                    color='k',
                    marker='>',
                    linestyle='',
                    markersize=8)
    a5 = plt.Line2D((0, 1), (0, 0),
                    color='k',
                    marker=(5, 1),
                    linestyle='',
                    markersize=8)
    a6 = plt.Line2D((0, 1), (0, 0),
                    color='k',
                    marker='o',
                    linestyle='',
                    markersize=8)

    #Create legend from custom artist/label lists
    if add_legend:
        first_legend = plt.legend([a1, a2, a3], ['healthy', 'CN', 'CBF'],
                                  fontsize=16,
                                  loc=1,
                                  fancybox=True)
        plt.gca().add_artist(first_legend)
        plt.legend([a4, a5, a6], ['train', 'valid', 'test'],
                   fontsize=16,
                   loc=4,
                   fancybox=True)

    #plt.xlim(-2, 2)
    #plt.ylim(-2, 2)
    ax.set_aspect('equal', 'datalim')
    ax.margins(0.1)
    if xlabel is not None:
        plt.xlabel(xlabel, fontsize=12)
    if ylabel is not None:
        plt.ylabel(ylabel, fontsize=12)

    plt.tight_layout()
    sns.despine()
    plt.savefig(figpath, format='eps')
    plt.clf()
    plt.close()
def plot_metrics_for_all_the_algos(file_name,
                                   dest_dir_loca,
                                   lbl_plot_data_dict_ls,
                                   metrics_index,
                                   np_group_size,
                                   color_dict,
                                   marker_dict,
                                   xlable,
                                   ylable,
                                   title,
                                   x1=None,
                                   x2=None,
                                   xticks_ls=np.arange(40, 111, 10),
                                   y1=None,
                                   y2=None,
                                   ytick_ls=None,
                                   ytick_lbl_ls=None,
                                   loc=0):
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    i = 0
    eb_l = []
    l_s = []
    if len(lbl_plot_data_dict_ls) == 1:
        linestyles = ['-']
    else:
        linestyles = ['--', '-']
    for lbl_plot_data_dict in lbl_plot_data_dict_ls:
        eb_l = []
        l_s = []
        sorted_order = sorted(lbl_plot_data_dict.keys())
        for l in sorted_order:
            plot_data = lbl_plot_data_dict[l]
            # for l, plot_data in lbl_plot_data_dict.items():
            metric = plot_data[metrics_index]
            l_s.append(l)
            # plt.plot(np_group_size, metric[:,0], color=color_dict[l], marker=marker.next(), label=l)
            err = ss.t._ppf((1 + 0.95) / 2, np.subtract(metric[:, 1], 1))
            # print metric[:,1]
            eb = plt.errorbar(np_group_size,
                              metric[:, 0],
                              color=color_dict[l],
                              fmt=marker_dict[l] + linestyles[i],
                              yerr=err * metric[:, 2],
                              label=l)
            eb[-1][0].set_linestyle(linestyles[i])
            eb_l.append(eb)
            plt.xlabel(xlable)
            plt.ylabel(ylable)
            plt.title(title)
            if xticks_ls is not None:
                plt.xticks(xticks_ls)
            elif x1 is not None and x2 is not None:
                plt.xlim(x1, x2)
            else:
                ''
            if ytick_ls is not None and ytick_lbl_ls is not None:
                plt.yticks(ytick_ls, ytick_lbl_ls)
            elif ytick_ls is not None:
                plt.yticks(ytick_ls)
            elif y1 is not None and y2 is not None:
                plt.ylim(y1, y2)
            else:
                ''
        i += 1
    if i == 2:
        sim = plt.Line2D((0, 1), (0, 0), color='k', linestyle='--')
        testbed = plt.Line2D((0, 1), (0, 0), color='k', linestyle='-')
        eb_l += [sim, testbed]
        l_s += ['Simulation', 'Testbed']
    legends = ax.legend(eb_l, l_s, loc=loc, ncol=2, frameon=False)
    for t in legends.get_texts():
        if color_dict.has_key(t.get_text()):
            t.set_color(color_dict[t.get_text()])
        # print t.get_text()
    # fig.savefig(dest_dir_loca + file_name + ".svg", format='svg', dpi=1200)
    fig.savefig(dest_dir_loca + file_name + ".eps", format='eps', dpi=1200)
def warming_up_fine_tuning_history(
        warming_up_history,
        warming_up_lr,
        fine_tuning_history,
        fig1_size,
        fig2_size,
        epsilon=1e-07  # keras.backend.epsilon()
) -> Figure:
    """
    """

    try:
        warming_up_epochs = len(warming_up_history['loss'])
    except KeyError as kerr:
        raise KeyError('"warming_up_history.loss" column missing')

    ######################################################################
    ##       concatenate "warming-up" and "fine-tuning" histories       ##
    ######################################################################
    history_df = pd.concat((pd.concat(
        (pd.DataFrame(copy.deepcopy(warming_up_history)),
         pd.DataFrame({'lr': [warming_up_lr] * warming_up_epochs})),
        axis=1), pd.DataFrame(copy.deepcopy(fine_tuning_history))),
                           axis=0).reset_index(drop=True)
    history_df.index -= warming_up_epochs

    column_names = [
        'val_loss', 'val_categorical_crossentropy', 'val_categorical_accuracy',
        'val_precision', 'val_recall', 'val_precision_NEG', 'val_recall_NEG',
        'val_precision_NEUT', 'val_recall_NEUT', 'val_precision_POS',
        'val_recall_POS', 'loss', 'categorical_crossentropy',
        'categorical_accuracy', 'precision', 'recall', 'precision_NEG',
        'recall_NEG', 'precision_NEUT', 'recall_NEUT', 'precision_POS',
        'recall_POS', 'lr'
    ]
    assert (~(~pd.DataFrame(column_names).isin(history_df.columns.values)).any())[0] \
        , "check the format of the input data. at least 1 column name is missing"
    assert (len(history_df.loc[:,(column_names)].select_dtypes(include=[np.number]).columns) ==
            len(history_df.loc[:,(column_names)].columns)) \
        , "check the format of the input data. at least 1 column contains non-numeric"
    ######################################################################

    ######################################################################
    ## calculate the "F1 SCORE" columns (from "PRECISION" and "RECALL") ##
    ## f1_score = 2*(precision*recall)/(precision+recall+epsilon)       ##
    ######################################################################
    history_df['f1_score'] = (
        2 * (history_df['precision'] * history_df['recall']) /
        (history_df['precision'] + history_df['recall'] + epsilon))
    history_df['val_f1_score'] = (
        2 * (history_df['val_precision'] * history_df['val_recall']) /
        (history_df['val_precision'] + history_df['val_recall'] + epsilon))

    history_df['f1_score_NEG'] = (
        2 * (history_df['precision_NEG'] * history_df['recall_NEG']) /
        (history_df['precision_NEG'] + history_df['recall_NEG'] + epsilon))
    history_df['val_f1_score_NEG'] = (
        2 * (history_df['val_precision_NEG'] * history_df['val_recall_NEG']) /
        (history_df['val_precision_NEG'] + history_df['val_recall_NEG'] +
         epsilon))

    history_df['f1_score_NEUT'] = (
        2 * (history_df['precision_NEUT'] * history_df['recall_NEUT']) /
        (history_df['precision_NEUT'] + history_df['recall_NEUT'] + epsilon))
    history_df['val_f1_score_NEUT'] = (
        2 *
        (history_df['val_precision_NEUT'] * history_df['val_recall_NEUT']) /
        (history_df['val_precision_NEUT'] + history_df['val_recall_NEUT'] +
         epsilon))

    history_df['f1_score_POS'] = (
        2 * (history_df['precision_POS'] * history_df['recall_POS']) /
        (history_df['precision_POS'] + history_df['recall_POS'] + epsilon))
    history_df['val_f1_score_POS'] = (
        2 * (history_df['val_precision_POS'] * history_df['val_recall_POS']) /
        (history_df['val_precision_POS'] + history_df['val_recall_POS'] +
         epsilon))
    ######################################################################

    ######################################################################
    ##                      Actual figure plotting                      ##
    ##                   ('class per metric' version)                   ##
    ######################################################################
    training_linestyle = (0, (3, 1, 1, 1))

    plt.ioff()
    fig1, axes = plt.subplots(figsize=fig1_size, nrows=9, ncols=1)
    y = 1
    line = plt.Line2D([0, 1], [y, y],
                      transform=fig1.transFigure,
                      color="black")
    fig1.add_artist(line)
    nrow = 0

    def draw_metric(ax_, col_name, title):
        ax_.set_title(title)
        ax_.plot(history_df.index, history_df[col_name], label='training')
        ax_.plot(history_df.index,
                 history_df['val_' + col_name],
                 label='validation')

    def draw_class_metric(ax_, col_name, title, linestyle=None):
        ax_.set_title(title)
        ax_.plot(history_df.index,
                 history_df[col_name],
                 linestyle=linestyle,
                 label='weighted avg',
                 color='black')
        ax_.plot(history_df.index,
                 history_df[col_name + '_NEG'],
                 linestyle=linestyle,
                 label='NEGATIVE class',
                 color='red')
        ax_.plot(history_df.index,
                 history_df[col_name + '_NEUT'],
                 linestyle=linestyle,
                 label='NEUTRAL class',
                 color='#c7be1c')
        ax_.plot(history_df.index,
                 history_df[col_name + '_POS'],
                 linestyle=linestyle,
                 label='POSITIVE class',
                 color='green')
        ax_.vlines(0,
                   ymin=ax_.get_ylim()[0],
                   ymax=ax_.get_ylim()[1],
                   linewidth=.5)

    def set_common_yaxis(ax_0, ax_1):
        # make sure 'ax_0' & 'ax_1' (dealing with the same performance metric) have common y-axis bounds
        ylim = (min(ax_0.get_ylim()[0],
                    ax_1.get_ylim()[0]),
                max(ax_0.get_ylim()[1],
                    ax_1.get_ylim()[1]))
        ax_0.set_ylim(ylim)
        ax_1.set_ylim(ylim)

    def gray_out_warming_up(ax_):
        ax_.vlines(0,
                   ymin=ax_.get_ylim()[0],
                   ymax=ax_.get_ylim()[1],
                   linewidth=.5)
        ax_.axvspan(xmin=min(history_df.index),
                    xmax=0,
                    ymin=.05,
                    ymax=.95,
                    alpha=0.1,
                    color='gray')

    def horizontal_separator(fig_, ax_, r_):
        bbox = ax_.get_tightbbox(r_).transformed(fig_.transFigure.inverted())
        y = bbox.y0 - .003
        line = plt.Line2D([0, 1], [y, y],
                          transform=fig_.transFigure,
                          color="black")
        fig_.add_artist(line)

    ## LOSS ##
    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'loss', 'Training Loss')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Loss')
    ax.legend(loc="upper right", prop={'size': 8})
    gray_out_warming_up(ax)

    ## ACCURACY ##
    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'categorical_accuracy', 'Training Performance')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Categorical Accuracy')
    gray_out_warming_up(ax)

    ## CROSS-ENTROPY ##
    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'categorical_crossentropy', 'Training Performance')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Categorical Crossentropy')
    gray_out_warming_up(ax)

    fig1.tight_layout(
    )  # DO IT HERE, TO ALLOW FOR CORRECT PLACEMENT OF HORIZONTAL SEPARATORS

    # SEPARATOR
    r = fig1.canvas.get_renderer()
    horizontal_separator(fig1, ax, r)

    ## PRECISION ##
    val_ax = axes[nrow]
    nrow += 1
    draw_class_metric(val_ax, 'val_precision',
                      'Training Performance (validation dataset)')
    ax = axes[nrow]
    nrow += 1
    draw_class_metric(ax,
                      'precision',
                      'Training Performance (training dataset)',
                      linestyle=training_linestyle)
    set_common_yaxis(val_ax, ax)
    gray_out_warming_up(val_ax)
    gray_out_warming_up(ax)
    val_ax.set_xlabel('Epoch')
    val_ax.set_ylabel('Precision')
    val_ax.legend(loc="lower right", prop={'size': 7})
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Precision')

    # SEPARATOR
    horizontal_separator(fig1, ax, r)

    ## RECALL ##
    val_ax = axes[nrow]
    nrow += 1
    draw_class_metric(val_ax, 'val_recall',
                      'Training Performance (validation dataset)')
    ax = axes[nrow]
    nrow += 1
    draw_class_metric(ax,
                      'recall',
                      'Training Performance (training dataset)',
                      linestyle=training_linestyle)
    set_common_yaxis(val_ax, ax)
    gray_out_warming_up(val_ax)
    gray_out_warming_up(ax)
    val_ax.set_xlabel('Epoch')
    val_ax.set_ylabel('Recall')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Recall')

    # SEPARATOR
    horizontal_separator(fig1, ax, r)

    ## F1 SCORE ##
    val_ax = axes[nrow]
    nrow += 1
    draw_class_metric(val_ax, 'val_f1_score',
                      'Training Performance (validation dataset)')
    ax = axes[nrow]
    nrow += 1
    draw_class_metric(ax,
                      'f1_score',
                      'Training Performance (training dataset)',
                      linestyle=training_linestyle)
    set_common_yaxis(val_ax, ax)
    gray_out_warming_up(val_ax)
    gray_out_warming_up(ax)
    val_ax.set_xlabel('Epoch')
    val_ax.set_ylabel('F1 score')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('F1 score')

    # SEPARATOR
    y = -.005
    line = plt.Line2D([0, 1], [y, y],
                      transform=fig1.transFigure,
                      color="black")
    fig1.add_artist(line)
    ######################################################################

    ######################################################################
    ##                      Actual figure plotting                      ##
    ##                   ('metric per class' version)                   ##
    ######################################################################

    fig2, axes = plt.subplots(figsize=fig2_size, nrows=12, ncols=1)
    y = 1
    line = plt.Line2D([0, 1], [y, y],
                      transform=fig2.transFigure,
                      color="black")
    fig2.add_artist(line)
    nrow = 0

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'precision', 'Training Performance')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Precision')
    ax.legend(loc="upper right", prop={'size': 8})
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'precision_NEG', 'Training Performance (NEGATIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Precision')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'precision_NEUT', 'Training Performance (NEUTRAL class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Precision')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'precision_POS', 'Training Performance (POSITIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Precision')
    gray_out_warming_up(ax)

    fig2.tight_layout(
    )  # DO IT HERE, TO ALLOW FOR CORRECT PLACEMENT OF HORIZONTAL SEPARATORS

    # SEPARATOR
    horizontal_separator(fig2, ax, r)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'recall', 'Training Performance')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Recall')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'recall_NEG', 'Training Performance (NEGATIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Recall')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'recall_NEUT', 'Training Performance (NEUTRAL class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Recall')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'recall_POS', 'Training Performance (POSITIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('Recall')
    gray_out_warming_up(ax)

    # SEPARATOR
    horizontal_separator(fig2, ax, r)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'f1_score', 'Training Performance')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('F1 score')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'f1_score_NEG', 'Training Performance (NEGATIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('F1 score')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'f1_score_NEUT', 'Training Performance (NEUTRAL class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('F1 score')
    gray_out_warming_up(ax)

    ax = axes[nrow]
    nrow += 1
    draw_metric(ax, 'f1_score_POS', 'Training Performance (POSITIVE class)')
    ax.set_xlabel('Epoch')
    ax.set_ylabel('F1 score')
    gray_out_warming_up(ax)

    # SEPARATOR
    y = -.005
    line = plt.Line2D([0, 1], [y, y],
                      transform=fig2.transFigure,
                      color="black")
    fig2.add_artist(line)
    ######################################################################

    return fig1, fig2
def visualize_genealogy(genealogy,
                        weights=None,
                        rejuvenation=None,
                        colormap='jet'):
    """
    Creates an inline figure visualizing the particle genealogy over one resampling step.
    
    @params:
        genealogy           - Required  : vector describing genealogy of resampled particles, referring to indices
        weights             - Optional  : weight of particles prior to resampling
        rejuvenation        - Optional  : vector of booleans describing whether particles were rejuvenated
        colormap            - Optional  : colormap string for visualization
    """

    import numpy as np
    from IPython import get_ipython
    import matplotlib
    import matplotlib.pyplot as plt

    # Determine number of particles
    n_particles = len(genealogy)

    # Assign optional variables, if not provided
    if weights is None == True:
        weights = np.ones(n_particles)
#    if rejuvenation is None == True:
#        rejuvenation = np.ones((n_particles),dtype = np.bool)

# Switch to inline printing
    get_ipython().run_line_magic('matplotlib', 'inline')

    # Create dummy features for the legend
    full_line = plt.Line2D([], [], color='black', label='inherited')
    dashed_line = plt.Line2D([], [],
                             linestyle='--',
                             color='black',
                             label='rejuvenated')
    particle = plt.Line2D([], [],
                          linestyle='None',
                          marker='.',
                          color='black',
                          label='particle')

    # Plot legend
    plt.legend(handles=[dashed_line, full_line, particle],
               bbox_to_anchor=(0., -0.05, 1., .102),
               loc=3,
               ncol=3,
               mode="expand",
               borderaxespad=0.)

    # Determine colormap for particles
    cmap = matplotlib.cm.get_cmap(colormap)

    # Extract particle colors
    rgba = [None] * n_particles
    for n in range(n_particles):
        rgba[n] = matplotlib.colors.rgb2hex(cmap(n / (n_particles - 1)))

    # Create plot
    for n in range(n_particles):
        plt.plot([genealogy[n], n], [1, 2], '--', c=rgba[genealogy[n]])
        # Draw genealogy of current particle
        #        if rejuvenation[n] == False:
        #            plt.plot([genealogy[n],n],[1,2],c=rgba[genealogy[n]])
        #        else:
        #            plt.plot([genealogy[n],n],[1,2],c='w')
        #            plt.plot([genealogy[n],n],[1,2],'--',c=rgba[genealogy[n]])

        # Scatter previous and current particle index
        if weights[n] == 0:  # Particle weight is zero - print as greyscale
            plt.scatter(n,
                        1,
                        s=weights[n] / np.max(weights) * 55 + 5,
                        c='xkcd:medium grey')
        else:
            plt.scatter(n,
                        1,
                        s=weights[n] / np.max(weights) * 55 + 5,
                        c=rgba[n])
        plt.scatter(n, 2, s=20, c=rgba[n])

    # Deactivate axes
    plt.axis('off')

    # Show, and revert to automatic printing
    plt.show()
    get_ipython().run_line_magic('matplotlib', 'qt5')
Esempio n. 11
0
def plotParedData(dataDict):

    # loop through all the dat types.
    for dataType in dataDict.keys():
        # make empty list for the legend lines and label
        leglines = []
        leglabels = []
        # assign special parameters for each data type.

        # linear acceleration
        if dataType == 'A':
            (xAccelList, yAccelList, zAccelList) = ([], [], [])
            coordinatesList = dataDict[dataType].coordinatesList
            for (xAccel, yAccel, zAccel) in coordinatesList:
                xAccelList.append(xAccel)
                yAccelList.append(yAccel)
                zAccelList.append(zAccel)
            listOfPlotLists = [xAccelList, yAccelList, zAccelList]
            # choose the colors
            colors = ['firebrick', 'dodgerblue', 'darkorchid']
            # add things to the legend labels
            legLabelSeeds = [' x ', ' y ', ' z ']
            units = dataDict[dataType].units
            if units is None:
                units = ''
            # make a label for the y axis
            plt.ylabel("Acceleration (" + str(units) + ")")
            # make a title
            plt.title('Linear Acceleration')

        # rotation rate
        elif dataType == 'G':
            (xRotationRateList, yRotationRateList,
             zRotationRateList) = ([], [], [])
            coordinatesList = dataDict[dataType].coordinatesList
            for (xRotRate, yRotRate, zRotRate) in coordinatesList:
                xRotationRateList.append(xRotRate)
                yRotationRateList.append(yRotRate)
                zRotationRateList.append(zRotRate)
            listOfPlotLists = [
                xRotationRateList, yRotationRateList, zRotationRateList
            ]
            colors = ['DarkGoldenRod', 'OliveDrab', 'Tomato']
            # add things to the legend labels
            legLabelSeeds = [
                ' x Rotation Rate ', ' y Rotation Rate ', ' z Rotation Rate '
            ]
            units = dataDict[dataType].units
            if units is None:
                units = ''
            # make a label for the y axis
            plt.ylabel("Rotation Rate (" + str(units) + ")")
            # make a title
            plt.title('Rotation Rate About Different Axes')

        # magnetic Field
        elif dataType == 'M':
            (xMagFieldList, yMagFieldList, zMagFieldList) = ([], [], [])
            coordinatesList = dataDict[dataType].coordinatesList
            for (xMagField, yMagField, zMagField) in coordinatesList:
                xMagFieldList.append(xMagField)
                yMagFieldList.append(yMagField)
                zMagFieldList.append(zMagField)
            listOfPlotLists = [xMagFieldList, yMagFieldList, zMagFieldList]
            colors = ['CadetBlue', 'Chartreuse', 'Pink']
            # add things to the legend labels
            legLabelSeeds = [
                ' x Magnetic Field ', ' y Magnetic Field ',
                ' z Magnetic Field '
            ]
            units = dataDict[dataType].units
            if units is None:
                units = ''
            # make a label for the y axis
            plt.ylabel("Magnetic Field (" + str(units) + ")")
            # make a title
            plt.title('3 Axis Magnetic Field')

        elif dataType == 'Pitch,Roll':
            (pitchList, rollList) = ([], [])
            coordinatesList = dataDict[dataType].coordinatesList
            for (pitch, roll) in coordinatesList:
                pitchList.append(pitch)
                rollList.append(roll)

            listOfPlotLists = [pitchList, rollList]
            colors = ['Chocolate', 'CornflowerBlue']
            # add things to the legend labels
            legLabelSeeds = [' pitch ', ' roll ']
            units = dataDict[dataType].units
            if units is None:
                units = ''
            # make a label for the y axis
            plt.ylabel("Pitch and Roll " + units)
            # make a title
            plt.title('Pitch and Roll')

        else:
            coordinatesList = dataDict[dataType].coordinatesList
            dataList = []
            for datum in coordinatesList:
                dataList.append(datum)
            listOfPlotLists = [dataList]
            colors = ['Sienna']
            # add things to the legend labels
            legLabelSeeds = [' ']
            units = dataDict[dataType].units
            if units is None:
                units = ''
            # make a label for the y axis
            plt.ylabel(dataType + units)
            # make a title
            plt.title(dataType)

        for (listIndex, dataList) in list(enumerate(listOfPlotLists)):
            color = colors[listIndex]
            # append the legend label to a list to use later.
            leglabels.append(dataType + ':' + legLabelSeeds[listIndex])
            # append the legend line to a list to use later.
            leglines.append(plt.Line2D(range(10), range(10), color=color))
            # plot the data
            plt.plot(dataList, color=color)

        # make the legend
        plt.legend(leglines, leglabels, loc=0, numpoints=3, handlelength=4)

        # make a label for the x axis
        plt.xlabel("Number of points")

        # display the plot in pop-up window
        plt.show()
    return
Esempio n. 12
0
    def draw_neural_net(self,error=False):
        cmap = plt.get_cmap('seismic')
        left=0.1
        right=0.91
        bottom=0.1
        top=0.91
        fig = plt.figure(figsize=(12, 12))
        ax = fig.gca()
        ax.axis('off')
        layer_sizes = list(self.topology.values())
        n_layers = len(layer_sizes)
        v_spacing = (top - bottom)/float(max(layer_sizes))
        h_spacing = (right - left)/float(len(layer_sizes) - 1)
        
        # Input-Arrows
        layer_top_0 = v_spacing*(layer_sizes[0] - 1)/2. + (top + bottom)/2.
        for m in range(layer_sizes[0]):
            plt.arrow(left-0.18, layer_top_0 - m*v_spacing, 0.12, 0,  lw =1, head_width=0.01, head_length=0.02)

        # Nodes
        for n, layer_size in enumerate(layer_sizes):
            layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2.
            for m in range(layer_size):
                    #format_value = "{:.2}".format(float(self.network[n][m].error))
                    #c_color = 1-float(self.network[n][m].a_value)
                format_value = "a-{:.2}\nz-{:.2}\ne-{:.2}".format(
                    float(self.network[n][m].a_value),
                    float(self.network[n][m].z_value),
                    float(self.network[n][m].error))
                c_color = 1-float(self.network[n][m].a_value)
                #print(format_value)
                circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/8.,
                                    color='black', ec='k', zorder=4)
                if n == 0:
                    plt.text(left-0.125, layer_top - m*v_spacing, r'$X_{'+str(m)+'}$', fontsize=15)
                elif (n_layers > 2) & (n > 0) & (n < n_layers):
                    
                    plt.text(n*h_spacing + left+0.00, layer_top - m*v_spacing+ (v_spacing/8.+0.01*v_spacing), r'$H_{'+str(m)+'}$', fontsize=15)
                elif n == n_layers -1:
                    plt.text(n*h_spacing + left+0.10, layer_top - m*v_spacing, r'$y_{'+str(m)+'}$', fontsize=15)
                ax.add_artist(circle)
                ax.annotate(
                        format_value, xy=(n*h_spacing + left, layer_top - m*v_spacing), fontsize=12,ha='center', va="center",zorder=5,color='white',fontweight='bold')

                #     # Bias-Nodes
        for n, layer_size in enumerate(layer_sizes):
            if n < n_layers -1:
                x_bias = (n+0.5)*h_spacing + left
                y_bias = top + 0.005
                circle = plt.Circle((x_bias, y_bias), v_spacing/8., color='w', ec='k', zorder=4)
                plt.text(x_bias-(v_spacing/8.+0.10*v_spacing+0.01), y_bias, r'$\beta$', fontsize=15)
                ax.add_artist(circle)   
        # Edges
        # Edges between nodes
        for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
            layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
            layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
            for m in range(layer_size_a):
                for o in range(layer_size_b):
                    thickness = self.network[n+1][o].weights[m]
                    line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left],
                                      [layer_top_a - m*v_spacing, layer_top_b - o*v_spacing], c='black',lw=thickness*2+0.1)
                    ax.add_artist(line)
                    xm = (n*h_spacing + left)
                    xo = ((n + 1)*h_spacing + left)
                    ym = (layer_top_a - m*v_spacing)
                    yo = (layer_top_b - o*v_spacing)
                    rot_mo_rad = np.arctan((yo-ym)/(xo-xm))
                    rot_mo_deg = rot_mo_rad*180./np.pi
                    xm1 = xm + (v_spacing/8.+0.05)*np.cos(rot_mo_rad)
                    if n == 0:
                        if yo > ym:
                            ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
                        else:
                            ym1 = ym + (v_spacing/8.+0.05)*np.sin(rot_mo_rad)
                    else:
                        if yo > ym:
                            ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
                        else:
                            ym1 = ym + (v_spacing/8.+0.04)*np.sin(rot_mo_rad)
                    plt.text( xm1, ym1,\
                              "{}  {:.3}".format(
                        self.network[n+1][o].weights_annotations[m],
                        self.network[n+1][o].weights[m]),\
                              rotation = rot_mo_deg, \
                              fontsize = 15,
                            )
        # Edges between bias and nodes
        for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
            if n < n_layers-1:
                layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
                layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
            x_bias = (n+0.5)*h_spacing + left
            y_bias = top + 0.005 
            for o in range(layer_size_b):
                line = plt.Line2D([x_bias, (n + 1)*h_spacing + left],
                              [y_bias, layer_top_b - o*v_spacing], c='k')
                ax.add_artist(line)
                xo = ((n + 1)*h_spacing + left)
                yo = (layer_top_b - o*v_spacing)
                rot_bo_rad = np.arctan((yo-y_bias)/(xo-x_bias))
                rot_bo_deg = rot_bo_rad*180./np.pi
                xo2 = xo - (v_spacing/8.+0.01)*np.cos(rot_bo_rad)
                yo2 = yo - (v_spacing/8.+0.01)*np.sin(rot_bo_rad)
                xo1 = xo2 -0.05 *np.cos(rot_bo_rad)
                yo1 = yo2 -0.05 *np.sin(rot_bo_rad)
                plt.text( xo1, yo1,\
                     str(round(float(self[n+1][o].bias),4)),\
                     rotation = rot_bo_deg, \
                     fontsize = 10)    

        layer_top_0 = v_spacing*(layer_sizes[-1] - 1)/2. + (top + bottom)/2.
        for m in range(layer_sizes[-1]):
            plt.arrow(right+0.015, layer_top_0 - m*v_spacing, 0.16*h_spacing, 0,  lw =1, head_width=0.01, head_length=0.02)
        return ax
Esempio n. 13
0
def plot_gate_map(backend,
                  figsize=None,
                  plot_directed=False,
                  label_qubits=True,
                  qubit_size=24,
                  line_width=4,
                  font_size=12,
                  qubit_color=None,
                  qubit_labels=None,
                  line_color=None,
                  font_color='w',
                  ax=None):
    """Plots the gate map of a device.

    Args:
        backend (BaseBackend): A backend instance,
        figsize (tuple): Output figure size (wxh) in inches.
        plot_directed (bool): Plot directed coupling map.
        label_qubits (bool): Label the qubits.
        qubit_size (float): Size of qubit marker.
        line_width (float): Width of lines.
        font_size (int): Font size of qubit labels.
        qubit_color (list): A list of colors for the qubits
        qubit_labels (list): A list of qubit labels
        line_color (list): A list of colors for each line from coupling_map.
        font_color (str): The font color for the qubit labels.
        ax (Axes): A Matplotlib axes instance.

    Returns:
        Figure: A Matplotlib figure instance.

    Raises:
        QiskitError: if tried to pass a simulator.
        ImportError: if matplotlib not installed.
    """
    if not HAS_MATPLOTLIB:
        raise ImportError('Must have Matplotlib installed.')

    if backend.configuration().simulator:
        raise QiskitError('Requires a device backend, not simulator.')

    input_axes = False
    if ax:
        input_axes = True

    mpl_data = {}

    mpl_data[20] = [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1],
                    [1, 2], [1, 3], [1, 4], [2, 0], [2, 1], [2, 2], [2, 3],
                    [2, 4], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4]]

    mpl_data[14] = [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6],
                    [1, 7], [1, 6], [1, 5], [1, 4], [1, 3], [1, 2], [1, 1]]

    mpl_data[16] = [[1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
                    [0, 6], [0, 7], [1, 7], [1, 6], [1, 5], [1, 4], [1, 3],
                    [1, 2], [1, 1]]

    mpl_data[5] = [[1, 0], [0, 1], [1, 1], [1, 2], [2, 1]]

    mpl_data[53] = [[0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [1, 2], [1, 6],
                    [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6],
                    [2, 7], [2, 8], [3, 0], [3, 4], [3, 8], [4, 0], [4, 1],
                    [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7], [4, 8],
                    [5, 2], [5, 6], [6, 0], [6, 1], [6, 2], [6, 3], [6, 4],
                    [6, 5], [6, 6], [6, 7], [6, 8], [7, 0], [7, 4], [7, 8],
                    [8, 0], [8, 1], [8, 2], [8, 3], [8, 4], [8, 5], [8, 6],
                    [8, 7], [8, 8], [9, 2], [9, 6]]

    config = backend.configuration()
    n_qubits = config.n_qubits
    cmap = config.coupling_map

    if qubit_labels is None:
        qubit_labels = list(range(n_qubits))
    else:
        if len(qubit_labels) != n_qubits:
            raise QiskitError('Length of qubit labels '
                              'does not equal number '
                              'of qubits.')

    if n_qubits in mpl_data.keys():
        grid_data = mpl_data[n_qubits]
    else:
        if not input_axes:
            fig, ax = plt.subplots(figsize=(5, 5))  # pylint: disable=invalid-name
            ax.axis('off')
            return fig

    x_max = max([d[1] for d in grid_data])
    y_max = max([d[0] for d in grid_data])
    max_dim = max(x_max, y_max)

    if figsize is None:
        if x_max / max_dim > 0.33 and y_max / max_dim > 0.33:
            figsize = (5, 5)
        else:
            figsize = (9, 3)

    if ax is None:
        fig, ax = plt.subplots(figsize=figsize)  # pylint: disable=invalid-name
        ax.axis('off')

    # set coloring
    if qubit_color is None:
        qubit_color = ['#648fff'] * config.n_qubits
    if line_color is None:
        line_color = ['#648fff'] * len(cmap)

    # Add lines for couplings
    for ind, edge in enumerate(cmap):
        is_symmetric = False
        if edge[::-1] in cmap:
            is_symmetric = True
        y_start = grid_data[edge[0]][0]
        x_start = grid_data[edge[0]][1]
        y_end = grid_data[edge[1]][0]
        x_end = grid_data[edge[1]][1]

        if is_symmetric:
            if y_start == y_end:
                x_end = (x_end - x_start) / 2 + x_start

            elif x_start == x_end:
                y_end = (y_end - y_start) / 2 + y_start

            else:
                x_end = (x_end - x_start) / 2 + x_start
                y_end = (y_end - y_start) / 2 + y_start
        ax.add_artist(
            plt.Line2D([x_start, x_end], [-y_start, -y_end],
                       color=line_color[ind],
                       linewidth=line_width,
                       zorder=0))
        if plot_directed:
            dx = x_end - x_start  # pylint: disable=invalid-name
            dy = y_end - y_start  # pylint: disable=invalid-name
            if is_symmetric:
                x_arrow = x_start + dx * 0.95
                y_arrow = -y_start - dy * 0.95
                dx_arrow = dx * 0.01
                dy_arrow = -dy * 0.01
                head_width = 0.15
            else:
                x_arrow = x_start + dx * 0.5
                y_arrow = -y_start - dy * 0.5
                dx_arrow = dx * 0.2
                dy_arrow = -dy * 0.2
                head_width = 0.2
            ax.add_patch(
                mpatches.FancyArrow(x_arrow,
                                    y_arrow,
                                    dx_arrow,
                                    dy_arrow,
                                    head_width=head_width,
                                    length_includes_head=True,
                                    edgecolor=None,
                                    linewidth=0,
                                    facecolor=line_color[ind],
                                    zorder=1))

    # Add circles for qubits
    for var, idx in enumerate(grid_data):
        _idx = [idx[1], -idx[0]]
        width = _GraphDist(qubit_size, ax, True)
        height = _GraphDist(qubit_size, ax, False)
        ax.add_artist(
            mpatches.Ellipse(_idx,
                             width,
                             height,
                             color=qubit_color[var],
                             zorder=1))
        if label_qubits:
            ax.text(*_idx,
                    s=qubit_labels[var],
                    horizontalalignment='center',
                    verticalalignment='center',
                    color=font_color,
                    size=font_size,
                    weight='bold')
    ax.set_xlim([-1, x_max + 1])
    ax.set_ylim([-(y_max + 1), 1])
    if not input_axes:
        if get_backend() in [
                'module://ipykernel.pylab.backend_inline', 'nbAgg'
        ]:
            plt.close(fig)
        return fig
    return None
Esempio n. 14
0
ys = [y0]

for t in np.arange(0, 60, 0.01):

    # Equations of motion
    v_t = g * t + v0  # update the velocity of the particle at point time t
    y_t = y0 + (v_t**2 - v0**2) / (
        2 * g)  #update the position of the particle at time t

    # Print out the time elapsed and the speed
    time_passed.set_text("Time Elapsed (s): {0}".format(np.round(t, 2)))
    speed.set_text("Speed (m/s): {0}".format(np.abs(np.round(v_t, 2))))

    if y_t >= 0:
        # update the point with the new position
        points.set_data(x0, y_t)

        #draw the t,y line
        ts.append(t)
        ys.append(y_t)
        line_seg = plt.Line2D(ts, ys)
        ax[1].add_line(line_seg)

    else:
        # exit the loop if the particle hits the ground at 0
        points.set_data(x0, 0)
        break
    plt.pause(0.001)

plt.draw()
Esempio n. 15
0
def plot_variant_locator(pos,
                         step=None,
                         ax=None,
                         start=None,
                         stop=None,
                         flip=False,
                         line_kwargs=None):
    """
    Plot lines indicating the physical genome location of variants from a
    single chromosome/contig. By default the top x axis is in variant index
    space, and the bottom x axis is in genome position space.

    Parameters
    ----------

    pos : array_like
        A sorted 1-dimensional array of genomic positions from a single
        chromosome/contig.
    step : int, optional
        Plot a line for every `step` variants.
    ax : axes, optional
        The axes on which to draw. If not provided, a new figure will be
        created.
    start : int, optional
        The start position for the region to draw.
    stop : int, optional
        The stop position for the region to draw.
    flip : bool, optional
        Flip the plot upside down.
    line_kwargs : dict-like
        Additional keyword arguments passed through to `plt.Line2D`.

    Returns
    -------

    ax : axes
        The axes on which the plot was drawn

    """

    import matplotlib.pyplot as plt

    # check inputs
    pos = SortedIndex(pos, copy=False)

    # set up axes
    if ax is None:
        x = plt.rcParams['figure.figsize'][0]
        y = x / 7
        fig, ax = plt.subplots(figsize=(x, y))
        fig.tight_layout()

    # determine x axis limits
    if start is None:
        start = np.min(pos)
    if stop is None:
        stop = np.max(pos)
    loc = pos.locate_range(start, stop)
    pos = pos[loc]
    if step is None:
        step = len(pos) // 100
    ax.set_xlim(start, stop)

    # plot the lines
    if line_kwargs is None:
        line_kwargs = dict()
    # line_kwargs.setdefault('linewidth', .5)
    n_variants = len(pos)
    for i, p in enumerate(pos[::step]):
        xfrom = p
        xto = (start + ((i * step / n_variants) * (stop - start)))
        l = plt.Line2D([xfrom, xto], [0, 1], **line_kwargs)
        ax.add_line(l)

    # invert?
    if flip:
        ax.invert_yaxis()
        ax.xaxis.tick_top()
    else:
        ax.xaxis.tick_bottom()

    # tidy up
    ax.set_yticks([])
    ax.xaxis.set_tick_params(direction='out')
    for l in 'left', 'right':
        ax.spines[l].set_visible(False)

    return ax
Esempio n. 16
0
def plotOpticalDups():
	#read in optical duplicates file, extract info
	data = []
	with open("optical_duplicates.txt", "r") as f:
		for line in f:
			if not line.strip():
				continue
			else:
				try:
					optRecord = line.split()
					t = optRecord[2]
				except IndexError:
					break
				optRecord = line.split()
				read = optRecord[0].split(":")
				tile = read[4]
				x = read[5]
				y = read[6]
				sampleID = read[0]
				data.append([tile, sampleID, x, y])
			dfOptDups = DataFrame(data)
			dfOptDups.columns = ['tile', 'sampleID', 'x', 'y']
	tileGroups = dfOptDups.groupby('tile')
	tiles = []
	for name, group in tileGroups:
		tiles.append(name)
	
	#initalize values
	git = 0 #group iterator
	fontP = FontProperties()
	fontP.set_size('small')

	#get colors, x y values for group
	for i in range(0, len(tiles)):
		colorDict = {}
		currentGroup = tileGroups.get_group(tiles[git])
		currentName = currentGroup.tile[0]
		x = map(int, currentGroup.x.tolist())
		y = map(int, currentGroup.y.tolist())
		xnorm = [float(i)/sum(x) for i in x]
		ynorm = [float(i)/sum(y) for i in y]
		
		for i in range(0, len(currentGroup.sampleID.unique())):
			colorDict[currentGroup.sampleID.unique()[i]] = matplotlib.colors.cnames.values()[random.randrange(1, 150)] #link random color to each sample
		#loop to create figures	
		fig = plt.figure()
		backgroundColor = '#ffffff'
		ax = fig.add_subplot(111, axisbg=backgroundColor)
		ax.scatter(xnorm, ynorm, color=colorDict.values(), marker=',', s=5)
		ax.spines['top'].set_visible(False) #remove plot borders
		ax.spines['right'].set_visible(False)
		ax.spines['bottom'].set_visible(False)
		ax.spines['left'].set_visible(False)
		plt.xlim(0, 1.0)
		plt.ylim(0, 1.0)
		plt.tick_params(bottom='off', top='off', right='off', left='off') #no tick marks
		plt.grid(color="white")
		markers = [plt.Line2D([0,0], [0,0], color=color, marker='s', linestyle='') for color in colorDict.values()]
		lgd = plt.legend(markers, colorDict.keys(), numpoints=1, prop=fontP, bbox_to_anchor=(0.5, -0.1), ncol=5, fancybox=True)
		fig.savefig(currentName + '.pdf', bbox_extra_artists=(lgd,), bbox_inches='tight', format='pdf')
		git += 1	
	print bcolors.COMPLETE + "Complete! Figures successfully generated"
Esempio n. 17
0
def plot(simtable, associations, cmap, mask, axlabels, outfile, similarity):
    # reverse roworder of simtable to match plotting
    order = range(simtable.nrows)[::-1]
    simtable.rowheads = reorder(simtable.rowheads, order)
    simtable.data = simtable.data[order, :]
    simtable.update()
    # scale of the data
    tmin = np.min(simtable.data)
    tmax = np.max(simtable.data)
    crit = 0
    while crit < max(abs(tmin), tmax):
        crit += c_simstep
    vmin = 0 if tmin >= 0 else -crit
    vmax = crit
    # masking
    if mask:
        mask_table(simtable, associations)
    # begin plotting
    fig = plt.figure()
    width = max(c_unit_w * simtable.ncols, c_min_width)
    width += c_char_pad * max(list(map(len, simtable.rowheads)))
    height = max(c_unit_h * simtable.nrows, c_min_height)
    height += c_char_pad * max(list(map(len, simtable.colheads)))
    fig.set_size_inches(width, height)
    span = simtable.ncols
    cbarspan = c_cbarspan
    ax = plt.subplot2grid((1, span), (0, cbarspan),
                          rowspan=1,
                          colspan=span - cbarspan)
    ax_cbar = plt.subplot2grid((2, span), (0, 0), rowspan=2, colspan=cbarspan)
    ax.yaxis.tick_right()
    ax.yaxis.set_label_position("right")
    ax.set_yticks([0.5 + i for i in range(simtable.nrows)])
    ax.set_xticks([0.5 + i for i in range(simtable.ncols)])
    ax.set_yticklabels(simtable.rowheads, size=c_large_text)
    ax.set_xticklabels(simtable.colheads,
                       rotation=90,
                       rotation_mode="anchor",
                       ha="right",
                       va="center",
                       size=c_large_text)
    ax.xaxis.set_ticks_position('none')
    ax.yaxis.set_ticks_position('none')
    ax.set_ylim(0, len(simtable.rowheads))
    ax.set_xlim(0, len(simtable.colheads))
    ax.set_ylabel(axlabels[0], size=c_giant_text, fontweight='bold')
    ax.set_xlabel(axlabels[1], size=c_giant_text, fontweight='bold')
    # if masking, draw a light grid to help with orientation
    if mask:
        kwargs = {"zorder": 0, "color": c_grid_color}
        xmin, xmax = ax.get_xlim()
        ymin, ymax = ax.get_ylim()
        for y in ax.get_yticks():
            ax.add_line(plt.Line2D([xmin, xmax], [y, y], **kwargs))
        for x in ax.get_xticks():
            ax.add_line(plt.Line2D([x, x], [ymin, ymax], **kwargs))
    # main heatmap
    heatmap = ax.pcolormesh(simtable.data, cmap=cmap, vmin=vmin, vmax=vmax)
    # craziness for getting cbar on the left with left-facing ticks
    norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
    cbar = matplotlib.colorbar.ColorbarBase(
        ax_cbar,
        norm=norm,
        cmap=cmap,
        orientation="vertical",
    )
    cbar.set_ticks([])
    twin_ax = plt.twinx(ax=cbar.ax)
    twin_ax.yaxis.set_label_position("left")
    twin_ax.yaxis.tick_left()
    [tick.set_size(c_large_text) for tick in twin_ax.get_yticklabels()]
    twin_ax.set_ylim(vmin, vmax)
    if similarity != "Pairwise Similarity":
        similarity = "Pairwise " + similarity
    twin_ax.set_ylabel(similarity,
                       size=c_giant_text)  #, fontsize=c_large_text )
    ticks = [vmin]
    while ticks[-1] < vmax:
        ticks.append(ticks[-1] + c_simstep)
    twin_ax.set_yticks(ticks)
    #
    # number is used as rank of the association based on the order
    number = 0
    # add associations
    for sim_rank, row_items, col_items, sig, _, _ in associations:
        number += 1
        row_items = row_items[::-1]
        y1 = simtable.rowmap[row_items[0]]
        y2 = simtable.rowmap[row_items[-1]]
        x1 = simtable.colmap[col_items[0]]
        x2 = simtable.colmap[col_items[-1]]
        delx = abs(x2 - x1) + 1
        dely = abs(y2 - y1) + 1
        # box
        ax.add_patch(
            patches.Rectangle(
                (x1, y1),
                x2 - x1 + 1,
                y2 - y1 + 1,
                facecolor="none",
                edgecolor="black",
                linewidth=c_line_width,
                clip_on=False,
            ))
        # label
        text = str(number)
        size = c_label_scale * min(delx, dely)
        size /= 1 if len(text) == 1 else c_label_aspect * len(text)
        size = int(size)
        text = ax.text(#np.mean( [x1, x2] )+0.5+c_label_shift*size,
            np.mean( [x1, x2] )+.75+c_label_shift*size if (len(row_items)%2 != 0 and len(row_items)>1 and len(col_items) >1)  else\
            np.mean( [x1, x2] )+.5+c_label_shift*size if len(row_items) == 1 else np.mean( [x1, x2] )+0.5+c_label_shift*size ,
            np.mean( [y1, y2] )+0.5+c_label_shift*size,
            text,
            size=size,
            color="white",
            ha="center",
            va="center",
            weight="bold",
        )
        text.set_path_effects([
            path_effects.Stroke(linewidth=c_outline_width, foreground='black'),
            path_effects.Normal(),
        ])
    # craziness for hiding the border
    plt.setp([
        child for child in ax.get_children()
        if isinstance(child, matplotlib.spines.Spine)
    ],
             visible=False)
    try:
        plt.tight_layout()
    except:
        pass
    plt.savefig(outfile, dpi=paper_dpi, papertype=None)
Esempio n. 18
0
    def _plot_configure(self):
        """
        Creates a figure with subplots for states and actions.

        :return: (figure, axes) where:
            figure: a matplotlib Figure with subplots for state and controls
            axes: an AxesTuple object with references to all figure subplot axes
        """
        plt.ion()  # interactive mode allows dynamic updating of plot
        figure = plt.figure(figsize=(6, 11))

        spec = plt.GridSpec(nrows=3,
                            ncols=2,
                            width_ratios=[5, 1],  # second column very thin
                            height_ratios=[6, 5, 1],  # bottom row very short
                            wspace=0.3)

        # create subplots
        axes_state = figure.add_subplot(spec[0, 0:])
        axes_stick = figure.add_subplot(spec[1, 0])
        axes_throttle = figure.add_subplot(spec[1, 1])
        axes_rudder = figure.add_subplot(spec[2, 0])

        # hide state subplot axes - text will be printed to it
        axes_state.axis('off')
        self._prepare_state_printing(axes_state)

        # config subplot for stick (aileron and elevator control in x/y axes)
        axes_stick.set_xlabel('ailerons [-]', )
        axes_stick.set_ylabel('elevator [-]')
        axes_stick.set_xlim(left=-1, right=1)
        axes_stick.set_ylim(bottom=-1, top=1)
        axes_stick.xaxis.set_label_coords(0.5, 1.08)
        axes_stick.yaxis.set_label_coords(-0.05, 0.5)
        # make axes cross at origin
        axes_stick.spines['left'].set_position('zero')
        axes_stick.spines['bottom'].set_position('zero')
        # only show ticks at extremes of range
        axes_stick.set_xticks([-1, 1])
        axes_stick.xaxis.set_ticks_position('bottom')
        axes_stick.set_yticks([-1, 1])
        axes_stick.yaxis.set_ticks_position('left')
        axes_stick.tick_params(which='both', direction='inout')
        # show minor ticks throughout
        minor_locator = plt.MultipleLocator(0.2)
        axes_stick.xaxis.set_minor_locator(minor_locator)
        axes_stick.yaxis.set_minor_locator(minor_locator)
        # hide unneeded spines
        axes_stick.spines['right'].set_visible(False)
        axes_stick.spines['top'].set_visible(False)

        # config subplot for throttle: a 1D vertical plot
        axes_throttle.set_ylabel('throttle [-]')
        axes_throttle.set_ylim(bottom=0, top=1)
        axes_throttle.set_xlim(left=0, right=1)
        axes_throttle.spines['left'].set_position('zero')
        axes_throttle.yaxis.set_label_coords(0.5, 0.5)
        axes_throttle.set_yticks([0, 0.5, 1])
        axes_throttle.yaxis.set_minor_locator(minor_locator)
        axes_throttle.tick_params(axis='y', which='both', direction='inout')
        # hide horizontal x-axis and related spines
        axes_throttle.xaxis.set_visible(False)
        for spine in ['right', 'bottom', 'top']:
            axes_throttle.spines[spine].set_visible(False)

        # config rudder subplot: 1D horizontal plot
        axes_rudder.set_xlabel('rudder [-]')
        axes_rudder.set_xlim(left=-1, right=1)
        axes_rudder.set_ylim(bottom=0, top=1)
        axes_rudder.xaxis.set_label_coords(0.5, -0.5)
        axes_stick.spines['bottom'].set_position('zero')
        axes_rudder.set_xticks([-1, 0, 1])
        axes_rudder.xaxis.set_minor_locator(minor_locator)
        axes_rudder.tick_params(axis='x', which='both', direction='inout')
        axes_rudder.get_yaxis().set_visible(False)  # only want a 1D subplot
        for spine in ['left', 'right', 'top']:
            axes_rudder.spines[spine].set_visible(False)

        all_axes = AxesTuple(axes_state=axes_state,
                             axes_stick=axes_stick,
                             axes_throttle=axes_throttle,
                             axes_rudder=axes_rudder)

        # create figure-wide legend
        cmd_entry = (
            plt.Line2D([], [], color='b', marker='o', ms=10, linestyle='', fillstyle='none'),
            'Commanded Position, normalised')
        pos_entry = (plt.Line2D([], [], color='r', marker='+', ms=10, linestyle=''),
                     'Current Position, normalised')
        figure.legend((cmd_entry[0], pos_entry[0]),
                      (cmd_entry[1], pos_entry[1]),
                      loc='lower center')

        plt.show()
        plt.pause(self.PLOT_PAUSE_SECONDS)  # voodoo pause needed for figure to appear

        return figure, all_axes
                label='Series C\'s = %.1f pF, Shunt L = %i nH, %s' %
                (cap_value * 1e12, shunt_inductor_value * 1e9, antenna_name))
            if plot_smith == True:
                all_ax_sm = feed.plotSmithChart(ax=all_ax_sm,
                                                linestyle=linestyle)

        all_ax_lm.set_xlim(0, 1000)
        all_ax_lm.set_ylim(logmag_lims[0], logmag_lims[1])
        #Handle Legend
        #Get artists and labels for legend and chose which ones to display
        handles, labels = all_ax_lm.get_legend_handles_labels()
        display = numpy.arange(len(handles))  #(0,len(cu_roots))

        #Create custom artists
        cuArtist = plt.Line2D((0, 1), (0, 0),
                              color='k',
                              linestyle=cu_linestyle,
                              linewidth=linewidth)
        alArtist = plt.Line2D((0, 1), (0, 0),
                              color='k',
                              linestyle=al_linestyle,
                              linewidth=linewidth)

        #Create legend from custom artist/label lists
        all_ax_lm.legend(
            [handle for i, handle in enumerate(handles) if i in display] +
            [cuArtist, alArtist],
            [label for i, label in enumerate(labels) if i in display] +
            ['Copper Tabbed Antenna', 'Aluminum Antenna'],
            fontsize=legend_fontsize,
            loc='lower left')
Esempio n. 20
0
    def render(self, mode='human', output_file=None):
        from matplotlib import animation
        import matplotlib.pyplot as plt
        plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'

        x_offset = 0.11
        y_offset = 0.11
        cmap = plt.cm.get_cmap('hsv', 10)
        robot_color = 'yellow'
        goal_color = 'red'
        arrow_color = 'red'
        arrow_style = patches.ArrowStyle("->", head_length=4, head_width=2)

        if mode == 'human':
            fig, ax = plt.subplots(figsize=(7, 7))
            ax.set_xlim(-4, 4)
            ax.set_ylim(-4, 4)
            for human in self.humans:
                human_circle = plt.Circle(human.get_position(),
                                          human.radius,
                                          fill=False,
                                          color='b')
                ax.add_artist(human_circle)
            ax.add_artist(
                plt.Circle(self.robot.get_position(),
                           self.robot.radius,
                           fill=True,
                           color='r'))
            plt.show()
        elif mode == 'traj':
            fig, ax = plt.subplots(figsize=(7, 7))
            ax.tick_params(labelsize=16)
            ax.set_xlim(-5, 5)
            ax.set_ylim(-5, 5)
            ax.set_xlabel('x(m)', fontsize=16)
            ax.set_ylabel('y(m)', fontsize=16)

            robot_positions = [
                self.states[i][0].position for i in range(len(self.states))
            ]
            human_positions = [[
                self.states[i][1][j].position for j in range(len(self.humans))
            ] for i in range(len(self.states))]
            for k in range(len(self.states)):
                if k % 4 == 0 or k == len(self.states) - 1:
                    robot = plt.Circle(robot_positions[k],
                                       self.robot.radius,
                                       fill=True,
                                       color=robot_color)
                    humans = [
                        plt.Circle(human_positions[k][i],
                                   self.humans[i].radius,
                                   fill=False,
                                   color=cmap(i))
                        for i in range(len(self.humans))
                    ]
                    ax.add_artist(robot)
                    for human in humans:
                        ax.add_artist(human)
                # add time annotation
                global_time = k * self.time_step
                if global_time % 4 == 0 or k == len(self.states) - 1:
                    agents = humans + [robot]
                    times = [
                        plt.text(agents[i].center[0] - x_offset,
                                 agents[i].center[1] - y_offset,
                                 '{:.1f}'.format(global_time),
                                 color='black',
                                 fontsize=14)
                        for i in range(self.human_num + 1)
                    ]
                    for time in times:
                        ax.add_artist(time)
                if k != 0:
                    nav_direction = plt.Line2D(
                        (self.states[k - 1][0].px, self.states[k][0].px),
                        (self.states[k - 1][0].py, self.states[k][0].py),
                        color=robot_color,
                        ls='solid')
                    human_directions = [
                        plt.Line2D((self.states[k - 1][1][i].px,
                                    self.states[k][1][i].px),
                                   (self.states[k - 1][1][i].py,
                                    self.states[k][1][i].py),
                                   color=cmap(i),
                                   ls='solid') for i in range(self.human_num)
                    ]
                    ax.add_artist(nav_direction)
                    for human_direction in human_directions:
                        ax.add_artist(human_direction)
            plt.legend([robot], ['Robot'], fontsize=16)
            plt.show()
        elif mode == 'video':
            fig, ax = plt.subplots(figsize=(7, 7))
            ax.tick_params(labelsize=16)
            ax.set_xlim(-6, 6)
            ax.set_ylim(-6, 6)
            ax.set_xlabel('x(m)', fontsize=16)
            ax.set_ylabel('y(m)', fontsize=16)

            # add robot and its goal
            robot_positions = [state[0].position for state in self.states]
            goal = mlines.Line2D([0], [4],
                                 color=goal_color,
                                 marker='*',
                                 linestyle='None',
                                 markersize=15,
                                 label='Goal')
            robot = plt.Circle(robot_positions[0],
                               self.robot.radius,
                               fill=True,
                               color=robot_color)
            ax.add_artist(robot)
            ax.add_artist(goal)
            plt.legend([robot, goal], ['Robot', 'Goal'], fontsize=16)

            # add humans and their numbers
            human_positions = [[
                state[1][j].position for j in range(len(self.humans))
            ] for state in self.states]
            humans = [
                plt.Circle(human_positions[0][i],
                           self.humans[i].radius,
                           fill=False) for i in range(len(self.humans))
            ]
            human_numbers = [
                plt.text(humans[i].center[0] - x_offset,
                         humans[i].center[1] - y_offset,
                         str(i),
                         color='black',
                         fontsize=12) for i in range(len(self.humans))
            ]
            for i, human in enumerate(humans):
                ax.add_artist(human)
                ax.add_artist(human_numbers[i])

            # add time annotation
            time = plt.text(-1, 5, 'Time: {}'.format(0), fontsize=16)
            ax.add_artist(time)

            # compute attention scores
            if self.attention_weights is not None:
                attention_scores = [
                    plt.text(-5.5,
                             5 - 0.5 * i,
                             'Human {}: {:.2f}'.format(
                                 i + 1, self.attention_weights[0][i]),
                             fontsize=16) for i in range(len(self.humans))
                ]

            # compute orientation in each step and use arrow to show the direction
            radius = self.robot.radius
            if self.robot.kinematics == 'unicycle':
                orientation = [
                    ((state[0].px, state[0].py),
                     (state[0].px + radius * np.cos(state[0].theta),
                      state[0].py + radius * np.sin(state[0].theta)))
                    for state in self.states
                ]
                orientations = [orientation]
            else:
                orientations = []
                for i in range(self.human_num + 1):
                    orientation = []
                    for state in self.states:
                        if i == 0:
                            agent_state = state[0]
                        else:
                            agent_state = state[1][i - 1]
                        theta = np.arctan2(agent_state.vy, agent_state.vx)
                        orientation.append(
                            ((agent_state.px, agent_state.py),
                             (agent_state.px + radius * np.cos(theta),
                              agent_state.py + radius * np.sin(theta))))
                    orientations.append(orientation)
            arrows = [
                patches.FancyArrowPatch(*orientation[0],
                                        color=arrow_color,
                                        arrowstyle=arrow_style)
                for orientation in orientations
            ]
            for arrow in arrows:
                ax.add_artist(arrow)
            global_step = 0

            def update(frame_num):
                nonlocal global_step
                nonlocal arrows
                global_step = frame_num
                robot.center = robot_positions[frame_num]
                for i, human in enumerate(humans):
                    human.center = human_positions[frame_num][i]
                    human_numbers[i].set_position((human.center[0] - x_offset,
                                                   human.center[1] - y_offset))
                    for arrow in arrows:
                        arrow.remove()
                    arrows = [
                        patches.FancyArrowPatch(*orientation[frame_num],
                                                color=arrow_color,
                                                arrowstyle=arrow_style)
                        for orientation in orientations
                    ]
                    for arrow in arrows:
                        ax.add_artist(arrow)
                    if self.attention_weights is not None:
                        human.set_color(
                            str(self.attention_weights[frame_num][i]))
                        attention_scores[i].set_text('human {}: {:.2f}'.format(
                            i, self.attention_weights[frame_num][i]))

                time.set_text('Time: {:.2f}'.format(frame_num *
                                                    self.time_step))

            def plot_value_heatmap():
                assert self.robot.kinematics == 'holonomic'
                for agent in [self.states[global_step][0]
                              ] + self.states[global_step][1]:
                    print(('{:.4f}, ' * 6 + '{:.4f}').format(
                        agent.px, agent.py, agent.gx, agent.gy, agent.vx,
                        agent.vy, agent.theta))
                # when any key is pressed draw the action value plot
                fig, axis = plt.subplots()
                speeds = [0] + self.robot.policy.speeds
                rotations = self.robot.policy.rotations + [np.pi * 2]
                r, th = np.meshgrid(speeds, rotations)
                z = np.array(self.action_values[global_step %
                                                len(self.states)][1:])
                z = (z - np.min(z)) / (np.max(z) - np.min(z))
                z = np.reshape(z, (16, 5))
                polar = plt.subplot(projection="polar")
                polar.tick_params(labelsize=16)
                mesh = plt.pcolormesh(th, r, z, vmin=0, vmax=1)
                plt.plot(rotations, r, color='k', ls='none')
                plt.grid()
                cbaxes = fig.add_axes([0.85, 0.1, 0.03, 0.8])
                cbar = plt.colorbar(mesh, cax=cbaxes)
                cbar.ax.tick_params(labelsize=16)
                plt.show()

            def on_click(event):
                anim.running ^= True
                if anim.running:
                    anim.event_source.stop()
                    if hasattr(self.robot.policy, 'action_values'):
                        plot_value_heatmap()
                else:
                    anim.event_source.start()

            fig.canvas.mpl_connect('key_press_event', on_click)
            anim = animation.FuncAnimation(fig,
                                           update,
                                           frames=len(self.states),
                                           interval=self.time_step * 1000)
            anim.running = True

            if output_file is not None:
                ffmpeg_writer = animation.writers['ffmpeg']
                writer = ffmpeg_writer(fps=8,
                                       metadata=dict(artist='Me'),
                                       bitrate=1800)
                anim.save(output_file, writer=writer)
            else:
                plt.show()
        else:
            raise NotImplementedError
Esempio n. 21
0
 def line(self, _color=None) -> plt.Line2D:
     return plt.Line2D((self.p.x, self.q.x), (self.p.y, self.q.y), label=self.m, color=_color)
Esempio n. 22
0
def plot_with_smart_legend(hi):
    """

    """

    # plt the different X and chi lines: all countries in one plot
    ####################################################################################################################
    plt.figure(figsize=(12, 7))

    X_axis = plt.gca()
    chi_axis = X_axis.twinx()
    colors = sb.color_palette("bright", 2)
    markers = ['s', 'o', 'D', 'H', '^', 'x', '1', 'p', '*', 'X', '2', '4']
    fontsize = 14

    lines = []
    labels = []

    for i, (whatever) in enumerate(whatevers):

        ax.plot(whatever, marker=markers[i])
        axt.plot(whatever)

        p = plt.Line2D(
            (0, 1),
            (0, 0),
            color='black',
            linestyle='--',
            fillstyle='none',
            marker=markers[i],
            markersize=12,
        )
        l = 'exports from %s to %s' % (c1, c2)
        lines += [p]
        labels += [l]

    X_axis.legend(lines,
                  labels,
                  loc='best',
                  fontsize=fontsize,
                  title='country pairs')

    chi_axis.grid(False)

    # add properly colored axis
    ################################################################################################################
    customaxis(
        ax=X_axis,
        position='left',
        color=colors[0],
        label=r'trade matrix $X_{ik}$ in billion USD',
        scale='linear',
        size=fontsize,
        full_nrs=False,
    )

    customaxis(
        ax=chi_axis,
        position='right',
        color=colors[1],
        label=r'ease matrix $\chi_{ik}$',
        scale='linear',
        size=fontsize,
        full_nrs=False,
    )

    # add title and save to the output
    ################################################################################################################
    plt.savefig(folder + 'all_together.pdf', bbox_inches='tight')
    plt.close()
Esempio n. 23
0
def plot_eval_depth_lea_2d(evals_lea,
                           depths_lea,
                           evals_2d,
                           depths_2d,
                           path=None):
    """Plot LEA & 2D comparison at evaluation points along depth.
    Args:
        evals_* [P x D x 6]: superposition results at evaluation points. P is No. of points, D is No. of depth, 6 is result fields ['Displacement_X', 'Displacement_Y', 'Displacement_Z', 'Normal_X', 'Normal_Y', 'Normal_Z'].
        depths_* [D x 1]: depth values (negative!)
        path [str]: path for saving figure. None if plot only.
    """
    # legend
    custom_legend = [
        plt.Line2D([], [], marker='o', color='red', linestyle='None'),
        plt.Line2D([], [], marker='o', color='blue', linestyle='None')
    ]

    for i in range(len(evals_lea)):
        fig = plt.figure()
        ax = fig.subplots(ncols=2)  # y is depth
        plt.suptitle('Responses at Evaluation Point {}'.format(i), y=0.01)

        ax[0].set_xlabel('Vertical Displacement (in.)')
        ax[1].set_xlabel('Vertical Stress (psi)')
        ax[0].set_xlim(
            0,
            max(np.max(evals_lea[i][:, 2]), np.max(evals_2d[i][:, 2])) * 1.3)

        data = evals_lea[i]  # D x 6
        ax[0].plot(data[:, 2],
                   depths_lea,
                   marker='o',
                   color='red',
                   markersize=2)
        ax[1].plot(data[:, 5],
                   depths_lea,
                   marker='o',
                   color='red',
                   markersize=2)
        data = evals_2d[i]  # D x 6
        ax[0].plot(data[:, 2],
                   depths_2d,
                   marker='o',
                   color='blue',
                   markersize=2)
        ax[1].plot(data[:, 5],
                   depths_2d,
                   marker='o',
                   color='blue',
                   markersize=2)

        for j in range(2):  # two subplots
            ax[j].xaxis.set_ticks_position('top')
            ax[j].xaxis.set_label_position('top')
            ax[j].minorticks_on()
            ax[j].grid(which='major', linestyle='-', color='gray')
            ax[j].grid(which='minor', linestyle='--')
            ax[j].set_ylabel('Depth (in.)')
            ax[j].tick_params(labelsize='small')
            ax[j].legend(custom_legend, ['WinJULEA', '2D Superposition'],
                         loc='lower right',
                         fontsize='small',
                         framealpha=0.5)

        plt.tight_layout()

        if path != None:
            plt.savefig(os.path.join(path, 'eval_{}_lea_2d.png'.format(i)),
                        dpi=300,
                        bbox_inches='tight')
            plt.close(fig)
        else:
            plt.show()
Esempio n. 24
0
    def plot_multi_loss_history(self,
                                histories,
                                highlight_best_count=0,
                                title="",
                                lim_x=None,
                                lim_y=None):
        # e.g. histories = { (param0, param1): {loss: ..., val_loss: ..., ...}, (param0, param1): {loss: ..., val_loss: ..., ...} }
        loss_histories = {}
        val_loss_histories = {}
        for parameter, hist in histories.items():
            for metric, value in hist.items():
                if "loss" == metric:
                    loss_histories[parameter] = value
                if "val_loss" == metric:
                    val_loss_histories[parameter] = value

        assert len(loss_histories) == len(val_loss_histories), (
            "loss and val_loss must be of same length")

        fig, ax = plt.subplots(1, 1, figsize=(8, 8))
        colormap = plt.cm.gist_ncar

        # style definition
        loss_style = dict(alpha=0.15 if highlight_best_count > 0 else 0.5,
                          linestyle='-')
        val_loss_style = dict(alpha=0.15 if highlight_best_count > 0 else 0.5,
                              linestyle='--')

        # draw loss
        for idx, (parameter, hist) in enumerate(loss_histories.items()):
            # Setup plot color
            color = colormap(idx / len(loss_histories))
            # Plot histories
            ax.plot(hist,
                    color=color,
                    label=str(round_tuple(parameter, sig=3)),
                    **loss_style)
        # draw val loss
        for idx, (parameter, hist) in enumerate(val_loss_histories.items()):
            # Setup plot color
            color = colormap(idx / len(val_loss_histories))
            # Plot histories
            ax.plot(hist, color=color, **val_loss_style)

        # higlight the best val_loss
        if highlight_best_count > 0:
            val_loss = val_loss_histories.items()
            sorted_val_loss = nsmallest(len(val_loss),
                                        enumerate(val_loss),
                                        key=lambda x: x[1][1][-1])
            for i in range(0, highlight_best_count):
                idx = sorted_val_loss[i][0]
                parameter = sorted_val_loss[i][1][0]
                color = colormap(idx / len(val_loss_histories))
                highlight_style = loss_style
                highlight_style["alpha"] = 0.9
                highlight_style["linestyle"] = "-"
                ax.plot(loss_histories[parameter],
                        color=color,
                        label=str(round_tuple(parameter, sig=3)),
                        **highlight_style)
                highlight_style = val_loss_style
                highlight_style["alpha"] = 0.9
                highlight_style["linestyle"] = "--"
                ax.plot(val_loss_histories[parameter],
                        color=color,
                        **highlight_style)

        if lim_x is not None:
            ax.set_xlim(lim_x[0], lim_x[1])
        if lim_y is not None:
            ax.set_ylim(lim_y[0], lim_y[1])

        ax.set_axisbelow(True)
        ax.grid(linewidth='0.5')

        ax.set_ylabel("Loss")
        ax.set_xlabel("Epoch")
        ax.xaxis.set_major_locator(
            MaxNLocator(integer=True))  # force integer values on epoch axis

        # Labels to display
        handles, labels = ax.get_legend_handles_labels()
        #Create custom artists
        cust_loss_style = loss_style
        cust_loss_style["alpha"] = 1.0
        training = plt.Line2D((0, 1), (0, 0), color='k', **cust_loss_style)
        cust_loss_style = val_loss_style
        cust_loss_style["alpha"] = 1.0
        validation = plt.Line2D((0, 1), (0, 0), color='k', **cust_loss_style)
        handles = [handle for i, handle in enumerate(handles)
                   ] + [training, validation]
        labels = [label for i, label in enumerate(labels)
                  ] + ['Training', 'Validation']

        ## Shrink current axis by 20%
        # box = ax.get_position()
        # ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

        #Create legend from custom artist/label lists
        ncols = ceil(len(handles) / 20)  # 20 entries per column
        ncols = max(1, ncols)
        ax.legend(handles,
                  labels,
                  ncol=ncols,
                  loc='center left',
                  bbox_to_anchor=(1, 0.5))

        fig.suptitle(title, fontsize=20)
        self._figures.append(fig)
Esempio n. 25
0
def visualize3DData(X, scale, cmap):
    """Visualize data in 3d plot with popover next to mouse position.
	Args:
		X (np.array) - array of points, of shape (numPoints, 3)
	Returns:
		None
	"""
    fig = plt.figure(figsize=(16, 10))
    ax = fig.add_subplot(111, projection='3d')
    im = ax.scatter(X[:, 0],
                    X[:, 1],
                    X[:, 2],
                    c=X[:, 3],
                    s=X[:, 4] * scale,
                    cmap=cmap,
                    alpha=1,
                    picker=True)

    ax.set_xlabel('OBJECTIVE 1')
    ax.set_ylabel('OBJECTIVE 2')
    ax.set_zlabel('OBJECTIVE 3')

    cbar = fig.colorbar(im)
    cbar.ax.set_ylabel('OBJECTIVE 4')

    objs = X[:, 4]

    max_size = np.amax(objs) * scale / 32.0
    min_size = np.amin(objs) * scale / 4.5
    handles, labels = ax.get_legend_handles_labels()
    display = (0, 1, 2)

    size_max = plt.Line2D((0, 1), (0, 0),
                          color='k',
                          marker='o',
                          markersize=max_size,
                          linestyle='')
    size_min = plt.Line2D((0, 1), (0, 0),
                          color='k',
                          marker='o',
                          markersize=min_size,
                          linestyle='')
    legend1 = ax.legend(
        [handle for i, handle in enumerate(handles) if i in display] +
        [size_max, size_min],
        [label for i, label in enumerate(labels) if i in display] +
        ["%.2f" %
         (np.amax(objs)), "%.2f" % (np.amin(objs))],
        labelspacing=1.5,
        title='OBJECTIVE 5',
        loc=1,
        frameon=True,
        numpoints=1,
        markerscale=1)

    def distance(point, event):
        """Return distance between mouse position and given data point
		Args:
			point (np.array): np.array of shape (3,), with x,y,z in data coords
			event (MouseEvent): mouse event (which contains mouse position in .x and .xdata)
		Returns:
			distance (np.float64): distance (in screen coords) between mouse pos and data point
		"""
        assert point.shape == (
            3,
        ), "distance: point.shape is wrong: %s, must be (3,)" % point.shape

        # Project 3d data space to 2d data space
        x2, y2, _ = proj3d.proj_transform(point[0], point[1], point[2],
                                          plt.gca().get_proj())
        # Convert 2d data space to 2d screen space
        x3, y3 = ax.transData.transform((x2, y2))

        return np.sqrt((x3 - event.x)**2 + (y3 - event.y)**2)

    def calcClosestDatapoint(X, event):
        """"Calculate which data point is closest to the mouse position.
		Args:
			X (np.array) - array of points, of shape (numPoints, 3)
			event (MouseEvent) - mouse event (containing mouse position)
		Returns:
			smallestIndex (int) - the index (into the array of points X) of the element closest to the mouse position
		"""
        distances = [distance(X[i, 0:3], event) for i in range(X.shape[0])]
        return np.argmin(distances)

    def annotatePlot(X, index):
        """Create popover label in 3d chart
		Args:
			X (np.array) - array of points, of shape (numPoints, 3)
			index (int) - index (into points array X) of item which should be printed
		Returns:
			None
		"""
        # If we have previously displayed another label, remove it first
        if hasattr(annotatePlot, 'label'):
            annotatePlot.label.remove()
        # Get data point from array of points X, at position index
        x2, y2, _ = proj3d.proj_transform(X[index, 0], X[index, 1], X[index,
                                                                      2],
                                          ax.get_proj())
        annotatePlot.label = plt.annotate("index: %d" % index,
                                          xy=(x2, y2),
                                          xytext=(-20, 20),
                                          textcoords='offset points',
                                          ha='right',
                                          va='bottom',
                                          bbox=dict(boxstyle='round,pad=0.5',
                                                    fc='yellow',
                                                    alpha=0.5),
                                          arrowprops=dict(
                                              arrowstyle='->',
                                              connectionstyle='arc3,rad=0'))
        fig.canvas.draw()

    def onMouseMotion(event):
        """Event that is triggered when mouse is moved. Shows text annotation over data point closest to mouse."""
        closestIndex = calcClosestDatapoint(X, event)
        annotatePlot(X, closestIndex)

    fig.canvas.mpl_connect('motion_notify_event',
                           onMouseMotion)  # on mouse motion
    plt.show()
Esempio n. 26
0
    

# Move the final axis' ticks to the right-hand side
ax = plt.twinx(axes[-1])
dim = len(axes)
ax.xaxis.set_major_locator(ticker.FixedLocator([x[-2], x[-1]]))
set_ticks_for_axis(dim, ax, ticks=6)
ax.set_xticklabels([cols[-2], cols[-1]])


# Remove space between subplots
plt.subplots_adjust(wspace=0)

# Add legend to plot
plt.legend(
    [plt.Line2D((0,1),(0,0), color=colours[cat]) for cat in df['cluster'].cat.categories],
    df['cluster'].cat.categories,
    bbox_to_anchor=(1, 0.5), loc='center left', borderaxespad=0.)
    
plt.title("Centroides por Cluster - Data Original")

plt.show()








Esempio n. 27
0
def make_legend(categories, ax, ncol=3, legend_type=LINE, alpha=1):
    '''
    Helper function responsible for making the legend

    Parameters
    ----------
    categories : str or tuple
                 the categories in the legend
    ax : axes instance 
         the axes with which the legend is associated
    ncol : int
           the number of columns to use
    legend_type : {LINES, SCATTER, PATCH}
                  whether the legend is linked to lines, patches, or scatter 
                  plots
    alpha : float
            the alpha of the artists

    '''

    some_identifiers = []
    labels = []
    for i, category in enumerate(categories):
        color = get_color(i)

        if legend_type == LINE:
            artist = plt.Line2D([0, 1], [0, 1], color=color,
                                alpha=alpha)  # TODO
        elif legend_type == SCATTER:
            #             marker_obj = mpl.markers.MarkerStyle('o')
            #             path = marker_obj.get_path().transformed(
            #                              marker_obj.get_transform())
            #             artist  = mpl.collections.PathCollection((path,),
            #                                         sizes = [20],
            #                                         facecolors = COLOR_LIST[i],
            #                                         edgecolors = 'k',
            #                                         offsets = (0,0)
            #                                         )
            # TODO work arround, should be a proper proxyartist for scatter legends
            artist = mpl.lines.Line2D([0], [0],
                                      linestyle="none",
                                      c=color,
                                      marker='o')

        elif legend_type == PATCH:
            artist = plt.Rectangle((0, 0),
                                   1,
                                   1,
                                   edgecolor=color,
                                   facecolor=color,
                                   alpha=alpha)

        some_identifiers.append(artist)

        if type(category) == tuple:
            label = '%.2f - %.2f' % category
        else:
            label = category

        labels.append(str(label))

    ax.legend(some_identifiers,
              labels,
              ncol=ncol,
              loc=3,
              borderaxespad=0.1,
              mode='expand',
              bbox_to_anchor=(0., 1.1, 1., .102))
Esempio n. 28
0
# plot the joint distribution
plt.subplot(plotgrid[0, 0])
# plot the contour plot of the exact posterior (c_levels is used to give
# a vector of linearly spaced values at which levels contours are drawn)
c_levels = np.linspace(1e-5, Z.max(), 6)[:-1]
plt.contour(t1, t2, Z, c_levels, colors='blue')
# plot the samples from the joint posterior
samps = plt.scatter(mu, sigma, 5, color=[0.25, 0.75, 0.25])
# decorate
plt.xlim(tl1)
plt.ylim(tl2)
plt.xlabel('$\mu$', fontsize=20)
plt.ylabel('$\sigma$', fontsize=20)
plt.title('joint posterior')
plt.legend((plt.Line2D([], [], color='blue'), samps),
           ('exact contour plot', 'samples'))

# plot the marginal of mu
plt.subplot(plotgrid[1, 0])
# empirical
plt.plot(t1, pk_mu, color='#ff8f20', linewidth=2.5, label='empirical')
# exact
plt.plot(t1, pm_mu, 'k--', linewidth=1.5, label='exact')
# decorate
plt.xlim(tl1)
plt.title('marginal of $\mu$')
plt.yticks(())
plt.legend()

# plot the marginal of sigma
Esempio n. 29
0
        wrist.prv3DErr *= 0

        # temperature magnitude
        if epoch < 10000:
            T = 1. * utils.clipped_exp(-epoch / float(maxEpoch))

        # INIT PLOTTING
        if epoch == startPlotting:

            #♣ INIT plotting
            fig1 = plt.figure("Workspace", figsize=(80, 80), dpi=120)
            gs = plt.GridSpec(1, 2, width_ratios=[3, 3])

            # MAZE COORDINATES
            # north track
            line1 = plt.Line2D([4.5, 4.5], [6.2, 9.5], color='black')
            line2 = plt.Line2D([4.5, 5.5], [9.5, 9.5], color='black')
            line3 = plt.Line2D([5.5, 5.5], [6.2, 9.5], color='black')
            line4 = plt.Line2D([4.5, 5.5], [6.2, 6.2], color='silver')
            circle1 = plt.Circle((5, 9.24), 0.7, color='springgreen')
            edgecircle1 = plt.Circle((5, 9.24), 0.7, color='black', fill=False)
            # north east track
            line5 = plt.Line2D([5.5, 8], [6.2, 8.7], color='black')
            line6 = plt.Line2D([8, 8.7], [8.7, 8], color='black')
            line7 = plt.Line2D([8.7, 6.2], [8, 5.5], color='black')
            line8 = plt.Line2D([5.5, 6.2], [6.2, 5.5], color='silver')
            circle2 = plt.Circle((8, 8), 0.7, color='springgreen')
            edgecircle2 = plt.Circle((8, 8), 0.7, color='black', fill=False)
            # east track
            line9 = plt.Line2D([6.20, 9.5], [5.5, 5.5], color='black')
            line10 = plt.Line2D([9.5, 9.5], [5.5, 4.5], color='black')
def plot_statistics(stat_type, y1, y2):
    """stat_type is either "location_error" or "vorticity_bias" """
    fig, ax = plt.subplots()
    fig.set_size_inches(6, 6)

    year_data_analysis = np.genfromtxt(datadir + "average_" + str(stat_type) +
                                       "_per_lead_time_vs_analysis.txt")
    year_data_ibtracs = np.genfromtxt(datadir + "average_" + str(stat_type) +
                                      "_per_lead_time_vs_ibtracs.txt")

    x = np.arange(0, 7.25, 0.25)
    plt.plot(x, year_data_analysis, color='k')
    plt.plot(x, year_data_ibtracs, color='k', linestyle='--')

    list_of_files = os.listdir(datadir)
    ib_pattern = "tr*_average_" + stat_type + "*_ibtracs.txt"
    an_pattern = "tr*_average_" + stat_type + "*_analysis.txt"
    ib_files_to_plot = []
    an_files_to_plot = []
    for entry in list_of_files:
        if fnmatch.fnmatch(entry, ib_pattern):
            ib_files_to_plot.append(datadir + entry)
        elif fnmatch.fnmatch(entry, an_pattern):
            an_files_to_plot.append(datadir + entry)

    ib_storms_stats = np.genfromtxt(ib_files_to_plot[0])
    for ff, i in zip(ib_files_to_plot, range(1, len(ib_files_to_plot))):
        ib_data = np.genfromtxt(ff)
        ib_storms_stats = np.vstack([ib_storms_stats, ib_data])

    an_storms_stats = np.genfromtxt(an_files_to_plot[0])
    for ff, i in zip(an_files_to_plot, range(1, len(an_files_to_plot))):
        an_data = np.genfromtxt(ff)
        an_storms_stats = np.vstack([an_storms_stats, an_data])

    ib_storms_stats[ib_storms_stats == 0] = np.nan
    an_storms_stats[an_storms_stats == 0] = np.nan

    ib_maxline, ib_minline, an_maxline, an_minline = (np.zeros(29)
                                                      for i in range(4))
    for lt in range(29):
        ib_maxline[lt] = np.nanmax(ib_storms_stats[:, lt])
        ib_minline[lt] = np.nanmin(ib_storms_stats[:, lt])
        an_maxline[lt] = np.nanmax(an_storms_stats[:, lt])
        an_minline[lt] = np.nanmin(an_storms_stats[:, lt])

    ax.fill_between(x,
                    ib_minline,
                    ib_maxline,
                    facecolor='b',
                    edgecolor='b',
                    alpha=0.2,
                    linestyle='--')
    ax.fill_between(x,
                    an_minline,
                    an_maxline,
                    facecolor='b',
                    edgecolor='b',
                    alpha=0.2)

    plt.xlim(0, 7)
    plt.xticks(fontsize=8)
    plt.yticks(fontsize=8)

    plt.xlabel('Lead Time (Days)', fontsize=10)
    if stat_type == "location_error":
        plt.ylabel('Track Error (km)', fontsize=10)
        plt.ylim(bottom=0)
    elif stat_type == "mslp_bias":
        plt.ylabel('MSLP Bias (hPa)', fontsize=10)
    elif stat_type == "wind_bias":
        plt.ylabel(r'10m Wind Bias (ms$^{-1}$)', fontsize=10)

    an = plt.Line2D((0, 1), (0, 0),
                    color='k',
                    linewidth=0.5,
                    label='Mean error vs. analysis')
    ib = plt.Line2D((0, 1), (0, 0),
                    color='k',
                    linestyle='--',
                    linewidth=0.5,
                    label='Mean error vs. IBTrACS')
    spread = mpatches.Patch(color='b', alpha=0.2, label='Spread across storms')
    legend = ax.legend(handles=[an, ib, spread],
                       fontsize=8,
                       loc='upper left',
                       title="South-West Indian Ocean\n" + y1 + "-" + y2 +
                       ", " + str(NS) + " Cyclones")
    legend._legend_box.align = "left"
    plt.setp(legend.get_title(), fontsize='8')

    plt.savefig(savedir + y1 + "_" + y2 + "_" + stat_type + ".png", dpi=400)