Ejemplo n.º 1
0
def plotECDF(x, n=None, **plotArgs):
    """Plot an empirical cumulative distribution function.

    :param seq x: data
    :param int n: number of samples, if not provided len(x) is used
    :param plotArgs: optional keyword arguments provided to plot.

    :returns: handles of the plot elements.

    """
    if n is None:
        n = len(x)

    nx = len(x)
    if n == 0 or nx == 0:
        res = plt.plot([], [], **plotArgs)
    else:
        x = sorted(x)  # do not sort in place
        x = np.hstack((x, x[-1]))
        y = np.hstack((np.arange(0., nx) / n, float(nx) / n))
        res = plotUnifLogXMarkers(x,
                                  y,
                                  nbperdecade=nbperdecade,
                                  drawstyle='steps',
                                  **plotArgs)
    return res
Ejemplo n.º 2
0
def plotECDF(x, n=None, **plotArgs):
    """Plot an empirical cumulative distribution function.

    :param seq x: data
    :param int n: number of samples, if not provided len(x) is used
    :param plotArgs: optional keyword arguments provided to plot.

    :returns: handles of the plot elements.

    """
    if n is None:
        n = len(x)

    nx = len(x)
    if n == 0 or nx == 0:
        res = plt.plot([], [], **plotArgs)
    else:
        x = sorted(x)  # do not sort in place
        x = np.hstack((x, x[-1]))
        y = np.hstack((np.arange(0.0, nx) / n, float(nx) / n))
        res = plotUnifLogXMarkers(x, y, nbperdecade=nbperdecade, drawstyle="steps", **plotArgs)
    return res
Ejemplo n.º 3
0
def plotdata(data, maxval=None, maxevals=None, CrE=0., **kwargs):
    """Draw a normalized ECDF. What means normalized?
    
    :param seq data: data set, a 1-D ndarray of runlengths
    :param float maxval: right-most value to be displayed, will use the
                         largest non-inf, non-nan value in data if not
                         provided
    :param seq maxevals: if provided, will plot the median of this
                         sequence as a single cross marker
    :param float CrE: Crafting effort the data will be multiplied by
                      the exponential of this value.
    :param kwargs: optional arguments provided to plot function.
    
    """

    #Expect data to be a ndarray.
    x = data[np.isnan(data) == False]  # Take away the nans
    nn = len(x)

    x = x[np.isinf(x) == False]  # Take away the infs
    n = len(x)

    x = np.exp(CrE) * x  # correction by crafting effort CrE

    if n == 0:
        #res = plt.plot((1., ), (0., ), **kwargs)
        res = pprldistr.plotECDF(np.array((1., )), n=np.inf, **kwargs)
    else:
        dictx = {}  # number of appearances of each value in x
        for i in x:
            dictx[i] = dictx.get(i, 0) + 1

        x = np.array(sorted(dictx))  # x is not a multiset anymore
        y = np.cumsum(list(
            dictx[i]
            for i in x))  # cumsum of size of y-steps (nb of appearences)
        idx = sum(x <= x_limit**annotation_space_end_relative) - 1
        y_last, x_last = y[idx] / float(nn), x[idx]
        if maxval is None:
            maxval = max(x)
        end = np.sum(x <= maxval)
        x = x[:end]
        y = y[:end]

        try:  # plot the very last point outside of the "normal" plotting area
            c = kwargs['color']
            plt_plot([x_last] * 2, [y_last] * 2,
                     '.',
                     color=c,
                     markeredgecolor=c)
        except:
            pass
        x2 = np.hstack([np.repeat(x, 2),
                        maxval])  # repeat x-values for each step in the cdf
        y2 = np.hstack([0.0, np.repeat(y / float(nn), 2)])

        res = ppfig.plotUnifLogXMarkers(x2,
                                        y2,
                                        nbperdecade * 3 / np.log10(maxval),
                                        logscale=False,
                                        clip_on=False,
                                        **kwargs)
        # res = plotUnifLogXMarkers(x2, y2, nbperdecade, logscale=False, **kwargs)

        if maxevals:  # Should cover the case where maxevals is None or empty
            x3 = np.median(maxevals)
            if (x3 <= maxval and
                    # np.any(x2 <= x3) and   # maxval < median(maxevals)
                    not plt.getp(res[-1], 'label').startswith('best')
                ):  # TODO: HACK for not considering the best 2009 line
                try:
                    y3 = y2[x2 <= x3][
                        -1]  # find right y-value for x3==median(maxevals)
                except IndexError:  # median(maxevals) is smaller than any data, can only happen because of CrE?
                    y3 = y2[0]
                h = plt.plot(
                    (x3, ),
                    (y3, ),
                    marker=median_max_evals_marker_format[0],
                    markersize=median_max_evals_marker_format[1],
                    markeredgewidth=median_max_evals_marker_format[2],
                    # marker='x', markersize=24, markeredgewidth=3,
                    markeredgecolor=plt.getp(res[0], 'color'),
                    ls=plt.getp(res[0], 'ls'),
                    color=plt.getp(res[0], 'color'))
                h.extend(res)
                res = h  # so the last element in res still has the label.
                # Only take sequences for x and y!

    return res
