示例#1
0
def visualize_temporal_activities(class_predictions, max_value=200, fps=1, title=None, legend=False):
    normalize = Normalize(vmin=1, vmax=max_value)
    normalize.clip=False
    cmap = plt.cm.Reds
    cmap.set_under('w')
    nb_instances = len(class_predictions)
    plt.figure(num=None, figsize=(18, 1), dpi=100)
    to_plot = class_predictions.astype(np.float32)
    to_plot[class_predictions==0.] = np.ma.masked
    plt.imshow(np.broadcast_to(to_plot, (20, nb_instances)), norm=normalize, interpolation='nearest', aspect='auto', cmap=cmap)
    if title:
        plt.title(title)
    ax = plt.gca()
    ax.get_yaxis().set_visible(False)

    if legend:
        index = np.arange(0,200)
        colors_index = np.unique(to_plot).astype(np.int64)
        if 0 in colors_index:
            colors_index = np.delete(colors_index, 0)
        patches = []
        for c in colors_index:
            patches.append(mpatches.Patch(color=cmap(normalize(c)), label=dataset.labels[c][1]))
        if patches:
            plt.legend(handles=patches, loc='upper center', bbox_to_anchor=(0.5, -.2), ncol=len(patches), fancybox=True, shadow=True)
    plt.show()
示例#2
0
def compare_temporal_activities(ground_truth, class_predictions, max_value=200, fps=1, title=None, legend=False, save_file='./img/activity_detection_sample_{}.png'):
    global count
    normalize = Normalize(vmin=1, vmax=max_value)
    normalize.clip=False
    cmap = plt.cm.Reds
    cmap.set_under('w')
    nb_instances = len(class_predictions)
    to_plot = np.zeros((20, nb_instances))
    to_plot[:10,:] = np.broadcast_to(ground_truth, (10, nb_instances))
    to_plot[10:,:] = np.broadcast_to(class_predictions, (10, nb_instances))
    to_plot = to_plot.astype(np.float32)
    to_plot[to_plot==0.] = np.ma.masked

    # Normalize the values and give them the largest distance possible between them
    unique_values = np.unique(to_plot).astype(np.int64)
    if 0 in unique_values:
        unique_values = np.delete(unique_values, 0)
    nb_different_values = len(unique_values)
    color_values = np.linspace(40, 190, nb_different_values)
    for i in range(nb_different_values):
        to_plot[to_plot == unique_values[i]] = color_values[i]

    plt.figure(num=None, figsize=(18, 1), dpi=100)
    plt.imshow(to_plot, norm=normalize, interpolation='nearest', aspect='auto', cmap=cmap)
    #plt.grid(True)
    plt.axhline(9, linestyle='-', color='k')
    plt.xlim([0,nb_instances])
    if title:
        plt.title(title)
    ax = plt.gca()
    #ax.get_yaxis().set_visible(False)
    ax.xaxis.grid(True, which='major')
    labels=['Ground\nTruth', 'Prediction']
    plt.yticks([5,15], labels, rotation="horizontal", size=13)
    plt.xlabel('Time (s)', horizontalalignment='left', fontsize=13)
    ax.xaxis.set_label_coords(0, -0.3)

    if legend:
        patches = []
        for c, l in zip(color_values, unique_values):
            patches.append(mpatches.Patch(color=cmap(normalize(c)), label=dataset.labels[l][1]))
        if patches:
            plt.legend(handles=patches, loc='upper center', bbox_to_anchor=(.5, -.2), ncol=len(patches), fancybox=True, shadow=True)
    #plt.show()
    plt.savefig(save_file.format(count), bbox_inches='tight')
    count += 1
