コード例 #1
0
def single_plot_computational_time(ax, data, colname, whichmethods, pve, rho,
                                   dims, sfrac):
    yscale = 'linear'
    xscale = 'log10'
    ylabels = list()
    for i, method in enumerate(whichmethods):
        # Select relevant rows
        mconditions = [f"$(fit) == {method}"]
        mconditions += [f"$(simulate.pve) == {pve}"]
        mconditions += [f"$(simulate.rho) == {rho}"]
        mconditions += [f"$(simulate.sfrac) == {sfrac}"]
        dfselect = pd_utils.select_dfrows(data, mconditions)

        # Plotting style
        pm = methodprops.plot_metainfo()[method]
        boxprops = dict(linewidth=0, color=pm.color, facecolor=pm.color)
        medianprops = dict(linewidth=0, color=pm.color)
        whiskerprops = dict(color=pm.color)
        flierprops = dict(marker='o',
                          markerfacecolor=pm.color,
                          markersize=4,
                          markeredgewidth=0,
                          markeredgecolor=pm.color)

        # Boxplot
        times = dfselect[colname].to_numpy()
        xx = mpl_utils.scale_array(times, xscale)
        ax.boxplot(xx,
                   positions=[i + 1],
                   showfliers=True,
                   showcaps=False,
                   widths=0.6,
                   vert=False,
                   patch_artist=True,
                   notch=False,
                   boxprops=boxprops,
                   medianprops=medianprops,
                   whiskerprops=whiskerprops,
                   flierprops=flierprops)
        ylabels.append(pm.label)

        # Background barplot
        xleft = mpl_utils.scale_array(0.1, xscale)
        xmean = mpl_utils.scale_array(np.mean(times), xscale) - xleft
        ax.barh(i + 1,
                xmean,
                left=xleft,
                align='center',
                color=pm.color,
                linewidth=0,
                height=0.6,
                alpha=0.2)

    ax.tick_params(labelcolor="#333333", left=False)
    ax.set_yticklabels(ylabels, rotation=0)
    mpl_utils.set_soft_xlim(ax, 0.09, 40, scale=xscale)
    mpl_utils.set_xticks(ax, scale=xscale, kmin=3, kmax=4, spacing='log10')
    mpl_utils.decorate_axes(ax, hide=["top", "right", "left"], ticklimits=True)
    return
コード例 #2
0
def create_single_setting_score_evolution_plot(data, method, dim, sfrac, pve,
                                               rho_list):
    mpl_stylesheet.banskt_presentation(fontsize=12)

    ncol = 10
    nrow = 2
    wspace = 0.05
    hspace = 0.05
    axwidth = 1.2
    aspect = 1
    xscale = 'linear'
    yscale = 'log10'
    fgap = 0.05
    pm = methodprops.plot_metainfo()[method]

    figw = ncol * axwidth
    axheight = axwidth * aspect
    subfigh = nrow * axheight + hspace * (nrow - 1) * axheight
    figh = (subfigh * 2) / (1 - fgap)
    fig = plt.figure(figsize=(figw, figh))

    # Hard-coded Gridspec
    gs = [None for x in rho_list]
    gs[0] = gridspec.GridSpec(nrow, ncol, top=1.0, bottom=0.5 + fgap)
    gs[1] = gridspec.GridSpec(nrow, ncol, top=0.5 - fgap, bottom=0.0)
    nplot = ncol * nrow * len(gs)

    axlist = [list() for x in rho_list]
    for ig, rho in enumerate(rho_list):
        gs[ig].update(wspace=wspace, hspace=hspace)
        for i, scores in enumerate(data[rho]):
            rownum = int(i / ncol)
            colnum = i - rownum * ncol
            ax = fig.add_subplot(gs[ig][rownum, colnum])
            ax.tick_params(left=False,
                           labelleft=False,
                           bottom=False,
                           labelbottom=False)
            mpl_utils.decorate_axes(ax, hide=["all"])
            ax.plot(np.arange(scores.shape[0]), np.log10(scores), lw=1)
            ax.set_facecolor('#E8E8E8')
            axlist[ig].append(ax)
        pcaus = max(1, int(sfrac * dim[1]))
        figtitle = f"{pm.label} (n = {dim[0]}, p = {dim[1]}, s = {pcaus}, " + r"$\rho$ = " + f"{rho}, pve = {pve})"

        xlab_idx = int(ncol / 2)
        xlab_offset = -wspace / 2 if nplot % 2 != 0 else 0
        axlist[ig][xlab_idx].set_title(figtitle, x=xlab_offset, pad=10)

    #ncol * int(nrow / 2)
    ylab_offset = 1 + figh * fgap
    axlist[1][0].set_ylabel(r"$\log_{10}$(RMSE / $\sigma$)", y=ylab_offset)
    axlist[1][15].set_xlabel(r"Number of iterations", x=xlab_offset)

    plt.show()
    return
