def ensure_layout(self, layout, graph=None): """Helper method that ensures that I{layout} is an instance of L{Layout}. If it is not, the method will try to convert it to a L{Layout} according to the following rules: - If I{layout} is a string, it is assumed to be a name of an igraph layout, and it will be passed on to the C{layout} method of the given I{graph} if I{graph} is not C{None}. - If I{layout} is C{None}, the C{layout} method of I{graph} will be invoked with no parameters, which will call the default layout algorithm. - Otherwise, I{layout} will be passed on to the constructor of L{Layout}. This handles lists of lists, lists of tuples and such. If I{layout} is already a L{Layout} instance, it will still be copied and a copy will be returned. This is because graph drawers are allowed to transform the layout for their purposes, and we don't want the transformation to propagate back to the caller. """ if isinstance(layout, Layout): layout = Layout(layout.coords) elif isinstance(layout, str) or layout is None: layout = graph.layout(layout) else: layout = Layout(layout) return layout
g.vs[g.vs.find(root_user.id).index]['color'] = 'rgb(255,0,0)' #g.vs['shape'] = ['hidden' if x == 0 else 'circle' for x in community_idx_list] #community_stats community_stats = get_community_stats(network, g, vertex_clustering, layout_list) #set node size based on community_stats size_array = np.array([0.0] * len(community_idx_list)) for community_idx, values_dict in community_stats.items(): size_array[values_dict['member_idxs']] = values_dict['hybrid_pagerank'] g.vs['size'] = list(size_array / (max(size_array) / 30)) g.vs[g.vs.find(root_user.id).index]['size'] = 40 g.vs['size'] = 1 #plot to file layout = Layout(layout_list) filepath = 'io/%s.png' % root_user.screen_name thumb_filepath = 'io/%s_thumb.png' % root_user.screen_name plot_graph(g, layout, filepath, size_tup=(600, 600)) #need to adjust vs['size'] if i want to do this #plot_graph(g, layout, thumb_filepath, size_tup=(50, 50)) if not smarttypes.config.IS_PROD: os.system('cp io/%s*.png /home/timmyt/projects/smarttypes/smarttypes/static/images/maps/.' % root_user.screen_name) else: os.system('scp io/%s*.png cottie:/home/timmyt/projects/smarttypes/smarttypes/static/images/maps/.' % root_user.screen_name) print 'save to disk' twitter_reduction = TwitterReduction.create_reduction(root_user.id, postgres_handle) postgres_handle.connection.commit() for community_idx, values_dict in community_stats.items(): #params:
def __plot__(self, context, bbox, palette, *args, **kwds): """Draws the dendrogram on the given Cairo context Supported keyword arguments are: - C{orientation}: the orientation of the dendrogram. Must be one of the following values: C{left-right}, C{bottom-top}, C{right-left} or C{top-bottom}. Individual elements are always placed at the former edge and merges are performed towards the latter edge. Possible aliases: C{horizontal} = C{left-right}, C{vertical} = C{bottom-top}, C{lr} = C{left-right}, C{rl} = C{right-left}, C{tb} = C{top-bottom}, C{bt} = C{bottom-top}. The default is C{left-right}. """ from igraph.layout import Layout if not hasattr(self, "_names"): self._names = map(str, xrange(self._n)) orientation = kwds.get("orientation", "lr") orientation_aliases = { "lr": "left-right", "rl": "right-left", "tb": "top-bottom", "bt": "bottom-top", "horizontal": "left-right", "horiz": "left-right", "h": "left-right", "vertical": "bottom-top", "vert": "bottom-top", "v": "bottom-top" } orientation = orientation_aliases.get(orientation, orientation) if orientation not in ("left-right", "right-left", "top-bottom", "bottom-top"): raise ValueError, "unknown orientation: %s" % orientation horiz = orientation in ("left-right", "right-left") # Calculate space needed for individual items at the bottom of the dendrogram item_boxes = [self._item_box_size(context, horiz, idx) \ for idx in xrange(self._n)] # Calculate coordinates w, h = bbox.width, bbox.height lo = Layout([(0, 0)] * self._n, dim=2) inorder = self._traverse_inorder() if not horiz: x, y = 0, 0 for idx, element in enumerate(inorder): lo[element] = (x + item_boxes[element][0] / 2., 0) x += item_boxes[element][0] for c1, c2 in self._merges: y += 1 lo.append(((lo[c1][0] + lo[c2][0]) / 2., y)) # Mirror or rotate the layout if necessary if orientation == "bottom-top": lo.mirror(1) else: x, y = 0, 0 for idx, element in enumerate(inorder): lo[element] = (0, y + item_boxes[element][1] / 2.) y += item_boxes[element][1] for c1, c2 in self._merges: x += 1 lo.append((x, (lo[c1][1] + lo[c2][1]) / 2.)) # Mirror or rotate the layout if necessary if orientation == "right-left": lo.mirror(0) # Rescale layout to the bounding box maxw, maxh = max([e[0] for e in item_boxes ]), max([e[1] for e in item_boxes]) # w, h: width and height of the area containing the dendrogram tree without # the items. dx, dy: displacement of the dendrogram tree w, h, dx, dy = float(bbox.width), float(bbox.height), 0, 0 if horiz: w -= maxw if orientation == "left-right": dx = maxw else: h -= maxh if orientation == "top-bottom": dy = maxh sl, st, sr, sb = lo.bounding_box() sw, sh = max(sr - sl, 1), max(sb - st, 1) rx, ry = w / sw, h / sh lo.scale(rx, ry) lo.translate(dx - sl * rx + bbox.coords[0], dy - st * ry + bbox.coords[1]) context.set_source_rgb(0., 0., 0.) context.set_line_width(1) # Draw items if horiz: sgn = -1 if orientation == "right-left": sgn = 0 for idx in xrange(self._n): x = lo[idx][0] + sgn * item_boxes[idx][0] y = lo[idx][1] - item_boxes[idx][1] / 2. self._plot_item(context, horiz, idx, x, y) else: sgn = 0 if orientation == "bottom-top": sgn = 1 for idx in xrange(self._n): x = lo[idx][0] - item_boxes[idx][0] / 2. y = lo[idx][1] + sgn * item_boxes[idx][1] self._plot_item(context, horiz, idx, x, y) # Draw dendrogram lines if not horiz: for idx, (c1, c2) in enumerate(self._merges): x0, y0 = lo[c1] x1, y1 = lo[c2] x2, y2 = lo[idx + self._n] context.move_to(x0, y0) context.line_to(x0, y2) context.line_to(x1, y2) context.line_to(x1, y1) context.stroke() else: for idx, (c1, c2) in enumerate(self._merges): x0, y0 = lo[c1] x1, y1 = lo[c2] x2, y2 = lo[idx + self._n] context.move_to(x0, y0) context.line_to(x2, y0) context.line_to(x2, y1) context.line_to(x1, y1) context.stroke()
def __plot__(self, context, bbox, palette, *args, **kwds): """Draws the dendrogram on the given Cairo context Supported keyword arguments are: - C{orientation}: the orientation of the dendrogram. Must be one of the following values: C{left-right}, C{bottom-top}, C{right-left} or C{top-bottom}. Individual elements are always placed at the former edge and merges are performed towards the latter edge. Possible aliases: C{horizontal} = C{left-right}, C{vertical} = C{bottom-top}, C{lr} = C{left-right}, C{rl} = C{right-left}, C{tb} = C{top-bottom}, C{bt} = C{bottom-top}. The default is C{left-right}. """ from igraph.layout import Layout if not hasattr(self, "_names"): self._names = map(str, xrange(self._n)) orientation = kwds.get("orientation", "lr") orientation_aliases = { "lr": "left-right", "rl": "right-left", "tb": "top-bottom", "bt": "bottom-top", "horizontal": "left-right", "horiz": "left-right", "h": "left-right", "vertical": "bottom-top", "vert": "bottom-top", "v": "bottom-top" } orientation = orientation_aliases.get(orientation, orientation) if orientation not in ("left-right", "right-left", "top-bottom", "bottom-top"): raise ValueError, "unknown orientation: %s" % orientation horiz = orientation in ("left-right", "right-left") # Calculate space needed for individual items at the bottom of the dendrogram item_boxes = [self._item_box_size(context, horiz, idx) \ for idx in xrange(self._n)] # Calculate coordinates w, h = bbox.width, bbox.height lo = Layout([(0,0)]*self._n, dim=2) inorder = self._traverse_inorder() if not horiz: x, y = 0, 0 for idx, element in enumerate(inorder): lo[element] = (x + item_boxes[element][0]/2., 0) x += item_boxes[element][0] for c1, c2 in self._merges: y += 1 lo.append(((lo[c1][0]+lo[c2][0])/2., y)) # Mirror or rotate the layout if necessary if orientation == "bottom-top": lo.mirror(1) else: x, y = 0, 0 for idx, element in enumerate(inorder): lo[element] = (0, y + item_boxes[element][1]/2.) y += item_boxes[element][1] for c1, c2 in self._merges: x += 1 lo.append((x, (lo[c1][1]+lo[c2][1])/2.)) # Mirror or rotate the layout if necessary if orientation == "right-left": lo.mirror(0) # Rescale layout to the bounding box maxw, maxh = max([e[0] for e in item_boxes]), max([e[1] for e in item_boxes]) # w, h: width and height of the area containing the dendrogram tree without # the items. dx, dy: displacement of the dendrogram tree w, h, dx, dy = float(bbox.width), float(bbox.height), 0, 0 if horiz: w -= maxw if orientation == "left-right": dx = maxw else: h -= maxh if orientation == "top-bottom": dy = maxh sl, st, sr, sb = lo.bounding_box() sw, sh = max(sr-sl, 1), max(sb-st, 1) rx, ry = w/sw, h/sh lo.scale(rx, ry) lo.translate(dx-sl*rx+bbox.coords[0], dy-st*ry+bbox.coords[1]) context.set_source_rgb(0.,0.,0.) context.set_line_width(1) # Draw items if horiz: sgn = -1 if orientation == "right-left": sgn = 0 for idx in xrange(self._n): x = lo[idx][0] + sgn * item_boxes[idx][0] y = lo[idx][1] - item_boxes[idx][1]/2. self._plot_item(context, horiz, idx, x, y) else: sgn = 0 if orientation == "bottom-top": sgn = 1 for idx in xrange(self._n): x = lo[idx][0] - item_boxes[idx][0]/2. y = lo[idx][1] + sgn * item_boxes[idx][1] self._plot_item(context, horiz, idx, x, y) # Draw dendrogram lines if not horiz: for idx, (c1, c2) in enumerate(self._merges): x0, y0 = lo[c1] x1, y1 = lo[c2] x2, y2 = lo[idx + self._n] context.move_to(x0, y0) context.line_to(x0, y2) context.line_to(x1, y2) context.line_to(x1, y1) context.stroke() else: for idx, (c1, c2) in enumerate(self._merges): x0, y0 = lo[c1] x1, y1 = lo[c2] x2, y2 = lo[idx + self._n] context.move_to(x0, y0) context.line_to(x2, y0) context.line_to(x2, y1) context.line_to(x1, y1) context.stroke()