def testConstructor(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0)]) self.assertEqual(layout.dim, 3) layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0)], 3) self.assertEqual(layout.dim, 3) self.assertRaises(ValueError, Layout, [(0, 1), (1, 0)], 3) self.assertRaises(ValueError, Layout, [])
def testBoundaries(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) self.assertEqual(layout.boundaries(), ([0, 0, 0], [2, 1, 3])) self.assertEqual(layout.boundaries(1), ([-1, -1, -1], [3, 2, 4])) layout = Layout([]) self.assertRaises(ValueError, layout.boundaries) layout = Layout([], dim=3) self.assertRaises(ValueError, layout.boundaries)
def testFitInto(self): layout = Layout([(-2, 0), (-2, -2), (0, -2), (0, 0)]) layout.fit_into(BoundingBox(5, 5, 8, 10), keep_aspect_ratio=False) self.assertEqual(layout.coords, [[5, 10], [5, 5], [8, 5], [8, 10]]) layout = Layout([(-2, 0), (-2, -2), (0, -2), (0, 0)]) layout.fit_into(BoundingBox(5, 5, 8, 10)) self.assertEqual(layout.coords, [[5, 9], [5, 6], [8, 6], [8, 9]]) layout = Layout([(-1, -1, -1), (0, 0, 0), (1, 1, 1), (2, 2, 0), (3, 3, -1)]) layout.fit_into((0, 0, 0, 8, 8, 4)) self.assertEqual(layout.coords, \ [[0, 0, 0], [2, 2, 2], [4, 4, 4], [6, 6, 2], [8, 8, 0]] )
def testTransform(self): def tr(coord, dx, dy): return coord[0] + dx, coord[1] + dy layout = Layout([(1, 2), (3, 4)]) layout.transform(tr, 2, -1) self.assertEqual(layout.coords, [[3, 1], [5, 3]])
def testCentroid(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) centroid = layout.centroid() self.assertEqual(len(centroid), 3) self.assertAlmostEqual(centroid[0], 0.75) self.assertAlmostEqual(centroid[1], 0.5) self.assertAlmostEqual(centroid[2], 1.)
def testToPolar(self): layout = Layout([(0, 0), (-1, 1), (0, 1), (1, 1)]) layout.to_radial(min_angle=180, max_angle=0, max_radius=2) exp = [[0., 0.], [-2., 0.], [0., 2.], [2, 0.]] for idx in range(4): self.assertAlmostEqual(layout.coords[idx][0], exp[idx][0], places=3) self.assertAlmostEqual(layout.coords[idx][1], exp[idx][1], places=3)
def setup_canvas(self): """Populate the canvas with the initial instructions. """ self._selecting_nnodes = False self.G = Graph.Star(self.nnodes, mode="out") self._unscaled_layout = Layout([(0.0, 0.0), *circle_points(self.nnodes - 1)]) self.scale = INIT_SCALE self.offset_x, self.offset_y = INIT_OFFSET self._selected_edge = self._selected_node = None self._source_node = self._target_edge = None self.canvas.clear() with self.canvas.before: self.background_color = Color(*BACKGROUND_COLOR) self._background = Rectangle(size=self.size, pos=self.pos) with self.canvas: self.animated_edge_color = Color(*HIGHLIGHTED_EDGE) self.animated_edge_color.a = 0 self.animated_edge = Line(width=1.1) # Edge instructions before Node instructions so they're drawn underneath nodes. self._edge_instructions = CanvasBase() with self._edge_instructions: self.edges = { edge.tuple: Edge(edge.tuple, self) for edge in self.G.es } self.canvas.add(self._edge_instructions) # Animated node drawn above edges but below other nodes. with self.canvas: PushMatrix() self.rotation_instruction = Rotate() self.animated_node_color = Color(*ANIMATED_NODE_COLOR) self.animated_node_color.a = 0 self.animated_node = Rectangle(size=(ANIMATION_WIDTH, ANIMATION_HEIGHT), source=ANIMATED_NODE_SOURCE) PopMatrix() self._node_instructions = CanvasBase() with self._node_instructions: self.nodes = [Node(vertex.index, self) for vertex in self.G.vs] self.canvas.add(self._node_instructions) # TODO: Refactor so we only need to do this once self.bind(size=self._delayed_resize, pos=self._delayed_resize) Window.bind(mouse_pos=self.on_mouse_pos) self.step_layout() self.layout_stepper() self._mouse_pos_disabled = False
def testCenter(self): layout = Layout([(-2, 0), (-2, -2), (0, -2), (0, 0)]) layout.center() self.assertEqual(layout.coords, [[-1, 1], [-1, -1], [1, -1], [1, 1]]) layout.center(5, 5) self.assertEqual(layout.coords, [[4, 6], [4, 4], [6, 4], [6, 6]]) self.assertRaises(ValueError, layout.center, 3) self.assertRaises(TypeError, layout.center, p=6)
def testScaling(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) layout.scale(1.5) self.assertEqual( layout.coords, [[0.0, 0.0, 1.5], [0.0, 1.5, 0.0], [1.5, 0.0, 0.0], [3.0, 1.5, 4.5]], ) layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) layout.scale(1, 1, 3) self.assertEqual(layout.coords, [[0, 0, 3], [0, 1, 0], [1, 0, 0], [2, 1, 9]]) layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) layout.scale((2, 2, 1)) self.assertEqual(layout.coords, [[0, 0, 1], [0, 2, 0], [2, 0, 0], [4, 2, 3]]) self.assertRaises(ValueError, layout.scale, 2, 3)
def testTranslation(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) layout2 = layout.copy() layout.translate(1, 3, 2) self.assertEqual(layout.coords, [[1, 3, 3], [1, 4, 2], [2, 3, 2], [3, 4, 5]]) layout.translate((-1, -3, -2)) self.assertEqual(layout.coords, layout2.coords) self.assertRaises(ValueError, layout.translate, v=[3])
def network_visualization(w_plot, membership, name, coord_xy): """ plot the newwork @ param w_plot: weight matrix for plotting @ param membership: a vector indicating the community index for each vertex i.e. [0,0,0,1,1,1,0,0,2,2]) @ param name: vertex name vectors @ param coord_xy: x and y coordinates in a planar for vertices (n_vertices * 2) """ # construct the plot graph g_plot = Graph().Adjacency((w_plot > 0).tolist(), mode='UNDIRECTED') n_clusters = len(set(membership)) n_vertics = len(g_plot.vs) n_edges = len(g_plot.es) # set the attributes for vertices and edges g_plot.vs['name'] = name g_plot.vs['i_cluster'] = membership # the cluster index for each vertex g_plot.es['weight'] = w_plot # assign the cluster index for each edge for v1, v2 in g_plot.get_edgelist(): eid = g_plot.get_eid(v1, v2) if membership[v1] != membership[v2]: g_plot.es[eid][ 'i_cluster'] = n_clusters #if v1,v2 belong to different clusters, g_plot.es['i_cluster'] = n_cluster, crossing clusters condition else: g_plot.es[eid]['i_cluster'] = membership[ v1] # if v1,v2 belong to the same cluster i, g_plot.es['i_cluster'] = i palette = ClusterColoringPalette( n=n_clusters + 1 ) # set one palette for each cluster, extra one color for the crossing edge vertex_color_list = [ palette.get(g_plot.vs[i]['i_cluster']) for i in range(n_vertics) ] # configure the vertex color edge_color_list = [ palette.get(g_plot.es[i]['i_cluster']) for i in range(n_edges) ] # configure the edge color visual_style = {} layout = Layout(list(coord_xy)) layout.mirror(1) visual_style["layout"] = layout visual_style["vertex_label"] = g_plot.vs["name"] visual_style["vertex_color"] = vertex_color_list visual_style["edge_color"] = edge_color_list #visual_style["edge_width"] = [1 + 2 * int(is_formal) for is_formal in g.es["is_formal"]] plot(g_plot, "network.png", **visual_style) plot(g_plot, **visual_style)
def testIndexing(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) self.assertEqual(len(layout), 4) self.assertEqual(layout[1], [0, 1, 0]) self.assertEqual(layout[3], [2, 1, 3]) row = layout[2] row[2] = 1 self.assertEqual(layout[2], [1, 0, 1]) del layout[1] self.assertEqual(len(layout), 3)
def demo_epidemic(): from igraph import Graph, Layout from random import sample, random # Specify the simulation parameters initial_outbreak_size = 3 spread_probability = 0.05 recovery_probability = 0.1 # Set up the mapping from vertex states to colors colormap = dict(S="white", I="red", R="green") # Generate the graph graph, xs, ys = Graph.GRG(100, 0.2, return_coordinates=True) layout = Layout(zip(xs, ys)) # Set up the initial state of the individuals graph.vs["state"] = ["S"] * graph.vcount() for vertex in sample(graph.vs, initial_outbreak_size): vertex["state"] = "I" graph.vs["size"] = [20] * graph.vcount() # Set up the video encoder encoder = MEncoderVideoEncoder(bbox=(600, 600), fps=5) # Generate frames in the animation one by one with encoder.encode_to("demo_epidemic.avi"): # Run the simulation until hell freezes over while True: # Create the plot and add to the encoder colors = [colormap[state] for state in graph.vs["state"]] encoder.add(graph, layout=layout, vertex_color=colors, vertex_label=graph.vs["state"], margin=20) # First, the infected individuals try to infect their neighbors infected = graph.vs.select(state="I") for vertex in infected: for idx in graph.neighbors(vertex.index): if graph.vs[idx]["state"] == "S" and random( ) < spread_probability: graph.vs[idx]["state"] = "I" # Second, the infected individuals try to recover for vertex in infected: if random() < recovery_probability: vertex["state"] = "R" # Okay, are there any infected people left? if not infected: break
elif graph.degree(v.index) <= 304: v["size"] = 20 elif graph.degree(v.index) <= 1216: v["size"] = 15 elif graph.degree(v.index) <= 4864: v["size"] = 12 elif graph.degree(v.index) <= 19456: v["size"] = 10 else: v["size"] = 6 shells = list(set(coreness)) shells.sort() layout = Layout(dim=2) frequency = {} for item in coreness: if item not in frequency: frequency[item] = 0 frequency[item] += 1 for shell in shells: print(f"Calculating {shell} from {len(shells)-1}") v = (1 - ((shell) / max(shells))) nodes_in_shell = frequency[shell] angles = [] angle = 0 while angle <= 360.1: angles.append(angle)
def testBoundingBox(self): layout = Layout([(0, 1), (2, 7)]) self.assertEqual(layout.bounding_box(), BoundingBox(0, 1, 2, 7)) self.assertEqual(layout.bounding_box(1), BoundingBox(-1, 0, 3, 8)) layout = Layout([]) self.assertEqual(layout.bounding_box(), BoundingBox(0, 0, 0, 0))
def _get_layout(g, coordinates): return g.layout(GRAPH_LAYOUT) if coordinates is None else Layout( coordinates.tolist())
def testBoundingBox(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) self.assertEqual(layout.bounding_box(), (0, 0, 0, 2, 1, 3)) self.assertEqual(layout.bounding_box(1), (-1, -1, -1, 3, 2, 4))
def testBoundaries(self): layout = Layout([(0, 0, 1), (0, 1, 0), (1, 0, 0), (2, 1, 3)]) self.assertEqual(layout.boundaries(), ([0, 0, 0], [2, 1, 3])) self.assertEqual(layout.boundaries(1), ([-1, -1, -1], [3, 2, 4]))