def show_internals(self, fnum=None): import plottool as pt pt.qtensure() pnum_ = pt.make_pnum_nextgen(nRows=1, nCols=len(self.forests)) for level, forest in enumerate(self.forests): pt.show_nx(forest.to_networkx(), title='level=%r' % (level,), fnum=fnum, pnum=pnum_())
def check_relationships(branches): ancestors = {b: set() for b in branches} length = len(branches) * (len(branches) - 1) for b1, b2 in ub.ProgIter(it.combinations(branches, 2), length=length): ret = ub.cmd('git merge-base --is-ancestor {} {}'.format(b1, b2))['ret'] if ret == 0: ancestors[b1].add(b2) ret = ub.cmd('git merge-base --is-ancestor {} {}'.format(b2, b1))['ret'] if ret == 0: ancestors[b2].add(b1) print('<key> is an ancestor of <value>') print(ub.repr2(ancestors)) descendants = {b: set() for b in branches} for key, others in ancestors.items(): for o in others: descendants[o].add(key) print('<key> descends from <value>') print(ub.repr2(descendants)) import plottool as pt import networkx as nx G = nx.DiGraph() G.add_nodes_from(branches) for key, others in ancestors.items(): for o in others: # G.add_edge(key, o) G.add_edge(o, key) from networkx.algorithms.connectivity.edge_augmentation import collapse flag = True G2 = G while flag: flag = False for u, v in list(G2.edges()): if G2.has_edge(v, u): G2 = collapse(G2, [[u, v]]) node_relabel = ub.ddict(list) for old, new in G2.graph['mapping'].items(): node_relabel[new].append(old) G2 = nx.relabel_nodes(G2, {k: '\n'.join(v) for k, v in node_relabel.items()}) flag = True break G3 = nx.transitive_reduction(G2) pt.show_nx(G3, arrow_width=1.5, prog='dot', layoutkw=dict(prog='dot')) pt.zoom_factory() pt.pan_factory() pt.plt.show()
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()
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')
def viz_factor_graph(gm): """ ut.qtensure() gm = build_factor_graph(G, nodes, edges , n_annots, n_names, lookup_annot_idx, use_unaries=True, edge_probs=None, operator='multiplier') """ ut.qtensure() import networkx from networkx.drawing.nx_agraph import graphviz_layout networkx.graphviz_layout = graphviz_layout opengm.visualizeGm(gm, show=False, layout="neato", plotUnaries=True, iterations=1000, plotFunctions=True, plotNonShared=False, relNodeSize=1.0) _ = pt.show_nx(gm.G) # NOQA
def double_depcache_graph(): r""" CommandLine: python -m ibeis.scripts.specialdraw double_depcache_graph --show --testmode python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite --arrow-width=.5 python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite --arrow-width=5 Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = double_depcache_graph() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import networkx as nx import plottool as pt pt.ensure_pylab_qt4() # pt.plt.xkcd() ibs = ibeis.opendb('testdb1') reduced = True implicit = True annot_graph = ibs.depc_annot.make_graph(reduced=reduced, implicit=implicit) image_graph = ibs.depc_image.make_graph(reduced=reduced, implicit=implicit) to_rename = ut.isect(image_graph.nodes(), annot_graph.nodes()) nx.relabel_nodes(annot_graph, {x: 'annot_' + x for x in to_rename}, copy=False) nx.relabel_nodes(image_graph, {x: 'image_' + x for x in to_rename}, copy=False) graph = nx.compose_all([image_graph, annot_graph]) #graph = nx.union_all([image_graph, annot_graph], rename=('image', 'annot')) # userdecision = ut.nx_makenode(graph, 'user decision', shape='rect', color=pt.DARK_YELLOW, style='diagonals') # userdecision = ut.nx_makenode(graph, 'user decision', shape='circle', color=pt.DARK_YELLOW) userdecision = ut.nx_makenode( graph, 'User decision', shape='rect', #width=100, height=100, color=pt.YELLOW, style='diagonals') #longcat = True longcat = False #edge = ('feat', 'neighbor_index') #data = graph.get_edge_data(*edge)[0] #print('data = %r' % (data,)) #graph.remove_edge(*edge) ## hack #graph.add_edge('featweight', 'neighbor_index', **data) graph.add_edge('detections', userdecision, constraint=longcat, color=pt.PINK) graph.add_edge(userdecision, 'annotations', constraint=longcat, color=pt.PINK) # graph.add_edge(userdecision, 'annotations', implicit=True, color=[0, 0, 0]) if not longcat: pass #graph.add_edge('images', 'annotations', style='invis') #graph.add_edge('thumbnails', 'annotations', style='invis') #graph.add_edge('thumbnails', userdecision, style='invis') graph.remove_node('Has_Notch') graph.remove_node('annotmask') layoutkw = { 'ranksep': 5, 'nodesep': 5, 'dpi': 96, # 'nodesep': 1, } ns = 1000 ut.nx_set_default_node_attributes(graph, 'fontsize', 72) ut.nx_set_default_node_attributes(graph, 'fontname', 'Ubuntu') ut.nx_set_default_node_attributes(graph, 'style', 'filled') ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns * (1 / ut.PHI)) #for u, v, d in graph.edge(data=True): for u, vkd in graph.edge.items(): for v, dk in vkd.items(): for k, d in dk.items(): localid = d.get('local_input_id') if localid: # d['headlabel'] = localid if localid not in ['1']: d['taillabel'] = localid #d['label'] = localid if d.get('taillabel') in {'1'}: del d['taillabel'] node_alias = { 'chips': 'Chip', 'images': 'Image', 'feat': 'Feat', 'featweight': 'Feat Weights', 'thumbnails': 'Thumbnail', 'detections': 'Detections', 'annotations': 'Annotation', 'Notch_Tips': 'Notch Tips', 'probchip': 'Prob Chip', 'Cropped_Chips': 'Croped Chip', 'Trailing_Edge': 'Trailing\nEdge', 'Block_Curvature': 'Block\nCurvature', # 'BC_DTW': 'block curvature /\n dynamic time warp', 'BC_DTW': 'DTW Distance', 'vsone': 'Hots vsone', 'feat_neighbs': 'Nearest\nNeighbors', 'neighbor_index': 'Neighbor\nIndex', 'vsmany': 'Hots vsmany', 'annot_labeler': 'Annot Labeler', 'labeler': 'Labeler', 'localizations': 'Localizations', 'classifier': 'Classifier', 'sver': 'Spatial\nVerification', 'Classifier': 'Existence', 'image_labeler': 'Image Labeler', } node_alias = { 'Classifier': 'existence', 'feat_neighbs': 'neighbors', 'sver': 'spatial_verification', 'Cropped_Chips': 'cropped_chip', 'BC_DTW': 'dtw_distance', 'Block_Curvature': 'curvature', 'Trailing_Edge': 'trailing_edge', 'Notch_Tips': 'notch_tips', 'thumbnails': 'thumbnail', 'images': 'image', 'annotations': 'annotation', 'chips': 'chip', #userdecision: 'User de' } node_alias = ut.delete_dict_keys( node_alias, ut.setdiff(node_alias.keys(), graph.nodes())) nx.relabel_nodes(graph, node_alias, copy=False) fontkw = dict(fontname='Ubuntu', fontweight='normal', fontsize=12) #pt.gca().set_aspect('equal') #pt.figure() pt.show_nx(graph, layoutkw=layoutkw, fontkw=fontkw) pt.zoom_factory()
def graphcut_flow(): r""" Returns: ?: name CommandLine: python -m ibeis.scripts.specialdraw graphcut_flow --show --save cutflow.png --diskshow --clipwhite python -m ibeis.scripts.specialdraw graphcut_flow --save figures4/cutiden.png --diskshow --clipwhite --dpath ~/latex/crall-candidacy-2015/ --figsize=24,10 --arrow-width=2.0 Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> graphcut_flow() >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import plottool as pt pt.ensure_pylab_qt4() import networkx as nx # pt.plt.xkcd() graph = nx.DiGraph() def makecluster(name, num, **attrkw): return [ ut.nx_makenode(graph, name + str(n), **attrkw) for n in range(num) ] def add_edge2(u, v, *args, **kwargs): v = ut.ensure_iterable(v) u = ut.ensure_iterable(u) for _u, _v in ut.product(u, v): graph.add_edge(_u, _v, *args, **kwargs) ns = 512 # *** Primary color: p_shade2 = '#41629A' # *** Secondary color s1_shade2 = '#E88B53' # *** Secondary color s2_shade2 = '#36977F' # *** Complement color c_shade2 = '#E8B353' annot1 = ut.nx_makenode(graph, 'Unlabeled\nannotations\n(query)', width=ns, height=ns, groupid='annot', color=p_shade2) annot2 = ut.nx_makenode(graph, 'Labeled\nannotations\n(database)', width=ns, height=ns, groupid='annot', color=s1_shade2) occurprob = ut.nx_makenode(graph, 'Dense \nprobabilities', color=lighten_hex(p_shade2, .1)) cacheprob = ut.nx_makenode(graph, 'Cached \nprobabilities', color=lighten_hex(s1_shade2, .1)) sparseprob = ut.nx_makenode(graph, 'Sparse\nprobabilities', color=lighten_hex(c_shade2, .1)) graph.add_edge(annot1, occurprob) graph.add_edge(annot1, sparseprob) graph.add_edge(annot2, sparseprob) graph.add_edge(annot2, cacheprob) matchgraph = ut.nx_makenode(graph, 'Graph of\npotential matches', color=lighten_hex(s2_shade2, .1)) cutalgo = ut.nx_makenode(graph, 'Graph cut algorithm', color=lighten_hex(s2_shade2, .2), shape='ellipse') cc_names = ut.nx_makenode( graph, 'Identifications,\n splits, and merges are\nconnected compoments', color=lighten_hex(s2_shade2, .3)) graph.add_edge(occurprob, matchgraph) graph.add_edge(sparseprob, matchgraph) graph.add_edge(cacheprob, matchgraph) graph.add_edge(matchgraph, cutalgo) graph.add_edge(cutalgo, cc_names) ut.nx_set_default_node_attributes(graph, 'shape', 'rect') ut.nx_set_default_node_attributes(graph, 'style', 'filled,rounded') ut.nx_set_default_node_attributes(graph, 'fixedsize', 'true') ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns * (1 / ut.PHI)) ut.nx_set_default_node_attributes(graph, 'regular', False) layoutkw = { 'prog': 'dot', 'rankdir': 'LR', 'splines': 'line', 'sep': 100 / 72, 'nodesep': 300 / 72, 'ranksep': 300 / 72, } fontkw = dict(fontname='Ubuntu', fontweight='light', fontsize=14) pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, **fontkw) pt.zoom_factory()
def general_identify_flow(): r""" CommandLine: python -m ibeis.scripts.specialdraw general_identify_flow --show --save pairsim.png --dpi=100 --diskshow --clipwhite python -m ibeis.scripts.specialdraw general_identify_flow --dpi=200 --diskshow --clipwhite --dpath ~/latex/cand/ --figsize=20,10 --save figures4/pairprob.png --arrow-width=2.0 Example: >>> # SCRIPT >>> from ibeis.scripts.specialdraw import * # NOQA >>> general_identify_flow() >>> ut.quit_if_noshow() >>> ut.show_if_requested() """ import networkx as nx import plottool as pt pt.ensure_pylab_qt4() # pt.plt.xkcd() graph = nx.DiGraph() def makecluster(name, num, **attrkw): return [ut.nx_makenode(name + str(n), **attrkw) for n in range(num)] def add_edge2(u, v, *args, **kwargs): v = ut.ensure_iterable(v) u = ut.ensure_iterable(u) for _u, _v in ut.product(u, v): graph.add_edge(_u, _v, *args, **kwargs) # *** Primary color: p_shade2 = '#41629A' # *** Secondary color s1_shade2 = '#E88B53' # *** Secondary color s2_shade2 = '#36977F' # *** Complement color c_shade2 = '#E8B353' ns = 512 ut.inject_func_as_method(graph, ut.nx_makenode) annot1_color = p_shade2 annot2_color = s1_shade2 #annot1_color2 = pt.color_funcs.lighten_rgb(colors.hex2color(annot1_color), .01) annot1 = graph.nx_makenode('Annotation X', width=ns, height=ns, groupid='annot', color=annot1_color) annot2 = graph.nx_makenode('Annotation Y', width=ns, height=ns, groupid='annot', color=annot2_color) featX = graph.nx_makenode('Features X', size=(ns / 1.2, ns / 2), groupid='feats', color=lighten_hex(annot1_color, .1)) featY = graph.nx_makenode('Features Y', size=(ns / 1.2, ns / 2), groupid='feats', color=lighten_hex(annot2_color, .1)) #'#4771B3') global_pairvec = graph.nx_makenode( 'Global similarity\n(viewpoint, quality, ...)', width=ns * ut.PHI * 1.2, color=s2_shade2) findnn = graph.nx_makenode('Find correspondences\n(nearest neighbors)', shape='ellipse', color=c_shade2) local_pairvec = graph.nx_makenode( 'Local similarities\n(LNBNN, spatial error, ...)', size=(ns * 2.2, ns), color=lighten_hex(c_shade2, .1)) agglocal = graph.nx_makenode('Aggregate', size=(ns / 1.1, ns / 2), shape='ellipse', color=lighten_hex(c_shade2, .2)) catvecs = graph.nx_makenode('Concatenate', size=(ns / 1.1, ns / 2), shape='ellipse', color=lighten_hex(s2_shade2, .1)) pairvec = graph.nx_makenode('Vector of\npairwise similarities', color=lighten_hex(s2_shade2, .2)) classifier = graph.nx_makenode('Classifier\n(SVM/RF/DNN)', color=lighten_hex(s2_shade2, .3)) prob = graph.nx_makenode( 'Matching Probability\n(same individual given\nsimilar viewpoint)', color=lighten_hex(s2_shade2, .4)) graph.add_edge(annot1, global_pairvec) graph.add_edge(annot2, global_pairvec) add_edge2(annot1, featX) add_edge2(annot2, featY) add_edge2(featX, findnn) add_edge2(featY, findnn) add_edge2(findnn, local_pairvec) graph.add_edge(local_pairvec, agglocal, constraint=True) graph.add_edge(agglocal, catvecs, constraint=False) graph.add_edge(global_pairvec, catvecs) graph.add_edge(catvecs, pairvec) # graph.add_edge(annot1, classifier, style='invis') # graph.add_edge(pairvec, classifier , constraint=False) graph.add_edge(pairvec, classifier) graph.add_edge(classifier, prob) ut.nx_set_default_node_attributes(graph, 'shape', 'rect') #ut.nx_set_default_node_attributes(graph, 'fillcolor', nx.get_node_attributes(graph, 'color')) #ut.nx_set_default_node_attributes(graph, 'style', 'rounded') ut.nx_set_default_node_attributes(graph, 'style', 'filled,rounded') ut.nx_set_default_node_attributes(graph, 'fixedsize', 'true') ut.nx_set_default_node_attributes(graph, 'xlabel', nx.get_node_attributes(graph, 'label')) ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns) ut.nx_set_default_node_attributes(graph, 'regular', False) #font = 'MonoDyslexic' #font = 'Mono_Dyslexic' font = 'Ubuntu' ut.nx_set_default_node_attributes(graph, 'fontsize', 72) ut.nx_set_default_node_attributes(graph, 'fontname', font) #ut.nx_delete_node_attr(graph, 'width') #ut.nx_delete_node_attr(graph, 'height') #ut.nx_delete_node_attr(graph, 'fixedsize') #ut.nx_delete_node_attr(graph, 'style') #ut.nx_delete_node_attr(graph, 'regular') #ut.nx_delete_node_attr(graph, 'shape') #graph.node[annot1]['label'] = "<f0> left|<f1> mid\ dle|<f2> right" #graph.node[annot2]['label'] = ut.codeblock( # ''' # <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> # <TR><TD>left</TD><TD PORT="f1">mid dle</TD><TD PORT="f2">right</TD></TR> # </TABLE>> # ''') #graph.node[annot1]['label'] = ut.codeblock( # ''' # <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> # <TR><TD>left</TD><TD PORT="f1">mid dle</TD><TD PORT="f2">right</TD></TR> # </TABLE>> # ''') #graph.node[annot1]['shape'] = 'none' #graph.node[annot1]['margin'] = '0' layoutkw = { 'forcelabels': True, 'prog': 'dot', 'rankdir': 'LR', # 'splines': 'curved', 'splines': 'line', 'samplepoints': 20, 'showboxes': 1, # 'splines': 'polyline', #'splines': 'spline', 'sep': 100 / 72, 'nodesep': 300 / 72, 'ranksep': 300 / 72, #'inputscale': 72, # 'inputscale': 1, # 'dpi': 72, # 'concentrate': 'true', # merges edge lines # 'splines': 'ortho', # 'aspect': 1, # 'ratio': 'compress', # 'size': '5,4000', # 'rank': 'max', } #fontkw = dict(fontfamilty='sans-serif', fontweight='normal', fontsize=12) #fontkw = dict(fontname='Ubuntu', fontweight='normal', fontsize=12) #fontkw = dict(fontname='Ubuntu', fontweight='light', fontsize=20) fontkw = dict(fontname=font, fontweight='light', fontsize=12) #prop = fm.FontProperties(fname='/usr/share/fonts/truetype/groovygh.ttf') pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, **fontkw) pt.zoom_factory()
def do_fine_graine_level_sets(self, mapping): inverted = ub.invert_dict(mapping, False) for sup, subs in inverted.items(): print('sup = {!r}'.format(sup)) for sub in subs: if sub in self.name_to_cat: cat = self.name_to_cat[sub] n = len(self.cid_to_aids[cat['id']]) if n: print(' * {} = {}'.format(sub, n)) mapping = get_coarse_mapping() inverted = ub.invert_dict(mapping, False) fine_grained_map = {} custom_fine_grained_map = {v: k for k, vs in { 'unidentified roundfish': [ 'unidentified roundfish', 'unidentified roundfish (less than half)', 'unknown roundfish' 'Rockfish Unid.' ], 'unidentified sebastomus': [ 'sebastes_2species', 'unknown sebastomus', 'unknown rockfish', 'Thornyhead Unid.', 'Hexagrammidae sp.', ], 'prickleback': [ 'Prickleback', 'Stichaeidae', ], 'Flatfish Unid.': [ 'Flatfish Unid.', 'unknown flatfish', ] }.items() for v in vs} catnames = [cat['name'] for cat in self.cats.values()] catnames = list(mapping.keys()) for name in catnames: # normalize the name norm = normalize_name(name) fine_grained_map[name] = norm fine_grained_level_set = ub.invert_dict(fine_grained_map, False) print(ub.repr2(fine_grained_level_set)) for sup, subs in inverted.items(): print('* COARSE-CLASS = {!r}'.format(sup)) for norm in sorted(set([normalize_name(sub) for sub in subs])): raws = fine_grained_level_set.get(norm, []) if raws: print(' * fine-class = {!r}'.format(norm)) if len(raws) > 1: # or list(raws)[0] != norm: print(ub.indent('* raw-classes = {}'.format(ub.repr2(raws, nl=1)), ' ' * 8)) import networkx as nx G = nx.DiGraph() for norm in fine_grained_map.values(): G.add_node(norm) for sup, subs in inverted.items(): G.add_node(sup) for norm in sorted(set([normalize_name(sub) for sub in subs])): G.add_edge(norm, sup) if False: import plottool as pt pt.show_nx(G, layoutkw=dict(prog='neato'), arrow_width=.1, sep=10)
def intraoccurrence_connected(): r""" CommandLine: python -m ibeis.scripts.specialdraw intraoccurrence_connected --show python -m ibeis.scripts.specialdraw intraoccurrence_connected --show --postcut python -m ibeis.scripts.specialdraw intraoccurrence_connected --show --smaller Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = intraoccurrence_connected() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import plottool as pt from ibeis.viz import viz_graph import networkx as nx pt.ensure_pylab_qt4() ibs = ibeis.opendb(defaultdb='PZ_Master1') nid2_aid = { #4880: [3690, 3696, 3703, 3706, 3712, 3721], 4880: [3690, 3696, 3703], 6537: [3739], 6653: [7671], 6610: [7566, 7408], #6612: [7664, 7462, 7522], #6624: [7465, 7360], #6625: [7746, 7383, 7390, 7477, 7376, 7579], 6630: [7586, 7377, 7464, 7478], #6677: [7500] } nid2_dbaids = { 4880: [33, 6120, 7164], 6537: [7017, 7206], 6653: [7660] } if ut.get_argflag('--small') or ut.get_argflag('--smaller'): del nid2_aid[6630] del nid2_aid[6537] del nid2_dbaids[6537] if ut.get_argflag('--smaller'): nid2_dbaids[4880].remove(33) nid2_aid[4880].remove(3690) nid2_aid[6610].remove(7408) #del nid2_aid[4880] #del nid2_dbaids[4880] aids = ut.flatten(nid2_aid.values()) temp_nids = [1] * len(aids) postcut = ut.get_argflag('--postcut') aids_list = ibs.group_annots_by_name(aids)[0] ensure_edges = 'all' if True or not postcut else None unlabeled_graph = viz_graph.make_netx_graph_from_aid_groups( ibs, aids_list, #invis_edges=invis_edges, ensure_edges=ensure_edges, temp_nids=temp_nids) viz_graph.color_by_nids(unlabeled_graph, unique_nids=[1] * len(list(unlabeled_graph.nodes()))) viz_graph.ensure_node_images(ibs, unlabeled_graph) nx.set_node_attributes(unlabeled_graph, 'shape', 'rect') #unlabeled_graph = unlabeled_graph.to_undirected() # Find the "database exemplars for these annots" if False: gt_aids = ibs.get_annot_groundtruth(aids) gt_aids = [ut.setdiff(s, aids) for s in gt_aids] dbaids = ut.unique(ut.flatten(gt_aids)) dbaids = ibs.filter_annots_general(dbaids, minqual='good') ibs.get_annot_quality_texts(dbaids) else: dbaids = ut.flatten(nid2_dbaids.values()) exemplars = nx.DiGraph() #graph = exemplars # NOQA exemplars.add_nodes_from(dbaids) def add_clique(graph, nodes, edgeattrs={}, nodeattrs={}): edge_list = ut.upper_diag_self_prodx(nodes) graph.add_edges_from(edge_list, **edgeattrs) return edge_list for aids_, nid in zip(*ibs.group_annots_by_name(dbaids)): add_clique(exemplars, aids_) viz_graph.ensure_node_images(ibs, exemplars) viz_graph.color_by_nids(exemplars, ibs=ibs) nx.set_node_attributes(unlabeled_graph, 'framewidth', False) nx.set_node_attributes(exemplars, 'framewidth', 4.0) nx.set_node_attributes(unlabeled_graph, 'group', 'unlab') nx.set_node_attributes(exemplars, 'group', 'exemp') #big_graph = nx.compose_all([unlabeled_graph]) big_graph = nx.compose_all([exemplars, unlabeled_graph]) # add sparse connections from unlabeled to exemplars import numpy as np rng = np.random.RandomState(0) if True or not postcut: for aid_ in unlabeled_graph.nodes(): flags = rng.rand(len(exemplars)) > .5 nid_ = ibs.get_annot_nids(aid_) exnids = np.array(ibs.get_annot_nids(list(exemplars.nodes()))) flags = np.logical_or(exnids == nid_, flags) exmatches = ut.compress(list(exemplars.nodes()), flags) big_graph.add_edges_from(list(ut.product([aid_], exmatches)), color=pt.ORANGE, implicit=True) else: for aid_ in unlabeled_graph.nodes(): flags = rng.rand(len(exemplars)) > .5 exmatches = ut.compress(list(exemplars.nodes()), flags) nid_ = ibs.get_annot_nids(aid_) exnids = np.array(ibs.get_annot_nids(exmatches)) exmatches = ut.compress(exmatches, exnids == nid_) big_graph.add_edges_from(list(ut.product([aid_], exmatches))) pass nx.set_node_attributes(big_graph, 'shape', 'rect') #if False and postcut: # ut.nx_delete_node_attr(big_graph, 'nid') # ut.nx_delete_edge_attr(big_graph, 'color') # viz_graph.ensure_graph_nid_labels(big_graph, ibs=ibs) # viz_graph.color_by_nids(big_graph, ibs=ibs) # big_graph = big_graph.to_undirected() layoutkw = { 'sep' : 1 / 5, 'prog': 'neato', 'overlap': 'false', #'splines': 'ortho', 'splines': 'spline', } as_directed = False #as_directed = True #hacknode = True hacknode = 0 graph = big_graph ut.nx_ensure_agraph_color(graph) if hacknode: nx.set_edge_attributes(graph, 'taillabel', {e: str(e[0]) for e in graph.edges()}) nx.set_edge_attributes(graph, 'headlabel', {e: str(e[1]) for e in graph.edges()}) explicit_graph = pt.get_explicit_graph(graph) _, layout_info = pt.nx_agraph_layout(explicit_graph, orig_graph=graph, inplace=True, **layoutkw) if ut.get_argflag('--smaller'): graph.node[7660]['pos'] = np.array([550, 350]) graph.node[6120]['pos'] = np.array([200, 600]) + np.array([350, -400]) graph.node[7164]['pos'] = np.array([200, 480]) + np.array([350, -400]) nx.set_node_attributes(graph, 'pin', 'true') _, layout_info = pt.nx_agraph_layout(graph, inplace=True, **layoutkw) elif ut.get_argflag('--small'): graph.node[7660]['pos'] = np.array([750, 350]) graph.node[33]['pos'] = np.array([300, 600]) + np.array([350, -400]) graph.node[6120]['pos'] = np.array([500, 600]) + np.array([350, -400]) graph.node[7164]['pos'] = np.array([410, 480]) + np.array([350, -400]) nx.set_node_attributes(graph, 'pin', 'true') _, layout_info = pt.nx_agraph_layout(graph, inplace=True, **layoutkw) if not postcut: #pt.show_nx(graph.to_undirected(), layout='agraph', layoutkw=layoutkw, # as_directed=False) #pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, # as_directed=as_directed, hacknode=hacknode) pt.show_nx(graph, layout='custom', layoutkw=layoutkw, as_directed=as_directed, hacknode=hacknode) else: #explicit_graph = pt.get_explicit_graph(graph) #_, layout_info = pt.nx_agraph_layout(explicit_graph, orig_graph=graph, # **layoutkw) #layout_info['edge']['alpha'] = .8 #pt.apply_graph_layout_attrs(graph, layout_info) #graph_layout_attrs = layout_info['graph'] ##edge_layout_attrs = layout_info['edge'] ##node_layout_attrs = layout_info['node'] #for key, vals in layout_info['node'].items(): # #print('[special] key = %r' % (key,)) # nx.set_node_attributes(graph, key, vals) #for key, vals in layout_info['edge'].items(): # #print('[special] key = %r' % (key,)) # nx.set_edge_attributes(graph, key, vals) #nx.set_edge_attributes(graph, 'alpha', .8) #graph.graph['splines'] = graph_layout_attrs.get('splines', 'line') #graph.graph['splines'] = 'polyline' # graph_layout_attrs.get('splines', 'line') #graph.graph['splines'] = 'line' cut_graph = graph.copy() edge_list = list(cut_graph.edges()) edge_nids = np.array(ibs.unflat_map(ibs.get_annot_nids, edge_list)) cut_flags = edge_nids.T[0] != edge_nids.T[1] cut_edges = ut.compress(edge_list, cut_flags) cut_graph.remove_edges_from(cut_edges) ut.nx_delete_node_attr(cut_graph, 'nid') viz_graph.ensure_graph_nid_labels(cut_graph, ibs=ibs) #ut.nx_get_default_node_attributes(exemplars, 'color', None) ut.nx_delete_node_attr(cut_graph, 'color', nodes=unlabeled_graph.nodes()) aid2_color = ut.nx_get_default_node_attributes(cut_graph, 'color', None) nid2_colors = ut.group_items(aid2_color.values(), ibs.get_annot_nids(aid2_color.keys())) nid2_colors = ut.map_dict_vals(ut.filter_Nones, nid2_colors) nid2_colors = ut.map_dict_vals(ut.unique, nid2_colors) #for val in nid2_colors.values(): # assert len(val) <= 1 # Get initial colors nid2_color_ = {nid: colors_[0] for nid, colors_ in nid2_colors.items() if len(colors_) == 1} graph = cut_graph viz_graph.color_by_nids(cut_graph, ibs=ibs, nid2_color_=nid2_color_) nx.set_node_attributes(cut_graph, 'framewidth', 4) pt.show_nx(cut_graph, layout='custom', layoutkw=layoutkw, as_directed=as_directed, hacknode=hacknode) pt.zoom_factory()
def show_arch_nx_graph(layers, fnum=None, fullinfo=True): r""" CommandLine: python -m ibeis_cnn.draw_net show_arch_nx_graph:0 --show python -m ibeis_cnn.draw_net show_arch_nx_graph:1 --show Example0: >>> # ENABLE_DOCTEST >>> from ibeis_cnn.draw_net import * # NOQA >>> from ibeis_cnn import models >>> model = models.mnist.MNISTModel(batch_size=128, output_dims=10, >>> data_shape=(24, 24, 3)) >>> model.init_arch() >>> layers = model.get_all_layers() >>> show_arch_nx_graph(layers) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() Example1: >>> # ENABLE_DOCTEST >>> from ibeis_cnn.draw_net import * # NOQA >>> from ibeis_cnn import models >>> model = models.SiameseCenterSurroundModel(autoinit=True) >>> layers = model.get_all_layers() >>> show_arch_nx_graph(layers) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import networkx as nx import plottool as pt import ibeis_cnn.__LASAGNE__ as lasange #from matplotlib import offsetbox #import matplotlib as mpl REMOVE_BATCH_SIZE = True from ibeis_cnn import net_strs def get_hex_color(layer_type): if 'Input' in layer_type: return '#A2CECE' if 'Conv2D' in layer_type: return '#7C9ABB' if 'Dense' in layer_type: return '#6CCF8D' if 'Pool' in layer_type: return '#9D9DD2' if 'SoftMax' in layer_type: return '#7E9FD9' else: return '#{0:x}'.format(hash(layer_type + 'salt') % 2 ** 24) node_dict = {} edge_list = [] edge_attrs = ut.ddict(dict) # Make layer ids (ensure no duplicates) layer_to_id = { l: repr(l) if l.name is None else l.name for l in set(layers) } keys_ = layer_to_id.keys() dups = ut.find_duplicate_items(layer_to_id.values()) for dupval, dupidxs in dups.items(): newval_fmt = dupval + '_%d' for layer in ut.take(keys_, dupidxs): newid = ut.get_nonconflicting_string(newval_fmt, layer_to_id.values()) layer_to_id[layer] = newid def layerid(layer): return layer_to_id[layer] main_nodes = [] for i, layer in enumerate(layers): layer_info = net_strs.get_layer_info(layer) layer_type = layer_info['classalias'] key = layerid(layer) color = get_hex_color(layer_info['classalias']) # Make label lines = [] if layer_info['name'] is not None: lines.append(layer_info['name']) if fullinfo: lines.append(layer_info['classalias']) for attr, val in layer_info['layer_attrs'].items(): if attr == 'shape' and REMOVE_BATCH_SIZE: val = val[1:] if attr == 'output_shape' and REMOVE_BATCH_SIZE: val = val[1:] lines.append('{0}: {1}'.format(attr, val)) nonlinearity = layer_info.get('nonlinearity') if nonlinearity is not None: alias_map = { 'LeakyRectify': 'LReLU', } val = layer_info['nonlinearity']['type'] val = alias_map.get(val, val) lines.append('nonlinearity:\n{0}'.format(val)) label = '\n'.join(lines) # append node is_main_layer = len(layer.params) > 0 #is_main_layer = len(lasange.layers.get_all_params(layer, trainable=True)) > 0 if layer_info['classname'] in lasange.layers.normalization.__all__: is_main_layer = False if layer_info['classname'] in lasange.layers.special.__all__: is_main_layer = False if layer_info['classname'].startswith('BatchNorm'): is_main_layer = False if layer_info['classname'].startswith('ElemwiseSum'): is_main_layer = True if layer_type == 'Input': is_main_layer = True if hasattr(layer, '_is_main_layer'): is_main_layer = layer._is_main_layer #if getattr(layer, 'name', '') is not None and getattr(layer, 'name', '') .endswith('/sum'): # is_main_layer = True node_attr = dict(name=key, label=label, color=color, fillcolor=color, style='filled', is_main_layer=is_main_layer) node_attr['is_main_layer'] = is_main_layer if is_main_layer: main_nodes.append(key) node_attr['classalias'] = layer_info['classalias'] if is_main_layer or node_attr['classalias'].startswith('Conv'): if hasattr(layer, 'shape'): if len(layer.shape) == 3: node_attr['out_size'] = (layer.shape[2], layer.shape[1]) node_attr['depth'] = layer.output_shape[0] if hasattr(layer, 'output_shape'): if len(layer.output_shape) == 4: depth = layer.output_shape[1] width, height = (layer.output_shape[3], layer.output_shape[2]) xshift = -width * (.1 / (depth ** (1 / 3))) / 3 yshift = height * (.1 / (depth ** (1 / 3))) / 2 node_attr['depth'] = depth node_attr['xshift'] = xshift node_attr['yshift'] = yshift node_attr['out_size'] = (width, height) if len(layer.output_shape) == 2: node_attr['out_size'] = (1, layer.output_shape[1]) node_dict[key] = node_attr _input_layers = [] if hasattr(layer, 'input_layers'): _input_layers += layer.input_layers if hasattr(layer, 'input_layer'): _input_layers += [layer.input_layer] for input_layer in _input_layers: parent_key = layerid(input_layer) edge = (parent_key, key) edge_list.append(edge) main_size_ = np.array((100, 100)) * 4 sub_size = np.array((75, 50)) * 4 # Setup scaled width and heights out_size_list = [v['out_size'] for v in node_dict.values() if 'out_size' in v] out_size_list = np.array(out_size_list) #out_size_list = out_size_list[out_size_list.T[0] > 1] area_arr = np.prod(out_size_list, axis=1) main_outsize = np.array(out_size_list[area_arr.argmax()]) #main_outsize = np.array(out_size_list[area_arr.argmin()]) scale = main_size_ / main_outsize scale_dense_max = .25 scale_dense_min = 8 for k, v in node_dict.items(): if v['is_main_layer'] or v['classalias'].startswith('Conv'): if 'out_size' in v: # Make dense layers more visible if v['classalias'] == 'Dense': v['shape'] = 'rect' v['width'] = scale_dense_min if v['out_size'][1] > main_outsize[1]: v['height'] = v['out_size'][1] * scale[1] * scale_dense_max elif v['out_size'][1] < scale_dense_min: v['height'] = scale_dense_min * v['out_size'][1] else: v['height'] = v['out_size'][1] elif v['classalias'].startswith('Conv'): v['shape'] = 'stack' #v['shape'] = 'rect' v['width'] = v['out_size'][0] * scale[0] v['height'] = v['out_size'][1] * scale[1] else: v['shape'] = 'rect' v['width'] = v['out_size'][0] * scale[0] v['height'] = v['out_size'][1] * scale[1] else: v['shape'] = 'rect' v['width'] = main_size_[0] v['height'] = main_size_[1] else: #v['shape'] = 'ellipse' v['shape'] = 'rect' v['style'] = 'rounded' v['width'] = sub_size[0] v['height'] = sub_size[1] key_order = ut.take(layer_to_id, layers) node_dict = ut.dict_subset(node_dict, key_order) #print('node_dict = ' + ut.repr3(node_dict)) # Create the networkx graph structure G = nx.DiGraph() G.add_nodes_from(node_dict.items()) G.add_edges_from(edge_list) for key, val in edge_attrs.items(): nx.set_edge_attributes(G, key, val) # Add invisible structure #main_nodes = [key for key, val in # nx.get_node_attributes(G, 'is_main_layer').items() if val] main_children = ut.odict() #for n1, n2 in ut.itertwo(main_nodes): # print('n1, n2 = %r %r' % (n1, n2)) # import utool # utool.embed() # children = ut.nx_all_nodes_between(G, n1, n2) # if n1 in children: # children.remove(n1) # if n2 in children: # children.remove(n2) # main_children[n1] = children # #pass #main_children[main_nodes[-1]] = [] for n1 in main_nodes: main_children[n1] = [] # Main nodes only place constraints on nodes in the next main group. # Not their own next_main = None G.node[n1]['group'] = n1 for (_, n2) in nx.bfs_edges(G, n1): if next_main is None: if n2 in main_nodes: next_main = n2 else: G.node[n2]['group'] = n1 main_children[n1].append(n2) else: if n2 not in list(nx.descendants(G, next_main)): G.node[n2]['group'] = n1 main_children[n1].append(n2) # Custom positioning x = 0 y = 1000 #print('main_children = %s' % (ut.repr3(main_children),)) #main_nodes = ut.isect(list(nx.topological_sort(G)), main_nodes) xpad = main_size_[0] * .3 ypad = main_size_[1] * .3 # Draw each main node, and then put its children under it # Then move to the left and draw the next main node. cumwidth = 0 for n1 in main_nodes: cumheight = 0 maxwidth = G.node[n1]['width'] for n2 in main_children[n1]: maxwidth = max(maxwidth, G.node[n2]['width']) cumwidth += xpad cumwidth += maxwidth / 2 pos = np.array([x + cumwidth, y - cumheight]) G.node[n1]['pos'] = pos G.node[n1]['pin'] = 'true' height = G.node[n1]['height'] cumheight += height / 2 for n2 in main_children[n1]: height = G.node[n2]['height'] cumheight += ypad cumheight += height / 2 pos = np.array([x + cumwidth, y - cumheight]) G.node[n2]['pos'] = pos G.node[n2]['pin'] = 'true' cumheight += height / 2 cumwidth += maxwidth / 2 # Pin everybody nx.set_node_attributes(G, 'pin', 'true') layoutkw = dict(prog='neato', splines='line') #layoutkw = dict(prog='neato', splines='spline') layoutkw = dict(prog='neato', splines='ortho') G_ = G.copy() # delete lables for positioning _labels = nx.get_node_attributes(G_, 'label') ut.nx_delete_node_attr(G_, 'label') nx.set_node_attributes(G_, 'label', '') nolayout = False if nolayout: G_.remove_edges_from(list(G_.edges())) else: layout_info = pt.nx_agraph_layout(G_, inplace=True, **layoutkw) # NOQA # reset labels if not nolayout: nx.set_node_attributes(G_, 'label', _labels) _ = pt.show_nx(G_, fontsize=8, arrow_width=.3, layout='custom', fnum=fnum) # NOQA #pt.adjust_subplots(top=1, bot=0, left=0, right=1) pt.plt.tight_layout()
def expected_input_order(table): """ Returns what input (to depc.get_rowids) ordering should be be in parent_rowids """ from six.moves import zip_longest nonfinal_compute_order = table.nonfinal_compute_order() expanded_input_graph = table.expanded_input_graph hgroupids = ut.ddict(list) for _tablename, order in reversed(nonfinal_compute_order): if _tablename == table.depc.root: continue for t in order: s = expanded_input_graph[t] colxs = [y['parent_colx'] for x in s.pred[_tablename].values() for y in x.values()] assert len(colxs) > 0 colx = min(colxs) #order_colxs.append(colx) hgroupids[t].append(colx) hgroupids = dict(hgroupids) keys = hgroupids.keys() vals = hgroupids.values() groupids = list(zip_longest(*vals, fillvalue=0)) hgroups = ut.hierarchical_group_items(keys, groupids) fgroups = ut.flatten_dict_items(hgroups) fkey_list = [int(''.join(map(str, key))) for key in fgroups.keys()] fval_list = fgroups.values() dupkeys = ut.find_duplicate_items(fkey_list) assert len(dupkeys) == 0, 'cannot have duplicate orderings' expected_input_order = ut.flatten(ut.sortedby(fval_list, fkey_list)) return expected_input_order if False: nodes = ut.all_nodes_between(graph, source, target) tablegraph = graph.subgraph(nodes) import plottool as pt # pt.show_nx(tablegraph.reverse()) # sink = ut.nx_sink_nodes(tablegraph)[0] # bfs_edges = list(ut.bfs_multi_edges(G, sink, data=True, reverse=True)) G = tablegraph source = ut.nx_source_nodes(tablegraph)[0] bfs_edges = list(ut.bfs_multi_edges(G, source, data=0, reverse=False)) print('bfs_edges = %r' % (bfs_edges,)) T = nx.MultiDiGraph() T.add_node(source) T.add_edges_from(bfs_edges) pt.show_nx(T) def find_suffix(k, d): suffix = '' if d['ismulti']: suffix += '_SET' if k != 0: suffix += '_X' + str(k) return suffix G2 = nx.MultiDiGraph() # for u, v, k, d in G.edges(keys=True, data=True): edge_iter = ((u, v, k, d) for u in nx.topological_sort(G)[::-1] for v, kd in G[u].items() for k, d in kd.items()) edges = list(edge_iter) for u, v, k, d in edges: s0 = '' # s0 = '_X0' suffix = find_suffix(k, d) if len(suffix) == 0: G2.add_edge(u + s0, v + s0, attr_dict=d) else: G2.add_edge(u + suffix, v + s0, attr_dict=d) path_list = list(ut.all_multi_paths(G, source, u, data=True)) for path in path_list: rpath = ut.reverse_path_edges(path) parent_suffix = suffix for redge in rpath: v2, u2, k2, d2 = redge u2 += parent_suffix parent_suffix += find_suffix(k2, d2) v2 += parent_suffix if not G2.has_edge(u2, v2): # if p2 not in G2.node: G2.add_edge(u2, v2) pt.show_nx(G2) pt.show_nx(G) def compress_rinput_pathid(rinput_path_id): prev = None # rinput_path_id[0] compressed = [] for item in rinput_path_id: #if item != prev and not (item == '1' and prev == '2'): #if item != prev: compressed.append(item) # else: # compressed.append(prev) #prev = item if len(compressed) > 1: compressed = compressed[1:] compressed = tuple(compressed) return compressed [[edge[3]['rinput_path_id'] for edge in path] for path in accum_path_edges] x = [[compress_rinput_pathid(edge[3]['rinput_path_id']) for edge in path] for path in accum_path_edges] >>> for type_, subgraph in expanded_input_graph.items(): >>> inter.append_plot(ut.partial(pt.show_nx, subgraph, >>> title=type_))
def setcover_example(): """ CommandLine: python -m ibeis.scripts.specialdraw setcover_example --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = setcover_example() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import plottool as pt from ibeis.viz import viz_graph import networkx as nx pt.ensure_pylab_qt4() ibs = ibeis.opendb(defaultdb='testdb2') if False: # Select a good set aids = ibs.get_name_aids(ibs.get_valid_nids()) # ibeis.testdata_aids('testdb2', a='default:mingt=2') aids = [a for a in aids if len(a) > 1] for a in aids: print(ut.repr3(ibs.get_annot_stats_dict(a))) print(aids[-2]) #aids = [78, 79, 80, 81, 88, 91] aids = [78, 79, 81, 88, 91] qreq_ = ibs.depc.new_request('vsone', aids, aids, cfgdict={}) cm_list = qreq_.execute() from ibeis.algo.hots import graph_iden infr = graph_iden.AnnotInference(cm_list) unique_aids, prob_annots = infr.make_prob_annots() import numpy as np print(ut.hz_str('prob_annots = ', ut.array2string2(prob_annots, precision=2, max_line_width=140, suppress_small=True))) # ut.setcover_greedy(candidate_sets_dict) max_weight = 3 prob_annots[np.diag_indices(len(prob_annots))] = np.inf prob_annots = prob_annots thresh_points = np.sort(prob_annots[np.isfinite(prob_annots)]) # probably not the best way to go about searching for these thresholds # but when you have a hammer... if False: quant = sorted(np.diff(thresh_points))[(len(thresh_points) - 1) // 2 ] candset = {point: thresh_points[np.abs(thresh_points - point) < quant] for point in thresh_points} check_thresholds = len(aids) * 2 thresh_points2 = np.array(ut.setcover_greedy(candset, max_weight=check_thresholds).keys()) thresh_points = thresh_points2 # pt.plot(sorted(thresh_points), 'rx') # pt.plot(sorted(thresh_points2), 'o') # prob_annots = prob_annots.T # thresh_start = np.mean(thresh_points) current_idxs = [] current_covers = [] current_val = np.inf for thresh in thresh_points: covering_sets = [np.where(row >= thresh)[0] for row in (prob_annots)] candidate_sets_dict = {ax: others for ax, others in enumerate(covering_sets)} soln_cover = ut.setcover_ilp(candidate_sets_dict, max_weight=max_weight) exemplar_idxs = list(soln_cover.keys()) soln_weight = len(exemplar_idxs) val = max_weight - soln_weight # print('val = %r' % (val,)) # print('soln_weight = %r' % (soln_weight,)) if val < current_val: current_val = val current_covers = covering_sets current_idxs = exemplar_idxs exemplars = ut.take(aids, current_idxs) ensure_edges = [(aids[ax], aids[ax2]) for ax, other_xs in enumerate(current_covers) for ax2 in other_xs] graph = viz_graph.make_netx_graph_from_aid_groups( ibs, [aids], allow_directed=True, ensure_edges=ensure_edges, temp_nids=[1] * len(aids)) viz_graph.ensure_node_images(ibs, graph) nx.set_node_attributes(graph, 'framewidth', False) nx.set_node_attributes(graph, 'framewidth', {aid: 4.0 for aid in exemplars}) nx.set_edge_attributes(graph, 'color', pt.ORANGE) nx.set_node_attributes(graph, 'color', pt.LIGHT_BLUE) nx.set_node_attributes(graph, 'shape', 'rect') layoutkw = { 'sep' : 1 / 10, 'prog': 'neato', 'overlap': 'false', #'splines': 'ortho', 'splines': 'spline', } pt.show_nx(graph, layout='agraph', layoutkw=layoutkw) pt.zoom_factory()
def _model_data_flow_to_networkx(model_info): layers = model_info['layer'] import networkx as nx G = nx.DiGraph() prev = None # Stores last node with the data for this layer in it prev_map = {} SHOW_LOOPS = False for layer in layers: name = layer.get('name') print('name = {!r}'.format(name)) G.add_node(name) bottom = set(layer.get('bottom', [])) top = set(layer.get('top', [])) both = top.intersection(bottom) if both: if prev is None: prev = both for b in both: prev_map[b] = name for b in prev: print(' * b = {!r}'.format(b)) G.add_edge(b, name, constraint=False) for b in both: print(' * b = {!r}'.format(b)) kw = {} if not G.has_edge(b, name): kw['color'] = 'red' G.add_edge(b, name, constraint=True, **kw) prev = [name] else: prev = None # for b in (bottom - both): for b in bottom: print(' * b = {!r}'.format(b)) constraint = True G.add_edge(prev_map.get(b, b), name, constraint=constraint) if SHOW_LOOPS: G.add_edge(b, name) # for t in (bottom - top): for t in top: print(' * t = {!r}'.format(t)) constraint = True G.add_edge(name, prev_map.get(t, t), constraint=constraint) if SHOW_LOOPS: G.add_edge(name, t) G.remove_edges_from(list(G.selfloop_edges())) import plottool as pt pt.qtensure() pt.show_nx(G, arrow_width=1) pt.adjust_subplots(left=0, right=1, top=1, bottom=0) pt.pan_factory() pt.zoom_factory() list(nx.topological_sort(G))
def show_graph(infr, graph=None, use_image=False, update_attrs=True, with_colorbar=False, pnum=(1, 1, 1), zoomable=True, pickable=False, **kwargs): r""" Args: infr (?): graph (None): (default = None) use_image (bool): (default = False) update_attrs (bool): (default = True) with_colorbar (bool): (default = False) pnum (tuple): plot number(default = (1, 1, 1)) zoomable (bool): (default = True) pickable (bool): (de = False) **kwargs: verbose, with_labels, fnum, layout, ax, pos, img_dict, title, layoutkw, framewidth, modify_ax, as_directed, hacknoedge, hacknode, node_labels, arrow_width, fontsize, fontweight, fontname, fontfamilty, fontproperties CommandLine: python -m ibeis.algo.graph.mixin_viz GraphVisualization.show_graph --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.graph.mixin_viz import * # NOQA >>> from ibeis.algo.graph import demo >>> import plottool as pt >>> infr = demo.demodata_infr(ccs=ut.estarmap( >>> range, [(1, 6), (6, 10), (10, 13), (13, 15), (15, 16), >>> (17, 20)])) >>> pnum_ = pt.make_pnum_nextgen(nRows=1, nCols=3) >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> infr.add_feedback((1, 5), INCMP) >>> infr.add_feedback((14, 18), INCMP) >>> infr.refresh_candidate_edges() >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> infr.add_feedback((17, 18), NEGTV) # add inconsistency >>> infr.apply_nondynamic_update() >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> ut.show_if_requested() """ import plottool as pt if graph is None: graph = infr.graph # kwargs['fontsize'] = kwargs.get('fontsize', 8) with warnings.catch_warnings(): warnings.simplefilter("ignore") # default_update_kw = ut.get_func_kwargs(infr.update_visual_attrs) # update_kw = ut.update_existing(default_update_kw, kwargs) # infr.update_visual_attrs(**update_kw) if update_attrs: infr.update_visual_attrs(graph=graph, **kwargs) verbose = kwargs.pop('verbose', infr.verbose) pt.show_nx(graph, layout='custom', as_directed=False, modify_ax=False, use_image=use_image, pnum=pnum, verbose=verbose, **kwargs) if zoomable: pt.zoom_factory() pt.pan_factory(pt.gca()) # if with_colorbar: # # Draw a colorbar # _normal_ticks = np.linspace(0, 1, num=11) # _normal_scores = np.linspace(0, 1, num=500) # _normal_colors = infr.get_colored_weights(_normal_scores) # cb = pt.colorbar(_normal_scores, _normal_colors, lbl='weights', # ticklabels=_normal_ticks) # # point to threshold location # thresh = None # if thresh is not None: # xy = (1, thresh) # xytext = (2.5, .3 if thresh < .5 else .7) # cb.ax.annotate('threshold', xy=xy, xytext=xytext, # arrowprops=dict( # alpha=.5, fc="0.6", # connectionstyle="angle3,angleA=90,angleB=0"),) # infr.graph if graph.graph.get('dark_background', None): pt.dark_background(force=True) if pickable: fig = pt.gcf() fig.canvas.mpl_connect('pick_event', ut.partial(on_pick, infr=infr))
def double_depcache_graph(): r""" CommandLine: python -m ibeis.scripts.specialdraw double_depcache_graph --show --testmode python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite --arrow-width=.5 python -m ibeis.scripts.specialdraw double_depcache_graph --save=figures5/doubledepc.png --dpath ~/latex/cand/ --diskshow --figsize=8,20 --dpi=220 --testmode --show --clipwhite --arrow-width=5 Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = double_depcache_graph() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import networkx as nx import plottool as pt pt.ensure_pylab_qt4() # pt.plt.xkcd() ibs = ibeis.opendb('testdb1') reduced = True implicit = True annot_graph = ibs.depc_annot.make_graph(reduced=reduced, implicit=implicit) image_graph = ibs.depc_image.make_graph(reduced=reduced, implicit=implicit) to_rename = ut.isect(image_graph.nodes(), annot_graph.nodes()) nx.relabel_nodes(annot_graph, {x: 'annot_' + x for x in to_rename}, copy=False) nx.relabel_nodes(image_graph, {x: 'image_' + x for x in to_rename}, copy=False) graph = nx.compose_all([image_graph, annot_graph]) #graph = nx.union_all([image_graph, annot_graph], rename=('image', 'annot')) # userdecision = ut.nx_makenode(graph, 'user decision', shape='rect', color=pt.DARK_YELLOW, style='diagonals') # userdecision = ut.nx_makenode(graph, 'user decision', shape='circle', color=pt.DARK_YELLOW) userdecision = ut.nx_makenode(graph, 'User decision', shape='rect', #width=100, height=100, color=pt.YELLOW, style='diagonals') #longcat = True longcat = False #edge = ('feat', 'neighbor_index') #data = graph.get_edge_data(*edge)[0] #print('data = %r' % (data,)) #graph.remove_edge(*edge) ## hack #graph.add_edge('featweight', 'neighbor_index', **data) graph.add_edge('detections', userdecision, constraint=longcat, color=pt.PINK) graph.add_edge(userdecision, 'annotations', constraint=longcat, color=pt.PINK) # graph.add_edge(userdecision, 'annotations', implicit=True, color=[0, 0, 0]) if not longcat: pass #graph.add_edge('images', 'annotations', style='invis') #graph.add_edge('thumbnails', 'annotations', style='invis') #graph.add_edge('thumbnails', userdecision, style='invis') graph.remove_node('Has_Notch') graph.remove_node('annotmask') layoutkw = { 'ranksep': 5, 'nodesep': 5, 'dpi': 96, # 'nodesep': 1, } ns = 1000 ut.nx_set_default_node_attributes(graph, 'fontsize', 72) ut.nx_set_default_node_attributes(graph, 'fontname', 'Ubuntu') ut.nx_set_default_node_attributes(graph, 'style', 'filled') ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns * (1 / ut.PHI)) #for u, v, d in graph.edge(data=True): for u, vkd in graph.edge.items(): for v, dk in vkd.items(): for k, d in dk.items(): localid = d.get('local_input_id') if localid: # d['headlabel'] = localid if localid not in ['1']: d['taillabel'] = localid #d['label'] = localid if d.get('taillabel') in {'1'}: del d['taillabel'] node_alias = { 'chips': 'Chip', 'images': 'Image', 'feat': 'Feat', 'featweight': 'Feat Weights', 'thumbnails': 'Thumbnail', 'detections': 'Detections', 'annotations': 'Annotation', 'Notch_Tips': 'Notch Tips', 'probchip': 'Prob Chip', 'Cropped_Chips': 'Croped Chip', 'Trailing_Edge': 'Trailing\nEdge', 'Block_Curvature': 'Block\nCurvature', # 'BC_DTW': 'block curvature /\n dynamic time warp', 'BC_DTW': 'DTW Distance', 'vsone': 'Hots vsone', 'feat_neighbs': 'Nearest\nNeighbors', 'neighbor_index': 'Neighbor\nIndex', 'vsmany': 'Hots vsmany', 'annot_labeler': 'Annot Labeler', 'labeler': 'Labeler', 'localizations': 'Localizations', 'classifier': 'Classifier', 'sver': 'Spatial\nVerification', 'Classifier': 'Existence', 'image_labeler': 'Image Labeler', } node_alias = { 'Classifier': 'existence', 'feat_neighbs': 'neighbors', 'sver': 'spatial_verification', 'Cropped_Chips': 'cropped_chip', 'BC_DTW': 'dtw_distance', 'Block_Curvature': 'curvature', 'Trailing_Edge': 'trailing_edge', 'Notch_Tips': 'notch_tips', 'thumbnails': 'thumbnail', 'images': 'image', 'annotations': 'annotation', 'chips': 'chip', #userdecision: 'User de' } node_alias = ut.delete_dict_keys(node_alias, ut.setdiff(node_alias.keys(), graph.nodes())) nx.relabel_nodes(graph, node_alias, copy=False) fontkw = dict(fontname='Ubuntu', fontweight='normal', fontsize=12) #pt.gca().set_aspect('equal') #pt.figure() pt.show_nx(graph, layoutkw=layoutkw, fontkw=fontkw) pt.zoom_factory()
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
def draw_twoday_count(ibs, visit_info_list_): import copy visit_info_list = copy.deepcopy(visit_info_list_) aids_day1, aids_day2 = ut.take_column(visit_info_list_, 'aids') nids_day1, nids_day2 = ut.take_column(visit_info_list_, 'unique_nids') resight_nids = ut.isect(nids_day1, nids_day2) if False: # HACK REMOVE DATA TO MAKE THIS FASTER num = 20 for info in visit_info_list: non_resight_nids = list(set(info['unique_nids']) - set(resight_nids)) sample_nids2 = non_resight_nids[0:num] + resight_nids[:num] info['grouped_aids'] = ut.dict_subset(info['grouped_aids'], sample_nids2) info['unique_nids'] = sample_nids2 # Build a graph of matches if False: debug = False for info in visit_info_list: edges = [] grouped_aids = info['grouped_aids'] aids_list = list(grouped_aids.values()) ams_list = ibs.get_annotmatch_rowids_in_cliques(aids_list) aids1_list = ibs.unflat_map(ibs.get_annotmatch_aid1, ams_list) aids2_list = ibs.unflat_map(ibs.get_annotmatch_aid2, ams_list) for ams, aids, aids1, aids2 in zip(ams_list, aids_list, aids1_list, aids2_list): edge_nodes = set(aids1 + aids2) ##if len(edge_nodes) != len(set(aids)): # #print('--') # #print('aids = %r' % (aids,)) # #print('edge_nodes = %r' % (edge_nodes,)) bad_aids = edge_nodes - set(aids) if len(bad_aids) > 0: print('bad_aids = %r' % (bad_aids,)) unlinked_aids = set(aids) - edge_nodes mst_links = list(ut.itertwo(list(unlinked_aids) + list(edge_nodes)[:1])) bad_aids.add(None) user_links = [(u, v) for (u, v) in zip(aids1, aids2) if u not in bad_aids and v not in bad_aids] new_edges = mst_links + user_links new_edges = [(int(u), int(v)) for u, v in new_edges if u not in bad_aids and v not in bad_aids] edges += new_edges info['edges'] = edges # Add edges between days grouped_aids1, grouped_aids2 = ut.take_column(visit_info_list, 'grouped_aids') nids_day1, nids_day2 = ut.take_column(visit_info_list, 'unique_nids') resight_nids = ut.isect(nids_day1, nids_day2) resight_aids1 = ut.take(grouped_aids1, resight_nids) resight_aids2 = ut.take(grouped_aids2, resight_nids) #resight_aids3 = [list(aids1) + list(aids2) for aids1, aids2 in zip(resight_aids1, resight_aids2)] ams_list = ibs.get_annotmatch_rowids_between_groups(resight_aids1, resight_aids2) aids1_list = ibs.unflat_map(ibs.get_annotmatch_aid1, ams_list) aids2_list = ibs.unflat_map(ibs.get_annotmatch_aid2, ams_list) between_edges = [] for ams, aids1, aids2, rawaids1, rawaids2 in zip(ams_list, aids1_list, aids2_list, resight_aids1, resight_aids2): link_aids = aids1 + aids2 rawaids3 = rawaids1 + rawaids2 badaids = ut.setdiff(link_aids, rawaids3) assert not badaids user_links = [(int(u), int(v)) for (u, v) in zip(aids1, aids2) if u is not None and v is not None] # HACK THIS OFF user_links = [] if len(user_links) == 0: # Hack in an edge between_edges += [(rawaids1[0], rawaids2[0])] else: between_edges += user_links assert np.all(0 == np.diff(np.array(ibs.unflat_map(ibs.get_annot_nids, between_edges)), axis=1)) import plottool as pt import networkx as nx #pt.qt4ensure() #len(list(nx.connected_components(graph1))) #print(ut.graph_info(graph1)) # Layout graph layoutkw = dict( prog='neato', draw_implicit=False, splines='line', #splines='curved', #splines='spline', #sep=10 / 72, #prog='dot', rankdir='TB', ) def translate_graph_to_origin(graph): x, y, w, h = ut.get_graph_bounding_box(graph) ut.translate_graph(graph, (-x, -y)) def stack_graphs(graph_list, vert=False, pad=None): graph_list_ = [g.copy() for g in graph_list] for g in graph_list_: translate_graph_to_origin(g) bbox_list = [ut.get_graph_bounding_box(g) for g in graph_list_] if vert: dim1 = 3 dim2 = 2 else: dim1 = 2 dim2 = 3 dim1_list = np.array([bbox[dim1] for bbox in bbox_list]) dim2_list = np.array([bbox[dim2] for bbox in bbox_list]) if pad is None: pad = np.mean(dim1_list) / 2 offset1_list = ut.cumsum([0] + [d + pad for d in dim1_list[:-1]]) max_dim2 = max(dim2_list) offset2_list = [(max_dim2 - d2) / 2 for d2 in dim2_list] if vert: t_xy_list = [(d2, d1) for d1, d2 in zip(offset1_list, offset2_list)] else: t_xy_list = [(d1, d2) for d1, d2 in zip(offset1_list, offset2_list)] for g, t_xy in zip(graph_list_, t_xy_list): ut.translate_graph(g, t_xy) nx.set_node_attributes(g, name='pin', values='true') new_graph = nx.compose_all(graph_list_) #pt.show_nx(new_graph, layout='custom', node_labels=False, as_directed=False) # NOQA return new_graph # Construct graph for count, info in enumerate(visit_info_list): graph = nx.Graph() edges = [(int(u), int(v)) for u, v in info['edges'] if u is not None and v is not None] graph.add_edges_from(edges, attr_dict={'zorder': 10}) nx.set_node_attributes(graph, name='zorder', values=20) # Layout in neato _ = pt.nx_agraph_layout(graph, inplace=True, **layoutkw) # NOQA # Extract components and then flatten in nid ordering ccs = list(nx.connected_components(graph)) root_aids = [] cc_graphs = [] for cc_nodes in ccs: cc = graph.subgraph(cc_nodes) try: root_aids.append(list(ut.nx_source_nodes(cc.to_directed()))[0]) except nx.NetworkXUnfeasible: root_aids.append(list(cc.nodes())[0]) cc_graphs.append(cc) root_nids = ibs.get_annot_nids(root_aids) nid2_graph = dict(zip(root_nids, cc_graphs)) resight_nids_ = set(resight_nids).intersection(set(root_nids)) noresight_nids_ = set(root_nids) - resight_nids_ n_graph_list = ut.take(nid2_graph, sorted(noresight_nids_)) r_graph_list = ut.take(nid2_graph, sorted(resight_nids_)) if len(n_graph_list) > 0: n_graph = nx.compose_all(n_graph_list) _ = pt.nx_agraph_layout(n_graph, inplace=True, **layoutkw) # NOQA n_graphs = [n_graph] else: n_graphs = [] r_graphs = [stack_graphs(chunk) for chunk in ut.ichunks(r_graph_list, 100)] if count == 0: new_graph = stack_graphs(n_graphs + r_graphs, vert=True) else: new_graph = stack_graphs(r_graphs[::-1] + n_graphs, vert=True) #pt.show_nx(new_graph, layout='custom', node_labels=False, as_directed=False) # NOQA info['graph'] = new_graph graph1_, graph2_ = ut.take_column(visit_info_list, 'graph') if False: _ = pt.show_nx(graph1_, layout='custom', node_labels=False, as_directed=False) # NOQA _ = pt.show_nx(graph2_, layout='custom', node_labels=False, as_directed=False) # NOQA graph_list = [graph1_, graph2_] twoday_graph = stack_graphs(graph_list, vert=True, pad=None) nx.set_node_attributes(twoday_graph, name='pin', values='true') if debug: ut.nx_delete_None_edge_attr(twoday_graph) ut.nx_delete_None_node_attr(twoday_graph) print('twoday_graph(pre) info' + ut.repr3(ut.graph_info(twoday_graph), nl=2)) # Hack, no idea why there are nodes that dont exist here between_edges_ = [edge for edge in between_edges if twoday_graph.has_node(edge[0]) and twoday_graph.has_node(edge[1])] twoday_graph.add_edges_from(between_edges_, attr_dict={'alpha': .2, 'zorder': 0}) ut.nx_ensure_agraph_color(twoday_graph) layoutkw['splines'] = 'line' layoutkw['prog'] = 'neato' agraph = pt.nx_agraph_layout(twoday_graph, inplace=True, return_agraph=True, **layoutkw)[-1] # NOQA if False: fpath = ut.truepath('~/ggr_graph.png') agraph.draw(fpath) ut.startfile(fpath) if debug: print('twoday_graph(post) info' + ut.repr3(ut.graph_info(twoday_graph))) _ = pt.show_nx(twoday_graph, layout='custom', node_labels=False, as_directed=False) # NOQA
def make_graph(infr, show=False): import networkx as nx import itertools cm_list = infr.cm_list unique_nids, prob_names = infr.make_prob_names() thresh = infr.choose_thresh() # Simply cut any edge with a weight less than a threshold qaid_list = [cm.qaid for cm in cm_list] postcut = prob_names > thresh qxs, nxs = np.where(postcut) if False: kw = dict(precision=2, max_line_width=140, suppress_small=True) print(ut.hz_str('prob_names = ', ut.array2string2((prob_names), **kw))) print(ut.hz_str('postcut = ', ut.array2string2((postcut).astype(np.int), **kw))) matching_qaids = ut.take(qaid_list, qxs) matched_nids = ut.take(unique_nids, nxs) qreq_ = infr.qreq_ nodes = ut.unique(qreq_.qaids.tolist() + qreq_.daids.tolist()) if not hasattr(qreq_, 'dnids'): qreq_.dnids = qreq_.ibs.get_annot_nids(qreq_.daids) qreq_.qnids = qreq_.ibs.get_annot_nids(qreq_.qaids) dnid2_daids = ut.group_items(qreq_.daids, qreq_.dnids) grouped_aids = dnid2_daids.values() matched_daids = ut.take(dnid2_daids, matched_nids) name_cliques = [list(itertools.combinations(aids, 2)) for aids in grouped_aids] aid_matches = [list(ut.product([qaid], daids)) for qaid, daids in zip(matching_qaids, matched_daids)] graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(ut.flatten(name_cliques)) graph.add_edges_from(ut.flatten(aid_matches)) #matchless_quries = ut.take(qaid_list, ut.index_complement(qxs, len(qaid_list))) name_nodes = [('nid', l) for l in qreq_.dnids] db_aid_nid_edges = list(zip(qreq_.daids, name_nodes)) #query_aid_nid_edges = list(zip(matching_qaids, [('nid', l) for l in matched_nids])) #G = nx.Graph() #G.add_nodes_from(matchless_quries) #G.add_edges_from(db_aid_nid_edges) #G.add_edges_from(query_aid_nid_edges) graph.add_edges_from(db_aid_nid_edges) if infr.user_feedback is not None: user_feedback = ut.map_dict_vals(np.array, infr.user_feedback) p_bg = 0.0 part1 = user_feedback['p_match'] * (1 - user_feedback['p_notcomp']) part2 = p_bg * user_feedback['p_notcomp'] p_same_list = part1 + part2 for aid1, aid2, p_same in zip(user_feedback['aid1'], user_feedback['aid2'], p_same_list): if p_same > .5: if not graph.has_edge(aid1, aid2): graph.add_edge(aid1, aid2) else: if graph.has_edge(aid1, aid2): graph.remove_edge(aid1, aid2) if show: import plottool as pt nx.set_node_attributes(graph, 'color', {aid: pt.LIGHT_PINK for aid in qreq_.daids}) nx.set_node_attributes(graph, 'color', {aid: pt.TRUE_BLUE for aid in qreq_.qaids}) nx.set_node_attributes(graph, 'color', { aid: pt.LIGHT_PURPLE for aid in np.intersect1d(qreq_.qaids, qreq_.daids)}) nx.set_node_attributes(graph, 'label', {node: 'n%r' % (node[1],) for node in name_nodes}) nx.set_node_attributes(graph, 'color', {node: pt.LIGHT_GREEN for node in name_nodes}) if show: import plottool as pt pt.show_nx(graph, layoutkw={'prog': 'neato'}, verbose=False) return graph
def setcover_example(): """ CommandLine: python -m ibeis.scripts.specialdraw setcover_example --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = setcover_example() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import plottool as pt from ibeis.viz import viz_graph import networkx as nx pt.ensure_pylab_qt4() ibs = ibeis.opendb(defaultdb='testdb2') if False: # Select a good set aids = ibs.get_name_aids(ibs.get_valid_nids()) # ibeis.testdata_aids('testdb2', a='default:mingt=2') aids = [a for a in aids if len(a) > 1] for a in aids: print(ut.repr3(ibs.get_annot_stats_dict(a))) print(aids[-2]) #aids = [78, 79, 80, 81, 88, 91] aids = [78, 79, 81, 88, 91] qreq_ = ibs.depc.new_request('vsone', aids, aids, cfgdict={}) cm_list = qreq_.execute() from ibeis.algo.hots import graph_iden infr = graph_iden.AnnotInference(cm_list) unique_aids, prob_annots = infr.make_prob_annots() import numpy as np print( ut.hz_str( 'prob_annots = ', ut.array2string2(prob_annots, precision=2, max_line_width=140, suppress_small=True))) # ut.setcover_greedy(candidate_sets_dict) max_weight = 3 prob_annots[np.diag_indices(len(prob_annots))] = np.inf prob_annots = prob_annots thresh_points = np.sort(prob_annots[np.isfinite(prob_annots)]) # probably not the best way to go about searching for these thresholds # but when you have a hammer... if False: quant = sorted(np.diff(thresh_points))[(len(thresh_points) - 1) // 2] candset = { point: thresh_points[np.abs(thresh_points - point) < quant] for point in thresh_points } check_thresholds = len(aids) * 2 thresh_points2 = np.array( ut.setcover_greedy(candset, max_weight=check_thresholds).keys()) thresh_points = thresh_points2 # pt.plot(sorted(thresh_points), 'rx') # pt.plot(sorted(thresh_points2), 'o') # prob_annots = prob_annots.T # thresh_start = np.mean(thresh_points) current_idxs = [] current_covers = [] current_val = np.inf for thresh in thresh_points: covering_sets = [np.where(row >= thresh)[0] for row in (prob_annots)] candidate_sets_dict = { ax: others for ax, others in enumerate(covering_sets) } soln_cover = ut.setcover_ilp(candidate_sets_dict, max_weight=max_weight) exemplar_idxs = list(soln_cover.keys()) soln_weight = len(exemplar_idxs) val = max_weight - soln_weight # print('val = %r' % (val,)) # print('soln_weight = %r' % (soln_weight,)) if val < current_val: current_val = val current_covers = covering_sets current_idxs = exemplar_idxs exemplars = ut.take(aids, current_idxs) ensure_edges = [(aids[ax], aids[ax2]) for ax, other_xs in enumerate(current_covers) for ax2 in other_xs] graph = viz_graph.make_netx_graph_from_aid_groups( ibs, [aids], allow_directed=True, ensure_edges=ensure_edges, temp_nids=[1] * len(aids)) viz_graph.ensure_node_images(ibs, graph) nx.set_node_attributes(graph, 'framewidth', False) nx.set_node_attributes(graph, 'framewidth', {aid: 4.0 for aid in exemplars}) nx.set_edge_attributes(graph, 'color', pt.ORANGE) nx.set_node_attributes(graph, 'color', pt.LIGHT_BLUE) nx.set_node_attributes(graph, 'shape', 'rect') layoutkw = { 'sep': 1 / 10, 'prog': 'neato', 'overlap': 'false', #'splines': 'ortho', 'splines': 'spline', } pt.show_nx(graph, layout='agraph', layoutkw=layoutkw) pt.zoom_factory()
def intraoccurrence_connected(): r""" CommandLine: python -m ibeis.scripts.specialdraw intraoccurrence_connected --show python -m ibeis.scripts.specialdraw intraoccurrence_connected --show --postcut python -m ibeis.scripts.specialdraw intraoccurrence_connected --show --smaller Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = intraoccurrence_connected() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import ibeis import plottool as pt from ibeis.viz import viz_graph import networkx as nx pt.ensure_pylab_qt4() ibs = ibeis.opendb(defaultdb='PZ_Master1') nid2_aid = { #4880: [3690, 3696, 3703, 3706, 3712, 3721], 4880: [3690, 3696, 3703], 6537: [3739], 6653: [7671], 6610: [7566, 7408], #6612: [7664, 7462, 7522], #6624: [7465, 7360], #6625: [7746, 7383, 7390, 7477, 7376, 7579], 6630: [7586, 7377, 7464, 7478], #6677: [7500] } nid2_dbaids = {4880: [33, 6120, 7164], 6537: [7017, 7206], 6653: [7660]} if ut.get_argflag('--small') or ut.get_argflag('--smaller'): del nid2_aid[6630] del nid2_aid[6537] del nid2_dbaids[6537] if ut.get_argflag('--smaller'): nid2_dbaids[4880].remove(33) nid2_aid[4880].remove(3690) nid2_aid[6610].remove(7408) #del nid2_aid[4880] #del nid2_dbaids[4880] aids = ut.flatten(nid2_aid.values()) temp_nids = [1] * len(aids) postcut = ut.get_argflag('--postcut') aids_list = ibs.group_annots_by_name(aids)[0] ensure_edges = 'all' if True or not postcut else None unlabeled_graph = viz_graph.make_netx_graph_from_aid_groups( ibs, aids_list, #invis_edges=invis_edges, ensure_edges=ensure_edges, temp_nids=temp_nids) viz_graph.color_by_nids(unlabeled_graph, unique_nids=[1] * len(list(unlabeled_graph.nodes()))) viz_graph.ensure_node_images(ibs, unlabeled_graph) nx.set_node_attributes(unlabeled_graph, 'shape', 'rect') #unlabeled_graph = unlabeled_graph.to_undirected() # Find the "database exemplars for these annots" if False: gt_aids = ibs.get_annot_groundtruth(aids) gt_aids = [ut.setdiff(s, aids) for s in gt_aids] dbaids = ut.unique(ut.flatten(gt_aids)) dbaids = ibs.filter_annots_general(dbaids, minqual='good') ibs.get_annot_quality_texts(dbaids) else: dbaids = ut.flatten(nid2_dbaids.values()) exemplars = nx.DiGraph() #graph = exemplars # NOQA exemplars.add_nodes_from(dbaids) def add_clique(graph, nodes, edgeattrs={}, nodeattrs={}): edge_list = ut.upper_diag_self_prodx(nodes) graph.add_edges_from(edge_list, **edgeattrs) return edge_list for aids_, nid in zip(*ibs.group_annots_by_name(dbaids)): add_clique(exemplars, aids_) viz_graph.ensure_node_images(ibs, exemplars) viz_graph.color_by_nids(exemplars, ibs=ibs) nx.set_node_attributes(unlabeled_graph, 'framewidth', False) nx.set_node_attributes(exemplars, 'framewidth', 4.0) nx.set_node_attributes(unlabeled_graph, 'group', 'unlab') nx.set_node_attributes(exemplars, 'group', 'exemp') #big_graph = nx.compose_all([unlabeled_graph]) big_graph = nx.compose_all([exemplars, unlabeled_graph]) # add sparse connections from unlabeled to exemplars import numpy as np rng = np.random.RandomState(0) if True or not postcut: for aid_ in unlabeled_graph.nodes(): flags = rng.rand(len(exemplars)) > .5 nid_ = ibs.get_annot_nids(aid_) exnids = np.array(ibs.get_annot_nids(list(exemplars.nodes()))) flags = np.logical_or(exnids == nid_, flags) exmatches = ut.compress(list(exemplars.nodes()), flags) big_graph.add_edges_from(list(ut.product([aid_], exmatches)), color=pt.ORANGE, implicit=True) else: for aid_ in unlabeled_graph.nodes(): flags = rng.rand(len(exemplars)) > .5 exmatches = ut.compress(list(exemplars.nodes()), flags) nid_ = ibs.get_annot_nids(aid_) exnids = np.array(ibs.get_annot_nids(exmatches)) exmatches = ut.compress(exmatches, exnids == nid_) big_graph.add_edges_from(list(ut.product([aid_], exmatches))) pass nx.set_node_attributes(big_graph, 'shape', 'rect') #if False and postcut: # ut.nx_delete_node_attr(big_graph, 'nid') # ut.nx_delete_edge_attr(big_graph, 'color') # viz_graph.ensure_graph_nid_labels(big_graph, ibs=ibs) # viz_graph.color_by_nids(big_graph, ibs=ibs) # big_graph = big_graph.to_undirected() layoutkw = { 'sep': 1 / 5, 'prog': 'neato', 'overlap': 'false', #'splines': 'ortho', 'splines': 'spline', } as_directed = False #as_directed = True #hacknode = True hacknode = 0 graph = big_graph ut.nx_ensure_agraph_color(graph) if hacknode: nx.set_edge_attributes(graph, 'taillabel', {e: str(e[0]) for e in graph.edges()}) nx.set_edge_attributes(graph, 'headlabel', {e: str(e[1]) for e in graph.edges()}) explicit_graph = pt.get_explicit_graph(graph) _, layout_info = pt.nx_agraph_layout(explicit_graph, orig_graph=graph, inplace=True, **layoutkw) if ut.get_argflag('--smaller'): graph.node[7660]['pos'] = np.array([550, 350]) graph.node[6120]['pos'] = np.array([200, 600]) + np.array([350, -400]) graph.node[7164]['pos'] = np.array([200, 480]) + np.array([350, -400]) nx.set_node_attributes(graph, 'pin', 'true') _, layout_info = pt.nx_agraph_layout(graph, inplace=True, **layoutkw) elif ut.get_argflag('--small'): graph.node[7660]['pos'] = np.array([750, 350]) graph.node[33]['pos'] = np.array([300, 600]) + np.array([350, -400]) graph.node[6120]['pos'] = np.array([500, 600]) + np.array([350, -400]) graph.node[7164]['pos'] = np.array([410, 480]) + np.array([350, -400]) nx.set_node_attributes(graph, 'pin', 'true') _, layout_info = pt.nx_agraph_layout(graph, inplace=True, **layoutkw) if not postcut: #pt.show_nx(graph.to_undirected(), layout='agraph', layoutkw=layoutkw, # as_directed=False) #pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, # as_directed=as_directed, hacknode=hacknode) pt.show_nx(graph, layout='custom', layoutkw=layoutkw, as_directed=as_directed, hacknode=hacknode) else: #explicit_graph = pt.get_explicit_graph(graph) #_, layout_info = pt.nx_agraph_layout(explicit_graph, orig_graph=graph, # **layoutkw) #layout_info['edge']['alpha'] = .8 #pt.apply_graph_layout_attrs(graph, layout_info) #graph_layout_attrs = layout_info['graph'] ##edge_layout_attrs = layout_info['edge'] ##node_layout_attrs = layout_info['node'] #for key, vals in layout_info['node'].items(): # #print('[special] key = %r' % (key,)) # nx.set_node_attributes(graph, key, vals) #for key, vals in layout_info['edge'].items(): # #print('[special] key = %r' % (key,)) # nx.set_edge_attributes(graph, key, vals) #nx.set_edge_attributes(graph, 'alpha', .8) #graph.graph['splines'] = graph_layout_attrs.get('splines', 'line') #graph.graph['splines'] = 'polyline' # graph_layout_attrs.get('splines', 'line') #graph.graph['splines'] = 'line' cut_graph = graph.copy() edge_list = list(cut_graph.edges()) edge_nids = np.array(ibs.unflat_map(ibs.get_annot_nids, edge_list)) cut_flags = edge_nids.T[0] != edge_nids.T[1] cut_edges = ut.compress(edge_list, cut_flags) cut_graph.remove_edges_from(cut_edges) ut.nx_delete_node_attr(cut_graph, 'nid') viz_graph.ensure_graph_nid_labels(cut_graph, ibs=ibs) #ut.nx_get_default_node_attributes(exemplars, 'color', None) ut.nx_delete_node_attr(cut_graph, 'color', nodes=unlabeled_graph.nodes()) aid2_color = ut.nx_get_default_node_attributes(cut_graph, 'color', None) nid2_colors = ut.group_items(aid2_color.values(), ibs.get_annot_nids(aid2_color.keys())) nid2_colors = ut.map_dict_vals(ut.filter_Nones, nid2_colors) nid2_colors = ut.map_dict_vals(ut.unique, nid2_colors) #for val in nid2_colors.values(): # assert len(val) <= 1 # Get initial colors nid2_color_ = { nid: colors_[0] for nid, colors_ in nid2_colors.items() if len(colors_) == 1 } graph = cut_graph viz_graph.color_by_nids(cut_graph, ibs=ibs, nid2_color_=nid2_color_) nx.set_node_attributes(cut_graph, 'framewidth', 4) pt.show_nx(cut_graph, layout='custom', layoutkw=layoutkw, as_directed=as_directed, hacknode=hacknode) pt.zoom_factory()
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
def graphcut_flow(): r""" Returns: ?: name CommandLine: python -m ibeis.scripts.specialdraw graphcut_flow --show --save cutflow.png --diskshow --clipwhite python -m ibeis.scripts.specialdraw graphcut_flow --save figures4/cutiden.png --diskshow --clipwhite --dpath ~/latex/crall-candidacy-2015/ --figsize=24,10 --arrow-width=2.0 Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> graphcut_flow() >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import plottool as pt pt.ensure_pylab_qt4() import networkx as nx # pt.plt.xkcd() graph = nx.DiGraph() def makecluster(name, num, **attrkw): return [ut.nx_makenode(graph, name + str(n), **attrkw) for n in range(num)] def add_edge2(u, v, *args, **kwargs): v = ut.ensure_iterable(v) u = ut.ensure_iterable(u) for _u, _v in ut.product(u, v): graph.add_edge(_u, _v, *args, **kwargs) ns = 512 # *** Primary color: p_shade2 = '#41629A' # *** Secondary color s1_shade2 = '#E88B53' # *** Secondary color s2_shade2 = '#36977F' # *** Complement color c_shade2 = '#E8B353' annot1 = ut.nx_makenode(graph, 'Unlabeled\nannotations\n(query)', width=ns, height=ns, groupid='annot', color=p_shade2) annot2 = ut.nx_makenode(graph, 'Labeled\nannotations\n(database)', width=ns, height=ns, groupid='annot', color=s1_shade2) occurprob = ut.nx_makenode(graph, 'Dense \nprobabilities', color=lighten_hex(p_shade2, .1)) cacheprob = ut.nx_makenode(graph, 'Cached \nprobabilities', color=lighten_hex(s1_shade2, .1)) sparseprob = ut.nx_makenode(graph, 'Sparse\nprobabilities', color=lighten_hex(c_shade2, .1)) graph.add_edge(annot1, occurprob) graph.add_edge(annot1, sparseprob) graph.add_edge(annot2, sparseprob) graph.add_edge(annot2, cacheprob) matchgraph = ut.nx_makenode(graph, 'Graph of\npotential matches', color=lighten_hex(s2_shade2, .1)) cutalgo = ut.nx_makenode(graph, 'Graph cut algorithm', color=lighten_hex(s2_shade2, .2), shape='ellipse') cc_names = ut.nx_makenode(graph, 'Identifications,\n splits, and merges are\nconnected compoments', color=lighten_hex(s2_shade2, .3)) graph.add_edge(occurprob, matchgraph) graph.add_edge(sparseprob, matchgraph) graph.add_edge(cacheprob, matchgraph) graph.add_edge(matchgraph, cutalgo) graph.add_edge(cutalgo, cc_names) ut.nx_set_default_node_attributes(graph, 'shape', 'rect') ut.nx_set_default_node_attributes(graph, 'style', 'filled,rounded') ut.nx_set_default_node_attributes(graph, 'fixedsize', 'true') ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns * (1 / ut.PHI)) ut.nx_set_default_node_attributes(graph, 'regular', False) layoutkw = { 'prog': 'dot', 'rankdir': 'LR', 'splines': 'line', 'sep': 100 / 72, 'nodesep': 300 / 72, 'ranksep': 300 / 72, } fontkw = dict(fontname='Ubuntu', fontweight='light', fontsize=14) pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, **fontkw) pt.zoom_factory()
def make_graph(infr, show=False): import networkx as nx import itertools cm_list = infr.cm_list unique_nids, prob_names = infr.make_prob_names() thresh = infr.choose_thresh() # Simply cut any edge with a weight less than a threshold qaid_list = [cm.qaid for cm in cm_list] postcut = prob_names > thresh qxs, nxs = np.where(postcut) if False: kw = dict(precision=2, max_line_width=140, suppress_small=True) print( ut.hz_str('prob_names = ', ut.array2string2((prob_names), **kw))) print( ut.hz_str('postcut = ', ut.array2string2((postcut).astype(np.int), **kw))) matching_qaids = ut.take(qaid_list, qxs) matched_nids = ut.take(unique_nids, nxs) qreq_ = infr.qreq_ nodes = ut.unique(qreq_.qaids.tolist() + qreq_.daids.tolist()) if not hasattr(qreq_, 'dnids'): qreq_.dnids = qreq_.ibs.get_annot_nids(qreq_.daids) qreq_.qnids = qreq_.ibs.get_annot_nids(qreq_.qaids) dnid2_daids = ut.group_items(qreq_.daids, qreq_.dnids) grouped_aids = dnid2_daids.values() matched_daids = ut.take(dnid2_daids, matched_nids) name_cliques = [ list(itertools.combinations(aids, 2)) for aids in grouped_aids ] aid_matches = [ list(ut.product([qaid], daids)) for qaid, daids in zip(matching_qaids, matched_daids) ] graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(ut.flatten(name_cliques)) graph.add_edges_from(ut.flatten(aid_matches)) #matchless_quries = ut.take(qaid_list, ut.index_complement(qxs, len(qaid_list))) name_nodes = [('nid', l) for l in qreq_.dnids] db_aid_nid_edges = list(zip(qreq_.daids, name_nodes)) #query_aid_nid_edges = list(zip(matching_qaids, [('nid', l) for l in matched_nids])) #G = nx.Graph() #G.add_nodes_from(matchless_quries) #G.add_edges_from(db_aid_nid_edges) #G.add_edges_from(query_aid_nid_edges) graph.add_edges_from(db_aid_nid_edges) if infr.user_feedback is not None: user_feedback = ut.map_dict_vals(np.array, infr.user_feedback) p_bg = 0.0 part1 = user_feedback['p_match'] * (1 - user_feedback['p_notcomp']) part2 = p_bg * user_feedback['p_notcomp'] p_same_list = part1 + part2 for aid1, aid2, p_same in zip(user_feedback['aid1'], user_feedback['aid2'], p_same_list): if p_same > .5: if not graph.has_edge(aid1, aid2): graph.add_edge(aid1, aid2) else: if graph.has_edge(aid1, aid2): graph.remove_edge(aid1, aid2) if show: import plottool as pt nx.set_node_attributes(graph, 'color', {aid: pt.LIGHT_PINK for aid in qreq_.daids}) nx.set_node_attributes(graph, 'color', {aid: pt.TRUE_BLUE for aid in qreq_.qaids}) nx.set_node_attributes( graph, 'color', { aid: pt.LIGHT_PURPLE for aid in np.intersect1d(qreq_.qaids, qreq_.daids) }) nx.set_node_attributes( graph, 'label', {node: 'n%r' % (node[1], ) for node in name_nodes}) nx.set_node_attributes( graph, 'color', {node: pt.LIGHT_GREEN for node in name_nodes}) if show: import plottool as pt pt.show_nx(graph, layoutkw={'prog': 'neato'}, verbose=False) return graph
def show_nx(self, labels=['value'], edge_labels=False, fnum=None): import plottool as pt graph = self.to_networkx(labels=labels, edge_labels=edge_labels) pt.show_nx(graph, fnum=fnum)
def draw_em_graph(P, Pn, PL, gam, num_labels): """ python -m ibeis.algo.hots.testem test_em --show --no-cnn """ num_labels = PL.shape[1] name_nodes = ['N%d' % x for x in list(range(1, num_labels + 1))] #annot_nodes = ut.chr_range(len(Pn), base='A') annot_nodes = ['X%d' % x for x in list(range(1, len(Pn) + 1))] # name_nodes = ut.chr_range(num_labels, base='A') nodes = name_nodes + annot_nodes PL2 = gam[:, num_labels:].T PL2 += .01 PL2 = PL2 / PL2.sum(axis=1)[:, None] # PL2 = PL2 / np.linalg.norm(PL2, axis=0) zero_part = np.zeros((num_labels, len(Pn) + num_labels)) prob_part = np.hstack([PL2, Pn]) print(ut.hz_str(' PL2 = ', ut.array_repr2(PL2, precision=2))) # Redo p with posteriors if ut.get_argflag('--postem'): P = np.vstack([zero_part, prob_part]) weight_matrix = P # NOQA graph = ut.nx_from_matrix(P, nodes=nodes) graph = graph.to_directed() # delete graph dup_edges = [] seen_ = set([]) for u, v in graph.edges(): if u < v: u, v = v, u if (u, v) not in seen_: seen_.add((u, v)) else: dup_edges.append((u, v)) graph.remove_edges_from(dup_edges) import plottool as pt import networkx as nx if len(name_nodes) == 3 and len(annot_nodes) == 4: graph.node[annot_nodes[0]]['pos'] = (20., 200.) graph.node[annot_nodes[1]]['pos'] = (220., 200.) graph.node[annot_nodes[2]]['pos'] = (20., 100.) graph.node[annot_nodes[3]]['pos'] = (220., 100.) graph.node[name_nodes[0]]['pos'] = (10., 300.) graph.node[name_nodes[1]]['pos'] = (120., 300.) graph.node[name_nodes[2]]['pos'] = (230., 300.) nx.set_node_attributes(graph, 'pin', 'true') print('annot_nodes = %r' % (annot_nodes,)) print('name_nodes = %r' % (name_nodes,)) for u in annot_nodes: for v in name_nodes: if graph.has_edge(u, v): print('1) u, v = %r' % ((u, v),)) graph.edge[u][v]['taillabel'] = graph.edge[u][v]['label'] graph.edge[u][v]['color'] = pt.ORANGE graph.edge[u][v]['labelcolor'] = pt.BLUE del graph.edge[u][v]['label'] elif graph.has_edge(v, u): print('2) u, v = %r' % ((u, v),)) graph.edge[v][u]['headlabel'] = graph.edge[v][u]['label'] graph.edge[v][u]['color'] = pt.ORANGE graph.edge[v][u]['labelcolor'] = pt.BLUE del graph.edge[v][u]['label'] else: print((u, v)) print('!!') # import itertools # name_const_edges = [(u, v, {'style': 'invis'}) for u, v in itertools.combinations(name_nodes, 2)] # graph.add_edges_from(name_const_edges) # nx.set_edge_attributes(graph, 'constraint', {edge: False for edge in graph.edges() if edge[0] == 'b' or edge[1] == 'b'}) # nx.set_edge_attributes(graph, 'constraint', {edge: False for edge in graph.edges() if edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, 'constraint', {edge: True for edge in graph.edges() if edge[0] in name_nodes or edge[1] in name_nodes}) # nx.set_edge_attributes(graph, 'constraint', {edge: True for edge in graph.edges() if (edge[0] in ['a', 'b'] and edge[1] in ['a', 'b']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, 'constraint', {edge: True for edge in graph.edges() if (edge[0] in ['c'] or edge[1] in ['c']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, 'constraint', {edge: True for edge in graph.edges() if (edge[0] in ['a'] or edge[1] in ['a']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, 'constraint', {edge: True for edge in graph.edges() if (edge[0] in ['b'] or edge[1] in ['b']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # graph.add_edges_from([('root', n) for n in nodes]) # {node: 'names' for node in name_nodes}) nx.set_node_attributes(graph, 'color', {node: pt.RED for node in name_nodes}) # nx.set_node_attributes(graph, 'width', {node: 20 for node in nodes}) # nx.set_node_attributes(graph, 'height', {node: 20 for node in nodes}) #nx.set_node_attributes(graph, 'group', {node: 'names' for node in name_nodes}) #nx.set_node_attributes(graph, 'group', {node: 'annots' for node in annot_nodes}) nx.set_node_attributes(graph, 'groupid', {node: 'names' for node in name_nodes}) nx.set_node_attributes(graph, 'groupid', {node: 'annots' for node in annot_nodes}) graph.graph['clusterrank'] = 'local' # graph.graph['groupattrs'] = { # 'names': {'rankdir': 'LR', 'rank': 'source'}, # 'annots': {'rankdir': 'TB', 'rank': 'source'}, # } ut.nx_delete_edge_attr(graph, 'weight') # pt.show_nx(graph, fontsize=10, layoutkw={'splines': 'spline', 'prog': 'dot', 'sep': 2.0}, verbose=1) layoutkw = { # 'rankdir': 'LR', 'splines': 'spline', # 'splines': 'ortho', # 'splines': 'curved', # 'compound': 'True', # 'prog': 'dot', 'prog': 'neato', # 'packMode': 'clust', # 'sep': 4, # 'nodesep': 1, # 'ranksep': 1, } #pt.show_nx(graph, fontsize=12, layoutkw=layoutkw, verbose=0, as_directed=False) pt.show_nx(graph, fontsize=6, fontname='Ubuntu', layoutkw=layoutkw, verbose=0, as_directed=False) pt.interactions.zoom_factory()
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)
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)
def draw_em_graph(P, Pn, PL, gam, num_labels): """ python -m ibeis.algo.hots.testem test_em --show --no-cnn """ num_labels = PL.shape[1] name_nodes = ['N%d' % x for x in list(range(1, num_labels + 1))] annot_nodes = ['X%d' % x for x in list(range(1, len(Pn) + 1))] nodes = name_nodes + annot_nodes PL2 = gam[:, num_labels:].T PL2 += .01 PL2 = PL2 / PL2.sum(axis=1)[:, None] # PL2 = PL2 / np.linalg.norm(PL2, axis=0) zero_part = np.zeros((num_labels, len(Pn) + num_labels)) prob_part = np.hstack([PL2, Pn]) print(ut.hz_str(' PL2 = ', ut.repr2(PL2, precision=2))) # Redo p with posteriors if ut.get_argflag('--postem'): P = np.vstack([zero_part, prob_part]) weight_matrix = P # NOQA graph = ut.nx_from_matrix(P, nodes=nodes) graph = graph.to_directed() # delete graph dup_edges = [] seen_ = set([]) for u, v in graph.edges(): if u < v: u, v = v, u if (u, v) not in seen_: seen_.add((u, v)) else: dup_edges.append((u, v)) graph.remove_edges_from(dup_edges) import plottool as pt import networkx as nx if len(name_nodes) == 3 and len(annot_nodes) == 4: graph.nodes[annot_nodes[0]]['pos'] = (20., 200.) graph.nodes[annot_nodes[1]]['pos'] = (220., 200.) graph.nodes[annot_nodes[2]]['pos'] = (20., 100.) graph.nodes[annot_nodes[3]]['pos'] = (220., 100.) graph.nodes[name_nodes[0]]['pos'] = (10., 300.) graph.nodes[name_nodes[1]]['pos'] = (120., 300.) graph.nodes[name_nodes[2]]['pos'] = (230., 300.) nx.set_node_attributes(graph, name='pin', values='true') print('annot_nodes = %r' % (annot_nodes,)) print('name_nodes = %r' % (name_nodes,)) for u in annot_nodes: for v in name_nodes: if graph.has_edge(u, v): print('1) u, v = %r' % ((u, v),)) graph.edge[u][v]['taillabel'] = graph.edge[u][v]['label'] graph.edge[u][v]['color'] = pt.ORANGE graph.edge[u][v]['labelcolor'] = pt.BLUE del graph.edge[u][v]['label'] elif graph.has_edge(v, u): print('2) u, v = %r' % ((u, v),)) graph.edge[v][u]['headlabel'] = graph.edge[v][u]['label'] graph.edge[v][u]['color'] = pt.ORANGE graph.edge[v][u]['labelcolor'] = pt.BLUE del graph.edge[v][u]['label'] else: print((u, v)) print('!!') # import itertools # name_const_edges = [(u, v, {'style': 'invis'}) for u, v in itertools.combinations(name_nodes, 2)] # graph.add_edges_from(name_const_edges) # nx.set_edge_attributes(graph, name='constraint', values={edge: False for edge in graph.edges() if edge[0] == 'b' or edge[1] == 'b'}) # nx.set_edge_attributes(graph, name='constraint', values={edge: False for edge in graph.edges() if edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, name='constraint', values={edge: True for edge in graph.edges() if edge[0] in name_nodes or edge[1] in name_nodes}) # nx.set_edge_attributes(graph, name='constraint', values={edge: True for edge in graph.edges() if (edge[0] in ['a', 'b'] and edge[1] in ['a', 'b']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, name='constraint', values={edge: True for edge in graph.edges() if (edge[0] in ['c'] or edge[1] in ['c']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, name='constraint', values={edge: True for edge in graph.edges() if (edge[0] in ['a'] or edge[1] in ['a']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # nx.set_edge_attributes(graph, name='constraint', values={edge: True for edge in graph.edges() if (edge[0] in ['b'] or edge[1] in ['b']) and edge[0] in annot_nodes and edge[1] in annot_nodes}) # graph.add_edges_from([('root', n) for n in nodes]) # {node: 'names' for node in name_nodes}) nx.set_node_attributes(graph, name='color', values={node: pt.RED for node in name_nodes}) # nx.set_node_attributes(graph, name='width', values={node: 20 for node in nodes}) # nx.set_node_attributes(graph, name='height', values={node: 20 for node in nodes}) #nx.set_node_attributes(graph, name='group', values={node: 'names' for node in name_nodes}) #nx.set_node_attributes(graph, name='group', values={node: 'annots' for node in annot_nodes}) nx.set_node_attributes(graph, name='groupid', values={node: 'names' for node in name_nodes}) nx.set_node_attributes(graph, name='groupid', values={node: 'annots' for node in annot_nodes}) graph.graph['clusterrank'] = 'local' # graph.graph['groupattrs'] = { # 'names': {'rankdir': 'LR', 'rank': 'source'}, # 'annots': {'rankdir': 'TB', 'rank': 'source'}, # } ut.nx_delete_edge_attr(graph, 'weight') # pt.show_nx(graph, fontsize=10, layoutkw={'splines': 'spline', 'prog': 'dot', 'sep': 2.0}, verbose=1) layoutkw = { # 'rankdir': 'LR', 'splines': 'spline', # 'splines': 'ortho', # 'splines': 'curved', # 'compound': 'True', # 'prog': 'dot', 'prog': 'neato', # 'packMode': 'clust', # 'sep': 4, # 'nodesep': 1, # 'ranksep': 1, } #pt.show_nx(graph, fontsize=12, layoutkw=layoutkw, verbose=0, as_directed=False) pt.show_nx(graph, fontsize=6, fontname='Ubuntu', layoutkw=layoutkw, verbose=0, as_directed=False) pt.interactions.zoom_factory()
def general_identify_flow(): r""" CommandLine: python -m ibeis.scripts.specialdraw general_identify_flow --show --save pairsim.png --dpi=100 --diskshow --clipwhite python -m ibeis.scripts.specialdraw general_identify_flow --dpi=200 --diskshow --clipwhite --dpath ~/latex/cand/ --figsize=20,10 --save figures4/pairprob.png --arrow-width=2.0 Example: >>> # SCRIPT >>> from ibeis.scripts.specialdraw import * # NOQA >>> general_identify_flow() >>> ut.quit_if_noshow() >>> ut.show_if_requested() """ import networkx as nx import plottool as pt pt.ensure_pylab_qt4() # pt.plt.xkcd() graph = nx.DiGraph() def makecluster(name, num, **attrkw): return [ut.nx_makenode(name + str(n), **attrkw) for n in range(num)] def add_edge2(u, v, *args, **kwargs): v = ut.ensure_iterable(v) u = ut.ensure_iterable(u) for _u, _v in ut.product(u, v): graph.add_edge(_u, _v, *args, **kwargs) # *** Primary color: p_shade2 = '#41629A' # *** Secondary color s1_shade2 = '#E88B53' # *** Secondary color s2_shade2 = '#36977F' # *** Complement color c_shade2 = '#E8B353' ns = 512 ut.inject_func_as_method(graph, ut.nx_makenode) annot1_color = p_shade2 annot2_color = s1_shade2 #annot1_color2 = pt.color_funcs.lighten_rgb(colors.hex2color(annot1_color), .01) annot1 = graph.nx_makenode('Annotation X', width=ns, height=ns, groupid='annot', color=annot1_color) annot2 = graph.nx_makenode('Annotation Y', width=ns, height=ns, groupid='annot', color=annot2_color) featX = graph.nx_makenode('Features X', size=(ns / 1.2, ns / 2), groupid='feats', color=lighten_hex(annot1_color, .1)) featY = graph.nx_makenode('Features Y', size=(ns / 1.2, ns / 2), groupid='feats', color=lighten_hex(annot2_color, .1)) #'#4771B3') global_pairvec = graph.nx_makenode('Global similarity\n(viewpoint, quality, ...)', width=ns * ut.PHI * 1.2, color=s2_shade2) findnn = graph.nx_makenode('Find correspondences\n(nearest neighbors)', shape='ellipse', color=c_shade2) local_pairvec = graph.nx_makenode('Local similarities\n(LNBNN, spatial error, ...)', size=(ns * 2.2, ns), color=lighten_hex(c_shade2, .1)) agglocal = graph.nx_makenode('Aggregate', size=(ns / 1.1, ns / 2), shape='ellipse', color=lighten_hex(c_shade2, .2)) catvecs = graph.nx_makenode('Concatenate', size=(ns / 1.1, ns / 2), shape='ellipse', color=lighten_hex(s2_shade2, .1)) pairvec = graph.nx_makenode('Vector of\npairwise similarities', color=lighten_hex(s2_shade2, .2)) classifier = graph.nx_makenode('Classifier\n(SVM/RF/DNN)', color=lighten_hex(s2_shade2, .3)) prob = graph.nx_makenode('Matching Probability\n(same individual given\nsimilar viewpoint)', color=lighten_hex(s2_shade2, .4)) graph.add_edge(annot1, global_pairvec) graph.add_edge(annot2, global_pairvec) add_edge2(annot1, featX) add_edge2(annot2, featY) add_edge2(featX, findnn) add_edge2(featY, findnn) add_edge2(findnn, local_pairvec) graph.add_edge(local_pairvec, agglocal, constraint=True) graph.add_edge(agglocal, catvecs, constraint=False) graph.add_edge(global_pairvec, catvecs) graph.add_edge(catvecs, pairvec) # graph.add_edge(annot1, classifier, style='invis') # graph.add_edge(pairvec, classifier , constraint=False) graph.add_edge(pairvec, classifier) graph.add_edge(classifier, prob) ut.nx_set_default_node_attributes(graph, 'shape', 'rect') #ut.nx_set_default_node_attributes(graph, 'fillcolor', nx.get_node_attributes(graph, 'color')) #ut.nx_set_default_node_attributes(graph, 'style', 'rounded') ut.nx_set_default_node_attributes(graph, 'style', 'filled,rounded') ut.nx_set_default_node_attributes(graph, 'fixedsize', 'true') ut.nx_set_default_node_attributes(graph, 'xlabel', nx.get_node_attributes(graph, 'label')) ut.nx_set_default_node_attributes(graph, 'width', ns * ut.PHI) ut.nx_set_default_node_attributes(graph, 'height', ns) ut.nx_set_default_node_attributes(graph, 'regular', False) #font = 'MonoDyslexic' #font = 'Mono_Dyslexic' font = 'Ubuntu' ut.nx_set_default_node_attributes(graph, 'fontsize', 72) ut.nx_set_default_node_attributes(graph, 'fontname', font) #ut.nx_delete_node_attr(graph, 'width') #ut.nx_delete_node_attr(graph, 'height') #ut.nx_delete_node_attr(graph, 'fixedsize') #ut.nx_delete_node_attr(graph, 'style') #ut.nx_delete_node_attr(graph, 'regular') #ut.nx_delete_node_attr(graph, 'shape') #graph.node[annot1]['label'] = "<f0> left|<f1> mid\ dle|<f2> right" #graph.node[annot2]['label'] = ut.codeblock( # ''' # <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> # <TR><TD>left</TD><TD PORT="f1">mid dle</TD><TD PORT="f2">right</TD></TR> # </TABLE>> # ''') #graph.node[annot1]['label'] = ut.codeblock( # ''' # <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> # <TR><TD>left</TD><TD PORT="f1">mid dle</TD><TD PORT="f2">right</TD></TR> # </TABLE>> # ''') #graph.node[annot1]['shape'] = 'none' #graph.node[annot1]['margin'] = '0' layoutkw = { 'forcelabels': True, 'prog': 'dot', 'rankdir': 'LR', # 'splines': 'curved', 'splines': 'line', 'samplepoints': 20, 'showboxes': 1, # 'splines': 'polyline', #'splines': 'spline', 'sep': 100 / 72, 'nodesep': 300 / 72, 'ranksep': 300 / 72, #'inputscale': 72, # 'inputscale': 1, # 'dpi': 72, # 'concentrate': 'true', # merges edge lines # 'splines': 'ortho', # 'aspect': 1, # 'ratio': 'compress', # 'size': '5,4000', # 'rank': 'max', } #fontkw = dict(fontfamilty='sans-serif', fontweight='normal', fontsize=12) #fontkw = dict(fontname='Ubuntu', fontweight='normal', fontsize=12) #fontkw = dict(fontname='Ubuntu', fontweight='light', fontsize=20) fontkw = dict(fontname=font, fontweight='light', fontsize=12) #prop = fm.FontProperties(fname='/usr/share/fonts/truetype/groovygh.ttf') pt.show_nx(graph, layout='agraph', layoutkw=layoutkw, **fontkw) pt.zoom_factory()