示例#3
0
文件: plot.py 项目: jiawu/GSEApy
def dotplot(df,
            column='Adjusted P-value',
            title='',
            cutoff=0.05,
            top_term=10,
            sizes=None,
            norm=None,
            legend=True,
            figsize=(6, 5.5),
            cmap='RdBu_r',
            ofname=None,
            **kwargs):
    """Visualize enrichr results.

    :param df: GSEApy DataFrame results.
    :param column: which column of DataFrame to show. Default: Adjusted P-value
    :param title: figure title
    :param cutoff: terms with 'column' value < cut-off are shown.
    :param top_term: number of enriched terms to show.
    :param ascending: bool, the order of y axis.
    :param sizes: tuple, (min, max) scatter size. Not functional for now
    :param norm: maplotlib.colors.Normalize object.
    :param legend: bool, whether to show legend.
    :param figsize: tuple, figure size. 
    :param cmap: matplotlib colormap
    :param ofname: output file name. If None, don't save figure 

    """

    colname = column
    # sorting the dataframe for better visualization
    if colname in ['Adjusted P-value', 'P-value']:
        # check if any values in `df[colname]` can't be coerced to floats
        can_be_coerced = df[colname].map(isfloat)
        if np.sum(~can_be_coerced) > 0:
            raise ValueError(
                'some value in %s could not be typecast to `float`' % colname)
        else:
            df.loc[:, colname] = df[colname].map(float)
        df = df[df[colname] <= cutoff]
        if len(df) < 1:
            msg = "Warning: No enrich terms when cutoff = %s" % cutoff
            return msg
        df = df.assign(logAP=lambda x: -x[colname].apply(np.log10))
        colname = 'logAP'
    df = df.sort_values(by=colname).iloc[-top_term:, :]
    #
    temp = df['Overlap'].str.split("/", expand=True).astype(int)
    df = df.assign(Hits=temp.iloc[:, 0], Background=temp.iloc[:, 1])
    df = df.assign(Hits_ratio=lambda x: x.Hits / x.Background)
    # x axis values
    x = df.loc[:, colname].values
    combined_score = df['Combined Score'].round().astype('int')
    # y axis index and values
    y = [i for i in range(0, len(df))]
    ylabels = df['Term'].values
    # Normalise to [0,1]
    # b = (df['Count']  - df['Count'].min())/ np.ptp(df['Count'])
    # area = 100 * b

    # control the size of scatter and legend marker
    levels = numbers = np.sort(df.Hits.unique())
    if norm is None:
        norm = Normalize()
    elif isinstance(norm, tuple):
        norm = Normalize(*norm)
    elif not isinstance(norm, Normalize):
        err = ("``size_norm`` must be None, tuple, " "or Normalize object.")
        raise ValueError(err)
    min_width, max_width = np.r_[20, 100] * plt.rcParams["lines.linewidth"]
    norm.clip = True
    if not norm.scaled():
        norm(np.asarray(numbers))
    size_limits = norm.vmin, norm.vmax
    scl = norm(numbers)
    widths = np.asarray(min_width + scl * (max_width - min_width))
    if scl.mask.any():
        widths[scl.mask] = 0
    sizes = dict(zip(levels, widths))
    df['sizes'] = df.Hits.map(sizes)
    area = df['sizes'].values

    # create scatter plot
    if hasattr(sys, 'ps1') and (ofname is None):
        # working inside python console, show figure
        fig, ax = plt.subplots(figsize=figsize)
    else:
        # If working on commandline, don't show figure
        fig = Figure(figsize=figsize)
        canvas = FigureCanvas(fig)
        ax = fig.add_subplot(111)
    vmin = np.percentile(combined_score.min(), 2)
    vmax = np.percentile(combined_score.max(), 98)
    sc = ax.scatter(x=x,
                    y=y,
                    s=area,
                    edgecolors='face',
                    c=combined_score,
                    cmap=cmap,
                    vmin=vmin,
                    vmax=vmax)

    if column in ['Adjusted P-value', 'P-value']:
        xlabel = "-log$_{10}$(%s)" % column
    else:
        xlabel = column
    ax.set_xlabel(xlabel, fontsize=14, fontweight='bold')
    ax.yaxis.set_major_locator(plt.FixedLocator(y))
    ax.yaxis.set_major_formatter(plt.FixedFormatter(ylabels))
    ax.set_yticklabels(ylabels, fontsize=16)

    # ax.set_ylim([-1, len(df)])
    ax.grid()
    # colorbar
    cax = fig.add_axes([0.95, 0.20, 0.03, 0.22])
    cbar = fig.colorbar(
        sc,
        cax=cax,
    )
    cbar.ax.tick_params(right=True)
    cbar.ax.set_title('Combined\nScore', loc='left', fontsize=12)

    # for terms less than 3
    if len(df) >= 3:
        # find the index of the closest value to the median
        idx = [
            area.argmax(),
            np.abs(area - area.mean()).argmin(),
            area.argmin()
        ]
        idx = unique(idx)
    else:
        idx = range(len(df))
    label = df.iloc[idx, df.columns.get_loc('Hits')]

    if legend:
        handles, _ = ax.get_legend_handles_labels()
        legend_markers = []
        for ix in idx:
            legend_markers.append(ax.scatter([], [], s=area[ix], c='b'))
        # artist = ax.scatter([], [], s=size_levels,)
        ax.legend(legend_markers, label, title='Hits')
    ax.set_title(title, fontsize=20, fontweight='bold')

    if ofname is not None:
        # canvas.print_figure(ofname, bbox_inches='tight', dpi=300)
        fig.savefig(ofname, bbox_inches='tight', dpi=300)
        return
    return ax