def updateAssessmentGraphs():
    states = []
    imageRadius = IMAGE_SIZE / 2
    for xImg in range(IMAGE_SIZE):
        for yImg in range(IMAGE_SIZE):
            x = xImg - imageRadius
            y = yImg - imageRadius
            v = np.clip(math.sqrt(x * x + y * y) * 16 / imageRadius, 0, 16)
            v = v - 8

            theta = None
            if x < 0:
                theta = math.atan(y / x) + math.pi
            elif x == 0 and y > 0:
                theta = math.pi
            elif x == 0 and y < 0:
                theta = -math.pi
            elif x == 0 and y == 0:
                theta = 0
            elif x > 0 and y < 0:
                theta = math.atan(y / x) + math.pi + math.pi
            else:
                theta = math.atan(y / x)
            states.append([math.cos(theta), math.sin(theta), v])
    (action_mean_val) = sess.run([action_mean], feed_dict={state_ph: states})

    actionsChosenImg = np.reshape(action_mean_val, [IMAGE_SIZE, IMAGE_SIZE])

    policyGraph.cla()
    policyColorBar.cla()
    ax = policyGraph.imshow(actionsChosenImg)
    colorbar(ax, cax=policyColorBar)
    policyFigure.canvas.draw()
Ejemplo n.º 2
0
def plot_inset_nn_all(ax, pw, xlim=(12e6, 16e6), ylim=(4e6, 6e6)):

    pw = pw.sel(x=slice(*xlim), y=slice(*ylim))
    x, y, z = pw.x / 1e6, pw.y / 1e6, pw.values

    c = "w"

    styles = {
        'axes.edgecolor': c,
        'axes.labelcolor': c,
        'xtick.color': c,
        'ytick.color': c
    }

    with plt.rc_context(styles):
        axins = zoomed_inset_axes(ax, 2, loc='center left')  # zoom = 6
        im = axins.pcolormesh(x, y, z, rasterized=True)
        # colorbar
        cax = inset_axes(
            axins,
            width="100%",  # width = 10% of parent_bbox width
            height="20%",  # height : 50%
            loc="lower center",
            #                  bbox_to_anchor=(1.05, 0., 1, 1),
            bbox_to_anchor=(0.0, -.30, 1., 1),
            bbox_transform=axins.transAxes,
            borderpad=0,
        )

        colorbar(im, cax=cax, orientation='horizontal')

        axins.set_xticklabels('')
        axins.set_yticklabels('')
        mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec=c)
Ejemplo n.º 3
0
def plot_heatmap3d(df_heatmap):
    """Plot 3D heatmap."""
    # 3D Plotting
    fig = plt.figure(figsize=(25, 18))
    ax = plt.axes(projection="3d")
    fig.set_facecolor("white")
    ax.set_facecolor("white")

    # axes
    ax.w_xaxis.pane.fill = False
    ax.w_yaxis.pane.fill = False
    ax.w_zaxis.pane.fill = False
    ax.w_zaxis.grid(visible=False)

    # labels
    ax.yaxis.labelpad = 30
    ax.xaxis.labelpad = 50
    ax.zaxis.labelpad = 30

    ax.tick_params(axis="z", pad=15)
    ax.tick_params(axis="x", pad=20)

    # data
    df_pivot = df_heatmap.pivot_table(values="val", index="beta", columns="delta")

    # coordinates
    x = np.arange(0.944, 0.962, 0.002)
    y = np.arange(0.75, 1.05, 0.01)
    x, y = np.meshgrid(x, y)

    z = df_pivot.iloc[:, 11:].values
    ax.set_zticks([])

    # colormap
    colormap = get_custom_cmap()
    surf = ax.plot_surface(x, y, z, cmap=colormap)
    format_ = mpl.ticker.ScalarFormatter(useOffset=False, useMathText=True)
    format_.set_powerlimits((3, 3))
    colorbar(
        surf,
        ax=ax,
        shrink=0.7,
        aspect=10,
        ticks=[10_000] + list(np.linspace(20_000, 140_000, 7)),
        format=format_,
    )

    # labels' names
    ax.set_xlabel(r"$\delta$", fontsize=20, rotation=270)
    ax.set_ylabel(r"$\beta$", fontsize=20)
    ax.set_zlabel("Criterion", fontsize=20, rotation=360)
    ax.set_title("Heatmap criterion", weight="bold", fontsize=28, y=1.05)

    # disable automatic rotation
    ax.zaxis.set_rotate_label(False)

    ax.view_init(50, 10)

    return fig
Ejemplo n.º 4
0
def _plot_bicmaps(X_orig_std, best_co_config):

    # Train model with best config.
    orig_co_model = SpectralCoclustering(random_state=0, svd_method='arpack')
    orig_co_model.set_params(**best_co_config)
    orig_co_model.fit(X_orig_std)
    orig_co_row_sorted = X_orig_std[np.argsort(orig_co_model.row_labels_), :]
    orig_co_fit_data = orig_co_row_sorted[:, np.argsort(orig_co_model.column_labels_)]

    hmap = sns.heatmap(
        orig_co_fit_data,
        robust=True,
        cmap=plt.cm.viridis,
        fmt='f',
        vmin=np.min(orig_co_fit_data),
        vmax=np.max(orig_co_fit_data),
        cbar=False
    )
    coords = bic_coords(orig_co_model, best_co_config['n_clusters'])
    for num in coords.index:
        plt.plot(
            (coords.loc[num, ['x1', 'x2', 'x2', 'x1', 'x1']]),
            (coords.loc[num, ['y1', 'y1', 'y2', 'y2', 'y1']]),
            c='darkred'
    )
    plt.ylabel('Patients')
    plt.xlabel('Features')

    plt.yticks([], [])
    plt.xticks([], [])

    ax_divider = make_axes_locatable(hmap)
    cax = ax_divider.append_axes('right', size='3%', pad='2%')
    colorbar.colorbar(
        hmap.get_children()[0],
        cax=cax,
        orientation='vertical'
    )
    #cax.xaxis.set_label_text('AUC', fontname='Sans')
    #cax.xaxis.set_label_position('top')
    cbar_ticks = np.linspace(
        np.nanmin(orig_co_fit_data),
        np.nanmax(orig_co_fit_data),
        6
    )
    cax.yaxis.set_ticks(cbar_ticks)
    cax.yaxis.set_ticklabels([f'{num:.01f}' for num in cbar_ticks])

    plt.savefig(
        '../biclustering/bic_map_original_images.pdf',
        bbox_inches='tight',
        transparent=True,
        dpi=CONFIG.DPI,
    )
Ejemplo n.º 5
0
    def updateAssessmentGraphs(self):
        states = []
        imageRadius = constants.IMAGE_SIZE / 2
        for xImg in range(constants.IMAGE_SIZE):
            for yImg in range(constants.IMAGE_SIZE):
                x = xImg - imageRadius
                y = yImg - imageRadius
                v = np.clip(math.sqrt(x * x + y * y) * 16 / imageRadius, 0, 16)
                v = v - 8

                theta = None
                if x < 0:
                    theta = math.atan(y / x) + math.pi
                elif x == 0 and y > 0:
                    theta = math.pi
                elif x == 0 and y < 0:
                    theta = -math.pi
                elif x == 0 and y == 0:
                    theta = 0
                elif x > 0 and y < 0:
                    theta = math.atan(y / x) + math.pi + math.pi
                else:
                    theta = math.atan(y / x)
                states.append([math.cos(theta), math.sin(theta), v])
        (actionsChosen, qAssessments) = self.sess.run(
            self.graphingOperations,
            feed_dict={
                self.statePh:
                states,
                self.quantileThresholdsPh:
                np.random.uniform(low=0.0,
                                  high=1.0,
                                  size=(len(states), self.numQuantiles))
            })
        actionsChosenImg = np.reshape(
            actionsChosen, [constants.IMAGE_SIZE, constants.IMAGE_SIZE])
        qAssessmentsImg = np.reshape(
            qAssessments, [constants.IMAGE_SIZE, constants.IMAGE_SIZE])

        self.qAssessmentGraph.cla()
        self.qAssessmentColorBar.cla()
        ax = self.qAssessmentGraph.imshow(qAssessmentsImg)
        colorbar(ax, cax=self.qAssessmentColorBar)
        self.qAssessmentFigure.canvas.draw()

        self.policyGraph.cla()
        self.policyColorBar.cla()
        ax = self.policyGraph.imshow(actionsChosenImg)
        colorbar(ax, cax=self.policyColorBar)
        self.policyFigure.canvas.draw()
