Example #1
0
def draw_scatterplot(figdir, ibs, datax, datay, xlabel, ylabel, color, fnum=None):
    from plottool import df2
    datac = [color for _ in range(len(datax))]
    assert len(datax) == len(datay), '%r %r' % (len(datax), len(datay))
    df2.figure(fnum=fnum, doclf=True, docla=True)
    df2.plt.scatter(datax, datay,  c=datac, s=20, marker='o', alpha=.9)
    ax = df2.gca()
    title = '%s vs %s.\nnWords=%r. db=%r' % (xlabel, ylabel, len(datax), ibs.get_dbname())
    df2.set_xlabel(xlabel)
    df2.set_ylabel(ylabel)
    ax.set_ylim(min(datay) - 1, max(datay) + 1)
    ax.set_xlim(min(datax) - 1, max(datax) + 1)
    df2.dark_background()
    df2.set_figtitle(title)
    figpath = join(figdir, title)
    df2.save_figure(fnum, figpath)
Example #2
0
def draw_scatterplot(figdir,
                     ibs,
                     datax,
                     datay,
                     xlabel,
                     ylabel,
                     color,
                     fnum=None):
    from plottool import df2
    datac = [color for _ in range(len(datax))]
    assert len(datax) == len(datay), '%r %r' % (len(datax), len(datay))
    df2.figure(fnum=fnum, doclf=True, docla=True)
    df2.plt.scatter(datax, datay, c=datac, s=20, marker='o', alpha=.9)
    ax = df2.gca()
    title = '%s vs %s.\nnWords=%r. db=%r' % (xlabel, ylabel, len(datax),
                                             ibs.get_dbname())
    df2.set_xlabel(xlabel)
    df2.set_ylabel(ylabel)
    ax.set_ylim(min(datay) - 1, max(datay) + 1)
    ax.set_xlim(min(datax) - 1, max(datax) + 1)
    df2.dark_background()
    df2.set_figtitle(title)
    figpath = join(figdir, title)
    df2.save_figure(fnum, figpath)
Example #3
0
def plot_chip_metric(ibs,
                     aid,
                     metric=None,
                     fnum=1,
                     lbl='',
                     figtitle='',
                     colortype='score',
                     darken=.5,
                     cmap_='hot',
                     reverse_cmap=False,
                     **kwargs):
    """
    Plots one annotation with one metric.

    The word metric is used liberally.

    Example:
        >>> from ibeis.algo.hots.smk.smk_plots import *  # NOQA
        >>> from ibeis.algo.hots.smk import smk_debug
        >>> from ibeis.algo.hots.smk import smk_plots
        >>> from ibeis.algo.hots.smk import smk_repr
        >>> #tup = smk_debug.testdata_raw_internals0(db='GZ_ALL', nWords=64000)
        >>> #tup = smk_debug.testdata_raw_internals0(db='GZ_ALL', nWords=8000)
        >>> #tup = smk_debug.testdata_raw_internals0(db='PZ_Master0', nWords=64000)
        >>> tup = smk_debug.testdata_raw_internals0(db='PZ_Mothers', nWords=8000)
        >>> ibs, annots_df, daids, qaids, invindex, qreq_ = tup
        >>> smk_repr.compute_data_internals_(invindex, qreq_.qparams, delete_rawvecs=False)
        >>> invindex.idx2_wxs = np.array(invindex.idx2_wxs)
        >>> metric = None
        >>> aid = 1
        >>> fnum = 0
        >>> lbl='test'
        >>> colortype='score'
        >>> kwargs = {'annote': False}
        #>>> df2.rrr()
        >>> smk_plots.plot_chip_metric(ibs, aid, metric, fnum, lbl, colortype, **kwargs)
        >>> df2.present()
    """
    import plottool.draw_func2 as df2
    from ibeis.viz import viz_chip
    df2.figure(fnum=fnum, doclf=True, docla=True)
    if metric is not None:
        if colortype == 'score':
            colors = df2.scores_to_color(metric,
                                         cmap_=cmap_,
                                         reverse_cmap=reverse_cmap)
        elif colortype == 'label':
            colors = df2.label_to_colors(metric)
        elif colortype == 'custom':
            # Give ranks of -1 and -2 special meaning
            val2_customcolor = {
                -1: df2.UNKNOWN_PURP,
                -2: df2.LIGHT_BLUE,
            }
            # Inconsistent but visable colors
            scale_max = .7
            #consistent colors (needs to know highest K)
            #maxval = np.array(metric).max()
            #scale_max = .7 * (float(maxval) / 20.0)
            colors = df2.scores_to_color(metric,
                                         cmap_=cmap_,
                                         reverse_cmap=reverse_cmap,
                                         scale_max=scale_max,
                                         val2_customcolor=val2_customcolor)
        else:
            raise ValueError('no known colortype = %r' % (colortype, ))
    else:
        colors = 'distinct'
    viz_chip.show_chip(
        ibs,
        aid,
        color=colors,
        darken=darken,
        ell_alpha=.8,
        #ell_linewidth=4,
        ell_linewidth=2,
        **kwargs)
    df2.set_figtitle(figtitle)
    if metric is not None:
        cb = df2.colorbar(metric, colors, custom=(colortype == 'custom'))
        cb.set_label(lbl)
