Ejemplo n.º 1
0
def draw_precision_recall_curve(recall_domain, p_interp, title_pref=None, fnum=1, pnum=None, color=None):
    import plottool as pt

    if color is None:
        color = (0.4, 1.0, 0.4) if pt.is_default_dark_bg() else (0.1, 0.4, 0.4)
    if recall_domain is None:
        recall_domain = np.array([])
        p_interp = np.array([])
    if recall_domain is None:
        ave_p = -1.0  # np.nan
    else:
        ave_p = p_interp.sum() / p_interp.size

    pt.plot2(
        recall_domain,
        p_interp,
        marker="o--",
        x_label="recall",
        y_label="precision",
        unitbox=True,
        flipx=False,
        color=color,
        fnum=fnum,
        pnum=pnum,
        title="Interplated Precision Vs Recall\n" + "avep = %.3f" % ave_p,
    )
Ejemplo n.º 2
0
def draw_precision_recall_curve(recall_domain,
                                p_interp,
                                title_pref=None,
                                fnum=1,
                                pnum=None,
                                color=None):
    import plottool as pt
    if color is None:
        color = (0.4, 1.0, 0.4) if pt.is_default_dark_bg() else (0.1, 0.4, 0.4)
    if recall_domain is None:
        recall_domain = np.array([])
        p_interp = np.array([])
    if recall_domain is None:
        ave_p = -1.0  # np.nan
    else:
        ave_p = p_interp.sum() / p_interp.size

    pt.plot2(recall_domain,
             p_interp,
             marker='o--',
             x_label='recall',
             y_label='precision',
             unitbox=True,
             flipx=False,
             color=color,
             fnum=fnum,
             pnum=pnum,
             title='Interplated Precision Vs Recall\n' + 'avep = %.3f' % ave_p)
