xticks=np.arange(0, nUnitTimes, tSeriesXtickStep),
                         xticklabels=np.arange(1, nUnitTimes + 1,
                                               tSeriesXtickStep))
            elif i == total5MinPlotIndex:
                ax[i, j].set_title(plotTitles[j],
                                   y=1,
                                   loc='center',
                                   fontsize=titleFontSize)
    plt.setp([axs for axs in ax[total5MinPlotIndex, :]],
             xlim=[0, len(genotypes) + 1],
             xticks=[0],
             xticklabels=[])
    ax[tSeriesPlotIndex, total5MinPlotIndex].add_patch(
        plt.Rectangle((-0.25, -2.55),
                      29,
                      0.85,
                      facecolor='0.9',
                      clip_on=False,
                      linewidth=0))
    #plt.setp(ax[tSeriesPlotIndex,nParamsToPlot/2], xlabel = 'minutes', **{'fontsize':10})
    plt.text(-11.55, -0.53, 'minutes', fontsize=titleFontSize)
    legendHandles, legendLabels = ax[
        legendAxesRowGet, legendAxesColGet].get_legend_handles_labels()
    legend = fig.legend(handles=legendHandles,labels=legendLabels,  \
                      loc='lower center', edgecolor=(0,0,0), fontsize=8, ncol=len(genotypes),\
                       bbox_transform=plt.gcf().transFigure)

    plt.savefig(figFName)
    plt.savefig(figFNameSvg)
    #plt.show()

    gtinParamsToPlot = 1
示例#2
0
def plot_sf_difference_lines(gb,
                             envs_to_plot,
                             reps_to_plot,
                             pcts_to_plot,
                             save=False,
                             savename='',
                             plot_title='',
                             legend=False,
                             **kwargs):
    if save:
        if savename == '':
            raise Exception(
                'Must pass argument to savename to specify title to save plot')

    convert_rep_to_color = kwargs.get('colors', plot_specs['rep_colors'])
    bar_width = 0.3
    fig, ax = plt.subplots(len(envs_to_plot),
                           2,
                           figsize=(10, 15),
                           sharex='col',
                           sharey='col',
                           gridspec_kw={'height_ratios': [1, 1, 1, 1]})
    for i, env in enumerate(envs_to_plot):
        if env[-2:] == '51':
            rwd_colrow = (16, 9)
        else:
            rwd_colrow = (14, 14)

        rect = plt.Rectangle(rwd_colrow, 1, 1, color='g', alpha=0.3)
        ax[i, 0].pcolor(grids[i],
                        cmap='bone_r',
                        edgecolors='k',
                        linewidths=0.1)
        ax[i, 0].axis(xmin=0, xmax=20, ymin=0, ymax=20)
        ax[i, 0].set_aspect('equal')
        ax[i, 0].add_patch(rect)
        ax[i, 0].get_xaxis().set_visible(False)
        ax[i, 0].get_yaxis().set_visible(False)
        ax[i, 0].invert_yaxis()
        for j, rep in enumerate(reps_to_plot):
            difs_to_plot = []
            errs_to_plot = []
            for k, pct in enumerate(pcts_to_plot):
                list_of_ids = list(
                    gb.get_group((env, rep, cache_limits[env][pct])))
                if env[3:] == 'v11' and rep == 'structured' and pct == '75':
                    print('hello')

                s, f = d_r_success_fail(list_of_ids)

                dif_of_means = np.mean(f[1]) - np.mean(s[1])
                rat_of_stds = (np.std(f[1]) / np.sqrt(len(f[1]))) / (
                    np.std(s[1]) / np.sqrt(len(s[1])))

                print(
                    env[-3:], rep, pct,
                    f'avg d fail: {np.mean(f[1])}, avg d succ: {np.mean(s[1])} diff of means: {dif_of_means}'
                )

                difs_to_plot.append(dif_of_means)
                errs_to_plot.append(rat_of_stds)
            print(env[-3:], rep, pcts_to_plot, difs_to_plot)
            ax[i, 1].errorbar(pcts_to_plot,
                              difs_to_plot,
                              marker='o',
                              color=convert_rep_to_color[rep])
            #ax[i,1].set_yticklabels([])

        ax[i, 1].set_ylabel('Average Distance\n of Closest Memory')

    ax[i, 1].set_ylim([0, 1])
    ax[i, 1].set_xlim([80, 20])
    ax[i, 1].set_xticks([75, 50, 25])
    ax[i, 1].set_xlabel('Memory Capacity (%)')

    ax[0, 1].set_title('Difference (fail-success) Trials', fontsize=12)

    if legend == 'reps':
        legend_patch_list = []
        for rep in reps_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color=convert_rep_to_color[rep],
                               label=labels_for_plot[rep]))
        plt.legend(handles=legend_patch_list,
                   bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                   loc='lower right',
                   ncol=len(legend_patch_list),
                   title='State Encoding')
    elif legend == 'pcts':
        legend_patch_list = []
        for pct in pcts_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color='gray', label=f'{pct}', alpha=pct / 100))
            plt.legend(handles=legend_patch_list,
                       bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                       loc='lower right',
                       ncol=len(legend_patch_list),
                       title='Episodic Memory Capacity (%)')
    if save:
        format = kwargs.get('format', 'svg')
        plt.savefig(f'../figures/CH2/{savename}.{format}', format=format)
    plt.show()
示例#3
0
def plot_sequence_ids_frames_kalman(ids_frames, tracks, kalman_tracks, db,
                                    output_dir):

    if not osp.exists(output_dir):
        os.makedirs(output_dir)

    for i, v in enumerate(db):
        for choosen_id, choosen_frame in ids_frames:
            if i == choosen_frame:
                im_path = v['img_path']
                im_name = osp.basename(im_path)
                im_output = osp.join(output_dir, im_name)
                im = cv2.imread(im_path)
                im = im[:, :, (2, 1, 0)]

                sizes = np.shape(im)
                height = float(sizes[0])
                width = float(sizes[1])

                fig = plt.figure()
                fig.set_size_inches(width / 100, height / 100)
                ax = plt.Axes(fig, [0., 0., 1., 1.])
                ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(im)

                # Ramki ścieżek
                for j, t in tracks.items():
                    if j == choosen_id:
                        if i in t.keys():
                            t_i = t[i]
                            ax.add_patch(
                                plt.Rectangle((t_i[0], t_i[1]),
                                              t_i[2] - t_i[0],
                                              t_i[3] - t_i[1],
                                              fill=False,
                                              linewidth=2.0,
                                              color='cyan'))

                            ax.annotate(j,
                                        xy=(t_i[0], t_i[1] + 3),
                                        color='cyan',
                                        weight='bold',
                                        fontsize=20,
                                        ha='left',
                                        va='bottom')

                # Ramki przewidziane przez Kalmana
                for j, t in kalman_tracks.items():
                    if j == choosen_id:
                        if i in t.keys():
                            t_i = t[i]
                            ax.add_patch(
                                plt.Rectangle((t_i[0], t_i[1]),
                                              t_i[2] - t_i[0],
                                              t_i[3] - t_i[1],
                                              fill=False,
                                              linewidth=2.0,
                                              color='red'))

                plt.axis('off')
                plt.draw()
                plt.savefig(im_output, dpi=100)
                plt.close()
示例#4
0
def bayesian_hinton(log_p01,
                    output_directory,
                    filename,
                    row_labels=None,
                    column_labels=None,
                    displayed_mutations=None,
                    put_driver_vars=None):
    """
    Draw bayesian Hinton diagram for visualizing uncertainty in mutation data of multiple samples.
    :param log_p01: mutation data decoded as log probability that a variant is (absent, present) in each sample
    :param output_directory: output directory
    :param filename: plot filename (without ending), add pdf and png later
    :param row_labels: sample names
    :param column_labels: names of genes where the variant occurred
    :param displayed_mutations: depict only the given list of mutations in the table
    :param put_driver_vars: defaultdict with mutation IDs and tuples of (mutation effect, sources, CGC driver)
                            to highlight mutations associated with cancer
    """

    # create colorbar and return scalar map
    # scalar_map = _create_colorbar(output_directory)
    # colors = [scalar_map.to_rgba(1.0), scalar_map.to_rgba(0.85), scalar_map.to_rgba(0.7), scalar_map.to_rgba(0.5),
    #           scalar_map.to_rgba(0.3), scalar_map.to_rgba(0.15), scalar_map.to_rgba(0.0)]
    # from seaborn sns.color_palette("RdBu_r", 7)
    # sns.diverging_palette(245, 15, s=95, n=15)
    div_pal = sns.diverging_palette(245, 15, s=95, n=23)
    colors = []
    for c in div_pal:
        colors.append(c[:3])

    # size and position settings
    height = 4
    width = 2
    y_spacing = 1
    label_x_pos = -2
    label_y_pos = 0

    # show all mutations in the table if no subset is given
    if displayed_mutations is None:
        displayed_mutations = [i for i in range(len(log_p01))]

    x_length = ((-label_x_pos + 20) if row_labels is not None else
                0) + (len(displayed_mutations) * width)
    y_length = len(log_p01[0]) * (height + y_spacing) - y_spacing + (
        label_y_pos + 20 if column_labels is not None else 0)

    # create new figure
    fig = plt.figure(figsize=(x_length / 20.0, y_length / 20.0),
                     dpi=150)  # , frameon=False

    ax = plt.axes([0, 1, 1, 1])

    ax.patch.set_facecolor('white')
    ax.set_aspect('auto')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    # sort mutation table according to status
    priorities = [0 for _ in range(len(log_p01))]
    for mut_idx in displayed_mutations:
        for sa_idx, (log_prob0, _) in enumerate(log_p01[mut_idx]):
            p0 = math.exp(log_prob0)
            if p0 <= 0.01:  # mutation is most likely present
                priorities[mut_idx] += 7 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            elif p0 <= 0.1:  # mutation is probably present
                priorities[mut_idx] += 6 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            elif p0 <= 0.25:  # mutation is maybe present
                priorities[mut_idx] += 5 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            elif p0 < 0.75:  # mutation is unknown
                priorities[mut_idx] += 3 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            elif p0 < 0.9:  # mutation is maybe absent
                priorities[mut_idx] += 2 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            elif p0 < 0.99:  # mutation is probably absent
                priorities[mut_idx] += 1 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))
            else:  # mutation is most likely absent
                priorities[mut_idx] += 0 * (8**(len(log_p01[mut_idx]) -
                                                sa_idx - 1))

    edge_color = 'black'

    for x_pos, mut_idx in enumerate(
            sorted(displayed_mutations,
                   key=lambda k: (-priorities[k], column_labels[k]
                                  if column_labels is not None else 0))):

        for sa_idx, (log_prob0, _) in enumerate(log_p01[mut_idx]):
            p0 = math.exp(log_prob0)

            if p0 <= 0.00001:  # mutation is most likely present
                color = colors[0]
            elif p0 <= 0.0001:  # mutation is most likely present
                color = colors[1]
            elif p0 <= 0.001:  # mutation is most likely present
                color = colors[2]
            elif p0 <= 0.0031:  # mutation is probably present
                color = colors[3]
            elif p0 <= 0.01:  # mutation is probably present
                color = colors[4]
            elif p0 <= 0.02:  # mutation is probably present
                color = colors[5]
            elif p0 <= 0.05:  # mutation is probably present
                color = colors[6]
            elif p0 <= 0.1:  # mutation is probably present
                color = colors[7]
            elif p0 <= 0.15:  # mutation is probably present
                color = colors[8]
            elif p0 <= 0.2:  # mutation is maybe present
                color = colors[9]
            elif p0 <= 0.4:  # mutation is maybe present
                color = colors[10]
            elif p0 < 0.6:  # mutation is unknown
                color = colors[11]
            elif p0 < 0.8:  # mutation is maybe absent
                color = colors[12]
            elif p0 < 0.85:  # mutation is maybe absent
                color = colors[13]
            elif p0 < 0.9:  # mutation is probably absent
                color = colors[14]
            elif p0 < 0.95:  # mutation is probably absent
                color = colors[15]
            elif p0 < 0.98:  # mutation is probably absent
                color = colors[16]
            elif p0 < 0.99:  # mutation is probably absent
                color = colors[17]
            elif p0 < 0.9969:  # mutation is probably absent
                color = colors[18]
            elif p0 < 0.999:  # mutation is probably absent
                color = colors[19]
            elif p0 < 0.9999:  # mutation is probably absent
                color = colors[20]
            elif p0 < 0.99999:  # mutation is probably absent
                color = colors[21]
            else:  # mutation is most likely absent
                color = colors[22]

            rect = plt.Rectangle([
                x_pos * width,
                (height + y_spacing) * (len(log_p01[mut_idx]) - sa_idx - 1)
            ],
                                 width,
                                 height,
                                 facecolor=color,
                                 edgecolor=edge_color,
                                 linewidth=1.0)
            ax.add_patch(rect)

    if row_labels is not None:
        for sa_idx, row_name in enumerate(row_labels):
            ax.text(label_x_pos,
                    (height + y_spacing) * (len(row_labels) - sa_idx - 1) + 1,
                    row_name.replace('_', ' '),
                    horizontalalignment='right',
                    verticalalignment='bottom',
                    fontsize=12)

    if column_labels is not None:
        for x_pos, mut_idx in enumerate(
                sorted(displayed_mutations,
                       key=lambda k: (-priorities[k], column_labels[k]))):

            # number of supporting sources
            c = ((Driver.colors()[len(put_driver_vars[mut_idx].sources)]
                  if Driver.MaxSourceSupport > 0 else 'darkred')
                 if put_driver_vars is not None and mut_idx in put_driver_vars
                 else 'black')

            ax.text(
                x_pos * width + (width / 2) + 0.2,
                label_y_pos + (height + y_spacing) * (len(log_p01[mut_idx])),
                _format_gene_name(column_labels[mut_idx], max_length=12),
                rotation='vertical',
                horizontalalignment='center',
                verticalalignment='bottom',
                fontsize=8,
                color=c,  # sources?
                weight=('bold' if put_driver_vars is not None
                        and mut_idx in put_driver_vars
                        and put_driver_vars[mut_idx].cgc_driver else
                        'normal'))  # is in CGC

    ax.autoscale_view()
    # fig.tight_layout()
    ax.set_axis_off()
    fig.add_axes(ax)

    plt.savefig(os.path.join(output_directory, filename + '.pdf'),
                dpi=150,
                bbox_inches='tight',
                transparent=True)
    plt.savefig(os.path.join(output_directory, filename + '.png'),
                dpi=150,
                bbox_inches='tight',
                transparent=True)

    logger.info('Generated bayesian mutation table plot: {}'.format(filename +
                                                                    '.pdf'))

    # plt.show(block=True)
    plt.close()