Example #4
0
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath):
    """
    Builds mosaics of patches assigned to words in sample
    ouptuts them to disk
    """
    from plottool import draw_func2 as df2
    import vtool as vt
    import parse

    vocabdir = join(figdir, 'vocab_patches2')
    ut.ensuredir(vocabdir)
    dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics)

    # COLLECTING PART --- collects patches in word folders
    #vocabdir

    seldpath = vocabdir + '_selected'
    ut.ensurepath(seldpath)
    # stack for show
    for wx, dpath in ut.progiter(six.iteritems(wx2_dpath),
                                 lbl='Dumping Word Images:',
                                 num=len(wx2_dpath),
                                 freq=1,
                                 backspace=False):
        #df2.rrr()
        fpath_list = ut.ls(dpath)
        fname_list = [basename(fpath_) for fpath_ in fpath_list]
        patch_list = [vt.imread(fpath_) for fpath_ in fpath_list]
        # color each patch by nid
        nid_list = [
            int(parse.parse('{}_nid={nid}_{}', fname)['nid'])
            for fname in fname_list
        ]
        nid_set = set(nid_list)
        nid_list = np.array(nid_list)
        if len(nid_list) == len(nid_set):
            # no duplicate names
            newpatch_list = patch_list
        else:
            # duplicate names. do coloring
            sortx = nid_list.argsort()
            patch_list = np.array(patch_list, dtype=object)[sortx]
            fname_list = np.array(fname_list, dtype=object)[sortx]
            nid_list = nid_list[sortx]
            colors = (255 *
                      np.array(df2.distinct_colors(len(nid_set)))).astype(
                          np.int32)
            color_dict = dict(zip(nid_set, colors))
            wpad, hpad = 3, 3
            newshape_list = [
                tuple(
                    (np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist())
                for patch in patch_list
            ]
            color_list = [color_dict[nid_] for nid_ in nid_list]
            newpatch_list = [
                np.zeros(shape) + color[None, None]
                for shape, color in zip(newshape_list, color_list)
            ]
            for patch, newpatch in zip(patch_list, newpatch_list):
                newpatch[wpad:-wpad, hpad:-hpad, :] = patch
            #img_list = patch_list
            #bigpatch = vt.stack_image_recurse(patch_list)
        #bigpatch = vt.stack_image_list(patch_list, vert=False)
        bigpatch = vt.stack_square_images(newpatch_list)
        bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png')

        #
        def _dictstr(dict_):
            str_ = ut.dict_str(dict_, newlines=False)
            str_ = str_.replace('\'', '').replace(': ', '=').strip('{},')
            return str_

        figtitle = '\n'.join([
            'wx=%r' % wx,
            'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]),
            'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]),
        ])
        metrics.wx2_nMembers[wx]

        df2.figure(fnum=1, doclf=True, docla=True)
        fig, ax = df2.imshow(bigpatch, figtitle=figtitle)
        #fig.show()
        df2.set_figtitle(figtitle)
        df2.adjust_subplots(top=.878, bottom=0)
        df2.save_figure(1, bigpatch_fpath)
Example #5
0
def plot_chip_metric(ibs, aid, metric=None, fnum=1, lbl='', figtitle='', colortype='score',
                     darken=.5, cmap_='hot', reverse_cmap=False, **kwargs):
    """
    Plots one annotation with one metric.

    The word metric is used liberally.

    Example:
        >>> from ibeis.algo.hots.smk.smk_plots import *  # NOQA
        >>> from ibeis.algo.hots.smk import smk_debug
        >>> from ibeis.algo.hots.smk import smk_plots
        >>> from ibeis.algo.hots.smk import smk_repr
        >>> #tup = smk_debug.testdata_raw_internals0(db='GZ_ALL', nWords=64000)
        >>> #tup = smk_debug.testdata_raw_internals0(db='GZ_ALL', nWords=8000)
        >>> #tup = smk_debug.testdata_raw_internals0(db='PZ_Master0', nWords=64000)
        >>> tup = smk_debug.testdata_raw_internals0(db='PZ_Mothers', nWords=8000)
        >>> ibs, annots_df, daids, qaids, invindex, qreq_ = tup
        >>> smk_repr.compute_data_internals_(invindex, qreq_.qparams, delete_rawvecs=False)
        >>> invindex.idx2_wxs = np.array(invindex.idx2_wxs)
        >>> metric = None
        >>> aid = 1
        >>> fnum = 0
        >>> lbl='test'
        >>> colortype='score'
        >>> kwargs = {'annote': False}
        #>>> df2.rrr()
        >>> smk_plots.plot_chip_metric(ibs, aid, metric, fnum, lbl, colortype, **kwargs)
        >>> df2.present()
    """
    import plottool.draw_func2 as df2
    from ibeis.viz import viz_chip
    df2.figure(fnum=fnum, doclf=True, docla=True)
    if metric is not None:
        if  colortype == 'score':
            colors = df2.scores_to_color(metric, cmap_=cmap_, reverse_cmap=reverse_cmap)
        elif colortype == 'label':
            colors = df2.label_to_colors(metric)
        elif colortype == 'custom':
            # Give ranks of -1 and -2 special meaning
            val2_customcolor = {
                -1: df2.UNKNOWN_PURP,
                -2: df2.LIGHT_BLUE,
            }
            # Inconsistent but visable colors
            scale_max = .7
            #consistent colors (needs to know highest K)
            #maxval = np.array(metric).max()
            #scale_max = .7 * (float(maxval) / 20.0)
            colors = df2.scores_to_color(metric, cmap_=cmap_,
                                         reverse_cmap=reverse_cmap,
                                         scale_max=scale_max,
                                         val2_customcolor=val2_customcolor)
        else:
            raise ValueError('no known colortype = %r' % (colortype,))
    else:
        colors = 'distinct'
    viz_chip.show_chip(ibs, aid, color=colors, darken=darken,
                       ell_alpha=.8,
                       #ell_linewidth=4,
                       ell_linewidth=2,
                       **kwargs)
    df2.set_figtitle(figtitle)
    if metric is not None:
        cb = df2.colorbar(metric, colors, custom=(colortype == 'custom'))
        cb.set_label(lbl)
Example #6
0
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath):
    """
    Builds mosaics of patches assigned to words in sample
    ouptuts them to disk
    """
    from plottool import draw_func2 as df2
    import vtool as vt
    import parse

    vocabdir = join(figdir, 'vocab_patches2')
    ut.ensuredir(vocabdir)
    dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics)

    # COLLECTING PART --- collects patches in word folders
    #vocabdir

    seldpath = vocabdir + '_selected'
    ut.ensurepath(seldpath)
    # stack for show
    for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False):
        #df2.rrr()
        fpath_list = ut.ls(dpath)
        fname_list = [basename(fpath_) for fpath_ in fpath_list]
        patch_list = [gtool.imread(fpath_) for fpath_ in fpath_list]
        # color each patch by nid
        nid_list = [int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list]
        nid_set = set(nid_list)
        nid_list = np.array(nid_list)
        if len(nid_list) == len(nid_set):
            # no duplicate names
            newpatch_list = patch_list
        else:
            # duplicate names. do coloring
            sortx = nid_list.argsort()
            patch_list = np.array(patch_list, dtype=object)[sortx]
            fname_list = np.array(fname_list, dtype=object)[sortx]
            nid_list = nid_list[sortx]
            colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype(np.int32)
            color_dict = dict(zip(nid_set, colors))
            wpad, hpad = 3, 3
            newshape_list = [tuple((np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list]
            color_list = [color_dict[nid_] for nid_ in nid_list]
            newpatch_list = [np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list)]
            for patch, newpatch in zip(patch_list, newpatch_list):
                newpatch[wpad:-wpad, hpad:-hpad, :] = patch
            #img_list = patch_list
            #bigpatch = vt.stack_image_recurse(patch_list)
        #bigpatch = vt.stack_image_list(patch_list, vert=False)
        bigpatch = vt.stack_square_images(newpatch_list)
        bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png')

        #
        def _dictstr(dict_):
            str_ = ut.dict_str(dict_, newlines=False)
            str_ = str_.replace('\'', '').replace(': ', '=').strip('{},')
            return str_

        figtitle = '\n'.join([
            'wx=%r' % wx,
            'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]),
            'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]),
        ])
        metrics.wx2_nMembers[wx]

        df2.figure(fnum=1, doclf=True, docla=True)
        fig, ax = df2.imshow(bigpatch, figtitle=figtitle)
        #fig.show()
        df2.set_figtitle(figtitle)
        df2.adjust_subplots(top=.878, bottom=0)
        df2.save_figure(1, bigpatch_fpath)