Ejemplo n.º 3
0
def distinct_colors(N, brightness=.878, randomize=True, hue_range=(0.0, 1.0), cmap_seed=None):
    r"""
    Args:
        N (int):
        brightness (float):

    Returns:
        list: RGB_tuples

    CommandLine:
        python -m plottool.color_funcs --test-distinct_colors --N 2 --show --hue-range=0.05,.95
        python -m plottool.color_funcs --test-distinct_colors --N 3 --show --hue-range=0.05,.95
        python -m plottool.color_funcs --test-distinct_colors --N 4 --show --hue-range=0.05,.95
        python -m plottool.color_funcs --test-distinct_colors --N 3 --show --no-randomize
        python -m plottool.color_funcs --test-distinct_colors --N 4 --show --no-randomize
        python -m plottool.color_funcs --test-distinct_colors --N 20 --show

    References:
        http://blog.jianhuashao.com/2011/09/generate-n-distinct-colors.html

    CommandLine:
        python -m plottool.color_funcs --exec-distinct_colors --show
        python -m plottool.color_funcs --exec-distinct_colors --show --no-randomize --N 50
        python -m plottool.color_funcs --exec-distinct_colors --show --cmap_seed=foobar

    Example:
        >>> # ENABLE_DOCTEST
        >>> from plottool.color_funcs import *  # NOQA
        >>> # build test data
        >>> N = ut.get_argval('--N', int, 2)
        >>> randomize = not ut.get_argflag('--no-randomize')
        >>> brightness = 0.878
        >>> # execute function
        >>> cmap_seed = ut.get_argval('--cmap_seed', str, default=None)
        >>> hue_range = ut.get_argval('--hue-range', list, default=(0.00, 1.0))
        >>> RGB_tuples = distinct_colors(N, brightness, randomize, hue_range, cmap_seed=cmap_seed)
        >>> # verify results
        >>> assert len(RGB_tuples) == N
        >>> result = str(RGB_tuples)
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> color_list = RGB_tuples
        >>> testshow_colors(color_list)
        >>> ut.show_if_requested()
    """
    # TODO: Add sin wave modulation to the sat and value
    #import plottool as pt
    if True:
        import plottool as pt
        # HACK for white figures
        remove_yellow = not pt.is_default_dark_bg()
        #if not pt.is_default_dark_bg():
        #    brightness = .8

    use_jet = False
    if use_jet:
        import plottool as pt
        cmap = pt.plt.cm.jet
        RGB_tuples = list(map(tuple, cmap(np.linspace(0, 1, N))))
    elif cmap_seed is not None:
        # Randomized map based on a seed
        #cmap_ = 'Set1'
        #cmap_ = 'Dark2'
        choices = [
            #'Set1', 'Dark2',
            'jet',
            #'gist_rainbow',
            #'rainbow',
            #'gnuplot',
            #'Accent'
        ]
        cmap_hack = ut.get_argval('--cmap-hack', type_=str, default=None)
        ncolor_hack = ut.get_argval('--ncolor-hack', type_=int, default=None)
        if cmap_hack is not None:
            choices = [cmap_hack]
        if ncolor_hack is not None:
            N = ncolor_hack
            N_ = N
        seed = sum(list(map(ord, ut.hashstr27(cmap_seed))))
        rng = np.random.RandomState(seed + 48930)
        cmap_str = rng.choice(choices, 1)[0]
        #print('cmap_str = %r' % (cmap_str,))
        cmap = pt.plt.cm.get_cmap(cmap_str)
        #ut.hashstr27(cmap_seed)
        #cmap_seed = 0
        #pass
        jitter = (rng.randn(N) / (rng.randn(100).max() / 2)).clip(-1, 1) * ((1 / (N ** 2)))
        range_ = np.linspace(0, 1, N, endpoint=False)
        #print('range_ = %r' % (range_,))
        range_ = range_ + jitter
        #print('range_ = %r' % (range_,))
        while not (np.all(range_ >= 0) and np.all(range_ <= 1)):
            range_[range_ < 0] = np.abs(range_[range_ < 0] )
            range_[range_ > 1] = 2 - range_[range_ > 1]
        #print('range_ = %r' % (range_,))
        shift = rng.rand()
        range_ = (range_ + shift) % 1
        #print('jitter = %r' % (jitter,))
        #print('shift = %r' % (shift,))
        #print('range_ = %r' % (range_,))
        if ncolor_hack is not None:
            range_ = range_[0:N_]
        RGB_tuples = list(map(tuple, cmap(range_)))
    else:
        sat = brightness
        val = brightness
        hmin, hmax = hue_range
        if remove_yellow:
            hue_skips = [(.13, .24)]
        else:
            hue_skips = []
        hue_skip_ranges = [_[1] - _[0] for _ in hue_skips]
        total_skip = sum(hue_skip_ranges)
        hmax_ = hmax - total_skip
        hue_list = np.linspace(hmin, hmax_, N, endpoint=False, dtype=np.float)
        # Remove colors (like hard to see yellows) in specified ranges
        for skip, range_ in zip(hue_skips, hue_skip_ranges):
            hue_list = [hue if hue <= skip[0] else hue + range_ for hue in hue_list]
        HSV_tuples = [(hue, sat, val) for hue in hue_list]
        RGB_tuples = [colorsys.hsv_to_rgb(*x) for x in HSV_tuples]
    if randomize:
        ut.deterministic_shuffle(RGB_tuples)
    return RGB_tuples
