Esempio n. 1
0
File: views.py Progetto: HMP1/bumps
def _decorate_histogram(vstats):
    import pylab
    from matplotlib.transforms import blended_transform_factory as blend
    # Shade things inside 1-sigma
    pylab.axvspan(vstats.p68[0],vstats.p68[1],
                  color='gold',alpha=0.5,zorder=-1)
    # build transform with x=data, y=axes(0,1)
    ax = pylab.gca()
    transform = blend(ax.transData, ax.transAxes)

    l95,h95 = vstats.p95
    l68,h68 = vstats.p68
    def marker(s,v):
        if v < l95: s,v,ha = '<'+s,l95,'left'
        elif v > h95: s,v,ha = '>'+s,h95,'right'
        else: ha='center'
        pylab.text(v, 0.95, s, va='top', ha=ha,
                   transform=transform, zorder=3, color='g')
        #pylab.axvline(v)
    marker('|',vstats.median)
    marker('E',vstats.mean)
    marker('*',vstats.best)

    pylab.text(0.01, 0.95, vstats.label, zorder=2,
        backgroundcolor=(1,1,0,0.2),
        verticalalignment='top',
        horizontalalignment='left',
        transform=pylab.gca().transAxes)
    pylab.setp([pylab.gca().get_yticklabels()],visible=False)
    ticks = (l95, l68, vstats.median, h68, h95)
    labels = [format_value(v,h95-l95) for v in ticks]
    if len(labels[2]) > 5:
        # Drop 68% values if too many digits
        ticks,labels= ticks[0::2],labels[0::2]
    pylab.xticks(ticks, labels)
Esempio n. 2
0
def _decorate_histogram(vstats):
    import pylab
    from matplotlib.transforms import blended_transform_factory as blend
    # Shade things inside 1-sigma
    pylab.axvspan(vstats.p68[0],
                  vstats.p68[1],
                  color='gold',
                  alpha=0.5,
                  zorder=-1)
    # build transform with x=data, y=axes(0,1)
    ax = pylab.gca()
    transform = blend(ax.transData, ax.transAxes)

    l95, h95 = vstats.p95
    l68, h68 = vstats.p68

    def marker(symbol, position):
        if position < l95:
            symbol, position, ha = '<' + symbol, l95, 'left'
        elif position > h95:
            symbol, position, ha = '>' + symbol, h95, 'right'
        else:
            symbol, position, ha = symbol, position, 'center'
        pylab.text(position,
                   0.95,
                   symbol,
                   va='top',
                   ha=ha,
                   transform=transform,
                   zorder=3,
                   color='g')
        #pylab.axvline(v)

    marker('|', vstats.median)
    marker('E', vstats.mean)
    marker('*', vstats.best)

    pylab.text(0.01,
               0.95,
               vstats.label,
               zorder=2,
               backgroundcolor=(1, 1, 0, 0.2),
               verticalalignment='top',
               horizontalalignment='left',
               transform=pylab.gca().transAxes)
    pylab.setp([pylab.gca().get_yticklabels()], visible=False)
    ticks = (l95, l68, vstats.median, h68, h95)
    labels = [format_value(v, h95 - l95) for v in ticks]
    if len(labels[2]) > 5:
        # Drop 68% values if too many digits
        ticks, labels = ticks[0::2], labels[0::2]
    pylab.xticks(ticks, labels)
Esempio n. 3
0
def _decorate_histogram(vstats):
    import pylab
    from matplotlib.transforms import blended_transform_factory as blend

    l95, h95 = vstats.p95_range
    l68, h68 = vstats.p68_range

    # Shade things inside 1-sigma
    pylab.axvspan(l68, h68, color='gold', alpha=0.5, zorder=-1)
    # build transform with x=data, y=axes(0,1)
    ax = pylab.gca()
    transform = blend(ax.transData, ax.transAxes)

    def marker(symbol, position):
        if position < l95:
            symbol, position, ha = '<' + symbol, l95, 'left'
        elif position > h95:
            symbol, position, ha = '>' + symbol, h95, 'right'
        else:
            symbol, position, ha = symbol, position, 'center'
        pylab.text(position,
                   0.95,
                   symbol,
                   va='top',
                   ha=ha,
                   transform=transform,
                   zorder=3,
                   color='g')
        #pylab.axvline(v)

    marker('|', vstats.median)
    marker('E', vstats.mean)
    marker('*', vstats.best)

    pylab.text(0.01,
               0.95,
               vstats.label,
               zorder=2,
               backgroundcolor=(1, 1, 0, 0.2),
               verticalalignment='top',
               horizontalalignment='left',
               transform=pylab.gca().transAxes)
    ax.set_yticklabels([])
Esempio n. 4
0
def annotate(entry0, entry1, dim, minfvalue=1e-8, nbtests=1):
    """Display some annotations associated to the graphs generated."""

    isEarlyStop = False
    ha = 'left'
    va = 'center'
    lastfvalue = min(entry0.evals[-1][0], entry1.evals[-1][0])
    if minfvalue < lastfvalue:
        isEarlyStop = True
        #ha = 'center'
        #va = 'top'

    if not minfvalue or minfvalue < lastfvalue:
        minfvalue = lastfvalue

    line = []
    data0 = entry0.detEvals([minfvalue])[0]
    evals0 = data0.copy()
    succ = (np.isnan(evals0) == False)
    evals0[np.isnan(evals0)] = entry0.maxevals[np.isnan(evals0)]
    line.append(toolsstats.sp(evals0, issuccessful=succ))
    data1 = entry1.detEvals([minfvalue])[0]
    evals1 = data1.copy()
    succ = (np.isnan(evals1) == False)
    evals1[np.isnan(evals1)] = entry1.maxevals[np.isnan(evals1)]
    line.append(toolsstats.sp(evals1, issuccessful=succ))

    # What's the situation?
    txt = '%dD' % dim
    if (line[0][2] > 0 and line[1][2] > 0 and line[1][2] < 10):
        tmp = str(int(line[1][2]))
        tmp2 = str(int(line[0][2]))
        txt = tmp + '/' + tmp2

    dims = dimension_index
    ax = plt.gca()
    assert line[0][2] > 0 or line[1][2] > 0
    signdata = line[1][0] - line[0][0]

    if line[0][2] > 0 and line[1][2] > 0:
        trans = ax.transData
        annotcoord = [minfvalue, line[1][0] / line[0][0]]
    elif line[0][2] == 0:
        trans = blend(ax.transData, ax.transAxes)
        annotcoord = [
            minfvalue, -line[1][1] / 2 + 0.5 + offset * (5 - dims[dim])
        ]
        #if va == 'top':
        #    va = 'bottom'
    elif line[1][2] == 0:
        trans = blend(ax.transData, ax.transAxes)
        annotcoord = [
            minfvalue, line[0][1] / 2 + 0.5 - offset * (5 - dims[dim])
        ]

    plt.text(annotcoord[0],
             annotcoord[1],
             txt,
             horizontalalignment=ha,
             verticalalignment=va,
             transform=trans)

    #ranksum test
    line0 = np.power(data0, -1.)
    line0[np.isnan(line0)] = -entry0.finalfunvals[np.isnan(line0)]
    line1 = np.power(data1, -1.)
    line1[np.isnan(line1)] = -entry1.finalfunvals[np.isnan(line1)]
    # one-tailed statistics: scipy.stats.mannwhitneyu, two-tailed statistics: scipy.stats.ranksumtest
    z, p = ranksumtest(line0, line1)
    # Set the correct line in data0 and data1
    nbstars = 0
    # sign of z-value and data must agree
    if ((nbtests * p) < 0.05 and (z * signdata) > 0):
        nbstars = np.min([5, -np.ceil(np.log10(nbtests * p + 1e-99))])
    if nbstars > 0:
        xstars = annotcoord[0] * np.power(incrstars, np.arange(
            1., 1. + nbstars))
        # the additional slicing [0:int(nbstars)] is due to
        # np.arange(1., 1. - 0.1 * nbstars, -0.1) not having the right number
        # of elements due to numerical error
        ystars = [annotcoord[1]] * nbstars

        try:
            h = plt.plot(xstars,
                         ystars,
                         marker='*',
                         ls='',
                         color='w',
                         markersize=5 * linewidth,
                         markeredgecolor='k',
                         markerfacecolor='None',
                         zorder=20,
                         markeredgewidth=0.4 * linewidth,
                         transform=trans,
                         clip_on=False)
        except KeyError:
            #Version problem
            h = plt.plot(xstars,
                         ystars,
                         marker='+',
                         ls='',
                         color='w',
                         markersize=2.5 * linewidth,
                         markeredgecolor='k',
                         zorder=20,
                         markeredgewidth=0.2 * linewidth,
                         transform=trans,
                         clip_on=False)