Ejemplo n.º 6
0
def gen_heatmap(results, path_to_fig=None, kind='test_score', show=False):

    results_mat, selector_lbls, estimator_lbls = gen_results_matrix(results,
                                                                    kind=kind)
    plt.title('AUC', x=1.035, y=1.03)
    hmap = sns.heatmap(
        results_mat.T * 100,
        yticklabels=format_selector_labels(selector_lbls),
        xticklabels=format_estimator_labels(estimator_lbls),
        vmin=np.nanmin(results_mat) * 100 - 3,
        vmax=np.nanmax(results_mat) * 100 + 3,
        cmap=plt.cm.viridis,
        robust=True,
        annot=True,
        fmt='.1f',
        square=1,
        linewidth=.5,
        cbar=False,
        annot_kws={'size': 18},
    )
    plt.xlabel('Classification Algorithm', va='top', ha='center')
    plt.ylabel('Feature Selection Algorithm', va='bottom', ha='center')
    hmap.set_yticklabels(hmap.get_yticklabels(), rotation=0)
    hmap.set_xticklabels(hmap.get_xticklabels(),
                         rotation=0,
                         va='top',
                         ha='center')
    # labelpad=-40, y=1.05, rotation=0
    ax_divider = make_axes_locatable(hmap)
    cax = ax_divider.append_axes('right', size='3%', pad='2%')
    colorbar.colorbar(hmap.get_children()[0], cax=cax, orientation='vertical')
    #cax.xaxis.set_label_text('AUC', fontname='Sans')
    #cax.xaxis.set_label_position('top')
    cbar_ticks = np.linspace(
        np.nanmin(results_mat) * 100 - 3,
        np.nanmax(results_mat) * 100 + 3, 6)
    cax.yaxis.set_ticks(cbar_ticks)
    cax.yaxis.set_ticklabels([f'{num:.01f}' for num in cbar_ticks])

    if path_to_fig is not None:
        plt.savefig(
            path_to_fig,
            bbox_inches='tight',
            transparent=True,
            dpi=CONFIG.DPI,
        )
    if show:
        plt.show()
Ejemplo n.º 7
0
    def showcov(self,
                triang,
                val,
                vmin=-130,
                vmax=-50,
                cmap=plt.cm.jet,
                bbuild=False,
                btile=True):
        """ Show a coverage

        Parameters
        ----------

        triang : triangulation
        val : values

        """
        lonmin = np.min(triang.x)
        lonmax = np.max(triang.x)
        latmin = np.min(triang.y)
        latmax = np.max(triang.y)
        extent = (lonmin, lonmax, latmin, latmax)
        print(extent)
        mp = smopy.Map((extent[2] + 0.1, extent[0] + 0.1, extent[3] - 0.1,
                        extent[1] - 0.1),
                       z=12)
        if bbuild:
            f, ax, d = self.show(fig=f,
                                 ax=ax,
                                 contour=False,
                                 btile=False,
                                 bldg=True,
                                 height=False,
                                 coord='lonlat',
                                 extent=self.extent)

        #ax = plt.gca()
        if mp != []:
            triang.x, triang.y = mp.to_pixels(triang.y, triang.x)
        #ax = mp.show_mpl(figsize=(10,10))
        ax = plt.gca()
        tc = ax.tripcolor(
            triang,
            val.flatten(),
            #shading='gouraud',
            #shading='flat',
            cmap=cmap,
            vmax=vmax,
            vmin=vmin,
            alpha=0.4,
            edgecolors='k',
            linewidth=0.0)
        #plt.axis('equal')
        ax = mp.show_mpl(ax=ax)

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad="5%")
        cb = colorbar(tc, cax=cax)
        cb.set_label_text('Loss(dB)', fontsize=18)
        return (cb)
Ejemplo n.º 8
0
def plot_Z(Z_mean,
           wi_mean,
           lami_est,
           w_thresh=.01,
           cm_greys=plt.cm.get_cmap('Greys', 5),
           fs_lab=10,
           add_colorbar=True,
           fs_cbar=10,
           fs_w=10,
           fs_celltypes=10,
           xlab="markers",
           ylab="cell subpopulations (abundance)",
           markernames=[],
           fs_markers=10,
           w_digits=1):

    J = Z_mean.shape[0]
    k_ord = wi_mean.argsort()
    z_cols = []

    for k in k_ord.tolist():
        if wi_mean[k] > w_thresh:
            z_cols.append(k)

    z_cols = np.array(z_cols)
    Z_hat = Z_mean[:, z_cols].T

    im = plt.imshow(Z_hat, aspect='auto', vmin=0, vmax=1, cmap=cm_greys)
    plt.xlabel(xlab, fontsize=fs_lab)
    plt.ylabel(ylab, fontsize=fs_lab)

    # W percentages
    w_perc = wi_mean[z_cols]
    w_perc = [str((wp * 100).round(w_digits)) + '%' for wp in w_perc]

    ax = plt.gca()
    # plt.xticks([])
    labels = ['{} ({})'.format(zc + 1, wp) for (zc, wp) in zip(z_cols, w_perc)]
    plt.yticks(np.arange(len(z_cols)), labels, fontsize=fs_celltypes)
    add_gridlines_Z(Z_hat)
    plt.xticks(rotation=90, fontsize=fs_markers)
    if len(markernames) == 0:
        plt.xticks(np.arange(J), np.arange(J) + 1)
    else:
        plt.xticks(np.arange(J), markernames)

    # add wi_mean on right side
    # K = z_cols.shape[0]
    # ax2 = ax.twinx()
    # ax2.set_yticks(range(K))
    # plt.yticks((K-1) / K * np.arange(K) + .5, w_perc[::-1], fontsize=fs_w)
    # ax2.tick_params(length=0)

    # colorbar
    if add_colorbar:
        ax_divider = make_axes_locatable(ax)
        cax = ax_divider.append_axes("top", size="7%", pad="2%")
        cax.xaxis.set_ticks_position("top")
        cbar = colorbar(im, cax=cax, orientation="horizontal")
        cbar.ax.tick_params(labelsize=fs_cbar)