Ejemplo n.º 4
0
def draw_roc_curve(
    fpr,
    tpr,
    fnum=None,
    pnum=None,
    marker="-",
    target_tpr=None,
    target_fpr=None,
    thresholds=None,
    color=None,
    show_operating_point=False,
):
    r"""
    Args:
        fpr (?):
        tpr (?):
        fnum (int):  figure number(default = None)
        pnum (tuple):  plot number(default = None)
        marker (str): (default = '-x')
        target_tpr (None): (default = None)
        target_fpr (None): (default = None)
        thresholds (None): (default = None)
        color (None): (default = None)
        show_operating_point (bool): (default = False)

    CommandLine:
        python -m vtool.confusion --exec-draw_roc_curve --show --lightbg

    Example:
        >>> # DISABLE_DOCTEST
        >>> from vtool.confusion import *  # NOQA
        >>> scores, labels = testdata_scores_labels()
        >>> confusions = get_confusion_metrics(scores, labels)
        >>> fpr = confusions.fpr
        >>> tpr = confusions.tpr
        >>> thresholds = confusions.thresholds
        >>> fnum = None
        >>> pnum = None
        >>> marker = '-x'
        >>> target_tpr = .85
        >>> target_fpr = None
        >>> color = None
        >>> show_operating_point = True
        >>> draw_roc_curve(fpr, tpr, fnum, pnum, marker, target_tpr, target_fpr,
        >>>   thresholds, color, show_operating_point)
        >>> ut.show_if_requested()
    """
    import plottool as pt

    if fnum is None:
        fnum = pt.next_fnum()

    if color is None:
        color = (0.4, 1.0, 0.4) if pt.is_default_dark_bg() else (0.1, 0.4, 0.4)

    roc_auc = sklearn.metrics.auc(fpr, tpr)

    title_suffix = ""

    if target_fpr is not None:
        # func = scipy.interpolate.interp1d(fpr, tpr, kind='linear', assume_sorted=False)
        # func = scipy.interpolate.interp1d(xdata, ydata, kind='nearest', assume_sorted=False)
        # interp_vals[interp_mask] = func(pt[interp_mask])
        target_fpr = np.clip(target_fpr, 0, 1)
        interp_tpr = interpolate_replbounds(fpr, tpr, target_fpr)
        choice_tpr = interp_tpr
        choice_fpr = target_fpr
    elif target_tpr is not None:
        target_tpr = np.clip(target_tpr, 0, 1)
        interp_fpr = interpolate_replbounds(tpr, fpr, target_tpr)
        choice_tpr = target_tpr
        choice_fpr = interp_fpr
    else:
        choice_tpr = None
        choice_fpr = None

    if choice_fpr is not None:
        choice_thresh = 0
        if thresholds is not None:
            try:
                index = np.nonzero(tpr >= choice_tpr)[0][0]
            except IndexError:
                index = len(thresholds) - 1
            choice_thresh = thresholds[index]
        # percent = ut.scalar_str(choice_tpr * 100).split('.')[0]
        # title_suffix = ', FPR%s=%05.2f%%' % (percent, choice_fpr)
        title_suffix = ""
        if show_operating_point:
            title_suffix = ", fpr=%.2f, tpr=%.2f, thresh=%.2f" % (choice_fpr, choice_tpr, choice_thresh)
    else:
        title_suffix = ""

    # if recall_domain is None:
    #    ave_p = np.nan
    # else:
    #    ave_p = p_interp.sum() / p_interp.size
    title = "Receiver operating characteristic\n" + "AUC=%.3f" % (roc_auc,)
    title += title_suffix

    pt.plot2(
        fpr,
        tpr,
        marker=marker,
        x_label="False Positive Rate",
        y_label="True Positive Rate",
        unitbox=True,
        flipx=False,
        color=color,
        fnum=fnum,
        pnum=pnum,
        title=title,
    )

    if False:
        # Interp does not work right because of duplicate values
        # in xdomain
        line_ = np.linspace(0.11, 0.9, 20)
        # np.append([np.inf], np.diff(fpr)) > 0
        # np.append([np.inf], np.diff(tpr)) > 0
        unique_tpr_idxs = np.nonzero(np.append([np.inf], np.diff(tpr)) > 0)[0]
        unique_fpr_idxs = np.nonzero(np.append([np.inf], np.diff(fpr)) > 0)[0]

        pt.plt.plot(line_, interpolate_replbounds(fpr[unique_fpr_idxs], tpr[unique_fpr_idxs], line_), "b-x")
        pt.plt.plot(interpolate_replbounds(tpr[unique_tpr_idxs], fpr[unique_tpr_idxs], line_), line_, "r-x")
    if choice_fpr is not None:
        pt.plot(choice_fpr, choice_tpr, "o", color=pt.PINK)
