def show_chip_distinctiveness_plot(chip, kpts, dstncvs, fnum=1, pnum=None):
    import plottool as pt
    pt.figure(fnum, pnum=pnum)
    ax = pt.gca()
    divider = pt.ensure_divider(ax)
    #ax1 = divider.append_axes("left", size="50%", pad=0)
    ax1 = ax
    ax2 = divider.append_axes("bottom", size="100%", pad=0.05)
    #f, (ax1, ax2) = pt.plt.subplots(1, 2, sharex=True)
    cmapstr = 'rainbow'  # 'hot'
    color_list = pt.df2.plt.get_cmap(cmapstr)(ut.norm_zero_one(dstncvs))
    sortx = dstncvs.argsort()
    #pt.df2.plt.plot(qfx2_dstncvs[sortx], c=color_list[sortx])
    pt.plt.sca(ax1)
    pt.colorline(np.arange(len(sortx)), dstncvs[sortx],
                 cmap=pt.plt.get_cmap(cmapstr))
    pt.gca().set_xlim(0, len(sortx))
    pt.dark_background()
    pt.plt.sca(ax2)
    pt.imshow(chip, darken=.2)
    # MATPLOTLIB BUG CANNOT SHOW DIFFERENT ALPHA FOR POINTS AND KEYPOINTS AT ONCE
    #pt.draw_kpts2(kpts, pts_color=color_list, ell_color=color_list, ell_alpha=.1, ell=True, pts=True)
    #pt.draw_kpts2(kpts, color_list=color_list, pts_alpha=1.0, pts_size=1.5,
    #              ell=True, ell_alpha=.1, pts=False)
    ell = ut.get_argflag('--ell')
    pt.draw_kpts2(kpts, color_list=color_list, pts_alpha=1.0, pts_size=1.5,
                  ell=ell, ell_alpha=.3, pts=not ell)
    pt.plt.sca(ax)
Beispiel #2
0
def testshow_extramargin_info(ibs, aid_list, arg_list, newsize_list, halfoffset_cs_list):
    #cfpath, gfpath, bbox, theta, new_size, filter_list = tup
    # TEMP TESTING
    from vtool import chip as ctool
    import plottool as pt
    import vtool as vt
    from ibeis.viz import viz_chip

    index = 0
    cfpath, gfpath, expanded_bbox, theta, expanded_new_size, filter_list = arg_list[index]
    expanded_chipBGR = ctool.compute_chip(gfpath, expanded_bbox, theta, expanded_new_size, filter_list)
    bbox_cs_list = [
        (xo_pcs, yo_pcs, w_pcs, h_pcs)
        for (w_pcs, h_pcs), (xo_pcs, yo_pcs) in zip(newsize_list, halfoffset_cs_list)
    ]
    bbox_pcs = bbox_cs_list[index]
    aid = aid_list[0]
    #print('new_size = %r' % (new_size,))
    print('newsize_list[index] = %r' % (newsize_list[index],))

    fnum = 1
    viz_chip.show_chip(ibs, aid, pnum=(1, 3, 1), fnum=fnum, annote=False, in_image=True,
                       title_suffix='\noriginal image')
    viz_chip.show_chip(ibs, aid, pnum=(1, 3, 2), fnum=fnum, annote=False,
                       title_suffix='\noriginal chip')
    bboxed_chip = vt.draw_verts(expanded_chipBGR,
                                vt.scaled_verts_from_bbox(bbox_pcs, theta, 1, 1))
    pt.imshow(bboxed_chip, pnum=(1, 3, 3), fnum=fnum,
              title='scaled chip with expanded margin.\n(orig margin drawn in orange)')
    pt.gca().set_xlabel(str(bboxed_chip.shape))
    pt.show_if_requested()
Beispiel #3
0
def test_mcc():
    import plottool as pt
    import sklearn.metrics
    num = 100
    xdata = np.linspace(0, 1, num * 2)
    ydata = np.linspace(1, -1, num * 2)
    pt.plt.plot(xdata, ydata, '--k', label='linear')

    y_true = [1] * num + [0] * num
    y_pred = y_true[:]
    xs = []
    for i in range(0, len(y_true)):
        y_pred[-i] = 1 - y_pred[-i]
        xs.append(sklearn.metrics.matthews_corrcoef(y_true, y_pred))

    pt.plot(xdata, xs, label='change one class at a time')

    y_true = ut.flatten(zip([1] * num, [0] * num))
    y_pred = y_true[:]
    xs = []
    for i in range(0, len(y_true)):
        y_pred[-i] = 1 - y_pred[-i]
        xs.append(sklearn.metrics.matthews_corrcoef(y_true, y_pred))

    pt.plot(xdata, xs, label='change classes evenly')
    pt.gca().legend()
Beispiel #4
0
def draw_tree_model(model, **kwargs):
    import plottool as pt
    import networkx as netx
    if not ut.get_argval('--hackjunc'):
        fnum = pt.ensure_fnum(None)
        fig = pt.figure(fnum=fnum, doclf=True)  # NOQA
        ax = pt.gca()
        #name_nodes = sorted(ut.list_getattr(model.ttype2_cpds[NAME_TTYPE], 'variable'))
        netx_graph = model.to_markov_model()
        #pos = netx.pygraphviz_layout(netx_graph)
        #pos = netx.graphviz_layout(netx_graph)
        #pos = get_hacked_pos(netx_graph, name_nodes, prog='neato')
        pos = netx.nx_pydot.pydot_layout(netx_graph)
        node_color = [pt.WHITE] * len(pos)
        drawkw = dict(pos=pos,
                      ax=ax,
                      with_labels=True,
                      node_color=node_color,
                      node_size=1100)
        netx.draw(netx_graph, **drawkw)
        if kwargs.get('show_title', True):
            pt.set_figtitle('Markov Model')

    if not ut.get_argval('--hackmarkov'):
        fnum = pt.ensure_fnum(None)
        fig = pt.figure(fnum=fnum, doclf=True)  # NOQA
        ax = pt.gca()
        netx_graph = model.to_junction_tree()

        # prettify nodes
        def fixtupkeys(dict_):
            return {
                ', '.join(k) if isinstance(k, tuple) else k: fixtupkeys(v)
                for k, v in dict_.items()
            }

        # FIXME
        n = fixtupkeys(netx_graph.node)
        e = fixtupkeys(netx_graph.edge)
        a = fixtupkeys(netx_graph.adj)
        netx_graph.nodes.update(n)
        netx_graph.edges.update(e)
        netx_graph.adj.update(a)
        #netx_graph = model.to_markov_model()
        #pos = netx.pygraphviz_layout(netx_graph)
        #pos = netx.graphviz_layout(netx_graph)
        pos = netx.nx_pydot.pydot_layout(netx_graph)
        node_color = [pt.WHITE] * len(pos)
        drawkw = dict(pos=pos,
                      ax=ax,
                      with_labels=True,
                      node_color=node_color,
                      node_size=2000)
        netx.draw(netx_graph, **drawkw)
        if kwargs.get('show_title', True):
            pt.set_figtitle('Junction/Clique Tree / Cluster Graph')
Beispiel #5
0
def testshow_extramargin_info(ibs, aid_list, arg_list, newsize_list,
                              halfoffset_cs_list):
    #cfpath, gfpath, bbox, theta, new_size, filter_list = tup
    # TEMP TESTING
    from vtool import chip as ctool
    import plottool as pt
    import vtool as vt
    from ibeis.viz import viz_chip

    index = 0
    cfpath, gfpath, expanded_bbox, theta, expanded_new_size, filter_list = arg_list[
        index]
    expanded_chipBGR = ctool.compute_chip(gfpath, expanded_bbox, theta,
                                          expanded_new_size, filter_list)
    bbox_cs_list = [
        (xo_pcs, yo_pcs, w_pcs, h_pcs)
        for (w_pcs, h_pcs), (xo_pcs,
                             yo_pcs) in zip(newsize_list, halfoffset_cs_list)
    ]
    bbox_pcs = bbox_cs_list[index]
    aid = aid_list[0]
    #print('new_size = %r' % (new_size,))
    print('newsize_list[index] = %r' % (newsize_list[index], ))

    fnum = 1
    viz_chip.show_chip(ibs,
                       aid,
                       pnum=(1, 3, 1),
                       fnum=fnum,
                       annote=False,
                       in_image=True,
                       title_suffix='\noriginal image')
    viz_chip.show_chip(ibs,
                       aid,
                       pnum=(1, 3, 2),
                       fnum=fnum,
                       annote=False,
                       title_suffix='\noriginal chip')
    bboxed_chip = vt.draw_verts(
        expanded_chipBGR, vt.scaled_verts_from_bbox(bbox_pcs, theta, 1, 1))
    pt.imshow(
        bboxed_chip,
        pnum=(1, 3, 3),
        fnum=fnum,
        title='scaled chip with expanded margin.\n(orig margin drawn in orange)'
    )
    pt.gca().set_xlabel(str(bboxed_chip.shape))
    pt.show_if_requested()
Beispiel #6
0
        def plot(self, fnum, pnum):
            self.update_netx_graph()
            #if split_check:

            #{self.infr.model.graph}
            if split_check:
                layoutkw = dict(prog='neato', splines='spline', sep=10 / 72)
                self.plotinfo = pt.show_nx(self.infr.model.graph,
                                           as_directed=False,
                                           fnum=self.fnum,
                                           layoutkw=layoutkw,
                                           use_image=self.use_image,
                                           verbose=0)
                #ax = pt.gca()
                #pt.zoom_factory()
            else:
                self.plotinfo = viz_netx_chipgraph(self.ibs,
                                                   self.graph,
                                                   fnum=self.fnum,
                                                   use_image=self.use_image,
                                                   **kwargs)
            ax = pt.gca()
            self.enable_pan_and_zoom(ax)
            ax.autoscale()

            for aid in self.selected_aids:
                self.highlight_aid(aid)

            self.make_hud()
Beispiel #7
0
 def _plotarrow(x, y, dx, dy, color=pt.BLUE, label=''):
     ax = pt.gca()
     arrowargs = dict(head_width=.5, length_includes_head=True, label=label)
     arrow = mpl.patches.FancyArrow(x, y, dx, dy, **arrowargs)
     arrow.set_edgecolor(color)
     arrow.set_facecolor(color)
     ax.add_patch(arrow)
Beispiel #8
0
        def plot(self, fnum, pnum):
            self.update_netx_graph()
            #if split_check:

            #{self.infr.model.graph}
            if split_check:
                layoutkw = dict(prog='neato', splines='spline', sep=10 / 72)
                self.plotinfo = pt.show_nx(self.infr.model.graph,
                                           as_directed=False, fnum=self.fnum,
                                           layoutkw=layoutkw,
                                           use_image=self.use_image, verbose=0)
                #ax = pt.gca()
                #pt.zoom_factory()
            else:
                self.plotinfo = viz_netx_chipgraph(self.ibs, self.graph,
                                                   fnum=self.fnum,
                                                   use_image=self.use_image,
                                                   **kwargs)
            ax = pt.gca()
            self.enable_pan_and_zoom(ax)
            ax.autoscale()

            for aid in self.selected_aids:
                self.highlight_aid(aid)

            self.make_hud()
Beispiel #9
0
    def show_page(self):
        if self.fig is None:
            raise AssertionError('fig is None, did you run interction.start()?')
        import plottool as pt
        fig = ih.begin_interaction('expandable', self.fnum)
        if not any(self.pnum_list) and self.nRows is None and self.nRows is None:
            # Hack if no pnum was given
            self.nRows, self.nCols = pt.get_num_rc(len(self.pnum_list),
                                                   nRows=self.nRows,
                                                   nCols=self.nCols)
            nSubplots = len(self.func_list)
            pnum_ = pt.make_pnum_nextgen(self.nRows, self.nCols, nSubplots=nSubplots)
            self.pnum_list = [pnum_() for _ in self.pnum_list]

        for index, (pnum, func) in enumerate(zip(self.pnum_list, self.func_list)):
            if check_if_subinteract(func):
                # Hack
                interclass = func
                interclass.static_plot(fnum=self.fnum, pnum=pnum)
            elif hasattr(func, 'plot'):
                inter = func
                inter.plot(fnum=self.fnum, pnum=pnum)
            else:
                func(fnum=self.fnum, pnum=pnum)
            ax = pt.gca()
            pt.set_plotdat(ax, 'plot_func', func)
            pt.set_plotdat(ax, 'expandable_index', index)
        #if self.interactive is None or self.interactive:
        #    ih.connect_callback(fig, 'button_press_event', self.onclick)
        self.connect_callbacks()
        self.fig = fig
        return fig
Beispiel #10
0
def draw_junction_tree(model, fnum=None, **kwargs):
    import plottool as pt
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum)
    ax = pt.gca()
    from pgmpy.models import JunctionTree
    if not isinstance(model, JunctionTree):
        netx_graph = model.to_junction_tree()
    else:
        netx_graph = model
    # prettify nodes
    def fixtupkeys(dict_):
        return {
            ', '.join(k) if isinstance(k, tuple) else k: fixtupkeys(v)
            for k, v in dict_.items()
        }
    n = fixtupkeys(netx_graph.node)
    e = fixtupkeys(netx_graph.edge)
    a = fixtupkeys(netx_graph.adj)
    netx_graph.node = n
    netx_graph.edge = e
    netx_graph.adj = a
    #netx_graph = model.to_markov_model()
    #pos = netx.pygraphviz_layout(netx_graph)
    #pos = netx.graphviz_layout(netx_graph)
    pos = netx.pydot_layout(netx_graph)
    node_color = [pt.NEUTRAL] * len(pos)
    drawkw = dict(pos=pos, ax=ax, with_labels=True, node_color=node_color,
                  node_size=2000)
    netx.draw(netx_graph, **drawkw)
    if kwargs.get('show_title', True):
        pt.set_figtitle('Junction / Clique Tree / Cluster Graph')
Beispiel #11
0
def draw_tree_model(model, **kwargs):
    import plottool as pt
    import networkx as netx
    if not ut.get_argval('--hackjunc'):
        fnum = pt.ensure_fnum(None)
        fig = pt.figure(fnum=fnum, doclf=True)  # NOQA
        ax = pt.gca()
        #name_nodes = sorted(ut.list_getattr(model.ttype2_cpds['name'], 'variable'))
        netx_graph = model.to_markov_model()
        #pos = netx.pygraphviz_layout(netx_graph)
        #pos = netx.graphviz_layout(netx_graph)
        #pos = get_hacked_pos(netx_graph, name_nodes, prog='neato')
        pos = netx.pydot_layout(netx_graph)
        node_color = [pt.WHITE] * len(pos)
        drawkw = dict(pos=pos, ax=ax, with_labels=True, node_color=node_color,
                      node_size=1100)
        netx.draw(netx_graph, **drawkw)
        if kwargs.get('show_title', True):
            pt.set_figtitle('Markov Model')

    if not ut.get_argval('--hackmarkov'):
        fnum = pt.ensure_fnum(None)
        fig = pt.figure(fnum=fnum, doclf=True)  # NOQA
        ax = pt.gca()
        netx_graph = model.to_junction_tree()
        # prettify nodes
        def fixtupkeys(dict_):
            return {
                ', '.join(k) if isinstance(k, tuple) else k: fixtupkeys(v)
                for k, v in dict_.items()
            }
        n = fixtupkeys(netx_graph.node)
        e = fixtupkeys(netx_graph.edge)
        a = fixtupkeys(netx_graph.adj)
        netx_graph.node = n
        netx_graph.edge = e
        netx_graph.adj = a
        #netx_graph = model.to_markov_model()
        #pos = netx.pygraphviz_layout(netx_graph)
        #pos = netx.graphviz_layout(netx_graph)
        pos = netx.pydot_layout(netx_graph)
        node_color = [pt.WHITE] * len(pos)
        drawkw = dict(pos=pos, ax=ax, with_labels=True, node_color=node_color,
                      node_size=2000)
        netx.draw(netx_graph, **drawkw)
        if kwargs.get('show_title', True):
            pt.set_figtitle('Junction/Clique Tree / Cluster Graph')
Beispiel #12
0
def draw_sift_on_patch(patch, sift, **kwargs):
    import plottool as pt
    pt.imshow(patch)
    ax = pt.gca()
    half_size = patch.shape[0] / 2
    invVR = np.array([[half_size, 0, half_size], [0, half_size, half_size], [0, 0, 1]])
    invVR_aff2Ds = np.array([invVR])
    sifts = np.array([sift])
    return draw_sifts(ax, sifts, invVR_aff2Ds)
Beispiel #13
0
 def static_plot(self, fnum=None, pnum=(1, 1, 1)):
     import plottool as pt
     self.ax = pt.gca()
     #self.ax.imshow(img, interpolation='nearest', alpha=1)
     #self.ax.imshow(mask, interpolation='nearest', alpha=0.6)
     pt.imshow(self.img, ax=self.ax, interpolation='nearest', alpha=1)
     pt.imshow(self.mask, ax=self.ax, interpolation='nearest', alpha=0.6)
     self.update_title()
     self.ax.grid(False)
 def static_plot(self, fnum=None, pnum=(1, 1, 1)):
     import plottool as pt
     self.ax = pt.gca()
     #self.ax.imshow(img, interpolation='nearest', alpha=1)
     #self.ax.imshow(mask, interpolation='nearest', alpha=0.6)
     pt.imshow(self.img, ax=self.ax, interpolation='nearest', alpha=1)
     pt.imshow(self.mask, ax=self.ax, interpolation='nearest', alpha=0.6)
     self.update_title()
     self.ax.grid(False)
Beispiel #15
0
def draw_sift_on_patch(patch, sift, **kwargs):
    import plottool as pt
    pt.imshow(patch)
    ax = pt.gca()
    half_size = patch.shape[0] / 2
    invVR = np.array([[half_size, 0, half_size], [0, half_size, half_size],
                      [0, 0, 1]])
    invVR_aff2Ds = np.array([invVR])
    sifts = np.array([sift])
    return draw_sifts(ax, sifts, invVR_aff2Ds)
Beispiel #16
0
        def plot(self, fnum, pnum):
            from ibeis.viz.viz_graph import viz_netx_chipgraph
            self.update_netx_graph()
            self.pos = viz_netx_chipgraph(self.ibs, self.netx_graph,
                                          fnum=self.fnum, with_images=self.with_images,
                                          zoom=zoom)
            self.ax = pt.gca()

            for aid in self.selected_aids:
                self.highlight_aid(aid)
                pass
    def plot_image(self, index):
        px = index - self.start_index
        gpath      = self.gpath_list[index]

        if self.vizkw is None:
            _vizkw = {}
        else:
            _vizkw = self.vizkw.copy()

        _vizkw.update({
            'fnum': self.fnum,
            'pnum': self.pnum_(px),
        })

        if ut.is_funclike(gpath):
            showfunc = gpath
            # HACK
            # override of plot image function
            showfunc(**_vizkw)
            import plottool as pt
            ax = pt.gca()
        else:
            if isinstance(gpath, six.string_types):
                img = vt.imread(gpath)
            else:
                img = gpath

            bbox_list  = self.bboxes_list[index]
            #print('bbox_list %r in display for px: %r ' % (bbox_list, px))
            theta_list = self.thetas_list[index]

            label_list = [ix + 1 for ix in range(len(bbox_list))]
            #Add true values for every bbox to display
            sel_list = [True for ix in range(len(bbox_list))]
            _vizkw.update({
                #title should always be the image number
                'title': str(index),
                'bbox_list'  : bbox_list,
                'theta_list' : theta_list,
                'sel_list'   : sel_list,
                'label_list' : label_list,
            })
            #print(utool.dict_str(_vizkw))
            #print('vizkw = ' + utool.dict_str(_vizkw))
            _, ax = viz_image2.show_image(img, **_vizkw)
            if self.xlabel_list is not None:
                import plottool as pt
                pt.set_xlabel(self.xlabel_list[index])
            #print(index)
            ph.set_plotdat(ax, 'bbox_list', bbox_list)
            ph.set_plotdat(ax, 'gpath', gpath)
        ph.set_plotdat(ax, 'px', str(px))
        ph.set_plotdat(ax, 'index', index)
    def plot_image(self, index):
        px = index - self.start_index
        gpath = self.gpath_list[index]

        if self.vizkw is None:
            _vizkw = {}
        else:
            _vizkw = self.vizkw.copy()

        _vizkw.update({
            'fnum': self.fnum,
            'pnum': self.pnum_(px),
        })

        if ut.is_funclike(gpath):
            showfunc = gpath
            # HACK
            # override of plot image function
            showfunc(**_vizkw)
            import plottool as pt
            ax = pt.gca()
        else:
            if isinstance(gpath, six.string_types):
                img = vt.imread(gpath)
            else:
                img = gpath

            bbox_list = self.bboxes_list[index]
            #print('bbox_list %r in display for px: %r ' % (bbox_list, px))
            theta_list = self.thetas_list[index]

            label_list = [ix + 1 for ix in range(len(bbox_list))]
            #Add true values for every bbox to display
            sel_list = [True for ix in range(len(bbox_list))]
            _vizkw.update({
                #title should always be the image number
                'title': str(index),
                'bbox_list': bbox_list,
                'theta_list': theta_list,
                'sel_list': sel_list,
                'label_list': label_list,
            })
            #print(utool.dict_str(_vizkw))
            #print('vizkw = ' + utool.dict_str(_vizkw))
            _, ax = viz_image2.show_image(img, **_vizkw)
            if self.xlabel_list is not None:
                import plottool as pt
                pt.set_xlabel(self.xlabel_list[index])
            #print(index)
            ph.set_plotdat(ax, 'bbox_list', bbox_list)
            ph.set_plotdat(ax, 'gpath', gpath)
        ph.set_plotdat(ax, 'px', str(px))
        ph.set_plotdat(ax, 'index', index)
Beispiel #19
0
def testshow_extramargin_info(gfpath, bbox_gs, theta, new_size, halfoffset_ms, mbbox_gs, margin_size):
    import plottool as pt
    import vtool as vt

    imgBGR = vt.imread(gfpath)
    chipBGR = compute_chip(gfpath, bbox_gs, theta, new_size, [])
    mchipBGR = compute_chip(gfpath, mbbox_gs, theta, margin_size, [])

    #index = 0
    w_cs, h_cs = new_size
    xo_ms, yo_ms = halfoffset_ms
    bbox_ms = [xo_ms, yo_ms, w_cs, h_cs]

    verts_gs = vt.scaled_verts_from_bbox(bbox_gs, theta, 1, 1)
    expanded_verts_gs = vt.scaled_verts_from_bbox(mbbox_gs, theta, 1, 1)
    expanded_verts_ms = vt.scaled_verts_from_bbox(bbox_ms, 0, 1, 1)
    # topheavy
    imgBGR = vt.draw_verts(imgBGR, verts_gs)
    imgBGR = vt.draw_verts(imgBGR, expanded_verts_gs)

    mchipBGR = vt.draw_verts(mchipBGR, expanded_verts_ms)

    fnum = 1
    pt.imshow(imgBGR, pnum=(1, 3, 1), fnum=fnum, title='original image')
    pt.gca().set_xlabel(str(imgBGR.shape))
    pt.imshow(chipBGR, pnum=(1, 3, 2), fnum=fnum, title='original chip')
    pt.gca().set_xlabel(str(chipBGR.shape))
    pt.imshow(mchipBGR, pnum=(1, 3, 3), fnum=fnum,
              title='scaled chip with expanded margin.\n(orig margin drawn in orange)')
    pt.gca().set_xlabel(str(mchipBGR.shape))

    pt.show_if_requested()
Beispiel #20
0
def zoom_factory(ax=None, zoomable_list=[], base_scale=1.1):
    """
    References:
        https://gist.github.com/tacaswell/3144287
        http://stackoverflow.com/questions/11551049/matplotlib-plot-zooming-with-scroll-wheel
    """
    if ax is None:
        ax = pt.gca()
    def zoom_fun(event):
        #print('zooming')
        # get the current x and y limits
        cur_xlim = ax.get_xlim()
        cur_ylim = ax.get_ylim()
        xdata = event.xdata  # get event x location
        ydata = event.ydata  # get event y location
        if xdata is None or ydata is None:
            return
        if event.button == 'up':
            # deal with zoom in
            scale_factor = 1 / base_scale
        elif event.button == 'down':
            # deal with zoom out
            scale_factor = base_scale
        else:
            raise NotImplementedError('event.button=%r' % (event.button,))
            # deal with something that should never happen
            scale_factor = 1
            print(event.button)
        for zoomable in zoomable_list:
            zoom = zoomable.get_zoom()
            new_zoom = zoom / (scale_factor ** (1.2))
            zoomable.set_zoom(new_zoom)
        # Get distance from the cursor to the edge of the figure frame
        x_left = xdata - cur_xlim[0]
        x_right = cur_xlim[1] - xdata
        y_top = ydata - cur_ylim[0]
        y_bottom = cur_ylim[1] - ydata
        ax.set_xlim([xdata - x_left * scale_factor, xdata + x_right * scale_factor])
        ax.set_ylim([ydata - y_top * scale_factor, ydata + y_bottom * scale_factor])

        # ----
        ax.figure.canvas.draw()  # force re-draw

    fig = ax.get_figure()  # get the figure of interest
    # attach the call back
    fig.canvas.mpl_connect('scroll_event', zoom_fun)

    #return the function
    return zoom_fun