Esempio n. 5
0
def plot(xdata, ydata):
    """Plot the ERT log loss figures.

    Two cases: box-whisker plot is used for representing the data of all
    functions, otherwise all data is represented using crosses.

    """
    res = []

    tmp = list(10**np.mean(i[np.isfinite(i)]) for i in ydata)
    res.extend(plt.plot(xdata, tmp, ls='-', color='k', lw=3, #marker='+',
                        markersize=20, markeredgewidth=3))

    if max(len(i) for i in ydata) < 20: # TODO: subgroups of function, hopefully.
        for i, y in enumerate(ydata):
            # plot all single data points
            if (np.isfinite(y)==False).any():
                assert not (np.isinf(y) * y > 0.).any()
                assert not np.isnan(y).any()

                ax = plt.gca()
                trans = blend(ax.transData, ax.transAxes)
                res.extend(plt.plot((xdata[i], ), (0., ),
                                    marker='+', color=flierscolor,
                                    ls='', markersize=20, markeredgewidth=3,
                                    transform=trans, clip_on=False))
                res.append(plt.text(xdata[i], 0.02, '%d' % len(y[np.isinf(y)]),
                                    transform=trans, horizontalalignment='left',
                                    verticalalignment='bottom'))
                y = y[np.isfinite(y)]
                if len(y) == 0:
                    continue

            res.extend(plt.plot([xdata[i]]*len(y), 10**np.array(y),
                                marker='+', color=flierscolor,
                                ls='', markersize=20, markeredgewidth=3))

            # plot dashed vertical line between min and max 
            plt.plot([xdata[i]]*2, 10**np.array([min(y), max(y)]),
                                color='k',  # marker='+', 
                                ls='--', linewidth=2) #, markersize=20, markeredgewidth=3)
            # plot min and max with different symbol
            #plt.plot([xdata[i]], 10**min(np.array(y)),
            #                    marker='+', color='k',
            #                    ls='', markersize=20, markeredgewidth=3)
            #plt.plot([xdata[i]], 10**max(np.array(y)),
            #                    marker='+', color='k',
            #                    ls='', markersize=20, markeredgewidth=3)
    else:
        for i, y in enumerate(ydata):
            # plot all single data points
            if (np.isfinite(y)==False).any():
                assert not (np.isinf(y) * y > 0.).any()
                assert not np.isnan(y).any()

                ax = plt.gca()
                trans = blend(ax.transData, ax.transAxes)
                res.extend(plt.plot((xdata[i], ), (0, ),
                                    marker='.', color='k',
                                    ls='', markersize=20, markeredgewidth=3,
                                    transform=trans, clip_on=False))
                res.append(plt.text(xdata[i], 0.02, '%d' % len(y[np.isinf(y)]),
                                    transform=trans, horizontalalignment='left',
                                    verticalalignment='bottom'))
                y = y[np.isfinite(y)]

        dictboxwhisker = boxplot(list(10**np.array(i) for i in ydata),
                                 sym='', notch=0, widths=None,
                                 positions=xdata)
        #'medians', 'fliers', 'whiskers', 'boxes', 'caps'
        plt.setp(dictboxwhisker['medians'], lw=3)
        plt.setp(dictboxwhisker['boxes'], lw=3)
        plt.setp(dictboxwhisker['caps'], lw=3)
        plt.setp(dictboxwhisker['whiskers'], lw=3)
        for i in dictboxwhisker.values():
            res.extend(i)
        res.extend(plt.plot(xdata, list(10**min(i) for i in ydata), marker='.',
                            markersize=20, color='k', ls=''))
        res.extend(plt.plot(xdata, list(10**max(i) for i in ydata), marker='.',
                             markersize=20, color='k', ls=''))

    return res