示例#5
0
def clustered_table(filename,
                    patient,
                    clusters,
                    row_labels=None,
                    column_labels=None,
                    displayed_mutations=None):
    """
    Create a combined figure of the mutation table and the clustering results
    to visualize evolutionary conflicts.
    :param filename: path to the output file of the created figure
    :param patient: data
    :param clusters: matrix with 4 columns and n-1 rows stating which clusters were combined at each step
    :param row_labels: sample names
    :param column_labels: names of genes where the variant occurred
    :param displayed_mutations: depict only the given list of mutations in the table
    """

    # size and position settings
    height = 4
    width = 2
    y_spacing = 1
    label_x_pos = -2
    label_y_pos = 0

    row_label_rect_width = 21
    row_label_rect_height = 4

    # show all mutations in the table if no subset is given
    if displayed_mutations is None:
        displayed_mutations = [i for i in range(len(patient.data))]

    table_ax_width = -label_x_pos + (len(displayed_mutations) * width)
    dendrogram_ax_width = patient.n * width / 2.0
    x_length = ((-label_x_pos + 20) if row_labels is not None else
                0) + table_ax_width + dendrogram_ax_width
    y_length = (len(patient.data[0]) * (height + y_spacing) - y_spacing +
                (label_y_pos + 20 if column_labels is not None else 0))

    fig = plt.figure(figsize=(x_length / 20.0, y_length / 20.0), dpi=150)

    # - - - - - - ADD DENDROGRAM - -  - - - - -
    ax_dg = plt.axes([
        1.0 - (dendrogram_ax_width / float(x_length)), 1,
        dendrogram_ax_width / float(x_length), 1
    ],
                     frameon=False)
    ax_dg.axis('off')

    den = sch.dendrogram(clusters,
                         distance_sort='descending',
                         orientation='left',
                         labels=patient.sample_names,
                         color_threshold=len(patient.sample_names),
                         leaf_font_size=12,
                         link_color_func=lambda k: 'black')

    # - - - - - ADD MUTATION TABLE - - - - -
    ax_hin = plt.axes([0, 1, 1.0 - (dendrogram_ax_width / float(x_length)), 1])
    ax_hin.patch.set_facecolor('white')
    ax_hin.set_aspect('auto')
    ax_hin.xaxis.set_major_locator(plt.NullLocator())
    ax_hin.yaxis.set_major_locator(plt.NullLocator())

    # sort mutation table according to clustering and status
    priorities = [0 for _ in range(len(patient.data))]
    leave_ordering = deepcopy(
        den['leaves'])  # leave ordering based on the results of the clustering
    leave_ordering.reverse()
    for mut_idx in displayed_mutations:
        for i in range(len(patient.data[mut_idx])):

            sa_idx = int(leave_ordering[i])
            maf = patient.data[mut_idx][sa_idx]

            if maf > 0:  # mutation is present
                priorities[mut_idx] += 3 * (4**(len(patient.data[mut_idx]) -
                                                i - 1))
            elif maf == POS_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                priorities[mut_idx] += 1 * (4**(len(patient.data[mut_idx]) -
                                                i - 1))
            elif maf == NEG_UNKNOWN:
                priorities[mut_idx] += 1 * (4**(len(patient.data[mut_idx]) -
                                                i - 1))
            else:
                priorities[mut_idx] += 0 * (4**(len(patient.data[mut_idx]) -
                                                i - 1))

    edge_color = 'black'
    for x_pos, mut_idx in enumerate(
            sorted(displayed_mutations,
                   key=lambda k: (-priorities[k], patient.gene_names[k]))):

        for i in range(len(patient.data[mut_idx])):

            sa_idx = leave_ordering[i]
            maf = patient.data[mut_idx][sa_idx]

            if maf > 0:  # mutation is present
                color = 'blue'
            elif maf == POS_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                color = (0.9, 0.75, 0.75)
            elif maf == NEG_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                color = (0.9, 0.75, 0.75)
            else:
                color = (1.0, 0.3, 0.3)

            rect = plt.Rectangle([
                x_pos * width,
                (height + y_spacing) * (len(patient.data[mut_idx]) - i - 1)
            ],
                                 width,
                                 height,
                                 facecolor=color,
                                 edgecolor=edge_color)
            ax_hin.add_patch(rect)

    # add sample name labels
    if row_labels is not None:
        for i, sa_idx in enumerate(reversed(den['leaves'])):
            ax_hin.text(label_x_pos,
                        (height + y_spacing) * (len(row_labels) - i - 1) + 0.5,
                        row_labels[sa_idx].replace('_', ' '),
                        horizontalalignment='right',
                        verticalalignment='bottom',
                        fontsize=12)

    # add mutation gene name labels
    if column_labels is not None:
        for x_pos, mut_idx in enumerate(
                sorted(displayed_mutations,
                       key=lambda k: (-priorities[k], patient.gene_names[k]))):

            ax_hin.text(x_pos * width + 0.5,
                        label_y_pos + (height + y_spacing) *
                        (len(patient.data[mut_idx])),
                        _format_gene_name(patient.gene_names[mut_idx],
                                          max_length=12),
                        rotation='vertical',
                        horizontalalignment='left',
                        verticalalignment='bottom',
                        fontsize=8)

    ax_dg.autoscale_view()
    ax_hin.autoscale_view()

    plt.savefig(filename, dpi=150, bbox_inches='tight', transparent=True)
    plt.close()
    logger.info(
        'Generated combined mutation table and dendrogram {}'.format(filename))
示例#6
0
def plot_positioner(ax,
                    patrol_rad,
                    fiber,
                    center,
                    cb,
                    fh,
                    color="k",
                    linewidth=0.2):
    """Plot one fiber positioner.
    """
    patrol = plt.Circle((center[0], center[1]),
                        radius=patrol_rad,
                        fc=color,
                        ec="none",
                        alpha=0.1)
    ax.add_artist(patrol)
    # Plot the arm from the center to the body
    cbcent = cb.circles[0].center
    armwidth = 0.25
    armlen = np.sqrt((cbcent[1] - center[1])**2 + (cbcent[0] - center[0])**2)
    armang = np.arctan2(cbcent[1] - center[1], cbcent[0] - center[0])
    sinarm = np.sin(armang)
    cosarm = np.cos(armang)
    arm_xoff = center[0] + (0.5 * armwidth) * sinarm
    arm_yoff = center[1] - (0.5 * armwidth) * cosarm
    armang_deg = armang * 180.0 / np.pi
    arm = plt.Rectangle((arm_xoff, arm_yoff),
                        armlen,
                        armwidth,
                        angle=armang_deg,
                        color=color,
                        linewidth=2 * linewidth,
                        fill=False)
    ax.add_artist(arm)
    for piece in [cb, fh]:
        for circle in piece.circles:
            xcent, ycent = circle.center
            rad = circle.radius
            circ = plt.Circle((xcent, ycent),
                              radius=rad,
                              fc="none",
                              ec=color,
                              linewidth=linewidth)
            ax.add_artist(circ)
        for segs in piece.segments:
            xpts = np.array([p[0] for p in segs.points])
            ypts = np.array([p[1] for p in segs.points])
            ax.plot(xpts, ypts, linewidth=linewidth, color=color)
    fontpix = armwidth * 2
    fontpt = int(0.25 * fontpix)
    xtxt = center[0] - 2 * armwidth * cosarm
    ytxt = center[1] - 2 * armwidth * sinarm
    ax.text(xtxt,
            ytxt,
            "{}".format(fiber),
            color='k',
            fontsize=fontpt,
            horizontalalignment='center',
            verticalalignment='center',
            bbox=None)
    #        bbox=dict(fc='w', ec='none', pad=1, alpha=1.0))
    return
示例#7
0
fig, ax = plt.subplots(figsize=(6, 3))
ax.set_title("Apartments for rent")

ax.scatter(df_apartments.square_meters, df_apartments.price, zorder=9)

xlim = ax.get_xlim()
ylim = ax.get_ylim()

ax.scatter([84], [15500], zorder=12, color="k", s=75)

# 15500 84

left, bottom, width, height = (0, 15500, 84, 20000)
rect = plt.Rectangle((left, bottom),
                     width,
                     height,
                     facecolor=COLORS[0],
                     alpha=0.25,
                     zorder=7)
ax.add_patch(rect)

ax.set_xlim(xlim)
ax.set_ylim(ylim)

ax.set_xlabel("Square meters")
ax.set_ylabel("Price")

ax.grid(True, ls="--", zorder=5, alpha=0.5)
plt.tight_layout()
plt.savefig("apartments7.pdf")

# In[20]:
示例#8
0
def demo(net, image_name, image_path, CONF_THRESH, NMS_THRESH, boxes_savepath, labels_savepath, images_savepath):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = cv2.imread(image_path)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    timer.toc()
    print ('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class

    cand = []
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4:8]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]

        one = [cls, dets, CONF_THRESH]
        cand.append(one)
    rects, cas, rects_out = vis_detections(im, cand)

    header_rects = ['xmin','ymin','xmax','ymax']
    header_cas = ['label','accuracy']
    
    csvfileName_rects = boxes_savepath + '/' + image_name.split('.')[0] + '_boxes.csv'
    csvfileName_rects2 = boxes_savepath + '/' + image_name.split('.')[0] + '_boxes2.csv'
    csvfileName_cas = labels_savepath  + '/' + image_name.split('.')[0] + '_label.csv'
    
    List2CSV(csvfileName_rects, rects_out, header_rects)
    List2CSV(csvfileName_rects2, rects, header_rects)
    List2CSV(csvfileName_cas, cas, header_cas)

    fig_save_name = images_savepath + '/new-' + image_name  # path + fig_name of output

    fig, ax = plt.subplots(figsize=(12,12))
    im = im[:, :, (2, 1, 0)]
    ax.imshow(im, aspect='equal')
    for ii in range(len(rects)):
        r = rects[ii]
        ax.add_patch(
            plt.Rectangle((r[0], r[1]), r[2], r[3] ,
                fill=False, edgecolor='red', linewidth=3.5))
        c = cas[ii]
        ax.text(r[0], r[1] - 2,
                '{:s} {:.3f}'.format(c[0], c[1]),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=16, color='white')
    plt.axis('off')
    plt.tight_layout()
    plt.draw()

    plt.savefig(fig_save_name)  # save and output the labeled figure
    plt.close()
    
    return scores, boxes, timer