Beispiel #21
0
 def show_edge(infr, edge, fnum=None, pnum=None, **kwargs):
     import plottool as pt
     match = infr._exec_pairwise_match([edge])[0]
     fnum = pt.ensure_fnum(fnum)
     pt.figure(fnum=fnum, pnum=pnum)
     ax = pt.gca()
     showkw = dict(vert=False,
                   heatmask=True,
                   show_lines=False,
                   show_ell=False,
                   show_ori=False,
                   show_eig=False,
                   modifysize=True)
     showkw.update(kwargs)
     match.show(ax, **showkw)
Beispiel #22
0
    def plot(self, fnum, pnum):
        self.infr.update_visual_attrs(self.show_cuts)

        layoutkw = dict(prog='neato', splines='spline', sep=10 / 72)

        #draw_implicit=self.show_cuts)
        self.plotinfo = pt.show_nx(
            self.infr.graph,
            as_directed=False,
            fnum=self.fnum,
            layoutkw=layoutkw,
            #node_labels=True,
            modify_ax=False,
            use_image=self.use_image,
            verbose=0)

        ut.util_graph.graph_info(self.infr.graph, verbose=True)

        #_, edge_weights, edge_colors = self.infr.get_colored_edge_weights()
        #pt.colorbar(edge_weights, edge_colors, lbl='weights')

        # _normal_ticks = np.linspace(0, 1, num=11)
        # _normal_scores = np.linspace(0, 1, num=500)
        # _normal_colors = self.infr.get_colored_weights(_normal_scores)
        # cb = pt.colorbar(_normal_scores, _normal_colors, lbl='weights',
        #                  ticklabels=_normal_ticks)

        # cb.ax.annotate('threshold',
        #                xy=(1, self.infr.thresh),
        #                xytext=(2.5, .3 if self.infr.thresh < .5 else .7),
        #                arrowprops=dict(
        #                    alpha=.5,
        #                    fc="0.6",
        #                    connectionstyle="angle3,angleA=90,angleB=0"),)

        ax = pt.gca()
        self.enable_pan_and_zoom(ax)
        #ax.autoscale()
        for aid in self.selected_aids:
            self.highlight_aid(aid, pt.ORANGE)
        #self.static_plot(fnum, pnum)
        self.make_hud()
        #print(ut.repr2(self.infr.graph.edges, nl=2))
        print('Finished Plot')
Beispiel #23
0
    def visualize(encoder):
        import plottool as pt

        # is_timedata = False
        is_timedelta = True
        p_xdata = encoder.p_xdata
        xdata_domain = encoder.xdata_domain
        # if is_timedata:
        #    xdata_domain_ = [ut.unixtime_to_datetimeobj(unixtime) for unixtime in xdata_domain]
        if is_timedelta:
            # xdata_domain_ = [ut.unixtime_to_timedelta(unixtime) for unixtime in xdata_domain]
            pass
        else:
            pass
            # xdata_domain_ = xdata_domain
        pt.plot_probabilities([p_xdata], [""], xdata=xdata_domain)
        ax = pt.gca()

        # HISTOGRAM
        if False:
            X = encoder.support["X"]
            xdata = X[~np.isnan(X)]
            n, bins, patches = pt.plt.hist(xdata, 1000)

        ax.set_xlabel("xdata")
        if is_timedelta:
            ax.set_xlabel("Time Delta")
            ax.set_title("Timedelta distribution")

            def timeTicks(x, pos):
                import datetime

                d = datetime.timedelta(seconds=x)
                return str(d)

            import matplotlib as mpl

            formatter = mpl.ticker.FuncFormatter(timeTicks)
            ax.xaxis.set_major_formatter(formatter)
            pt.gcf().autofmt_xdate()
Beispiel #24
0
    def get_popup_options(self):
        from ibeis.gui import inspect_gui
        options = []

        ax = pt.gca()  # HACK

        from plottool import plot_helpers as ph
        viztype = ph.get_plotdat(ax, 'viztype', '')
        is_match_type = viztype in ['matches', 'multi_match']

        if is_match_type:
            options += inspect_gui.get_aidpair_context_menu_options(
                self.ibs, self.qaid, self.daid, self.cm,
                qreq_=self.qreq_,
                #update_callback=self.show_page,
                #backend_callback=None, aid_list=aid_list)
            )

        options += [
            #('Toggle same_fig', self.toggle_samefig),
            #('Toggle vert', self.toggle_vert),
            ('query last feature', self.query_last_feature),
            ('show each chip', self.show_each_chip),
            ('show each distinctiveness chip', self.show_each_dstncvs_chip),
            ('show each foreground weight chip', self.show_each_fgweight_chip),
            ('show each probchip', self.show_each_probchip),
            ('show coverage', self.show_coverage),
            #('show each probchip', self.query_last_feature),
        ]

        #options.append(('name_interaction', self.name_interaction))
        #if self.H1 is not None:
        #    options.append(('Toggle homog', self.toggle_homog))
        if ut.is_developer():
            options.append(('dev_reload', self.dev_reload))
            options.append(('dev_embed', self.dev_embed))
        #options.append(('cancel', lambda: print('cancel')))
        options += super(MatchInteraction, self).get_popup_options()

        return options
Beispiel #25
0
    def visualize(encoder):
        import plottool as pt
        #is_timedata = False
        is_timedelta = True
        p_xdata = encoder.p_xdata
        xdata_domain = encoder.xdata_domain
        #if is_timedata:
        #    xdata_domain_ = [ut.unixtime_to_datetimeobj(unixtime) for unixtime in xdata_domain]
        if is_timedelta:
            #xdata_domain_ = [ut.unixtime_to_timedelta(unixtime) for unixtime in xdata_domain]
            pass
        else:
            pass
            #xdata_domain_ = xdata_domain
        pt.plot_probabilities([p_xdata], [''], xdata=xdata_domain)
        ax = pt.gca()

        # HISTOGRAM
        if False:
            X = encoder.support['X']
            xdata = X[~np.isnan(X)]
            n, bins, patches = pt.plt.hist(xdata, 1000)

        ax.set_xlabel('xdata')
        if is_timedelta:
            ax.set_xlabel('Time Delta')
            ax.set_title('Timedelta distribution')

            def timeTicks(x, pos):
                import datetime
                d = datetime.timedelta(seconds=x)
                return str(d)

            import matplotlib as mpl
            formatter = mpl.ticker.FuncFormatter(timeTicks)
            ax.xaxis.set_major_formatter(formatter)
            pt.gcf().autofmt_xdate()
Beispiel #26
0
def draw_junction_tree(model, fnum=None, **kwargs):
    import plottool as pt
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum)
    ax = pt.gca()
    from pgmpy.models import JunctionTree
    if not isinstance(model, JunctionTree):
        netx_graph = model.to_junction_tree()
    else:
        netx_graph = model
    # prettify nodes
    def fixtupkeys(dict_):
        return {
            ', '.join(k) if isinstance(k, tuple) else k: fixtupkeys(v)
            for k, v in dict_.items()
        }

    n = fixtupkeys(netx_graph.node)
    e = fixtupkeys(netx_graph.edge)
    a = fixtupkeys(netx_graph.adj)
    netx_graph.node = n
    netx_graph.edge = e
    netx_graph.adj = a
    #netx_graph = model.to_markov_model()
    #pos = nx.nx_agraph.pygraphviz_layout(netx_graph)
    #pos = nx.nx_agraph.graphviz_layout(netx_graph)
    pos = nx.pydot_layout(netx_graph)
    node_color = [pt.NEUTRAL] * len(pos)
    drawkw = dict(pos=pos,
                  ax=ax,
                  with_labels=True,
                  node_color=node_color,
                  node_size=2000)
    nx.draw(netx_graph, **drawkw)
    if kwargs.get('show_title', True):
        pt.set_figtitle('Junction / Clique Tree / Cluster Graph')
Beispiel #27
0
        def label_ticks(label_texts):
            import plottool as pt
            truncated_labels = [repr(lbl[0:100]) for lbl in label_texts]
            ax = pt.gca()
            ax.set_xticks(list(range(len(label_texts))))
            ax.set_xticklabels(truncated_labels)
            [lbl.set_rotation(-55) for lbl in ax.get_xticklabels()]
            [
                lbl.set_horizontalalignment('left')
                for lbl in ax.get_xticklabels()
            ]

            #xgrid, ygrid = np.meshgrid(range(len(label_texts)), range(len(label_texts)))
            #pt.plot_surface3d(xgrid, ygrid, disjoint_mat)
            ax.set_yticks(list(range(len(label_texts))))
            ax.set_yticklabels(truncated_labels)
            [
                lbl.set_horizontalalignment('right')
                for lbl in ax.get_yticklabels()
            ]
            [
                lbl.set_verticalalignment('center')
                for lbl in ax.get_yticklabels()
            ]
Beispiel #28
0
def testshow_extramargin_info(gfpath, bbox_gs, theta, new_size, halfoffset_ms,
                              mbbox_gs, margin_size):
    import plottool as pt
    import vtool as vt

    imgBGR = vt.imread(gfpath)
    chipBGR = compute_chip(gfpath, bbox_gs, theta, new_size, [])
    mchipBGR = compute_chip(gfpath, mbbox_gs, theta, margin_size, [])

    #index = 0
    w_cs, h_cs = new_size
    xo_ms, yo_ms = halfoffset_ms
    bbox_ms = [xo_ms, yo_ms, w_cs, h_cs]

    verts_gs = vt.scaled_verts_from_bbox(bbox_gs, theta, 1, 1)
    expanded_verts_gs = vt.scaled_verts_from_bbox(mbbox_gs, theta, 1, 1)
    expanded_verts_ms = vt.scaled_verts_from_bbox(bbox_ms, 0, 1, 1)
    # topheavy
    imgBGR = vt.draw_verts(imgBGR, verts_gs)
    imgBGR = vt.draw_verts(imgBGR, expanded_verts_gs)

    mchipBGR = vt.draw_verts(mchipBGR, expanded_verts_ms)

    fnum = 1
    pt.imshow(imgBGR, pnum=(1, 3, 1), fnum=fnum, title='original image')
    pt.gca().set_xlabel(str(imgBGR.shape))
    pt.imshow(chipBGR, pnum=(1, 3, 2), fnum=fnum, title='original chip')
    pt.gca().set_xlabel(str(chipBGR.shape))
    pt.imshow(
        mchipBGR,
        pnum=(1, 3, 3),
        fnum=fnum,
        title='scaled chip with expanded margin.\n(orig margin drawn in orange)'
    )
    pt.gca().set_xlabel(str(mchipBGR.shape))

    pt.show_if_requested()
Beispiel #29
0
def annotate_matches3(ibs, aid_list, bbox_list, offset_list, name_fm_list,
                      name_fs_list, qreq_=None, **kwargs):
    """
    TODO: use this as the main function.
    """
    # TODO Use this function when you clean show_matches
    in_image    = kwargs.get('in_image', False)
    #show_query  = kwargs.get('show_query', True)
    draw_border = kwargs.get('draw_border', True)
    draw_lbl    = kwargs.get('draw_lbl', True)
    notitle     = kwargs.get('notitle', False)
    # List of annotation scores for each annot in the name

    #printDBG('[viz] annotate_matches3()')
    #truth = ibs.get_match_truth(aid1, aid2)

    #name_equality = (
    #    np.array(ibs.get_annot_nids(aid_list[1:])) == ibs.get_annot_nids(aid_list[0])
    #).tolist()
    #truth = 1 if all(name_equality) else (2 if any(name_equality) else 0)
    #truth_color = vh.get_truth_color(truth)
    ## Build title

    #score         = kwargs.pop('score', None)
    #rawscore      = kwargs.pop('rawscore', None)
    #aid2_raw_rank = kwargs.pop('aid2_raw_rank', None)
    #print(kwargs)
    #title = vh.get_query_text(ibs, None, aid2, truth, qaid=aid1, **kwargs)
    # Build xlbl
    ax = pt.gca()
    ph.set_plotdat(ax, 'viztype', 'multi_match')
    ph.set_plotdat(ax, 'qaid', aid_list[0])
    ph.set_plotdat(ax, 'num_matches', len(aid_list) - 1)
    ph.set_plotdat(ax, 'aid_list', aid_list[1:])
    for count, aid in enumerate(aid_list, start=1):
        ph.set_plotdat(ax, 'aid%d' % (count,), aid)

    #name_equality = (ibs.get_annot_nids(aid_list[0]) ==
    #                 np.array(ibs.get_annot_nids(aid_list[1:])))
    #truth = 1 if np.all(name_equality) else (2 if np.any(name_equality) else 0)
    truth = get_multitruth(ibs, aid_list)
    if any(ibs.is_aid_unknown(aid_list[1:])) or ibs.is_aid_unknown(aid_list[0]):
        truth = ibs.const.TRUTH_UNKNOWN
    truth_color = vh.get_truth_color(truth)

    name_annot_scores = kwargs.get('name_annot_scores', None)
    if len(aid_list) == 2:
        # HACK; generalize to multple annots
        title = vh.get_query_text(ibs, None, aid_list[1], truth, qaid=aid_list[0], **kwargs)
        if not notitle:
            pt.set_title(title, ax)

    if draw_lbl:
        # Build labels
        nid_list = ibs.get_annot_nids(aid_list, distinguish_unknowns=False)
        name_list = ibs.get_annot_names(aid_list)
        lbls_list = [[] for _ in range(len(aid_list))]
        if kwargs.get('show_name', False):
            for count, (lbls, name) in enumerate(zip(lbls_list, name_list)):
                lbls.append(ut.repr2((name)))
        if kwargs.get('show_nid', True):
            for count, (lbls, nid) in enumerate(zip(lbls_list, nid_list)):
                # only label the first two images with nids
                LABEL_ALL_NIDS = False
                if count <= 1 or LABEL_ALL_NIDS:
                    #lbls.append(vh.get_nidstrs(nid))
                    lbls.append(('q' if count == 0 else '') + vh.get_nidstrs(nid))
        if kwargs.get('show_aid', True):
            for count, (lbls, aid) in enumerate(zip(lbls_list, aid_list)):
                lbls.append(('q' if count == 0 else '') + vh.get_aidstrs(aid))
        if (kwargs.get('show_annot_score', True) and
              name_annot_scores is not None):
            max_digits = kwargs.get('score_precision', None)
            for (lbls, score) in zip(lbls_list[1:], name_annot_scores):
                lbls.append(ut.num_fmt(score, max_digits=max_digits))
        lbl_list = [' : '.join(lbls) for lbls in lbls_list]
    else:
        lbl_list = [None] * len(aid_list)
    #pt.set_title(title, ax)
    # Plot annotations over images
    if in_image:
        in_image_bbox_list = vh.get_bboxes(ibs, aid_list, offset_list)
        in_image_theta_list = ibs.get_annot_thetas(aid_list)
        # HACK!
        #if show_query:
        #    pt.draw_bbox(bbox1, bbox_color=pt.ORANGE, lbl=lbl1, theta=theta1)
        bbox_color = pt.ORANGE
        bbox_color = truth_color if draw_border else pt.ORANGE
        for bbox, theta, lbl in zip(in_image_bbox_list, in_image_theta_list,
                                    lbl_list):
            pt.draw_bbox(bbox, bbox_color=bbox_color, lbl=lbl, theta=theta)
            pass
    else:
        xy, w, h = pt.get_axis_xy_width_height(ax)
        #theta2 = 0

        #if xywh2 is None:
        #    #xywh2 = (xy[0], xy[1], w, h)
        #    # weird when sidebyside is off y seems to be inverted
        #    xywh2 = (0,  0, w, h)

        #if not show_query and xywh1 is None:
        #    data_config2 = None if qreq_ is None else
        #    qreq_.get_external_data_config2()
        #    kpts2 = ibs.get_annot_kpts([aid2], config2_=data_config2)[0]
        #    #pt.draw_kpts2(kpts2.take(fm.T[1], axis=0))
        #    # Draw any selected matches
        #    #sm_kw = dict(rect=True, colors=pt.BLUE)
        #    pt.plot_fmatch(None, xywh2, None, kpts2, fm, fs=fs, **kwargs)
        #if draw_border:
        #    pt.draw_border(ax, truth_color, 4, offset=offset2)
        if draw_border:
            pt.draw_border(ax, color=truth_color, lw=4)
        if draw_lbl:
            # Custom user lbl for chips 1 and 2
            #if show_query:
            #    (x1, y1, w1, h1) = xywh1
            #    pt.absolute_lbl(x1 + w1, y1, lbl1)
            for bbox, lbl in zip(bbox_list, lbl_list):
                (x, y, w, h) = bbox
                pt.absolute_lbl(x + w, y, lbl)
    # No matches draw a red box
    if True:
        no_matches = name_fm_list is None or all([True if fm is None else len(fm) == 0 for fm in name_fm_list])
        if no_matches:
            xy, w, h = pt.get_axis_xy_width_height(ax)
            #axes_bbox = (xy[0], xy[1], w, h)
            if draw_border:
                pass
Beispiel #30
0
def draw_bayesian_model(model, evidence={}, soft_evidence={}, fnum=None,
                        pnum=None, **kwargs):

    from pgmpy.models import BayesianModel
    if not isinstance(model, BayesianModel):
        model = model.to_bayesian_model()

    import plottool as pt
    import networkx as nx
    kwargs = kwargs.copy()
    factor_list = kwargs.pop('factor_list', [])

    ttype_colors, ttype_scalars = make_colorcodes(model)

    textprops = {
        'horizontalalignment': 'left', 'family': 'monospace', 'size': 8, }

    # build graph attrs
    tup = get_node_viz_attrs(
        model, evidence, soft_evidence, factor_list, ttype_colors, **kwargs)
    node_color, pos_list, pos_dict, takws = tup

    # draw graph
    has_infered = evidence or 'factor_list' in kwargs

    if False:
        fig = pt.figure(fnum=fnum, pnum=pnum, doclf=True)  # NOQA
        ax = pt.gca()
        drawkw = dict(pos=pos_dict, ax=ax, with_labels=True, node_size=1100,
                      node_color=node_color)
        nx.draw(model, **drawkw)
    else:
        # BE VERY CAREFUL
        if 1:
            graph = model.copy()
            graph.__class__ = nx.DiGraph
            graph.graph['groupattrs'] = ut.ddict(dict)
            #graph = model.
            if getattr(graph, 'ttype2_cpds', None) is not None:
                # Add invis edges and ttype groups
                for ttype in model.ttype2_cpds.keys():
                    ttype_cpds = model.ttype2_cpds[ttype]
                    # use defined ordering
                    ttype_nodes = ut.list_getattr(ttype_cpds, 'variable')
                    # ttype_nodes = sorted(ttype_nodes)
                    invis_edges = list(ut.itertwo(ttype_nodes))
                    graph.add_edges_from(invis_edges)
                    nx.set_edge_attributes(graph, 'style', {edge: 'invis' for edge in invis_edges})
                    nx.set_node_attributes(graph, 'groupid', {node: ttype for node in ttype_nodes})
                    graph.graph['groupattrs'][ttype]['rank'] = 'same'
                    graph.graph['groupattrs'][ttype]['cluster'] = False
        else:
            graph = model
        pt.show_nx(graph, layout_kw={'prog': 'dot'}, fnum=fnum, pnum=pnum, verbose=0)
        pt.zoom_factory()
        fig = pt.gcf()
        ax = pt.gca()
        pass
    hacks = [pt.draw_text_annotations(textprops=textprops, **takw)
             for takw in takws if takw]

    xmin, ymin = np.array(pos_list).min(axis=0)
    xmax, ymax = np.array(pos_list).max(axis=0)
    if 'name' in model.ttype2_template:
        num_names = len(model.ttype2_template['name'].basis)
        num_annots = len(model.ttype2_cpds['name'])
        if num_annots > 4:
            ax.set_xlim((xmin - 40, xmax + 40))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(30, 7)
        else:
            ax.set_xlim((xmin - 42, xmax + 42))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(23, 7)
        title = 'num_names=%r, num_annots=%r' % (num_names, num_annots,)
    else:
        title = ''
    map_assign = kwargs.get('map_assign', None)

    def word_insert(text):
        return '' if len(text) == 0 else text + ' '

    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        map_assign, map_prob = top_assignments[0]
        if map_assign is not None:
            title += '\n%sMAP: ' % (word_insert(kwargs.get('method', '')))
            title += map_assign + ' @' + '%.2f%%' % (100 * map_prob,)
    if kwargs.get('show_title', True):
        pt.set_figtitle(title, size=14)

    for hack in hacks:
        hack()

    if has_infered:
        # Hack in colorbars
        # if ut.list_type(basis) is int:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=np.array(basis) + 1)
        # else:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=basis)
        keys = ['name', 'score']
        locs = ['left', 'right']
        for key, loc in zip(keys, locs):
            if key in ttype_colors:
                basis = model.ttype2_template[key].basis
                # scalars =
                colors = ttype_colors[key]
                scalars = ttype_scalars[key]
                pt.colorbar(scalars, colors, lbl=key, ticklabels=basis,
                            ticklocation=loc)
Beispiel #31
0
def get_iters_vs_miou(harn):
    from pysseg.util import jsonutil
    import pysseg.backend.iface_caffe as iface
    harn.prepare_test_model()
    test_weight_dpaths = harn.find_test_weights_dpaths()
    # for test_weights_dpath in test_weight_dpaths:
    #     harn.test_weights_dpath = test_weights_dpath
    #     harn.test_weights_fpath = ub.readfrom(join(test_weights_dpath, 'test_weights.caffemodel.lnk'))
    #     # if not exists(join(harn.test_weights_dpath, 'pred')):
    #     results_fpath = join(harn.test_weights_dpath, 'results.json')
    #     if exists(results_fpath):
    #         results = json.load(results_fpath)

    iter_results = {}
    for test_weights_dpath in test_weight_dpaths:
        results_fpath = join(test_weights_dpath, 'results.json')
        if exists(results_fpath):
            iterno = iface.snapshot_iterno(test_weights_dpath)
            results = json.load(open(results_fpath, 'r'))
            ious = eval(results['ious'])
            iter_results[iterno] = ious

    iter_df = pd.DataFrame(iter_results)
    iter_df.columns.name = 'iterno'
    iter_df.index.name = 'class'

    fpath = join(harn.test_dpath, 'iter_ious.json')
    jsonutil.write_json(fpath, iter_df)

    iter_df = iter_df.drop([57], axis=1)
    iter_df.drop(harn.task.ignore_classnames).mean(axis=0)

    if False:
        """
        ffmpeg -y -f image2 -i ~/aretha/store/data/work/camvid/arch/segnet_proper/test/input_nqmmrhd/weights_abvroyo_segnet_proper_None_xwfmwfo_00040000/blend_pred/%*.png -crf 25  -vcodec libx264  -vf "setpts=4*PTS" camvid-results.avi

        ffmpeg -y -f image2 -i out_haul83/%*.png -vcodec mpeg4 -vf "setpts=10*PTS" haul83-results.avi

        """
        # move to computer with plottool
        iter_df = pd.read_json(
            '/home/joncrall/aretha/store/data/work/camvid/arch/segnet_proper/test/input_nqmmrhd/iter_ious.json'
        )

        import plottool as pt
        pt.qtensure()

        from pysseg.tasks import CamVid
        task = CamVid()

        iter_miou = iter_df.drop(task.ignore_classnames).mean(axis=0)
        iter_miou = iter_miou.sort_index()

        _set_mpl_rcparams()

        fig = pt.figure(fnum=1, pnum=(1, 1, 1))
        ax = pt.gca()
        iter_miou.plot(ax=ax)
        ax.set_xlabel('train iters')
        ax.set_ylabel('test mIoU')
        ax.set_title('Reproduced CamVid Results (init using VGG)')
        ub.ensuredir('result_plots')
        from pysseg.draw import render_figure_to_image
        import cv2
        cv2.imwrite('result_plots/miou.png',
                    render_figure_to_image(fig, dpi=100, transparent=True))

        fig = pt.figure(fnum=2, pnum=(1, 1, 1))
        ax = pt.gca()
        iter_iou = iter_df.drop(task.ignore_classnames).T.sort_index()

        # sort by results
        iter_iou = iter_iou[iter_iou.iloc[-1].sort_values().index[::-1]]

        colors = [
            tuple(np.array(v[::-1]) / 255) + (1, )
            for v in ub.take(task.class_colors, iter_iou.columns)
        ]

        iter_iou.plot(ax=ax, colors=colors, lw=4)
        ax.set_xlabel('train iters')

        ax.set_ylabel('test IoU')
        ax.set_title('Reproduced CamVid Results (init using VGG)')
        ub.ensuredir('result_plots')
        from pysseg.draw import render_figure_to_image
        cv2.imwrite('result_plots/perclass_iou.png',
                    render_figure_to_image(fig, dpi=100, transparent=True))