Ejemplo n.º 9
0
def plotMixtureMatrix(ax, mixtureMatrix, labels):
    """
        auxiliary function to plot the mixture matrix

        Keywords:
            --- ax: the axes object
            --- mixtureMatrix: the data for the mixture matrix
            --- labels: labels in the mixture matrix corresponding to classes

    """

    # Add a finite number to the otherwise zero values
    # to get around the logarithmic nan values
    mixtureMatrix[np.where(mixtureMatrix == 0)] += 1

    disc = np.max(mixtureMatrix)
    fonts = {'fontsize': 8}
    tickSize = 6
    cmap = mpl.cm.get_cmap('inferno', disc)
    cmap.set_under((0., 0., 0.))
    im = ax.imshow(mixtureMatrix,
                   cmap=cmap,
                   aspect=1.,
                   interpolation='nearest',
                   norm=LogNorm())
    cax = inset_axes(
        ax,
        width="5%",  # width = 10% of parent_bbox width
        height="100%",  # height : 50%
        loc=3,
        bbox_to_anchor=(1.05, 0., 1, 1),
        bbox_transform=ax.transAxes,
        borderpad=0,
    )
    f = ticker.ScalarFormatter(useOffset=False, useMathText=True)
    g = lambda x, pos: "${}$".format(f._formatSciNotation('%1.10e' % x))
    cbar = colorbar(im,
                    cax=cax,
                    ticks=[100, 900],
                    extend='both',
                    format=ticker.FuncFormatter(g))
    cbar.ax.tick_params(labelsize=6)
    ax.set_ylabel('true label', **fonts)
    ax.set_xlabel('predicted label', **fonts)

    for location in ['top', 'bottom', 'left', 'right']:
        ax.spines[location].set_linewidth(5.)

    # Change the ticks to labels names
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    ax.tick_params(length=0., pad=5)
    ax.set_xticks(np.arange(len(labels)))
    ax.set_xticklabels(labels, fontsize=tickSize)
    ax.set_yticks(np.arange(len(labels)))
    ax.set_yticklabels(labels, fontsize=tickSize)
    ax.tick_params(axis='both', which='minor')
Ejemplo n.º 10
0
    def showcov(self, triang, val,
                vmin=-130,
                vmax=-50,
                cmap=plt.cm.jet,
                bbuild = False,
                btile=True):
        """ Show a coverage

        Parameters
        ----------

        triang : triangulation
        val : values

        """
        lonmin = np.min(triang.x)
        lonmax = np.max(triang.x)
        latmin = np.min(triang.y)
        latmax = np.max(triang.y)
        extent = (lonmin, lonmax, latmin, latmax)
        print(extent)
        mp = smopy.Map((extent[2]+0.1, extent[0]+0.1, extent[3]-0.1,extent[1]-0.1), z=10)
        if bbuild:
            f, ax, d = self.show(fig=f,
                                 ax=ax,
                                 contour=False,
                                 btile=False,
                                 bldg=True,
                                 height=False,
                                 coord='lonlat',
                                 extent=self.extent)

        #ax = plt.gca()
        triang_ = copy.deepcopy(triang)
        if mp!=[]:
            triang_.x,triang_.y = mp.to_pixels(triang_.y, triang_.x)
        #ax = mp.show_mpl(figsize=(10,10))
        ax = plt.gca()
        tc = ax.tripcolor(triang_,
                          val.flatten(),
                          #shading='gouraud',
                          #shading='flat',
                          cmap=cmap,
                          vmax=vmax,
                          vmin=vmin,
                          alpha = 0.4,
                          edgecolors='k',
                          linewidth=0.0)
        #plt.axis('equal')
        ax = mp.show_mpl(ax=ax)

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right",size="5%",pad="5%")
        cb = colorbar(tc, cax=cax)
        cb.set_label_text('Loss(dB)',fontsize=18)
        return(cb)
Ejemplo n.º 11
0
    def __plot_colorbar(self, img, orientation='vertical'):
        if orientation == 'horizontal':
            ax_divider = make_axes_locatable(self.ax)
            if self.is_inverted:
                cax = ax_divider.append_axes("top", size=0.09, pad=0.2)
            else:
                cax = ax_divider.append_axes("bottom", size=0.09, pad=0.2)
            colorbar(img, cax=cax, orientation='horizontal')
        else:  # vertical
            y_ax = self.y_ax

            if self.properties['norm'] == 'log':
                from matplotlib.ticker import LogFormatter
                formatter = LogFormatter(10, labelOnlyBase=False)
                aa = np.array([1, 2, 5])
                c_min, c_max = self.matrix_val_range

                def abs_inc(num):
                    if num != 0:
                        sign = num / abs(num)
                        return int(sign * abs(num + 1))
                    else:
                        return 1

                lower_ = int(np.log10(c_min))
                upper_ = abs_inc(int(np.log10(c_max)))
                tick_values = np.concatenate(
                    [aa * 10**x for x in range(lower_, upper_)])

                c_bar = plt.colorbar(img,
                                     ax=y_ax,
                                     ticks=tick_values,
                                     format=formatter,
                                     fraction=0.98)
            else:
                c_bar = plt.colorbar(img, ax=y_ax, fraction=0.98)

            c_bar.solids.set_edgecolor("face")
            c_bar.ax.tick_params(labelsize='smaller')

            c_bar.ax.yaxis.set_ticks_position('left')
Ejemplo n.º 12
0
def visualize(r, obser, predit, var):
    """Draws original, encoded and decoded images"""

    fig, (ax1, ax2) = plt.subplots(1, 2)
    fig.set_size_inches(10, 10)
    fig.subplots_adjust(wspace=0.5)
    fig.suptitle("Week " + str(r + 1) + " Plots", fontsize=16)

    im1 = ax1.imshow(obser, vmin=-1, vmax=1)
    ax1_divider = make_axes_locatable(ax1)
    cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
    cb1 = colorbar(im1, cax=cax1)
    ax1.set_title("Observed" + var)

    im2 = ax2.imshow(predit, vmin=-1, vmax=1)
    ax2_divider = make_axes_locatable(ax2)
    cax2 = ax2_divider.append_axes("right", size="7%", pad="2%")
    cb2 = colorbar(im2, cax=cax2)
    ax2.set_title("Predicted" + var)

    fig.savefig(pp, format='pdf')
Ejemplo n.º 13
0
    def showSubim(self):
        """Shows the subimage"""

        # We might select only one of the filters
        if self.subimFilter > 0:
            if self.tData['FILTER'][self.iData] <> self.subimFilter:
                return

        if not self.axInset:
            self.setupSubim()

        # hilight here.
        self.hilightCurrentScatter()

        # dynamic scaling
        vmin = -5.0e-3
        vmax = 5.0e-3
        if self.t[self.iData] - np.min(self.t) > 1.2:
            vmin = -1.0e-3
            vmax = 1.0e-3

        dum = self.axInset.imshow(self.subim, origin='lower', \
                                  cmap='gray', interpolation='nearest', \
                                  vmin=vmin, vmax=vmax)

        try:
            self.cax.remove()
        except:
            notAlreadyHere = True

        # add colorbar
        self.cax = inset_axes(
            self.axInset,
            width="5%",  # width = 10% of parent_bbox width
            height="100%",  # height : 50%
            loc=3,
            bbox_to_anchor=(1.05, 0., 1, 1),
            bbox_transform=self.axInset.transAxes,
            borderpad=0)
        colorbar(dum, cax=self.cax)