Esempio n. 6
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure.
    
    TODO: """

    markers = genericsettings.dim_related_markers
    colors = genericsettings.dim_related_colors
    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    targets = testbedsettings.current_testbed.ppscatter_target_values
    if isinstance(targets, pproc.RunlengthBasedTargetValues):
        linewidth = linewidth_rld_based
    else:
        linewidth = linewidth_default

    funInfos = ppfigparam.read_fun_infos()    

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable aRT values as a line
                all_targets = np.array(sorted(set(entry0.target).union(entry1.target), reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(all_targets <= targets((f, d))[0])[0]]  # 
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and aRT <= maxevals
                idx = []
                for ari in (np.where(entry0.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(entry1.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(xdata_all <= max(entry0.maxevals))[0], 
                         np.where(ydata_all <= max(entry1.maxevals))[0]
                        ):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1])) 
                    xdata_all = xdata_all[:max_idx + 1]
                    ydata_all = ydata_all[:max_idx + 1]
    
                    idx = (numpy.isfinite(xdata_all)) * (numpy.isfinite(ydata_all))
                    assert idx.all() 
                    if idx.any():
                        plt.plot(xdata_all[idx], ydata_all[idx], colors[i], ls='solid', lw=linewidth, 
                                 # TODO: ls has changed, check whether this works out
                                 clip_on=False)
                
            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            # plot "valid" data, those within maxevals
            idx = np.logical_and(xdata < entry0.mMaxEvals(),
                                 ydata < entry1.mMaxEvals())
            # was:
            #       (numpy.isinf(xdata) == False) *
            #       (numpy.isinf(ydata) == False) *
            #       (xdata < entry0.mMaxEvals()) *
            #       (ydata < entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx], ydata[idx], ls='',
                             markersize=markersize,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3, 
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx], ydata[idx], ls='', markersize=markersize,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             clip_on=False)
                #try:
                #    plt.scatter(xdata[idx], ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            # plot beyond maxevals but finite data
            idx = ((numpy.isinf(xdata) == False) *
                   (numpy.isinf(ydata) == False) *
                   np.logical_or(xdata >= entry0.mMaxEvals(),
                                 ydata >= entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx], ydata[idx], ls='',
                             markersize=markersize + markersize_addon_beyond_maxevals,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=1,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx], ydata[idx], ls='', markersize=markersize,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=2,
                             clip_on=False)
            #ax = plt.gca()
            ax = plt.axes()

            # plot data on the right edge
            idx = numpy.isinf(xdata) * (numpy.isinf(ydata) == False)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(idx), ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.]*numpy.sum(idx), ydata[idx],
                             markersize=markersize + markersize_addon_beyond_maxevals, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=1,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot([1.]*numpy.sum(idx), ydata[idx],
                             markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=2,
                             transform=trans, clip_on=False)
                #set_trace()

            # plot data on the left edge
            idx = (numpy.isinf(xdata)==False) * numpy.isinf(ydata)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[idx], [1.-offset]*numpy.sum(idx),
                             markersize=markersize + markersize_addon_beyond_maxevals, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=1,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx], [1.-offset]*numpy.sum(idx),
                             markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=2,
                             transform=trans, clip_on=False)

            # plot data in the top corner
            idx = numpy.isinf(xdata) * numpy.isinf(ydata)
            if idx.any():
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1.-offset]*numpy.sum(idx), [1.-offset]*numpy.sum(idx),
                             markersize=markersize + markersize_addon_beyond_maxevals, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=1,
                             transform=ax.transAxes, clip_on=False)
                except KeyError:
                    plt.plot([1.-offset]*numpy.sum(idx), [1.-offset]*numpy.sum(idx),
                             markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=2,
                             transform=ax.transAxes, clip_on=False)

        targetlabels = targets.labels()
        if isinstance(targets, pproc.RunlengthBasedTargetValues):
            text = (str(len(targetlabels)) + ' target RLs/dim: ' +
                    targetlabels[0] + '..' +
                    targetlabels[len(targetlabels)-1] + '\n')
            text += '   from ' + testbedsettings.current_testbed.best_algorithm_filename
        else:
            text = (str(len(targetlabels)) + ' targets in ' +
                    targetlabels[0] + '..' +
                    targetlabels[len(targetlabels)-1])
        # add number of instances 
        text += '\n'
        num_of_instances_alg0 = []
        num_of_instances_alg1 = []
        for d in dims:
            num_of_instances_alg0.append((dictDim0[d][0]).nbRuns())
            num_of_instances_alg1.append((dictDim1[d][0]).nbRuns())
        # issue a warning if the numbers of instances are inconsistent:
        if (len(set(num_of_instances_alg0)) > 1):
            warnings.warn('Inconsistent numbers of instances over dimensions found for ALG0:\n\
                           found instances %s' % str(num_of_instances_alg0))
        if (len(set(num_of_instances_alg1)) > 1):
            warnings.warn('Inconsistent numbers of instances over dimensions found for ALG1:\n\
                           found instances %s' % str(num_of_instances_alg1))
        if (len(set(num_of_instances_alg0)) == 1 and len(set(num_of_instances_alg0)) == 1):
            text += '%s and %s instances' % (num_of_instances_alg0[0], num_of_instances_alg1[0])
        else:
            for n in num_of_instances_alg0:
                text += '%d, ' % n
            text = text.rstrip(', ')
            text += ' and '
            for n in num_of_instances_alg1:
                text += '%d, ' % n
            text = text.rstrip(', ')
            text += ' instances'
        plt.text(0.01, 0.98, text, horizontalalignment="left",
                 verticalalignment="top", transform=plt.gca().transAxes, size='small')

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot((entry0.mMaxEvals(), entry0.mMaxEvals()),
                     # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                     (max([minbnd, entry1.mMaxEvals()/max_evals_line_length]), entry1.mMaxEvals()), ls='-', color=colors[i],
                     zorder=-1)
            plt.plot(# (minbnd, entry0.mMaxEvals()),
                     (max([minbnd, entry0.mMaxEvals()/max_evals_line_length]), entry0.mMaxEvals()),
                     (entry1.mMaxEvals(), entry1.mMaxEvals()), ls='-',
                     color=colors[i], zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        fontSize = genericsettings.getFontSize(funInfos.values())
        if f in funInfos.keys():        
            plt.ylabel(funInfos[f], fontsize=fontSize)

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        plt.close()
Esempio n. 7
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure."""

    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            xdata = numpy.array(entry0.detERT(targets))
            ydata = numpy.array(entry1.detERT(targets))

            tmp = (numpy.isinf(xdata)==False) * (numpy.isinf(ydata)==False)
            if tmp.any():
                try:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=markersize,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3)
                except KeyError:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=markersize,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3)
                #try:
                #    plt.scatter(xdata[tmp], ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            #ax = plt.gca()
            ax = plt.axes()

            tmp = numpy.isinf(xdata) * (numpy.isinf(ydata)==False)
            if tmp.any():
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(tmp), ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                #set_trace()

            tmp = (numpy.isinf(xdata)==False) * numpy.isinf(ydata)
            if tmp.any():
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)

            tmp = numpy.isinf(xdata) * numpy.isinf(ydata)
            if tmp.any():
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)
                except KeyError:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)

                #set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot((entry0.mMaxEvals(), entry0.mMaxEvals()),
                     # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                     (max([minbnd, entry1.mMaxEvals()/10.]), entry1.mMaxEvals()), ls='-', color=colors[i],
                     zorder=-1)
            plt.plot(# (minbnd, entry0.mMaxEvals()),
                     (max([minbnd, entry0.mMaxEvals()/10.]), entry0.mMaxEvals()),
                     (entry1.mMaxEvals(), entry1.mMaxEvals()), ls='-',
                     color=colors[i], zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        if isBenchmarkinfosFound:
            try:
                plt.ylabel(funInfos[f])
            except IndexError:
                pass

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        plt.close()
Esempio n. 8
0
def main(dsList0, dsList1, outputdir, settings):
    """Generate a scatter plot figure.
    
    TODO: """

    markers = genericsettings.dim_related_markers
    colors = genericsettings.dim_related_colors
    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    targets = testbedsettings.current_testbed.ppscatter_target_values
    if isinstance(targets, pproc.RunlengthBasedTargetValues):
        linewidth = linewidth_rld_based
    else:
        linewidth = linewidth_default

    funInfos = ppfigparam.read_fun_infos()

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable aRT values as a line
                all_targets = np.array(
                    sorted(set(entry0.target).union(entry1.target),
                           reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(
                    all_targets <= targets((f, d))[0])[0]]  #
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and aRT <= maxevals
                idx = []
                for ari in (np.where(
                        entry0.detSuccessRates(all_targets) >= 0.5)[0],
                            np.where(
                                entry1.detSuccessRates(all_targets) >= 0.5)[0],
                            np.where(xdata_all <= max(entry0.maxevals))[0],
                            np.where(ydata_all <= max(entry1.maxevals))[0]):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1]))
                    xdata_all = xdata_all[:max_idx + 1]
                    ydata_all = ydata_all[:max_idx + 1]

                    idx = (numpy.isfinite(xdata_all)) * (
                        numpy.isfinite(ydata_all))
                    assert idx.all()
                    if idx.any():
                        plt.plot(
                            xdata_all[idx],
                            ydata_all[idx],
                            colors[i],
                            ls='solid',
                            lw=linewidth,
                            # TODO: ls has changed, check whether this works out
                            clip_on=False)

            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            # plot "valid" data, those within maxevals
            idx = np.logical_and(xdata < entry0.mMaxEvals(),
                                 ydata < entry1.mMaxEvals())
            # was:
            #       (numpy.isinf(xdata) == False) *
            #       (numpy.isinf(ydata) == False) *
            #       (xdata < entry0.mMaxEvals()) *
            #       (ydata < entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             clip_on=False)
                #try:
                #    plt.scatter(xdata[idx], ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            # plot beyond maxevals but finite data
            idx = ((numpy.isinf(xdata) == False) *
                   (numpy.isinf(ydata) == False) *
                   np.logical_or(xdata >= entry0.mMaxEvals(),
                                 ydata >= entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             clip_on=False)
            #ax = plt.gca()
            ax = plt.axes()

            # plot data on the right edge
            idx = numpy.isinf(xdata) * (numpy.isinf(ydata) == False)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(idx), ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.] * numpy.sum(idx),
                             ydata[idx],
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot([1.] * numpy.sum(idx),
                             ydata[idx],
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=trans,
                             clip_on=False)
                #set_trace()

            # plot data on the left edge
            idx = (numpy.isinf(xdata) == False) * numpy.isinf(ydata)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[idx], [1. - offset] * numpy.sum(idx),
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx], [1. - offset] * numpy.sum(idx),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=trans,
                             clip_on=False)

            # plot data in the top corner
            idx = numpy.isinf(xdata) * numpy.isinf(ydata)
            if idx.any():
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1. - offset] * numpy.sum(idx),
                             [1. - offset] * numpy.sum(idx),
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=ax.transAxes,
                             clip_on=False)
                except KeyError:
                    plt.plot([1. - offset] * numpy.sum(idx),
                             [1. - offset] * numpy.sum(idx),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=ax.transAxes,
                             clip_on=False)

        targetlabels = targets.labels()
        if isinstance(targets, pproc.RunlengthBasedTargetValues):
            text = (str(len(targetlabels)) + ' target RLs/dim: ' +
                    targetlabels[0] + '..' +
                    targetlabels[len(targetlabels) - 1] + '\n')
            text += '   from ' + testbedsettings.current_testbed.reference_algorithm_filename
        else:
            text = (str(len(targetlabels)) + ' targets in ' + targetlabels[0] +
                    '..' + targetlabels[len(targetlabels) - 1])
        # add number of instances
        text += '\n'
        num_of_instances_alg0 = []
        num_of_instances_alg1 = []
        for d in dims:
            num_of_instances_alg0.append((dictDim0[d][0]).nbRuns())
            num_of_instances_alg1.append((dictDim1[d][0]).nbRuns())
        # issue a warning if the numbers of instances are inconsistent:
        if (len(set(num_of_instances_alg0)) > 1):
            warnings.warn(
                'Inconsistent numbers of instances over dimensions found for ALG0:\n\
                           found instances %s' % str(num_of_instances_alg0))
        if (len(set(num_of_instances_alg1)) > 1):
            warnings.warn(
                'Inconsistent numbers of instances over dimensions found for ALG1:\n\
                           found instances %s' % str(num_of_instances_alg1))
        if (len(set(num_of_instances_alg0)) == 1
                and len(set(num_of_instances_alg0)) == 1):
            text += '%s and %s instances' % (num_of_instances_alg0[0],
                                             num_of_instances_alg1[0])
        else:
            for n in num_of_instances_alg0:
                text += '%d, ' % n
            text = text.rstrip(', ')
            text += ' and '
            for n in num_of_instances_alg1:
                text += '%d, ' % n
            text = text.rstrip(', ')
            text += ' instances'
        plt.text(0.01,
                 0.98,
                 text,
                 horizontalalignment="left",
                 verticalalignment="top",
                 transform=plt.gca().transAxes,
                 size='small')

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot(
                (entry0.mMaxEvals(), entry0.mMaxEvals()),
                # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                (max([minbnd,
                      entry1.mMaxEvals() / max_evals_line_length
                      ]), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.plot(  # (minbnd, entry0.mMaxEvals()),
                (max([minbnd,
                      entry0.mMaxEvals() / max_evals_line_length
                      ]), entry0.mMaxEvals()),
                (entry1.mMaxEvals(), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        fontSize = genericsettings.getFontSize(funInfos.values())
        if f in funInfos.keys():
            plt.ylabel(funInfos[f], fontsize=fontSize)

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        save_figure(filename, dsList0[0].algId)
        plt.close()
Esempio n. 9
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure.
    
    TODO: """

    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable ERT values as a line
                all_targets = np.array(sorted(set(entry0.target).union(entry1.target), reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(all_targets <= targets((f, d))[0])[0]]  # 
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and ERT <= maxevals
                idx = []
                for ari in (np.where(entry0.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(entry1.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(xdata_all <= max(entry0.maxevals))[0], 
                         np.where(ydata_all <= max(entry1.maxevals))[0]
                        ):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1])) 
                    xdata_all = xdata_all[:max_idx + 1]
                    ydata_all = ydata_all[:max_idx + 1]
    
                    idx = (numpy.isfinite(xdata_all)) * (numpy.isfinite(ydata_all))
                    assert idx.all() 
                    if idx.any():
                        plt.plot(xdata_all[idx], ydata_all[idx], colors[i], ls='solid', lw=linewidth, 
                                 # TODO: ls has changed, check whether this works out
                                 clip_on=False)
                
            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            tmp = (numpy.isinf(xdata)==False) * (numpy.isinf(ydata)==False)
            if tmp.any():
                try:
                    plt.plot(xdata[tmp], ydata[tmp], ls='',
                             markersize=markersize,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3, 
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=markersize,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3, 
                             clip_on=False)
                #try:
                #    plt.scatter(xdata[tmp], ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            #ax = plt.gca()
            ax = plt.axes()

            tmp = numpy.isinf(xdata) * (numpy.isinf(ydata)==False)
            if tmp.any():
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(tmp), ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                #set_trace()

            tmp = (numpy.isinf(xdata)==False) * numpy.isinf(ydata)
            if tmp.any():
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)

            tmp = numpy.isinf(xdata) * numpy.isinf(ydata)
            if tmp.any():
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)
                except KeyError:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)

                #set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot((entry0.mMaxEvals(), entry0.mMaxEvals()),
                     # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                     (max([minbnd, entry1.mMaxEvals()/max_evals_line_length]), entry1.mMaxEvals()), ls='-', color=colors[i],
                     zorder=-1)
            plt.plot(# (minbnd, entry0.mMaxEvals()),
                     (max([minbnd, entry0.mMaxEvals()/max_evals_line_length]), entry0.mMaxEvals()),
                     (entry1.mMaxEvals(), entry1.mMaxEvals()), ls='-',
                     color=colors[i], zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        try:
            plt.ylabel(funInfos[f])
        except IndexError:
            pass

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        plt.close()
Esempio n. 10
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure.
    
    TODO: """

    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    if isinstance(targets, pproc.RunlengthBasedTargetValues):
        linewidth = linewidth_rld_based
    else:
        linewidth = linewidth_default

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable ERT values as a line
                all_targets = np.array(
                    sorted(set(entry0.target).union(entry1.target),
                           reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(
                    all_targets <= targets((f, d))[0])[0]]  #
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and ERT <= maxevals
                idx = []
                for ari in (np.where(
                        entry0.detSuccessRates(all_targets) >= 0.5)[0],
                            np.where(
                                entry1.detSuccessRates(all_targets) >= 0.5)[0],
                            np.where(xdata_all <= max(entry0.maxevals))[0],
                            np.where(ydata_all <= max(entry1.maxevals))[0]):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1]))
                    xdata_all = xdata_all[:max_idx + 1]
                    ydata_all = ydata_all[:max_idx + 1]

                    idx = (numpy.isfinite(xdata_all)) * (
                        numpy.isfinite(ydata_all))
                    assert idx.all()
                    if idx.any():
                        plt.plot(
                            xdata_all[idx],
                            ydata_all[idx],
                            colors[i],
                            ls='solid',
                            lw=linewidth,
                            # TODO: ls has changed, check whether this works out
                            clip_on=False)

            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            # plot "valid" data, those within maxevals
            idx = np.logical_and(xdata < entry0.mMaxEvals(),
                                 ydata < entry1.mMaxEvals())
            # was:
            #       (numpy.isinf(xdata) == False) *
            #       (numpy.isinf(ydata) == False) *
            #       (xdata < entry0.mMaxEvals()) *
            #       (ydata < entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             clip_on=False)
                #try:
                #    plt.scatter(xdata[idx], ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            # plot beyond maxevals but finite data
            idx = ((numpy.isinf(xdata) == False) *
                   (numpy.isinf(ydata) == False) *
                   np.logical_or(xdata >= entry0.mMaxEvals(),
                                 ydata >= entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx],
                             ydata[idx],
                             ls='',
                             markersize=markersize,
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             clip_on=False)
            #ax = plt.gca()
            ax = plt.axes()

            # plot data on the right edge
            idx = numpy.isinf(xdata) * (numpy.isinf(ydata) == False)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(idx), ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.] * numpy.sum(idx),
                             ydata[idx],
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot([1.] * numpy.sum(idx),
                             ydata[idx],
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=trans,
                             clip_on=False)
                #set_trace()

            # plot data on the left edge
            idx = (numpy.isinf(xdata) == False) * numpy.isinf(ydata)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[idx], [1. - offset] * numpy.sum(idx),
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[idx], [1. - offset] * numpy.sum(idx),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=trans,
                             clip_on=False)

            # plot data in the top corner
            idx = numpy.isinf(xdata) * numpy.isinf(ydata)
            if idx.any():
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1. - offset] * numpy.sum(idx),
                             [1. - offset] * numpy.sum(idx),
                             markersize=markersize +
                             markersize_addon_beyond_maxevals,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=1,
                             transform=ax.transAxes,
                             clip_on=False)
                except KeyError:
                    plt.plot([1. - offset] * numpy.sum(idx),
                             [1. - offset] * numpy.sum(idx),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=2,
                             transform=ax.transAxes,
                             clip_on=False)

                #set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot(
                (entry0.mMaxEvals(), entry0.mMaxEvals()),
                # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                (max([minbnd,
                      entry1.mMaxEvals() / max_evals_line_length
                      ]), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.plot(  # (minbnd, entry0.mMaxEvals()),
                (max([minbnd,
                      entry0.mMaxEvals() / max_evals_line_length
                      ]), entry0.mMaxEvals()),
                (entry1.mMaxEvals(), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        try:
            plt.ylabel(funInfos[f])
        except IndexError:
            pass

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        if f == 1:
            algName1 = toolsdivers.str_to_latex(
                toolsdivers.strip_pathname1(entry1.algId))
            algName0 = toolsdivers.str_to_latex(
                toolsdivers.strip_pathname1(entry0.algId))
            save_single_functions_html(os.path.join(
                outputdir, genericsettings.two_algorithm_file_name),
                                       "%s vs %s" % (algName1, algName0),
                                       algorithmCount=AlgorithmCount.TWO)
        plt.close()
def main(dsList0, dsList1, outputdir, verbose=True):

    plt.rc("axes", labelsize=20, titlesize=24)
    plt.rc("xtick", labelsize=20)
    plt.rc("ytick", labelsize=20)
    plt.rc("font", size=20)
    plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    nbmarkers = 46
    inc = 45./(nbmarkers-1)
    targets = numpy.power(10, numpy.arange(-40, 5 + inc, inc)/5.)

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate((2, 3, 5, 10, 20, 40)):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            xdata = numpy.array(entry0.detERT(targets))
            ydata = numpy.array(entry1.detERT(targets))
            #targets, xdata, ydata = generateData(entry0, entry1)

            #plt.plot(xdata, ydata, ls='', color=colors[i], marker=markers[i],
            #         markerfacecolor='None', markeredgecolor=colors[i],
            #         markersize=10, markeredgewidth=3)
            tmp = (numpy.isinf(xdata)==False) * (numpy.isinf(ydata)==False)
            if tmp.any():
                try:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=10,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3)
                except KeyError:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=10,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3)
                #try:
                #    plt.scatter(xdata[tmp], ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            #ax = plt.gca()
            ax = plt.axes()

            tmp = numpy.isinf(xdata) * (numpy.isinf(ydata)==False)
            if tmp.any():
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(tmp), ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=10, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=10, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                #set_trace()

            tmp = (numpy.isinf(xdata)==False) * numpy.isinf(ydata)
            if tmp.any():
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=10, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=10, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)

            tmp = numpy.isinf(xdata) * numpy.isinf(ydata)
            if tmp.any():
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=10, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)
                except KeyError:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=10, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)

                #set_trace()

        beautify()

        #if isBenchmarkinfosFound:
        #    plt.ylabel(funInfos[f])

        filename = os.path.join(outputdir, 'scatter_f%d' % f)
        saveFigure(filename, figFormat=figFormat, verbose=verbose)
        plt.close()

    plt.rcdefaults()