Ejemplo n.º 4
0
def main(dsList0, dsList1, minfvalue=1e-8, outputdir='', verbose=True):
    """Returns ERT1/ERT0 comparison figure."""

    #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)

    # minfvalue = pproc.TargetValues.cast(minfvalue)

    dictFun0 = dsList0.dictByFunc()
    dictFun1 = dsList1.dictByFunc()

    for func in set.intersection(set(dictFun0), set(dictFun1)):
        dictDim0 = dictFun0[func].dictByDim()
        dictDim1 = dictFun1[func].dictByDim()

        if isBenchmarkinfosFound:
            title = funInfos[func]
        else:
            title = ''

        filename = os.path.join(outputdir, 'ppfig2_f%03d' % (func))

        dims = sorted(set.intersection(set(dictDim0), set(dictDim1)))

        handles = []
        dataperdim = {}
        fvalueswitch = {}
        nbtests = 0
        for i, dim in enumerate(dimensions):
            try:
                entry0 = dictDim0[dim][0]
                entry1 = dictDim1[dim][0]
            except KeyError:
                continue

            nbtests += 1
            # generateData:
            data = _generateData(entry0, entry1, fthresh=fthresh)
            dataperdim[dim] = data

            if len(data[0]) == 0 and len(data[1]) == 0:
                continue

            # TODO: hack, modify slightly so line goes to 'zero'
            if minfvalue:
                for d in data:
                    tmp = d[:, 0]
                    tmp[tmp == 0] = min(min(tmp[tmp > 0]), minfvalue)**2

            # plot
            idx = np.isfinite(data[0][:, 1]) * np.isfinite(data[1][:, 1])
            ydata = data[1][idx, 1] / data[0][idx, 1]
            kwargs = styles[i].copy()
            kwargs['label'] = '%2d-D' % dim
            tmp = plotUnifLogXMarkers(data[0][idx, 0],
                                      ydata,
                                      nbperdecade=1,
                                      logscale=True,
                                      **kwargs)
            plt.setp(tmp, markersize=3 * linewidth)
            plt.setp(tmp[0], ls='--')

            # This is only one possibility:
            #idx = (data[0][:, 3] >= 5) * (data[1][:, 3] >= 5)
            idx = ((data[0][:, 1] <= 3 * np.median(entry0.maxevals)) *
                   (data[1][:, 1] <= 3 * np.median(entry1.maxevals)))

            if not idx.any():
                fvalueswitch[dim] = np.inf
                # Hack: fvalueswitch is the smallest value of f where the line
                # was still solid.
                continue

            fvalueswitch[dim] = min(data[0][idx, 0])
            ydata = data[1][idx, 1] / data[0][idx, 1]
            tmp = plotUnifLogXMarkers(data[0][idx, 0],
                                      ydata,
                                      nbperdecade=1,
                                      logscale=True,
                                      **styles[i])
            plt.setp(tmp[1], markersize=3 * linewidth)

        beautify(xmin=minfvalue)
        #beautify()
        ax = plt.gca()
        # Freeze the boundaries
        ax.set_autoscale_on(False)
        #trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)

        # Plot everything else
        for i, dim in enumerate(dimensions):
            try:
                entry0 = dictDim0[dim][0]
                entry1 = dictDim1[dim][0]
                data = dataperdim[dim]
            except KeyError:
                continue

            if len(data[0]) == 0 and len(data[1]) == 0:
                continue

            # annotation
            annotate(entry0, entry1, dim, minfvalue, nbtests=nbtests)

            tmp0 = np.isfinite(data[0][:, 1])
            tmp1 = np.isfinite(data[1][:, 1])
            idx = tmp0 * tmp1

            if not idx.any():
                continue

            #Do not plot anything else if it happens after minfvalue
            if data[0][idx, 0][-1] <= minfvalue:
                # hack for the legend
                continue

            # Determine which algorithm went further
            algstoppedlast = 0
            algstoppedfirst = 1

            if np.sum(tmp0) < np.sum(tmp1):
                algstoppedlast = 1
                algstoppedfirst = 0

            #marker if an algorithm stopped
            ydata = data[1][idx, 1] / data[0][idx, 1]
            plt.plot((data[0][idx, 0][-1], ), (ydata[-1], ),
                     marker='D',
                     ls='',
                     color=styles[i]['color'],
                     markeredgecolor=styles[i]['color'],
                     markerfacecolor=styles[i]['color'],
                     markersize=4 * linewidth)
            tmpy = ydata[-1]

            # plot probability of success line
            dataofinterest = data[algstoppedlast]

            tmp = np.nonzero(idx)[0][-1]  # Why [0]?
            # add the last line for which both algorithm still have a success
            idx = (data[algstoppedfirst][:, 2]
                   == 0.) * (dataofinterest[:, 2] > 0.)
            idx[tmp] = True

            if np.sum(idx) <= 1:  #len(idx) == 0 or not idx.any():
                continue

            ymin, ymax = plt.ylim()
            #orientation = -1
            ybnd = ymin
            if algstoppedlast == 0:
                ybnd = ymax
                #orientation = 1

            #ydata = orientation * dataofinterest[idx, 2] / 2 + 0.5
            ydata = np.power(
                10,
                np.log10(ybnd) *
                (dataofinterest[idx, 2] - offset *
                 (5 - i) * np.log10(ymax / ymin) / np.abs(np.log10(ybnd))))

            ls = '-'
            if dataofinterest[idx, 0][0] < fvalueswitch[dim]:
                ls = '--'

            tmp = plt.plot([dataofinterest[idx, 0][0]] * 2, (tmpy, ydata[0]),
                           **styles[i])
            plt.setp(tmp, ls=ls, marker='')
            tmp = plt.plot((dataofinterest[idx, 0][0], ), (ydata[0], ),
                           marker='D',
                           ls='',
                           color=styles[i]['color'],
                           markeredgecolor=styles[i]['color'],
                           markerfacecolor=styles[i]['color'],
                           markersize=4 * linewidth)

            kwargs = styles[i].copy()
            kwargs['ls'] = ls
            tmp = plotUnifLogXMarkers(dataofinterest[idx, 0],
                                      ydata,
                                      nbperdecade=1,
                                      logscale=True,
                                      **kwargs)
            plt.setp(tmp, markersize=3 * linewidth)

            #Do not plot anything else if it happens after minfvalue
            if dataofinterest[idx, 0][-1] <= minfvalue:
                continue
            #plt.plot((dataofinterest[idx, 0][-1], ), (ydata[-1], ), marker='d',
            #         color=styles[i]['color'], markeredgecolor=styles[i]['color'],
            #         markerfacecolor=styles[i]['color'], markersize=4*linewidth)

        if isBenchmarkinfosFound:
            plt.title(funInfos[func])

        if func in functions_with_legend:
            plt.legend(loc='best')

        # save
        saveFigure(filename, verbose=verbose)
        plt.close()