示例#9
0
def plot_world(world, **kwargs):
    scale = kwargs.get('scale', 0.35)
    title = kwargs.get('title', 'Grid World')
    ax_labels = kwargs.get('ax_labels', False)
    state_labels = kwargs.get('states', False)
    invert_ = kwargs.get('invert', False)
    if invert_:
        cmap = 'bone'
    else:
        cmap = 'bone_r'
    r, c = world.shape

    fig = plt.figure(figsize=(c * scale, r * scale))
    ax = fig.add_subplot(1, 1, 1)

    gridMat = np.zeros(world.shape)
    for i, j in world.obstacle2D:
        gridMat[i, j] = 1.0
    for i, j in world.terminal2D:
        gridMat[i, j] = 0.2
    ax.pcolor(world.grid,
              edgecolors='k',
              linewidths=0.75,
              cmap=cmap,
              vmin=0,
              vmax=1)

    U = np.zeros((r, c))
    V = np.zeros((r, c))
    U[:] = np.nan
    V[:] = np.nan

    if len(world.action_list) > 4:
        if world.jump is not None:
            for (a, b) in world.jump.keys():
                (a2, b2) = world.jump[(a, b)]
                U[a, b] = (b2 - b)
                V[a, b] = (a - a2)

    C, R = np.meshgrid(np.arange(0, c) + 0.5, np.arange(0, r) + 0.5)
    ax.quiver(C, R, U, V, scale=1, units='xy')

    for rwd_loc in world.rewards.keys():
        rwd_r, rwd_c = rwd_loc
        if world.rewards[rwd_loc] < 0:
            colorcode = 'red'
        else:
            colorcode = 'darkgreen'
        ax.add_patch(
            plt.Rectangle((rwd_c, rwd_r),
                          width=1,
                          height=1,
                          linewidth=2,
                          facecolor=colorcode,
                          alpha=0.5))

    if state_labels:
        for (i, j) in world.useable:
            # i = row, j = col
            ax.annotate(f'{world.twoD2oneD((i,j))}', (j + 0.3, i + 0.7))

    #ax.set_xticks([np.arange(c) + 0.5, np.arange(c)])
    #ax.set_yticks([np.arange(r) + 0.5, np.arange(r)])
    ax.invert_yaxis()
    ax.set_aspect('equal')
    if not ax_labels:
        ax.get_xaxis().set_ticks([])
        ax.get_yaxis().set_ticks([])
    ax.set_title(title)

    return fig, ax
示例#10
0
    E_data = np.mean(repeat_T5[:, 75:675], 0)
    F_data = np.mean(repeat_T6[:, 75:675], 0)
    H_data = np.mean(repeat_T7[:, 75:675], 0)
    G_data = np.mean(repeat_T8[:, 75:675], 0)

    plt.figure(figsize=(12, 6), facecolor='none', edgecolor='none')
    plt.plot([-200, 1000], [0, 0], color='k', linewidth=1, linestyle="-")
    plt.plot([0, 0], [np.min(A_data), np.max(B_data)],
             color='k',
             linewidth=1,
             linestyle="-")
    plt.xlabel('Time/ms', fontsize=20)
    plt.ylabel('ERP/uV', fontsize=20)
    plt.tick_params(labelsize=20)
    sig_x_mat = range(-200, 1000, 2)
    plt.Rectangle((9, 0), 9, 9, color='red')
    plt.style.use('ggplot')
    plt.plot(sig_x_mat,
             A_data,
             linestyle='-',
             color="#0000FF",
             linewidth=2,
             label='Switch_T1')
    plt.plot(sig_x_mat,
             B_data,
             linestyle='-',
             color="#FF0000",
             linewidth=2,
             label='Switch_T2')
    plt.plot(sig_x_mat,
             C_data,
示例#11
0
def vis_one_image(im,
                  im_name,
                  output_dir,
                  boxes,
                  segms=None,
                  keypoints=None,
                  thresh=0.9,
                  kp_thresh=2,
                  dpi=200,
                  box_alpha=0.0,
                  dataset=None,
                  show_class=False,
                  ext='jpg'):
    """Visual debugging of detections."""
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    if isinstance(boxes, list):
        boxes, segms, keypoints, classes = convert_from_cls_format(
            boxes, segms, keypoints)
    if boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < thresh:
        return

    dataset_keypoints, _ = keypoint_utils.get_keypoints()
    #print('segms:{},keypoints:{}'.format(segms,keypoints))
    if segms is not None and len(segms) > 0:
        masks = mask_util.decode(segms)

    color_list = colormap(rgb=True) / 255

    kp_lines = kp_connections(dataset_keypoints)
    cmap = plt.get_cmap('rainbow')
    colors = [cmap(i) for i in np.linspace(0, 1, len(kp_lines) + 2)]

    fig = plt.figure(frameon=False)
    fig.set_size_inches(im.shape[1] / dpi, im.shape[0] / dpi)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.axis('off')
    fig.add_axes(ax)
    ax.imshow(im)

    # Display in largest to smallest order to reduce occlusion
    areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
    sorted_inds = np.argsort(-areas)

    mask_color_id = 0
    #print('vis:',boxes,classes)
    for i in sorted_inds:
        bbox = boxes[i, :4]
        score = boxes[i, -1]
        if score < thresh:
            continue

        # show box (off by default)
        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1],
                          fill=False,
                          edgecolor='g',
                          linewidth=0.5,
                          alpha=box_alpha))

        if show_class:
            ax.text(bbox[0],
                    bbox[1] - 2,
                    get_class_string(classes[i], score, dataset),
                    fontsize=3,
                    family='serif',
                    bbox=dict(facecolor='g',
                              alpha=0.4,
                              pad=0,
                              edgecolor='none'),
                    color='white')

        # show mask
        if segms is not None and len(segms) > i:
            img = np.ones(im.shape)
            color_mask = color_list[mask_color_id % len(color_list), 0:3]
            mask_color_id += 1

            w_ratio = .4
            for c in range(3):
                color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio
            for c in range(3):
                img[:, :, c] = color_mask[c]
            e = masks[:, :, i]

            _, contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP,
                                                cv2.CHAIN_APPROX_NONE)

            for c in contour:
                polygon = Polygon(c.reshape((-1, 2)),
                                  fill=True,
                                  facecolor=color_mask,
                                  edgecolor='w',
                                  linewidth=1.2,
                                  alpha=0.5)
                ax.add_patch(polygon)

        # show keypoints
        if keypoints is not None and len(keypoints) > i:
            kps = keypoints[i]
            plt.autoscale(False)
            for l in range(len(kp_lines)):
                i1 = kp_lines[l][0]
                i2 = kp_lines[l][1]
                if kps[2, i1] > kp_thresh and kps[2, i2] > kp_thresh:
                    x = [kps[0, i1], kps[0, i2]]
                    y = [kps[1, i1], kps[1, i2]]
                    line = plt.plot(x, y)
                    plt.setp(line, color=colors[l], linewidth=1.0, alpha=0.7)
                if kps[2, i1] > kp_thresh:
                    plt.plot(kps[0, i1],
                             kps[1, i1],
                             '.',
                             color=colors[l],
                             markersize=3.0,
                             alpha=0.7)

                if kps[2, i2] > kp_thresh:
                    plt.plot(kps[0, i2],
                             kps[1, i2],
                             '.',
                             color=colors[l],
                             markersize=3.0,
                             alpha=0.7)

            # add mid shoulder / mid hip for better visualization
            mid_shoulder = (
                kps[:2, dataset_keypoints.index('right_shoulder')] +
                kps[:2, dataset_keypoints.index('left_shoulder')]) / 2.0
            sc_mid_shoulder = np.minimum(
                kps[2, dataset_keypoints.index('right_shoulder')],
                kps[2, dataset_keypoints.index('left_shoulder')])
            mid_hip = (kps[:2, dataset_keypoints.index('right_hip')] +
                       kps[:2, dataset_keypoints.index('left_hip')]) / 2.0
            sc_mid_hip = np.minimum(
                kps[2, dataset_keypoints.index('right_hip')],
                kps[2, dataset_keypoints.index('left_hip')])
            if (sc_mid_shoulder > kp_thresh
                    and kps[2, dataset_keypoints.index('nose')] > kp_thresh):
                x = [mid_shoulder[0], kps[0, dataset_keypoints.index('nose')]]
                y = [mid_shoulder[1], kps[1, dataset_keypoints.index('nose')]]
                line = plt.plot(x, y)
                plt.setp(line,
                         color=colors[len(kp_lines)],
                         linewidth=1.0,
                         alpha=0.7)
            if sc_mid_shoulder > kp_thresh and sc_mid_hip > kp_thresh:
                x = [mid_shoulder[0], mid_hip[0]]
                y = [mid_shoulder[1], mid_hip[1]]
                line = plt.plot(x, y)
                plt.setp(line,
                         color=colors[len(kp_lines) + 1],
                         linewidth=1.0,
                         alpha=0.7)

    output_name = os.path.basename(im_name) + '.' + ext
    fig.savefig(os.path.join(output_dir, '{}'.format(output_name)), dpi=dpi)
    plt.close('all')
示例#12
0
# In[13]:


def calculate_first(n=1000):
    return calculate_integral(integrand_1, cauchy.rvs(size=n), cauchy.pdf)


calculate_first()

# ### Для второго интеграла, ограничим область интегрирования квадратом `4x4`:

# In[180]:

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, aspect='equal')
square = plt.Rectangle((-2, -2), 4, 4, color='#C1FFF9', label='bounding box')
circle1 = plt.Circle((0, 0), 2, color='#338AF3', label='1≤x^2+y^2≤4')
circle2 = plt.Circle((0, 0), 1, color='#C1FFF9')
ax.add_patch(square)
ax.add_patch(circle1)
ax.add_patch(circle2)
plt.axis([-2.5, 2.5, -2.5, 2.5])
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

# ### Определим функцию-индикатор попадания в область интегрирования:

# In[24]:


def region(x, y):
示例#13
0
    parser.add_argument("filelist")
    parser.add_argument("output")
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    a = init()
    f = open(a.filelist)
    plt.figure()
    plt.hold(True)
    framess = []
    fpss = []
    count = 0
    for l in f.readlines():
        count += 1
        if count % 100 == 0:
            print "processing", count, "videos"
        filename = l.strip()
        fps, size, frames = info_video(filename)
        framess.append(frames)
        fpss.append(fps)
        plt.Rectangle((0, 0), size[0], size[1], linewidth=0.1)
    plt.savefig(a.output)
    print "fps mean:", np.mean(fpss)
    print "fps min:", np.min(fpss)
    print "fps max:", np.max(fpss)
    print "frames mean", np.mean(framess)
    print "frames min", np.min(framess)
    print "frames max", np.max(framess)
示例#14
0
    currentAxis = plt.gca()

    for i in range(top_conf.shape[0]):
        xmin = int(round(top_xmin[i] * img.shape[1]))
        ymin = int(round(top_ymin[i] * img.shape[0]))
        xmax = int(round(top_xmax[i] * img.shape[1]))
        ymax = int(round(top_ymax[i] * img.shape[0]))
        score = top_conf[i]
        label = int(top_label_indices[i])
        label_name = voc_classes[label - 1]
        display_txt = '{:0.2f}, {}'.format(score, label_name)

        #display_txt = '{:0.2f}, {}'.format(score, label)
        coords = (xmin, ymin), xmax - xmin + 1, ymax - ymin + 1
        color = colors[label]
        currentAxis.add_patch(
            plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
        currentAxis.text(xmin,
                         ymin,
                         display_txt,
                         bbox={
                             'facecolor': color,
                             'alpha': 0.5
                         })

    plt.pause(1)
    print('i=', i)
    count += 1
    plt.savefig("recognized_picture{0:03d}_".format(count) +
                "_{0:03d}.png".format(top_conf.shape[0]))
    plt.close()
示例#15
0
def show_splitter(splitter, alpha=0.2, area_buffer=0.2, show_legend=False):
    area_bbox = splitter.get_area_bbox()
    minx, miny, maxx, maxy = area_bbox
    lng, lat = area_bbox.get_middle()
    w, h = maxx - minx, maxy - miny
    minx = minx - area_buffer * w
    miny = miny - area_buffer * h
    maxx = maxx + area_buffer * w
    maxy = maxy + area_buffer * h

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)

    base_map = Basemap(projection='mill',
                       lat_0=lat,
                       lon_0=lng,
                       llcrnrlon=minx,
                       llcrnrlat=miny,
                       urcrnrlon=maxx,
                       urcrnrlat=maxy,
                       resolution='l',
                       epsg=4326)
    base_map.drawcoastlines(color=(0, 0, 0, 0))

    area_shape = splitter.get_area_shape()
    if isinstance(area_shape, Polygon):
        area_shape = [area_shape]
    for polygon in area_shape:
        if isinstance(polygon.boundary, MultiLineString):
            for linestring in polygon.boundary:
                ax.add_patch(
                    plt_polygon(np.array(linestring),
                                closed=True,
                                facecolor=(0, 0, 0, 0),
                                edgecolor='red'))
        else:
            ax.add_patch(
                plt_polygon(np.array(polygon.boundary),
                            closed=True,
                            facecolor=(0, 0, 0, 0),
                            edgecolor='red'))

    bbox_list = splitter.get_bbox_list()
    info_list = splitter.get_info_list()

    cm = plt.get_cmap('jet', len(bbox_list))
    legend_shapes = []
    for i, (bbox, info) in enumerate(zip(bbox_list, info_list)):
        wgs84_bbox = transform_bbox(bbox, CRS.WGS84).get_polygon()

        tile_color = tuple(list(cm(i))[:3] + [alpha])
        ax.add_patch(
            plt_polygon(np.array(wgs84_bbox),
                        closed=True,
                        facecolor=tile_color,
                        edgecolor='green'))

        if show_legend:
            legend_shapes.append(plt.Rectangle((0, 0), 1, 1, fc=cm(i)))

    if show_legend:
        plt.legend(legend_shapes, [
            '{},{}'.format(info['index_x'], info['index_y'])
            for info in info_list
        ])
    plt.tight_layout()
    plt.show()