Beispiel #32
0
def show_time_distributions(ibs, unixtime_list):
    r"""
    """
    #import vtool as vt
    import plottool as pt
    unixtime_list = np.array(unixtime_list)
    num_nan = np.isnan(unixtime_list).sum()
    num_total = len(unixtime_list)
    unixtime_list = unixtime_list[~np.isnan(unixtime_list)]
    if False:
        from matplotlib import dates as mpldates
        #data_list = list(map(ut.unixtime_to_datetimeobj, unixtime_list))
        n, bins, patches = pt.plt.hist(unixtime_list, 365)
        #n_ = list(map(ut.unixtime_to_datetimeobj, n))
        #bins_ = list(map(ut.unixtime_to_datetimeobj, bins))
        pt.plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
        ax = pt.gca()
        #ax.xaxis.set_major_locator(mpldates.YearLocator())
        #hfmt = mpldates.DateFormatter('%y/%m/%d')
        #ax.xaxis.set_major_formatter(hfmt)
        mpldates.num2date(unixtime_list)
        #pt.gcf().autofmt_xdate()
        #y = pt.plt.normpdf( bins, unixtime_list.mean(), unixtime_list.std())
        #ax.set_xticks(bins_)
        #l = pt.plt.plot(bins_, y, 'k--', linewidth=1.5)
    else:
        pt.draw_time_distribution(unixtime_list)
        #pt.draw_histogram()
        ax = pt.gca()
        ax.set_xlabel('Date')
        ax.set_title('Timestamp distribution of %s. #nan=%d/%d' % (
            ibs.get_dbname_alias(),
            num_nan, num_total))
        pt.gcf().autofmt_xdate()

        icon = ibs.get_database_icon()
        if icon is not None:
            #import matplotlib as mpl
            #import vtool as vt
            ax = pt.gca()
            # Overlay a species icon
            # http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html
            #icon = vt.convert_image_list_colorspace([icon], 'RGB', 'BGR')[0]
            pt.overlay_icon(icon, coords=(0, 1), bbox_alignment=(0, 1))
            #imagebox = mpl.offsetbox.OffsetImage(icon, zoom=1.0)
            ##xy = [ax.get_xlim()[0] + 5, ax.get_ylim()[1]]
            ##ax.set_xlim(1, 100)
            ##ax.set_ylim(0, 100)
            ##x = np.array(ax.get_xlim()).sum() / 2
            ##y = np.array(ax.get_ylim()).sum() / 2
            ##xy = [x, y]
            ##print('xy = %r' % (xy,))
            ##x = np.nanmin(unixtime_list)
            ##xy = [x, y]
            ##print('xy = %r' % (xy,))
            ##ax.get_ylim()[0]]
            #xy = [ax.get_xlim()[0], ax.get_ylim()[1]]
            #ab = mpl.offsetbox.AnnotationBbox(
            #    imagebox, xy, xycoords='data',
            #    xybox=(-0., 0.),
            #    boxcoords="offset points",
            #    box_alignment=(0, 1), pad=0.0)
            #ax.add_artist(ab)

    if ut.get_argflag('--contextadjust'):
        #pt.adjust_subplots2(left=.08, bottom=.1, top=.9, wspace=.3, hspace=.1)
        pt.adjust_subplots2(use_argv=True)
Beispiel #33
0
def netx_draw_images_at_positions(img_list, pos_list, zoom=.4):
    """
    References:
        https://gist.github.com/shobhit/3236373
        http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html

        http://matplotlib.org/api/text_api.html
        http://matplotlib.org/api/offsetbox_api.html

    TODO: look into DraggableAnnotation
    """
    from matplotlib.offsetbox import OffsetImage, AnnotationBbox
    print('[viz_graph] drawing %d images' % len(img_list))
    # Thumb stackartist
    ax  = pt.gca()
    artist_list = []
    offset_img_list = []
    for pos, img in zip(pos_list, img_list):
        x, y = pos
        height, width = img.shape[0:2]
        offset_img = OffsetImage(img, zoom=zoom)
        artist = AnnotationBbox(
            offset_img,
            (x, y),
            xybox=(-0., 0.),
            xycoords='data',
            boxcoords="offset points",
            #pad=0.1,
            pad=0.25,
            #frameon=False,
            frameon=True,
            #bboxprops=dict(fc="cyan"),
        )  # ,arrowprops=dict(arrowstyle="->"))
        offset_img_list.append(offset_img)
        artist_list.append(artist)

    for artist in artist_list:
        ax.add_artist(artist)

    # TODO: move this to the interaction

    def _onresize(event):
        print('foo' + ut.get_timestamp())

    def zoom_factory(ax, base_scale=1.1):
        """
        References:
            https://gist.github.com/tacaswell/3144287
        """
        def zoom_fun(event):
            #print('zooming')
            # get the current x and y limits
            cur_xlim = ax.get_xlim()
            cur_ylim = ax.get_ylim()
            # set the range
            #cur_xrange = (cur_xlim[1] - cur_xlim[0]) * .5
            #cur_yrange = (cur_ylim[1] - cur_ylim[0]) * .5
            xdata = event.xdata  # get event x location
            ydata = event.ydata  # get event y location
            if xdata is None or ydata is None:
                return
            if event.button == 'up':
                # deal with zoom in
                scale_factor = 1 / base_scale
            elif event.button == 'down':
                # deal with zoom out
                scale_factor = base_scale
            else:
                raise NotImplementedError('event.button=%r' % (event.button,))
                # deal with something that should never happen
                scale_factor = 1
                print(event.button)
            # set new limits
            #ax.set_xlim([xdata - cur_xrange * scale_factor,
            #             xdata + cur_xrange * scale_factor])
            #ax.set_ylim([ydata - cur_yrange * scale_factor,
            #             ydata + cur_yrange * scale_factor])
            # ----
            for offset_img in offset_img_list:
                zoom = offset_img.get_zoom()
                offset_img.set_zoom(zoom / (scale_factor ** (1.2)))
            # Get distance from the cursor to the edge of the figure frame
            x_left = xdata - cur_xlim[0]
            x_right = cur_xlim[1] - xdata
            y_top = ydata - cur_ylim[0]
            y_bottom = cur_ylim[1] - ydata
            ax.set_xlim([xdata - x_left * scale_factor, xdata + x_right * scale_factor])
            ax.set_ylim([ydata - y_top * scale_factor, ydata + y_bottom * scale_factor])

            # ----
            ax.figure.canvas.draw()  # force re-draw

        fig = ax.get_figure()  # get the figure of interest
        # attach the call back
        fig.canvas.mpl_connect('scroll_event', zoom_fun)

        #return the function
        return zoom_fun
    #pt.interact_helpers.connect_callback(fig, 'resize_event', _onresize)
    zoom_factory(ax)
    return artist_list
Beispiel #34
0
def show_model(model, evidence={}, soft_evidence={}, **kwargs):
    """
    References:
        http://stackoverflow.com/questions/22207802/pygraphviz-networkx-set-node-level-or-layer

    Ignore:
        pkg-config --libs-only-L libcgraph
        sudo apt-get  install libgraphviz-dev -y
        sudo apt-get  install libgraphviz4 -y

        # sudo apt-get install pkg-config
        sudo apt-get install libgraphviz-dev
        # pip install git+git://github.com/pygraphviz/pygraphviz.git
        pip install pygraphviz
        python -c "import pygraphviz; print(pygraphviz.__file__)"

        sudo pip3 install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"
        python3 -c "import pygraphviz; print(pygraphviz.__file__)"
    """
    if ut.get_argval('--hackmarkov') or ut.get_argval('--hackjunc'):
        draw_tree_model(model, **kwargs)
        return

    import plottool as pt
    import networkx as netx
    import matplotlib as mpl
    fnum = pt.ensure_fnum(None)
    fig = pt.figure(fnum=fnum, pnum=(3, 1, (slice(0, 2), 0)),
                    doclf=True)  # NOQA
    #fig = pt.figure(fnum=fnum, pnum=(3, 2, (1, slice(1, 2))), doclf=True)  # NOQA
    ax = pt.gca()
    var2_post = {f.variables[0]: f for f in kwargs.get('factor_list', [])}

    netx_graph = (model)
    #netx_graph.graph.setdefault('graph', {})['size'] = '"10,5"'
    #netx_graph.graph.setdefault('graph', {})['rankdir'] = 'LR'

    pos = get_hacked_pos(netx_graph)
    #netx.nx_agraph.pygraphviz_layout(netx_graph)
    #pos = netx.nx_agraph.pydot_layout(netx_graph, prog='dot')
    #pos = netx.nx_agraph.graphviz_layout(netx_graph)

    drawkw = dict(pos=pos, ax=ax, with_labels=True, node_size=1500)
    if evidence is not None:
        node_colors = [
            # (pt.TRUE_BLUE
            (pt.WHITE if node not in soft_evidence else pt.LIGHT_PINK)
            if node not in evidence else pt.FALSE_RED
            for node in netx_graph.nodes()
        ]

        for node in netx_graph.nodes():
            cpd = model.var2_cpd[node]
            if cpd.ttype == 'score':
                pass
        drawkw['node_color'] = node_colors

    netx.draw(netx_graph, **drawkw)

    show_probs = True
    if show_probs:
        textprops = {
            'family': 'monospace',
            'horizontalalignment': 'left',
            #'horizontalalignment': 'center',
            #'size': 12,
            'size': 8,
        }

        textkw = dict(
            xycoords='data',
            boxcoords='offset points',
            pad=0.25,
            framewidth=True,
            arrowprops=dict(arrowstyle='->'),
            #bboxprops=dict(fc=node_attr['fillcolor']),
        )

        netx_nodes = model.nodes(data=True)
        node_key_list = ut.get_list_column(netx_nodes, 0)
        pos_list = ut.dict_take(pos, node_key_list)

        artist_list = []
        offset_box_list = []
        for pos_, node in zip(pos_list, netx_nodes):
            x, y = pos_
            variable = node[0]

            cpd = model.var2_cpd[variable]

            prior_marg = (cpd if cpd.evidence is None else cpd.marginalize(
                cpd.evidence, inplace=False))

            prior_text = None

            text = None
            if variable in evidence:
                text = cpd.variable_statenames[evidence[variable]]
            elif variable in var2_post:
                post_marg = var2_post[variable]
                text = pgm_ext.make_factor_text(post_marg, 'post')
                prior_text = pgm_ext.make_factor_text(prior_marg, 'prior')
            else:
                if len(evidence) == 0 and len(soft_evidence) == 0:
                    prior_text = pgm_ext.make_factor_text(prior_marg, 'prior')

            show_post = kwargs.get('show_post', False)
            show_prior = kwargs.get('show_prior', False)
            show_prior = True
            show_post = True

            show_ev = (evidence is not None and variable in evidence)
            if (show_post or show_ev) and text is not None:
                offset_box = mpl.offsetbox.TextArea(text, textprops)
                artist = mpl.offsetbox.AnnotationBbox(
                    # offset_box, (x + 5, y), xybox=(20., 5.),
                    offset_box,
                    (x, y + 5),
                    xybox=(4., 20.),
                    #box_alignment=(0, 0),
                    box_alignment=(.5, 0),
                    **textkw)
                offset_box_list.append(offset_box)
                artist_list.append(artist)

            if show_prior and prior_text is not None:
                offset_box2 = mpl.offsetbox.TextArea(prior_text, textprops)
                artist2 = mpl.offsetbox.AnnotationBbox(
                    # offset_box2, (x - 5, y), xybox=(-20., -15.),
                    # offset_box2, (x, y - 5), xybox=(-15., -20.),
                    offset_box2,
                    (x, y - 5),
                    xybox=(-4, -20.),
                    #box_alignment=(1, 1),
                    box_alignment=(.5, 1),
                    **textkw)
                offset_box_list.append(offset_box2)
                artist_list.append(artist2)

        for artist in artist_list:
            ax.add_artist(artist)

        xmin, ymin = np.array(pos_list).min(axis=0)
        xmax, ymax = np.array(pos_list).max(axis=0)
        num_annots = len(model.ttype2_cpds['name'])
        if num_annots > 4:
            ax.set_xlim((xmin - 40, xmax + 40))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(30, 7)
        else:
            ax.set_xlim((xmin - 42, xmax + 42))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(23, 7)
        fig = pt.gcf()

        title = 'num_names=%r, num_annots=%r' % (
            model.num_names,
            num_annots,
        )
        map_assign = kwargs.get('map_assign', None)
        #max_marginal_list = []
        #for name, marginal in marginalized_joints.items():
        #    states = list(ut.iprod(*marginal.statenames))
        #    vals = marginal.values.ravel()
        #    x = vals.argmax()
        #    max_marginal_list += ['P(' + ', '.join(states[x]) + ') = ' + str(vals[x])]
        # title += str(marginal)
        top_assignments = kwargs.get('top_assignments', None)
        if top_assignments is not None:
            map_assign, map_prob = top_assignments[0]
            if map_assign is not None:
                # title += '\nMAP=' + ut.repr2(map_assign, strvals=True)
                title += '\nMAP: ' + map_assign + ' @' + '%.2f%%' % (
                    100 * map_prob, )
        if kwargs.get('show_title', True):
            pt.set_figtitle(title, size=14)
        #pt.set_xlabel()

        def hack_fix_centeralign():
            if textprops['horizontalalignment'] == 'center':
                print('Fixing centeralign')
                fig = pt.gcf()
                fig.canvas.draw()

                # Superhack for centered text. Fix bug in
                # /usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py
                # /usr/local/lib/python2.7/dist-packages/matplotlib/text.py
                for offset_box in offset_box_list:
                    offset_box.set_offset
                    z = offset_box._text.get_window_extent()
                    (z.x1 - z.x0) / 2
                    offset_box._text
                    T = offset_box._text.get_transform()
                    A = mpl.transforms.Affine2D()
                    A.clear()
                    A.translate((z.x1 - z.x0) / 2, 0)
                    offset_box._text.set_transform(T + A)

        hack_fix_centeralign()
    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        bin_labels = ut.get_list_column(top_assignments, 0)
        bin_vals = ut.get_list_column(top_assignments, 1)

        # bin_labels = ['\n'.join(ut.textwrap.wrap(_lbl, width=30)) for _lbl in bin_labels]

        pt.draw_histogram(
            bin_labels,
            bin_vals,
            fnum=fnum,
            pnum=(3, 8, (2, slice(4, None))),
            transpose=True,
            use_darkbackground=False,
            #xtick_rotation=-10,
            ylabel='Prob',
            xlabel='assignment')
        pt.set_title('Assignment probabilities')
Beispiel #35
0
def show_chip(ibs,
              aid,
              in_image=False,
              annote=True,
              title_suffix='',
              weight_label=None,
              weights=None,
              config2_=None,
              **kwargs):
    r""" Driver function to show chips

    Args:
        ibs (ibeis.IBEISController):
        aid (int): annotation rowid
        in_image (bool): displays annotation with the context of its source image
        annote (bool): enables overlay annoations
        title_suffix (str):
        weight_label (None): (default = None)
        weights (None): (default = None)
        config2_ (dict): (default = None)

    Kwargs:
        enable_chip_title_prefix, nokpts, kpts_subset, kpts, text_color,
        notitle, draw_lbls, show_aidstr, show_gname, show_name, show_nid,
        show_exemplar, show_num_gt, show_quality_text, show_yawtext, fnum,
        title, figtitle, pnum, interpolation, cmap, heatmap, data_colorbar,
        darken, update, xlabel, redraw_image, ax, alpha, docla, doclf,
        projection, use_gridspec, pts, ell
        color (3/4-tuple, ndarray, or str): colors for keypoints

    CommandLine:
        python -m ibeis.viz.viz_chip --test-show_chip --show --ecc
        python -c "import utool as ut; ut.print_auto_docstr('ibeis.viz.viz_chip', 'show_chip')"
        python -m ibeis.viz.viz_chip --test-show_chip --show --db NNP_Master3 --aids 14047 --no-annote
        python -m ibeis.viz.viz_chip --test-show_chip --show --db NNP_Master3 --aids 14047 --no-annote

        python -m ibeis.viz.viz_chip --test-show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn
        python -m ibeis.viz.viz_chip --test-show_chip --show --db PZ_MTEST --aid 1 --bgmethod=cnn --scale_max=30

    Example:
        >>> # VIZ_TEST
        >>> from ibeis.viz.viz_chip import *  # NOQA
        >>> import numpy as np
        >>> import vtool as vt
        >>> in_image = False
        >>> ibs, aid_list, kwargs, config2_ = testdata_showchip()
        >>> aid = aid_list[0]
        >>> if ut.get_argflag('--ecc'):
        >>>     kpts = ibs.get_annot_kpts(aid, config2_=config2_)
        >>>     weights = ibs.get_annot_fgweights([aid], ensure=True, config2_=config2_)[0]
        >>>     kpts = ut.random_sample(kpts[weights > .9], 200, seed=0)
        >>>     ecc = vt.get_kpts_eccentricity(kpts)
        >>>     scale = 1 / vt.get_scales(kpts)
        >>>     s = ecc if config2_.affine_invariance else scale
        >>>     colors = pt.scores_to_color(s, cmap_='jet')
        >>>     kwargs['color'] = colors
        >>>     kwargs['kpts'] = kpts
        >>> show_chip(ibs, aid, in_image=in_image, config2_=config2_, **kwargs)
        >>> pt.show_if_requested()
    """
    if ut.VERBOSE:
        print('[viz] show_chip(aid=%r)' % (aid, ))
    #ibs.assert_valid_aids((aid,))
    # Get chip
    #print('in_image = %r' % (in_image,))
    chip = vh.get_chips(ibs, aid, in_image=in_image, config2_=config2_)
    # Create chip title
    chip_text = vh.get_annot_texts(ibs, [aid], **kwargs)[0]
    if kwargs.get('enable_chip_title_prefix', True):
        chip_title_text = chip_text + title_suffix
    else:
        chip_title_text = title_suffix
    chip_title_text = chip_title_text.strip('\n')
    # Draw chip
    fig, ax = pt.imshow(chip, **kwargs)
    # Populate axis user data
    vh.set_ibsdat(ax, 'viztype', 'chip')
    vh.set_ibsdat(ax, 'aid', aid)
    if annote and not kwargs.get('nokpts', False):
        # Get and draw keypoints
        if 'color' not in kwargs:
            if weight_label == 'fg_weights':
                if weights is None and ibs.has_species_detector(
                        ibs.get_annot_species_texts(aid)):
                    weight_label = 'fg_weights'
                    weights = ibs.get_annot_fgweights([aid],
                                                      ensure=True,
                                                      config2_=config2_)[0]
            if weights is not None:
                cmap_ = 'hot'
                #if weight_label == 'dstncvs':
                #    cmap_ = 'rainbow'
                color = pt.scores_to_color(weights,
                                           cmap_=cmap_,
                                           reverse_cmap=False)
                kwargs['color'] = color
                kwargs['ell_color'] = color
                kwargs['pts_color'] = color

        kpts_ = vh.get_kpts(ibs,
                            aid,
                            in_image,
                            config2_=config2_,
                            kpts_subset=kwargs.get('kpts_subset', None),
                            kpts=kwargs.get('kpts', None))
        try:
            del kwargs['kpts']
        except KeyError:
            pass
        pt.viz_keypoints._annotate_kpts(kpts_, **kwargs)
        if not ut.get_argflag('--noaidlabel'):
            pt.upperleft_text(chip_text, color=kwargs.get('text_color', None))
    use_title = not kwargs.get('notitle', False)
    if use_title:
        pt.set_title(chip_title_text)
    if in_image:
        gid = ibs.get_annot_gids(aid)
        aid_list = ibs.get_image_aids(gid)
        annotekw = viz_image.get_annot_annotations(ibs,
                                                   aid_list,
                                                   sel_aids=[aid],
                                                   draw_lbls=kwargs.get(
                                                       'draw_lbls', True))
        # Put annotation centers in the axis
        ph.set_plotdat(ax, 'annotation_bbox_list', annotekw['bbox_list'])
        ph.set_plotdat(ax, 'aid_list', aid_list)
        pt.viz_image2.draw_image_overlay(ax, **annotekw)

        zoom_ = ut.get_argval('--zoom', type_=float, default=None)
        if zoom_ is not None:
            import vtool as vt
            # Zoom into the chip for some image context
            rotated_verts = ibs.get_annot_rotated_verts(aid)
            bbox = ibs.get_annot_bboxes(aid)
            #print(bbox)
            #print(rotated_verts)
            rotated_bbox = vt.bbox_from_verts(rotated_verts)
            imgw, imgh = ibs.get_image_sizes(gid)

            pad_factor = zoom_
            pad_length = min(bbox[2], bbox[3]) * pad_factor
            minx = max(rotated_bbox[0] - pad_length, 0)
            miny = max(rotated_bbox[1] - pad_length, 0)
            maxx = min((rotated_bbox[0] + rotated_bbox[2]) + pad_length, imgw)
            maxy = min((rotated_bbox[1] + rotated_bbox[3]) + pad_length, imgh)

            #maxy = imgh - maxy
            #miny = imgh - miny

            ax = pt.gca()
            ax.set_xlim(minx, maxx)
            ax.set_ylim(miny, maxy)
            ax.invert_yaxis()
    else:
        ph.set_plotdat(ax, 'chipshape', chip.shape)

    #if 'featweights' in vars() and 'color' in kwargs:
    if weights is not None and weight_label is not None:
        ## HACK HACK HACK
        if len(weights) > 0:
            cb = pt.colorbar(weights, kwargs['color'])
            cb.set_label(weight_label)
    return fig, ax
Beispiel #36
0
def draw_markov_model(model, fnum=None, **kwargs):
    import plottool as pt
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum, doclf=True)
    ax = pt.gca()
    from pgmpy.models import MarkovModel
    if isinstance(model, MarkovModel):
        markovmodel = model
    else:
        markovmodel = model.to_markov_model()
    # pos = nx.nx_agraph.pydot_layout(markovmodel)
    pos = nx.nx_agraph.pygraphviz_layout(markovmodel)
    # Referenecs:
    # https://groups.google.com/forum/#!topic/networkx-discuss/FwYk0ixLDuY

    # pos = nx.spring_layout(markovmodel)
    # pos = nx.circular_layout(markovmodel)
    # curved-arrow
    # markovmodel.edge_attr['curved-arrow'] = True
    # markovmodel.graph.setdefault('edge', {})['splines'] = 'curved'
    # markovmodel.graph.setdefault('graph', {})['splines'] = 'curved'
    # markovmodel.graph.setdefault('edge', {})['splines'] = 'curved'

    node_color = [pt.NEUTRAL] * len(pos)
    drawkw = dict(
        pos=pos,
        ax=ax,
        with_labels=True,
        node_color=node_color,  # NOQA
        node_size=1100)

    from matplotlib.patches import FancyArrowPatch, Circle
    import numpy as np

    def draw_network(G, pos, ax, sg=None):
        for n in G:
            c = Circle(pos[n], radius=10, alpha=0.5, color=pt.NEUTRAL_BLUE)
            ax.add_patch(c)
            G.node[n]['patch'] = c
            x, y = pos[n]
            pt.ax_absolute_text(x, y, n, ha='center', va='center')
        seen = {}
        for (u, v, d) in G.edges(data=True):
            n1 = G.node[u]['patch']
            n2 = G.node[v]['patch']
            rad = 0.1
            if (u, v) in seen:
                rad = seen.get((u, v))
                rad = (rad + np.sign(rad) * 0.1) * -1
            alpha = 0.5
            color = 'k'

            e = FancyArrowPatch(
                n1.center,
                n2.center,
                patchA=n1,
                patchB=n2,
                # arrowstyle='-|>',
                arrowstyle='-',
                connectionstyle='arc3,rad=%s' % rad,
                mutation_scale=10.0,
                lw=2,
                alpha=alpha,
                color=color)
            seen[(u, v)] = rad
            ax.add_patch(e)
        return e

    # nx.draw(markovmodel, **drawkw)
    draw_network(markovmodel, pos, ax)
    ax.autoscale()
    pt.plt.axis('equal')
    pt.plt.axis('off')

    if kwargs.get('show_title', True):
        pt.set_figtitle('Markov Model')