Ejemplo n.º 5
0
def main(dsList0, dsList1, minfvalue=1e-8, outputdir='', verbose=True):
    """Returns ERT1/ERT0 comparison figure."""

    #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)
    
    # minfvalue = pproc.TargetValues.cast(minfvalue)

    dictFun0 = dsList0.dictByFunc()
    dictFun1 = dsList1.dictByFunc()

    for func in set.intersection(set(dictFun0), set(dictFun1)):
        dictDim0 = dictFun0[func].dictByDim()
        dictDim1 = dictFun1[func].dictByDim()

        if isBenchmarkinfosFound:
            title = funInfos[func]
        else:
            title = ''

        filename = os.path.join(outputdir,'ppfig2_f%03d' % (func))

        dims = sorted(set.intersection(set(dictDim0), set(dictDim1)))

        handles = []
        dataperdim = {}
        fvalueswitch = {}
        nbtests = 0
        for i, dim in enumerate(dimensions):
            try:
                entry0 = dictDim0[dim][0]
                entry1 = dictDim1[dim][0]
            except KeyError:
                continue

            nbtests += 1
            # generateData:
            data = _generateData(entry0, entry1, fthresh=fthresh)
            dataperdim[dim] = data

            if len(data[0]) == 0 and len(data[1]) == 0:
                continue

            # TODO: hack, modify slightly so line goes to 'zero'
            if minfvalue:
                for d in data:
                    tmp = d[:, 0]
                    tmp[tmp == 0] = min(min(tmp[tmp > 0]), minfvalue)**2

            # plot
            idx = np.isfinite(data[0][:, 1]) * np.isfinite(data[1][:, 1])
            ydata = data[1][idx, 1]/data[0][idx, 1]
            kwargs = styles[i].copy()
            kwargs['label'] = '%2d-D' % dim
            tmp = plotUnifLogXMarkers(data[0][idx, 0], ydata, nbperdecade=1, logscale=True, **kwargs)
            plt.setp(tmp, markersize=3*linewidth)
            plt.setp(tmp[0], ls='--')

            # This is only one possibility:
            #idx = (data[0][:, 3] >= 5) * (data[1][:, 3] >= 5)
            idx = ((data[0][:, 1] <= 3 * np.median(entry0.maxevals))
                   * (data[1][:, 1] <= 3 * np.median(entry1.maxevals)))

            if not idx.any():
                fvalueswitch[dim] = np.inf
                # Hack: fvalueswitch is the smallest value of f where the line
                # was still solid.
                continue

            fvalueswitch[dim] = min(data[0][idx, 0])
            ydata = data[1][idx, 1]/data[0][idx, 1]
            tmp = plotUnifLogXMarkers(data[0][idx, 0], ydata, nbperdecade=1, logscale=True, **styles[i])
            plt.setp(tmp[1], markersize=3*linewidth)

        beautify(xmin=minfvalue)
        #beautify()
        ax = plt.gca()
        # Freeze the boundaries
        ax.set_autoscale_on(False)
        #trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)

        # Plot everything else
        for i, dim in enumerate(dimensions):
            try:
                entry0 = dictDim0[dim][0]
                entry1 = dictDim1[dim][0]
                data = dataperdim[dim]
            except KeyError:
                continue

            if len(data[0]) == 0 and len(data[1]) == 0:
                continue

            # annotation
            annotate(entry0, entry1, dim, minfvalue, nbtests=nbtests)

            tmp0 = np.isfinite(data[0][:, 1])
            tmp1 = np.isfinite(data[1][:, 1])
            idx = tmp0 * tmp1

            if not idx.any():
                continue

            #Do not plot anything else if it happens after minfvalue
            if data[0][idx, 0][-1] <= minfvalue:
                # hack for the legend
                continue

            # Determine which algorithm went further
            algstoppedlast = 0
            algstoppedfirst = 1

            if np.sum(tmp0) < np.sum(tmp1):
                algstoppedlast = 1
                algstoppedfirst = 0

            #marker if an algorithm stopped
            ydata = data[1][idx, 1]/data[0][idx, 1]
            plt.plot((data[0][idx, 0][-1], ), (ydata[-1], ), marker='D', ls='',
                     color=styles[i]['color'], markeredgecolor=styles[i]['color'],
                     markerfacecolor=styles[i]['color'], markersize=4*linewidth)
            tmpy = ydata[-1]

            # plot probability of success line
            dataofinterest = data[algstoppedlast]

            tmp = np.nonzero(idx)[0][-1] # Why [0]?
            # add the last line for which both algorithm still have a success
            idx = (data[algstoppedfirst][:, 2] == 0.) * (dataofinterest[:, 2] > 0.)
            idx[tmp] = True

            if np.sum(idx) <= 1:#len(idx) == 0 or not idx.any():
                continue

            ymin, ymax = plt.ylim()
            #orientation = -1
            ybnd = ymin
            if algstoppedlast == 0:
                ybnd = ymax
                #orientation = 1

            #ydata = orientation * dataofinterest[idx, 2] / 2 + 0.5
            ydata = np.power(10, np.log10(ybnd) * (dataofinterest[idx, 2]
                                                         -offset*(5-i)*np.log10(ymax/ymin)/np.abs(np.log10(ybnd))))

            ls = '-'
            if dataofinterest[idx, 0][0] < fvalueswitch[dim]:
                ls = '--'

            tmp = plt.plot([dataofinterest[idx, 0][0]]*2, (tmpy, ydata[0]),
                           **styles[i])
            plt.setp(tmp, ls=ls, marker='')
            tmp = plt.plot((dataofinterest[idx, 0][0], ), (ydata[0], ), marker='D', ls='',
                     color=styles[i]['color'], markeredgecolor=styles[i]['color'],
                     markerfacecolor=styles[i]['color'], markersize=4*linewidth)

            kwargs = styles[i].copy()
            kwargs['ls'] = ls
            tmp = plotUnifLogXMarkers(dataofinterest[idx, 0], ydata, nbperdecade=1, logscale=True, **kwargs)
            plt.setp(tmp, markersize=3*linewidth)

            #Do not plot anything else if it happens after minfvalue
            if dataofinterest[idx, 0][-1] <= minfvalue:
                continue
            #plt.plot((dataofinterest[idx, 0][-1], ), (ydata[-1], ), marker='d',
            #         color=styles[i]['color'], markeredgecolor=styles[i]['color'],
            #         markerfacecolor=styles[i]['color'], markersize=4*linewidth)

        if isBenchmarkinfosFound:
            plt.title(funInfos[func])

        if func in functions_with_legend:
            plt.legend(loc='best')

        # save
        saveFigure(filename, verbose=verbose)
        plt.close()