コード例 #3
0
def single_plot_score_methods(ax,
                              resdf,
                              colname,
                              methods,
                              pve,
                              rho,
                              dims,
                              sfracs,
                              use_median=False):
    xvals = [max(1, int(x * dims[1])) for x in sfracs]
    xscale = 'log10'
    yscale = 'log10'
    for method in methods:
        score = [0 for x in sfracs]
        mconditions = [f"$(fit) == {method}"]
        mconditions += [f"$(simulate.pve) == {pve}"]
        mconditions += [f"$(simulate.rho) == {rho}"]
        for i, sfrac in enumerate(sfracs):
            sfrac_condition = f"$(simulate.sfrac) == {sfrac}"
            dfselect = pd_utils.select_dfrows(resdf,
                                              mconditions + [sfrac_condition])
            scores = dfselect[colname].to_numpy()
            if use_median:
                score[i] = np.median(scores[~np.isnan(scores)])
            else:
                score[i] = np.mean(scores[~np.isnan(scores)])

        # Plot xvals vs score
        pm = methodprops.plot_metainfo()[method]
        xx = mpl_utils.scale_array(xvals, xscale)
        yy = mpl_utils.scale_array(score, yscale)
        ax.plot(xx,
                yy,
                label=pm.label,
                color=pm.color,
                lw=pm.linewidth / 2,
                ls=pm.linestyle,
                marker=pm.marker,
                ms=pm.size / 1.2,
                mec=pm.color,
                mfc=pm.facecolor,
                mew=pm.linewidth,
                zorder=pm.zorder)

    mpl_utils.set_soft_ylim(ax, 1.0, 1.2, scale=yscale)
    mpl_utils.set_xticks(ax, scale=xscale, tickmarks=xvals)
    mpl_utils.set_yticks(ax, scale=yscale, kmin=3, kmax=4, forceticks=[1.0])
    mpl_utils.decorate_axes(ax, hide=["top", "right"], ticklimits=True)
    return
コード例 #4
0
def create_single_method_score_distribution_plot(data, method, dim_list,
                                                 rho_list, pve_list, sfracs,
                                                 colname):
    ncol = 4
    nrow = 2
    wspace = 0.2
    hspace = 1.5
    aspect = 0.7
    xscale = 'log10'
    yscale = 'log10'
    nan_pos = -1.5
    figw = 12

    figh, blockh, axh, axw = get_dimensions(1,
                                            nrow,
                                            ncol,
                                            wspace,
                                            hspace,
                                            0,
                                            aspectratio=aspect)
    fig = plt.figure(figsize=(figw, figh))
    gs = gridspec.GridSpec(nrow, ncol)
    gs.update(wspace=wspace, hspace=hspace)

    pm = methodprops.plot_metainfo()[method]
    figtitle = f"{pm.label}"
    xlab_offset = -wspace / 2 if ncol % 2 == 0 else 0.5
    ylab_offset = (1 + hspace / 2) if nrow % 2 == 0 else 0.5

    axrow = list()
    for i, dim in enumerate(dim_list):
        xvals = [max(1, int(x * dim[1])) for x in sfracs]
        axlist = list()
        allscores = list()
        resdf = pd_utils.select_dfrows(
            data, [f"$(simulate.dims) == '({dim[0]},{dim[1]})'"])
        for j, rho in enumerate(rho_list):
            for k, pve in enumerate(pve_list):
                colnum = j * 2 + k
                if len(axlist) == 0:
                    ax = fig.add_subplot(gs[i, colnum])
                    ax.text(0,
                            1.3,
                            f"n = {dim[0]}",
                            va='bottom',
                            ha='left',
                            transform=ax.transAxes)
                else:
                    ax = fig.add_subplot(gs[i, colnum], sharey=axlist[0])
                ax.text(0.5,
                        1.05,
                        f"pve = {pve:g}, " + r"$\rho$ = {:g}".format(rho),
                        va='bottom',
                        ha='center',
                        transform=ax.transAxes)

                # Main plot
                mconditions = [f"$(fit) == {method}"]
                mconditions += [f"$(simulate.rho) == {rho}"]
                mconditions += [f"$(simulate.pve) == {pve}"]
                for s, sfrac in enumerate(sfracs):
                    scondition = [f"$(simulate.sfrac) == {sfrac}"]
                    dfselect = pd_utils.select_dfrows(resdf,
                                                      mconditions + scondition)
                    scores = dfselect[colname].to_numpy()
                    num_nan = np.sum(np.isnan(scores))
                    xpos = mpl_utils.scale_list([xvals[s]], scale=xscale)
                    yvals = mpl_utils.scale_array(scores[~np.isnan(scores)],
                                                  scale=yscale)
                    ax.scatter(xpos * len(yvals), yvals, alpha=0.5)
                    ax.text(xpos[0],
                            nan_pos,
                            f"{num_nan}",
                            ha='center',
                            va='bottom')

                # Tick marks and axes decoration
                mpl_utils.set_xticks(ax,
                                     scale=xscale,
                                     tickmarks=xvals,
                                     rotation=90)
                ax.tick_params(labelcolor="#333333", left=False)
                if len(axlist) > 0: ax.tick_params(labelleft=False)
                mpl_utils.decorate_axes(ax,
                                        hide=["left", "right", "top"],
                                        ticklimits=True,
                                        pads=[34, 10])
                mpl_utils.set_xticks(ax,
                                     scale=xscale,
                                     tickmarks=xvals,
                                     rotation=90)
                for side, border in ax.spines.items():
                    if side == "top": border.set_visible(True)
                ax.grid(which='major', axis='y', ls='dotted')
                axlist.append(ax)
        '''
        Following indices are now hard-coded
        '''
        axlist[2].set_xlabel(r"Number of non-zero coefficients (s)",
                             x=xlab_offset)
        mpl_utils.set_yticks(axlist[0], scale=yscale, spacing='log10')
        axlist[0].text(0, nan_pos, f'nan', ha='right', va='bottom')
        axrow.append(axlist)

    axrow[1][0].set_ylabel(r"Prediction Error (RMSE / $\sigma$)",
                           y=ylab_offset)
    axrow[0][2].set_title(figtitle, x=xlab_offset, pad=40)

    plt.show()
    return