Beispiel #37
0
    def plot_chip(self, aid, nRows, nCols, px, **kwargs):
        """ Plots an individual chip in a subaxis """
        ibs = self.ibs
        enable_chip_title_prefix = ut.is_developer()
        #enable_chip_title_prefix = False
        if aid in self.comp_aids:
            score = self.cm.get_annot_scores([aid])[0]
            rawscore = self.cm.get_annot_scores([aid])[0]
            title_suf = kwargs.get('title_suffix', '')
            if score != rawscore:
                if score is None:
                    title_suf += '\n score=____'
                else:
                    title_suf += '\n score=%0.2f' % score
            title_suf += '\n rawscore=%0.2f' % rawscore
        else:
            title_suf = kwargs.get('title_suffix', '')
            if enable_chip_title_prefix:
                title_suf = '\n' + title_suf

        #nid = ibs.get_annot_name_rowids(aid)
        viz_chip_kw = {
            'fnum': self.fnum,
            'pnum': (nRows, nCols, px),
            'nokpts': True,
            'show_gname': False,
            'show_exemplar': False,
            'show_num_gt': False,
            'show_gname': False,
            'title_suffix': title_suf,
            # 'text_color': kwargs.get('color'),
            ###
            #'show_name': False,
            #'show_aidstr': False,
            'enable_chip_title_prefix': enable_chip_title_prefix,
            'show_name': True,
            'show_aidstr': True,
            'show_yawtext': True,
            'show_quality_text': True,
        }

        viz_chip.show_chip(ibs, aid, **viz_chip_kw)
        ax = pt.gca()
        if kwargs.get('make_buttons', True):
            divider = pt.ensure_divider(ax)
            butkw = {'divider': divider, 'size': '13%'}

        self.aid2_ax = {}
        self.aid2_border = {}

        if aid in self.comp_aids:
            callback = partial(self.select, aid)
            self.append_button('Select This Animal',
                               callback=callback,
                               **butkw)
            #Hack to toggle colors
            if aid in self.aid_checkbox_states:
                #If we are selecting it, then make it green, otherwise change it back to grey
                if self.aid_checkbox_states[aid]:
                    border = pt.draw_border(ax, color=(0, 1, 0), lw=4)
                else:
                    border = pt.draw_border(ax, color=(.7, .7, .7), lw=4)
                self.aid2_border[aid] = border
            else:
                self.aid_checkbox_states[aid] = False
            self.append_button('Examine',
                               callback=partial(self.examine, aid),
                               **butkw)
Beispiel #38
0
    def plot_chip(self, aid, nRows, nCols, px, fulldraw=True, **kwargs):
        """ Plots an individual chip in a subaxis """
        ibs = self.ibs
        if aid in [self.aid1, self.aid2]:
            # Bold color for the matching chips
            lw = 5
            text_color = np.array((135, 206, 235, 255)) / 255.0
        else:
            lw = 2
            text_color = None

        pnum = (nRows, nCols, px)
        if not fulldraw:
            # not doing full draw so we have to clear any axes
            # that are here already manually
            ax = self.fig.add_subplot(*pnum)
            self.clear_parent_axes(ax)
            #ut.embed()
            #print(subax)

        viz_chip_kw = {
            'fnum': self.fnum,
            'pnum': pnum,
            'nokpts': True,
            'show_name': True,
            'show_gname': False,
            'show_aidstr': True,
            'notitle': True,
            'show_num_gt': False,
            'text_color': text_color,
        }
        if False and ut.is_developer():
            enable_chip_title_prefix = True
            viz_chip_kw.update({
                'enable_chip_title_prefix': enable_chip_title_prefix,
                'show_name': True,
                'show_aidstr': True,
                'show_viewcode': True,
                'show_num_gt': True,
                'show_quality_text': True,
            })

        viz_chip.show_chip(ibs, aid, **viz_chip_kw)
        ax = pt.gca()
        pt.draw_border(ax, color=kwargs.get('color'), lw=lw)
        if kwargs.get('make_buttons', True):
            #divider = pt.ensure_divider(ax)
            butkw = {
                #'divider': divider,
                'ax': ax,
                'size': '13%'
                #'size': '15%'
            }
        # Chip matching/naming options
        nid = ibs.get_annot_name_rowids(aid)
        annotation_unknown = ibs.is_nid_unknown([nid])[0]
        if not annotation_unknown:
            # remove name
            callback = functools.partial(self.unname_annotation, aid)
            self.append_button('remove name (' + ibs.get_name_texts(nid) + ')',
                               callback=callback,
                               **butkw)
        else:
            # new name
            callback = functools.partial(self.mark_annotation_as_new_name, aid)
            self.append_button('mark as new name', callback=callback, **butkw)
        if nid != self.nid2 and not ibs.is_nid_unknown(
            [self.nid2])[0] and not self.is_split_case:
            # match to nid2
            callback = functools.partial(self.rename_annotation, aid,
                                         self.nid2)
            text = 'match to name2: ' + ibs.get_name_texts(self.nid2)
            self.append_button(text, callback=callback, **butkw)
        if nid != self.nid1 and not ibs.is_nid_unknown([self.nid1])[0]:
            # match to nid1
            callback = functools.partial(self.rename_annotation, aid,
                                         self.nid1)
            text = 'match to name1: ' + ibs.get_name_texts(self.nid1)
            self.append_button(text, callback=callback, **butkw)

        other_nid_list = self.get_other_nids()
        for other_nid in other_nid_list:
            if other_nid == nid:
                continue
            # rename nid2
            callback = functools.partial(self.rename_annotation, aid,
                                         other_nid)
            text = 'match to: ' + ibs.get_name_texts(other_nid)
            self.append_button(text, callback=callback, **butkw)
        return ax
Beispiel #39
0
def show_name_matches(ibs, qaid, name_daid_list, name_fm_list, name_fs_list,
                      name_H1_list, name_featflag_list, qreq_=None, **kwargs):
    """
    Called from chip_match.py

    Args:
        ibs (IBEISController):  ibeis controller object
        qaid (int):  query annotation id
        name_daid_list (list):
        name_fm_list (list):
        name_fs_list (list):
        name_H1_list (list):
        name_featflag_list (list):
        qreq_ (QueryRequest):  query request object with hyper-parameters(default = None)

    Kwargs:
        draw_fmatches, name_rank, fnum, pnum, colorbar_, nonvote_mode,
        fastmode, show_matches, fs, fm_norm, lbl1, lbl2, rect, draw_border,
        cmap, H1, H2, scale_factor1, scale_factor2, draw_pts, draw_ell,
        draw_lines, show_nMatches, all_kpts, in_image, show_query, draw_lbl,
        name_annot_scores, score, rawscore, aid2_raw_rank, show_name,
        show_nid, show_aid, show_annot_score, show_truth, name_score,
        show_name_score, show_name_rank, show_timedelta

    CommandLine:
        python -m ibeis.viz.viz_matches --exec-show_name_matches
        python -m ibeis.viz.viz_matches --test-show_name_matches --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_matches import *  # NOQA
        >>> from ibeis.algo.hots import chip_match
        >>> from ibeis.algo.hots import name_scoring
        >>> import vtool as vt
        >>> from ibeis.algo.hots import _pipeline_helpers as plh  # NOQA
        >>> import numpy as np
        >>> func = chip_match.ChipMatch.show_single_namematch
        >>> sourcecode = ut.get_func_sourcecode(func, stripdef=True, stripret=True,
        >>>                                     strip_docstr=True)
        >>> setup = ut.regex_replace('viz_matches.show_name_matches', '#', sourcecode)
        >>> homog = False
        >>> print(ut.indent(setup, '>>> '))
        >>> ibs, qreq_, cm_list = plh.testdata_post_sver('PZ_MTEST', qaid_list=[1])
        >>> cm = cm_list[0]
        >>> cm.score_nsum(qreq_)
        >>> dnid = ibs.get_annot_nids(cm.qaid)
        >>> # +--- COPIED SECTION
        >>> locals_ = locals()
        >>> var_list = ut.exec_func_src(
        >>>     func, locals_=locals_,
        >>>     sentinal='name_annot_scores = cm.annot_score_list.take(sorted_groupxs')
        >>> exec(ut.execstr_dict(var_list))
        >>> # L___ COPIED SECTION
        >>> kwargs = {}
        >>> show_name_matches(ibs, qaid, name_daid_list, name_fm_list,
        >>>                   name_fs_list, name_h1_list, name_featflag_list,
        >>>                   qreq_=qreq_, **kwargs)
        >>> ut.quit_if_noshow()
        >>> ut.show_if_requested()
    """
    #print("SHOW NAME MATCHES")
    #print(ut.repr2(kwargs, nl=True))
    #from ibeis import constants as const
    from ibeis import tag_funcs
    draw_fmatches = kwargs.pop('draw_fmatches', True)
    rchip1, kpts1 = get_query_annot_pair_info(ibs, qaid, qreq_, draw_fmatches)
    rchip2_list, kpts2_list = get_data_annot_pair_info(ibs, name_daid_list,
                                                       qreq_, draw_fmatches)
    fm_list = name_fm_list
    fs_list = name_fs_list
    featflag_list = name_featflag_list
    offset_list, sf_list, bbox_list = show_multichip_match(rchip1, rchip2_list,
                                                           kpts1, kpts2_list,
                                                           fm_list, fs_list,
                                                           featflag_list,
                                                           **kwargs)
    aid_list = [qaid] + name_daid_list
    annotate_matches3(ibs, aid_list, bbox_list, offset_list, name_fm_list,
                      name_fs_list, qreq_=None, **kwargs)
    ax = pt.gca()
    title = vh.get_query_text(ibs, None, name_daid_list, False, qaid=qaid,
                              **kwargs)

    pt.set_title(title, ax)

    # Case tags
    annotmatch_rowid_list = ibs.get_annotmatch_rowid_from_superkey(
        [qaid] * len(name_daid_list), name_daid_list)
    annotmatch_rowid_list = ut.filter_Nones(annotmatch_rowid_list)
    tags_list = ibs.get_annotmatch_case_tags(annotmatch_rowid_list)
    if not ut.get_argflag('--show'):  # False:
        tags_list = tag_funcs.consolodate_annotmatch_tags(tags_list)
    tag_list = ut.unique_ordered(ut.flatten(tags_list))

    name_rank = kwargs.get('name_rank', None)
    truth = get_multitruth(ibs, aid_list)

    xlabel = {1: 'Correct ID', 0: 'Incorrect ID', 2: 'Unknown ID'}[truth]

    if False:
        if name_rank is None:
            xlabel = {1: 'Genuine', 0: 'Imposter', 2: 'Unknown'}[truth]
            #xlabel = {1: 'True', 0: 'False', 2: 'Unknown'}[truth]
        else:
            if name_rank == 0:
                xlabel = {
                    1: 'True Positive', 0: 'False Positive', 2: 'Unknown'}[truth]
            else:
                xlabel = {
                    1: 'False Negative', 0: 'True Negative', 2: 'Unknown'}[truth]

    if len(tag_list) > 0:
        xlabel += '\n' + ', '.join(tag_list)

    pt.set_xlabel(xlabel)
    return ax
Beispiel #40
0
def bayesnet():
    """
    References:
        https://class.coursera.org/pgm-003/lecture/17
        http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
        http://www3.cs.stonybrook.edu/~sael/teaching/cse537/Slides/chapter14d_BP.pdf
        http://www.cse.unsw.edu.au/~cs9417ml/Bayes/Pages/PearlPropagation.html
        https://github.com/pgmpy/pgmpy.git
        http://pgmpy.readthedocs.org/en/latest/
        http://nipy.bic.berkeley.edu:5000/download/11
    """
    # import operator as op
    # # Enumerate all possible events
    # varcard_list = list(map(op.attrgetter('variable_card'), cpd_list))
    # _esdat = list(ut.iprod(*map(range, varcard_list)))
    # _escol = list(map(op.attrgetter('variable'), cpd_list))
    # event_space = pd.DataFrame(_esdat, columns=_escol)

    # # Custom compression of event space to inspect a specific graph
    # def compress_space_flags(event_space, var1, var2, var3, cmp12_):
    #     """
    #     var1, var2, cmp_ = 'Lj', 'Lk', op.eq
    #     """
    #     import vtool as vt
    #     data = event_space
    #     other_cols = ut.setdiff_ordered(data.columns.tolist(), [var1, var2, var3])
    #     case_flags12 = cmp12_(data[var1], data[var2]).values
    #     # case_flags23 = cmp23_(data[var2], data[var3]).values
    #     # case_flags = np.logical_and(case_flags12, case_flags23)
    #     case_flags = case_flags12
    #     case_flags = case_flags.astype(np.int64)
    #     subspace = np.hstack((case_flags[:, None], data[other_cols].values))
    #     sel_ = vt.unique_row_indexes(subspace)
    #     flags = np.logical_and(mask, case_flags)
    #     return flags

    # # Build special cases
    # case_same   = event_space.loc[compress_space_flags(event_space, 'Li', 'Lj', 'Lk', op.eq)]
    # case_diff = event_space.loc[compress_space_flags(event_space, 'Li', 'Lj', 'Lk', op.ne)]
    # special_cases = [
    #     case_same,
    #     case_diff,
    # ]

    from pgmpy.factors import TabularCPD
    from pgmpy.models import BayesianModel
    import pandas as pd
    from pgmpy.inference import BeliefPropagation  # NOQA
    from pgmpy.inference import VariableElimination  # NOQA

    name_nice = ['n1', 'n2', 'n3']
    score_nice = ['low', 'high']
    match_nice = ['diff', 'same']
    num_names = len(name_nice)
    num_scores = len(score_nice)
    nid_basis = list(range(num_names))
    score_basis = list(range(num_scores))

    semtype2_nice = {
        'score': score_nice,
        'name': name_nice,
        'match': match_nice,
    }
    var2_cpd = {
    }
    globals()['semtype2_nice'] = semtype2_nice
    globals()['var2_cpd'] = var2_cpd

    name_combo = np.array(list(ut.iprod(nid_basis, nid_basis)))
    combo_is_same = name_combo.T[0] == name_combo.T[1]
    def get_expected_scores_prob(level1, level2):
        part1 = combo_is_same * level1
        part2 = (1 - combo_is_same) * (1 - (level2))
        expected_scores_level = part1 + part2
        return expected_scores_level

    # def make_cpd():

    def name_cpd(aid):
        from pgmpy.factors import TabularCPD
        cpd = TabularCPD(
            variable='N' + aid,
            variable_card=num_names,
            values=[[1.0 / num_names] * num_names])
        cpd.semtype = 'name'
        return cpd

    name_cpds = [name_cpd('i'), name_cpd('j'), name_cpd('k')]
    var2_cpd.update(dict(zip([cpd.variable for cpd in name_cpds], name_cpds)))
    if True:
        num_same_diff = 2
        samediff_measure = np.array([
            # get_expected_scores_prob(.12, .2),
            # get_expected_scores_prob(.88, .8),
            get_expected_scores_prob(0, 0),
            get_expected_scores_prob(1, 1),
        ])
        samediff_vals = (samediff_measure / samediff_measure.sum(axis=0)).tolist()
        def samediff_cpd(aid1, aid2):
            cpd = TabularCPD(
                variable='A' + aid1 + aid2,
                variable_card=num_same_diff,
                values=samediff_vals,
                evidence=['N' + aid1, 'N' + aid2],  # [::-1],
                evidence_card=[num_names, num_names])  # [::-1])
            cpd.semtype = 'match'
            return cpd
        samediff_cpds = [samediff_cpd('i', 'j'), samediff_cpd('j', 'k'), samediff_cpd('k', 'i')]
        var2_cpd.update(dict(zip([cpd.variable for cpd in samediff_cpds], samediff_cpds)))

        if True:
            def score_cpd(aid1, aid2):
                semtype = 'score'
                evidence = ['A' + aid1 + aid2, 'N' + aid1, 'N' + aid2]
                evidence_cpds = [var2_cpd[key] for key in evidence]
                evidence_nice = [semtype2_nice[cpd.semtype] for cpd in evidence_cpds]
                evidence_card = list(map(len, evidence_nice))
                evidence_states = list(ut.iprod(*evidence_nice))
                variable_basis = semtype2_nice[semtype]

                variable_values = []
                for mystate in variable_basis:
                    row = []
                    for state in evidence_states:
                        if state[0] == state[1]:
                            if state[2] == 'same':
                                val = .2 if mystate == 'low' else .8
                            else:
                                val = 1
                                # val = .5 if mystate == 'low' else .5
                        elif state[0] != state[1]:
                            if state[2] == 'same':
                                val = .5 if mystate == 'low' else .5
                            else:
                                val = 1
                                # val = .9 if mystate == 'low' else .1
                        row.append(val)
                    variable_values.append(row)

                cpd = TabularCPD(
                    variable='S' + aid1 + aid2,
                    variable_card=len(variable_basis),
                    values=variable_values,
                    evidence=evidence,  # [::-1],
                    evidence_card=evidence_card)  # [::-1])
                cpd.semtype = semtype
                return cpd
        else:
            score_values = [
                [.8, .1],
                [.2, .9],
            ]
            def score_cpd(aid1, aid2):
                cpd = TabularCPD(
                    variable='S' + aid1 + aid2,
                    variable_card=num_scores,
                    values=score_values,
                    evidence=['A' + aid1 + aid2],  # [::-1],
                    evidence_card=[num_same_diff])  # [::-1])
                cpd.semtype = 'score'
                return cpd

        score_cpds = [score_cpd('i', 'j'), score_cpd('j', 'k')]
        cpd_list = name_cpds + score_cpds + samediff_cpds
    else:
        score_measure = np.array([get_expected_scores_prob(level1, level2)
                                  for level1, level2 in
                                  zip(np.linspace(.1, .9, num_scores),
                                      np.linspace(.2, .8, num_scores))])

        score_values = (score_measure / score_measure.sum(axis=0)).tolist()

        def score_cpd(aid1, aid2):
            cpd = TabularCPD(
                variable='S' + aid1 + aid2,
                variable_card=num_scores,
                values=score_values,
                evidence=['N' + aid1, 'N' + aid2],
                evidence_card=[num_names, num_names])
            cpd.semtype = 'score'
            return cpd
        score_cpds = [score_cpd('i', 'j'), score_cpd('j', 'k')]
        cpd_list = name_cpds + score_cpds
        pass

    input_graph = []
    for cpd in cpd_list:
        if cpd.evidence is not None:
            for evar in cpd.evidence:
                input_graph.append((evar, cpd.variable))
    name_model = BayesianModel(input_graph)
    name_model.add_cpds(*cpd_list)

    var2_cpd.update(dict(zip([cpd.variable for cpd in cpd_list], cpd_list)))
    globals()['var2_cpd'] = var2_cpd

    varnames = [cpd.variable for cpd in cpd_list]

    # --- PRINT CPDS ---

    cpd = score_cpds[0]
    def print_cpd(cpd):
        print('CPT: %r' % (cpd,))
        index = semtype2_nice[cpd.semtype]
        if cpd.evidence is None:
            columns = ['None']
        else:
            basis_lists = [semtype2_nice[var2_cpd[ename].semtype] for ename in cpd.evidence]
            columns = [','.join(x) for x in ut.iprod(*basis_lists)]
        data = cpd.get_cpd()
        print(pd.DataFrame(data, index=index, columns=columns))

    for cpd in name_model.get_cpds():
        print('----')
        print(cpd._str('phi'))
        print_cpd(cpd)

    # --- INFERENCE ---

    Ni = name_cpds[0]

    event_space_combos = {}
    event_space_combos[Ni.variable] = 0  # Set ni to always be Fred
    for cpd in cpd_list:
        if cpd.semtype == 'score':
            event_space_combos[cpd.variable] = list(range(cpd.variable_card))
    evidence_dict = ut.all_dict_combinations(event_space_combos)

    # Query about name of annotation k given different event space params

    def pretty_evidence(evidence):
        return [key + '=' + str(semtype2_nice[var2_cpd[key].semtype][val])
                for key, val in evidence.items()]

    def print_factor(factor):
        row_cards = factor.cardinality
        row_vars = factor.variables
        values = factor.values.reshape(np.prod(row_cards), 1).flatten()
        # col_cards = 1
        # col_vars = ['']
        basis_lists = list(zip(*list(ut.iprod(*[range(c) for c in row_cards]))))
        nice_basis_lists = []
        for varname, basis in zip(row_vars, basis_lists):
            cpd = var2_cpd[varname]
            _nice_basis = ut.take(semtype2_nice[cpd.semtype], basis)
            nice_basis = ['%s=%s' % (varname, val) for val in _nice_basis]
            nice_basis_lists.append(nice_basis)
        row_lbls = [', '.join(sorted(x)) for x in zip(*nice_basis_lists)]
        print(ut.repr3(dict(zip(row_lbls, values)), precision=3, align=True, key_order_metric='-val'))

    # name_belief = BeliefPropagation(name_model)
    name_belief = VariableElimination(name_model)
    import pgmpy
    import six  # NOQA

    def try_query(evidence):
        print('--------')
        query_vars = ut.setdiff_ordered(varnames, list(evidence.keys()))
        evidence_str = ', '.join(pretty_evidence(evidence))
        probs = name_belief.query(query_vars, evidence)
        factor_list = probs.values()
        joint_factor = pgmpy.factors.factor_product(*factor_list)
        print('P(' + ', '.join(query_vars) + ' | ' + evidence_str + ')')
        # print(six.text_type(joint_factor))
        factor = joint_factor  # NOQA
        # print_factor(factor)
        # import utool as ut
        print(ut.hz_str([(f._str(phi_or_p='phi')) for f in factor_list]))

    for evidence in evidence_dict:
        try_query(evidence)

    evidence = {'Aij': 1, 'Ajk': 1, 'Aki': 1, 'Ni': 0}
    try_query(evidence)

    evidence = {'Aij': 0, 'Ajk': 0, 'Aki': 0, 'Ni': 0}
    try_query(evidence)

    globals()['score_nice'] = score_nice
    globals()['name_nice'] = name_nice
    globals()['score_basis'] = score_basis
    globals()['nid_basis'] = nid_basis

    print('Independencies')
    print(name_model.get_independencies())
    print(name_model.local_independencies([Ni.variable]))

    # name_belief = BeliefPropagation(name_model)
    # # name_belief = VariableElimination(name_model)
    # for case in special_cases:
    #     test_data = case.drop('Lk', axis=1)
    #     test_data = test_data.reset_index(drop=True)
    #     print('----')
    #     for i in range(test_data.shape[0]):
    #         evidence = test_data.loc[i].to_dict()
    #         probs = name_belief.query(['Lk'], evidence)
    #         factor = probs['Lk']
    #         probs = factor.values
    #         evidence_ = evidence.copy()
    #         evidence_['Li'] = name_nice[evidence['Li']]
    #         evidence_['Lj'] = name_nice[evidence['Lj']]
    #         evidence_['Sij'] = score_nice[evidence['Sij']]
    #         evidence_['Sjk'] = score_nice[evidence['Sjk']]
    #         nice2_prob = ut.odict(zip(name_nice, probs.tolist()))
    #         ut.print_python_code('P(Lk | {evidence}) = {cpt}'.format(
    #             evidence=(ut.repr2(evidence_, explicit=True, nobraces=True, strvals=True)),
    #             cpt=ut.repr3(nice2_prob, precision=3, align=True, key_order_metric='-val')
    #         ))

    # for case in special_cases:
    #     test_data = case.drop('Lk', axis=1)
    #     test_data = test_data.drop('Lj', axis=1)
    #     test_data = test_data.reset_index(drop=True)
    #     print('----')
    #     for i in range(test_data.shape[0]):
    #         evidence = test_data.loc[i].to_dict()
    #         query_vars = ['Lk', 'Lj']
    #         probs = name_belief.query(query_vars, evidence)
    #         for queryvar in query_vars:
    #             factor = probs[queryvar]
    #             print(factor._str('phi'))
    #             probs = factor.values
    #             evidence_ = evidence.copy()
    #             evidence_['Li'] = name_nice[evidence['Li']]
    #             evidence_['Sij'] = score_nice[evidence['Sij']]
    #             evidence_['Sjk'] = score_nice[evidence['Sjk']]
    #             nice2_prob = ut.odict(zip([queryvar + '=' + x for x in name_nice], probs.tolist()))
    #             ut.print_python_code('P({queryvar} | {evidence}) = {cpt}'.format(
    #                 query_var=query_var,
    #                 evidence=(ut.repr2(evidence_, explicit=True, nobraces=True, strvals=True)),
    #                 cpt=ut.repr3(nice2_prob, precision=3, align=True, key_order_metric='-val')
    #             ))

    # _ draw model

    import plottool as pt
    import networkx as netx
    fig = pt.figure()  # NOQA
    fig.clf()
    ax = pt.gca()

    netx_nodes = [(node, {}) for node in name_model.nodes()]
    netx_edges = [(etup[0], etup[1], {}) for etup in name_model.edges()]
    netx_graph = netx.DiGraph()
    netx_graph.add_nodes_from(netx_nodes)
    netx_graph.add_edges_from(netx_edges)

    # pos = netx.graphviz_layout(netx_graph)
    pos = netx.pydot_layout(netx_graph, prog='dot')
    netx.draw(netx_graph, pos=pos, ax=ax, with_labels=True)

    pt.plt.savefig('foo.png')
    ut.startfile('foo.png')