Ejemplo n.º 6
0
def plotdata(data, maxval=None, maxevals=None, CrE=0., **kwargs):
    """Draw a normalized ECDF. What means normalized?
    
    :param seq data: data set, a 1-D ndarray of runlengths
    :param float maxval: right-most value to be displayed, will use the
                         largest non-inf, non-nan value in data if not
                         provided
    :param seq maxevals: if provided, will plot the median of this
                         sequence as a single cross marker
    :param float CrE: Crafting effort the data will be multiplied by
                      the exponential of this value.
    :param kwargs: optional arguments provided to plot function.
    
    """

    #Expect data to be a ndarray.
    x = data[np.isnan(data)==False] # Take away the nans
    nn = len(x)

    x = x[np.isinf(x)==False] # Take away the infs
    n = len(x)

    x = np.exp(CrE) * x  # correction by crafting effort CrE

    if n == 0:
        #res = plt.plot((1., ), (0., ), **kwargs)
        res = pprldistr.plotECDF(np.array((1., )), n=np.inf, **kwargs)
    else:
        dictx = {} # number of appearances of each value in x
        for i in x: 
            dictx[i] = dictx.get(i, 0) + 1  

        x = np.array(sorted(dictx))  # x is not a multiset anymore
        y = np.cumsum(list(dictx[i] for i in x)) # cumsum of size of y-steps (nb of appearences)
        idx = sum(x <= x_limit**annotation_space_end_relative) - 1 
        y_last, x_last = y[idx] / float(nn), x[idx] 
        if maxval is None:
            maxval = max(x)
        end = np.sum(x <= maxval)
        x = x[:end]
        y = y[:end]
        
        try:  # plot the very last point outside of the "normal" plotting area
            c = kwargs['color']
            plt_plot([x_last] * 2, [y_last] * 2, '.', color=c, markeredgecolor=c) 
        except:
            pass
        x2 = np.hstack([np.repeat(x, 2), maxval]) # repeat x-values for each step in the cdf
        y2 = np.hstack([0.0, np.repeat(y / float(nn), 2)])

        res = ppfig.plotUnifLogXMarkers(x2, y2, nbperdecade * 3 / np.log10(maxval), 
                                  logscale=False, clip_on=False, **kwargs)
        # res = plotUnifLogXMarkers(x2, y2, nbperdecade, logscale=False, **kwargs)

        if maxevals: # Should cover the case where maxevals is None or empty
            x3 = np.median(maxevals)
            if (x3 <= maxval and 
                # np.any(x2 <= x3) and   # maxval < median(maxevals)
                not plt.getp(res[-1], 'label').startswith('best')
                ): # TODO: HACK for not considering the best 2009 line
                try:
                    y3 = y2[x2<=x3][-1]  # find right y-value for x3==median(maxevals)
                except IndexError:  # median(maxevals) is smaller than any data, can only happen because of CrE?
                    y3 = y2[0]
                h = plt.plot((x3,), (y3,), 
                             marker=median_max_evals_marker_format[0],
                             markersize=median_max_evals_marker_format[1],
                             markeredgewidth=median_max_evals_marker_format[2],
                             # marker='x', markersize=24, markeredgewidth=3, 
                             markeredgecolor=plt.getp(res[0], 'color'),
                             ls=plt.getp(res[0], 'ls'),
                             color=plt.getp(res[0], 'color'))
                h.extend(res)
                res = h # so the last element in res still has the label.
                # Only take sequences for x and y!

    return res