コード例 #5
0
def create_figure_prediction_error(whichmethods,
                                   data,
                                   dims,
                                   rho_list,
                                   pve_list,
                                   sfracs,
                                   use_median=False):

    # Define main settings
    leg_hprop = 0.4
    axwidth = 6
    wspace = 0.2
    hspace = 0.5

    # Number of rows required to show all plots
    ncol = len(rho_list)
    nrow = len(pve_list)
    nplot = ncol * nrow
    #nplot = len(pve_list) * len(rho_list)
    #nrow = math.ceil(nplot / ncol)
    figw = ncol * axwidth

    # Figure height depends on number of rows
    # Height of all rows + height of legend axis + reserved space between rows
    figh = nrow * axwidth + leg_hprop * axwidth + hspace * (nrow - 1) * axwidth

    fig = plt.figure(figsize=(figw, figh))
    gs = gridspec.GridSpec(nrow + 1,
                           ncol,
                           height_ratios=[leg_hprop] +
                           [1 for x in range(nrow)])
    gs.update(wspace=wspace, hspace=hspace)

    # Subplots
    axlist = list()
    for i, pve in enumerate(pve_list):
        for j, rho in enumerate(rho_list):
            ax = fig.add_subplot(gs[i + 1, j])

            # Plot for this pve and rho
            single_plot_score_methods(ax,
                                      data,
                                      'score1',
                                      whichmethods,
                                      pve,
                                      rho,
                                      dims,
                                      sfracs,
                                      use_median=use_median)
            ax.tick_params(labelcolor="#333333")

            # Subplot title
            ax.text(0.05,
                    1.1,
                    f"pve = {pve:g}" + "\n" + r"$\rho$ = {:g}".format(rho),
                    va='top',
                    ha='left',
                    transform=ax.transAxes)

            axlist.append(ax)

    # Legend
    ax0 = fig.add_subplot(gs[0, :])
    ax0.tick_params(bottom=False,
                    left=False,
                    labelbottom=False,
                    labelleft=False)
    mhandles, mlabels = axlist[0].get_legend_handles_labels()
    ax0.legend(handles=mhandles,
               labels=mlabels,
               ncol=2,
               handlelength=2,
               loc='upper left',
               bbox_to_anchor=(0, 1.0),
               frameon=True,
               framealpha=1.0,
               borderpad=0.8)
    mpl_utils.decorate_axes(ax0, hide=["all"], ticklimits=False)

    # Axes labels at center of subplots
    ylab_idx = ncol * int(nrow / 2)
    ylab_offset = (1 + hspace / 2) if nrow % 2 == 0 else 0.5
    xlab_idx = nplot - 1
    xlab_offset = -wspace / 2 if nplot % 2 == 0 else 0.5
    axlist[ylab_idx].set_ylabel(r"Prediction Error (RMSE / $\sigma$)",
                                y=ylab_offset)
    axlist[xlab_idx].set_xlabel(r"Number of non-zero coefficients (s)",
                                x=xlab_offset)

    plt.show()
    return