Ejemplo n.º 14
0
def heatmap_sequence(one_hot,
                     ax=None,
                     sort_idx=None,
                     aspect='auto',
                     figsize_tmpl=(8, 4),
                     cbar=True,
                     title=None):
    """Plot a heatmap of sequences
    """
    if ax is None:
        figsize = (figsize_tmpl[0] * one_hot.shape[1] / 200,
                   figsize_tmpl[1] * one_hot.shape[0] / 2000)
        fig, ax = plt.subplots(figsize=figsize)

    if sort_idx is None:
        sort_idx = np.arange(one_hot.shape[0])

    cmap = colors.ListedColormap(["red", "orange", "blue", "green"][::-1])
    qrates = np.array(list("TGCA"))
    bounds = np.linspace(-.5, 3.5, 5)
    norm = colors.BoundaryNorm(bounds, 4)
    fmt = mpl.ticker.FuncFormatter(lambda x, pos: qrates[::-1][norm(x)])

    img = ax.imshow(one_hot.argmax(axis=-1)[sort_idx],
                    aspect=aspect,
                    cmap=cmap,
                    norm=norm,
                    alpha=0.8)
    if cbar:
        ax2_divider = make_axes_locatable(ax)
        cax2 = ax2_divider.append_axes("top", size="5%", pad=0.05)
        # cb2 = colorbar(im2, cax=cax2, orientation="horizontal")
        cb2 = colorbar(img,
                       cax=cax2,
                       cmap=cmap,
                       norm=norm,
                       boundaries=bounds,
                       orientation="horizontal",
                       ticks=[0, 1, 2, 3],
                       format=fmt)
        cax2.xaxis.set_ticks_position("top")
    seq_len = one_hot.shape[1]
    ticks = np.arange(0, seq_len + 1, 25)
    ax.set_xticks(ticks)
    ax.set_xticklabels(ticks - seq_len // 2)
    ax.set_ylabel("Seqlet index")
    ax.set_xlabel("Position")
    if title is not None:
        ax.set_title(title)
    return fig
Ejemplo n.º 15
0
 def show_xt(self, ax, cmp="jet", Vmin=-0.2, Vmax=0.2, fsz=12):
     ext = [self.time[0], self.time[-1], self.y0, self.y1]
     im = ax.imshow(self.amp,
                    vmin=Vmin,
                    vmax=Vmax,
                    extent=ext,
                    origin="lower",
                    interpolation="bilinear",
                    cmap=cmp)
     #ax.set_xlabel("time [$\mu$s]",fontsize=fsz)
     ax.set_ylabel("y [mm]", fontsize=fsz)
     ax_div = make_axes_locatable(ax)
     cax = ax_div.append_axes("right", size="5%", pad="2%")
     cbar = colorbar(im, cax=cax)
Ejemplo n.º 16
0
def plot_y(yi,
           wi_mean,
           lami_est,
           fs_lab=10,
           fs_cbar=10,
           lw=3,
           cm=blue2red.cm(6),
           vlim=(-3, 3),
           fs_xlab=10,
           fs_ylab=10,
           ylab="cells",
           markernames=[],
           interpolation=None,
           rotation=90,
           ha="center"):
    J = yi.shape[1]
    vmin, vmax = vlim

    if type(wi_mean) == int:
        K = wi_mean
        wi_mean = np.array([(lami_est == k + 1).mean() for k in range(K)])

    lami_new, counts = relabel_lam(lami_est, wi_mean)
    counts_cumsum = np.cumsum(counts)
    yi_sorted = yi[np.argsort(lami_new), :]

    im = plt.imshow(yi_sorted,
                    aspect='auto',
                    vmin=vmin,
                    vmax=vmax,
                    cmap=cm,
                    interpolation=interpolation)
    for c in counts_cumsum[:-1]:
        plt.axhline(c, color='yellow', linewidth=lw)
    plt.xticks(rotation=rotation, ha=ha)
    if len(markernames) == 0:
        plt.xticks(np.arange(J), np.arange(J) + 1, fontsize=fs_xlab)
    else:
        plt.xticks(np.arange(J), markernames, fontsize=fs_xlab)
    plt.yticks(fontsize=fs_ylab)
    plt.xlabel("markers", fontsize=fs_lab)
    plt.ylabel(ylab, fontsize=fs_lab)

    ax = plt.gca()
    ax_divider = make_axes_locatable(ax)
    cax = ax_divider.append_axes("top", size="7%", pad="2%")
    cax.xaxis.set_ticks_position("top")
    cbar = colorbar(im, cax=cax, orientation="horizontal")
    cbar.ax.tick_params(labelsize=fs_cbar)
Ejemplo n.º 17
0
def error_contourf(predictions, labels, ax, text_labels=None):
    '''Make a smoothed contour plot showing absolute errors along the slab.'''
    errors = num.sqrt(num.sum((predictions - labels)**2, axis=1))
    med = num.median(errors)
    vmin = 0.
    vmax = med + 1.5 * num.std(errors)

    s = ax.scatter(predictions.T[0],
                   predictions.T[2],
                   s=8,
                   c=errors,
                   linewidth=0.1,
                   edgecolors='grey',
                   vmin=vmin,
                   vmax=vmax,
                   cmap='viridis_r')

    ax.set_xlabel('N-S')
    ax.set_ylabel('Z')

    cax = inset_axes(
        ax,
        width="2%",  # width = 10% of parent_bbox width
        height="100%",  # height : 50%
        loc='lower left',
        bbox_to_anchor=(1.25, 0.0, 1, 1),
        bbox_transform=ax.transAxes,
        borderpad=0,
    )

    # cax.set_title('Err [km]')
    cbar = colorbar(s, cax=cax)
    cbar.ax.text(9.5,
                 0.5,
                 r'$\triangle$DD [km]',
                 rotation=90.,
                 horizontalalignment='center',
                 verticalalignment='center',
                 transform=cax.transAxes)

    cbar.ax.tick_params(labelsize=MAIN_FONT_SIZE - 2)

    if text_labels:
        assert len(text_labels) == len(predictions)
        for ip, p in enumerate(predictions):
            ax.text(p[0], p[2], text_labels[ip], fontsize=POINT_SIZE)
Ejemplo n.º 18
0
def error_contourf(predictions, labels, ax):
    '''Make a smoothed contour plot showing absolute errors along the slab.'''
    errors = num.sqrt(num.sum((predictions-labels)**2, axis=1))
    med = num.median(errors)
    vmin = 0.
    vmax = med + 1.5 * num.std(errors)
    s = ax.scatter(predictions.T[0], predictions.T[2], s=6, c=errors, linewidth=0,
            vmin=vmin, vmax=vmax)
    ax.set_xlabel('N-S')
    ax.set_ylabel('Z')
    # colorbar
    cax = inset_axes(ax,
		     width="2%",  # width = 10% of parent_bbox width
		     height="50%",  # height : 50%
		     loc='lower left',
		     bbox_to_anchor=(0., 0.05, 1, 1),
		     bbox_transform=ax.transAxes,
		     borderpad=0,
		     )
    cbar = colorbar(s, cax=cax)
    cbar.ax.tick_params(labelsize=MAIN_FONT_SIZE-2)
Ejemplo n.º 19
0
    def show(self, ax, fsz=16):
        ext = [self.time[0], self.time[-1], -self.ycod[0], -self.ycod[-1]]
        Vmax = np.sum(self.C[:]) / 150
        #ax.imshow(self.C, extent=ext,aspect="auto",origin="lower",cmap="jet",interpolation="bilinear",vmin=0,vmax=Vmax)
        im = ax.imshow(self.C,
                       extent=ext,
                       aspect="auto",
                       origin="lower",
                       cmap="jet",
                       interpolation="bilinear",
                       vmin=0,
                       vmax=Vmax)
        ax_div = make_axes_locatable(ax)
        cax = ax_div.append_axes("right", size="5%", pad="2.5%")
        cbar = colorbar(im, cax=cax, orientation="vertical")
        cbar.ax.tick_params(labelsize=fsz - 2)

        ax.set_xlim(ext[0:2])
        ax.set_ylim(ext[2:4])
        ax.set_ylabel("y [mm]", fontsize=fsz)
        ax.set_xlabel("time [$\mu$s]", fontsize=fsz)
        ax.tick_params(labelsize=fsz)
Ejemplo n.º 20
0
    def visualize_duty_phi(self):
        fig, axs = plt.subplots(2, 1, figsize=(12, 12))

        extent = [0, 1.0, self.phi_space[0], self.phi_space[-1]]
        img = axs[0].imshow(self.auto_correlation_matrix, origin='lower', aspect='auto', extent=extent, cmap='plasma')
        ax_div = make_axes_locatable(axs[0])
        # add an axes above the main axes.
        cax2 = ax_div.append_axes("top", size="7%", pad="2%")
        cb2 = colorbar(img, cax=cax2, orientation="horizontal")
        # change tick position to top. Tick position defaults to bottom and overlaps
        # the image.
        cax2.xaxis.set_ticks_position("top")
        cax2.set_xlabel(r"$\mathcal{L}$")
        axs[0].scatter(self.duty_param, self.phi_param, color='red', marker='X')
        axs[0].set_xlabel(r'Duty Cycle')
        axs[0].set_ylabel(r"Meander Start $(\Phi)$")

        axs[1].plot(self.voltage_offset, self.delta_f)
        axs[1].plot(self.voltage_offset, \
                    self.square_function(self.voltage_offset, self.phi_param, self.duty_param, self.period,
                                         max(self.delta_f)))

        return fig, axs
Ejemplo n.º 21
0
    def mean_bscan_FFT(self, ax, x1=0, x2=0):
        if x1 * x1 + x2 * x2 == 0:
            i1 = 0
            i2 = self.Ny
        else:
            i1 = np.argmin(abs(self.xcod - x1))
            i2 = np.argmin(abs(self.xcod - x2))

        Bmp = np.mean(self.amp[:, i1:i2 + 1, :], 1)
        BMP = np.fft.fft(Bmp, axis=1)
        fmax = 1 / self.dt
        ext = [0, fmax, self.Xa[1], self.Xb[1]]

        BMP = np.fft.ifft(BMP, axis=0)

        Bmax = np.max(np.abs(BMP))
        #im=ax.imshow(np.abs(BMP)/Bmax,cmap="jet",origin="lower",aspect="auto",interpolation="bilinear",extent=ext,vmin=0,vmax=0.5)

        BMP[0:int(self.Ny / 2), 0:int(self.Nt / 2)] = 0.0
        BMP[int(self.Ny / 2):-1, int(self.Nt / 2):-1] = 0.0
        Bt = np.fft.fft(BMP, axis=0)
        Bt = np.fft.ifft(Bt, axis=1)
        Btmax = np.max(np.real(Bt))
        im = ax.imshow(np.real(Bt) / Btmax,
                       cmap="jet",
                       origin="lower",
                       aspect="auto",
                       interpolation="bilinear",
                       extent=ext)  #,vmin=-0.5,vmax=0.5)
        #ax.set_xlim([0,10])

        ax_div = make_axes_locatable(ax)
        cax = ax_div.append_axes("right", size="5%", pad="2%")
        cb = colorbar(im, cax=cax)
        ax.set_ylabel("y [mm]")
        ax.set_xlabel("frequency [MHz]")
        ax.set_title("Frequency spectrum")
Ejemplo n.º 22
0
                                  density=True,
                                  weights=wgt)
        binm = 0.5 * (bins[0:-1] + bins[1:])
        cx0.plot(binm, hist, label=txtf)
        C = omg / K
        C = np.reshape(C, [Nx * Ny])
        Cb = np.sum(C * wgt) / np.sum(wgt[:])
        print("<c>=", np.mean(C[:]), Cb)

        axdiv = make_axes_locatable(ax[k])
        bxdiv = make_axes_locatable(bx[k])
        cxdiv = make_axes_locatable(cx[k])
        cax = axdiv.append_axes("right", size="7%", pad="2%")
        cbx = bxdiv.append_axes("right", size="7%", pad="2%")
        ccx = cxdiv.append_axes("right", size="7%", pad="2%")
        cba = colorbar(ima, cax=cax)
        cbb = colorbar(imb, cax=cbx)
        cbc = colorbar(imc, cax=ccx)

        ax[k].set_xlabel("x [mm]", fontsize=fsz)
        bx[k].set_xlabel("x [mm]", fontsize=fsz)
        cx[k].set_xlabel("x [mm]", fontsize=fsz)

    #ax0.text(0,5,"(a)",fontsize=fsz+2,horizontalalignment="center")
    ax0.set_xlabel("wave number [/mm]", fontsize=fsz)
    ax0.set_ylabel("probability density", fontsize=fsz)

    #bx0.text(-170,0.012,"(b)",fontsize=fsz+2,horizontalalignment="left",verticalalignment="top")
    bx0.set_xlabel("angle [deg]", fontsize=fsz)
    bx0.set_ylabel("probability density", fontsize=fsz)
    bx0.set_xlim([-180, 180])