Beispiel #41
0
def hackshow_names(ibs, aid_list, fnum=None):
    r"""
    Args:
        ibs (IBEISController):  ibeis controller object
        aid_list (list):

    CommandLine:
        python -m ibeis.other.dbinfo --exec-hackshow_names --show
        python -m ibeis.other.dbinfo --exec-hackshow_names --show --db PZ_Master1

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.other.dbinfo import *  # NOQA
        >>> import ibeis
        >>> ibs = ibeis.opendb(defaultdb='PZ_MTEST')
        >>> aid_list = ibs.get_valid_aids()
        >>> result = hackshow_names(ibs, aid_list)
        >>> print(result)
        >>> ut.show_if_requested()
    """
    import plottool as pt
    import vtool as vt
    grouped_aids, nid_list = ibs.group_annots_by_name(aid_list)
    grouped_aids = [aids for aids in grouped_aids if len(aids) > 1]
    unixtimes_list = ibs.unflat_map(ibs.get_annot_image_unixtimes_asfloat, grouped_aids)
    yaws_list = ibs.unflat_map(ibs.get_annot_yaws, grouped_aids)
    #markers_list = [[(1, 2, yaw * 360 / (np.pi * 2)) for yaw in yaws] for yaws in yaws_list]

    unixtime_list = ut.flatten(unixtimes_list)
    timemax = np.nanmax(unixtime_list)
    timemin = np.nanmin(unixtime_list)
    timerange = timemax - timemin
    unixtimes_list = [((unixtimes[:] - timemin) / timerange) for unixtimes in unixtimes_list]
    for unixtimes in unixtimes_list:
        num_nan = sum(np.isnan(unixtimes))
        unixtimes[np.isnan(unixtimes)] = np.linspace(-1, -.5, num_nan)
    #ydata_list = [np.arange(len(aids)) for aids in grouped_aids]
    sortx_list = vt.argsort_groups(unixtimes_list, reverse=False)
    #markers_list = ut.list_ziptake(markers_list, sortx_list)
    yaws_list = ut.list_ziptake(yaws_list, sortx_list)
    ydatas_list = vt.ziptake(unixtimes_list, sortx_list)
    #ydatas_list = sortx_list
    #ydatas_list = vt.argsort_groups(unixtimes_list, reverse=False)

    # Sort by num members
    #ydatas_list = ut.take(ydatas_list, np.argsort(list(map(len, ydatas_list))))
    xdatas_list = [np.zeros(len(ydatas)) + count for count, ydatas in enumerate(ydatas_list)]
    #markers = ut.flatten(markers_list)
    #yaws = np.array(ut.flatten(yaws_list))
    y_data = np.array(ut.flatten(ydatas_list))
    x_data = np.array(ut.flatten(xdatas_list))
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum)
    ax = pt.gca()

    #unique_yaws, groupxs = vt.group_indices(yaws)

    ax.scatter(x_data, y_data, color=[1, 0, 0], s=1, marker='.')
    #pt.draw_stems(x_data, y_data, marker=markers, setlims=True, linestyle='')
    pt.dark_background()
    ax = pt.gca()
    ax.set_xlim(min(x_data) - .1, max(x_data) + .1)
    ax.set_ylim(min(y_data) - .1, max(y_data) + .1)