Ejemplo n.º 5
0
def draw_roc_curve(fpr,
                   tpr,
                   fnum=None,
                   pnum=None,
                   marker='-',
                   target_tpr=None,
                   target_fpr=None,
                   thresholds=None,
                   color=None,
                   show_operating_point=False):
    r"""
    Args:
        fpr (?):
        tpr (?):
        fnum (int):  figure number(default = None)
        pnum (tuple):  plot number(default = None)
        marker (str): (default = '-x')
        target_tpr (None): (default = None)
        target_fpr (None): (default = None)
        thresholds (None): (default = None)
        color (None): (default = None)
        show_operating_point (bool): (default = False)

    CommandLine:
        python -m vtool.confusion --exec-draw_roc_curve --show --lightbg

    Example:
        >>> # DISABLE_DOCTEST
        >>> from vtool.confusion import *  # NOQA
        >>> scores, labels = testdata_scores_labels()
        >>> confusions = get_confusion_metrics(scores, labels)
        >>> fpr = confusions.fpr
        >>> tpr = confusions.tpr
        >>> thresholds = confusions.thresholds
        >>> fnum = None
        >>> pnum = None
        >>> marker = '-x'
        >>> target_tpr = .85
        >>> target_fpr = None
        >>> color = None
        >>> show_operating_point = True
        >>> draw_roc_curve(fpr, tpr, fnum, pnum, marker, target_tpr, target_fpr,
        >>>   thresholds, color, show_operating_point)
        >>> ut.show_if_requested()
    """
    import plottool as pt
    if fnum is None:
        fnum = pt.next_fnum()

    if color is None:
        color = (0.4, 1.0, 0.4) if pt.is_default_dark_bg() else (0.1, 0.4, 0.4)

    roc_auc = sklearn.metrics.auc(fpr, tpr)

    title_suffix = ''

    if target_fpr is not None:
        #func = scipy.interpolate.interp1d(fpr, tpr, kind='linear', assume_sorted=False)
        #func = scipy.interpolate.interp1d(xdata, ydata, kind='nearest', assume_sorted=False)
        #interp_vals[interp_mask] = func(pt[interp_mask])
        target_fpr = np.clip(target_fpr, 0, 1)
        interp_tpr = interpolate_replbounds(fpr, tpr, target_fpr)
        choice_tpr = interp_tpr
        choice_fpr = target_fpr
    elif target_tpr is not None:
        target_tpr = np.clip(target_tpr, 0, 1)
        interp_fpr = interpolate_replbounds(tpr, fpr, target_tpr)
        choice_tpr = target_tpr
        choice_fpr = interp_fpr
    else:
        choice_tpr = None
        choice_fpr = None

    if choice_fpr is not None:
        choice_thresh = 0
        if thresholds is not None:
            try:
                index = np.nonzero(tpr >= choice_tpr)[0][0]
            except IndexError:
                index = len(thresholds) - 1
            choice_thresh = thresholds[index]
        #percent = ut.scalar_str(choice_tpr * 100).split('.')[0]
        #title_suffix = ', FPR%s=%05.2f%%' % (percent, choice_fpr)
        title_suffix = ''
        if show_operating_point:
            title_suffix = ', fpr=%.2f, tpr=%.2f, thresh=%.2f' % (
                choice_fpr, choice_tpr, choice_thresh)
    else:
        title_suffix = ''

    #if recall_domain is None:
    #    ave_p = np.nan
    #else:
    #    ave_p = p_interp.sum() / p_interp.size
    title = 'Receiver operating characteristic\n' + 'AUC=%.3f' % (roc_auc, )
    title += title_suffix

    pt.plot2(fpr,
             tpr,
             marker=marker,
             x_label='False Positive Rate',
             y_label='True Positive Rate',
             unitbox=True,
             flipx=False,
             color=color,
             fnum=fnum,
             pnum=pnum,
             title=title)

    if False:
        # Interp does not work right because of duplicate values
        # in xdomain
        line_ = np.linspace(.11, .9, 20)
        #np.append([np.inf], np.diff(fpr)) > 0
        #np.append([np.inf], np.diff(tpr)) > 0
        unique_tpr_idxs = np.nonzero(np.append([np.inf], np.diff(tpr)) > 0)[0]
        unique_fpr_idxs = np.nonzero(np.append([np.inf], np.diff(fpr)) > 0)[0]

        pt.plt.plot(
            line_,
            interpolate_replbounds(fpr[unique_fpr_idxs], tpr[unique_fpr_idxs],
                                   line_), 'b-x')
        pt.plt.plot(
            interpolate_replbounds(tpr[unique_tpr_idxs], fpr[unique_tpr_idxs],
                                   line_), line_, 'r-x')
    if choice_fpr is not None:
        pt.plot(choice_fpr, choice_tpr, 'o', color=pt.PINK)