Ejemplo n.º 23
0
def plotTimeSpatial(argv):
    # plt.rcParams.update({'font.size': 14})
    sns.set(style='whitegrid', rc={"grid.linewidth": 0.1})
    sns.set_context("paper", font_scale=0.9)
    ncols = 2
    nrows = ceil(len(argv['models']) / ncols)
    fig, axes = plt.subplots(nrows=nrows,
                             ncols=ncols,
                             figsize=(14, 12),
                             dpi=100,
                             sharex=False,
                             sharey=True)
    axes = axes.flatten()
    for i, model in enumerate(argv['models']):
        # if i==1:
        fpath = '../data/month/%s.nc' % model
        dataset = Dataset(fpath, mode='r')
        LATS = dataset.variables['lat'][:]
        LONS = dataset.variables['long'][:]
        var = dataset.variables[argv['variableNames'][i]]

        ax = axes[i]
        ax.set_title(model + ': ' + argv['label'] + ' (' + var.units + ')',
                     fontdict={'fontsize': 18})
        # time lat lon
        data = np.ma.masked_equal(var[:], 0)
        data = data.reshape(-1, 12, LAT_NUM, LON_NUM).mean(axis=(0, 3))
        data = data.T[::-1, :]
        headNan = np.zeros([(int(90 - LAT_END + GRID_LENGTH) * 2 + 1), 12])
        footNan = np.zeros([(int(LAT_START + 60) * 2) + 1, 12])
        fulldata = np.concatenate((headNan, data, footNan), axis=0)
        df = pd.DataFrame(fulldata)
        # print(headNan.shape, data.shape, footNan.shape, fulldata.shape)
        LATS = -(np.arange((90 + 60) * 2 + 1) - 180) / 2
        LATS = LATS.astype(np.int32)
        df.index = LATS  # lat
        df.columns = np.arange(12) + 1  # 1-12 month
        # print(df.index, df.columns)
        # print(data[5:,:2])
        df = df.fillna(0)
        cmap = LinearSegmentedColormap.from_list('custom_cb',
                                                 [(0, '#EEEEEE'),
                                                  (0.125, '#615EFE'),
                                                  (0.25, '#0080AC'),
                                                  (0.375, '#01B251'),
                                                  (0.5, '#73C605'),
                                                  (0.675, '#DEF103'),
                                                  (0.75, '#FF9703'),
                                                  (0.875, '#FC0101'),
                                                  (1, '#B30404')],
                                                 N=125)
        hm = sns.heatmap(df,
                         annot=False,
                         yticklabels=30,
                         linewidths=0,
                         ax=ax,
                         cbar=False,
                         cmap=cmap,
                         cbar_kws={"orientation": "horizontal"})
        # hm.axes.set_title("Title",)
        # hm.set_xlabel("X Label",fontsize=30)
        # hm.set_ylabel("Y Label",fontsize=20)
        hm.tick_params(labelsize=15)
        ax_divider = make_axes_locatable(ax)
        cax = ax_divider.append_axes('right', size='5%', pad='2%')
        cax.tick_params(labelsize=18)
        colorbar(ax.get_children()[0], cax=cax, orientation='vertical')
        cax.xaxis.set_ticks_position('top')

        dataset.close()

    # fig.tight_layout(w_pad=5, h_pad=2)
    # fig.subplots_adjust(bottom=.1, hspace=0.2)
    # [left, bottom, width, height]
    # cbar_ax=fig.add_axes([.1, .05, .8, .015])
    # cbar = fig.colorbar(cs, cax=cbar_ax, orientation='horizontal')
    # cbar.set_label('kgC m-2 y-1')
    # fig.subplots_adjust(left=.05, bottom=.05)
    fig.text(0.5, 0.04, 'Month', ha='center', fontsize=22)
    fig.text(0.04,
             0.5,
             'Latitude',
             va='center',
             rotation='vertical',
             fontsize=22)

    plt.savefig('../figure/%s-lat-time.jpg' % argv['label'])
    plt.close('all')
    print(argv['label'] + ' time-spatial map')