Beispiel #42
0
def show_model(model, evidence={}, soft_evidence={}, **kwargs):
    """
    References:
        http://stackoverflow.com/questions/22207802/pygraphviz-networkx-set-node-level-or-layer

    Ignore:
        pkg-config --libs-only-L libcgraph
        sudo apt-get  install libgraphviz-dev -y
        sudo apt-get  install libgraphviz4 -y

        # sudo apt-get install pkg-config
        sudo apt-get install libgraphviz-dev
        # pip install git+git://github.com/pygraphviz/pygraphviz.git
        pip install pygraphviz
        python -c "import pygraphviz; print(pygraphviz.__file__)"

        sudo pip3 install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"
        python3 -c "import pygraphviz; print(pygraphviz.__file__)"

    CommandLine:
        python -m ibeis.algo.hots.bayes --exec-show_model --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.algo.hots.bayes import *  # NOQA
        >>> model = '?'
        >>> evidence = {}
        >>> soft_evidence = {}
        >>> result = show_model(model, evidence, soft_evidence)
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    if ut.get_argval('--hackmarkov') or ut.get_argval('--hackjunc'):
        draw_tree_model(model, **kwargs)
        return

    import plottool as pt
    import networkx as netx
    fnum = pt.ensure_fnum(None)
    netx_graph = (model)
    #netx_graph.graph.setdefault('graph', {})['size'] = '"10,5"'
    #netx_graph.graph.setdefault('graph', {})['rankdir'] = 'LR'

    pos_dict = get_hacked_pos(netx_graph)
    #pos_dict = netx.pygraphviz_layout(netx_graph)
    #pos = netx.pydot_layout(netx_graph, prog='dot')
    #pos_dict = netx.graphviz_layout(netx_graph)

    textprops = {
        'family': 'monospace',
        'horizontalalignment': 'left',
        #'horizontalalignment': 'center',
        #'size': 12,
        'size': 8,
    }

    netx_nodes = model.nodes(data=True)
    node_key_list = ut.get_list_column(netx_nodes, 0)
    pos_list = ut.dict_take(pos_dict, node_key_list)

    var2_post = {f.variables[0]: f for f in kwargs.get('factor_list', [])}

    prior_text = None
    post_text = None
    evidence_tas = []
    post_tas = []
    prior_tas = []
    node_color = []

    has_infered = evidence or var2_post
    if has_infered:
        ignore_prior_with_ttype = ['score', 'match']
        show_prior = False
    else:
        ignore_prior_with_ttype = []
        #show_prior = True
        show_prior = False

    dpy = 5
    dbx, dby = (20, 20)
    takw1 = {'bbox_align': (.5, 0), 'pos_offset': [0, dpy], 'bbox_offset': [dbx, dby]}
    takw2 = {'bbox_align': (.5, 1), 'pos_offset': [0, -dpy], 'bbox_offset': [-dbx, -dby]}

    name_colors = pt.distinct_colors(max(model.num_names, 10))
    name_colors = name_colors[:model.num_names]

    #cmap_ = 'hot' #mx = 0.65 #mn = 0.15
    cmap_, mn, mx = 'plasma', 0.15, 1.0
    _cmap = pt.plt.get_cmap(cmap_)
    def cmap(x):
        return _cmap((x * mx) + mn)

    for node, pos in zip(netx_nodes, pos_list):
        variable = node[0]
        cpd = model.var2_cpd[variable]
        prior_marg = (cpd if cpd.evidence is None else
                      cpd.marginalize(cpd.evidence, inplace=False))

        show_evidence = variable in evidence
        show_prior = cpd.ttype not in ignore_prior_with_ttype
        show_post = variable in var2_post
        show_prior |= cpd.ttype not in ignore_prior_with_ttype

        post_marg = None

        if show_post:
            post_marg = var2_post[variable]

        def get_name_color(phi):
            order = phi.values.argsort()[::-1]
            if len(order) < 2:
                dist_next = phi.values[order[0]]
            else:
                dist_next = phi.values[order[0]] - phi.values[order[1]]
            dist_total = (phi.values[order[0]])
            confidence = (dist_total * dist_next) ** (2.5 / 4)
            #print('confidence = %r' % (confidence,))
            color = name_colors[order[0]]
            color = pt.color_funcs.desaturate_rgb(color, 1 - confidence)
            color = np.array(color)
            return color

        if variable in evidence:
            if cpd.ttype == 'score':
                cmap_index = evidence[variable] / (cpd.variable_card - 1)
                color = cmap(cmap_index)
                color = pt.lighten_rgb(color, .4)
                color = np.array(color)
                node_color.append(color)
            elif cpd.ttype == 'name':
                color = name_colors[evidence[variable]]
                color = np.array(color)
                node_color.append(color)
            else:
                color = pt.FALSE_RED
                node_color.append(color)
        #elif variable in soft_evidence:
        #    color = pt.LIGHT_PINK
        #    show_prior = True
        #    color = get_name_color(prior_marg)
        #    node_color.append(color)
        else:
            if cpd.ttype == 'name' and post_marg is not None:
                color = get_name_color(post_marg)
                node_color.append(color)
            elif cpd.ttype == 'match' and post_marg is not None:
                color = cmap(post_marg.values[1])
                color = pt.lighten_rgb(color, .4)
                color = np.array(color)
                node_color.append(color)
            else:
                #color = pt.WHITE
                color = pt.NEUTRAL
                node_color.append(color)

        if show_prior:
            if variable in soft_evidence:
                prior_color = pt.LIGHT_PINK
            else:
                prior_color = None
            prior_text = pgm_ext.make_factor_text(prior_marg, 'prior')
            prior_tas.append(dict(text=prior_text, pos=pos, color=prior_color, **takw2))
        if show_evidence:
            _takw1 = takw1
            if cpd.ttype == 'score':
                _takw1 = takw2
            evidence_text = cpd.variable_statenames[evidence[variable]]
            if isinstance(evidence_text, int):
                evidence_text = '%d/%d' % (evidence_text + 1, cpd.variable_card)
            evidence_tas.append(dict(text=evidence_text, pos=pos, color=color, **_takw1))
        if show_post:
            _takw1 = takw1
            if cpd.ttype == 'match':
                _takw1 = takw2
            post_text = pgm_ext.make_factor_text(post_marg, 'post')
            post_tas.append(dict(text=post_text, pos=pos, color=None, **_takw1))

    def trnps_(dict_list):
        """ tranpose dict list """
        list_dict = ut.ddict(list)
        for dict_ in dict_list:
            for key, val in dict_.items():
                list_dict[key + '_list'].append(val)
        return list_dict

    takw1_ = trnps_(post_tas + evidence_tas)
    takw2_ = trnps_(prior_tas)

    # Draw graph
    if has_infered:
        pnum1 = (3, 1, (slice(0, 2), 0))
    else:
        pnum1 = None

    fig = pt.figure(fnum=fnum, pnum=pnum1, doclf=True)  # NOQA
    ax = pt.gca()
    #print('node_color = %s' % (ut.repr3(node_color),))
    drawkw = dict(pos=pos_dict, ax=ax, with_labels=True, node_size=1500,
                  node_color=node_color)
    netx.draw(netx_graph, **drawkw)

    hacks = []
    if len(post_tas + evidence_tas):
        hacks.append(pt.draw_text_annotations(textprops=textprops, **takw1_))
    if prior_tas:
        hacks.append(pt.draw_text_annotations(textprops=textprops, **takw2_))

    xmin, ymin = np.array(pos_list).min(axis=0)
    xmax, ymax = np.array(pos_list).max(axis=0)
    num_annots = len(model.ttype2_cpds['name'])
    if num_annots > 4:
        ax.set_xlim((xmin - 40, xmax + 40))
        ax.set_ylim((ymin - 50, ymax + 50))
        fig.set_size_inches(30, 7)
    else:
        ax.set_xlim((xmin - 42, xmax + 42))
        ax.set_ylim((ymin - 50, ymax + 50))
        fig.set_size_inches(23, 7)
    fig = pt.gcf()

    title = 'num_names=%r, num_annots=%r' % (model.num_names, num_annots,)
    map_assign = kwargs.get('map_assign', None)

    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        map_assign, map_prob = top_assignments[0]
        if map_assign is not None:
            def word_insert(text):
                return '' if len(text) == 0 else text + ' '
            title += '\n%sMAP: ' % (word_insert(kwargs.get('method', '')))
            title += map_assign + ' @' + '%.2f%%' % (100 * map_prob,)
    if kwargs.get('show_title', True):
        pt.set_figtitle(title, size=14)

    for hack in hacks:
        hack()

    # Hack in colorbars
    if has_infered:
        pt.colorbar(np.linspace(0, 1, len(name_colors)), name_colors, lbl='name',
                    ticklabels=model.ttype2_template['name'].basis, ticklocation='left')

        basis = model.ttype2_template['score'].basis
        scalars = np.linspace(0, 1, len(basis))
        scalars = np.linspace(0, 1, 100)
        colors = pt.scores_to_color(scalars, cmap_=cmap_, reverse_cmap=False,
                                    cmap_range=(mn, mx))
        colors = [pt.lighten_rgb(c, .4) for c in colors]

        if ut.list_type(basis) is int:
            pt.colorbar(scalars, colors, lbl='score', ticklabels=np.array(basis) + 1)
        else:
            pt.colorbar(scalars, colors, lbl='score', ticklabels=basis)
            #print('basis = %r' % (basis,))

    # Draw probability hist
    if has_infered and top_assignments is not None:
        bin_labels = ut.get_list_column(top_assignments, 0)
        bin_vals =  ut.get_list_column(top_assignments, 1)

        # bin_labels = ['\n'.join(ut.textwrap.wrap(_lbl, width=30)) for _lbl in bin_labels]

        pt.draw_histogram(bin_labels, bin_vals, fnum=fnum, pnum=(3, 8, (2, slice(4, None))),
                          transpose=True,
                          use_darkbackground=False,
                          #xtick_rotation=-10,
                          ylabel='Prob', xlabel='assignment')
        pt.set_title('Assignment probabilities')
Beispiel #43
0
    def plot_chip(self, aid, nRows, nCols, px, fulldraw=True, **kwargs):
        """ Plots an individual chip in a subaxis """
        ibs = self.ibs
        if aid in [self.aid1, self.aid2]:
            # Bold color for the matching chips
            lw = 5
            text_color = np.array((135, 206, 235, 255)) / 255.0
        else:
            lw = 2
            text_color = None

        pnum = (nRows, nCols, px)
        if not fulldraw:
            # not doing full draw so we have to clear any axes
            # that are here already manually
            ax = self.fig.add_subplot(*pnum)
            self.clear_parent_axes(ax)
            #ut.embed()
            #print(subax)

        viz_chip_kw = {
            'fnum': self.fnum,
            'pnum': pnum,
            'nokpts': True,
            'show_name': True,
            'show_gname': False,
            'show_aidstr': True,
            'notitle': True,
            'show_num_gt': False,
            'text_color': text_color,
        }
        if False and ut.is_developer():
            enable_chip_title_prefix = True
            viz_chip_kw.update(
                {
                    'enable_chip_title_prefix': enable_chip_title_prefix,
                    'show_name': True,
                    'show_aidstr': True,
                    'show_yawtext': True,
                    'show_num_gt': True,
                    'show_quality_text': True,
                }
            )

        viz_chip.show_chip(ibs, aid, **viz_chip_kw)
        ax = pt.gca()
        pt.draw_border(ax, color=kwargs.get('color'), lw=lw)
        if kwargs.get('make_buttons', True):
            #divider = pt.ensure_divider(ax)
            butkw = {
                #'divider': divider,
                'ax': ax,
                'size': '13%'
                #'size': '15%'
            }
        # Chip matching/naming options
        nid = ibs.get_annot_name_rowids(aid)
        annotation_unknown = ibs.is_nid_unknown([nid])[0]
        if not annotation_unknown:
            # remove name
            callback = functools.partial(self.unname_annotation, aid)
            self.append_button('remove name (' + ibs.get_name_texts(nid) + ')', callback=callback, **butkw)
        else:
            # new name
            callback = functools.partial(self.mark_annotation_as_new_name, aid)
            self.append_button('mark as new name', callback=callback, **butkw)
        if nid != self.nid2 and not ibs.is_nid_unknown([self.nid2])[0] and not self.is_split_case:
            # match to nid2
            callback = functools.partial(self.rename_annotation, aid, self.nid2)
            text = 'match to name2: ' + ibs.get_name_texts(self.nid2)
            self.append_button(text, callback=callback, **butkw)
        if nid != self.nid1 and not ibs.is_nid_unknown([self.nid1])[0]:
            # match to nid1
            callback = functools.partial(self.rename_annotation, aid, self.nid1)
            text = 'match to name1: ' + ibs.get_name_texts(self.nid1)
            self.append_button(text, callback=callback, **butkw)

        other_nid_list = self.get_other_nids()
        for other_nid in other_nid_list:
            if other_nid == nid:
                continue
            # rename nid2
            callback = functools.partial(self.rename_annotation, aid, other_nid)
            text = 'match to: ' + ibs.get_name_texts(other_nid)
            self.append_button(text, callback=callback, **butkw)
        return ax
Beispiel #44
0
def draw_bayesian_model(model,
                        evidence={},
                        soft_evidence={},
                        fnum=None,
                        pnum=None,
                        **kwargs):

    from pgmpy.models import BayesianModel
    if not isinstance(model, BayesianModel):
        model = model.to_bayesian_model()

    import plottool as pt
    import networkx as nx
    kwargs = kwargs.copy()
    factor_list = kwargs.pop('factor_list', [])

    ttype_colors, ttype_scalars = make_colorcodes(model)

    textprops = {
        'horizontalalignment': 'left',
        'family': 'monospace',
        'size': 8,
    }

    # build graph attrs
    tup = get_node_viz_attrs(model, evidence, soft_evidence, factor_list,
                             ttype_colors, **kwargs)
    node_color, pos_list, pos_dict, takws = tup

    # draw graph
    has_infered = evidence or 'factor_list' in kwargs

    if False:
        fig = pt.figure(fnum=fnum, pnum=pnum, doclf=True)  # NOQA
        ax = pt.gca()
        drawkw = dict(pos=pos_dict,
                      ax=ax,
                      with_labels=True,
                      node_size=1100,
                      node_color=node_color)
        nx.draw(model, **drawkw)
    else:
        # BE VERY CAREFUL
        if 1:
            graph = model.copy()
            graph.__class__ = nx.DiGraph
            graph.graph['groupattrs'] = ut.ddict(dict)
            #graph = model.
            if getattr(graph, 'ttype2_cpds', None) is not None:
                # Add invis edges and ttype groups
                for ttype in model.ttype2_cpds.keys():
                    ttype_cpds = model.ttype2_cpds[ttype]
                    # use defined ordering
                    ttype_nodes = ut.list_getattr(ttype_cpds, 'variable')
                    # ttype_nodes = sorted(ttype_nodes)
                    invis_edges = list(ut.itertwo(ttype_nodes))
                    graph.add_edges_from(invis_edges)
                    nx.set_edge_attributes(
                        graph, 'style',
                        {edge: 'invis'
                         for edge in invis_edges})
                    nx.set_node_attributes(
                        graph, 'groupid',
                        {node: ttype
                         for node in ttype_nodes})
                    graph.graph['groupattrs'][ttype]['rank'] = 'same'
                    graph.graph['groupattrs'][ttype]['cluster'] = False
        else:
            graph = model
        pt.show_nx(graph,
                   layout_kw={'prog': 'dot'},
                   fnum=fnum,
                   pnum=pnum,
                   verbose=0)
        pt.zoom_factory()
        fig = pt.gcf()
        ax = pt.gca()
        pass
    hacks = [
        pt.draw_text_annotations(textprops=textprops, **takw) for takw in takws
        if takw
    ]

    xmin, ymin = np.array(pos_list).min(axis=0)
    xmax, ymax = np.array(pos_list).max(axis=0)
    if 'name' in model.ttype2_template:
        num_names = len(model.ttype2_template['name'].basis)
        num_annots = len(model.ttype2_cpds['name'])
        if num_annots > 4:
            ax.set_xlim((xmin - 40, xmax + 40))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(30, 7)
        else:
            ax.set_xlim((xmin - 42, xmax + 42))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(23, 7)
        title = 'num_names=%r, num_annots=%r' % (
            num_names,
            num_annots,
        )
    else:
        title = ''
    map_assign = kwargs.get('map_assign', None)

    def word_insert(text):
        return '' if len(text) == 0 else text + ' '

    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        map_assign, map_prob = top_assignments[0]
        if map_assign is not None:
            title += '\n%sMAP: ' % (word_insert(kwargs.get('method', '')))
            title += map_assign + ' @' + '%.2f%%' % (100 * map_prob, )
    if kwargs.get('show_title', True):
        pt.set_figtitle(title, size=14)

    for hack in hacks:
        hack()

    if has_infered:
        # Hack in colorbars
        # if ut.list_type(basis) is int:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=np.array(basis) + 1)
        # else:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=basis)
        keys = ['name', 'score']
        locs = ['left', 'right']
        for key, loc in zip(keys, locs):
            if key in ttype_colors:
                basis = model.ttype2_template[key].basis
                # scalars =
                colors = ttype_colors[key]
                scalars = ttype_scalars[key]
                pt.colorbar(scalars,
                            colors,
                            lbl=key,
                            ticklabels=basis,
                            ticklocation=loc)
Beispiel #45
0
def do_infr_test(ccs, edges, new_edges):
    """
    Creates a graph with `ccs` + `edges` and then adds `new_edges`
    """
    # import networkx as nx
    import plottool as pt

    infr = demo.make_demo_infr(ccs, edges)

    if ut.show_was_requested():
        pt.qtensure()

    # Preshow
    fnum = 1
    if ut.show_was_requested():
        infr.set_node_attrs('shape', 'circle')
        infr.show(pnum=(2, 1, 1),
                  fnum=fnum,
                  show_unreviewed_edges=True,
                  show_reviewed_cuts=True,
                  splines='spline',
                  show_inferred_diff=True,
                  groupby='name_label',
                  show_labels=True,
                  pickable=True)
        pt.set_title('pre-review')
        pt.gca().set_aspect('equal')
        infr.set_node_attrs('pin', 'true')
        # fig1 = pt.gcf()
        # fig1.canvas.mpl_connect('pick_event', ut.partial(on_pick, infr=infr))

    infr1 = infr
    infr2 = infr.copy()
    for new_edge in new_edges:
        aid1, aid2, data = new_edge
        evidence_decision = data['evidence_decision']
        infr2.add_feedback((aid1, aid2), evidence_decision)
    infr2.relabel_using_reviews(rectify=False)
    infr2.apply_nondynamic_update()

    # Postshow
    if ut.show_was_requested():
        infr2.show(pnum=(2, 1, 2),
                   fnum=fnum,
                   show_unreviewed_edges=True,
                   show_inferred_diff=True,
                   show_labels=True)
        pt.gca().set_aspect('equal')
        pt.set_title('post-review')
        # fig2 = pt.gcf()
        # if fig2 is not fig1:
        #     fig2.canvas.mpl_connect('pick_event', ut.partial(on_pick, infr=infr2))

    class Checker(object):
        """
        Asserts pre and post test properties of the graph
        """
        def __init__(self, infr1, infr2):
            self._errors = []
            self.infr1 = infr1
            self.infr2 = infr2

        def __call__(self, infr, u, v, key, val, msg):
            data = infr.get_nonvisual_edge_data((u, v))
            if data is None:
                assert infr.graph.has_edge(u, v), ('uv=%r, %r does not exist' %
                                                   (u, v))
            got = data.get(key)
            if got != val:
                msg1 = 'key=%s %r!=%r, ' % (key, got, val)
                errmsg = ''.join([
                    msg1, msg, '\nedge=',
                    ut.repr2((u, v)), '\n',
                    infr.repr_edge_data(data)
                ])
                self._errors.append(errmsg)

        def custom_precheck(self, func):
            try:
                func(self.infr1)
            except AssertionError as ex:
                self._errors.append(str(ex))

        def after(self, errors=[]):
            """
            Delays error reporting until after visualization

            prints errors, then shows you the graph, then
            finally if any errors were discovered they are raised
            """

            errors = errors + self._errors
            if errors:
                ut.cprint('PRINTING %d FAILURE' % (len(errors)), 'red')
                for msg in errors:
                    print(msg)
                ut.cprint('HAD %d FAILURE' % (len(errors)), 'red')
            if ut.show_was_requested():
                pt.all_figures_tile(percent_w=.5)
                ut.show_if_requested()
            if errors:
                raise AssertionError('There were errors')

    check = Checker(infr1, infr2)
    return infr1, infr2, check
Beispiel #46
0
def test_featweight_worker():
    """
    test function

    python -m ibeis.algo.preproc.preproc_featweight --test-gen_featweight_worker --show --cnn
    """
    import ibeis
    qreq_ = ibeis.main_helpers.testdata_qreq_(defaultdb='PZ_MTEST', p=['default:fw_detector=cnn'], qaid_override=[1])
    ibs = qreq_.ibs
    config2_ = qreq_.qparams
    lazy = True
    aid_list            = qreq_.get_external_qaids()
    #aid_list = ibs.get_valid_aids()[0:30]
    kpts_list           = ibs.get_annot_kpts(aid_list)
    chipsize_list       = ibs.get_annot_chip_sizes(aid_list, config2_=config2_)
    probchip_fpath_list = preproc_probchip.compute_and_write_probchip(ibs,
                                                                      aid_list,
                                                                      lazy=lazy,
                                                                      config2_=config2_)
    print('probchip_fpath_list = %r' % (probchip_fpath_list,))
    probchip_list       = [vt.imread(fpath, grayscale=True) if exists(fpath) else None
                           for fpath in probchip_fpath_list]

    _iter = list(zip(aid_list, kpts_list, probchip_list, chipsize_list))
    _iter = ut.InteractiveIter(_iter, enabled=ut.get_argflag('--show'))
    for aid, kpts, probchip, chipsize in _iter:
        #kpts     = kpts_list[0]
        #aid      = aid_list[0]
        #probchip = probchip_list[0]
        #chipsize = chipsize_list[0]
        tup = (aid, kpts, probchip, chipsize)
        (aid, weights) = gen_featweight_worker(tup)
        if aid == 3 and ibs.get_dbname() == 'testdb1':
            # Run Asserts if not interactive
            weights_03_test = weights[0:3]
            print('weights[0:3] = %r' % (weights_03_test,))
            #weights_03_target = [ 0.098, 0.155,  0.422]
            #weights_03_target = [ 0.324, 0.407,  0.688]
            #weights_thresh    = [ 0.09, 0.09,  0.09]
            #ut.assert_almost_eq(weights_03_test, weights_03_target, weights_thresh)
            ut.assert_inbounds(weights_03_test, 0, 1)
            if not ut.show_was_requested():
                break
        if ut.show_was_requested():
            import plottool as pt
            #sfx, sfy = (probchip.shape[1] / chipsize[0], probchip.shape[0] / chipsize[1])
            #kpts_ = vt.offset_kpts(kpts, (0, 0), (sfx, sfy))
            pnum_ = pt.make_pnum_nextgen(1, 3)  # *pt.get_square_row_cols(4))
            fnum = 1
            pt.figure(fnum=fnum, doclf=True)
            ###
            pt.imshow(ibs.get_annot_chips(aid, config2_=config2_), pnum=pnum_(0), fnum=fnum)
            if ut.get_argflag('--numlbl'):
                pt.gca().set_xlabel('(1)')
            ###
            pt.imshow(probchip, pnum=pnum_(2), fnum=fnum)
            if ut.get_argflag('--numlbl'):
                pt.gca().set_xlabel('(2)')
            #pt.draw_kpts2(kpts_, ell_alpha=.4, color_list=pt.ORANGE)
            ###
            #pt.imshow(probchip, pnum=pnum_(3), fnum=fnum)
            #color_list = pt.draw_kpts2(kpts_, weights=weights, ell_alpha=.7, cmap_='jet')
            #cb = pt.colorbar(weights, color_list)
            #cb.set_label('featweights')
            ###
            pt.imshow(ibs.get_annot_chips(aid, config2_=qreq_.qparams), pnum=pnum_(1), fnum=fnum)
            #color_list = pt.draw_kpts2(kpts, weights=weights, ell_alpha=.3, cmap_='jet')
            color_list = pt.draw_kpts2(kpts, weights=weights, ell_alpha=.3)
            cb = pt.colorbar(weights, color_list)
            cb.set_label('featweights')
            if ut.get_argflag('--numlbl'):
                pt.gca().set_xlabel('(3)')
            #pt.draw_kpts2(kpts, ell_alpha=.4)
            pt.draw()
            pt.show_if_requested()
Beispiel #47
0
def show_coverage_grid(num_rows, num_cols, subbin_xy_arr,
                       neighbor_bin_centers, neighbor_bin_weights,
                       neighbor_bin_indices, fnum=None, pnum=None):
    """
    visualizes the voting scheme on the grid. (not a mask, and no max)
    """
    import plottool as pt
    import vtool as vt
    import matplotlib as mpl

    if fnum is None:
        fnum = pt.next_fnum()
    fig = pt.figure(fnum, pnum=pnum)
    ax = fig.gca()
    x_edge_indices = np.arange(num_cols)
    y_edge_indices = np.arange(num_rows)
    x_center_indices = vt.hist_edges_to_centers(x_edge_indices)
    y_center_indices = vt.hist_edges_to_centers(y_edge_indices)
    x_center_grid, y_center_grid = np.meshgrid(x_center_indices, y_center_indices)
    ax.set_xticks(x_edge_indices)
    ax.set_yticks(y_edge_indices)
    # Plot keypoint loc
    ax.scatter(subbin_xy_arr[0], subbin_xy_arr[1], marker='o')
    # Plot Weighted Lines to Subbin
    pt_colors = pt.distinct_colors(len(subbin_xy_arr.T))
    segment_list = []
    color_list = []
    for subbin_centers, subbin_weights in zip(neighbor_bin_centers,
                                              neighbor_bin_weights):
        for pt_xys, center_xys, weight, color in zip(subbin_xy_arr.T, subbin_centers,
                                                     subbin_weights, pt_colors):
            # Adjsut weight to alpha for easier visualization
            alpha = weight
            INCRESE_ALPHA_VISIBILITY = True
            if INCRESE_ALPHA_VISIBILITY:
                min_viz_alpha = .05
                alpha = alpha * (1.0 - min_viz_alpha) + min_viz_alpha
                alpha **= 1.0
            #pt.plots.colorline(
            segment = np.vstack((pt_xys, center_xys))
            segment_list.append(segment)
            # Alpha becomes part of the colors
            color_list.append(list(color) + [alpha])
            # DO NOT USE PLOT VERY SLOW
            #ax.plot(*segment.T, color=color, alpha=alpha, lw=3)
    ax = pt.gca()
    # Plot all segments in single  line collection for speed
    # solid | dashed | dashdot | dotted
    lc = mpl.collections.LineCollection(segment_list, colors=color_list, linewidth=3, linestyles='solid')
    ax.add_collection(lc)
    # Plot Grid Center
    num_cells = num_cols * num_rows
    grid_alpha = min(.4, max(1 - (num_cells / 500), .1))
    grid_color = [.6, .6, .6, grid_alpha]
    #print(grid_color)
    # Plot grid cetner
    ax.scatter(x_center_grid, y_center_grid, marker='.', color=grid_color,
               s=grid_alpha)

    ax.set_xlim(0, num_cols - 1)
    ax.set_ylim(0, num_rows - 1)
    #-----
    pt.dark_background()
    ax.grid(True, color=[.3, .3, .3])
    ax.set_xticklabels([])
    ax.set_yticklabels([])
Beispiel #48
0
def viz_netx_chipgraph(ibs, netx_graph, fnum=None, with_images=False, zoom=ZOOM):
    r"""
    Args:
        ibs (IBEISController):  ibeis controller object
        netx_graph (?):
        fnum (int):  figure number(default = None)
        with_images (bool): (default = False)
        zoom (float): (default = 0.4)

    Returns:
        ?: pos

    CommandLine:
        python -m ibeis.viz.viz_graph --exec-viz_netx_chipgraph --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_graph import *  # NOQA
        >>> import ibeis
        >>> ibs = ibeis.opendb(defaultdb='testdb1')
        >>> nid_list = ibs.get_valid_nids()[0:5]
        >>> fnum = None
        >>> with_images = True
        >>> zoom = 0.4
        >>> #pos = viz_netx_chipgraph(ibs, netx_graph, fnum, with_images, zoom)
        >>> make_name_graph_interaction(ibs, None, ibs.get_valid_aids()[0:1])
        >>> #make_name_graph_interaction(ibs, nid_list)
        >>> ut.show_if_requested()
    """
    if fnum is None:
        fnum = pt.next_fnum()

    #zoom = .8
    print('[viz_graph] drawing chip graph')
    pt.figure(fnum=fnum, pnum=(1, 1, 1))
    ax = pt.gca()
    #pos = netx.spring_layout(graph)

    aid_list = netx_graph.nodes()

    IMPLICIT_LAYOUT = len(set(ibs.get_annot_nids(aid_list))) != 1
    # FIXME
    print('zoom = %r' % (zoom,))

    if IMPLICIT_LAYOUT:
        # HACK:
        # Use name edge to make pos (very bad)
        aids1, aids2 = get_name_rowid_edges_from_aids(ibs, aid_list)
        netx_graph_hack = make_netx_graph_from_aidpairs(ibs, aids1, aids2, unique_aids=aid_list)
        pos = netx.graphviz_layout(netx_graph_hack)
    else:
        pos = netx.graphviz_layout(netx_graph)

    #pos = netx.fruchterman_reingold_layout(netx_graph)
    #pos = netx.spring_layout(netx_graph)
    netx.draw(netx_graph, pos=pos, ax=ax)

    with_nid_edges = True
    if with_nid_edges:
        import matplotlib as mpl
        import scipy.sparse as spsparse

        aids1, aids2 = get_name_rowid_edges_from_aids(ibs, aid_list)
        edge_pts1 = np.array(ut.dict_take(pos, aids1), dtype=np.int32)
        edge_pts2 = np.array(ut.dict_take(pos, aids2), dtype=np.int32)

        if len(edge_pts1) == 0:
            edge_pts1 = edge_pts1[:, None]

        if len(edge_pts2) == 0:
            edge_pts2 = edge_pts2[:, None]

        I = np.array(aids1)
        J = np.array(aids2)
        if len(aid_list) > 0:
            N = max(aid_list) + 1
        else:
            N = 1
        forced_edge_idxs = ut.dict_take(dict(zip(zip(I, J), range(len(I)))), netx_graph.edges())
        data = vt.L2(edge_pts1, edge_pts2)
        if len(forced_edge_idxs) > 0:
            data[forced_edge_idxs] = 0.00001

        graph = spsparse.coo_matrix((data, (I, J)), shape=(N, N))

        def extract_connected_compoments(graph):
            import scipy.sparse as spsparse
            import utool as ut
            # I think this is how extraction is done?
            # only returns edge info
            # so singletons are not represented
            shape = graph.shape
            csr_graph = graph.tocsr()
            num_components, labels = spsparse.csgraph.connected_components(csr_graph)
            unique_labels = np.unique(labels)
            group_flags_list = [labels == groupid for groupid in unique_labels]
            subgraph_list = []
            for label, group_flags in zip(unique_labels, group_flags_list):
                num_members = group_flags.sum()
                ixs = list(range(num_members))
                if num_members == 0:
                    continue
                group_rowix, group_cols = csr_graph[group_flags, :].nonzero()
                if len(group_cols) == 0:
                    continue
                ix2_row = dict(zip(ixs, np.nonzero(group_flags)[0]))
                group_rows = ut.dict_take(ix2_row, group_rowix)
                component = (group_rows, group_cols.tolist())
                data = csr_graph[component].tolist()[0]
                subgraph = spsparse.coo_matrix((data, component), shape=shape)
                subgraph_list.append(subgraph)
            #assert len(compoment_list) == num_components, 'bad impl'
            return subgraph_list
        subgraph_list = extract_connected_compoments(graph)

        spantree_aids1_ = []
        spantree_aids2_ = []

        for subgraph in subgraph_list:
            subgraph_spantree = spsparse.csgraph.minimum_spanning_tree(subgraph)
            min_aids1_, min_aids2_ = subgraph_spantree.nonzero()
            spantree_aids1_.extend(min_aids1_)
            spantree_aids2_.extend(min_aids2_)

        edge_pts1_ = np.array(ut.dict_take(pos, spantree_aids1_))
        edge_pts2_ = np.array(ut.dict_take(pos, spantree_aids2_))

        segments = list(zip(edge_pts1_, edge_pts2_))
        #pt.distinct_colors
        color_list = pt.DARK_ORANGE
        #color_list = pt.BLACK
        line_group = mpl.collections.LineCollection(segments, color=color_list, alpha=.3, lw=4)
        ax.add_collection(line_group)

    if with_images:
        import cv2
        pos_list = ut.dict_take(pos, aid_list)
        img_list = ibs.get_annot_chips(aid_list)
        img_list = [vt.resize_thumb(img, (220, 220)) for img in img_list]
        img_list = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in img_list]
        artist_list = netx_draw_images_at_positions(img_list, pos_list, zoom=zoom)
        for artist, aid in zip(artist_list, aid_list):
            pt.set_plotdat(artist, 'aid', aid)
    return pos
Beispiel #49
0
def iters_until_threshold():
    """
    How many iterations of ewma until you hit the poisson / biniomal threshold

    This establishes a principled way to choose the threshold for the refresh
    criterion in my thesis. There are paramters --- moving parts --- that we
    need to work with: `a` the patience, `s` the span, and `mu` our ewma.

    `s` is a span paramter indicating how far we look back.

    `mu` is the average number of label-changing reviews in roughly the last
    `s` manual decisions.

    These numbers are used to estimate the probability that any of the next `a`
    manual decisions will be label-chanigng. When that probability falls below
    a threshold we terminate. The goal is to choose `a`, `s`, and the threshold
    `t`, such that the probability will fall below the threshold after a maximum
    of `a` consecutive non-label-chaning reviews. IE we want to tie the patience
    paramter (how far we look ahead) to how far we actually are willing to go.
    """
    import numpy as np
    import utool as ut
    import sympy as sym
    i = sym.symbols('i', integer=True, nonnegative=True, finite=True)
    # mu_i = sym.symbols('mu_i', integer=True, nonnegative=True, finite=True)
    s = sym.symbols('s', integer=True, nonnegative=True, finite=True)  # NOQA
    thresh = sym.symbols('tau', real=True, nonnegative=True,
                         finite=True)  # NOQA
    alpha = sym.symbols('alpha', real=True, nonnegative=True,
                        finite=True)  # NOQA
    c_alpha = sym.symbols('c_alpha', real=True, nonnegative=True, finite=True)
    # patience
    a = sym.symbols('a', real=True, nonnegative=True, finite=True)

    available_subs = {
        a: 20,
        s: a,
        alpha: 2 / (s + 1),
        c_alpha: (1 - alpha),
    }

    def dosubs(expr, d=available_subs):
        """ recursive expression substitution """
        expr1 = expr.subs(d)
        if expr == expr1:
            return expr1
        else:
            return dosubs(expr1, d=d)

    # mu is either the support for the poisson distribution
    # or is is the p in the binomial distribution
    # It is updated at timestep i based on ewma, assuming each incoming responce is 0
    mu_0 = 1.0
    mu_i = c_alpha**i

    # Estimate probability that any event will happen in the next `a` reviews
    # at time `i`.
    poisson_i = 1 - sym.exp(-mu_i * a)
    binom_i = 1 - (1 - mu_i)**a

    # Expand probabilities to be a function of i, s, and a
    part = ut.delete_dict_keys(available_subs.copy(), [a, s])
    mu_i = dosubs(mu_i, d=part)
    poisson_i = dosubs(poisson_i, d=part)
    binom_i = dosubs(binom_i, d=part)

    if True:
        # ewma of mu at time i if review is always not label-changing (meaningful)
        mu_1 = c_alpha * mu_0  # NOQA
        mu_2 = c_alpha * mu_1  # NOQA

    if True:
        i_vals = np.arange(0, 100)
        mu_vals = np.array(
            [dosubs(mu_i).subs({
                i: i_
            }).evalf() for i_ in i_vals])  # NOQA
        binom_vals = np.array(
            [dosubs(binom_i).subs({
                i: i_
            }).evalf() for i_ in i_vals])  # NOQA
        poisson_vals = np.array(
            [dosubs(poisson_i).subs({
                i: i_
            }).evalf() for i_ in i_vals])  # NOQA

        # Find how many iters it actually takes my expt to terminate
        thesis_draft_thresh = np.exp(-2)
        np.where(mu_vals < thesis_draft_thresh)[0]
        np.where(binom_vals < thesis_draft_thresh)[0]
        np.where(poisson_vals < thesis_draft_thresh)[0]

    sym.pprint(sym.simplify(mu_i))
    sym.pprint(sym.simplify(binom_i))
    sym.pprint(sym.simplify(poisson_i))

    # Find the thresholds that force termination after `a` reviews have passed
    # do this by setting i=a
    poisson_thresh = poisson_i.subs({i: a})
    binom_thresh = binom_i.subs({i: a})

    print('Poisson thresh')
    print(sym.latex(sym.Eq(thresh, poisson_thresh)))
    print(sym.latex(sym.Eq(thresh, sym.simplify(poisson_thresh))))

    poisson_thresh.subs({a: 115, s: 30}).evalf()

    sym.pprint(sym.Eq(thresh, poisson_thresh))
    sym.pprint(sym.Eq(thresh, sym.simplify(poisson_thresh)))

    print('Binomial thresh')
    sym.pprint(sym.simplify(binom_thresh))

    sym.pprint(sym.simplify(poisson_thresh.subs({s: a})))

    def taud(coeff):
        return coeff * 360

    if 'poisson_cache' not in vars():
        poisson_cache = {}
        binom_cache = {}

    S, A = np.meshgrid(np.arange(1, 150, 1), np.arange(0, 150, 1))

    import plottool as pt
    SA_coords = list(zip(S.ravel(), A.ravel()))
    for sval, aval in ut.ProgIter(SA_coords):
        if (sval, aval) not in poisson_cache:
            poisson_cache[(sval, aval)] = float(
                poisson_thresh.subs({
                    a: aval,
                    s: sval
                }).evalf())
    poisson_zdata = np.array([
        poisson_cache[(sval, aval)] for sval, aval in SA_coords
    ]).reshape(A.shape)
    fig = pt.figure(fnum=1, doclf=True)
    pt.gca().set_axis_off()
    pt.plot_surface3d(S,
                      A,
                      poisson_zdata,
                      xlabel='s',
                      ylabel='a',
                      rstride=3,
                      cstride=3,
                      zlabel='poisson',
                      mode='wire',
                      contour=True,
                      title='poisson3d')
    pt.gca().set_zlim(0, 1)
    pt.gca().view_init(elev=taud(1 / 16), azim=taud(5 / 8))
    fig.set_size_inches(10, 6)
    fig.savefig('a-s-t-poisson3d.png',
                dpi=300,
                bbox_inches=pt.extract_axes_extents(fig, combine=True))

    for sval, aval in ut.ProgIter(SA_coords):
        if (sval, aval) not in binom_cache:
            binom_cache[(sval, aval)] = float(
                binom_thresh.subs({
                    a: aval,
                    s: sval
                }).evalf())
    binom_zdata = np.array([
        binom_cache[(sval, aval)] for sval, aval in SA_coords
    ]).reshape(A.shape)
    fig = pt.figure(fnum=2, doclf=True)
    pt.gca().set_axis_off()
    pt.plot_surface3d(S,
                      A,
                      binom_zdata,
                      xlabel='s',
                      ylabel='a',
                      rstride=3,
                      cstride=3,
                      zlabel='binom',
                      mode='wire',
                      contour=True,
                      title='binom3d')
    pt.gca().set_zlim(0, 1)
    pt.gca().view_init(elev=taud(1 / 16), azim=taud(5 / 8))
    fig.set_size_inches(10, 6)
    fig.savefig('a-s-t-binom3d.png',
                dpi=300,
                bbox_inches=pt.extract_axes_extents(fig, combine=True))

    # Find point on the surface that achieves a reasonable threshold

    # Sympy can't solve this
    # sym.solve(sym.Eq(binom_thresh.subs({s: 50}), .05))
    # sym.solve(sym.Eq(poisson_thresh.subs({s: 50}), .05))
    # Find a numerical solution
    def solve_numeric(expr,
                      target,
                      solve_for,
                      fixed={},
                      method=None,
                      bounds=None):
        """
        Args:
            expr (Expr): symbolic expression
            target (float): numberic value
            solve_for (sympy.Symbol): The symbol you care about
            fixed (dict): fixed values of the symbol

        solve_numeric(poisson_thresh, .05, {s: 30}, method=None)
        solve_numeric(poisson_thresh, .05, {s: 30}, method='Nelder-Mead')
        solve_numeric(poisson_thresh, .05, {s: 30}, method='BFGS')
        """
        import scipy.optimize
        # Find the symbol you want to solve for
        want_symbols = expr.free_symbols - set(fixed.keys())
        # TODO: can probably extend this to multiple params
        assert len(want_symbols) == 1, 'specify all but one var'
        assert solve_for == list(want_symbols)[0]
        fixed_expr = expr.subs(fixed)

        def func(a1):
            expr_value = float(fixed_expr.subs({solve_for: a1}).evalf())
            return (expr_value - target)**2

        if not fixed:
            a1 = 0
        else:
            a1 = list(fixed.values())[0]
        # if method is None:
        #     method = 'Nelder-Mead'
        #     method = 'Newton-CG'
        #     method = 'BFGS'
        result = scipy.optimize.minimize(func,
                                         x0=a1,
                                         method=method,
                                         bounds=bounds)
        if not result.success:
            print('\n')
            print(result)
            print('\n')
        return result

    # Numeric measurments of thie line

    thresh_vals = [.001, .01, .05, .1, .135]
    svals = np.arange(1, 100)

    target_poisson_plots = {}
    for target in ut.ProgIter(thresh_vals, bs=False, freq=1):
        poisson_avals = []
        for sval in ut.ProgIter(svals, 'poisson', freq=1):
            expr = poisson_thresh
            fixed = {s: sval}
            want = a
            aval = solve_numeric(expr,
                                 target,
                                 want,
                                 fixed,
                                 method='Nelder-Mead').x[0]
            poisson_avals.append(aval)
        target_poisson_plots[target] = (svals, poisson_avals)

    fig = pt.figure(fnum=3)
    for target, dat in target_poisson_plots.items():
        pt.plt.plot(*dat, label='prob={}'.format(target))
    pt.gca().set_xlabel('s')
    pt.gca().set_ylabel('a')
    pt.legend()
    pt.gca().set_title('poisson')
    fig.set_size_inches(5, 3)
    fig.savefig('a-vs-s-poisson.png',
                dpi=300,
                bbox_inches=pt.extract_axes_extents(fig, combine=True))

    target_binom_plots = {}
    for target in ut.ProgIter(thresh_vals, bs=False, freq=1):
        binom_avals = []
        for sval in ut.ProgIter(svals, 'binom', freq=1):
            aval = solve_numeric(binom_thresh,
                                 target,
                                 a, {
                                     s: sval
                                 },
                                 method='Nelder-Mead').x[0]
            binom_avals.append(aval)
        target_binom_plots[target] = (svals, binom_avals)

    fig = pt.figure(fnum=4)
    for target, dat in target_binom_plots.items():
        pt.plt.plot(*dat, label='prob={}'.format(target))
    pt.gca().set_xlabel('s')
    pt.gca().set_ylabel('a')
    pt.legend()
    pt.gca().set_title('binom')
    fig.set_size_inches(5, 3)
    fig.savefig('a-vs-s-binom.png',
                dpi=300,
                bbox_inches=pt.extract_axes_extents(fig, combine=True))

    # ----
    if True:

        fig = pt.figure(fnum=5, doclf=True)
        s_vals = [1, 2, 3, 10, 20, 30, 40, 50]
        for sval in s_vals:
            pp = poisson_thresh.subs({s: sval})

            a_vals = np.arange(0, 200)
            pp_vals = np.array(
                [float(pp.subs({
                    a: aval
                }).evalf()) for aval in a_vals])  # NOQA

            pt.plot(a_vals, pp_vals, label='s=%r' % (sval, ))
        pt.legend()
        pt.gca().set_xlabel('a')
        pt.gca().set_ylabel('poisson prob after a reviews')
        fig.set_size_inches(5, 3)
        fig.savefig('a-vs-thresh-poisson.png',
                    dpi=300,
                    bbox_inches=pt.extract_axes_extents(fig, combine=True))

        fig = pt.figure(fnum=6, doclf=True)
        s_vals = [1, 2, 3, 10, 20, 30, 40, 50]
        for sval in s_vals:
            pp = binom_thresh.subs({s: sval})
            a_vals = np.arange(0, 200)
            pp_vals = np.array(
                [float(pp.subs({
                    a: aval
                }).evalf()) for aval in a_vals])  # NOQA
            pt.plot(a_vals, pp_vals, label='s=%r' % (sval, ))
        pt.legend()
        pt.gca().set_xlabel('a')
        pt.gca().set_ylabel('binom prob after a reviews')
        fig.set_size_inches(5, 3)
        fig.savefig('a-vs-thresh-binom.png',
                    dpi=300,
                    bbox_inches=pt.extract_axes_extents(fig, combine=True))

        # -------

        fig = pt.figure(fnum=5, doclf=True)
        a_vals = [1, 2, 3, 10, 20, 30, 40, 50]
        for aval in a_vals:
            pp = poisson_thresh.subs({a: aval})
            s_vals = np.arange(1, 200)
            pp_vals = np.array(
                [float(pp.subs({
                    s: sval
                }).evalf()) for sval in s_vals])  # NOQA
            pt.plot(s_vals, pp_vals, label='a=%r' % (aval, ))
        pt.legend()
        pt.gca().set_xlabel('s')
        pt.gca().set_ylabel('poisson prob')
        fig.set_size_inches(5, 3)
        fig.savefig('s-vs-thresh-poisson.png',
                    dpi=300,
                    bbox_inches=pt.extract_axes_extents(fig, combine=True))

        fig = pt.figure(fnum=5, doclf=True)
        a_vals = [1, 2, 3, 10, 20, 30, 40, 50]
        for aval in a_vals:
            pp = binom_thresh.subs({a: aval})
            s_vals = np.arange(1, 200)
            pp_vals = np.array(
                [float(pp.subs({
                    s: sval
                }).evalf()) for sval in s_vals])  # NOQA
            pt.plot(s_vals, pp_vals, label='a=%r' % (aval, ))
        pt.legend()
        pt.gca().set_xlabel('s')
        pt.gca().set_ylabel('binom prob')
        fig.set_size_inches(5, 3)
        fig.savefig('s-vs-thresh-binom.png',
                    dpi=300,
                    bbox_inches=pt.extract_axes_extents(fig, combine=True))

    #---------------------
    # Plot out a table

    mu_i.subs({s: 75, a: 75}).evalf()
    poisson_thresh.subs({s: 75, a: 75}).evalf()

    sval = 50
    for target, dat in target_poisson_plots.items():
        slope = np.median(np.diff(dat[1]))
        aval = int(np.ceil(sval * slope))
        thresh = float(poisson_thresh.subs({s: sval, a: aval}).evalf())
        print('aval={}, sval={}, thresh={}, target={}'.format(
            aval, sval, thresh, target))

    for target, dat in target_binom_plots.items():
        slope = np.median(np.diff(dat[1]))
        aval = int(np.ceil(sval * slope))
        pass
Beispiel #50
0
def show_model(model, evidence={}, soft_evidence={}, **kwargs):
    """
    References:
        http://stackoverflow.com/questions/22207802/pygraphviz-networkx-set-node-level-or-layer

    Ignore:
        pkg-config --libs-only-L libcgraph
        sudo apt-get  install libgraphviz-dev -y
        sudo apt-get  install libgraphviz4 -y

        # sudo apt-get install pkg-config
        sudo apt-get install libgraphviz-dev
        # pip install git+git://github.com/pygraphviz/pygraphviz.git
        pip install pygraphviz
        python -c "import pygraphviz; print(pygraphviz.__file__)"

        sudo pip3 install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"
        python3 -c "import pygraphviz; print(pygraphviz.__file__)"
    """
    if ut.get_argval('--hackmarkov') or ut.get_argval('--hackjunc'):
        draw_tree_model(model, **kwargs)
        return

    import plottool as pt
    import networkx as netx
    import matplotlib as mpl
    fnum = pt.ensure_fnum(None)
    fig = pt.figure(fnum=fnum, pnum=(3, 1, (slice(0, 2), 0)), doclf=True)  # NOQA
    #fig = pt.figure(fnum=fnum, pnum=(3, 2, (1, slice(1, 2))), doclf=True)  # NOQA
    ax = pt.gca()
    var2_post = {f.variables[0]: f for f in kwargs.get('factor_list', [])}

    netx_graph = (model)
    #netx_graph.graph.setdefault('graph', {})['size'] = '"10,5"'
    #netx_graph.graph.setdefault('graph', {})['rankdir'] = 'LR'

    pos = get_hacked_pos(netx_graph)
    #netx.pygraphviz_layout(netx_graph)
    #pos = netx.pydot_layout(netx_graph, prog='dot')
    #pos = netx.graphviz_layout(netx_graph)

    drawkw = dict(pos=pos, ax=ax, with_labels=True, node_size=1500)
    if evidence is not None:
        node_colors = [
            # (pt.TRUE_BLUE
            (pt.WHITE
             if node not in soft_evidence else
             pt.LIGHT_PINK)
            if node not in evidence
            else pt.FALSE_RED
            for node in netx_graph.nodes()]

        for node in netx_graph.nodes():
            cpd = model.var2_cpd[node]
            if cpd.ttype == 'score':
                pass
        drawkw['node_color'] = node_colors

    netx.draw(netx_graph, **drawkw)

    show_probs = True
    if show_probs:
        textprops = {
            'family': 'monospace',
            'horizontalalignment': 'left',
            #'horizontalalignment': 'center',
            #'size': 12,
            'size': 8,
        }

        textkw = dict(
            xycoords='data', boxcoords='offset points', pad=0.25,
            frameon=True, arrowprops=dict(arrowstyle='->'),
            #bboxprops=dict(fc=node_attr['fillcolor']),
        )

        netx_nodes = model.nodes(data=True)
        node_key_list = ut.get_list_column(netx_nodes, 0)
        pos_list = ut.dict_take(pos, node_key_list)

        artist_list = []
        offset_box_list = []
        for pos_, node in zip(pos_list, netx_nodes):
            x, y = pos_
            variable = node[0]

            cpd = model.var2_cpd[variable]

            prior_marg = (cpd if cpd.evidence is None else
                          cpd.marginalize(cpd.evidence, inplace=False))

            prior_text = None

            text = None
            if variable in evidence:
                text = cpd.variable_statenames[evidence[variable]]
            elif variable in var2_post:
                post_marg = var2_post[variable]
                text = pgm_ext.make_factor_text(post_marg, 'post')
                prior_text = pgm_ext.make_factor_text(prior_marg, 'prior')
            else:
                if len(evidence) == 0 and len(soft_evidence) == 0:
                    prior_text = pgm_ext.make_factor_text(prior_marg, 'prior')

            show_post = kwargs.get('show_post', False)
            show_prior = kwargs.get('show_prior', False)
            show_prior = True
            show_post = True

            show_ev = (evidence is not None and variable in evidence)
            if (show_post or show_ev) and text is not None:
                offset_box = mpl.offsetbox.TextArea(text, textprops)
                artist = mpl.offsetbox.AnnotationBbox(
                    # offset_box, (x + 5, y), xybox=(20., 5.),
                    offset_box, (x, y + 5), xybox=(4., 20.),
                    #box_alignment=(0, 0),
                    box_alignment=(.5, 0),
                    **textkw)
                offset_box_list.append(offset_box)
                artist_list.append(artist)

            if show_prior and prior_text is not None:
                offset_box2 = mpl.offsetbox.TextArea(prior_text, textprops)
                artist2 = mpl.offsetbox.AnnotationBbox(
                    # offset_box2, (x - 5, y), xybox=(-20., -15.),
                    # offset_box2, (x, y - 5), xybox=(-15., -20.),
                    offset_box2, (x, y - 5), xybox=(-4, -20.),
                    #box_alignment=(1, 1),
                    box_alignment=(.5, 1),
                    **textkw)
                offset_box_list.append(offset_box2)
                artist_list.append(artist2)

        for artist in artist_list:
            ax.add_artist(artist)

        xmin, ymin = np.array(pos_list).min(axis=0)
        xmax, ymax = np.array(pos_list).max(axis=0)
        num_annots = len(model.ttype2_cpds['name'])
        if num_annots > 4:
            ax.set_xlim((xmin - 40, xmax + 40))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(30, 7)
        else:
            ax.set_xlim((xmin - 42, xmax + 42))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(23, 7)
        fig = pt.gcf()

        title = 'num_names=%r, num_annots=%r' % (model.num_names, num_annots,)
        map_assign = kwargs.get('map_assign', None)
        #max_marginal_list = []
        #for name, marginal in marginalized_joints.items():
        #    states = list(ut.iprod(*marginal.statenames))
        #    vals = marginal.values.ravel()
        #    x = vals.argmax()
        #    max_marginal_list += ['P(' + ', '.join(states[x]) + ') = ' + str(vals[x])]
        # title += str(marginal)
        top_assignments = kwargs.get('top_assignments', None)
        if top_assignments is not None:
            map_assign, map_prob = top_assignments[0]
            if map_assign is not None:
                # title += '\nMAP=' + ut.repr2(map_assign, strvals=True)
                title += '\nMAP: ' + map_assign + ' @' + '%.2f%%' % (100 * map_prob,)
        if kwargs.get('show_title', True):
            pt.set_figtitle(title, size=14)
        #pt.set_xlabel()

        def hack_fix_centeralign():
            if textprops['horizontalalignment'] == 'center':
                print('Fixing centeralign')
                fig = pt.gcf()
                fig.canvas.draw()

                # Superhack for centered text. Fix bug in
                # /usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py
                # /usr/local/lib/python2.7/dist-packages/matplotlib/text.py
                for offset_box in offset_box_list:
                    offset_box.set_offset
                    z = offset_box._text.get_window_extent()
                    (z.x1 - z.x0) / 2
                    offset_box._text
                    T = offset_box._text.get_transform()
                    A = mpl.transforms.Affine2D()
                    A.clear()
                    A.translate((z.x1 - z.x0) / 2, 0)
                    offset_box._text.set_transform(T + A)
        hack_fix_centeralign()
    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        bin_labels = ut.get_list_column(top_assignments, 0)
        bin_vals =  ut.get_list_column(top_assignments, 1)

        # bin_labels = ['\n'.join(ut.textwrap.wrap(_lbl, width=30)) for _lbl in bin_labels]

        pt.draw_histogram(bin_labels, bin_vals, fnum=fnum, pnum=(3, 8, (2, slice(4, None))),
                          transpose=True,
                          use_darkbackground=False,
                          #xtick_rotation=-10,
                          ylabel='Prob', xlabel='assignment')
        pt.set_title('Assignment probabilities')
Beispiel #51
0
def ewma():
    import plottool as pt
    import ubelt as ub
    import numpy as np
    pt.qtensure()

    # Investigate the span parameter
    span = 20
    alpha = 2 / (span + 1)

    # how long does it take for the estimation to hit 0?
    # (ie, it no longer cares about the initial 1?)
    # about 93 iterations to get to 1e-4
    # about 47 iterations to get to 1e-2
    # about 24 iterations to get to 1e-1
    # 20 iterations goes to .135
    data = ([1] + [0] * 20 + [1] * 40 + [0] * 20 + [1] * 50 + [0] * 20 +
            [1] * 60 + [0] * 20 + [1] * 165 + [0] * 20 + [0])
    mave = []

    iter_ = iter(data)
    current = next(iter_)
    mave += [current]
    for x in iter_:
        current = (alpha * x) + (1 - alpha) * current
        mave += [current]

    if False:
        pt.figure(fnum=1, doclf=True)
        pt.plot(data)
        pt.plot(mave)

    np.where(np.array(mave) < 1e-1)

    import sympy as sym

    # span, alpha, n = sym.symbols('span, alpha, n')
    n = sym.symbols('n', integer=True, nonnegative=True, finite=True)
    span = sym.symbols('span', integer=True, nonnegative=True, finite=True)
    thresh = sym.symbols('thresh', real=True, nonnegative=True, finite=True)
    # alpha = 2 / (span + 1)

    a, b, c = sym.symbols('a, b, c', real=True, nonnegative=True, finite=True)
    sym.solve(sym.Eq(b**a, c), a)

    current = 1
    x = 0
    steps = []
    for _ in range(10):
        current = (alpha * x) + (1 - alpha) * current
        steps.append(current)

    alpha = sym.symbols('alpha', real=True, nonnegative=True, finite=True)
    base = sym.symbols('base', real=True, finite=True)
    alpha = 2 / (span + 1)
    thresh_expr = (1 - alpha)**n
    thresthresh_exprh_expr = base**n
    n_expr = sym.ceiling(sym.log(thresh) / sym.log(1 - 2 / (span + 1)))

    sym.pprint(sym.simplify(thresh_expr))
    sym.pprint(sym.simplify(n_expr))
    print(sym.latex(sym.simplify(n_expr)))

    # def calc_n2(span, thresh):
    #     return np.log(thresh) / np.log(1 - 2 / (span + 1))

    def calc_n(span, thresh):
        return np.log(thresh) / np.log((span - 1) / (span + 1))

    def calc_thresh_val(n, span):
        alpha = 2 / (span + 1)
        return (1 - alpha)**n

    span = np.arange(2, 200)
    n_frac = calc_n(span, thresh=.5)
    n = np.ceil(n_frac)
    calc_thresh_val(n, span)

    pt.figure(fnum=1, doclf=True)
    ydatas = ut.odict([('thresh=%f' % thresh,
                        np.ceil(calc_n(span, thresh=thresh)))
                       for thresh in [1e-3, .01, .1, .2, .3, .4, .5]])
    pt.multi_plot(
        span,
        ydatas,
        xlabel='span',
        ylabel='n iters to acheive thresh',
        marker='',
        # num_xticks=len(span),
        fnum=1)
    pt.gca().set_aspect('equal')

    def both_sides(eqn, func):
        return sym.Eq(func(eqn.lhs), func(eqn.rhs))

    eqn = sym.Eq(thresh_expr, thresh)
    n_expr = sym.solve(eqn,
                       n)[0].subs(base,
                                  (1 - alpha)).subs(alpha, (2 / (span + 1)))

    eqn = both_sides(eqn, lambda x: sym.log(x, (1 - alpha)))
    lhs = eqn.lhs

    from sympy.solvers.inequalities import solve_univariate_inequality

    def eval_expr(span_value, n_value):
        return np.array(
            [thresh_expr.subs(span, span_value).subs(n, n_) for n_ in n_value],
            dtype=np.float)

    eval_expr(20, np.arange(20))

    def linear(x, a, b):
        return a * x + b

    def sigmoidal_4pl(x, a, b, c, d):
        return d + (a - d) / (1 + (x / c)**b)

    def exponential(x, a, b, c):
        return a + b * np.exp(-c * x)

    import scipy.optimize

    # Determine how to choose span, such that you get to .01 from 1
    # in n timesteps
    thresh_to_span_to_n = []
    thresh_to_n_to_span = []
    for thresh_value in ub.ProgIter([.0001, .001, .01, .1, .2, .3, .4, .5]):
        print('')
        test_vals = sorted([2, 3, 4, 5, 6])
        n_to_span = []
        for n_value in ub.ProgIter(test_vals):
            # In n iterations I want to choose a span that the expression go
            # less than a threshold
            constraint = thresh_expr.subs(n, n_value) < thresh_value
            solution = solve_univariate_inequality(constraint, span)
            try:
                lowbound = np.ceil(float(solution.args[0].lhs))
                highbound = np.floor(float(solution.args[1].rhs))
                assert lowbound <= highbound
                span_value = lowbound
            except AttributeError:
                span_value = np.floor(float(solution.rhs))
            n_to_span.append((n_value, span_value))

        # Given a threshold, find a minimum number of steps
        # that brings you up to that threshold given a span
        test_vals = sorted(set(list(range(2, 1000, 50)) + [2, 3, 4, 5, 6]))
        span_to_n = []
        for span_value in ub.ProgIter(test_vals):
            constraint = thresh_expr.subs(span, span_value) < thresh_value
            solution = solve_univariate_inequality(constraint, n)
            n_value = solution.lhs
            span_to_n.append((span_value, n_value))

        thresh_to_n_to_span.append((thresh_value, n_to_span))
        thresh_to_span_to_n.append((thresh_value, span_to_n))

    thresh_to_params = []
    for thresh_value, span_to_n in thresh_to_span_to_n:
        xdata, ydata = [np.array(_, dtype=np.float) for _ in zip(*span_to_n)]

        p0 = (1 / np.diff((ydata - ydata[0])[1:]).mean(), ydata[0])
        func = linear
        popt, pcov = scipy.optimize.curve_fit(func, xdata, ydata, p0)
        # popt, pcov = scipy.optimize.curve_fit(exponential, xdata, ydata)

        if False:
            yhat = func(xdata, *popt)
            pt.figure(fnum=1, doclf=True)
            pt.plot(xdata, ydata, label='measured')
            pt.plot(xdata, yhat, label='predicteed')
            pt.legend()
        # slope = np.diff(ydata).mean()
        # pt.plot(d)
        thresh_to_params.append((thresh_value, popt))

    # pt.plt.plot(*zip(*thresh_to_slope), 'x-')

    # for thresh_value=.01, we get a rough line with slop ~2.302,
    # for thresh_value=.5, we get a line with slop ~34.66

    # if we want to get to 0 in n timesteps, with a thresh_value of
    # choose span=f(thresh_value) * (n + 2))
    # f is some inverse exponential

    # 0.0001, 460.551314197147
    # 0.001, 345.413485647860,
    # 0.01, 230.275657098573,
    # 0.1, 115.137828549287,
    # 0.2, 80.4778885203347,
    # 0.3, 60.2031233261536,
    # 0.4, 45.8179484913827,
    # 0.5, 34.6599400289520

    # Seems to be 4PL symetrical sigmoid
    # f(x) = -66500.85 + (66515.88 - -66500.85) / (1 + (x/0.8604672)^0.001503716)
    # f(x) = -66500.85 + (66515.88 - -66500.85)/(1 + (x/0.8604672)^0.001503716)

    def f(x):
        return -66500.85 + (66515.88 -
                            -66500.85) / (1 + (x / 0.8604672)**0.001503716)
        # return (10000 * (-6.65 + (13.3015) / (1 + (x/0.86) ** 0.00150)))

    # f(.5) * (n - 1)

    # f(
    solve_rational_inequalities(thresh_expr < .01, n)
Beispiel #52
0
def draw_bayesian_model(model, evidence={}, soft_evidence={}, fnum=None,
                        pnum=None, **kwargs):

    from pgmpy.models import BayesianModel
    if not isinstance(model, BayesianModel):
        model = model.to_bayesian_model()

    import plottool as pt
    import networkx as netx
    factor_list = kwargs.get('factor_list', [])

    ttype_colors, ttype_scalars = make_colorcodes(model)

    textprops = {
        'horizontalalignment': 'left', 'family': 'monospace', 'size': 8, }

    # build graph attrs
    tup = get_node_viz_attrs(
        model, evidence, soft_evidence, factor_list, ttype_colors, **kwargs)
    node_color, pos_list, pos_dict, takws = tup

    # draw graph
    has_infered = evidence or 'factor_list' in kwargs

    fig = pt.figure(fnum=fnum, pnum=pnum, doclf=True)  # NOQA
    ax = pt.gca()
    drawkw = dict(pos=pos_dict, ax=ax, with_labels=True, node_size=1100,
                  node_color=node_color)
    netx.draw(model, **drawkw)
    hacks = [pt.draw_text_annotations(textprops=textprops, **takw)
             for takw in takws if takw]

    xmin, ymin = np.array(pos_list).min(axis=0)
    xmax, ymax = np.array(pos_list).max(axis=0)
    if 'name' in model.ttype2_template:
        num_names = len(model.ttype2_template['name'].basis)
        num_annots = len(model.ttype2_cpds['name'])
        if num_annots > 4:
            ax.set_xlim((xmin - 40, xmax + 40))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(30, 7)
        else:
            ax.set_xlim((xmin - 42, xmax + 42))
            ax.set_ylim((ymin - 50, ymax + 50))
            fig.set_size_inches(23, 7)
        title = 'num_names=%r, num_annots=%r' % (num_names, num_annots,)
    else:
        title = ''
    map_assign = kwargs.get('map_assign', None)

    def word_insert(text):
        return '' if len(text) == 0 else text + ' '

    top_assignments = kwargs.get('top_assignments', None)
    if top_assignments is not None:
        map_assign, map_prob = top_assignments[0]
        if map_assign is not None:
            title += '\n%sMAP: ' % (word_insert(kwargs.get('method', '')))
            title += map_assign + ' @' + '%.2f%%' % (100 * map_prob,)
    if kwargs.get('show_title', True):
        pt.set_figtitle(title, size=14)

    for hack in hacks:
        hack()

    if has_infered:
        # Hack in colorbars
        # if ut.list_type(basis) is int:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=np.array(basis) + 1)
        # else:
        #     pt.colorbar(scalars, colors, lbl='score', ticklabels=basis)
        keys = ['name', 'score']
        locs = ['left', 'right']
        for key, loc in zip(keys, locs):
            if key in ttype_colors:
                basis = model.ttype2_template[key].basis
                # scalars =
                colors = ttype_colors[key]
                scalars = ttype_scalars[key]
                pt.colorbar(scalars, colors, lbl=key, ticklabels=basis,
                            ticklocation=loc)
Beispiel #53
0
def annotate_matches2(
        ibs,
        aid1,
        aid2,
        fm,
        fs,
        offset1=(0, 0),
        offset2=(0, 0),
        xywh2=None,  # (0, 0, 0, 0),
        xywh1=None,  # (0, 0, 0, 0),
        qreq_=None,
        **kwargs):
    """
    TODO: use this as the main function.
    """
    if True:
        aid_list = [aid1, aid2]
        bbox_list = [xywh1, xywh2]
        offset_list = [offset1, offset2]
        name_fm_list = [fm]
        name_fs_list = [fs]
        return annotate_matches3(ibs,
                                 aid_list,
                                 bbox_list,
                                 offset_list,
                                 name_fm_list,
                                 name_fs_list,
                                 qreq_=qreq_,
                                 **kwargs)
    else:
        # TODO: make sure all of this functionality is incorporated into annotate_matches3
        in_image = kwargs.get('in_image', False)
        show_query = kwargs.get('show_query', True)
        draw_border = kwargs.get('draw_border', True)
        draw_lbl = kwargs.get('draw_lbl', True)
        notitle = kwargs.get('notitle', False)

        truth = ibs.get_match_truth(aid1, aid2)
        truth_color = vh.get_truth_color(truth)
        # Build title
        title = vh.get_query_text(ibs, None, aid2, truth, qaid=aid1, **kwargs)
        # Build xlbl
        ax = pt.gca()
        ph.set_plotdat(ax, 'viztype', 'matches')
        ph.set_plotdat(ax, 'qaid', aid1)
        ph.set_plotdat(ax, 'aid1', aid1)
        ph.set_plotdat(ax, 'aid2', aid2)
        if draw_lbl:
            name1, name2 = ibs.get_annot_names([aid1, aid2])
            nid1, nid2 = ibs.get_annot_name_rowids([aid1, aid2],
                                                   distinguish_unknowns=False)
            #lbl1 = repr(name1)  + ' : ' + 'q' + vh.get_aidstrs(aid1)
            #lbl2 = repr(name2)  + ' : ' +  vh.get_aidstrs(aid2)
            lbl1_list = []
            lbl2_list = []
            if kwargs.get('show_aid', True):
                lbl1_list.append('q' + vh.get_aidstrs(aid1))
                lbl2_list.append(vh.get_aidstrs(aid2))
            if kwargs.get('show_name', True):
                lbl1_list.append(repr((name1)))
                lbl2_list.append(repr((name2)))
            if kwargs.get('show_nid', True):
                lbl1_list.append(vh.get_nidstrs(nid1))
                lbl2_list.append(vh.get_nidstrs(nid2))
            lbl1 = ' : '.join(lbl1_list)
            lbl2 = ' : '.join(lbl2_list)
        else:
            lbl1, lbl2 = None, None
        if vh.NO_LBL_OVERRIDE:
            title = ''
        if not notitle:
            pt.set_title(title, ax)
        # Plot annotations over images
        if in_image:
            bbox1, bbox2 = vh.get_bboxes(ibs, [aid1, aid2], [offset1, offset2])
            theta1, theta2 = ibs.get_annot_thetas([aid1, aid2])
            # HACK!
            if show_query:
                pt.draw_bbox(bbox1,
                             bbox_color=pt.ORANGE,
                             lbl=lbl1,
                             theta=theta1)
            bbox_color2 = truth_color if draw_border else pt.ORANGE
            pt.draw_bbox(bbox2, bbox_color=bbox_color2, lbl=lbl2, theta=theta2)
        else:
            xy, w, h = pt.get_axis_xy_width_height(ax)
            bbox2 = (xy[0], xy[1], w, h)
            theta2 = 0

            if xywh2 is None:
                #xywh2 = (xy[0], xy[1], w, h)
                # weird when sidebyside is off y seems to be inverted
                xywh2 = (0, 0, w, h)

            if not show_query and xywh1 is None:
                data_config2 = (None if qreq_ is None else
                                qreq_.get_external_data_config2())
                # FIXME, pass data in
                kpts2 = ibs.get_annot_kpts([aid2], config2_=data_config2)[0]
                #pt.draw_kpts2(kpts2.take(fm.T[1], axis=0))
                # Draw any selected matches
                #sm_kw = dict(rect=True, colors=pt.BLUE)
                pt.plot_fmatch(None, xywh2, None, kpts2, fm, fs=fs, **kwargs)
            if draw_border:
                pt.draw_border(ax, truth_color, 4, offset=offset2)
            if draw_lbl:
                # Custom user lbl for chips 1 and 2
                if show_query:
                    (x1, y1, w1, h1) = xywh1
                    pt.absolute_lbl(x1 + w1, y1, lbl1)
                (x2, y2, w2, h2) = xywh2
                pt.absolute_lbl(x2 + w2, y2, lbl2)
        if True:
            # No matches draw a red box
            if fm is None or len(fm) == 0:
                if draw_border:
                    pass
Beispiel #54
0
def viz_netx_chipgraph(ibs,
                       graph,
                       fnum=None,
                       use_image=False,
                       layout=None,
                       zoom=None,
                       prog='neato',
                       as_directed=False,
                       augment_graph=True,
                       layoutkw=None,
                       framewidth=3.0,
                       **kwargs):
    r"""
    DEPRICATE or improve

    Args:
        ibs (IBEISController):  ibeis controller object
        graph (nx.DiGraph):
        fnum (int):  figure number(default = None)
        use_image (bool): (default = False)
        zoom (float): (default = 0.4)

    Returns:
        ?: pos

    CommandLine:
        python -m ibeis --tf viz_netx_chipgraph --show

    Cand:
        ibeis review_tagged_joins --save figures4/mergecase.png --figsize=15,15
            --clipwhite --diskshow
        ibeis compute_occurrence_groups --save figures4/occurgraph.png
            --figsize=40,40 --clipwhite --diskshow
        ~/code/ibeis/ibeis/algo/preproc/preproc_occurrence.py

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_graph import *  # NOQA
        >>> import ibeis
        >>> ibs = ibeis.opendb(defaultdb='PZ_MTEST')
        >>> nid_list = ibs.get_valid_nids()[0:10]
        >>> fnum = None
        >>> use_image = True
        >>> zoom = 0.4
        >>> make_name_graph_interaction(ibs, nid_list, prog='neato')
        >>> ut.show_if_requested()
    """
    import plottool as pt
    print('[viz_graph] drawing chip graph')
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum, pnum=(1, 1, 1))
    ax = pt.gca()

    if layout is None:
        layout = 'agraph'
    print('layout = %r' % (layout, ))

    if use_image:
        ensure_node_images(ibs, graph)
    nx.set_node_attributes(graph, 'shape', 'rect')

    if layoutkw is None:
        layoutkw = {}
    layoutkw['prog'] = layoutkw.get('prog', prog)
    layoutkw.update(kwargs)

    if prog == 'neato':
        graph = graph.to_undirected()

    plotinfo = pt.show_nx(
        graph,
        ax=ax,
        # img_dict=img_dict,
        layout=layout,
        # hacknonode=bool(use_image),
        layoutkw=layoutkw,
        as_directed=as_directed,
        framewidth=framewidth,
    )
    return plotinfo
Beispiel #55
0
def draw_markov_model(model, fnum=None, **kwargs):
    import plottool as pt
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum=fnum, doclf=True)
    ax = pt.gca()
    from pgmpy.models import MarkovModel
    if isinstance(model, MarkovModel):
        markovmodel = model
    else:
        markovmodel = model.to_markov_model()
    # pos = netx.pydot_layout(markovmodel)
    pos = netx.pygraphviz_layout(markovmodel)
    # Referenecs:
    # https://groups.google.com/forum/#!topic/networkx-discuss/FwYk0ixLDuY

    # pos = netx.spring_layout(markovmodel)
    # pos = netx.circular_layout(markovmodel)
    # curved-arrow
    # markovmodel.edge_attr['curved-arrow'] = True
    # markovmodel.graph.setdefault('edge', {})['splines'] = 'curved'
    # markovmodel.graph.setdefault('graph', {})['splines'] = 'curved'
    # markovmodel.graph.setdefault('edge', {})['splines'] = 'curved'

    node_color = [pt.NEUTRAL] * len(pos)
    drawkw = dict(pos=pos, ax=ax, with_labels=True, node_color=node_color,  # NOQA
                  node_size=1100)

    from matplotlib.patches import FancyArrowPatch, Circle
    import numpy as np

    def draw_network(G, pos, ax, sg=None):
        for n in G:
            c = Circle(pos[n], radius=10, alpha=0.5, color=pt.NEUTRAL_BLUE)
            ax.add_patch(c)
            G.node[n]['patch'] = c
            x, y = pos[n]
            pt.ax_absolute_text(x, y, n, ha='center', va='center')
        seen = {}
        for (u, v, d) in G.edges(data=True):
            n1 = G.node[u]['patch']
            n2 = G.node[v]['patch']
            rad = 0.1
            if (u, v) in seen:
                rad = seen.get((u, v))
                rad = (rad + np.sign(rad) * 0.1) * -1
            alpha = 0.5
            color = 'k'

            e = FancyArrowPatch(n1.center, n2.center, patchA=n1, patchB=n2,
                                # arrowstyle='-|>',
                                arrowstyle='-',
                                connectionstyle='arc3,rad=%s' % rad,
                                mutation_scale=10.0,
                                lw=2,
                                alpha=alpha,
                                color=color)
            seen[(u, v)] = rad
            ax.add_patch(e)
        return e
    # netx.draw(markovmodel, **drawkw)
    draw_network(markovmodel, pos, ax)
    ax.autoscale()
    pt.plt.axis('equal')
    pt.plt.axis('off')

    if kwargs.get('show_title', True):
        pt.set_figtitle('Markov Model')
Beispiel #56
0
def annotate_matches3(ibs,
                      aid_list,
                      bbox_list,
                      offset_list,
                      name_fm_list,
                      name_fs_list,
                      qreq_=None,
                      **kwargs):
    """
    TODO: use this as the main function.
    """
    # TODO Use this function when you clean show_matches
    in_image = kwargs.get('in_image', False)
    #show_query  = kwargs.get('show_query', True)
    draw_border = kwargs.get('draw_border', True)
    draw_lbl = kwargs.get('draw_lbl', True)
    notitle = kwargs.get('notitle', False)
    # List of annotation scores for each annot in the name

    #printDBG('[viz] annotate_matches3()')
    #truth = ibs.get_match_truth(aid1, aid2)

    #name_equality = (
    #    np.array(ibs.get_annot_nids(aid_list[1:])) == ibs.get_annot_nids(aid_list[0])
    #).tolist()
    #truth = 1 if all(name_equality) else (2 if any(name_equality) else 0)
    #truth_color = vh.get_truth_color(truth)
    ## Build title

    #score         = kwargs.pop('score', None)
    #rawscore      = kwargs.pop('rawscore', None)
    #aid2_raw_rank = kwargs.pop('aid2_raw_rank', None)
    #print(kwargs)
    #title = vh.get_query_text(ibs, None, aid2, truth, qaid=aid1, **kwargs)
    # Build xlbl
    ax = pt.gca()
    ph.set_plotdat(ax, 'viztype', 'multi_match')
    ph.set_plotdat(ax, 'qaid', aid_list[0])
    ph.set_plotdat(ax, 'num_matches', len(aid_list) - 1)
    ph.set_plotdat(ax, 'aid_list', aid_list[1:])
    for count, aid in enumerate(aid_list, start=1):
        ph.set_plotdat(ax, 'aid%d' % (count, ), aid)

    #name_equality = (ibs.get_annot_nids(aid_list[0]) ==
    #                 np.array(ibs.get_annot_nids(aid_list[1:])))
    #truth = 1 if np.all(name_equality) else (2 if np.any(name_equality) else 0)
    truth = get_multitruth(ibs, aid_list)
    if any(ibs.is_aid_unknown(aid_list[1:])) or ibs.is_aid_unknown(
            aid_list[0]):
        truth = ibs.const.TRUTH_UNKNOWN
    truth_color = vh.get_truth_color(truth)

    name_annot_scores = kwargs.get('name_annot_scores', None)
    if len(aid_list) == 2:
        # HACK; generalize to multple annots
        title = vh.get_query_text(ibs,
                                  None,
                                  aid_list[1],
                                  truth,
                                  qaid=aid_list[0],
                                  **kwargs)
        if not notitle:
            pt.set_title(title, ax)

    if draw_lbl:
        # Build labels
        nid_list = ibs.get_annot_nids(aid_list, distinguish_unknowns=False)
        name_list = ibs.get_annot_names(aid_list)
        lbls_list = [[] for _ in range(len(aid_list))]
        if kwargs.get('show_name', False):
            for count, (lbls, name) in enumerate(zip(lbls_list, name_list)):
                lbls.append(ut.repr2((name)))
        if kwargs.get('show_nid', True):
            for count, (lbls, nid) in enumerate(zip(lbls_list, nid_list)):
                # only label the first two images with nids
                LABEL_ALL_NIDS = False
                if count <= 1 or LABEL_ALL_NIDS:
                    #lbls.append(vh.get_nidstrs(nid))
                    lbls.append(('q' if count == 0 else '') +
                                vh.get_nidstrs(nid))
        if kwargs.get('show_aid', True):
            for count, (lbls, aid) in enumerate(zip(lbls_list, aid_list)):
                lbls.append(('q' if count == 0 else '') + vh.get_aidstrs(aid))
        if (kwargs.get('show_annot_score', True)
                and name_annot_scores is not None):
            max_digits = kwargs.get('score_precision', None)
            for (lbls, score) in zip(lbls_list[1:], name_annot_scores):
                lbls.append(ut.num_fmt(score, max_digits=max_digits))
        lbl_list = [' : '.join(lbls) for lbls in lbls_list]
    else:
        lbl_list = [None] * len(aid_list)
    #pt.set_title(title, ax)
    # Plot annotations over images
    if in_image:
        in_image_bbox_list = vh.get_bboxes(ibs, aid_list, offset_list)
        in_image_theta_list = ibs.get_annot_thetas(aid_list)
        # HACK!
        #if show_query:
        #    pt.draw_bbox(bbox1, bbox_color=pt.ORANGE, lbl=lbl1, theta=theta1)
        bbox_color = pt.ORANGE
        bbox_color = truth_color if draw_border else pt.ORANGE
        for bbox, theta, lbl in zip(in_image_bbox_list, in_image_theta_list,
                                    lbl_list):
            pt.draw_bbox(bbox, bbox_color=bbox_color, lbl=lbl, theta=theta)
            pass
    else:
        xy, w, h = pt.get_axis_xy_width_height(ax)
        #theta2 = 0

        #if xywh2 is None:
        #    #xywh2 = (xy[0], xy[1], w, h)
        #    # weird when sidebyside is off y seems to be inverted
        #    xywh2 = (0,  0, w, h)

        #if not show_query and xywh1 is None:
        #    data_config2 = None if qreq_ is None else
        #    qreq_.get_external_data_config2()
        #    kpts2 = ibs.get_annot_kpts([aid2], config2_=data_config2)[0]
        #    #pt.draw_kpts2(kpts2.take(fm.T[1], axis=0))
        #    # Draw any selected matches
        #    #sm_kw = dict(rect=True, colors=pt.BLUE)
        #    pt.plot_fmatch(None, xywh2, None, kpts2, fm, fs=fs, **kwargs)
        #if draw_border:
        #    pt.draw_border(ax, truth_color, 4, offset=offset2)
        if draw_border:
            pt.draw_border(ax, color=truth_color, lw=4)
        if draw_lbl:
            # Custom user lbl for chips 1 and 2
            #if show_query:
            #    (x1, y1, w1, h1) = xywh1
            #    pt.absolute_lbl(x1 + w1, y1, lbl1)
            for bbox, lbl in zip(bbox_list, lbl_list):
                (x, y, w, h) = bbox
                pt.absolute_lbl(x + w, y, lbl)
    # No matches draw a red box
    if True:
        no_matches = name_fm_list is None or all(
            [True if fm is None else len(fm) == 0 for fm in name_fm_list])
        if no_matches:
            xy, w, h = pt.get_axis_xy_width_height(ax)
            #axes_bbox = (xy[0], xy[1], w, h)
            if draw_border:
                pass
Beispiel #57
0
def annotate_matches2(ibs, aid1, aid2, fm, fs,
                      offset1=(0, 0),
                      offset2=(0, 0),
                      xywh2=None,  # (0, 0, 0, 0),
                      xywh1=None,  # (0, 0, 0, 0),
                      qreq_=None,
                      **kwargs):
    """
    TODO: use this as the main function.
    """
    if True:
        aid_list = [aid1, aid2]
        bbox_list = [xywh1, xywh2]
        offset_list = [offset1, offset2]
        name_fm_list = [fm]
        name_fs_list = [fs]
        return annotate_matches3(ibs, aid_list, bbox_list, offset_list, name_fm_list, name_fs_list, qreq_=qreq_, **kwargs)
    else:
        # TODO: make sure all of this functionality is incorporated into annotate_matches3
        in_image    = kwargs.get('in_image', False)
        show_query  = kwargs.get('show_query', True)
        draw_border = kwargs.get('draw_border', True)
        draw_lbl    = kwargs.get('draw_lbl', True)
        notitle     = kwargs.get('notitle', False)

        truth = ibs.get_match_truth(aid1, aid2)
        truth_color = vh.get_truth_color(truth)
        # Build title
        title = vh.get_query_text(ibs, None, aid2, truth, qaid=aid1, **kwargs)
        # Build xlbl
        ax = pt.gca()
        ph.set_plotdat(ax, 'viztype', 'matches')
        ph.set_plotdat(ax, 'qaid', aid1)
        ph.set_plotdat(ax, 'aid1', aid1)
        ph.set_plotdat(ax, 'aid2', aid2)
        if draw_lbl:
            name1, name2 = ibs.get_annot_names([aid1, aid2])
            nid1, nid2 = ibs.get_annot_name_rowids([aid1, aid2],
                                                   distinguish_unknowns=False)
            #lbl1 = repr(name1)  + ' : ' + 'q' + vh.get_aidstrs(aid1)
            #lbl2 = repr(name2)  + ' : ' +  vh.get_aidstrs(aid2)
            lbl1_list = []
            lbl2_list = []
            if kwargs.get('show_aid', True):
                lbl1_list.append('q' + vh.get_aidstrs(aid1))
                lbl2_list.append(vh.get_aidstrs(aid2))
            if kwargs.get('show_name', True):
                lbl1_list.append(repr((name1)))
                lbl2_list.append(repr((name2)))
            if kwargs.get('show_nid', True):
                lbl1_list.append(vh.get_nidstrs(nid1))
                lbl2_list.append(vh.get_nidstrs(nid2))
            lbl1 = ' : '.join(lbl1_list)
            lbl2 = ' : '.join(lbl2_list)
        else:
            lbl1, lbl2 = None, None
        if vh.NO_LBL_OVERRIDE:
            title = ''
        if not notitle:
            pt.set_title(title, ax)
        # Plot annotations over images
        if in_image:
            bbox1, bbox2 = vh.get_bboxes(ibs, [aid1, aid2], [offset1, offset2])
            theta1, theta2 = ibs.get_annot_thetas([aid1, aid2])
            # HACK!
            if show_query:
                pt.draw_bbox(bbox1, bbox_color=pt.ORANGE, lbl=lbl1, theta=theta1)
            bbox_color2 = truth_color if draw_border else pt.ORANGE
            pt.draw_bbox(bbox2, bbox_color=bbox_color2, lbl=lbl2, theta=theta2)
        else:
            xy, w, h = pt.get_axis_xy_width_height(ax)
            bbox2 = (xy[0], xy[1], w, h)
            theta2 = 0

            if xywh2 is None:
                #xywh2 = (xy[0], xy[1], w, h)
                # weird when sidebyside is off y seems to be inverted
                xywh2 = (0,  0, w, h)

            if not show_query and xywh1 is None:
                data_config2 = (None if qreq_ is None else
                                qreq_.get_external_data_config2())
                # FIXME, pass data in
                kpts2 = ibs.get_annot_kpts([aid2], config2_=data_config2)[0]
                #pt.draw_kpts2(kpts2.take(fm.T[1], axis=0))
                # Draw any selected matches
                #sm_kw = dict(rect=True, colors=pt.BLUE)
                pt.plot_fmatch(None, xywh2, None, kpts2, fm, fs=fs, **kwargs)
            if draw_border:
                pt.draw_border(ax, truth_color, 4, offset=offset2)
            if draw_lbl:
                # Custom user lbl for chips 1 and 2
                if show_query:
                    (x1, y1, w1, h1) = xywh1
                    pt.absolute_lbl(x1 + w1, y1, lbl1)
                (x2, y2, w2, h2) = xywh2
                pt.absolute_lbl(x2 + w2, y2, lbl2)
        if True:
            # No matches draw a red box
            if fm is None or len(fm) == 0:
                if draw_border:
                    pass
Beispiel #58
0
def show_name_matches(ibs,
                      qaid,
                      name_daid_list,
                      name_fm_list,
                      name_fs_list,
                      name_H1_list,
                      name_featflag_list,
                      qreq_=None,
                      **kwargs):
    """
    Called from chip_match.py

    Args:
        ibs (IBEISController):  ibeis controller object
        qaid (int):  query annotation id
        name_daid_list (list):
        name_fm_list (list):
        name_fs_list (list):
        name_H1_list (list):
        name_featflag_list (list):
        qreq_ (QueryRequest):  query request object with hyper-parameters(default = None)

    Kwargs:
        draw_fmatches, name_rank, fnum, pnum, colorbar_, nonvote_mode,
        fastmode, show_matches, fs, fm_norm, lbl1, lbl2, rect, draw_border,
        cmap, H1, H2, scale_factor1, scale_factor2, draw_pts, draw_ell,
        draw_lines, show_nMatches, all_kpts, in_image, show_query, draw_lbl,
        name_annot_scores, score, rawscore, aid2_raw_rank, show_name,
        show_nid, show_aid, show_annot_score, show_truth, name_score,
        show_name_score, show_name_rank, show_timedelta

    CommandLine:
        python -m ibeis.viz.viz_matches --exec-show_name_matches
        python -m ibeis.viz.viz_matches --test-show_name_matches --show

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_matches import *  # NOQA
        >>> from ibeis.algo.hots import chip_match
        >>> from ibeis.algo.hots import name_scoring
        >>> import vtool as vt
        >>> from ibeis.algo.hots import _pipeline_helpers as plh  # NOQA
        >>> import numpy as np
        >>> func = chip_match.ChipMatch.show_single_namematch
        >>> sourcecode = ut.get_func_sourcecode(func, stripdef=True, stripret=True,
        >>>                                     strip_docstr=True)
        >>> setup = ut.regex_replace('viz_matches.show_name_matches', '#', sourcecode)
        >>> homog = False
        >>> print(ut.indent(setup, '>>> '))
        >>> ibs, qreq_, cm_list = plh.testdata_post_sver('PZ_MTEST', qaid_list=[1])
        >>> cm = cm_list[0]
        >>> cm.score_nsum(qreq_)
        >>> dnid = ibs.get_annot_nids(cm.qaid)
        >>> # +--- COPIED SECTION
        >>> locals_ = locals()
        >>> var_list = ut.exec_func_src(
        >>>     func, locals_=locals_,
        >>>     sentinal='name_annot_scores = cm.annot_score_list.take(sorted_groupxs')
        >>> exec(ut.execstr_dict(var_list))
        >>> # L___ COPIED SECTION
        >>> kwargs = {}
        >>> show_name_matches(ibs, qaid, name_daid_list, name_fm_list,
        >>>                   name_fs_list, name_h1_list, name_featflag_list,
        >>>                   qreq_=qreq_, **kwargs)
        >>> ut.quit_if_noshow()
        >>> ut.show_if_requested()
    """
    #print("SHOW NAME MATCHES")
    #print(ut.repr2(kwargs, nl=True))
    #from ibeis import constants as const
    from ibeis import tag_funcs
    draw_fmatches = kwargs.pop('draw_fmatches', True)
    rchip1, kpts1 = get_query_annot_pair_info(ibs, qaid, qreq_, draw_fmatches)
    rchip2_list, kpts2_list = get_data_annot_pair_info(ibs, name_daid_list,
                                                       qreq_, draw_fmatches)
    fm_list = name_fm_list
    fs_list = name_fs_list
    featflag_list = name_featflag_list
    offset_list, sf_list, bbox_list = show_multichip_match(
        rchip1, rchip2_list, kpts1, kpts2_list, fm_list, fs_list,
        featflag_list, **kwargs)
    aid_list = [qaid] + name_daid_list
    annotate_matches3(ibs,
                      aid_list,
                      bbox_list,
                      offset_list,
                      name_fm_list,
                      name_fs_list,
                      qreq_=None,
                      **kwargs)
    ax = pt.gca()
    title = vh.get_query_text(ibs,
                              None,
                              name_daid_list,
                              False,
                              qaid=qaid,
                              **kwargs)

    pt.set_title(title, ax)

    # Case tags
    annotmatch_rowid_list = ibs.get_annotmatch_rowid_from_superkey(
        [qaid] * len(name_daid_list), name_daid_list)
    annotmatch_rowid_list = ut.filter_Nones(annotmatch_rowid_list)
    tags_list = ibs.get_annotmatch_case_tags(annotmatch_rowid_list)
    if not ut.get_argflag('--show'):  # False:
        tags_list = tag_funcs.consolodate_annotmatch_tags(tags_list)
    tag_list = ut.unique_ordered(ut.flatten(tags_list))

    name_rank = kwargs.get('name_rank', None)
    truth = get_multitruth(ibs, aid_list)

    xlabel = {1: 'Correct ID', 0: 'Incorrect ID', 2: 'Unknown ID'}[truth]

    if False:
        if name_rank is None:
            xlabel = {1: 'Genuine', 0: 'Imposter', 2: 'Unknown'}[truth]
            #xlabel = {1: 'True', 0: 'False', 2: 'Unknown'}[truth]
        else:
            if name_rank == 0:
                xlabel = {
                    1: 'True Positive',
                    0: 'False Positive',
                    2: 'Unknown'
                }[truth]
            else:
                xlabel = {
                    1: 'False Negative',
                    0: 'True Negative',
                    2: 'Unknown'
                }[truth]

    if len(tag_list) > 0:
        xlabel += '\n' + ', '.join(tag_list)

    pt.set_xlabel(xlabel)
    return ax
    def plot_chip(self, aid, nRows, nCols, px, **kwargs):
        """ Plots an individual chip in a subaxis """
        ibs = self.ibs
        enable_chip_title_prefix = ut.is_developer()
        #enable_chip_title_prefix = False
        if aid in self.comp_aids:
            score    = self.cm.get_annot_scores([aid])[0]
            rawscore = self.cm.get_annot_scores([aid])[0]
            title_suf = kwargs.get('title_suffix', '')
            if score != rawscore:
                if score is None:
                    title_suf += '\n score=____'
                else:
                    title_suf += '\n score=%0.2f' % score
            title_suf += '\n rawscore=%0.2f' % rawscore
        else:
            title_suf = kwargs.get('title_suffix', '')
            if enable_chip_title_prefix:
                title_suf = '\n' + title_suf

        #nid = ibs.get_annot_name_rowids(aid)
        viz_chip_kw = {
            'fnum': self.fnum,
            'pnum': (nRows, nCols, px),
            'nokpts': True,
            'show_gname': False,
            'show_exemplar': False,
            'show_num_gt': False,
            'show_gname': False,
            'title_suffix': title_suf,
            # 'text_color': kwargs.get('color'),
            ###
            #'show_name': False,
            #'show_aidstr': False,
            'enable_chip_title_prefix': enable_chip_title_prefix,
            'show_name': True,
            'show_aidstr': True,
            'show_yawtext': True,
            'show_quality_text': True,
        }

        viz_chip.show_chip(ibs, aid, **viz_chip_kw)
        ax = pt.gca()
        if kwargs.get('make_buttons', True):
            divider = pt.ensure_divider(ax)
            butkw = {
                'divider': divider,
                'size': '13%'
            }

        self.aid2_ax = {}
        self.aid2_border = {}

        if aid in self.comp_aids:
            callback = partial(self.select, aid)
            self.append_button('Select This Animal', callback=callback, **butkw)
            #Hack to toggle colors
            if aid in self.aid_checkbox_states:
                #If we are selecting it, then make it green, otherwise change it back to grey
                if self.aid_checkbox_states[aid]:
                    border = pt.draw_border(ax, color=(0, 1, 0), lw=4)
                else:
                    border = pt.draw_border(ax, color=(.7, .7, .7), lw=4)
                self.aid2_border[aid] = border
            else:
                self.aid_checkbox_states[aid] = False
            self.append_button('Examine', callback=partial(self.examine, aid), **butkw)