Esempio n. 12
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure.
    
    TODO: """

    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable ERT values as a line
                all_targets = np.array(sorted(set(entry0.target).union(entry1.target), reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(all_targets <= targets((f, d))[0])[0]]  # 
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and ERT <= maxevals
                idx = []
                for ari in (np.where(entry0.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(entry1.detSuccessRates(all_targets) >= 0.5)[0], 
                         np.where(xdata_all <= max(entry0.maxevals))[0], 
                         np.where(ydata_all <= max(entry1.maxevals))[0]
                        ):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1])) 
                    xdata_all = xdata_all[:max_idx + 1]
                    ydata_all = ydata_all[:max_idx + 1]
    
                    idx = (numpy.isfinite(xdata_all)) * (numpy.isfinite(ydata_all))
                    assert idx.all() 
                    if idx.any():
                        plt.plot(xdata_all[idx], ydata_all[idx], colors[i], ls='solid', lw=linewidth, 
                                 # TODO: ls has changed, check whether this works out
                                 clip_on=False)
                
            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            tmp = (numpy.isinf(xdata)==False) * (numpy.isinf(ydata)==False)
            if tmp.any():
                try:
                    plt.plot(xdata[tmp], ydata[tmp], ls='',
                             markersize=markersize,
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3, 
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], ydata[tmp], ls='', markersize=markersize,
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3, 
                             clip_on=False)
                #try:
                #    plt.scatter(xdata[tmp], ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            #ax = plt.gca()
            ax = plt.axes()

            tmp = numpy.isinf(xdata) * (numpy.isinf(ydata)==False)
            if tmp.any():
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(tmp), ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot([1.]*numpy.sum(tmp), ydata[tmp], markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                #set_trace()

            tmp = (numpy.isinf(xdata)==False) * numpy.isinf(ydata)
            if tmp.any():
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=trans, clip_on=False)

            tmp = numpy.isinf(xdata) * numpy.isinf(ydata)
            if tmp.any():
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker=markers[i], markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)
                except KeyError:
                    plt.plot([1.-offset]*numpy.sum(tmp), [1.-offset]*numpy.sum(tmp), markersize=markersize, ls='',
                             marker='x', markerfacecolor='None',
                             markeredgecolor=colors[i], markeredgewidth=3,
                             transform=ax.transAxes, clip_on=False)

                #set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0] # should be only one element
                entry1 = dictDim1[d][0] # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot((entry0.mMaxEvals(), entry0.mMaxEvals()),
                     # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                     (max([minbnd, entry1.mMaxEvals()/max_evals_line_length]), entry1.mMaxEvals()), ls='-', color=colors[i],
                     zorder=-1)
            plt.plot(# (minbnd, entry0.mMaxEvals()),
                     (max([minbnd, entry0.mMaxEvals()/max_evals_line_length]), entry0.mMaxEvals()),
                     (entry1.mMaxEvals(), entry1.mMaxEvals()), ls='-',
                     color=colors[i], zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        try:
            plt.ylabel(funInfos[f])
        except IndexError:
            pass

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        plt.close()
Esempio n. 13
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure.
    
    TODO: """

    # plt.rc("axes", labelsize=24, titlesize=24)
    # plt.rc("xtick", labelsize=20)
    # plt.rc("ytick", labelsize=20)
    # plt.rc("font", size=20)
    # plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    if isinstance(targets, pproc.RunlengthBasedTargetValues):
        linewidth = linewidth_rld_based
    else:
        linewidth = linewidth_default

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        # set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue
            if linewidth:  # plot all reliable ERT values as a line
                all_targets = np.array(sorted(set(entry0.target).union(entry1.target), reverse=True))
                assert entry0.detSuccessRates([all_targets[0]]) == 1.0
                assert entry1.detSuccessRates([all_targets[0]]) == 1.0
                all_targets = all_targets[np.where(all_targets <= targets((f, d))[0])[0]]  #
                xdata_all = np.array(entry0.detERT(all_targets))
                ydata_all = np.array(entry1.detERT(all_targets))
                # idx of reliable targets: last index where success rate >= 1/2 and ERT <= maxevals
                idx = []
                for ari in (
                    np.where(entry0.detSuccessRates(all_targets) >= 0.5)[0],
                    np.where(entry1.detSuccessRates(all_targets) >= 0.5)[0],
                    np.where(xdata_all <= max(entry0.maxevals))[0],
                    np.where(ydata_all <= max(entry1.maxevals))[0],
                ):
                    if len(ari):
                        idx.append(ari[-1])
                if len(idx) == 4:
                    max_idx = min(idx)
                    ## at least up to the most difficult given target
                    ## idx = max((idx, np.where(all_targets >= targets((f, d))[-1])[0][-1]))
                    xdata_all = xdata_all[: max_idx + 1]
                    ydata_all = ydata_all[: max_idx + 1]

                    idx = (numpy.isfinite(xdata_all)) * (numpy.isfinite(ydata_all))
                    assert idx.all()
                    if idx.any():
                        plt.plot(
                            xdata_all[idx],
                            ydata_all[idx],
                            colors[i],
                            ls="solid",
                            lw=linewidth,
                            # TODO: ls has changed, check whether this works out
                            clip_on=False,
                        )

            xdata = numpy.array(entry0.detERT(targets((f, d))))
            ydata = numpy.array(entry1.detERT(targets((f, d))))

            # plot "valid" data, those within maxevals
            idx = np.logical_and(xdata < entry0.mMaxEvals(), ydata < entry1.mMaxEvals())
            # was:
            #       (numpy.isinf(xdata) == False) *
            #       (numpy.isinf(ydata) == False) *
            #       (xdata < entry0.mMaxEvals()) *
            #       (ydata < entry1.mMaxEvals()))
            if idx.any():
                try:
                    plt.plot(
                        xdata[idx],
                        ydata[idx],
                        ls="",
                        markersize=markersize,
                        marker=markers[i],
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=3,
                        clip_on=False,
                    )
                except KeyError:
                    plt.plot(
                        xdata[idx],
                        ydata[idx],
                        ls="",
                        markersize=markersize,
                        marker="x",
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=3,
                        clip_on=False,
                    )
                # try:
                #    plt.scatter(xdata[idx], ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                # except ValueError:
                #    set_trace()

            # plot beyond maxevals but finite data
            idx = (
                (numpy.isinf(xdata) == False)
                * (numpy.isinf(ydata) == False)
                * np.logical_or(xdata >= entry0.mMaxEvals(), ydata >= entry1.mMaxEvals())
            )
            if idx.any():
                try:
                    plt.plot(
                        xdata[idx],
                        ydata[idx],
                        ls="",
                        markersize=markersize + markersize_addon_beyond_maxevals,
                        marker=markers[i],
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=1,
                        clip_on=False,
                    )
                except KeyError:
                    plt.plot(
                        xdata[idx],
                        ydata[idx],
                        ls="",
                        markersize=markersize,
                        marker="x",
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=2,
                        clip_on=False,
                    )
            # ax = plt.gca()
            ax = plt.axes()

            # plot data on the right edge
            idx = numpy.isinf(xdata) * (numpy.isinf(ydata) == False)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transAxes, ax.transData)
                # plt.scatter([1.]*numpy.sum(idx), ydata[idx], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot(
                        [1.0] * numpy.sum(idx),
                        ydata[idx],
                        markersize=markersize + markersize_addon_beyond_maxevals,
                        ls="",
                        marker=markers[i],
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=1,
                        transform=trans,
                        clip_on=False,
                    )
                except KeyError:
                    plt.plot(
                        [1.0] * numpy.sum(idx),
                        ydata[idx],
                        markersize=markersize,
                        ls="",
                        marker="x",
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=2,
                        transform=trans,
                        clip_on=False,
                    )
                # set_trace()

            # plot data on the left edge
            idx = (numpy.isinf(xdata) == False) * numpy.isinf(ydata)
            if idx.any():
                # This (seems to) transform inf to the figure limits!?
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(
                        xdata[idx],
                        [1.0 - offset] * numpy.sum(idx),
                        markersize=markersize + markersize_addon_beyond_maxevals,
                        ls="",
                        marker=markers[i],
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=1,
                        transform=trans,
                        clip_on=False,
                    )
                except KeyError:
                    plt.plot(
                        xdata[idx],
                        [1.0 - offset] * numpy.sum(idx),
                        markersize=markersize,
                        ls="",
                        marker="x",
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=2,
                        transform=trans,
                        clip_on=False,
                    )

            # plot data in the top corner
            idx = numpy.isinf(xdata) * numpy.isinf(ydata)
            if idx.any():
                #    plt.scatter(xdata[idx], [1.-offset]*numpy.sum(idx), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(
                        [1.0 - offset] * numpy.sum(idx),
                        [1.0 - offset] * numpy.sum(idx),
                        markersize=markersize + markersize_addon_beyond_maxevals,
                        ls="",
                        marker=markers[i],
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=1,
                        transform=ax.transAxes,
                        clip_on=False,
                    )
                except KeyError:
                    plt.plot(
                        [1.0 - offset] * numpy.sum(idx),
                        [1.0 - offset] * numpy.sum(idx),
                        markersize=markersize,
                        ls="",
                        marker="x",
                        markerfacecolor="None",
                        markeredgecolor=colors[i],
                        markeredgewidth=2,
                        transform=ax.transAxes,
                        clip_on=False,
                    )

                # set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot(
                (entry0.mMaxEvals(), entry0.mMaxEvals()),
                # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                (max([minbnd, entry1.mMaxEvals() / max_evals_line_length]), entry1.mMaxEvals()),
                ls="-",
                color=colors[i],
                zorder=-1,
            )
            plt.plot(  # (minbnd, entry0.mMaxEvals()),
                (max([minbnd, entry0.mMaxEvals() / max_evals_line_length]), entry0.mMaxEvals()),
                (entry1.mMaxEvals(), entry1.mMaxEvals()),
                ls="-",
                color=colors[i],
                zorder=-1,
            )
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            # Set the boundaries again: they changed due to new plots.

            # plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            # plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        try:
            plt.ylabel(funInfos[f])
        except IndexError:
            pass

        filename = os.path.join(outputdir, "ppscatter_f%03d" % f)
        saveFigure(filename, verbose=verbose)
        if f == 1:
            algName1 = toolsdivers.str_to_latex(toolsdivers.strip_pathname1(entry1.algId))
            algName0 = toolsdivers.str_to_latex(toolsdivers.strip_pathname1(entry0.algId))
            save_single_functions_html(
                os.path.join(outputdir, genericsettings.two_algorithm_file_name),
                "%s vs %s" % (algName1, algName0),
                algorithmCount=AlgorithmCount.TWO,
            )
        plt.close()