Ejemplo n.º 24
0
    print("jmax=", jmax)

    Df = 0.125
    inc = int(Df / bndl.df)

    im = ax.imshow(np.abs(FK),
                   extent=ext,
                   interpolation="none",
                   aspect="auto",
                   cmap="jet",
                   origin="lower")
    indx = np.arange(jmin, jmax + 1, inc)
    ax.plot(bndl.freq[indx], -k_peak[indx], ".k", markersize="6")
    axdiv = make_axes_locatable(ax)
    cax = axdiv.append_axes("right", size="7%", pad="2%")
    cb = colorbar(im, cax=cax)

    fs = bndl.freq[jmin:jmax + 1]
    print(fs)
    print(k_peak)

    deg = 1
    coef = np.polyfit(k_peak[jmin:jmax + 1], fs, deg)
    pk = np.poly1d(coef)
    pkd = np.poly1d(np.polyder(coef))
    ax.plot(pk(k_peak[jmin:jmax + 1]), -k_peak[jmin:jmax + 1], "k--")
    print(coef)
    c = -bndl.freq[indx] / k_peak[indx]
    cg = -pkd(k_peak[indx])

    ax.tick_params(labelsize=fsz)
existing axes, creates a divider for it and returns an instance of the
AxesLocator class. The append_axes method of this AxesLocator can then be used
to create a new axes on a given side ("top", "right", "bottom", or "left") of
the original axes. This example uses Axes Divider to add colorbars next to
axes.
"""

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.colorbar import colorbar

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.subplots_adjust(wspace=0.5)

im1 = ax1.imshow([[1, 2], [3, 4]])
ax1_divider = make_axes_locatable(ax1)
# add an axes to the right of the main axes.
cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
cb1 = colorbar(im1, cax=cax1)

im2 = ax2.imshow([[1, 2], [3, 4]])
ax2_divider = make_axes_locatable(ax2)
# add an axes above the main axes.
cax2 = ax2_divider.append_axes("top", size="7%", pad="2%")
cb2 = colorbar(im2, cax=cax2, orientation="horizontal")
# change tick position to top. Tick position defaults to bottom and overlaps
# the image.
cax2.xaxis.set_ticks_position("top")

plt.show()
def get_demo_image():
    from matplotlib.cbook import get_sample_data
    import numpy as np
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
    z = np.load(f)
    # z is a numpy array of 15x15
    return z, (-3, 4, -4, 3)


fig, ax = plt.subplots(figsize=[5, 4])
Z, extent = get_demo_image()
ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))

axins = zoomed_inset_axes(ax, zoom=2, loc='upper left')
im = axins.imshow(Z, extent=extent, interpolation="nearest", origin="lower")

plt.xticks(visible=False)
plt.yticks(visible=False)

# colorbar
cax = inset_axes(
    axins,
    width="5%",  # width = 10% of parent_bbox width
    height="100%",  # height : 50%
    loc='lower left',
    bbox_to_anchor=(1.05, 0., 1, 1),
    bbox_transform=axins.transAxes,
    borderpad=0,
)
colorbar(im, cax=cax)
plt.show()
Ejemplo n.º 27
0
        aspect="equal",
        cmap="jet",
        origin="lower",
        extent=ext,
        interpolation="none")  #,vmin=0.,vmax=180.,interpolation="none")

    [X, Y] = np.meshgrid(x, y)
    X = np.transpose(X)
    Y = np.transpose(Y)
    Kx = np.transpose(Kx)
    C = np.angle(Kx) / np.pi * 180.
    ax.quiver(X + 20, Y, np.real(Kx), np.imag(Kx), np.abs(C), cmap="jet")

    ax_div = make_axes_locatable(ax)
    cax = ax_div.append_axes("right", size="5%", pad="2.5%")
    cbar1 = colorbar(im, cax=cax, orientation="vertical")

    fig2 = plt.figure()
    bx = fig2.add_subplot(111)
    bx.tick_params(labelsize=fsz)
    bx.set_xlabel("x[mm]", fontsize=fsz)
    bx.set_ylabel("y[mm]", fontsize=fsz)

    jm = bx.quiver(X,
                   Y,
                   np.real(Kx),
                   np.imag(Kx),
                   np.abs(C),
                   cmap="jet",
                   scale_units="xy",
                   scale=2.0,
fig, ax = plt.subplots(figsize=[5, 4])

Z, extent = get_demo_image()

ax.set(aspect=1,
       xlim=(-15, 15),
       ylim=(-20, 5))


axins = zoomed_inset_axes(ax, 2, loc=2)  # zoom = 6
im = axins.imshow(Z, extent=extent, interpolation="nearest",
                  origin="lower")

plt.xticks(visible=False)
plt.yticks(visible=False)


# colorbar
cax = inset_axes(axins,
                 width="5%",  # width = 10% of parent_bbox width
                 height="100%",  # height : 50%
                 loc=3,
                 bbox_to_anchor=(1.05, 0., 1, 1),
                 bbox_transform=axins.transAxes,
                 borderpad=0,
                 )

colorbar(im, cax=cax)

plt.show()
ax.set(aspect=1,
       xlim=(-15, 15),
       ylim=(-20, 5))


axins = zoomed_inset_axes(ax, 2, loc=2) # zoom = 6
im = axins.imshow(Z, extent=extent, interpolation="nearest",
                  origin="lower")

plt.xticks(visible=False)
plt.yticks(visible=False)


# colorbar
cax = inset_axes(axins,
                 width="5%", # width = 10% of parent_bbox width
                 height="100%", # height : 50%
                 loc=3,
                 bbox_to_anchor=(1.05, 0., 1, 1),
                 bbox_transform=axins.transAxes,
                 borderpad=0,
                 )


colorbar(im, cax=cax) #, ticks=[1,2,3])


plt.draw()
plt.show()
Ejemplo n.º 30
0
xlbl = 'X (um)'
ylbl = 'Y (um)'
fig, axarr = plt.subplots(plot_rows,
                          plot_columns,
                          figsize=(2 * plot_columns, 6 * plot_rows))
fig.tight_layout(pad=5.0)

vmin = 345
vmax = 358
cmap = plt.get_cmap('jet')

for ax in axarr.flat:
    try:  # In case the number of your files is not a multiple of 4
        dict_key = onlyfiles[axnum].split(".nid")[0]
        im[str(axnum)] = ax.pcolormesh(xaxis_dict[dict_key],
                                       yaxis_dict[dict_key],
                                       data_dict[dict_key],
                                       cmap=cmap)
        if force_limits:
            im[str(axnum)].set_clim(vmin=vmin, vmax=vmax)

        ax.set_title(dict_key[0:15])  # 15 characters from the filename
        ax.set(xlabel=xlbl, ylabel=ylbl)
        ax_divider = make_axes_locatable(ax)
        cax = ax_divider.append_axes("right", size="5%", pad="2%")
        colorbar(im[str(axnum)], cax=cax)
        axnum += 1
    except:
        pass
fig.subplots_adjust(wspace=0.6, hspace=0.8)
plt.show()
Ejemplo n.º 31
0
"""
=================
Demo New Colorbar
=================