Ejemplo n.º 7
0
def plotLogAbs(dsList0, dsList1, dim, targetValuesToReach, verbose=True):
    """Creates ECDF of run length ratios.

    :param DataSetList dsList0: reference
    :param DataSetList dsList1: data set list of algorithm of interest
    :param int dim: dimension
    :param TargetValues targetValuesToReach: target function values
    :param bool verbose: controls verbosity

    :returns: handles

    """
    res = []
    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    evals0 = {}
    evals1 = {}

    if not isinstance(targetValuesToReach, pproc.TargetValues):
        targetValuesToReach = pproc.TargetValues(targetValuesToReach)

    succ0 = [0] * len(targetValuesToReach)
    succ1 = [0] * len(targetValuesToReach)

    # TODO: check all functions are there...
    for func in set(dictFunc0.keys()) & set(dictFunc1.keys()):
        i0 = dictFunc0[func][0]
        i1 = dictFunc1[func][0]

        # tmp = list(i[func] for i in fvalueToReach)
        # if tmp != targets:
        #    set_trace() # should not occur
        #    warnings.warn('')

        evals0[func] = i0.detEvals(targetValuesToReach((func, dim)))
        for i, evals in enumerate(evals0[func]):
            tmp = numpy.isnan(evals)
            evals[tmp] = numpy.inf
            if not tmp.all():
                succ0[i] += 1

        evals1[func] = i1.detEvals(targetValuesToReach((func, dim)))
        for i, evals in enumerate(evals1[func]):
            tmp = numpy.isnan(evals)
            evals[tmp] = numpy.inf
            if not tmp.all():
                succ1[i] += 1

    for j in range(len(targetValuesToReach)):
        x = []
        for func in evals0:
            # Compute the pair-wise ratio
            tmp1 = numpy.reshape(evals1[func][j], (1, len(evals1[func][j])))
            tmp0 = numpy.reshape(evals0[func][j], (len(evals0[func][j]), 1))
            try:
                x.append((tmp1 / tmp0).flatten())  # inf/inf results in nan
            except FloatingPointError:
                if numpy.isfinite(tmp1).all() or numpy.isfinite(tmp1).all():
                    raise

                # TODO: check division, check numpy.inf...

        if isinstance(targetValuesToReach, pproc.RunlengthBasedTargetValues):
            label = "%s: %d/%d" % (targetValuesToReach.label(j), succ1[j], succ0[j])
        else:
            label = "%s: %d/%d" % (targetValuesToReach.loglabel(j), succ1[j], succ0[j])
        if len(x) > 0:  # prevent warning/error
            x = numpy.hstack(x)
            x = x[numpy.isnan(x) == False]  # Is it correct?
        n = len(x)

        if n == 0:
            res.extend(plt.plot([], [], label=label, linewidth=3.0, **rldStyles[j]))
            continue  # no plot?

        x.sort()
        # Catch negative values: zeros are not a problem...
        tmp = len(list(i for i in x if i <= 0))
        x = x[tmp:]
        # Catch inf, those could be a problem with the log scale...
        # tmp2 = 0
        tmp2 = len(list(i for i in x if i > 0 and numpy.isinf(i)))
        if tmp2 > 0:
            x = x[:-tmp2]

        # xbound = max(abs(numpy.floor(numpy.log10(x[0]))),
        #             abs(numpy.ceil(numpy.log10(x[-1]))))
        if len(x) == 0:
            res.append(plt.axhline(tmp / float(n), label=label, linewidth=3.0, **rldStyles[j]))
            # tmp/float(n) == (n-tmp2)/float(n) # TODO: check
        else:
            x2 = numpy.hstack([numpy.repeat(x, 2)])
            # maxEvalsF: used for the limit of the plot.
            y2 = numpy.hstack(
                [tmp / float(n), numpy.repeat(numpy.arange(tmp + 1, n - tmp2) / float(n), 2), (n - tmp2) / float(n)]
            )
            # res.extend(plt.plot(x2, y2, label=label, linewidth=3., **rldStyles[i]))
            plotArgs = rldStyles[j].copy()
            plotArgs["label"] = label
            plotArgs["linewidth"] = 3.0
            # res.extend(plotUnifLogXMarkers(x2, y2, 3, plotArgs))
            res.append(plotUnifLogXMarkers(x2, y2, nbperdecade=3, logscale=False, **plotArgs)[0])

        # TODO: check if all of evalsX[func] is numpy.inf and so on...

    return res