Esempio n. 14
0
def generate_line_plot(
    # data parameters
    data_dir=data_dir,
    data_file=data_file,
    read_csv_kw={},
    strike_zone_data_file=None,
    x_column=0,
    y_column=-1,
    x_minus_yvalues=None,
    yerr_columns=None,
    year=year,
    first_day=None,
    last_day=334,
    hline_ypos=None,

    # figure parameters
    width=width,
    height_to_width=height_to_width,
    width_pixels_save=width_pixels_save,
    axes_box=axes_box,

    # text parameters
    title_txt='',
    ylab_txt='',
    ylab_rotation=0,
    hline_labels=None,
    hline_label_units='',
    presidential_2020=False,
    hline_lab_xpos=0.85,
    watermark=watermark,
    watermark_pos=watermark_pos,

    # size parameters
    size_scale=size_scale,
    font_size=font_size,
    circle_size=3,

    # thickness parameters
    data_lw=1,
    grid_lw=0.25,
    spine_lw=0.25,
    hline_lw=1,
    tick_lw=0.25,

    # color parameters
    col_D=col_D,
    col_R=col_R,
    zone_colors=zone_colors,
    shading=True,
    color_reverse=False,
    shading_kw=dict(zorder=0, alpha=0.15, lw=0),
    yerr_kw=dict(color='grey', alpha=0.3, lw=0),
    hline_color='dimgrey',
    hline_lab_colors=None,
    data_line_color='k',
    yticklab_format=False,
    grid_alpha=0.5,
    strike_colors=['dimgrey', 'lightgrey'],

    # axes parameters
    ylim=None,

    # tick parameters
    tick_length=3,
    tick_pad=1,
    yticks_interval=1,

    # distance/pad parameters
    title_pad=3,
    ylab_pad=0.1,
    xtick_pad=0.015,
    hline_lab_pad=0.06,

    # output parameters
    out_path=out_path,
    out_format=out_format,

    # other customs
    plot_customization=False,
    thumbnail=False,
):

    # load data
    data_path = os.path.join(data_dir, data_file)
    data = pd.read_csv(data_path, **read_csv_kw)
    #data.drop_duplicates(x_column, inplace=True)

    # read in column labels
    fname, ext = os.path.splitext(data_file)
    column_file = fname + '_columns' + ext
    column_path = os.path.join(data_dir, column_file)
    columns = pd.read_csv(column_path).columns
    data.columns = columns
    data.sort_values(by=x_column, inplace=True)

    # convert dates to datetimes
    days = data[x_column].values
    day_of_year = days
    days = np.array([doy2dt(int(d)) for d in days])

    # specify data to plot
    vals = data[y_column].values
    if x_minus_yvalues is not None:
        vals = x_minus_yvalues - vals

    # use data to fill title text
    title_fillers = dict(
        last_value=vals[-1],
        party='',
    )

    # change hline_labels for thumnail versions, set fontsizes
    # for hline labels
    hlabel_font_size = font_size
    if thumbnail and hline_label_units == "given":
        font_size = font_size * 1.25
        hlabel_font_size = font_size * 1.5
    elif thumbnail and not plot_customization and hline_labels is not None:
        font_size = font_size * 1.25
        hlabel_font_size = font_size * 1.5
        rounded_last_value = round(float(title_fillers['last_value']), 1)
        if presidential_2020:
            if rounded_last_value > 0:
                hline_labels[1] = "Biden +" + str(
                    rounded_last_value) + hline_label_units
            else:
                hline_labels[1] = "Trump +" + str(
                    rounded_last_value) + hline_label_units

        elif rounded_last_value > 0:
            if hline_label_units == "seats":
                hline_labels[1] = "D+" + str(round(
                    rounded_last_value / 10, 1)) + " " + hline_label_units
            else:
                hline_labels[1] = "D+" + str(
                    rounded_last_value) + hline_label_units
        elif rounded_last_value < 0:
            if hline_label_units == "seats":
                hline_labels[1] = "R+" + str(round(
                    rounded_last_value / 10, 1)) + " " + hline_label_units
            else:
                hline_labels[1] = "Current R+" + str(
                    rounded_last_value) + hline_label_units
        else:
            hline_labels[1] = "Currently Tie"
        hline_labels[0] = ""

    # prepare figurex
    fig = pl.figure(figsize=(width, width * height_to_width))
    ax = fig.add_axes(axes_box)

    # plot main data line
    ax.plot(days,
            vals,
            color=data_line_color,
            lw=data_lw * size_scale,
            zorder=1000)

    # plot circle on most recent data point
    col = zone_colors[1] if vals[-1] > 0 else zone_colors[0]
    ax.plot(days[-1],
            vals[-1],
            marker='o',
            markersize=circle_size * size_scale,
            markerfacecolor=col,
            markeredgewidth=0,
            zorder=1001)

    # plot error bars
    err = None
    if yerr_columns is not None:
        err = data[yerr_columns].values
        if len(yerr_columns) == 1:
            # then column with symmetrical value to be added
            # otherwise abs values were assumed
            err = err.squeeze()
            err = np.array([vals - err, vals + err]).T
        if x_minus_yvalues is not None:
            err = x_minus_yvalues - err
            err = err[:, ::-1]  # keep [lower, upper]
        ax.fill_between(
            days,
            err[:, 0],
            err[:, 1],
            **yerr_kw,
        )

    # x axis limits
    first_day = first_day or np.min(day_of_year)
    last_day = last_day or np.max(day_of_year)
    first_date = doy2dt(first_day)
    last_date = doy2dt(last_day)
    ax.set_xlim(first_date, last_date)

    # y axis limits
    if ylim is None:
        mindat = err[:, 0] if err is not None else vals
        maxdat = err[:, 1] if err is not None else vals
        minn = np.floor(np.min(mindat))
        maxx = np.ceil(np.max(maxdat))
        pad = np.round((maxx - minn) / 5)
        y0, y1 = minn - pad, maxx + pad
        if pad > 10:

            def r210(val, fx=np.ceil):
                return int(fx(val / 10.0)) * 10

            y0 = r210(y0, np.ceil)
            y1 = r210(y1, np.floor)
        ylim = (y0, y1)
    ax.set_ylim(ylim)
    ylim = ax.get_ylim()

    # x ticks at month boundaries
    date_range = pd.date_range(first_date, last_date)
    tick_dates = date_range[date_range.day == 1]
    tick_dates_dt = np.array([td.to_datetime64() for td in tick_dates])
    ax.set_xticks(tick_dates_dt)
    ax.set_xticklabels([])

    # x tick labels at centers of months
    def get_mid(mo):
        rang = calendar.monthrange(year, mo)
        return int(np.round(rang[-1] / 2))

    if not thumbnail:
        mid_months = np.array([get_mid(d.month) for d in date_range])
        tick_label_dates = date_range[date_range.day == mid_months]
        for td in tick_label_dates:
            loc = td.to_datetime64()
            ax.text(loc,
                    -xtick_pad * size_scale,
                    mo_names[td.month],
                    color='k',
                    ha='center',
                    va='center',
                    fontsize=font_size,
                    transform=blend(ax.transData, ax.transAxes))

    # y ticks at integer values
    ax.set_yticks(
        np.arange(ylim[0], ylim[1] + yticks_interval, yticks_interval))

    if yticklab_format:
        # ytick labels with formatting specifications
        ytls = []
        for t in ax.get_yticks():
            plus = '+' if t != 0 else ''
            ytls.append(f'{plus}{abs(t):0.0f}%')
        ytls[0] = f'R {ytls[0]}'
        ytls[-1] = f'D {ytls[-1]}'
        ax.set_yticklabels(ytls)

        # color ytick labels
        for lab, pos in zip(ax.get_yticklabels(), ax.get_yticks()):
            if pos > 0:
                col = zone_colors[1]
            elif pos < 0:
                col = zone_colors[0]
            else:
                col = 'k'
            lab.set_color(col)

    # tick parameters
    ax.tick_params(labelsize=font_size,
                   length=tick_length * size_scale,
                   pad=tick_pad * size_scale,
                   width=tick_lw * size_scale)
    ax.tick_params(which='minor', length=0)

    # grid and spines
    ax.grid('on', linewidth=grid_lw * size_scale, alpha=grid_alpha)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    pl.setp(ax.spines.values(), linewidth=spine_lw * size_scale)

    # colors for hline labels and shading
    col0, col1 = zone_colors
    if color_reverse:
        col0, col1 = col1, col0

    # horizontal line
    if hline_ypos is not None:
        ax.axhline(hline_ypos, color=hline_color, lw=hline_lw * size_scale)

        #***************
        if hline_labels is not None:
            pad_data_units = hline_lab_pad * np.diff(ax.get_ylim())
            txt_kw = dict(fontsize=hlabel_font_size,
                          ha='center',
                          va='center',
                          transform=blend(ax.transAxes, ax.transData))
            if hline_lab_colors is None:
                hline_lab_colors = [col0, col1]
            ax.text(
                hline_lab_xpos,
                hline_ypos - pad_data_units,
                hline_labels[0].format(**title_fillers),
                color=hline_lab_colors[0],
                **txt_kw,
            )
            ax.text(
                hline_lab_xpos,
                hline_ypos + pad_data_units,
                hline_labels[1].format(**title_fillers),
                color=hline_lab_colors[1],
                **txt_kw,
            )

    # background shading of plot regions
    if shading:
        ax.axhspan(ax.get_ylim()[0], hline_ypos, color=col0, **shading_kw)
        ax.axhspan(hline_ypos, ax.get_ylim()[1], color=col1, **shading_kw)

    # target bar
    if strike_zone_data_file is not None:
        strike_path = os.path.join(data_dir, strike_zone_data_file)
        strike_data = pd.read_csv(strike_path)
        values = np.array(strike_data.columns).astype(float)
        if x_minus_yvalues is not None:
            values = x_minus_yvalues - values
        s1_lo, s1_hi, s2_lo, s2_hi = values
        pairs = [s1_lo, s1_hi], [s2_lo, s2_hi]
        zorders = [150, 149]

        for (lo, hi), col, zo in zip(pairs, strike_colors, zorders):
            ax.plot([election_day] * 2, [lo, hi],
                    lw=2 * size_scale,
                    color=col,
                    zorder=zo,
                    alpha=0.7,
                    transform=blend(ax.transData, ax.transData))

    # text labels
    if thumbnail:
        ax.set_title(title_txt.format(**title_fillers),
                     y=0.9,
                     fontsize=font_size,
                     weight='bold')
    else:
        ax.set_title(title_txt.format(**title_fillers),
                     pad=title_pad * size_scale,
                     fontsize=font_size,
                     weight='bold')

    ax.text(-ylab_pad * size_scale,
            0.5,
            ylab_txt,
            fontsize=font_size,
            rotation=ylab_rotation,
            ha='center',
            va='center',
            transform=ax.transAxes)

    # watermark
    if not thumbnail:
        ax.text(watermark_pos[0],
                watermark_pos[1],
                watermark,
                fontsize=font_size - 4,
                color='black',
                alpha=0.3,
                style='italic',
                ha='center',
                va='center',
                zorder=200,
                transform=ax.transAxes)

    # CUSTOM type special plot
    if plot_customization:
        new_ax_offset = 3

        ax2 = ax.twinx()

        ax.spines['right'].set_visible(True)
        ax.yaxis.tick_right()
        ax2.yaxis.tick_left()

        spec_ypos = 6
        spec_span = 3
        spec_col = '#f58025'
        ax.axhline(spec_ypos, color=spec_col, lw=0.5 * size_scale)
        ax.axhspan(
            spec_ypos - spec_span,
            spec_ypos + spec_span,
            color=spec_col,
            alpha=0.15,
            zorder=0,
            lw=0,
        )

        pad_data_units = 0.05 * np.diff(ax.get_ylim())
        ax.text(0.79,
                spec_ypos - pad_data_units,
                'Special elections',
                color=spec_col,
                fontsize=font_size - 4,
                alpha=0.8,
                ha='center',
                va='center',
                transform=blend(ax.transAxes, ax.transData))

        ax2.set_ylim(ax.get_ylim())
        ax2.set_yticks(ax.get_yticks())
        ax2.tick_params(labelsize=font_size,
                        length=tick_length * size_scale,
                        pad=tick_pad * size_scale,
                        width=tick_lw * size_scale)
        ax2.tick_params(which='minor', length=0)

        # ytick labels with formatting specifications
        ytls, ytls2 = [], []

        def make_str(t):
            plus = '+' if t != 0 else ''
            let = 'D' if t > 0 else 'R'
            if t == 0: let = ''
            return f'{let}{plus}{abs(t):0.0f}%'

        for t in ax.get_yticks():
            ytls.append(make_str(t))
            ytls2.append(make_str(t - new_ax_offset))
        if not thumbnail:
            ax.set_yticklabels(ytls)
            ax2.set_yticklabels(ytls2)

            ax.text(1 + ylab_pad * size_scale,
                    0.5,
                    '2020 generic ballot D-R',
                    fontsize=font_size,
                    rotation=-ylab_rotation,
                    ha='center',
                    va='center',
                    transform=ax.transAxes)

        lv = title_fillers['last_value']
        lv = lv - new_ax_offset
        if lv > 0:
            title_fillers['party'] = 'D+'
        elif lv < 0:
            title_fillers['party'] = 'R+'
        title_fillers['last_value'] = lv
        if not thumbnail:
            ax.set_title(title_txt.format(**title_fillers),
                         pad=title_pad * size_scale,
                         fontsize=font_size,
                         weight='bold')
        else:
            ax.set_title(title_txt.format(**title_fillers),
                         y=0.9,
                         fontsize=font_size,
                         weight='bold')

        if thumbnail:
            ax2.get_xaxis().set_visible(False)
            ax2.get_yaxis().set_visible(False)

    if thumbnail:
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

    # save out figure
    dpi = width_pixels_save / width
    if not out_path.endswith(out_format):
        out_path = f'{out_path}.{out_format}'
    if thumbnail:
        pl.savefig(out_path, dpi=dpi, bbox_inches='tight')
    else:
        pl.savefig(out_path, dpi=dpi)
    pl.close(fig)
