def make_prob_annots(infr): cm_list = infr.cm_list unique_aids = sorted(ut.list_union(*[cm.daid_list for cm in cm_list] + [[cm.qaid for cm in cm_list]])) aid2_aidx = ut.make_index_lookup(unique_aids) prob_annots = np.zeros((len(unique_aids), len(unique_aids))) for count, cm in enumerate(cm_list): idx = aid2_aidx[cm.qaid] annot_scores = ut.dict_take(cm.aid2_annot_score, unique_aids, 0) prob_annots[idx][:] = annot_scores prob_annots[np.diag_indices(len(prob_annots))] = np.inf prob_annots += 1E-9 #print(ut.hz_str('prob_names = ', ut.array2string2(prob_names, #precision=2, max_line_width=140, suppress_small=True))) return unique_aids, prob_annots
def make_prob_annots(infr): cm_list = infr.cm_list unique_aids = sorted( ut.list_union(*[cm.daid_list for cm in cm_list] + [[cm.qaid for cm in cm_list]])) aid2_aidx = ut.make_index_lookup(unique_aids) prob_annots = np.zeros((len(unique_aids), len(unique_aids))) for count, cm in enumerate(cm_list): idx = aid2_aidx[cm.qaid] annot_scores = ut.dict_take(cm.aid2_annot_score, unique_aids, 0) prob_annots[idx][:] = annot_scores prob_annots[np.diag_indices(len(prob_annots))] = np.inf prob_annots += 1E-9 #print(ut.hz_str('prob_names = ', ut.array2string2(prob_names, #precision=2, max_line_width=140, suppress_small=True))) return unique_aids, prob_annots
def make_prob_names(self): cm_list = self.cm_list # Consolodate information from a series of chip matches unique_nids = sorted(ut.list_union(*[cm.unique_nids for cm in cm_list])) # nid2_nidx = ut.make_index_lookup(unique_nids) # Populate matrix of raw name scores prob_names = np.zeros((len(cm_list), len(unique_nids))) for count, cm in enumerate(cm_list): try: name_scores = ut.dict_take(cm.nid2_name_score, unique_nids, 0) except AttributeError: unique_nidxs = ut.take(cm.nid2_nidx, unique_nids) name_scores = ut.take(cm.name_score_list, unique_nidxs) prob_names[count][:] = name_scores # Normalize to row stochastic matrix prob_names /= prob_names.sum(axis=1)[:, None] # logger.info(ut.hz_str('prob_names = ', ut.repr2(prob_names, # precision=2, max_line_width=140, suppress_small=True))) return unique_nids, prob_names
def make_prob_names(infr): cm_list = infr.cm_list # Consolodate information from a series of chip matches unique_nids = sorted(ut.list_union(*[cm.unique_nids for cm in cm_list])) #nid2_nidx = ut.make_index_lookup(unique_nids) # Populate matrix of raw name scores prob_names = np.zeros((len(cm_list), len(unique_nids))) for count, cm in enumerate(cm_list): try: name_scores = ut.dict_take(cm.nid2_name_score, unique_nids, 0) except AttributeError: unique_nidxs = ut.take(cm.nid2_nidx, unique_nids) name_scores = ut.take(cm.name_score_list, unique_nidxs) prob_names[count][:] = name_scores # Normalize to row stochastic matrix prob_names /= prob_names.sum(axis=1)[:, None] #print(ut.hz_str('prob_names = ', ut.array2string2(prob_names, #precision=2, max_line_width=140, suppress_small=True))) return unique_nids, prob_names
def initialize_graph_and_model(infr): """ Unused in internal split stuff pt.qt4ensure() layout_info = pt.show_nx(graph, as_directed=False, fnum=1, layoutkw=dict(prog='neato'), use_image=True, verbose=0) ax = pt.gca() pt.zoom_factory() pt.interactions.PanEvents() """ #import networkx as nx #import itertools cm_list = infr.cm_list hack = True hack = False if hack: cm_list = cm_list[:10] qaid_list = [cm.qaid for cm in cm_list] daids_list = [cm.daid_list for cm in cm_list] unique_aids = sorted(ut.list_union(*daids_list + [qaid_list])) if hack: unique_aids = sorted(ut.isect(unique_aids, qaid_list)) aid2_aidx = ut.make_index_lookup(unique_aids) # Construct K-broken graph edges = [] edge_weights = [] #top = (infr.qreq_.qparams.K + 1) * 2 #top = (infr.qreq_.qparams.K) * 2 top = (infr.qreq_.qparams.K + 2) for count, cm in enumerate(cm_list): qidx = aid2_aidx[cm.qaid] score_list = cm.annot_score_list sortx = ut.argsort(score_list)[::-1] score_list = ut.take(score_list, sortx)[:top] daid_list = ut.take(cm.daid_list, sortx)[:top] for score, daid in zip(score_list, daid_list): if daid not in qaid_list: continue didx = aid2_aidx[daid] edge_weights.append(score) edges.append((qidx, didx)) # make symmetric directed_edges = dict(zip(edges, edge_weights)) # Find edges that point in both directions undirected_edges = {} for (u, v), w in directed_edges.items(): if (v, u) in undirected_edges: undirected_edges[(v, u)] += w undirected_edges[(v, u)] /= 2 else: undirected_edges[(u, v)] = w edges = list(undirected_edges.keys()) edge_weights = list(undirected_edges.values()) nodes = list(range(len(unique_aids))) nid_labeling = infr.qreq_.ibs.get_annot_nids(unique_aids) labeling = ut.rebase_labels(nid_labeling) import networkx as nx from ibeis.viz import viz_graph set_node_attrs = nx.set_node_attributes set_edge_attrs = nx.set_edge_attributes # Create match-based graph structure graph = nx.DiGraph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) # Important properties nid_list = infr.qreq_.ibs.get_annot_nids(unique_aids) labeling = ut.rebase_labels(nid_list) set_node_attrs(graph, 'name_label', dict(zip(nodes, labeling))) set_edge_attrs(graph, 'weight', dict(zip(edges, edge_weights))) # Visualization properties import plottool as pt ax2_aid = ut.invert_dict(aid2_aidx) set_node_attrs(graph, 'aid', ax2_aid) viz_graph.ensure_node_images(infr.qreq_.ibs, graph) set_node_attrs(graph, 'framewidth', dict(zip(nodes, [3.0] * len(nodes)))) set_node_attrs(graph, 'framecolor', dict(zip(nodes, [pt.DARK_BLUE] * len(nodes)))) ut.color_nodes(graph, labelattr='name_label') edge_colors = pt.scores_to_color(np.array(edge_weights), cmap_='viridis') #import utool #utool.embed() #edge_colors = [pt.color_funcs.ensure_base255(color) for color in edge_colors] #print('edge_colors = %r' % (edge_colors,)) set_edge_attrs(graph, 'color', dict(zip(edges, edge_colors))) # Build inference model from ibeis.algo.hots import graph_iden #graph_iden.rrr() model = graph_iden.InfrModel(graph) #model = graph_iden.InfrModel(len(nodes), edges, edge_weights, labeling=labeling) infr.model = model