def get_graph_bounding_box(graph): import utool as ut import networkx as nx import vtool as vt #nx.get_node_attrs = nx.get_node_attributes nodes = list(graph.nodes()) pos_list = ut.take(nx.get_node_attributes(graph, 'pos'), nodes) shape_list = ut.take(nx.get_node_attributes(graph, 'size'), nodes) node_extents = np.array([ vt.extent_from_bbox(vt.bbox_from_center_wh(xy, wh)) for xy, wh in zip(pos_list, shape_list) ]) tl_x, br_x, tl_y, br_y = node_extents.T extent = tl_x.min(), br_x.max(), tl_y.min(), br_y.max() bbox = vt.bbox_from_extent(extent) return bbox
def on_click_inside(self, event, ax): self.ax = ax self.event = event event = self.event #print(ax) #print(event.x) #print(event.y) pos = self.plotinfo['node']['pos'] nodes = list(pos.keys()) pos_list = ut.dict_take(pos, nodes) # TODO: FIXME #x = 10 #y = 10 import numpy as np # NOQA x, y = event.xdata, event.ydata point = np.array([x, y]) pos_list = np.array(pos_list) index, dist = vt.closest_point(point, pos_list, distfunc=vt.L2) #print('dist = %r' % (dist,)) node = nodes[index] aid = self.node2_aid[node] context_shown = False CHECK_PAIR = True if CHECK_PAIR: if self.event.button == 3 and not context_shown: if len(self.selected_aids) != 2: print('This funciton only work if exactly 2 are selected') else: from ibeis.gui import inspect_gui context_shown = True aid1, aid2 = (self.selected_aids) qres = None qreq_ = None options = inspect_gui.get_aidpair_context_menu_options( self.infr.ibs, aid1, aid2, qres, qreq_=qreq_) self.show_popup_menu(options, event) bbox = vt.bbox_from_center_wh(self.plotinfo['node']['pos'][node], self.plotinfo['node']['size'][node]) SELECT_ANNOT = vt.point_inside_bbox(point, bbox) #SELECT_ANNOT = dist < 35 if SELECT_ANNOT: #print(ut.obj_str(ibs.get_annot_info(aid, default=True, # name=False, gname=False))) if self.event.button == 1: self.toggle_selected_aid(aid) if self.event.button == 3 and not context_shown: # right click from ibeis.viz.interact import interact_chip context_shown = True #refresh_func = functools.partial(viz.show_name, ibs, nid, #fnum=fnum, sel_aids=sel_aids) refresh_func = None config2_ = None options = interact_chip.build_annot_context_options( self.infr.ibs, aid, refresh_func=refresh_func, with_interact_name=False, config2_=config2_) self.show_popup_menu(options, event) else: if self.event.button == 3: options = [ ('Toggle images', self.toggle_imgs), ] self.show_popup_menu(options, event)
def netx_draw_images_at_positions(img_list, pos_list, size_list, color_list, framewidth_list): """ Overlays images on a networkx graph References: https://gist.github.com/shobhit/3236373 http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html http://stackoverflow.com/questions/11487797/mpl-overlay-small-image http://matplotlib.org/api/text_api.html http://matplotlib.org/api/offsetbox_api.html TODO: look into DraggableAnnotation """ #print('[viz_graph] drawing %d images' % len(img_list)) # Thumb stackartist import plottool as pt #ax = pt.gca() # Ensure all images have been read img_list_ = [vt.convert_colorspace(vt.imread(img), 'RGB') if isinstance(img, six.string_types) else img for img in img_list] size_list_ = [vt.get_size(img) if size is None else size for size, img in zip(size_list, img_list)] as_offset_image = False if as_offset_image: offset_img_list = [] artist_list = [] # THIS DOES NOT DO WHAT I WANT # Scales the image with data coords from matplotlib.offsetbox import OffsetImage, AnnotationBbox bboxkw = dict( xycoords='data', boxcoords='offset points', #boxcoords='data', pad=0.25, # frameon=False, bboxprops=dict(fc="cyan"), # arrowprops=dict(arrowstyle="->")) ) for pos, img, framewidth in zip(pos_list, img_list_, framewidth_list): offset_img = OffsetImage(img, zoom=.4) bboxkw['frameon'] = framewidth > 0 artist = AnnotationBbox(offset_img, pos, xybox=(-0., 0.), **bboxkw) offset_img_list.append(offset_img) artist_list.append(artist) else: # THIS DOES EXACTLY WHAT I WANT # Ties the image to data coords #for pos, img, size, color, framewidth in zip(pos_list, img_list_, # size_list_, color_list, # framewidth_list): for pos, img, size in zip(pos_list, img_list_, size_list_): bbox = vt.bbox_from_center_wh(pos, size) extent = vt.extent_from_bbox(bbox) pt.plt.imshow(img, extent=extent) imgdat = { #'offset_img_list': offset_img_list, #'artist_list': artist_list, } return imgdat