def coco_stats(): """ http://stattrek.com/online-calculator/hypergeometric.aspx CommandLine: python -m mtgmonte.stats --exec-coco_stats --show Example: >>> # DISABLE_DOCTEST >>> from mtgmonte.stats import * # NOQA >>> result = coco_stats() >>> print(result) >>> ut.show_if_requested() """ import plottool as pt from scipy.stats import hypergeom N = pop_size = 60 # cards in deck # NOQA K = num_success = 21 # number of creatures in deck # NOQA n = sample_size = 6 # cards seen by coco # NOQA # prob of at least that many hits hypergeom prb = hypergeom(N, K, n) k = number_of_success = 1 # number of hits you want # NOQA prb.pmf(k) # P(X = k) # prb.cdf(k) # P(X <= k) 1 - prb.cdf(k) # P(X > k) (1 - prb.cdf(k)) + prb.pmf(k) # P(X >= k) def prob_ge(k, prb=prb): return (1 - prb.cdf(k)) + prb.pmf(k) # P(X >= k) pt.ensure_pylab_qt4() import numpy as np k = np.arange(1, 3) K_list = np.arange(15, 30) label_list = [str(K_) + " creatures in deck" for K_ in K_list] ydata_list = [prob_ge(k, prb=hypergeom(N, K_, n)) for K_ in K_list] pt.multi_plot( k, ydata_list, label_list=label_list, title="probability of at least k hits with coco", xlabel="k", ylabel="prob", num_xticks=len(k), use_darkbackground=True, )
def land_stats(): """ http://stattrek.com/online-calculator/hypergeometric.aspx CommandLine: python -m mtgmonte.stats --exec-land_stats --show Example: >>> # DISABLE_DOCTEST >>> from mtgmonte.stats import * # NOQA >>> result = land_stats() >>> print(result) >>> ut.show_if_requested() """ import plottool as pt from scipy.stats import hypergeom N = pop_size = 60 # cards in deck # NOQA # K = num_success = 25 # lands in deck # NOQA n = sample_size = 6 # cards seen by coco # NOQA # prob of at least that many hits def prob_ge(k, prb): return (1 - prb.cdf(k)) + prb.pmf(k) # P(X >= k) pt.ensure_pylab_qt4() N = deck_size = 60 # NOQA land_range = (24, 27 + 1) # N = deck_size = 40 # NOQA # land_range = (15, 18 + 1) xdata = range(0, 15) # turn ydata_list = [[hypergeom(N, K, x + 7).expect() for x in xdata] for K in range(*land_range)] spread_list = [[hypergeom(N, K, x + 7).std() for x in xdata] for K in range(*land_range)] # spread_list = None import numpy as np label_list = ["%d lands" % (K,) for K in range(*land_range)] pt.multi_plot( xdata, ydata_list, spread_list=spread_list, label_list=label_list, num_xticks=15, num_yticks=13, fnum=1 ) min_lands_acceptable = np.minimum(np.array(xdata), [1, 2, 3, 4, 5, 6] + [6] * (len(xdata) - 6)) pt.multi_plot( xdata, [min_lands_acceptable, (np.array(xdata) ** 0.9) * 0.5 + 4], label_list=["minimum ok", "maximum ok"], num_xticks=15, num_yticks=13, fnum=1, marker="o", )
def show_toy_distributions(toy_params): import vtool as vt import plottool as pt pt.ensure_pylab_qt4() xdata = np.linspace(0, 8, 1000) tp_pdf = vt.gauss_func1d(xdata, **toy_params[True]) fp_pdf = vt.gauss_func1d(xdata, **toy_params[False]) pt.plot_probabilities([tp_pdf, fp_pdf], ['TP', 'TF'], prob_colors=[pt.TRUE_BLUE, pt.FALSE_RED], xdata=xdata, figtitle='Toy Distributions')
def show_toy_distributions(toy_params): import vtool as vt import plottool as pt pt.ensure_pylab_qt4() xdata = np.linspace(0, 8, 1000) tp_pdf = vt.gauss_func1d(xdata, **toy_params[True]) fp_pdf = vt.gauss_func1d(xdata, **toy_params[False]) pt.plot_probabilities( [tp_pdf, fp_pdf], ['TP', 'TF'], prob_colors=[pt.TRUE_BLUE, pt.FALSE_RED], xdata=xdata, figtitle='Toy Distributions')
def multidb_montage(): r""" CommandLine: python -m ibeis.scripts.specialdraw multidb_montage --save montage.jpg --dpath ~/slides --diskshow --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> multidb_montage() """ import ibeis import plottool as pt import vtool as vt import numpy as np pt.ensure_pylab_qt4() ibs1 = ibeis.opendb('PZ_MTEST') ibs2 = ibeis.opendb('GZ_ALL') ibs3 = ibeis.opendb('GIRM_Master1') chip_lists = [] aids_list = [] for ibs in [ibs1, ibs2, ibs3]: aids = ibs.sample_annots_general(minqual='good', sample_size=400) aids_list.append(aids) print(ut.depth_profile(aids_list)) for ibs, aids in zip([ibs1, ibs2, ibs3], aids_list): chips = ibs.get_annot_chips(aids) chip_lists.append(chips) chip_list = ut.flatten(chip_lists) np.random.shuffle(chip_list) widescreen_ratio = 16 / 9 ratio = ut.PHI ratio = widescreen_ratio fpath = pt.get_save_directions() #height = 6000 width = 6000 #width = int(height * ratio) height = int(width / ratio) dsize = (width, height) dst = vt.montage(chip_list, dsize) vt.imwrite(fpath, dst) if ut.get_argflag('--show'): pt.imshow(dst)
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 show_top_featmatches(qreq_, cm_list): """ Args: qreq_ (ibeis.QueryRequest): query request object with hyper-parameters cm_list (list): SeeAlso: python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:lnbnn_on=True,lnbnn_normalizer=normlnbnn-test -a default --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_Master1 -t best:lnbnn_on=True -a timectrl --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:lnbnn_on=True -a default:size=30 --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:K=1,Knorm=5,lnbnn_on=True -a default:size=30 --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:K=1,Knorm=3,lnbnn_on=True -a default --sephack CommandLine: python -m ibeis.viz.viz_nearest_descriptors --exec-show_top_featmatches --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> cm_list, qreq_ = ibeis.testdata_cmlist(defaultdb='PZ_MTEST', >>> a=['default:has_none=mother,size=30']) >>> show_top_featmatches(qreq_, cm_list) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ # for cm in cm_list: # cm.score_csum(qreq_) import numpy as np import vtool as vt from functools import partial # Stack chipmatches ibs = qreq_.ibs infos = [cm.get_flat_fm_info() for cm in cm_list] flat_metadata = dict([(k, np.concatenate(v)) for k, v in ut.dict_stack2(infos).items()]) fsv_flat = flat_metadata['fsv'] flat_metadata['fs'] = fsv_flat.prod(axis=1) aids1 = flat_metadata['aid1'][:, None] aids2 = flat_metadata['aid2'][:, None] flat_metadata['aid_pairs'] = np.concatenate([aids1, aids2], axis=1) # Take sample of metadata sortx = flat_metadata['fs'].argsort()[::-1] num = len(cm_list) * 3 # num = 10 taker = partial(np.take, indices=sortx[:num], axis=0) flat_metadata_top = ut.map_dict_vals(taker, flat_metadata) aid1s, aid2s, fms = ut.dict_take(flat_metadata_top, ['aid1', 'aid2', 'fm']) annots = {} aids = np.unique(np.hstack((aid1s, aid2s))) annots = {aid: ibs.get_annot_lazy_dict(aid, config2_=qreq_.qparams) for aid in aids} label_lists = ibs.get_aidpair_truths(aid1s, aid2s) == ibs.const.TRUTH_MATCH patch_size = 64 def extract_patches(annots, aid, fxs): """ custom_func(lazydict, key, subkeys) for multigroup_lookup """ annot = annots[aid] kpts = annot['kpts'] rchip = annot['rchip'] kpts_m = kpts.take(fxs, axis=0) warped_patches, warped_subkpts = vt.get_warped_patches(rchip, kpts_m, patch_size=patch_size) return warped_patches data_lists = vt.multigroup_lookup(annots, [aid1s, aid2s], fms.T, extract_patches) import plottool as pt pt.ensure_pylab_qt4() import ibeis_cnn inter = ibeis_cnn.draw_results.interact_patches( label_lists, data_lists, flat_metadata_top, chunck_sizes=(2, 4), ibs=ibs, hack_one_per_aid=False, sortby='fs', qreq_=qreq_) inter.show()
def segmentation_example(): import vigra import opengm import sklearn import sklearn.mixture import numpy as np from vigra import graphs import matplotlib as mpl import plottool as pt pt.ensure_pylab_qt4() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) - 1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) # Node Features nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) nCluster = 5 g = sklearn.mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:, :]) clusterProb = g.predict_proba(nodeFeatures) # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph( clusterProb.astype(np.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") ndim_data = clusterProbImg.reshape((-1, nCluster)) pca = sklearn.decomposition.PCA(n_components=3) print(ndim_data.shape) pca.fit(ndim_data) print(ut.repr2(pca.explained_variance_ratio_, precision=2)) oldshape = (clusterProbImg.shape[0:2] + (-1,)) clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape) print(clusterProgImg3.shape) # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster) # convert probabilites to energies probs = np.clip(clusterProb, 0.00001, 0.99999) costs = -1.0 * np.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids, np.arange(rag.nodeNum)) # add a potts function beta = 40.0 # strength of potts regularizer regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = np.sort(uvIds, axis=1) # add all second order factors at once gm.addFactors(fid, uvIds) # get super-pixels with slic on LAB image Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self,): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector),)) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) pt.imshow(clusterProgImg3.swapaxes(0, 1)) # plot superpixels cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3)) pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap) pt.imshow(nodeFeaturesImgRgb) cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3)) for arg in callback.labels: arg = vigra.taggedView(arg, "n") argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32)) argImg = vigra.taggedView(argImg, "xy") # plot superpixels pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
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 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 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 merge_viewpoint_graph(): r""" CommandLine: python -m ibeis.scripts.specialdraw merge_viewpoint_graph --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = merge_viewpoint_graph() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import plottool as pt import ibeis import networkx as nx defaultdb = 'PZ_Master1' ibs = ibeis.opendb(defaultdb=defaultdb) #nids = None aids = ibs.get_name_aids(4875) ibs.print_annot_stats(aids) left_aids = ibs.filter_annots_general(aids, view='left')[0:3] right_aids = ibs.filter_annots_general(aids, view='right') right_aids = list(set(right_aids) - {14517})[0:3] back = ibs.filter_annots_general(aids, view='back')[0:4] backleft = ibs.filter_annots_general(aids, view='backleft')[0:4] backright = ibs.filter_annots_general(aids, view='backright')[0:4] right_graph = nx.DiGraph(ut.upper_diag_self_prodx(right_aids)) left_graph = nx.DiGraph(ut.upper_diag_self_prodx(left_aids)) back_edges = [ tuple([back[0], backright[0]][::1]), tuple([back[0], backleft[0]][::1]), ] back_graph = nx.DiGraph(back_edges) # Let the graph be a bit smaller right_graph.edge[right_aids[1]][right_aids[2]]['constraint'] = ut.get_argflag('--constraint') left_graph.edge[left_aids[1]][left_aids[2]]['constraint'] = ut.get_argflag('--constraint') #right_graph = right_graph.to_undirected().to_directed() #left_graph = left_graph.to_undirected().to_directed() nx.set_node_attributes(right_graph, 'groupid', 'right') nx.set_node_attributes(left_graph, 'groupid', 'left') #nx.set_node_attributes(right_graph, 'scale', .2) #nx.set_node_attributes(left_graph, 'scale', .2) #back_graph.node[back[0]]['scale'] = 2.3 nx.set_node_attributes(back_graph, 'groupid', 'back') view_graph = nx.compose_all([left_graph, back_graph, right_graph]) view_graph.add_edges_from([ [backright[0], right_aids[0]][::-1], [backleft[0], left_aids[0]][::-1], ]) pt.ensure_pylab_qt4() graph = graph = view_graph # NOQA #graph = graph.to_undirected() nx.set_edge_attributes(graph, 'color', pt.DARK_ORANGE[0:3]) #nx.set_edge_attributes(graph, 'color', pt.BLACK) nx.set_edge_attributes(graph, 'color', {edge: pt.LIGHT_BLUE[0:3] for edge in back_edges}) #pt.close_all_figures(); from ibeis.viz import viz_graph layoutkw = { 'nodesep': 1, } viz_graph.viz_netx_chipgraph(ibs, graph, with_images=1, prog='dot', augment_graph=False, layoutkw=layoutkw) if False: """ #view_graph = left_graph pt.close_all_figures(); viz_netx_chipgraph(ibs, view_graph, with_images=0, prog='neato') #viz_netx_chipgraph(ibs, view_graph, layout='pydot', with_images=False) #back_graph = make_name_graph_interaction(ibs, aids=back, with_all=False) aids = left_aids + back + backleft + backright + right_aids for aid, chip in zip(aids, ibs.get_annot_chips(aids)): fpath = ut.truepath('~/slides/merge/aid_%d.jpg' % (aid,)) vt.imwrite(fpath, vt.resize_to_maxdims(chip, (400, 400))) ut.copy_files_to(, ) aids = ibs.filterannots_by_tags(ibs.get_valid_aids(), dict(has_any_annotmatch='splitcase')) aid1 = ibs.group_annots_by_name_dict(aids)[252] aid2 = ibs.group_annots_by_name_dict(aids)[6791] aids1 = ibs.get_annot_groundtruth(aid1)[0][0:4] aids2 = ibs.get_annot_groundtruth(aid2)[0] make_name_graph_interaction(ibs, aids=aids1 + aids2, with_all=False) ut.ensuredir(ut.truthpath('~/slides/split/)) for aid, chip in zip(aids, ibs.get_annot_chips(aids)): fpath = ut.truepath('~/slides/merge/aidA_%d.jpg' % (aid,)) vt.imwrite(fpath, vt.resize_to_maxdims(chip, (400, 400))) """ pass
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 event_space(): """ pip install matplotlib-venn """ from matplotlib import pyplot as plt # import numpy as np from matplotlib_venn import venn3, venn2, venn3_circles plt.figure(figsize=(4, 4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels=('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.show() same = set(['comparable', 'incomparable', 'same']) diff = set(['comparable', 'incomparable', 'diff']) # comparable = set(['comparable', 'same', 'diff']) # incomparable = set(['incomparable', 'same', 'diff']) subsets = [same, diff] # , comparable, incomparable] set_labels = ('same', 'diff') # , 'comparable', 'incomparable') venn3(subsets=subsets, set_labels=set_labels) plt.show() import plottool as pt pt.ensure_pylab_qt4() from matplotlib_subsets import treesets_rectangles tree = ( (120, 'Same', None), [ ((50, 'comparable', None), []), ((50, 'incomparable', None), []) ] (120, 'Diff', None), [ ((50, 'comparable', None), []), ((50, 'incomparable', None), []) ] ) treesets_rectangles(tree) plt.show() from matplotlib import pyplot as plt from matplotlib_venn import venn2, venn2_circles # NOQA # Subset sizes s = ( 2, # Ab 3, # aB 1, # AB ) v = venn2(subsets=s, set_labels=('A', 'B')) # Subset labels v.get_label_by_id('10').set_text('A but not B') v.get_label_by_id('01').set_text('B but not A') v.get_label_by_id('11').set_text('A and B') # Subset colors v.get_patch_by_id('10').set_color('c') v.get_patch_by_id('01').set_color('#993333') v.get_patch_by_id('11').set_color('blue') # Subset alphas v.get_patch_by_id('10').set_alpha(0.4) v.get_patch_by_id('01').set_alpha(1.0) v.get_patch_by_id('11').set_alpha(0.7) # Border styles c = venn2_circles(subsets=s, linestyle='solid') c[0].set_ls('dashed') # Line style c[0].set_lw(2.0) # Line width plt.show()
def limited_power_toughness_histogram(): r""" CommandLine: python -m mtgmonte.stats --exec-limited_power_toughness_histogram --show Example: >>> # DISABLE_DOCTEST >>> from mtgmonte.stats import * # NOQA >>> result = limited_power_toughness_histogram() >>> print(result) >>> ut.show_if_requested() """ from mtgmonte import mtgobjs from mtglib.gatherer_request import SearchRequest from mtglib.card_extractor import CardExtractor # from mtglib.card_renderer import CardList request = SearchRequest({"set": "Oath of the Gatewatch"}) def add_page(url, page): parts = url.split("/") part1 = "/".join(parts[:-1]) part2 = "/Default.aspx?page=%d&" % (page,) part3 = parts[-1].replace("Default.aspx?", "") url2 = part1 + part2 + part3 return url2 card_list = [] for page in range(0, 10): url = request.url url2 = add_page(url, page) extract = CardExtractor(url2) card_list0 = extract.cards for card in card_list0: card2 = mtgobjs.Card2() card2.__dict__.update(card.__dict__) card_list.append(card2) if len(card_list0) != 100: break for c in card_list: c.nice_attrs += ["rarity"] creats = [_card2 for _card2 in card_list if "Creature" in card2.types] creats = [_card2 for _card2 in creats if _card2.rarity in ["Common", "Uncommon"]] powtough = [] for c in creats: try: powtough.append((int(c.power), int(c.toughness))) except ValueError: pass import plottool as pt pt.ensure_pylab_qt4() import numpy as np scores_list = np.array(list(zip(*powtough))) xdata = np.arange(0, np.max(scores_list) + 1) powhist = np.histogram(scores_list[0], bins=xdata)[0] toughist = np.histogram(scores_list[1], bins=xdata)[0] pt.multi_plot(xdata, [powhist, toughist], label_list=["power", "toughness"], kind="bar") bothhist = ut.dict_hist(powtough) xdata = np.arange(len(bothhist)) dat = sorted(bothhist.items()) xticklabels = ut.take_column(dat, 0) ydata = ut.take_column(dat, 1) pt.multi_plot(xdata, [ydata], xticklabels=xticklabels, kind="bar")
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 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 merge_viewpoint_graph(): r""" CommandLine: python -m ibeis.scripts.specialdraw merge_viewpoint_graph --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.scripts.specialdraw import * # NOQA >>> result = merge_viewpoint_graph() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import plottool as pt import ibeis import networkx as nx defaultdb = 'PZ_Master1' ibs = ibeis.opendb(defaultdb=defaultdb) #nids = None aids = ibs.get_name_aids(4875) ibs.print_annot_stats(aids) left_aids = ibs.filter_annots_general(aids, view='left')[0:3] right_aids = ibs.filter_annots_general(aids, view='right') right_aids = list(set(right_aids) - {14517})[0:3] back = ibs.filter_annots_general(aids, view='back')[0:4] backleft = ibs.filter_annots_general(aids, view='backleft')[0:4] backright = ibs.filter_annots_general(aids, view='backright')[0:4] right_graph = nx.DiGraph(ut.upper_diag_self_prodx(right_aids)) left_graph = nx.DiGraph(ut.upper_diag_self_prodx(left_aids)) back_edges = [ tuple([back[0], backright[0]][::1]), tuple([back[0], backleft[0]][::1]), ] back_graph = nx.DiGraph(back_edges) # Let the graph be a bit smaller right_graph.edge[right_aids[1]][ right_aids[2]]['constraint'] = ut.get_argflag('--constraint') left_graph.edge[left_aids[1]][left_aids[2]]['constraint'] = ut.get_argflag( '--constraint') #right_graph = right_graph.to_undirected().to_directed() #left_graph = left_graph.to_undirected().to_directed() nx.set_node_attributes(right_graph, 'groupid', 'right') nx.set_node_attributes(left_graph, 'groupid', 'left') #nx.set_node_attributes(right_graph, 'scale', .2) #nx.set_node_attributes(left_graph, 'scale', .2) #back_graph.node[back[0]]['scale'] = 2.3 nx.set_node_attributes(back_graph, 'groupid', 'back') view_graph = nx.compose_all([left_graph, back_graph, right_graph]) view_graph.add_edges_from([ [backright[0], right_aids[0]][::-1], [backleft[0], left_aids[0]][::-1], ]) pt.ensure_pylab_qt4() graph = graph = view_graph # NOQA #graph = graph.to_undirected() nx.set_edge_attributes(graph, 'color', pt.DARK_ORANGE[0:3]) #nx.set_edge_attributes(graph, 'color', pt.BLACK) nx.set_edge_attributes(graph, 'color', {edge: pt.LIGHT_BLUE[0:3] for edge in back_edges}) #pt.close_all_figures(); from ibeis.viz import viz_graph layoutkw = { 'nodesep': 1, } viz_graph.viz_netx_chipgraph(ibs, graph, with_images=1, prog='dot', augment_graph=False, layoutkw=layoutkw) if False: """ #view_graph = left_graph pt.close_all_figures(); viz_netx_chipgraph(ibs, view_graph, with_images=0, prog='neato') #viz_netx_chipgraph(ibs, view_graph, layout='pydot', with_images=False) #back_graph = make_name_graph_interaction(ibs, aids=back, with_all=False) aids = left_aids + back + backleft + backright + right_aids for aid, chip in zip(aids, ibs.get_annot_chips(aids)): fpath = ut.truepath('~/slides/merge/aid_%d.jpg' % (aid,)) vt.imwrite(fpath, vt.resize_to_maxdims(chip, (400, 400))) ut.copy_files_to(, ) aids = ibs.filterannots_by_tags(ibs.get_valid_aids(), dict(has_any_annotmatch='splitcase')) aid1 = ibs.group_annots_by_name_dict(aids)[252] aid2 = ibs.group_annots_by_name_dict(aids)[6791] aids1 = ibs.get_annot_groundtruth(aid1)[0][0:4] aids2 = ibs.get_annot_groundtruth(aid2)[0] make_name_graph_interaction(ibs, aids=aids1 + aids2, with_all=False) ut.ensuredir(ut.truthpath('~/slides/split/)) for aid, chip in zip(aids, ibs.get_annot_chips(aids)): fpath = ut.truepath('~/slides/merge/aidA_%d.jpg' % (aid,)) vt.imwrite(fpath, vt.resize_to_maxdims(chip, (400, 400))) """ pass
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 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 show_top_featmatches(qreq_, cm_list): """ Args: qreq_ (ibeis.QueryRequest): query request object with hyper-parameters cm_list (list): SeeAlso: python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:lnbnn_on=True,lnbnn_normalizer=normlnbnn-test -a default --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_Master1 -t best:lnbnn_on=True -a timectrl --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:lnbnn_on=True -a default:size=30 --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:K=1,Knorm=5,lnbnn_on=True -a default:size=30 --sephack python -m ibeis --tf TestResult.draw_feat_scoresep --show --db PZ_MTEST -t best:K=1,Knorm=3,lnbnn_on=True -a default --sephack CommandLine: python -m ibeis.viz.viz_nearest_descriptors --exec-show_top_featmatches --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> cm_list, qreq_ = ibeis.testdata_cmlist(defaultdb='PZ_MTEST', >>> a=['default:has_none=mother,size=30']) >>> show_top_featmatches(qreq_, cm_list) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ # for cm in cm_list: # cm.score_csum(qreq_) import numpy as np import vtool as vt from functools import partial # Stack chipmatches ibs = qreq_.ibs infos = [cm.get_flat_fm_info() for cm in cm_list] flat_metadata = dict([(k, np.concatenate(v)) for k, v in ut.dict_stack2(infos).items()]) fsv_flat = flat_metadata['fsv'] flat_metadata['fs'] = fsv_flat.prod(axis=1) aids1 = flat_metadata['aid1'][:, None] aids2 = flat_metadata['aid2'][:, None] flat_metadata['aid_pairs'] = np.concatenate([aids1, aids2], axis=1) # Take sample of metadata sortx = flat_metadata['fs'].argsort()[::-1] num = len(cm_list) * 3 # num = 10 taker = partial(np.take, indices=sortx[:num], axis=0) flat_metadata_top = ut.map_dict_vals(taker, flat_metadata) aid1s, aid2s, fms = ut.dict_take(flat_metadata_top, ['aid1', 'aid2', 'fm']) annots = {} aids = np.unique(np.hstack((aid1s, aid2s))) annots = { aid: ibs.get_annot_lazy_dict(aid, config2_=qreq_.qparams) for aid in aids } label_lists = ibs.get_aidpair_truths(aid1s, aid2s) == ibs.const.TRUTH_MATCH patch_size = 64 def extract_patches(annots, aid, fxs): """ custom_func(lazydict, key, subkeys) for multigroup_lookup """ annot = annots[aid] kpts = annot['kpts'] rchip = annot['rchip'] kpts_m = kpts.take(fxs, axis=0) warped_patches, warped_subkpts = vt.get_warped_patches( rchip, kpts_m, patch_size=patch_size) return warped_patches data_lists = vt.multigroup_lookup(annots, [aid1s, aid2s], fms.T, extract_patches) import plottool as pt # NOQA pt.ensure_pylab_qt4() import ibeis_cnn inter = ibeis_cnn.draw_results.interact_patches(label_lists, data_lists, flat_metadata_top, chunck_sizes=(2, 4), ibs=ibs, hack_one_per_aid=False, sortby='fs', qreq_=qreq_) inter.show()
def chip_tester(): import plottool as pt pt.ensure_pylab_qt4() from ibeis.core_annots import * # NOQA import ibeis defaultdb = 'GZ_ALL' ibs = ibeis.opendb(defaultdb=defaultdb) aid_list = ibs.get_valid_aids() depc = ibs.depc_annot chips_orig = depc.get_property('chips', aid_list, 'img', config={}) chips_aeq = depc.get_property('chips', aid_list, 'img', config={'adapteq': True}) chips_heq = depc.get_property('chips', aid_list, 'img', config={'histeq': True}) import pyhesaff nkpts_list = np.array(list(ut.generate(pyhesaff.detect_num_kpts_in_image, chips_orig, force_serial=ibs.force_serial))) nkpts_list = np.array(nkpts_list) nfeats_orig = np.array(ibs.depc_annot.get('feat', aid_list, 'num_feats')) nfeats_hteq = np.array(ibs.depc_annot.get('feat', aid_list, 'num_feats', config={'histeq': True})) nfeats_ateq = np.array(ibs.depc_annot.get('feat', aid_list, 'num_feats', config={'adapteq': True})) sortx = np.array(nfeats_orig).argsort() sortx = np.array(nfeats_hteq).argsort() sortx = np.array(nfeats_ateq).argsort() aids = ut.take(aid_list, sortx) chips = chips_orig chips_bad = ut.take(chips, sortx) chips_good = ut.take(chips, sortx[::-1]) import ibeis.viz.interact.interact_chip ibeis.viz.interact.interact_chip.interact_multichips(ibs, aids) iteract_obj = pt.interact_multi_image.MultiImageInteraction(chips_bad, nPerPage=15) iteract_obj.start() iteract_obj = pt.interact_multi_image.MultiImageInteraction(chips_good, nPerPage=15) iteract_obj.start() x = sklearn.cluster.KMeans(2) x.fit(np.nan_to_num(measures)) import vtool.quality_classifier from vtool.quality_classifier import contrast_measures chips128 = depc.get_property('chips', aid_list, 'img', config={'dim_size': 256}) gray_chips = [vt.convert_colorspace(x, 'GRAY') for x in ut.ProgIter(chips128)] measures = list(ut.generate(contrast_measures, gray_chips, force_serial=ibs.force_serial)) measures = np.array(measures) measures = np.nan_to_num(measures) y = measures.T[3] sortx = y.argsort() ys = y.take(sortx) pca = sklearn.decomposition.PCA(1) pca.fit(measures) pca_measure = pca.transform(measures) nfeats_white = (nfeats_orig - nfeats_orig.mean()) / nfeats_orig.std() pca_white = (pca_measure - pca_measure.mean()) / pca_measure.std() sortx = nfeats_white.argsort() pt.plt.plot(pca_white[sortx], 'x') pt.plt.plot(nfeats_white[sortx], '.') pyhesaff.detect_feats_in_image svc = sklearn.svm.LinearSVC() svc.fit(measures, nfeats_orig > 500) svc.predict(measures) == (nfeats_orig > 500) svr = sklearn.svm.LinearSVR() svr.fit(measures, nfeats_orig) svr.predict(measures) depc['feat'].get_config_history(z1) depc['feat'].get_config_history(z2) pt.plt.plot(nfeats_hteq[sortx], 'x') pt.plt.plot(nfeats_orig[sortx], '.') pt.plt.plot(nfeats_ateq[sortx], 'o') z1 = ibs.depc_annot.get_rowids('feat', aid_list, config={'histeq': True}) z2 = ibs.depc_annot.get_rowids('feat', aid_list) assert len(set(z1).intersection(z2)) == 0
def chip_tester(): import plottool as pt pt.ensure_pylab_qt4() from ibeis.core_annots import * # NOQA import ibeis defaultdb = 'GZ_ALL' ibs = ibeis.opendb(defaultdb=defaultdb) aid_list = ibs.get_valid_aids() depc = ibs.depc_annot chips_orig = depc.get_property('chips', aid_list, 'img', config={}) chips_aeq = depc.get_property('chips', aid_list, 'img', config={'adapteq': True}) chips_heq = depc.get_property('chips', aid_list, 'img', config={'histeq': True}) import pyhesaff nkpts_list = np.array( list( ut.generate(pyhesaff.detect_num_kpts_in_image, chips_orig, force_serial=ibs.force_serial))) nkpts_list = np.array(nkpts_list) nfeats_orig = np.array(ibs.depc_annot.get('feat', aid_list, 'num_feats')) nfeats_hteq = np.array( ibs.depc_annot.get('feat', aid_list, 'num_feats', config={'histeq': True})) nfeats_ateq = np.array( ibs.depc_annot.get('feat', aid_list, 'num_feats', config={'adapteq': True})) sortx = np.array(nfeats_orig).argsort() sortx = np.array(nfeats_hteq).argsort() sortx = np.array(nfeats_ateq).argsort() aids = ut.take(aid_list, sortx) chips = chips_orig chips_bad = ut.take(chips, sortx) chips_good = ut.take(chips, sortx[::-1]) import ibeis.viz.interact.interact_chip ibeis.viz.interact.interact_chip.interact_multichips(ibs, aids) iteract_obj = pt.interact_multi_image.MultiImageInteraction(chips_bad, nPerPage=15) iteract_obj.start() iteract_obj = pt.interact_multi_image.MultiImageInteraction(chips_good, nPerPage=15) iteract_obj.start() x = sklearn.cluster.KMeans(2) x.fit(np.nan_to_num(measures)) import vtool.quality_classifier from vtool.quality_classifier import contrast_measures chips128 = depc.get_property('chips', aid_list, 'img', config={'dim_size': 256}) gray_chips = [ vt.convert_colorspace(x, 'GRAY') for x in ut.ProgIter(chips128) ] measures = list( ut.generate(contrast_measures, gray_chips, force_serial=ibs.force_serial)) measures = np.array(measures) measures = np.nan_to_num(measures) y = measures.T[3] sortx = y.argsort() ys = y.take(sortx) pca = sklearn.decomposition.PCA(1) pca.fit(measures) pca_measure = pca.transform(measures) nfeats_white = (nfeats_orig - nfeats_orig.mean()) / nfeats_orig.std() pca_white = (pca_measure - pca_measure.mean()) / pca_measure.std() sortx = nfeats_white.argsort() pt.plt.plot(pca_white[sortx], 'x') pt.plt.plot(nfeats_white[sortx], '.') pyhesaff.detect_feats_in_image svc = sklearn.svm.LinearSVC() svc.fit(measures, nfeats_orig > 500) svc.predict(measures) == (nfeats_orig > 500) svr = sklearn.svm.LinearSVR() svr.fit(measures, nfeats_orig) svr.predict(measures) depc['feat'].get_config_history(z1) depc['feat'].get_config_history(z2) pt.plt.plot(nfeats_hteq[sortx], 'x') pt.plt.plot(nfeats_orig[sortx], '.') pt.plt.plot(nfeats_ateq[sortx], 'o') z1 = ibs.depc_annot.get_rowids('feat', aid_list, config={'histeq': True}) z2 = ibs.depc_annot.get_rowids('feat', aid_list) assert len(set(z1).intersection(z2)) == 0
def dummy_cut_example(): r""" CommandLine: python -m ibeis.workflow --exec-dummy_cut_example --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.workflow import * # NOQA >>> result = dummy_cut_example() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import opengm import numpy as np import plottool as pt pt.ensure_pylab_qt4() # Matching Graph cost_matrix = np.array([ [0.5, 0.6, 0.2, 0.4], [0.0, 0.5, 0.2, 0.9], [0.0, 0.0, 0.5, 0.1], [0.0, 0.0, 0.0, 0.5], ]) cost_matrix += cost_matrix.T number_of_labels = 4 num_annots = 4 #cost_matrix = (cost_matrix * 2) - 1 #gm = opengm.gm(number_of_labels) gm = opengm.gm(np.ones(num_annots) * number_of_labels) aids = np.arange(num_annots) aid_pairs = np.array([(a1, a2) for a1, a2 in ut.iprod( aids, aids) if a1 != a2], dtype=np.uint32) aid_pairs.sort(axis=1) # add a potts function # penalizes neighbors for having different labels # beta = 0 # 0.1 # strength of potts regularizer #beta = 0.1 # 0.1 # strength of potts regularizer # Places to look for the definition of this stupid class # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx # /src/interfaces/python/opengm/opengmcore/function_injector.py #shape = [number_of_labels] * 2 #regularizer = opengm.PottsGFunction(shape, 0.0, beta) # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object : # values = np.arange(1, ut.num_partitions(num_annots) + 1) #regularizer = opengm.PottsGFunction(shape) #reg_fid = gm.addFunction(regularizer) # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems # http://arxiv.org/pdf/1404.0533.pdf # regularizer1 = opengm.pottsFunction([number_of_labels] * 2, valueEqual=0.0, valueNotEqual=beta) # gm.addFactors(reg_fid, aid_pairs) # 2nd order function pair_fid = gm.addFunction(cost_matrix) gm.addFactors(pair_fid, aid_pairs) if False: Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) else: Inf = opengm.inference.Multicut parameter = opengm.InfParam() inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self,): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector),)) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) print(callback.labels) print(cost_matrix) pt.imshow(cost_matrix, cmap='magma') opengm.visualizeGm(gm=gm) pass
def segmentation_example(): import vigra import opengm import sklearn import sklearn.mixture import numpy as np from vigra import graphs import matplotlib as mpl import plottool as pt pt.ensure_pylab_qt4() # load image and convert to LAB img_fpath = str(ut.grab_test_imgpath(str('lena.png'))) img = vigra.impex.readImage(img_fpath) imgLab = vigra.colors.transform_RGB2Lab(img) superpixelDiameter = 15 # super-pixel size slicWeight = 15.0 # SLIC color - spatial weight labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) - 1 # get 2D grid graph and RAG gridGraph = graphs.gridGraph(img.shape[0:2]) rag = graphs.regionAdjacencyGraph(gridGraph, labels) # Node Features nodeFeatures = rag.accumulateNodeFeatures(imgLab) nodeFeaturesImg = rag.projectNodeFeaturesToGridGraph(nodeFeatures) nodeFeaturesImg = vigra.taggedView(nodeFeaturesImg, "xyc") nodeFeaturesImgRgb = vigra.colors.transform_Lab2RGB(nodeFeaturesImg) nCluster = 5 g = sklearn.mixture.GMM(n_components=nCluster) g.fit(nodeFeatures[:, :]) clusterProb = g.predict_proba(nodeFeatures) # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Irregular%20Factor%20Graphs.ipynb # https://github.com/opengm/opengm/blob/master/src/interfaces/python/examples/tutorial/Hard%20and%20Soft%20Constraints.ipynb clusterProbImg = rag.projectNodeFeaturesToGridGraph( clusterProb.astype(np.float32)) clusterProbImg = vigra.taggedView(clusterProbImg, "xyc") ndim_data = clusterProbImg.reshape((-1, nCluster)) pca = sklearn.decomposition.PCA(n_components=3) print(ndim_data.shape) pca.fit(ndim_data) print(ut.repr2(pca.explained_variance_ratio_, precision=2)) oldshape = (clusterProbImg.shape[0:2] + (-1, )) clusterProgImg3 = pca.transform(ndim_data).reshape(oldshape) print(clusterProgImg3.shape) # graphical model with as many variables # as superpixels, each has 3 states gm = opengm.gm(np.ones(rag.nodeNum, dtype=opengm.label_type) * nCluster) # convert probabilites to energies probs = np.clip(clusterProb, 0.00001, 0.99999) costs = -1.0 * np.log(probs) # add ALL unaries AT ONCE fids = gm.addFunctions(costs) gm.addFactors(fids, np.arange(rag.nodeNum)) # add a potts function beta = 40.0 # strength of potts regularizer regularizer = opengm.pottsFunction([nCluster] * 2, 0.0, beta) fid = gm.addFunction(regularizer) # get variable indices of adjacent superpixels # - or "u" and "v" node id's for edges uvIds = rag.uvIds() uvIds = np.sort(uvIds, axis=1) # add all second order factors at once gm.addFactors(fid, uvIds) # get super-pixels with slic on LAB image Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self, ): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector), )) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) pt.imshow(clusterProgImg3.swapaxes(0, 1)) # plot superpixels cmap = mpl.colors.ListedColormap(np.random.rand(nseg, 3)) pt.imshow(labels.swapaxes(0, 1).squeeze(), cmap=cmap) pt.imshow(nodeFeaturesImgRgb) cmap = mpl.colors.ListedColormap(np.random.rand(nCluster, 3)) for arg in callback.labels: arg = vigra.taggedView(arg, "n") argImg = rag.projectNodeFeaturesToGridGraph(arg.astype(np.uint32)) argImg = vigra.taggedView(argImg, "xy") # plot superpixels pt.imshow(argImg.swapaxes(0, 1).squeeze(), cmap=cmap)
def crftest(): """ pip install pyqpbo pip install pystruct http://taku910.github.io/crfpp/#install cd ~/tmp #wget https://drive.google.com/folderview?id=0B4y35FiV1wh7fngteFhHQUN2Y1B5eUJBNHZUemJYQV9VWlBUb3JlX0xBdWVZTWtSbVBneU0&usp=drive_web#list 7z x CRF++-0.58.tar.gz 7z x CRF++-0.58.tar cd CRF++-0.58 chmod +x configure ./configure make """ import pystruct import pystruct.models inference_method_options = ['lp', 'max-product'] inference_method = inference_method_options[1] #graph = pystruct.models.GraphCRF( # n_states=None, # n_features=None, # inference_method=inference_method, # class_weight=None, # directed=False, #) num_annots = 5 num_names = num_annots aids = np.arange(5) rng = np.random.RandomState(0) hidden_nids = rng.randint(0, num_names, num_annots) unique_nids, groupxs = ut.group_indices(hidden_nids) # Indicator vector indicating the name node_features = np.zeros((num_annots, num_names)) node_features[(aids, hidden_nids)] = 1 toy_params = { True: { 'mu': 1.0, 'sigma': 2.2 }, False: { 'mu': 7.0, 'sigma': .9 } } if False: import vtool as vt import plottool as pt pt.ensure_pylab_qt4() xdata = np.linspace(0, 100, 1000) tp_pdf = vt.gauss_func1d(xdata, **toy_params[True]) fp_pdf = vt.gauss_func1d(xdata, **toy_params[False]) pt.plot_probabilities([tp_pdf, fp_pdf], ['TP', 'TF'], xdata=xdata) def metric(aidx1, aidx2, hidden_nids=hidden_nids, toy_params=toy_params): if aidx1 == aidx2: return 0 rng = np.random.RandomState(int(aidx1 + aidx2)) same = hidden_nids[int(aidx1)] == hidden_nids[int(aidx2)] mu, sigma = ut.dict_take(toy_params[same], ['mu', 'sigma']) return np.clip(rng.normal(mu, sigma), 0, np.inf) pairwise_aidxs = list(ut.iprod(range(num_annots), range(num_annots))) pairwise_labels = np.array( [hidden_nids[a1] == hidden_nids[a2] for a1, a2 in pairwise_aidxs]) pairwise_scores = np.array([metric(*zz) for zz in pairwise_aidxs]) pairwise_scores_mat = pairwise_scores.reshape(num_annots, num_annots) graph = pystruct.models.EdgeFeatureGraphCRF( n_states=num_annots, n_features=num_names, n_edge_features=1, inference_method=inference_method, ) import opengm numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam(workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.random.rand(10, 10, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.GraphCut(gm) inf.infer() arg = inf.arg()
def dummy_cut_example(): r""" CommandLine: python -m ibeis.workflow --exec-dummy_cut_example --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.workflow import * # NOQA >>> result = dummy_cut_example() >>> print(result) >>> ut.quit_if_noshow() >>> import plottool as pt >>> ut.show_if_requested() """ import opengm import numpy as np import plottool as pt pt.ensure_pylab_qt4() # Matching Graph cost_matrix = np.array([ [0.5, 0.6, 0.2, 0.4], [0.0, 0.5, 0.2, 0.9], [0.0, 0.0, 0.5, 0.1], [0.0, 0.0, 0.0, 0.5], ]) cost_matrix += cost_matrix.T number_of_labels = 4 num_annots = 4 #cost_matrix = (cost_matrix * 2) - 1 #gm = opengm.gm(number_of_labels) gm = opengm.gm(np.ones(num_annots) * number_of_labels) aids = np.arange(num_annots) aid_pairs = np.array([(a1, a2) for a1, a2 in ut.iprod(aids, aids) if a1 != a2], dtype=np.uint32) aid_pairs.sort(axis=1) # add a potts function # penalizes neighbors for having different labels # beta = 0 # 0.1 # strength of potts regularizer #beta = 0.1 # 0.1 # strength of potts regularizer # Places to look for the definition of this stupid class # ~/code/opengm/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx # /src/interfaces/python/opengm/opengmcore/function_injector.py #shape = [number_of_labels] * 2 #regularizer = opengm.PottsGFunction(shape, 0.0, beta) # __init__( (object)arg1, (object)shape [, (object)values=()]) -> object : # values = np.arange(1, ut.num_partitions(num_annots) + 1) #regularizer = opengm.PottsGFunction(shape) #reg_fid = gm.addFunction(regularizer) # A Comparative Study of Modern Inference Techniques for Structured Discrete Energy Minimization Problems # http://arxiv.org/pdf/1404.0533.pdf # regularizer1 = opengm.pottsFunction([number_of_labels] * 2, valueEqual=0.0, valueNotEqual=beta) # gm.addFactors(reg_fid, aid_pairs) # 2nd order function pair_fid = gm.addFunction(cost_matrix) gm.addFactors(pair_fid, aid_pairs) if False: Inf = opengm.inference.BeliefPropagation parameter = opengm.InfParam(steps=10, damping=0.5, convergenceBound=0.001) else: Inf = opengm.inference.Multicut parameter = opengm.InfParam() inf = Inf(gm, parameter=parameter) class PyCallback(object): def __init__(self, ): self.labels = [] def begin(self, inference): print("begin of inference") def end(self, inference): self.labels.append(inference.arg()) def visit(self, inference): gm = inference.gm() labelVector = inference.arg() print("energy %r" % (gm.evaluate(labelVector), )) self.labels.append(labelVector) callback = PyCallback() visitor = inf.pythonVisitor(callback, visitNth=1) inf.infer(visitor) print(callback.labels) print(cost_matrix) pt.imshow(cost_matrix, cmap='magma') opengm.visualizeGm(gm=gm) pass
def learn_prob_score(num_scores=5, pad=55, ret_enc=False, use_cache=None): r""" Args: num_scores (int): (default = 5) Returns: tuple: (discr_domain, discr_p_same) CommandLine: python -m ibeis.algo.hots.demobayes --exec-learn_prob_score --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.hots.demobayes import * # NOQA >>> num_scores = 2 >>> (discr_domain, discr_p_same, encoder) = learn_prob_score(num_scores, ret_enc=True, use_cache=False) >>> print('discr_p_same = %r' % (discr_p_same,)) >>> ut.quit_if_noshow() >>> import plottool as pt >>> encoder.visualize() >>> ut.show_if_requested() """ num_annots_train = 200 num_names_train = 5 toy_data = get_toy_data_1v1(num_annots_train, num_names_train) #pairwise_aidxs, pairwise_scores, pairwise_matches = ut.dict_take( # toy_data, 'pairwise_aidxs, pairwise_scores, pairwise_matches'.split(', ')) diag_scores, diag_labels = ut.dict_take( toy_data, 'diag_scores, diag_labels'.split(', ')) #is_diag = [r < c for r, c, in pairwise_aidxs] #diag_scores = pairwise_scores.compress(is_diag) #diag_labels = pairwise_matches.compress(is_diag) # Learn P(S_{ij} | M_{ij}) import vtool as vt encoder = vt.ScoreNormalizer( reverse=True, monotonize=True, adjust=4, ) encoder.fit(X=diag_scores, y=diag_labels, verbose=True) if False: import plottool as pt pt.ensure_pylab_qt4() encoder.visualize() #show_toy_distributions() def discretize_probs(encoder): p_tp_given_score = encoder.p_tp_given_score / encoder.p_tp_given_score.sum() bins = len(p_tp_given_score) - (pad * 2) stride = int(np.ceil(bins / num_scores)) idxs = np.arange(0, bins, stride) + pad discr_p_same = p_tp_given_score.take(idxs) discr_p_same = discr_p_same / discr_p_same.sum() discr_domain = encoder.score_domain.take(idxs) return discr_domain, discr_p_same discr_domain, discr_p_same = discretize_probs(encoder) if ret_enc: return discr_domain, discr_p_same, encoder return discr_domain, discr_p_same
def crftest(): """ pip install pyqpbo pip install pystruct http://taku910.github.io/crfpp/#install cd ~/tmp #wget https://drive.google.com/folderview?id=0B4y35FiV1wh7fngteFhHQUN2Y1B5eUJBNHZUemJYQV9VWlBUb3JlX0xBdWVZTWtSbVBneU0&usp=drive_web#list 7z x CRF++-0.58.tar.gz 7z x CRF++-0.58.tar cd CRF++-0.58 chmod +x configure ./configure make """ import pystruct import pystruct.models inference_method_options = ['lp', 'max-product'] inference_method = inference_method_options[1] #graph = pystruct.models.GraphCRF( # n_states=None, # n_features=None, # inference_method=inference_method, # class_weight=None, # directed=False, #) num_annots = 5 num_names = num_annots aids = np.arange(5) rng = np.random.RandomState(0) hidden_nids = rng.randint(0, num_names, num_annots) unique_nids, groupxs = ut.group_indices(hidden_nids) # Indicator vector indicating the name node_features = np.zeros((num_annots, num_names)) node_features[(aids, hidden_nids)] = 1 toy_params = { True: {'mu': 1.0, 'sigma': 2.2}, False: {'mu': 7.0, 'sigma': .9} } if False: import vtool as vt import plottool as pt pt.ensure_pylab_qt4() xdata = np.linspace(0, 100, 1000) tp_pdf = vt.gauss_func1d(xdata, **toy_params[True]) fp_pdf = vt.gauss_func1d(xdata, **toy_params[False]) pt.plot_probabilities([tp_pdf, fp_pdf], ['TP', 'TF'], xdata=xdata) def metric(aidx1, aidx2, hidden_nids=hidden_nids, toy_params=toy_params): if aidx1 == aidx2: return 0 rng = np.random.RandomState(int(aidx1 + aidx2)) same = hidden_nids[int(aidx1)] == hidden_nids[int(aidx2)] mu, sigma = ut.dict_take(toy_params[same], ['mu', 'sigma']) return np.clip(rng.normal(mu, sigma), 0, np.inf) pairwise_aidxs = list(ut.iprod(range(num_annots), range(num_annots))) pairwise_labels = np.array([hidden_nids[a1] == hidden_nids[a2] for a1, a2 in pairwise_aidxs]) pairwise_scores = np.array([metric(*zz) for zz in pairwise_aidxs]) pairwise_scores_mat = pairwise_scores.reshape(num_annots, num_annots) graph = pystruct.models.EdgeFeatureGraphCRF( n_states=num_annots, n_features=num_names, n_edge_features=1, inference_method=inference_method, ) import opengm numVar = 10 unaries = np.ones([numVar, 3], dtype=opengm.value_type) gm = opengm.gm(np.ones(numVar, dtype=opengm.label_type) * 3) unary_fids = gm.addFunctions(unaries) gm.addFactors(unary_fids, np.arange(numVar)) infParam = opengm.InfParam( workflow=ut.ensure_ascii('(IC)(TTC-I,CC-I)'), ) inf = opengm.inference.Multicut(gm, parameter=infParam) visitor = inf.verboseVisitor(printNth=1, multiline=False) inf.infer(visitor) arg = inf.arg() # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) # regularizer = opengm.pottsFunction([3, 3], 0.0, beta) # gridVariableIndices = opengm.secondOrderGridVis(img.shape[0], img.shape[1]) # fid = gm.addFunction(regularizer) # gm.addFactors(fid, gridVariableIndices) unaries = np.random.rand(10, 10, 2) potts = opengm.PottsFunction([2, 2], 0.0, 0.4) gm = opengm.grid2d2Order(unaries=unaries, regularizer=potts) inf = opengm.inference.GraphCut(gm) inf.infer() arg = inf.arg()
def learn_prob_score(num_scores=5, pad=55, ret_enc=False, use_cache=None): r""" Args: num_scores (int): (default = 5) Returns: tuple: (discr_domain, discr_p_same) CommandLine: python -m ibeis.algo.hots.demobayes --exec-learn_prob_score --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.hots.demobayes import * # NOQA >>> num_scores = 2 >>> (discr_domain, discr_p_same, encoder) = learn_prob_score(num_scores, ret_enc=True, use_cache=False) >>> print('discr_p_same = %r' % (discr_p_same,)) >>> ut.quit_if_noshow() >>> import plottool as pt >>> encoder.visualize() >>> ut.show_if_requested() """ num_annots_train = 200 num_names_train = 5 toy_data = get_toy_data_1v1(num_annots_train, num_names_train) #pairwise_aidxs, pairwise_scores, pairwise_matches = ut.dict_take( # toy_data, 'pairwise_aidxs, pairwise_scores, pairwise_matches'.split(', ')) diag_scores, diag_labels = ut.dict_take( toy_data, 'diag_scores, diag_labels'.split(', ')) #is_diag = [r < c for r, c, in pairwise_aidxs] #diag_scores = pairwise_scores.compress(is_diag) #diag_labels = pairwise_matches.compress(is_diag) # Learn P(S_{ij} | M_{ij}) import vtool as vt encoder = vt.ScoreNormalizer( reverse=True, monotonize=True, adjust=4, ) encoder.fit(X=diag_scores, y=diag_labels, verbose=True) if False: import plottool as pt pt.ensure_pylab_qt4() encoder.visualize() #show_toy_distributions() def discretize_probs(encoder): p_tp_given_score = encoder.p_tp_given_score / encoder.p_tp_given_score.sum( ) bins = len(p_tp_given_score) - (pad * 2) stride = int(np.ceil(bins / num_scores)) idxs = np.arange(0, bins, stride) + pad discr_p_same = p_tp_given_score.take(idxs) discr_p_same = discr_p_same / discr_p_same.sum() discr_domain = encoder.score_domain.take(idxs) return discr_domain, discr_p_same discr_domain, discr_p_same = discretize_probs(encoder) if ret_enc: return discr_domain, discr_p_same, encoder return discr_domain, discr_p_same
def event_space(): """ pip install matplotlib-venn """ from matplotlib import pyplot as plt # import numpy as np from matplotlib_venn import venn3, venn2, venn3_circles plt.figure(figsize=(4, 4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels=('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.show() same = set(['comparable', 'incomparable', 'same']) diff = set(['comparable', 'incomparable', 'diff']) # comparable = set(['comparable', 'same', 'diff']) # incomparable = set(['incomparable', 'same', 'diff']) subsets = [same, diff] # , comparable, incomparable] set_labels = ('same', 'diff') # , 'comparable', 'incomparable') venn3(subsets=subsets, set_labels=set_labels) plt.show() import plottool as pt pt.ensure_pylab_qt4() from matplotlib_subsets import treesets_rectangles tree = ((120, 'Same', None), [ ((50, 'comparable', None), []), ((50, 'incomparable', None), []) ](120, 'Diff', None), [((50, 'comparable', None), []), ((50, 'incomparable', None), [])]) treesets_rectangles(tree) plt.show() from matplotlib import pyplot as plt from matplotlib_venn import venn2, venn2_circles # NOQA # Subset sizes s = ( 2, # Ab 3, # aB 1, # AB ) v = venn2(subsets=s, set_labels=('A', 'B')) # Subset labels v.get_label_by_id('10').set_text('A but not B') v.get_label_by_id('01').set_text('B but not A') v.get_label_by_id('11').set_text('A and B') # Subset colors v.get_patch_by_id('10').set_color('c') v.get_patch_by_id('01').set_color('#993333') v.get_patch_by_id('11').set_color('blue') # Subset alphas v.get_patch_by_id('10').set_alpha(0.4) v.get_patch_by_id('01').set_alpha(1.0) v.get_patch_by_id('11').set_alpha(0.7) # Border styles c = venn2_circles(subsets=s, linestyle='solid') c[0].set_ls('dashed') # Line style c[0].set_lw(2.0) # Line width plt.show()