"""
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.colorbar import colorbar

plt.rcParams["text.usetex"] = False

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))

im1 = ax1.imshow([[1, 2], [3, 4]])
cb1 = fig.colorbar(im1, ax=ax1)
cb1.ax.set_yticks([1, 3])
ax1.set_title("Original MPL's colorbar w/\nset_yticks([1,3])", size=10)

im2 = ax2.imshow([[1, 2], [3, 4]])
cb2 = colorbar(im2, ax=ax2)
cb2.ax.set_yticks([1, 3])
ax2.set_title("AxesGrid's colorbar w/\nset_yticks([1,3])", size=10)

plt.show()
Ejemplo n.º 32
0
    isum = 0  # init. counter
    for k in nums:
        fname = dir_name + "/scope_" + str(k) + ".csv"  # data file name
        awv.load(fname)  # load A-scan data
        if isum == 0:
            bwv = Bwv(nfile, awv.Nt)  # initialize B-scan container
            bwv.set_time(awv.time[0],
                         awv.time[-1])  # set time & frequency axes
        # Copy from awv --> bwv
        bwv.B[isum, :] += awv.amp[:]  # time signal
        isum += 1  # increment counter
    fig1 = plt.figure()
    ax = fig1.add_subplot(111)
    ax_div = make_axes_locatable(ax)
    cax = ax_div.append_axes("right", size="7%", pad="2%")
    im = bwv.show(ax)
    cbar = colorbar(im, cax=cax)
    cbar.ax.tick_params(labelsize=12)
    #-----------------------------------------

    fsz = 14
    ax.set_xlabel("time [$\mu$sec]", fontsize=fsz)
    ax.set_ylabel("data No.", fontsize=fsz)
    ax.set_xlim([5, 35])

    ax.tick_params(labelsize=fsz)
    ax.set_title(name, fontsize=fsz)
    fig1.savefig("bscan_" + Mineral + ".png", bbox_inches="tight")
    plt.show()
Ejemplo n.º 33
0
    def pltshow(plt, dpi=150):
        global imgx, imgy
        temppath = tempimage()
        plt.savefig(temppath, dpi=dpi)
        dx,dy = imagesize(temppath)
        w = min(W,dx)
        image(temppath,imgx,imgy,width=w)
        imgy = imgy + dy + 20
        os.remove(temppath)
        size(W, HEIGHT+dy+40)
else:
    def pltshow(mplpyplot):
        mplpyplot.show()
# nodebox section end

plt.rcParams["text.usetex"] = False

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))

im1 = ax1.imshow([[1, 2], [3, 4]])
cb1 = fig.colorbar(im1, ax=ax1)
cb1.ax.set_yticks([1, 3])
ax1.set_title("Original MPL's colorbar w/\nset_yticks([1,3])", size=10)

im2 = ax2.imshow([[1, 2], [3, 4]])
cb2 = colorbar(im2, ax=ax2)
cb2.ax.set_yticks([1, 3])
ax2.set_title("AxesGrid's colorbar w/\nset_yticks([1,3])", size=10)

pltshow(plt)

fig = plt.figure(1, [5, 4])
ax = fig.add_subplot(111)

Z, extent = get_demo_image()

ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))

axins = zoomed_inset_axes(ax, 2, loc=2)  # zoom = 6
im = axins.imshow(Z, extent=extent, interpolation="nearest", origin="lower")

plt.xticks(visible=False)
plt.yticks(visible=False)

# colorbar
cax = inset_axes(
    axins,
    width="5%",  # width = 10% of parent_bbox width
    height="100%",  # height : 50%
    loc=3,
    bbox_to_anchor=(1.05, 0., 1, 1),
    bbox_transform=axins.transAxes,
    borderpad=0,
)

colorbar(im, cax=cax)  #, ticks=[1,2,3])

plt.draw()
plt.show()
def save_figures(data, source, model_version, filter, suffix=None, k=10):

    # Load data from json obj
    results = data['results']
    df = pd.DataFrame(results)

    # Aggregate by head
    # Convert column to 3d ndarray (num_examples x num_layers x num_heads)
    indirect_by_head = np.stack(df['indirect_effect_head'].to_numpy())
    direct_by_head = np.stack(df['direct_effect_head'].to_numpy())
    # Average by head
    mean_indirect_by_head = indirect_by_head.mean(axis=0)
    mean_direct_by_head = direct_by_head.mean(axis=0)
    # Select top k heads by indirect effect
    topk_inds = topk_indices(mean_indirect_by_head, k)

    # Aggregate by layer
    # Convert column to 2d ndarray (num_examples x num_layers)
    indirect_by_layer = np.stack(df['indirect_effect_layer'].to_numpy())
    direct_by_layer = np.stack(df['direct_effect_layer'].to_numpy())
    mean_indirect_by_layer = indirect_by_layer.mean(axis=0)
    mean_direct_by_layer = direct_by_layer.mean(axis=0)
    n_layers = indirect_by_layer.shape[1]

    plt.rc('figure', titlesize=20)

    # Plot stacked bar chart
    palette = sns.color_palette()  #('muted')
    plt.figure(num=1, figsize=(5, 2))
    topk_direct = []
    topk_indirect = []
    labels = []
    for ind in topk_inds:
        layer, head = np.unravel_index(ind, mean_indirect_by_head.shape)
        topk_indirect.append(mean_indirect_by_head[layer, head])
        topk_direct.append(mean_direct_by_head[layer, head])
        labels.append(f'{layer}-{head}')
    width = 0.6
    inds = range(k)
    p1 = plt.bar(inds, topk_indirect, width, linewidth=0, color=palette[1])
    p2 = plt.bar(inds,
                 topk_direct,
                 width,
                 bottom=topk_indirect,
                 linewidth=0,
                 color=palette[0])
    plt.ylabel('Effect', size=11)
    plt.title('Effects of top heads', fontsize=11)
    plt.xticks(inds, labels, size=10)
    plt.yticks(size=10)
    p3 = plt.axhline(data['mean_total_effect'], linestyle='--')
    plt.legend((p3, p2[0], p1[0]), ('Total', 'Direct', 'Indirect'),
               loc='upper right',
               fontsize=11,
               bbox_to_anchor=(.99, 0.90))
    sns.despine()
    path = 'structural_attention/figures/stacked_bar_charts'
    if not os.path.exists(path):
        os.makedirs(path)
    plt.savefig(f'{path}/{source}_{model_version}_{filter}.pdf', format='pdf')
    plt.close()
    annot = False

    # Plot heatmap for direct and indirect effect
    for effect_type in ('indirect', 'direct'):
        if effect_type == 'indirect':
            mean_effect = mean_indirect_by_head
        else:
            mean_effect = mean_direct_by_head
        ax = sns.heatmap(mean_effect,
                         rasterized=True,
                         annot=annot,
                         annot_kws={"size": 9},
                         fmt=".2f",
                         square=True)
        ax.set(xlabel='Head',
               ylabel='Layer',
               title=f'Mean {effect_type.capitalize()} Effect')
        plt.figure(num=1, figsize=(7, 5))
        path = f'structural_attention/figures/heat_maps_{effect_type}'
        if not os.path.exists(path):
            os.makedirs(path)
        plt.savefig(f'{path}/{source}_{model_version}_{filter}.pdf',
                    format='pdf')
        plt.close()

    # Plot layer-level bar chart for indirect and direct effects
    for effect_type in ('indirect', 'direct'):
        if effect_type == 'indirect':
            mean_effect = mean_indirect_by_layer
        else:
            mean_effect = mean_direct_by_layer
        plt.figure(num=1, figsize=(5, 5))
        ax = sns.barplot(x=mean_effect,
                         y=list(range(n_layers)),
                         orient="h",
                         color="#4472C4")
        ax.set(ylabel='Layer', title=f'Mean {effect_type.capitalize()} Effect')
        path = f'structural_attention/figures/layer_{effect_type}'
        if not os.path.exists(path):
            os.makedirs(path)
        plt.savefig(f'{path}/{source}_{model_version}_{filter}.pdf',
                    format='pdf')
        plt.close()

    # Plot combined heatmap and barchart for direct and indirect effects
    for do_sort in False, True:
        for effect_type in ('indirect', 'direct'):
            if effect_type == 'indirect':
                effect_head = mean_indirect_by_head
                effect_layer = mean_indirect_by_layer
                if do_sort:
                    effect_head = -np.sort(
                        -effect_head
                    )  # Sort indirect effects within each layer in descending order
            else:
                if do_sort:
                    continue
                effect_head = mean_direct_by_head
                effect_layer = mean_direct_by_layer
            fig = plt.figure(figsize=(3, 2.2))
            if model_version == 'distilgpt2':
                ax1 = plt.subplot2grid((100, 85), (0, 0),
                                       colspan=62,
                                       rowspan=99)
                ax2 = plt.subplot2grid((100, 85), (32, 69),
                                       colspan=17,
                                       rowspan=35)
            elif model_version in ('gpt2', 'gpt2_random'):
                ax1 = plt.subplot2grid((100, 85), (0, 0),
                                       colspan=65,
                                       rowspan=99)
                ax2 = plt.subplot2grid((100, 85), (12, 70),
                                       colspan=15,
                                       rowspan=75)
            elif model_version == 'gpt2-medium':
                ax1 = plt.subplot2grid((100, 85), (0, 5),
                                       colspan=55,
                                       rowspan=99)
                ax2 = plt.subplot2grid((100, 85), (2, 64),
                                       colspan=17,
                                       rowspan=95)
            elif model_version == 'gpt2-large':
                ax1 = plt.subplot2grid((100, 85), (0, 5),
                                       colspan=55,
                                       rowspan=96)
                ax2 = plt.subplot2grid((100, 85), (0, 62),
                                       colspan=17,
                                       rowspan=97)
            elif model_version == 'gpt2-xl':
                ax1 = plt.subplot2grid((100, 85), (0, 5),
                                       colspan=55,
                                       rowspan=96)
                ax2 = plt.subplot2grid((100, 85), (0, 62),
                                       colspan=17,
                                       rowspan=97)
            heatmap = sns.heatmap(effect_head,
                                  center=0.0,
                                  ax=ax1,
                                  annot=annot,
                                  annot_kws={"size": 9},
                                  fmt=".2f",
                                  square=True,
                                  cbar=False,
                                  linewidth=0.1,
                                  linecolor='#D0D0D0',
                                  cmap=LinearSegmentedColormap.from_list(
                                      'rg', ["#F14100", "white", "#3D4FC4"],
                                      N=256))
            plt.setp(heatmap.get_yticklabels(), fontsize=7)
            plt.setp(heatmap.get_xticklabels(), fontsize=7)
            heatmap.tick_params(axis='x', pad=1, length=2)
            heatmap.tick_params(axis='y', pad=1, length=2)
            heatmap.yaxis.labelpad = 2
            heatmap.invert_yaxis()
            if model_version != 'gpt2-xl':
                for i, label in enumerate(heatmap.xaxis.get_ticklabels()):
                    if i % 2 == 1:
                        label.set_visible(False)
                for i, label in enumerate(heatmap.yaxis.get_ticklabels()):
                    if i % 2 == 1:
                        label.set_visible(False)
            if do_sort:
                heatmap.axes.get_xaxis().set_ticks([])
            else:
                if model_version == 'gpt2-xl':
                    every_nth = 2
                    for n, label in enumerate(ax1.xaxis.get_ticklabels()):
                        if n % every_nth != 0:
                            label.set_visible(False)
                    for n, label in enumerate(ax1.yaxis.get_ticklabels()):
                        if n % every_nth != 0:
                            label.set_visible(False)
            # split axes of heatmap to put colorbar
            ax_divider = make_axes_locatable(ax1)
            if model_version in ('gpt2-large', 'gpt2-xl'):
                cax = ax_divider.append_axes('left', size='7%', pad='45%')
            else:
                cax = ax_divider.append_axes('left', size='7%', pad='33%')
            # # make colorbar for heatmap.
            # # Heatmap returns an axes obj but you need to get a mappable obj (get_children)
            cbar = colorbar(ax1.get_children()[0],
                            cax=cax,
                            orientation='vertical')
            cax.yaxis.set_ticks_position('left')
            cbar.solids.set_edgecolor("face")
            cbar.ax.tick_params(labelsize=7, length=4, pad=2)
            ax1.set_title(structure_to_title[source], size=6)
            ax1.set_xlabel('Head', size=6)
            ax1.set_ylabel('Layer', size=6)
            for _, spine in ax1.spines.items():
                spine.set_visible(True)
            ax2.set_title('         Layer Effect', size=6)
            bp = sns.barplot(x=effect_layer,
                             ax=ax2,
                             y=list(range(n_layers)),
                             color="#3D4FC4",
                             orient="h")
            plt.setp(bp.get_xticklabels(), fontsize=7)
            bp.tick_params(axis='x', pad=1, length=3)
            ax2.invert_yaxis()
            ax2.set_yticklabels([])
            ax2.spines['top'].set_visible(False)
            ax2.spines['right'].set_visible(False)
            ax2.spines['left'].set_visible(False)
            ax2.xaxis.set_ticks_position('bottom')
            ax2.axvline(0, linewidth=.85, color='black')
            path = f'structural_attention/figures/heat_maps_with_bar_{effect_type}{"_sorted" if do_sort else ""}'
            if not os.path.exists(path):
                os.makedirs(path)
            fname = f'{path}/{source}_{model_version}_{filter}.pdf'
            plt.savefig(fname, format='pdf', bbox_inches='tight')
            plt.close()
Ejemplo n.º 36
0
import matplotlib.pyplot as plt

plt.rcParams["text.usetex"]=False

fig = plt.figure(1, figsize=(6, 3))

ax1 = fig.add_subplot(121)
im1 = ax1.imshow([[1,2],[3,4]])
cb1 = plt.colorbar(im1)
cb1.ax.set_yticks([1, 3])
ax1.set_title("Original MPL's colorbar w/\nset_yticks([1,3])", size=10)

from mpl_toolkits.axes_grid1.colorbar import colorbar
ax2 = fig.add_subplot(122)
im2 = ax2.imshow([[1,2],[3,4]])
cb2 = colorbar(im2)
cb2.ax.set_yticks([1, 3])
ax2.set_title("AxesGrid's colorbar w/\nset_yticks([1,3])", size=10)

plt.show()