Ejemplo n.º 8
0
def plotLogAbs(dsList0, dsList1, dim, targetValuesToReach, verbose=True):
    """Creates ECDF of run length ratios.

    :param DataSetList dsList0: reference
    :param DataSetList dsList1: data set list of algorithm of interest
    :param int dim: dimension
    :param TargetValues targetValuesToReach: target function values
    :param bool verbose: controls verbosity

    :returns: handles

    """
    res = []
    dictFunc0 = dsList0.dictByFunc()
    dictFunc1 = dsList1.dictByFunc()
    evals0 = {}
    evals1 = {}

    if not isinstance(targetValuesToReach, pproc.TargetValues):
        targetValuesToReach = pproc.TargetValues(targetValuesToReach)

    succ0 = [0] * len(targetValuesToReach)
    succ1 = [0] * len(targetValuesToReach)

    # TODO: check all functions are there...
    for func in set(dictFunc0.keys()) & set(dictFunc1.keys()):
        i0 = dictFunc0[func][0]
        i1 = dictFunc1[func][0]

        #tmp = list(i[func] for i in fvalueToReach)
        #if tmp != targets:
        #    set_trace() # should not occur
        #    warnings.warn('')

        evals0[func] = i0.detEvals(targetValuesToReach((func, dim)))
        for i, evals in enumerate(evals0[func]):
            tmp = numpy.isnan(evals)
            evals[tmp] = numpy.inf
            if not tmp.all():
                succ0[i] += 1

        evals1[func] = i1.detEvals(targetValuesToReach((func, dim)))
        for i, evals in enumerate(evals1[func]):
            tmp = numpy.isnan(evals)
            evals[tmp] = numpy.inf
            if not tmp.all():
                succ1[i] += 1

    for j in range(len(targetValuesToReach)):
        x = []
        for func in evals0:
            # Compute the pair-wise ratio
            tmp1 = numpy.reshape(evals1[func][j], (1, len(evals1[func][j])))
            tmp0 = numpy.reshape(evals0[func][j], (len(evals0[func][j]), 1))
            try:
                x.append((tmp1 / tmp0).flatten())  # inf/inf results in nan
            except FloatingPointError:
                if numpy.isfinite(tmp1).all() or numpy.isfinite(tmp1).all():
                    raise

                #TODO: check division, check numpy.inf...

        if isinstance(targetValuesToReach, pproc.RunlengthBasedTargetValues):
            label = '%s: %d/%d' % (targetValuesToReach.label(j), succ1[j],
                                   succ0[j])
        else:
            label = '%s: %d/%d' % (targetValuesToReach.loglabel(j), succ1[j],
                                   succ0[j])
        if len(x) > 0:  # prevent warning/error
            x = numpy.hstack(x)
            x = x[numpy.isnan(x) == False]  # Is it correct?
        n = len(x)

        if n == 0:
            res.extend(
                plt.plot([], [], label=label, linewidth=3., **rldStyles[j]))
            continue  # no plot?

        x.sort()
        #Catch negative values: zeros are not a problem...
        tmp = len(list(i for i in x if i <= 0))
        x = x[tmp:]
        #Catch inf, those could be a problem with the log scale...
        #tmp2 = 0
        tmp2 = len(list(i for i in x if i > 0 and numpy.isinf(i)))
        if tmp2 > 0:
            x = x[:-tmp2]

        #xbound = max(abs(numpy.floor(numpy.log10(x[0]))),
        #             abs(numpy.ceil(numpy.log10(x[-1]))))
        if len(x) == 0:
            res.append(
                plt.axhline(tmp / float(n),
                            label=label,
                            linewidth=3.,
                            **rldStyles[j]))
            # tmp/float(n) == (n-tmp2)/float(n) # TODO: check
        else:
            x2 = numpy.hstack([numpy.repeat(x, 2)])
            #maxEvalsF: used for the limit of the plot.
            y2 = numpy.hstack([
                tmp / float(n),
                numpy.repeat(numpy.arange(tmp + 1, n - tmp2) / float(n), 2),
                (n - tmp2) / float(n)
            ])
            #res.extend(plt.plot(x2, y2, label=label, linewidth=3., **rldStyles[i]))
            plotArgs = rldStyles[j].copy()
            plotArgs['label'] = label
            plotArgs['linewidth'] = 3.
            #res.extend(plotUnifLogXMarkers(x2, y2, 3, plotArgs))
            res.append(
                plotUnifLogXMarkers(x2,
                                    y2,
                                    nbperdecade=3,
                                    logscale=False,
                                    **plotArgs)[0])

        # TODO: check if all of evalsX[func] is numpy.inf and so on...

    return res