Esempio n. 15
0
def generate_histogram(
    # data parameters
    data_dir=data_dir,
    data_file=data_file,
    vline_xpos=None,
    xvals=None,
    data_factor=100.0,

    # figure parameters
    width=width,
    height_to_width=height_to_width,
    width_pixels_save=width_pixels_save,
    axes_box=axes_box,

    # text parameters
    title_txt='',
    ylab_txt='',
    ylab_rotation=0,
    xlab_txt='',
    vline_labels=None,
    vline_lab_ypos=0.92,
    last_date_label_pos=(0.01, 0.01),
    watermark=watermark,
    watermark_pos=watermark_pos,

    # size parameters
    size_scale=size_scale,
    font_size=font_size,

    # thickness parameters
    bar_width=0.8,
    grid_lw=0.25,
    spine_lw=0.25,
    vline_lw=1,
    tick_lw=0.25,

    # color parameters
    bar_color='black',
    col_D='#1660CE',
    col_R='#C62535',
    zone_colors=zone_colors,
    vline_color='dimgrey',
    grid_alpha=0.5,
    shading=True,
    color_reverse=False,
    shading_kw=dict(zorder=0, alpha=0.15, lw=0),

    # axes parameters
    ylim=(0, 1.15),
    xlim=(0, 100),

    # tick parameters
    tick_length=3,
    tick_pad=1,
    xticks_interval=1,
    yticks_interval=1,

    # distance/pad parameters
    title_pad=3,
    ylab_pad=6,
    xlab_pad=3,
    xtick_pad=0.015,
    vline_lab_pad=0.09,
    vline_font_size=font_size,

    # output parameters
    out_path=out_path,
    out_format=out_format,

    # generate a thumnail image
    thumbnail=False,
):

    # load data
    data_path = os.path.join(data_dir, data_file)
    data = pd.read_csv(data_path)
    data = data.values.squeeze()
    data = data * data_factor
    if xvals is None:
        xvals = np.arange(len(data))

    if thumbnail:
        font_size = font_size * 1.25
        vline_font_size = font_size * 1.25

    # use data to fill title text
    def compute_rv_median(p, xs):
        p = p / np.sum(p)
        cum = np.cumsum(p)
        #x1 = xs[np.argwhere(cum <= 0.5).squeeze()[-1]]
        #x2 = xs[np.argwhere(cum >= 0.5).squeeze()[0]]
        #return np.mean([x1, x2])
        return xs[np.argwhere(cum >= 0.5).squeeze()[0]]

    title_fillers = dict(
        mean_value=np.sum(data * xvals) / np.sum(data),
        median_value=compute_rv_median(data, xvals),
    )

    # prepare figure
    fig = pl.figure(figsize=(width, width * height_to_width))
    ax = fig.add_axes(axes_box)

    # plot main data line
    ax.bar(xvals, data, color=bar_color, width=bar_width, zorder=100)

    # x axis limits
    if xlim is None:
        thresh = 0.0001  # cuts of tails below this y value
        cs = np.cumsum(data)
        try:
            x0 = np.argwhere(cs < thresh).squeeze()[-1]
        except IndexError:
            x0 = 0
        cs_r = np.cumsum(data[::-1])
        try:
            x1 = len(data) - np.argwhere(cs_r < thresh).squeeze()[-1]
        except IndexError:
            x1 = len(data) - 1
        xlim = (xvals[x0], xvals[x1])
    ax.set_xlim(xlim)
    xlim = ax.get_xlim()

    # y axis limits
    ylim = [ylim[0], np.max(data) * ylim[1]]
    ax.set_ylim(ylim)
    ylim = ax.get_ylim()

    # x ticks
    ax.set_xticks(np.arange(xlim[0], xlim[1], xticks_interval))

    # x tick labels

    # y ticks
    ax.set_yticks(
        np.arange(ylim[0], ylim[1] + yticks_interval, yticks_interval))

    # tick parameters
    ax.tick_params(labelsize=font_size,
                   length=tick_length * size_scale,
                   pad=tick_pad * size_scale,
                   width=tick_lw * size_scale)
    ax.tick_params(which='minor', length=0)

    # grid and spines
    ax.grid('on', linewidth=grid_lw * size_scale, alpha=grid_alpha)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    pl.setp(ax.spines.values(), linewidth=spine_lw * size_scale)

    # colors for vline labels and shading
    col0, col1 = zone_colors
    if color_reverse:
        col0, col1 = col1, col0

    # vertical line
    if vline_xpos is not None:
        ax.axvline(
            vline_xpos,  #+ (bar_width / 2 + (1-bar_width) / 2),
            color=vline_color,
            lw=vline_lw * size_scale,
            zorder=100)

        if vline_labels is not None:
            pad_data_units = vline_lab_pad * np.diff(ax.get_xlim())
            txt_kw = dict(fontsize=vline_font_size,
                          ha='center',
                          va='center',
                          transform=blend(ax.transData, ax.transAxes))
            ax.text(
                vline_xpos - pad_data_units,
                vline_lab_ypos,
                vline_labels[0],
                color=col0,
                **txt_kw,
            )
            ax.text(
                vline_xpos + pad_data_units,
                vline_lab_ypos,
                vline_labels[1],
                color=col1,
                **txt_kw,
            )

    # background shading of plot regions
    if shading:
        ax.axvspan(ax.get_xlim()[0], vline_xpos, color=col0, **shading_kw)
        ax.axvspan(vline_xpos, ax.get_xlim()[1], color=col1, **shading_kw)

    # text labels
    ax.set_title(title_txt.format(**title_fillers),
                 pad=title_pad * size_scale,
                 fontsize=font_size,
                 weight='bold')

    ax.text(-ylab_pad * size_scale,
            0.5,
            ylab_txt,
            fontsize=font_size,
            rotation=ylab_rotation,
            ha='center',
            va='center',
            transform=ax.transAxes)

    ax.set_xlabel(
        xlab_txt,
        fontsize=font_size,
        labelpad=xlab_pad * size_scale,
    )

    # last date label
    if thumbnail: last_date_label_pos = None
    if last_date_label_pos is not None:
        h_filename = data_file.split('_')[0] + '_estimate_history'
        c_filename = h_filename + '_columns'
        hdata_path = os.path.join(data_dir, h_filename + '.csv')
        cdata_path = os.path.join(data_dir, c_filename + '.csv')
        try:
            hist = pd.read_csv(hdata_path)
            columns = pd.read_csv(cdata_path).columns
            hist.columns = columns
            last_date = doy2dt(np.max(hist.date.values))
            datestr = last_date.strftime('%Y/%m/%d')
            txt = f'Data date: {datestr}'
        except:
            txt = ''
        ax.text(last_date_label_pos[0],
                last_date_label_pos[1],
                txt,
                fontsize=font_size - 10,
                color='black',
                alpha=0.3,
                style='italic',
                ha='left',
                va='bottom',
                zorder=200,
                transform=fig.transFigure)

    # watermark
    if not thumbnail:
        ax.text(watermark_pos[0],
                watermark_pos[1],
                watermark,
                fontsize=font_size - 4,
                color='black',
                alpha=0.3,
                style='italic',
                ha='center',
                va='center',
                zorder=200,
                transform=ax.transAxes)

    # adjustments if a thumbnail image
    if thumbnail:
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

    # save out figure
    dpi = width_pixels_save / width
    if not out_path.endswith(out_format):
        out_path = f'{out_path}.{out_format}'
    if thumbnail:
        pl.savefig(out_path, dpi=dpi, bbox_inches='tight')
    else:
        pl.savefig(out_path, dpi=dpi)
    pl.close(fig)