def main():
    model_mode = 'inference'
    K.clear_session()  # Clear previous models from memory.

    model = ssd_512(image_size=(Config.img_height, Config.img_width,
                                Config.img_channels),
                    n_classes=Config.n_classes,
                    mode=model_mode,
                    l2_regularization=Config.l2_regularization,
                    scales=Config.scales,
                    aspect_ratios_per_layer=Config.aspect_ratios,
                    two_boxes_for_ar1=True,
                    steps=Config.steps,
                    offsets=Config.offsets,
                    clip_boxes=False,
                    variances=Config.variances,
                    normalize_coords=Config.normalize_coords,
                    subtract_mean=Config.mean_color,
                    swap_channels=[2, 1, 0],
                    confidence_thresh=0.01,
                    iou_threshold=0.45,
                    top_k=200,
                    nms_max_output_size=400)

    # 2: Load the trained weights into the model.

    weights_path = os.getcwd() + '/weights/' + args.model_name + ".h5"
    model.load_weights(weights_path, by_name=True)
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    test_dataset = DataGenerator(load_images_into_memory=True,
                                 hdf5_dataset_path=os.getcwd() + "/data/" +
                                 args.dataset + '/polyp_test.h5')

    test_dataset_size = test_dataset.get_dataset_size()
    print("Number of images in the test dataset:\t{:>6}".format(
        test_dataset_size))

    classes = ['background', 'polyp']

    generator = test_dataset.generate(batch_size=1,
                                      shuffle=True,
                                      transformations=[],
                                      returns={
                                          'processed_images', 'filenames',
                                          'inverse_transform',
                                          'original_images', 'original_labels'
                                      },
                                      keep_images_without_gt=False)

    # Generate a batch and make predictions.

    i = 0
    confidence_threshold = Config.confidence_threshold

    for val in range(test_dataset_size):
        batch_images, batch_filenames, batch_inverse_transforms, batch_original_images, batch_original_labels = next(
            generator)

        # print("Image:", batch_filenames[i])
        print("Ground truth boxes:\n")
        print(np.array(batch_original_labels[i]))

        y_pred = model.predict(batch_images)

        # Perform confidence thresholding.
        y_pred_thresh = [
            y_pred[k][y_pred[k, :, 1] > confidence_threshold]
            for k in range(y_pred.shape[0])
        ]

        # Convert the predictions for the original image.
        # y_pred_thresh_inv = apply_inverse_transforms(y_pred_thresh, batch_inverse_transforms)

        np.set_printoptions(precision=2, suppress=True, linewidth=90)
        print("Predicted boxes:\n")
        print('   class   conf xmin   ymin   xmax   ymax')
        print(y_pred_thresh[i])

        plt.figure(figsize=(20, 12))
        plt.imshow(batch_images[i])

        current_axis = plt.gca()

        colors = plt.cm.hsv(
            np.linspace(0, 1, Config.n_classes +
                        1)).tolist()  # Set the colors for the bounding boxes
        classes = [
            'background', 'polyps'
        ]  # Just so we can print class names onto the image instead of IDs

        for box in batch_original_labels[i]:
            xmin = box[1]
            ymin = box[2]
            xmax = box[3]
            ymax = box[4]
            label = '{}'.format(classes[int(box[0])])
            current_axis.add_patch(
                plt.Rectangle((xmin, ymin),
                              xmax - xmin,
                              ymax - ymin,
                              color='green',
                              fill=False,
                              linewidth=2))
            current_axis.text(xmin,
                              ymin,
                              label,
                              size='x-large',
                              color='white',
                              bbox={
                                  'facecolor': 'green',
                                  'alpha': 1.0
                              })

        for box in y_pred_thresh[i]:
            xmin = box[2]
            ymin = box[3]
            xmax = box[4]
            ymax = box[5]
            color = colors[int(box[0])]
            label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
            current_axis.add_patch(
                plt.Rectangle((xmin, ymin),
                              xmax - xmin,
                              ymax - ymin,
                              color=color,
                              fill=False,
                              linewidth=2))
            current_axis.text(xmin,
                              ymin,
                              label,
                              size='x-large',
                              color='white',
                              bbox={
                                  'facecolor': color,
                                  'alpha': 1.0
                              })
        image = plt.gcf()
        # plt.show()
        plt.draw()
        image.savefig(os.getcwd() + "/val_ssd512/val_" + str(val) + ".png",
                      dpi=100)

    evaluator = Evaluator(model=model,
                          n_classes=Config.n_classes,
                          data_generator=test_dataset,
                          model_mode=model_mode)

    results = evaluator(img_height=Config.img_height,
                        img_width=Config.img_width,
                        batch_size=args.batch_size,
                        data_generator_mode='resize',
                        round_confidences=False,
                        matching_iou_threshold=0.3,
                        border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample',
                        num_recall_points=11,
                        ignore_neutral_boxes=True,
                        return_precisions=True,
                        return_recalls=True,
                        return_average_precisions=True,
                        verbose=True)

    mean_average_precision, average_precisions, precisions, recalls, tp_count, fp_count, fn_count, polyp_precision, polyp_recall = results

    print("TP : %d, FP : %d, FN : %d " % (tp_count, fp_count, fn_count))
    print("{:<14}{:<6}{}".format('polyp', 'Precision',
                                 round(polyp_precision, 3)))
    print("{:<14}{:<6}{}".format('polyp', 'Recall', round(polyp_recall, 3)))
    # for i in range(1, len(average_precisions)):
    #     print("{:<14}{:<6}{}".format(classes[i], 'AP', round(average_precisions[i], 3)))
    #
    # print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3)))
    # print('Precisions', precisions)
    # print('Recalls', recalls)

    m = max((Config.n_classes + 1) // 2, 2)
    n = 2

    fig, cells = plt.subplots(m, n, figsize=(n * 8, m * 8))
    val = 0
    for i in range(m):
        for j in range(n):
            if n * i + j + 1 > Config.n_classes: break
            cells[i, j].plot(recalls[n * i + j + 1],
                             precisions[n * i + j + 1],
                             color='blue',
                             linewidth=1.0)
            cells[i, j].set_xlabel('recall', fontsize=14)
            cells[i, j].set_ylabel('precision', fontsize=14)
            cells[i, j].grid(True)
            cells[i, j].set_xticks(np.linspace(0, 1, 11))
            cells[i, j].set_yticks(np.linspace(0, 1, 11))
            cells[i, j].set_title("{}, AP: {:.3f}".format(
                classes[n * i + j + 1], average_precisions[n * i + j + 1]),
                                  fontsize=16)
            image = plt.gcf()
            # plt.show()
            plt.draw()
            image.savefig(os.getcwd() + "/test_out_512/test_" + str(val) +
                          ".png",
                          dpi=100)
            val += 1
示例#17
0
def demo_tuples(net, image_name):
    """Detect objects, attributes and relations in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes, attr_scores, rel_scores = im_detect(net, im)
    if attr_scores is not None:
        print 'Found attribute scores'
    if rel_scores is not None:
        print 'Found relation scores'
        rel_scores = rel_scores[:, 1:]  # drop no relation
        rel_argmax = np.argmax(rel_scores, axis=1).reshape(
            (boxes.shape[0], boxes.shape[0]))
        rel_score = np.max(rel_scores, axis=1).reshape(
            (boxes.shape[0], boxes.shape[0]))

    timer.toc()
    print('Detection took {:.3f}s for '
          '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.05
    ATTR_THRESH = 0.1

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im)

    # Detections
    det_indices = []
    det_scores = []
    det_objects = []
    det_bboxes = []
    det_attrs = []

    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = np.array(nms(dets, NMS_THRESH))
        dets = dets[keep, :]
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]

        if len(inds) > 0:
            keep = keep[inds]
            for k in keep:
                det_indices.append(k)
                det_bboxes.append(cls_boxes[k])
                det_scores.append(cls_scores[k])
                det_objects.append(cls)
                if attr_scores is not None:
                    attr_inds = np.where(attr_scores[k][1:] >= ATTR_THRESH)[0]
                    det_attrs.append([ATTRS[ix] for ix in attr_inds])
                else:
                    det_attrs.append([])

    rel_score = rel_score[det_indices].T[det_indices].T
    rel_argmax = rel_argmax[det_indices].T[det_indices].T
    for i, (idx, score, obj, bbox, attr) in enumerate(
            zip(det_indices, det_scores, det_objects, det_bboxes, det_attrs)):
        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1],
                          fill=False,
                          edgecolor='red',
                          linewidth=3.5))
        box_text = '{:s} {:.3f}'.format(obj, score)
        if len(attr) > 0:
            box_text += "(" + ",".join(attr) + ")"
        ax.text(bbox[0],
                bbox[1] - 2,
                box_text,
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14,
                color='white')

        # Outgoing
        score = np.max(rel_score[i])
        ix = np.argmax(rel_score[i])
        subject = det_objects[ix]
        relation = RELATIONS[rel_argmax[i][ix]]
        print 'Relation: %.2f %s -> %s -> %s' % (score, obj, relation, subject)
        # Incoming
        score = np.max(rel_score.T[i])
        ix = np.argmax(rel_score.T[i])
        subject = det_objects[ix]
        relation = RELATIONS[rel_argmax[ix][i]]
        print 'Relation: %.2f %s -> %s -> %s' % (score, subject, relation, obj)

    ax.set_title(('detections with '
                  'p(object|box) >= {:.1f}').format(CONF_THRESH),
                 fontsize=14)
    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    plt.savefig('data/demo/' +
                im_file.split('/')[-1].replace(".jpg", "_demo.jpg"))
示例#18
0
    def __init__(self):
        """Create the different objects present in the Simulator and place them in it"""
        self.ball = plt.Circle((1800, 300), 30, fc='r', label='ball')
        self.box1 = plt.Rectangle((1800, 450),
                                  67,
                                  67,
                                  fc='y',
                                  linewidth=3.5,
                                  label='box1')
        self.box2 = plt.Rectangle((2000, 900),
                                  67,
                                  67,
                                  fc='grey',
                                  linewidth=3.5,
                                  label='box2')
        self.robobo = patches.Rectangle((700, 300),
                                        75,
                                        60,
                                        angle=0.0,
                                        fc='cyan',
                                        label='robobo')
        self.robobo_act = patches.Rectangle((775, 300),
                                            20,
                                            60,
                                            angle=0.0,
                                            fc='blue',
                                            label='robobo_actuator')
        # self.baxter_rarm = patches.Rectangle((2000, 50), 75, 60, angle=0.0, fc=(0.8, 0, 0.2), label='baxter_rarm')
        # self.baxter_rarm_act = patches.Rectangle((2075, 50), 20, 60, angle=0.0, fc='black', label='baxter_rarm_act')
        # self.baxter_larm = patches.Rectangle((1600, 50), 75, 60, angle=0.0, fc=(0.8, 0, 0.2), label='baxter_larm')
        # self.baxter_larm_act = patches.Rectangle((1675, 50), 20, 60, angle=0.0, fc='black', label='baxter_larm_act')

        self.baxter_figure = patches.Circle((2700, 264), 12, fc=(0.8, 0, 0, 1))
        self.baxter_figure_1 = patches.Circle((2700, 264),
                                              12,
                                              fc=(0.8, 0, 0, 0.8))
        self.baxter_figure_2 = patches.Circle((2700, 264),
                                              12,
                                              fc=(0.8, 0, 0, 0.6))
        self.baxter_figure_3 = patches.Circle((2700, 264),
                                              12,
                                              fc=(0.8, 0, 0, 0.4))
        self.baxter_figure_4 = patches.Circle((2700, 264),
                                              12,
                                              fc=(0.8, 0, 0, 0.2))
        self.baxter_figure_5 = patches.Circle((2700, 264),
                                              12,
                                              fc=(0.8, 0, 0, 0.0))

        self.fig = plt.figure()
        self.fig.canvas.set_window_title('Simulator')
        self.ax = plt.axes(xlim=(0, 3500), ylim=(0, 1000))
        self.ax.axes.get_xaxis().set_visible(False)
        self.ax.axes.get_yaxis().set_visible(False)

        # Movement boundaries
        plt.axvline(
            x=1250)  # draw a default vline at x=1 that spans the yrange
        plt.axhline(y=800,
                    xmin=0.357,
                    xmax=0.686,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=50,
                    xmin=0.0286,
                    xmax=0.686,
                    linestyle='--',
                    color='grey')
        # plt.axhline(y=950, xmin=0.0286, xmax=0.357, linestyle='--', color='grey')
        plt.axhline(y=800,
                    xmin=0.0286,
                    xmax=0.357,
                    linestyle='--',
                    color='grey')
        # plt.axvline(x=100, ymin=0.05, ymax=0.95, linestyle='--', color='grey')
        plt.axvline(x=100, ymin=0.05, ymax=0.80, linestyle='--', color='grey')
        plt.axvline(x=2400, ymin=0.05, ymax=0.80, linestyle='--', color='grey')
        self.ball_position = None  # Indicates where is the ball: robobo, baxter_larm, bater_rarm, box1, box2 or None

        # Show figure and patches
        self.fig.show()
        self.ax.add_patch(self.box1)
        self.ax.add_patch(self.box2)
        self.ax.add_patch(self.robobo)
        self.ax.add_patch(self.robobo_act)
        # self.ax.add_patch(self.baxter_rarm)
        # self.ax.add_patch(self.baxter_rarm_act)
        # self.ax.add_patch(self.baxter_larm)
        # self.ax.add_patch(self.baxter_larm_act)
        self.ax.add_patch(self.ball)

        #plt.text(2700, 970, 'State Space')

        # Prueba espacio estados
        plt.axhline(y=950,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='-',
                    color='black',
                    linewidth=1.3)
        plt.axhline(y=264,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='-',
                    color='black',
                    linewidth=1.3)
        plt.axhline(y=364,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=464,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=564,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=664,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=764,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axhline(y=864,
                    xmin=0.771,
                    xmax=0.967,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=2700,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='-',
                    color='black',
                    linewidth=1.3)
        plt.axvline(x=3386,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='-',
                    color='black',
                    linewidth=1.3)
        plt.axvline(x=2800,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=2900,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=3000,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=3100,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=3200,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=3300,
                    ymin=0.264,
                    ymax=0.950,
                    linestyle='--',
                    color='grey')
        plt.axvline(x=2500)
        self.ax.add_patch(self.baxter_figure)
        self.ax.add_patch(self.baxter_figure_1)
        self.ax.add_patch(self.baxter_figure_2)
        self.ax.add_patch(self.baxter_figure_3)
        self.ax.add_patch(self.baxter_figure_4)
        self.ax.add_patch(self.baxter_figure_5)

        self.active_goal = 'goal_box1'  # Variable to determine the active goal at each moment
示例#19
0
def compare_events(events: list) -> None:
    r'''
    Plots at least two events side by side in their transverse and longitudinal projections

    Arguments:
        events: list of DataFrames containing vector coordinates for 3 momenta
    '''

    # TODO: check typing, why list?
    # TODO: make this work with a single event
    # TODO: add plot settings & saving

    with sns.axes_style('whitegrid'), sns.color_palette('tab10'):
        fig, axs = plt.subplots(3,
                                len(events),
                                figsize=(9 * len(events), 18),
                                gridspec_kw={'height_ratios': [1, 0.5, 0.5]})
        for vector in [
                x[:-3] for x in events[0].columns if '_px' in x.lower()
        ]:
            for i, in_data in enumerate(events):
                x = in_data[vector + '_px'].values[0]
                try:
                    y = in_data[vector + '_py'].values[0]
                except KeyError:
                    y = 0
                try:
                    z = in_data[vector + '_pz'].values[0]
                except KeyError:
                    z = 0
                axs[0, i].plot((0, x), (0, y), label=vector)
                axs[1, i].plot((0, z), (0, x), label=vector)
                axs[2, i].plot((0, z), (0, y), label=vector)
        for ax in axs[0]:
            ax.add_artist(
                plt.Circle((0, 0), 1, color='grey', fill=False, linewidth=2))
            ax.set_xlim(-1.1, 1.1)
            ax.set_ylim(-1.1, 1.1)
            ax.set_xlabel(r"$p_x$", fontsize=16, color='black')
            ax.set_ylabel(r"$p_y$", fontsize=16, color='black')
            ax.legend(loc='right', fontsize=12)
        for ax in axs[1]:
            ax.add_artist(
                plt.Rectangle((-2, -1),
                              4,
                              2,
                              color='grey',
                              fill=False,
                              linewidth=2))
            ax.set_xlim(-2.2, 2.2)
            ax.set_ylim(-1.1, 1.1)
            ax.set_xlabel(r"$p_z$", fontsize=16, color='black')
            ax.set_ylabel(r"$p_x$", fontsize=16, color='black')
            ax.legend(loc='right', fontsize=12)
        for ax in axs[2]:
            ax.add_artist(
                plt.Rectangle((-2, -1),
                              4,
                              2,
                              color='grey',
                              fill=False,
                              linewidth=2))
            ax.set_xlim(-2.2, 2.2)
            ax.set_ylim(-1.1, 1.1)
            ax.set_xlabel(r"$p_z$", fontsize=16, color='black')
            ax.set_ylabel(r"$p_y$", fontsize=16, color='black')
            ax.legend(loc='right', fontsize=12)
        fig.show()
示例#20
0
def vis_two(im_array, dets1, dets2, thresh=0.9):
    """Visualize detection results before and after calibration

    Parameters:
    ----------
    im_array: numpy.ndarray, shape(1, c, h, w)
        test image in rgb
    dets1: numpy.ndarray([[x1 y1 x2 y2 score]])
        detection results before calibration
    dets2: numpy.ndarray([[x1 y1 x2 y2 score]])
        detection results after calibration
    thresh: float
        boxes with scores > thresh will be drawn in red otherwise yellow

    Returns:
    -------
    """
    import matplotlib.pyplot as plt
    import random

    figure = plt.figure()
    plt.subplot(121)
    plt.imshow(im_array)
    color = 'yellow'

    for i in range(dets1.shape[0]):
        bbox = dets1[i, :4]
        landmarks = dets1[i, 5:]
        score = dets1[i, 4]
        if score > thresh:
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1],
                                 fill=False,
                                 edgecolor='red',
                                 linewidth=0.7)
            plt.gca().add_patch(rect)
            landmarks = landmarks.reshape((5, 2))
            for j in range(5):
                plt.scatter(landmarks[j, 0],
                            landmarks[j, 1],
                            c='yellow',
                            linewidths=0.1,
                            marker='x',
                            s=5)

            # plt.gca().text(bbox[0], bbox[1] - 2,
            #                '{:.3f}'.format(score),
            #                bbox=dict(facecolor='blue', alpha=0.5), fontsize=12, color='white')
        # else:
        #     rect = plt.Rectangle((bbox[0], bbox[1]),
        #                          bbox[2] - bbox[0],
        #                          bbox[3] - bbox[1], fill=False,
        #                          edgecolor=color, linewidth=0.5)
        #     plt.gca().add_patch(rect)

    plt.subplot(122)
    plt.imshow(im_array)
    color = 'yellow'

    for i in range(dets2.shape[0]):
        bbox = dets2[i, :4]
        landmarks = dets1[i, 5:]
        score = dets2[i, 4]
        if score > thresh:
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1],
                                 fill=False,
                                 edgecolor='red',
                                 linewidth=0.7)
            plt.gca().add_patch(rect)

            landmarks = landmarks.reshape((5, 2))
            for j in range(5):
                plt.scatter(landmarks[j, 0],
                            landmarks[j, 1],
                            c='yellow',
                            linewidths=0.1,
                            marker='x',
                            s=5)

            # plt.gca().text(bbox[0], bbox[1] - 2,
            #                '{:.3f}'.format(score),
            #                bbox=dict(facecolor='blue', alpha=0.5), fontsize=12, color='white')
        # else:
        #     rect = plt.Rectangle((bbox[0], bbox[1]),
        #                          bbox[2] - bbox[0],
        #                          bbox[3] - bbox[1], fill=False,
        #                          edgecolor=color, linewidth=0.5)
        #     plt.gca().add_patch(rect)
    plt.show()
示例#21
0
def hinton(data,
           filename,
           row_labels=None,
           column_labels=None,
           displayed_mutations=None,
           drivers=None):
    """
    Draw Hinton diagram for visualizing mutation data of multiple samples.
    :param data: mutation data
    :param filename: path to the output file of the created figure (without ending), add pdf and png later
    :param row_labels: sample names
    :param column_labels: names of genes where the variant occurred
    :param displayed_mutations: depict only the given list of mutations in the table
    :param drivers: highlight mutations associated with cancer
    """

    # size and position settings
    height = 4
    width = 2
    y_spacing = 1
    label_x_pos = -2
    label_y_pos = 0

    # show all mutations in the table if no subset is given
    if displayed_mutations is None:
        displayed_mutations = [i for i in range(len(data))]

    x_length = ((-label_x_pos + 20) if row_labels is not None else
                0) + (len(displayed_mutations) * width)
    y_length = len(data[0]) * (height + y_spacing) - y_spacing + (
        label_y_pos + 20 if column_labels is not None else 0)

    # create new figure
    plt.figure(figsize=(x_length / 20.0, y_length / 20.0), dpi=150)

    ax = plt.axes([0, 1, 1, 1])

    ax.patch.set_facecolor('white')
    ax.set_aspect('auto')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    # sort mutation table according to status
    priorities = [0 for _ in range(len(data))]
    for mut_idx in displayed_mutations:
        for sa_idx, maf in enumerate(data[mut_idx]):
            if maf > 0:  # mutation is present
                priorities[mut_idx] += 3 * (4**(len(data[mut_idx]) - sa_idx -
                                                1))
            elif maf == POS_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                priorities[mut_idx] += 1 * (4**(len(data[mut_idx]) - sa_idx -
                                                1))
            elif maf == NEG_UNKNOWN:
                priorities[mut_idx] += 1 * (4**(len(data[mut_idx]) - sa_idx -
                                                1))
            else:
                priorities[mut_idx] += 0 * (4**(len(data[mut_idx]) - sa_idx -
                                                1))

    edge_color = 'black'

    for x_pos, mut_idx in enumerate(
            sorted(displayed_mutations,
                   key=lambda k: (-priorities[k], column_labels[k]
                                  if column_labels is not None else 0))):

        for sa_idx, maf in enumerate(data[mut_idx]):
            if maf > 0:  # mutation is present
                color = 'blue'
            elif maf == POS_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                color = (0.9, 0.75, 0.75)
            elif maf == NEG_UNKNOWN:
                # merge the two unknown categories when variants are displayed
                color = (0.9, 0.75, 0.75)
            else:
                color = (1.0, 0.3, 0.3)

            rect = plt.Rectangle([
                x_pos * width,
                (height + y_spacing) * (len(data[mut_idx]) - sa_idx - 1)
            ],
                                 width,
                                 height,
                                 facecolor=color,
                                 edgecolor=edge_color)
            ax.add_patch(rect)

    if row_labels is not None:
        for sa_idx, row_name in enumerate(row_labels):
            ax.text(label_x_pos,
                    (height + y_spacing) * (len(row_labels) - sa_idx - 1) + 1,
                    row_name.replace('_', ' '),
                    horizontalalignment='right',
                    verticalalignment='bottom',
                    fontsize=12)

    if column_labels is not None:
        for x_pos, mut_idx in enumerate(
                sorted(displayed_mutations,
                       key=lambda k: (-priorities[k], column_labels[k]))):

            ax.text(x_pos * width + (width / 2) + 0.2,
                    label_y_pos + (height + y_spacing) * (len(data[mut_idx])),
                    _format_gene_name(column_labels[mut_idx], max_length=12),
                    rotation='vertical',
                    horizontalalignment='center',
                    verticalalignment='bottom',
                    fontsize=8,
                    color='red' if drivers is not None
                    and column_labels[mut_idx] in drivers else 'black')

    ax.autoscale_view()

    plt.savefig(filename + '.pdf',
                dpi=150,
                bbox_inches='tight',
                transparent=True)
    plt.savefig(filename + '.png',
                dpi=150,
                bbox_inches='tight',
                transparent=True)
    plt.close()
    logger.info('Generated mutation table plot: {}'.format(filename + '.pdf'))
示例#22
0
            current_axis = plt.gca()
            plt.axis('off')

        #bounding box with label definition
        xmin = bb[0]
        ymin = bb[1]
        xmax = bb[2]
        ymax = bb[3]

        color = 'red'
        label = '{}: {:.3f}'.format('Distance', d_Y_prediction[i])
        current_axis.add_patch(
            plt.Rectangle((xmin, ymin),
                          xmax - xmin,
                          ymax - ymin,
                          color=color,
                          fill=False,
                          linewidth=2))
        current_axis.text(xmin,
                          ymin,
                          label,
                          size='x-large',
                          color='white',
                          bbox={
                              'facecolor': color,
                              'alpha': 1.0
                          })

        i += 1
        frame_prec = frame
        print(i)
示例#23
0
def create_incompatible_mp_table(patient,
                                 filename,
                                 phylogeny,
                                 row_labels=None,
                                 column_labels=None):
    """
    Draw Hinton diagram for visualizing mutation data of multiple samples.
    :param patient: mutation data
    :param filename: path to the output file of the created figure (without ending), add pdf and png later
    :param phylogeny: data structure around the inferred phylogenetic tree
    :param row_labels: sample names
    :param column_labels: names of genes where the variant occurred
    """

    # size and position settings
    height = 4
    width = 1
    y_spacing = 1
    label_x_pos = -2
    label_y_pos = 0
    cb_width = 30.0
    x_space = 5.0

    # log probability to be classified with at least the given confidence threshold
    conf_clas_lpth = math.log(def_sets.CLA_CONFID_TH)
    opt_sol = phylogeny.solutions[0]  # optimal solution

    if isinstance(phylogeny, SimplePhylogeny):
        displayed_mutations = [
            mut_idx for mut_idx in phylogeny.conflicting_mutations
        ]
    elif isinstance(phylogeny, MaxLHPhylogeny):
        displayed_mutations = [
            mut_idx for mut_idx in set(opt_sol.false_positives.keys()).union(
                set(opt_sol.false_negatives.keys()))
        ]
        if opt_sol.conflicting_mutations is not None:
            for mut_idx in opt_sol.conflicting_mutations:
                displayed_mutations.append(mut_idx)

    # elif isinstance(phylogeny, SubclonalPhylogeny):
    #     logger.warning('Illustrative mutation table not yet implemented for subclonal detections.')
    #     return
    else:
        logger.error(
            'Could not create illustrative mutation table of incompatible mutation patterns. '
        )
        logger.error('Phylogeny object is of wrong type! ')
        return -1, -1

    if len(displayed_mutations) == 0:
        logger.info('There were no evolutionarily incompatible mutations!')
        return -1, -1

    x_length = ((-label_x_pos + 20) if row_labels is not None else
                0) + (len(displayed_mutations) * width * 3)
    y_length = (len(patient.data[0]) * (height + y_spacing) - y_spacing +
                (label_y_pos + 20 if column_labels is not None else 0))

    x_length += x_space + cb_width

    # create new figure
    fig = plt.figure(figsize=(x_length / 20.0, y_length / 20.0), dpi=150)

    ax = plt.axes([0, 1, (x_length - cb_width - x_space) / x_length, 1])
    ax.axis('off')

    ax.patch.set_facecolor('white')
    ax.patch.set_edgecolor(None)
    # ax.set_aspect('auto')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    present_color = 'blue'
    absent_color = (1.0, 0.3, 0.3)
    unknown_color = (0.9, 0.75, 0.75)

    for x_pos, mut_idx in enumerate(
            sorted(displayed_mutations,
                   key=lambda k: (column_labels[k].lower()
                                  if column_labels is not None else k))):

        for sa_idx, (p0, p1) in enumerate(patient.log_p01[mut_idx]):

            cov = float(patient.coverage[patient.mut_keys[mut_idx]][
                patient.sample_names[sa_idx]])
            if cov > 0:
                raw_maf = (float(patient.mut_reads[patient.mut_keys[mut_idx]][
                    patient.sample_names[sa_idx]]) / cov)
            else:
                raw_maf = 0.0

            if p1 > conf_clas_lpth:  # mutation is present
                class_color = present_color
            elif p0 > conf_clas_lpth:  # mutation is absent
                class_color = absent_color
            else:  # mutation can not be classified according to the Bayesian inference model
                class_color = unknown_color

            maf_color = plt.cm.Blues(2.0 * raw_maf if raw_maf < 0.5 else 1.0)
            cov_color = plt.cm.Greens(
                math.log(cov, 10) /
                3 if 0 < cov < 1000 else 0.0 if cov <= 0.0 else 1.0)

            rect_maf = plt.Rectangle(
                [(x_pos * width * 3) + 0, (height + y_spacing) *
                 (len(patient.data[mut_idx]) - sa_idx - 1)],
                width,
                height,
                linewidth=0,
                facecolor=maf_color)
            rect_cov = plt.Rectangle(
                [(x_pos * width * 3) + 1, (height + y_spacing) *
                 (len(patient.data[mut_idx]) - sa_idx - 1)],
                width,
                height,
                linewidth=0,
                facecolor=cov_color)
            box = plt.Rectangle([(x_pos * width * 3), (height + y_spacing) *
                                 (len(patient.data[mut_idx]) - sa_idx - 1)],
                                width * 2,
                                height,
                                linewidth=4,
                                facecolor=None,
                                edgecolor=class_color,
                                clip_on=False)
            ax.add_patch(box)
            ax.add_patch(rect_maf)
            ax.add_patch(rect_cov)

            if isinstance(phylogeny, MaxLHPhylogeny):
                if mut_idx in opt_sol.false_positives.keys() and \
                        sa_idx in opt_sol.false_positives[mut_idx]:

                    af = plt.Rectangle(
                        [(x_pos * width * 3), (height + y_spacing) *
                         (len(patient.data[mut_idx]) - sa_idx - 1)],
                        width * 2,
                        height / 2,
                        facecolor=absent_color,
                        linewidth=0)
                    ax.add_patch(af)

                elif mut_idx in opt_sol.false_negatives.keys(
                ) and sa_idx in opt_sol.false_negatives[mut_idx]:
                    af = plt.Rectangle(
                        [(x_pos * width * 3), (height + y_spacing) *
                         (len(patient.data[mut_idx]) - sa_idx - 1)],
                        width * 2,
                        height / 2,
                        facecolor=present_color,
                        linewidth=0)
                    ax.add_patch(af)

    if row_labels is not None:
        for sa_idx, row_name in enumerate(row_labels):
            ax.text(label_x_pos,
                    (height + y_spacing) * (len(row_labels) - sa_idx - 1) + 1,
                    row_name.replace('_', ' '),
                    horizontalalignment='right',
                    verticalalignment='bottom',
                    fontsize=12)

    if column_labels is not None:
        for x_pos, mut_idx in enumerate(
                sorted(displayed_mutations,
                       key=lambda k: (column_labels[k].lower()))):

            ax.text(x_pos * width * 3 + width + 0.1,
                    label_y_pos + (height + y_spacing) *
                    (len(patient.data[mut_idx])),
                    _format_gene_name(column_labels[mut_idx], max_length=12),
                    rotation='vertical',
                    horizontalalignment='center',
                    verticalalignment='bottom',
                    fontsize=8)

    # draw colorbar legend to MAFs
    ax1 = fig.add_axes([
        (x_length - cb_width) / x_length + (cb_width / 4 / x_length), 1.05,
        0.04, 0.9
    ])
    # Set the colormap and norm to correspond to the data for which the colorbar will be used.
    cmap = cm.Blues
    norm = mpl.colors.Normalize(vmin=0, vmax=0.5)

    cb1 = mpl.colorbar.ColorbarBase(ax1,
                                    cmap=cmap,
                                    norm=norm,
                                    orientation='vertical')
    cb1.ax.tick_params(labelsize=8)
    cb1.ax.yaxis.set_ticks_position('left')
    cb1.set_label('VAF')

    # draw colorbar legend to MAFs
    ax2 = fig.add_axes([
        (x_length - (cb_width / 2)) / x_length + (cb_width / 4 / x_length),
        1.05, 0.04, 0.9
    ])
    # Set the colormap and norm to correspond to the data for which the colorbar will be used.
    cmap = cm.Greens

    cb2 = mpl.colorbar.ColorbarBase(ax2,
                                    cmap=cmap,
                                    norm=mpl.colors.LogNorm(vmin=1, vmax=1000),
                                    orientation='vertical')
    cb2.ax.tick_params(labelsize=8)
    cb2.ax.yaxis.set_ticks_position('left')
    cb2.set_label('Coverage')

    ax.autoscale_view()

    plt.savefig(filename + '.pdf',
                dpi=150,
                bbox_inches='tight',
                transparent=True)
    plt.savefig(filename + '.png', bbox_inches='tight', transparent=True)
    plt.close()
    logger.info(
        'Generated illustrative mutation table plot of incompatible mutation patterns: {}'
        .format(filename + '.pdf'))

    return x_length, y_length
示例#24
0
def rdm_compare(rdms, models, comp=None, plot=None):
    '''function to compare target and model rdms'''

    global DefaultListOrderedDict
    from collections import OrderedDict

    import pandas as pd
    from scipy.spatial import distance
    from nilearn.connectome import sym_matrix_to_vec, vec_to_sym_matrix
    from scipy.stats import rankdata, spearmanr, kendalltau, pearsonr, mstats
    import numpy as np
    from itertools import combinations
    import pickle
    import seaborn as sns
    import matplotlib.pyplot as plt
    import copy

    class DefaultListOrderedDict(OrderedDict):
        def __missing__(self, k):
            self[k] = []
            return self[k]

    if isinstance(rdms, str) is True:
        with open(rdms, 'rb') as f:
            dict_rdms = pickle.load(f)
        target_rdms = copy.deepcopy(dict_rdms['rdm'])
        target_conds = target_rdms[0].keys()
    else:
        target_rdms = rdms
        target_conds = rdms[0].keys()

    if isinstance(models, str) is True:
        with open(models, 'rb') as f:
            dict_models = pickle.load(f)
            models = dict_models['rdm']
            model_ids = dict_models['id']

            models_zip = [x for _, x in sorted(zip(model_ids, models))]

            dict_models['rdm'] = models_zip
            models = models_zip

    else:
        models = models

    for rdm in dict_models['rdm']:
        if 'Unnamed: 0' in rdm:
            del rdm['Unnamed: 0']

    for index, rdm in enumerate(target_rdms):
        target_rdms[index] = target_rdms[index].to_numpy()

    list_cor_rdm = list(range(0, len(target_rdms)))
    list_p = list(range(0, len(target_rdms)))
    target_rdms_trans = list(range(0, len(target_rdms)))

    if comp is None or comp == 'spearman':
        for index, rdm in enumerate(target_rdms):
            target_rdms_trans[index] = vec_to_sym_matrix(
                rankdata(sym_matrix_to_vec(rdm)))
            rdm_avg = pd.DataFrame(np.mean(target_rdms_trans, axis=0),
                                   columns=target_conds)

        for index, part_rdm in enumerate(target_rdms_trans):
            list_cor_rdm[index], list_p[index] = spearmanr(
                sym_matrix_to_vec(part_rdm, discard_diagonal=True),
                sym_matrix_to_vec(rdm_avg.to_numpy(), discard_diagonal=True))

        list_cor_sub = list()
        list_cor_rdm_sub = list()
        list_p_sub = list()

        for index, part in enumerate(target_rdms_trans):
            tmp_rdms = target_rdms_trans.copy()
            tmp_part = target_rdms_trans[index]
            tmp_rdms.pop(index)
            tmp_rdm_avg = np.mean(tmp_rdms, axis=0)
            list_cor_sub.append(
                spearmanr(
                    sym_matrix_to_vec(tmp_part, discard_diagonal=True),
                    sym_matrix_to_vec(tmp_rdm_avg, discard_diagonal=True)))

        for i, cor in enumerate(list_cor_sub):
            list_cor_rdm_sub.append(cor.correlation)
            list_p_sub.append(cor.pvalue)

    elif comp == 'kendalltaua':
        for index, rdm in enumerate(target_rdms):
            target_rdms_trans[index] = vec_to_sym_matrix(
                rankdata(sym_matrix_to_vec(rdm)))
            rdm_avg = pd.DataFrame(np.mean(target_rdms, axis=0),
                                   columns=target_conds)

        for index, part_rdm in enumerate(target_rdms):
            list_cor_rdm[index], list_p[index] = kendalltau(
                sym_matrix_to_vec(part_rdm, discard_diagonal=True),
                sym_matrix_to_vec(rdm_avg.to_numpy(), discard_diagonal=True))

        list_cor_sub = list()
        list_cor_rdm_sub = list()
        list_p_sub = list()

        for index, part in enumerate(target_rdms):
            tmp_rdms = target_rdms.copy()
            tmp_part = target_rdms[index]
            tmp_rdms.pop(index)
            tmp_rdm_avg = np.mean(tmp_rdms, axis=0)
            list_cor_sub.append(
                kendalltau(
                    sym_matrix_to_vec(tmp_part, discard_diagonal=True),
                    sym_matrix_to_vec(tmp_rdm_avg, discard_diagonal=True)))

        for i, cor in enumerate(list_cor_sub):
            list_cor_rdm_sub.append(cor.correlation)
            list_p_sub.append(cor.pvalue)

    elif comp == 'pearson':
        for index, rdm in enumerate(target_rdms):
            target_rdms_trans[index] = vec_to_sym_matrix(
                mstats.zscore(sym_matrix_to_vec(rdm)))
            rdm_avg = pd.DataFrame(np.mean(target_rdms_trans, axis=0),
                                   columns=target_conds)

        for index, part_rdm in enumerate(target_rdms_trans):
            list_cor_rdm[index], list_p[index] = pearsonr(
                sym_matrix_to_vec(part_rdm, discard_diagonal=True),
                sym_matrix_to_vec(rdm_avg.to_numpy(), discard_diagonal=True))

        list_cor_sub = list()
        list_cor_rdm_sub = list()
        list_p_sub = list()

        for index, part in enumerate(target_rdms_trans):
            tmp_rdms = target_rdms_trans.copy()
            tmp_part = target_rdms_trans[index]
            tmp_rdms.pop(index)
            tmp_rdm_avg = np.mean(tmp_rdms, axis=0)
            list_cor_sub.append(
                pearsonr(sym_matrix_to_vec(tmp_part, discard_diagonal=True),
                         sym_matrix_to_vec(tmp_rdm_avg,
                                           discard_diagonal=True)))

        for i, cor in enumerate(list_cor_sub):
            list_cor_rdm_sub.append(cor[0])
            list_p_sub.append(cor[1])

    upper_noise_ceiling = np.mean(list_cor_rdm)
    lower_noise_ceiling = np.mean(list_cor_rdm_sub)

    model_comp = pd.DataFrame(
        columns=['participant', 'models', 'cor'],
        index=np.arange(len(dict_models['id']) * len(dict_rdms['id'])))
    model_comp['participant'] = dict_rdms['id'] * len(dict_models['id'])
    model_comp['models'] = sorted(dict_models['id'] * len(dict_rdms['id']))

    list_cor_models = list()

    snd_rdms = list()
    snd_rdms.append(
        sym_matrix_to_vec(rdm_avg.to_numpy(), discard_diagonal=True))
    for mod_rdm in models:
        snd_rdms.append(
            sym_matrix_to_vec(mod_rdm.to_numpy(), discard_diagonal=True))

    ids_rdms = list()
    ids_rdms.append('group average')
    for mod_ids in sorted(model_ids):
        ids_rdms.append(mod_ids)

    if comp is None or comp == 'spearman':
        for index, model_rdm in enumerate(dict_models['rdm']):
            for i, sub_rdm in enumerate(target_rdms_trans):
                list_cor_models.append(
                    spearmanr(
                        sym_matrix_to_vec(sub_rdm, discard_diagonal=True),
                        sym_matrix_to_vec(model_rdm.to_numpy(),
                                          discard_diagonal=True)).correlation)
                rdms_dist = [
                    spearmanr(x, y).correlation
                    for x, y in combinations(snd_rdms, 2)
                ]
                rdms_dist = pd.DataFrame(distance.squareform(rdms_dist),
                                         columns=ids_rdms)
                np.fill_diagonal(rdms_dist.values, 1)
                rdms_dist = rdms_dist.mask(rdms_dist.values > -1.05,
                                           1 - rdms_dist.values)
    elif comp == 'kendalltaua':
        for index, model_rdm in enumerate(dict_models['rdm']):
            for i, sub_rdm in enumerate(target_rdms_trans):
                list_cor_models.append(
                    kendalltau(
                        sym_matrix_to_vec(sub_rdm, discard_diagonal=True),
                        rankdata(
                            sym_matrix_to_vec(
                                model_rdm.to_numpy(),
                                discard_diagonal=True))).correlation)
                rdms_dist = [
                    kendalltau(x, y).correlation
                    for x, y in combinations(snd_rdms, 2)
                ]
                rdms_dist = pd.DataFrame(distance.squareform(rdms_dist),
                                         columns=ids_rdms)
                #rdms_dist = rdms_dist.mask(rdms_dist.values > 0, 1 - rdms_dist.values)
    elif comp == 'pearson':
        for index, model_rdm in enumerate(dict_models['rdm']):
            for i, sub_rdm in enumerate(target_rdms_trans):
                list_cor_models.append(
                    pearsonr(
                        sym_matrix_to_vec(sub_rdm, discard_diagonal=True),
                        sym_matrix_to_vec(model_rdm.to_numpy(),
                                          discard_diagonal=True))[0])
                rdms_dist = [
                    pearsonr(x, y)[0] for x, y in combinations(snd_rdms, 2)
                ]
                rdms_dist = pd.DataFrame(distance.squareform(rdms_dist),
                                         columns=ids_rdms)
                np.fill_diagonal(rdms_dist.values, 1)
                rdms_dist = rdms_dist.mask(rdms_dist.values > -1.05,
                                           1 - rdms_dist.values)

    model_comp['cor'] = list_cor_models
    model_comp['upper_noise_ceiling'] = upper_noise_ceiling
    model_comp['lower_noise_ceiling'] = lower_noise_ceiling

    sns.set_style('darkgrid')
    sns.set_context('paper')

    if plot is None:
        print('results will not be plotted')
    elif plot == 'bar':
        ax = sns.barplot(x=model_comp['models'],
                         y=model_comp['cor'],
                         data=model_comp)
        plt.plot(np.linspace(0, 1, 1000), [upper_noise_ceiling] * 1000,
                 'r',
                 alpha=0.1)
        plt.plot(np.linspace(0, 1, 1000), [lower_noise_ceiling] * 1000,
                 'r',
                 alpha=0.1)
        rect = plt.Rectangle((-20, lower_noise_ceiling),
                             10000,
                             (upper_noise_ceiling - lower_noise_ceiling),
                             color='r',
                             alpha=0.5)
        ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
        if comp is None or comp == 'spearman':
            ax.set(ylabel='spearman correlation with target RDM')
        if comp == 'pearson':
            ax.set(ylabel='pearson correlation with target RDM')
        if comp == 'kendalltaua':
            ax.set(ylabel='kendall tau a correlation with target RDM')
        ax.add_patch(rect)
        plt.tight_layout()
    elif plot == 'violin':
        ax = sns.violinplot(x=model_comp['models'],
                            y=model_comp['cor'],
                            data=model_comp)
        plt.plot(np.linspace(0, 1, 1000), [upper_noise_ceiling] * 1000,
                 'r',
                 alpha=0.1)
        plt.plot(np.linspace(0, 1, 1000), [lower_noise_ceiling] * 1000,
                 'r',
                 alpha=0.1)
        rect = plt.Rectangle((-20, lower_noise_ceiling),
                             10000,
                             (upper_noise_ceiling - lower_noise_ceiling),
                             color='r',
                             alpha=0.5)
        ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
        if comp is None or comp == 'spearman':
            ax.set(ylabel='spearman correlation with target RDM')
        if comp == 'pearson':
            ax.set(ylabel='pearson correlation with target RDM')
        if comp == 'kendalltaua':
            ax.set(ylabel='kendall tau a correlation with target RDM')
        ax.add_patch(rect)
        plt.tight_layout()

    return rdm_avg, model_comp, rdms_dist
示例#25
0
def plot_success_failure_violins(gb,
                                 envs_to_plot,
                                 reps_to_plot,
                                 pcts_to_plot,
                                 save=False,
                                 savename='',
                                 plot_title='',
                                 legend=False,
                                 **kwargs):
    if save:
        if savename == '':
            raise Exception(
                'Must pass argument to savename to specify title to save plot')

    convert_rep_to_color = kwargs.get('colors', plot_specs['rep_colors'])
    bar_width = 0.3
    fig, ax = plt.subplots(
        len(envs_to_plot),
        len(reps_to_plot) + 1,
        figsize=(12, 15),
        sharex='col',
        sharey='col',
        gridspec_kw={'height_ratios': np.ones(len(envs_to_plot))})
    for i, env in enumerate(envs_to_plot):
        if env[-2:] == '51':
            rwd_colrow = (16, 9)
        else:
            rwd_colrow = (14, 14)

        rect = plt.Rectangle(rwd_colrow, 1, 1, color='g', alpha=0.3)
        ax[i, 0].pcolor(grids[i],
                        cmap='bone_r',
                        edgecolors='k',
                        linewidths=0.1)
        ax[i, 0].axis(xmin=0, xmax=20, ymin=0, ymax=20)
        ax[i, 0].set_aspect('equal')
        ax[i, 0].add_patch(rect)
        ax[i, 0].get_xaxis().set_visible(False)
        ax[i, 0].get_yaxis().set_visible(False)
        ax[i, 0].invert_yaxis()
        violin_width = 10
        for j, rep in enumerate(reps_to_plot):
            dats_s = []
            dats_f = []
            for k, pct in enumerate(pcts_to_plot):
                list_of_ids = list(
                    gb.get_group((env, rep, cache_limits[env][pct])))
                print(list_of_ids)
                s, f = d_r_success_fail(list_of_ids)
                dats_s.append(s[1])
                dats_f.append(f[1])
                print(env, rep, pct, len(s[1]), len(f[1]),
                      len(s[1]) + len(f[1]))

            s_body = ax[i, 1].violinplot(positions=np.asarray(pcts_to_plot) +
                                         violin_width * (1 / 2 - j),
                                         dataset=dats_s,
                                         vert=True,
                                         widths=violin_width,
                                         showmeans=False)
            f_body = ax[i, 2].violinplot(positions=np.asarray(pcts_to_plot) +
                                         violin_width * (1 / 2 - j),
                                         dataset=dats_f,
                                         vert=True,
                                         widths=violin_width,
                                         showmeans=False)
            for violinkey in s_body.keys():
                if violinkey == 'bodies':
                    for b in s_body['bodies']:
                        b.set_color(convert_rep_to_color[rep])
                        b.set_alpha(75 / 100)
                else:
                    s_body[violinkey].set_color(convert_rep_to_color[rep])
            for violinkey in f_body.keys():
                if violinkey == 'bodies':
                    for b in f_body['bodies']:
                        b.set_color(convert_rep_to_color[rep])
                        b.set_alpha(25 / 100)
                else:
                    f_body[violinkey].set_color(convert_rep_to_color[rep])

        ax[i, 1].set_ylabel('Average Distance\n of Closest Memory')

    for f in range(2):
        ax[i, f + 1].set_ylim([0, 1])
        ax[i, f + 1].set_xticks([75, 50, 25])
        ax[i, f + 1].set_xlim([75 + violin_width + 2, 25 - violin_width - 2])
        ax[i, f + 1].set_xticklabels([75, 50, 25])
        ax[i, f + 1].set_xlabel('Memory Capacity (%)')

    ax[0, 1].set_title('Successful Trial', fontsize=12)
    ax[0, 2].set_title('Failed Trial', fontsize=12)

    if legend == 'reps':
        legend_patch_list = []
        for rep in reps_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color=convert_rep_to_color[rep],
                               label=labels_for_plot[rep]))
        plt.legend(handles=legend_patch_list,
                   bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                   loc='lower right',
                   ncol=len(legend_patch_list),
                   title='State Encoding')
    elif legend == 'pcts':
        legend_patch_list = []
        for pct in pcts_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color='gray', label=f'{pct}', alpha=pct / 100))
            plt.legend(handles=legend_patch_list,
                       bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                       loc='lower right',
                       ncol=len(legend_patch_list),
                       title='Episodic Memory Capacity (%)')
    elif legend == 'SF':
        legend_patch_list = []
        legend_patch_list.append(
            mpatches.Patch(color='gray',
                           label=f'Successful Trial',
                           alpha=75 / 100))
        legend_patch_list.append(
            mpatches.Patch(color='gray', label=f'Failed Trial',
                           alpha=25 / 100))
        plt.legend(handles=legend_patch_list,
                   bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                   loc='lower right',
                   ncol=len(legend_patch_list))

    if save:
        format = kwargs.get('format', 'svg')
        plt.savefig(f'../figures/CH2/{savename}.{format}', format=format)
    plt.show()
示例#26
0
def bbox_to_rect(bbox, color):
    """Convert bounding box to matplotlib format."""
    return plt.Rectangle(xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0],
                         height=bbox[3]-bbox[1], fill=False, edgecolor=color,
                         linewidth=2)
示例#27
0
def plot_success_failure_bars(gb,
                              envs_to_plot,
                              reps_to_plot,
                              pcts_to_plot,
                              save=False,
                              savename='',
                              plot_title='',
                              legend=False,
                              **kwargs):
    if save:
        if savename == '':
            raise Exception(
                'Must pass argument to savename to specify title to save plot')

    convert_rep_to_color = kwargs.get('colors', plot_specs['rep_colors'])
    bar_width = 0.3
    fig, ax = plt.subplots(len(envs_to_plot),
                           3,
                           figsize=(12, 15),
                           sharex='col',
                           sharey='col',
                           gridspec_kw={'height_ratios': [1, 1, 1, 1]})
    for i, env in enumerate(envs_to_plot):
        if env[-2:] == '51':
            rwd_colrow = (16, 9)
        else:
            rwd_colrow = (14, 14)

        rect = plt.Rectangle(rwd_colrow, 1, 1, color='g', alpha=0.3)
        ax[i, 0].pcolor(grids[i],
                        cmap='bone_r',
                        edgecolors='k',
                        linewidths=0.1)
        ax[i, 0].axis(xmin=0, xmax=20, ymin=0, ymax=20)
        ax[i, 0].set_aspect('equal')
        ax[i, 0].add_patch(rect)
        ax[i, 0].get_xaxis().set_visible(False)
        ax[i, 0].get_yaxis().set_visible(False)
        ax[i, 0].invert_yaxis()
        for j, rep in enumerate(reps_to_plot):
            for k, pct in enumerate(pcts_to_plot):
                list_of_ids = list(
                    gb.get_group((env, rep, cache_limits[env][pct])))
                print(list_of_ids)
                s, f = d_r_success_fail(list_of_ids)
                print(env, rep, pct, len(s[1]), len(f[1]),
                      len(s[1]) + len(f[1]))
                ax[i, 1].bar(k + j * bar_width,
                             np.mean(s[1]),
                             yerr=np.std(s[1]),
                             width=bar_width,
                             color=convert_rep_to_color[rep])
                ax[i, 2].bar(k + j * bar_width,
                             np.mean(f[1]),
                             yerr=np.std(f[1]),
                             width=bar_width,
                             color=convert_rep_to_color[rep])
                #ax[i,1].text(k+j*bar_width,0.5,f'{len(s[1])}',fontsize=6, rotation=90)
                #ax[i,2].text(k+j*bar_width,0.5,f'{len(f[1])}',fontsize=6, rotation=90)
                ax[i, 2].set_yticklabels([])

        ax[i, 1].set_ylabel('Average Distance\n of Closest Memory')

    for f in range(2):
        ax[i, f + 1].set_ylim([0, 1])
        ax[i, f + 1].set_xticks(np.arange(3) + bar_width / 2)
        ax[i, f + 1].set_xticklabels([75, 50, 25])
        ax[i, f + 1].set_xlabel('Memory Capacity (%)')

    ax[0, 1].set_title('Successful Trials', fontsize=12)
    ax[0, 2].set_title('Failed Trials', fontsize=12)

    if legend == 'reps':
        legend_patch_list = []
        for rep in reps_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color=convert_rep_to_color[rep],
                               label=labels_for_plot[rep]))
        plt.legend(handles=legend_patch_list,
                   bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                   loc='lower right',
                   ncol=len(legend_patch_list),
                   title='State Encoding')
    elif legend == 'pcts':
        legend_patch_list = []
        for pct in pcts_to_plot:
            legend_patch_list.append(
                mpatches.Patch(color='gray', label=f'{pct}', alpha=pct / 100))
            plt.legend(handles=legend_patch_list,
                       bbox_to_anchor=(0.5, len(envs_to_plot) * 1.18),
                       loc='lower right',
                       ncol=len(legend_patch_list),
                       title='Episodic Memory Capacity (%)')
    if save:
        format = kwargs.get('format', 'svg')
        plt.savefig(f'../figures/CH2/{savename}.{format}', format=format)
    plt.show()
示例#28
0
            first_line = False
        try:
            plot_rects(line[3], int(line[0]), line[1], line[2], hapl,
                       pop_order, colors, chrX)
        except ValueError:  # Flexibility for chrX
            plot_rects(line[3], 23, line[1], line[2], hapl, pop_order, colors,
                       chrX)
        except IndexError:
            plot_rects(line[3], int(line[0]), line[1],
                       centromeres[str(chrom)][-1], hapl, pop_order, colors,
                       chrX)

# Write a legend
p = []
for i in range(len(pop_order)):
    p.append(plt.Rectangle((0, 0), 1, 1, color=colors[i]))
p.append(plt.Rectangle((0, 0), 1, 1, color='k'))
labs = list(pop_order)
labs.append('UNK')
leg = ax.legend(p, labs, loc=4, fancybox=True)
leg.get_frame().set_alpha(0)

# Get rid of annoying plot features
spines_to_remove = ['top', 'right']
for spine in spines_to_remove:
    ax.spines[spine].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')

fig.savefig(args.out)
示例#29
0
def plot_tracks(blobs, tracks, gt_tracks=None, output_dir=None, name=None):
    im_paths = blobs['im_paths']
    if not name:
        im0_name = osp.basename(im_paths[0])
    else:
        im0_name = str(name) + ".jpg"
    im0 = cv2.imread(im_paths[0])
    im1 = cv2.imread(im_paths[1])
    im0 = im0[:, :, (2, 1, 0)]
    im1 = im1[:, :, (2, 1, 0)]

    im_scales = blobs['im_info'][0, 2]

    tracks = tracks.data.cpu().numpy() / im_scales
    num_tracks = tracks.shape[0]

    fig, ax = plt.subplots(1, 2, figsize=(12, 6))

    ax[0].imshow(im0, aspect='equal')
    ax[1].imshow(im1, aspect='equal')

    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    ax[0].set_title(('{} tracks').format(num_tracks), fontsize=14)

    for i, t in enumerate(tracks):
        t0 = t[0]
        t1 = t[1]
        ax[0].add_patch(
            plt.Rectangle((t0[0], t0[1]),
                          t0[2] - t0[0],
                          t0[3] - t0[1],
                          fill=False,
                          linewidth=1.0,
                          **styles[i]))

        ax[1].add_patch(
            plt.Rectangle((t1[0], t1[1]),
                          t1[2] - t1[0],
                          t1[3] - t1[1],
                          fill=False,
                          linewidth=1.0,
                          **styles[i]))

    if gt_tracks:
        for gt in gt_tracks:
            for i in range(2):
                ax[i].add_patch(
                    plt.Rectangle((gt[i][0], gt[i][1]),
                                  gt[i][2] - gt[i][0],
                                  gt[i][3] - gt[i][1],
                                  fill=False,
                                  edgecolor='blue',
                                  linewidth=1.0))

    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    image = None
    if output_dir:
        im_output = osp.join(output_dir, im0_name)
        plt.savefig(im_output)
    else:
        image = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8')
        image = image.reshape(fig.canvas.get_width_height()[::-1] + (3, ))
    plt.close()
    return image
示例#30
0
文件: demo.py 项目: zhangqizky/RGB-N
def cam(sess, net, image_name, bbox):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    if os.path.isfile(os.path.join(data_dir, image_name)):
        im_file = os.path.join(data_dir, image_name)

    else:
        im_file = os.path.join(data_dir_2, image_name)
    im = cv2.imread(im_file)
    im = im.astype(np.float32, copy=False)
    pixel_means = np.array([[[102.9801, 115.9465, 122.7717]]])
    revise = 40
    #im[bbox[1]:bbox[1]+revise,bbox[0]:bbox[2],:]=pixel_means
    #im[bbox[1]:bbox[3],bbox[0]:bbox[0]+revise,:]=pixel_means
    #im[bbox[3]-revise:bbox[3],bbox[0]:bbox[2],:]=pixel_means
    #im[bbox[1]:bbox[3],bbox[2]-revise:bbox[2],:]=pixel_means
    #im -= pixel_means
    scores, boxes, feat, s1 = im_detect(sess, net, im)
    print(s1.shape)

    #rgb

    CONF_THRESH = 0.1
    NMS_THRESH = 0.2

    for cls_ind, cls in enumerate(CLASSES[1:]):
        if cls == 'authentic':
            continue
        cls_ind += 1  # because we skipped background
        #cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        #cls_scores = scores[:, cls_ind]
        cam = np.dot(feat, s1[:, cls_ind])
        #pdb.set_trace()
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        #keep = nms(dets, NMS_THRESH,False)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        cam = cam[keep, :]

    inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
    if len(inds) == 0:
        return 0

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots()
    #fig=plt.figure()
    #ax.imshow(im, aspect='equal')
    avg_score = 0
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]
        cam_i = cam[i, :, :]
        #pdb.set_trace()
        cam_i[np.where(cam_i > 10)] = 10
        cam_i[np.where(cam_i < 0)] = 0

        cam_i = (cam_i - np.min(cam_i)) / np.max(cam_i)
        shape = np.shape(im[int(max(0, bbox[1])):int(bbox[3]),
                            max(0, int(bbox[0])):int(bbox[2]), :])
        #heatmap = cv2.applyColorMap(cv2.resize(CAMs[0],(width, height)), cv2.COLORMAP_JET)
        heatmap_x = np.round(cv2.resize(cam_i, (shape[1], shape[0])) *
                             255).astype(np.uint8)
        heatmap = cv2.applyColorMap(heatmap_x, cv2.COLORMAP_JET)
        #pdb.set_trace()
        avg_score = max(avg_score, score)
        #plt.imsave('{}.png'.format(os.path.join('.', image_name)),im[int(max(0,bbox[1])):int(bbox[3]),max(0,int(bbox[0])):int(bbox[2]),:].astype(np.uint8))
        im[int(max(0, bbox[1])):int(bbox[3]),
           max(0, int(bbox[0])):int(bbox[2]), :] = 0.5 * im[
               int(max(0, bbox[1])):int(bbox[3]),
               max(0, int(bbox[0])):int(bbox[2]), :] + 0.5 * heatmap

        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1],
                          fill=False,
                          edgecolor='red',
                          linewidth=3.5))
        ax.text(bbox[0],
                bbox[1] - 2,
                '{:s} {:.3f}'.format('tamper', score),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14,
                color='white')
        #if class_name=='manipulation':
        #class_name='copy move'
        #ax.text(bbox[0], bbox[1] - 2,
        #'{:s} '.format(class_name),
        #bbox=dict(facecolor='blue', alpha=0.5),
        #fontsize=20, color='white')
    #avg_score=avg_score/len(inds)
    #ax.set_title(('{} detections with '
    # 'p({} | box) >= {:.1f}').format(class_name, class_name,
    #thresh),
    #fontsize=14)
    ax.imshow(im.astype(np.uint8), aspect='equal')
    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    #pdb.set_trace()
    plt.savefig('{}.png'.format(
        os.path.join(
            vis_dir,
            os.path.basename(image_name) + '_cam' + '_' + str(avg_score))))
    plt.close(fig)
    #im_score=vis_detections(im, cls, dets,image_name, thresh=CONF_THRESH)

    #pdb.set_trace()
    #head,conv = debug_filter(sess, net, im)
    #except Exception as e:
    #print(e)
    #return
    return im