Esempio n. 16
0
def plot(xdata, ydata):
    """Plot the ERT log loss figures.

    Two cases: box-whisker plot is used for representing the data of all
    functions, otherwise all data is represented using crosses.

    """
    res = []

    tmp = list(10**numpy.mean(i[numpy.isfinite(i)]) for i in ydata)
    res.extend(
        plt.plot(
            xdata,
            tmp,
            ls='-',
            color='k',
            lw=3,  #marker='+',
            markersize=20,
            markeredgewidth=3))

    if max(len(i)
           for i in ydata) < 20:  # TODO: subgroups of function, hopefully.
        for i, y in enumerate(ydata):
            # plot all single data points
            if (numpy.isfinite(y) == False).any():
                assert not (numpy.isinf(y) * y > 0.).any()
                assert not numpy.isnan(y).any()

                ax = plt.gca()
                trans = blend(ax.transData, ax.transAxes)
                res.extend(
                    plt.plot((xdata[i], ), (0., ),
                             marker='+',
                             color=flierscolor,
                             ls='',
                             markersize=20,
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False))
                res.append(
                    plt.text(xdata[i],
                             0.02,
                             '%d' % len(y[numpy.isinf(y)]),
                             transform=trans,
                             horizontalalignment='left',
                             verticalalignment='bottom'))
                y = y[numpy.isfinite(y)]
                if len(y) == 0:
                    continue

            res.extend(
                plt.plot([xdata[i]] * len(y),
                         10**numpy.array(y),
                         marker='+',
                         color=flierscolor,
                         ls='',
                         markersize=20,
                         markeredgewidth=3))

            # plot dashed vertical line between min and max
            plt.plot(
                [xdata[i]] * 2,
                10**numpy.array([min(y), max(y)]),
                color='k',  # marker='+', 
                ls='--',
                linewidth=2)  #, markersize=20, markeredgewidth=3)
            # plot min and max with different symbol
            #plt.plot([xdata[i]], 10**min(numpy.array(y)),
            #                    marker='+', color='k',
            #                    ls='', markersize=20, markeredgewidth=3)
            #plt.plot([xdata[i]], 10**max(numpy.array(y)),
            #                    marker='+', color='k',
            #                    ls='', markersize=20, markeredgewidth=3)
    else:
        for i, y in enumerate(ydata):
            # plot all single data points
            if (numpy.isfinite(y) == False).any():
                assert not (numpy.isinf(y) * y > 0.).any()
                assert not numpy.isnan(y).any()

                ax = plt.gca()
                trans = blend(ax.transData, ax.transAxes)
                res.extend(
                    plt.plot((xdata[i], ), (0, ),
                             marker='.',
                             color='k',
                             ls='',
                             markersize=20,
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False))
                res.append(
                    plt.text(xdata[i],
                             0.02,
                             '%d' % len(y[numpy.isinf(y)]),
                             transform=trans,
                             horizontalalignment='left',
                             verticalalignment='bottom'))
                y = y[numpy.isfinite(y)]

        dictboxwhisker = boxplot(list(10**numpy.array(i) for i in ydata),
                                 sym='',
                                 notch=0,
                                 widths=None,
                                 positions=xdata)
        #'medians', 'fliers', 'whiskers', 'boxes', 'caps'
        plt.setp(dictboxwhisker['medians'], lw=3)
        plt.setp(dictboxwhisker['boxes'], lw=3)
        plt.setp(dictboxwhisker['caps'], lw=3)
        plt.setp(dictboxwhisker['whiskers'], lw=3)
        for i in dictboxwhisker.values():
            res.extend(i)
        res.extend(
            plt.plot(xdata,
                     list(10**min(i) for i in ydata),
                     marker='.',
                     markersize=20,
                     color='k',
                     ls=''))
        res.extend(
            plt.plot(xdata,
                     list(10**max(i) for i in ydata),
                     marker='.',
                     markersize=20,
                     color='k',
                     ls=''))

    return res
Esempio n. 17
0
def annotate(entry0, entry1, dim, minfvalue=1e-8, nbtests=1):
    """Display some annotations associated to the graphs generated."""

    isEarlyStop = False
    ha = 'left'
    va = 'center'
    lastfvalue = min(entry0.evals[-1][0], entry1.evals[-1][0])
    if minfvalue < lastfvalue:
        isEarlyStop = True
        #ha = 'center'
        #va = 'top'

    if not minfvalue or minfvalue < lastfvalue:
        minfvalue = lastfvalue

    line = []
    data0 = entry0.detEvals([minfvalue])[0]
    evals0 = data0.copy()
    succ = (np.isnan(evals0) == False)
    evals0[np.isnan(evals0)] = entry0.maxevals[np.isnan(evals0)]
    line.append(toolsstats.sp(evals0, issuccessful=succ))
    data1 = entry1.detEvals([minfvalue])[0]
    evals1 = data1.copy()
    succ = (np.isnan(evals1) == False)
    evals1[np.isnan(evals1)] = entry1.maxevals[np.isnan(evals1)]
    line.append(toolsstats.sp(evals1, issuccessful=succ))

    # What's the situation?
    txt = '%dD' % dim
    if (line[0][2] > 0 and line[1][2] > 0 and line[1][2] < 10):
        tmp = str(int(line[1][2]))
        tmp2 = str(int(line[0][2]))
        txt = tmp + '/' + tmp2

    dims = dimension_index
    ax = plt.gca()
    assert line[0][2] > 0 or line[1][2] > 0
    signdata = line[1][0] - line[0][0]

    if line[0][2] > 0 and line[1][2] > 0:
        trans = ax.transData
        annotcoord = [minfvalue, line[1][0]/line[0][0]]
    elif line[0][2] == 0:
        trans = blend(ax.transData, ax.transAxes)
        annotcoord = [minfvalue, -line[1][1]/2 + 0.5 + offset*(5-dims[dim])]
        #if va == 'top':
        #    va = 'bottom'
    elif line[1][2] == 0:
        trans = blend(ax.transData, ax.transAxes)
        annotcoord = [minfvalue, line[0][1]/2 + 0.5 - offset*(5-dims[dim])]

    plt.text(annotcoord[0], annotcoord[1], txt, horizontalalignment=ha,
             verticalalignment=va, transform=trans)

    #ranksum test
    line0 = np.power(data0, -1.)
    line0[np.isnan(line0)] = -entry0.finalfunvals[np.isnan(line0)]
    line1 = np.power(data1, -1.)
    line1[np.isnan(line1)] = -entry1.finalfunvals[np.isnan(line1)]
    # one-tailed statistics: scipy.stats.mannwhitneyu, two-tailed statistics: scipy.stats.ranksumtest
    z, p = ranksumtest(line0, line1)
    # Set the correct line in data0 and data1
    nbstars = 0
    # sign of z-value and data must agree
    if ((nbtests * p) < 0.05 and (z * signdata) > 0):
        nbstars = np.min([5, -np.ceil(np.log10(nbtests * p + 1e-99))])
    if nbstars > 0:
        xstars = annotcoord[0] * np.power(incrstars, np.arange(1., 1. + nbstars))
        # the additional slicing [0:int(nbstars)] is due to
        # np.arange(1., 1. - 0.1 * nbstars, -0.1) not having the right number
        # of elements due to numerical error
        ystars = [annotcoord[1]] * nbstars

        try:
            h = plt.plot(xstars, ystars, marker='*', ls='', color='w',
                         markersize=5*linewidth, markeredgecolor='k',
                         markerfacecolor='None',
                         zorder=20, markeredgewidth = 0.4 * linewidth,
                         transform=trans, clip_on=False)
        except KeyError:
            #Version problem
            h = plt.plot(xstars, ystars, marker='+', ls='', color='w',
                         markersize=2.5*linewidth, markeredgecolor='k',
                         zorder=20, markeredgewidth = 0.2 * linewidth,
                         transform=trans, clip_on=False)
Esempio n. 18
0
def main(dsList0, dsList1, outputdir, verbose=True):
    """Generate a scatter plot figure."""

    #plt.rc("axes", labelsize=24, titlesize=24)
    #plt.rc("xtick", labelsize=20)
    #plt.rc("ytick", labelsize=20)
    #plt.rc("font", size=20)
    #plt.rc("legend", fontsize=20)

    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    funcs = set(dictFunc0.keys()) & set(dictFunc1.keys())

    for f in funcs:
        dictDim0 = dictFunc0[f].dictByDim()
        dictDim1 = dictFunc1[f].dictByDim()
        dims = set(dictDim0.keys()) & set(dictDim1.keys())
        #set_trace()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue

            xdata = numpy.array(entry0.detERT(targets))
            ydata = numpy.array(entry1.detERT(targets))

            tmp = (numpy.isinf(xdata) == False) * (numpy.isinf(ydata) == False)
            if tmp.any():
                try:
                    plt.plot(xdata[tmp],
                             ydata[tmp],
                             ls='',
                             markersize=markersize,
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3)
                except KeyError:
                    plt.plot(xdata[tmp],
                             ydata[tmp],
                             ls='',
                             markersize=markersize,
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3)
                #try:
                #    plt.scatter(xdata[tmp], ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3)
                #except ValueError:
                #    set_trace()

            #ax = plt.gca()
            ax = plt.axes()

            tmp = numpy.isinf(xdata) * (numpy.isinf(ydata) == False)
            if tmp.any():
                trans = blend(ax.transAxes, ax.transData)
                #plt.scatter([1.]*numpy.sum(tmp), ydata[tmp], s=10, marker=markers[i],
                #            facecolor='None', edgecolor=colors[i], linewidth=3,
                #            transform=trans)
                try:
                    plt.plot([1.] * numpy.sum(tmp),
                             ydata[tmp],
                             markersize=markersize,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot([1.] * numpy.sum(tmp),
                             ydata[tmp],
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False)
                #set_trace()

            tmp = (numpy.isinf(xdata) == False) * numpy.isinf(ydata)
            if tmp.any():
                trans = blend(ax.transData, ax.transAxes)
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot(xdata[tmp], [1. - offset] * numpy.sum(tmp),
                             markersize=markersize,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False)
                except KeyError:
                    plt.plot(xdata[tmp], [1. - offset] * numpy.sum(tmp),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=trans,
                             clip_on=False)

            tmp = numpy.isinf(xdata) * numpy.isinf(ydata)
            if tmp.any():
                #    plt.scatter(xdata[tmp], [1.-offset]*numpy.sum(tmp), s=10, marker=markers[i],
                #                facecolor='None', edgecolor=colors[i], linewidth=3,
                #                transform=trans)
                try:
                    plt.plot([1. - offset] * numpy.sum(tmp),
                             [1. - offset] * numpy.sum(tmp),
                             markersize=markersize,
                             ls='',
                             marker=markers[i],
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=ax.transAxes,
                             clip_on=False)
                except KeyError:
                    plt.plot([1. - offset] * numpy.sum(tmp),
                             [1. - offset] * numpy.sum(tmp),
                             markersize=markersize,
                             ls='',
                             marker='x',
                             markerfacecolor='None',
                             markeredgecolor=colors[i],
                             markeredgewidth=3,
                             transform=ax.transAxes,
                             clip_on=False)

                #set_trace()

        beautify()

        for i, d in enumerate(dimensions):
            try:
                entry0 = dictDim0[d][0]  # should be only one element
                entry1 = dictDim1[d][0]  # should be only one element
            except (IndexError, KeyError):
                continue

            minbnd, maxbnd = plt.xlim()
            plt.plot(
                (entry0.mMaxEvals(), entry0.mMaxEvals()),
                # (minbnd, entry1.mMaxEvals()), ls='-', color=colors[i],
                (max([minbnd, entry1.mMaxEvals() / 10.]), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.plot(  # (minbnd, entry0.mMaxEvals()),
                (max([minbnd, entry0.mMaxEvals() / 10.]), entry0.mMaxEvals()),
                (entry1.mMaxEvals(), entry1.mMaxEvals()),
                ls='-',
                color=colors[i],
                zorder=-1)
            plt.xlim(minbnd, maxbnd)
            plt.ylim(minbnd, maxbnd)
            #Set the boundaries again: they changed due to new plots.

            #plt.axvline(entry0.mMaxEvals(), ls='--', color=colors[i])
            #plt.axhline(entry1.mMaxEvals(), ls='--', color=colors[i])

        if isBenchmarkinfosFound:
            try:
                plt.ylabel(funInfos[f])
            except IndexError:
                pass

        filename = os.path.join(outputdir, 'ppscatter_f%03d' % f)
        saveFigure(filename, verbose=verbose)
        plt.close()