def setUp(self): N = 8 self.nodes = circular_layout(np.arange(N)) self.source = np.arange(N, dtype=np.int32) self.target = np.zeros(N, dtype=np.int32) self.edge_info = np.arange(N) self.graph = Graph(((self.source, self.target), ))
def test_from_networkx_dictionary_positions(self): import networkx as nx G = nx.Graph() G.add_nodes_from([1, 2, 3]) positions = nx.circular_layout(G) graph = Graph.from_networkx(G, positions) self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
def setUp(self): N = 8 self.nodes = circular_layout(np.arange(N)) self.source = np.arange(N, dtype=np.int32) self.target = np.zeros(N, dtype=np.int32) self.edge_info = np.arange(N) self.graph = Graph(((self.source, self.target),))
def test_from_networkx_with_invalid_edge_attrs(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1, 2, []), (1, 3, []), (2, 4, []), (3, 4, [])]) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.vdims, [])
def test_from_networkx_custom_nodes(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.375)]) nodes = Dataset([(1, 'A'), (2, 'B'), (3, 'A'), (4, 'B')], 'index', 'some_attribute') graph = Graph.from_networkx(FG, nx.circular_layout, nodes=nodes) self.assertEqual(graph.nodes.dimension_values('some_attribute'), np.array(['A', 'B', 'A', 'B']))
def test_from_networkx_with_edge_attrs(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)]) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.dimension_values('weight'), np.array([0.125, 0.75, 1.2, 0.375]))
def test_from_networkx_only_nodes(self): try: import networkx as nx except: raise SkipTest('Test requires networkx to be installed') G = nx.Graph() G.add_nodes_from([1, 2, 3]) graph = Graph.from_networkx(G, nx.circular_layout) self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
def test_from_networkx_with_invalid_node_attrs(self): import networkx as nx FG = nx.Graph() FG.add_node(1, test=[]) FG.add_node(2, test=[]) FG.add_edge(1, 2) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.nodes.vdims, []) self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2])) self.assertEqual(graph.array(), np.array([(1, 2)]))
def test_from_networkx_custom_nodes(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)]) nodes = Dataset([(1, 'A'), (2, 'B'), (3, 'A'), (4, 'B')], 'index', 'some_attribute') graph = Graph.from_networkx(FG, nx.circular_layout, nodes=nodes) self.assertEqual(graph.nodes.dimension_values('some_attribute'), np.array(['A', 'B', 'A', 'B']))
def test_from_networkx_with_edge_attrs(self): try: import networkx as nx except: raise SkipTest('Test requires networkx to be installed') FG = nx.Graph() FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)]) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.dimension_values('weight'), np.array([0.125, 0.75, 1.2, 0.375]))
def test_from_networkx_with_node_attrs(self): import networkx as nx G = nx.karate_club_graph() graph = Graph.from_networkx(G, nx.circular_layout) clubs = np.array([ 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer']) self.assertEqual(graph.nodes.dimension_values('club'), clubs)
def test_from_networkx_with_node_attrs(self): import networkx as nx G = nx.karate_club_graph() graph = Graph.from_networkx(G, nx.circular_layout) clubs = np.array([ 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer' ]) self.assertEqual(graph.nodes.dimension_values('club'), clubs)
def test_from_networkx_with_node_attrs(self): try: import networkx as nx except: raise SkipTest('Test requires networkx to be installed') G = nx.karate_club_graph() graph = Graph.from_networkx(G, nx.circular_layout) clubs = np.array([ 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Officer', 'Mr. Hi', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Mr. Hi', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer', 'Officer' ]) self.assertEqual(graph.nodes.dimension_values('club'), clubs)
def generateGraph(): df = processCSVMatrix(file) names = df.columns # submatrix for quicker development if (len(names) > 150): df = df.head(150)[names[0:150]] # set defaults for HoloViews extension('bokeh') renderer('bokeh').webgl = True defaults = dict(width=400, height=400, padding=0.1) opts.defaults(opts.EdgePaths(**defaults), opts.Graph(**defaults), opts.Nodes(**defaults)) G = from_pandas_adjacency(df) graph = Graph.from_networkx(G, circular_layout).opts(directed=False, width=600, height=600, arrowhead_length=0.0005) # Make a panel and widgets with param for choosing a layout return pn.Column(graph)
def test_constructor_with_nodes_and_paths(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths graph = Graph(((self.source, self.target), self.nodes, paths.data)) self.assertEqual(graph._edgepaths, paths)
class GraphTests(ComparisonTestCase): def setUp(self): N = 8 self.nodes = circular_layout(np.arange(N)) self.source = np.arange(N, dtype=np.int32) self.target = np.zeros(N, dtype=np.int32) self.edge_info = np.arange(N) self.graph = Graph(((self.source, self.target),)) def test_basic_constructor(self): graph = Graph(((self.source, self.target),)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_constructor_with_nodes(self): graph = Graph(((self.source, self.target), self.nodes)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_graph_edge_segments(self): segments = connect_edges(self.graph) paths = [] nodes = np.column_stack(self.nodes) for start, end in zip(nodes[self.source], nodes[self.target]): paths.append(np.array([start[:2], end[:2]])) self.assertEqual(segments, paths) def test_graph_node_info_no_index(self): node_info = Dataset(np.arange(8), vdims=['Label']) graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(0)) def test_graph_node_info_no_index_mismatch(self): node_info = Dataset(np.arange(6), vdims=['Label']) with self.assertRaises(ValueError): Graph(((self.source, self.target), node_info)) def test_graph_node_info_merge_on_index(self): node_info = Dataset((np.arange(8), np.arange(1,9)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(1)) @pd_skip def test_graph_node_info_merge_on_index_partial(self): node_info = Dataset((np.arange(5), np.arange(1,6)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) expected = np.array([1., 2., 3., 4., 5., np.NaN, np.NaN, np.NaN]) self.assertEqual(graph.nodes.dimension_values(3), expected) @pd_skip def test_graph_edge_segments_pd(self): segments = connect_edges_pd(self.graph) paths = [] nodes = np.column_stack(self.nodes) for start, end in zip(nodes[self.source], nodes[self.target]): paths.append(np.array([start[:2], end[:2]])) self.assertEqual(segments, paths) def test_constructor_with_nodes_and_paths(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths graph = Graph(((self.source, self.target), self.nodes, paths.data)) self.assertEqual(graph._edgepaths, paths) def test_constructor_with_nodes_and_paths_dimension_mismatch(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths exception = 'Ensure that the first two key dimensions on Nodes and EdgePaths match: x != x2' with self.assertRaisesRegexp(ValueError, exception): Graph(((self.source, self.target), self.nodes, paths.redim(x='x2'))) def test_graph_clone_static_plot_id(self): self.assertEqual(self.graph.clone()._plot_id, self.graph._plot_id) def test_select_by_node_in_edges_selection_mode(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3])) self.assertEqual(graph.select(index=(1, 3)), selection) def test_select_by_node_in_nodes_selection_mode(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(1, 2)], list(zip(*self.nodes))[1:3])) self.assertEqual(graph.select(index=(1, 3), selection_mode='nodes'), selection) def test_select_by_source(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(0,0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_target(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(0,0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_source_and_target(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(0,1)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 3), end=1), selection) def test_select_by_edge_data(self): graph = Graph(((self.target, self.source, self.edge_info),), vdims=['info']) selection = Graph(([(0, 0, 0), (0, 1, 1)], list(zip(*self.nodes))[:2]), vdims=['info']) self.assertEqual(graph.select(info=(0, 2)), selection) def test_graph_node_range(self): graph = Graph(((self.target, self.source),)) self.assertEqual(graph.range('x'), (-1, 1)) self.assertEqual(graph.range('y'), (-1, 1)) def test_graph_redim_nodes(self): graph = Graph(((self.target, self.source),)) redimmed = graph.redim(x='x2', y='y2') self.assertEqual(redimmed.nodes, graph.nodes.redim(x='x2', y='y2')) self.assertEqual(redimmed.edgepaths, graph.edgepaths.redim(x='x2', y='y2'))
def test_graph_node_info_no_index(self): node_info = Dataset(np.arange(8), vdims=['Label']) graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(0))
def test_graph_node_info_merge_on_index(self): node_info = Dataset((np.arange(8), np.arange(1, 9)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(1))
class GraphTests(ComparisonTestCase): def setUp(self): N = 8 self.nodes = circular_layout(np.arange(N)) self.source = np.arange(N) self.target = np.zeros(N) self.edge_info = np.arange(N) self.graph = Graph(((self.source, self.target),)) def test_basic_constructor(self): graph = Graph(((self.source, self.target),)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_constructor_with_nodes(self): graph = Graph(((self.source, self.target), self.nodes)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_constructor_with_nodes_and_paths(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths graph = Graph(((self.source, self.target), self.nodes, paths.data)) nodes = Nodes(self.nodes) self.assertEqual(graph._edgepaths, paths) def test_constructor_with_nodes_and_paths_dimension_mismatch(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths exception = 'Ensure that the first two key dimensions on Nodes and EdgePaths match: x != x2' with self.assertRaisesRegexp(ValueError, exception): graph = Graph(((self.source, self.target), self.nodes, paths.redim(x='x2'))) def test_graph_clone_static_plot_id(self): self.assertEqual(self.graph.clone()._plot_id, self.graph._plot_id) def test_select_by_node_in_edges_selection_mode(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3])) self.assertEqual(graph.select(index=(1, 3)), selection) def test_select_by_node_in_nodes_selection_mode(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(1, 2)], list(zip(*self.nodes))[1:3])) self.assertEqual(graph.select(index=(1, 3), selection_mode='nodes'), selection) def test_select_by_source(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(0,0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_target(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(0,0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_source_and_target(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(0,1)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 3), end=1), selection) def test_select_by_edge_data(self): graph = Graph(((self.target, self.source, self.edge_info),), vdims=['info']) selection = Graph(([(0, 0, 0), (0, 1, 1)], list(zip(*self.nodes))[:2]), vdims=['info']) self.assertEqual(graph.select(info=(0, 2)), selection) def test_graph_node_range(self): graph = Graph(((self.target, self.source),)) self.assertEqual(graph.range('x'), (-1, 1)) self.assertEqual(graph.range('y'), (-1, 1)) def test_graph_redim_nodes(self): graph = Graph(((self.target, self.source),)) redimmed = graph.redim(x='x2', y='y2') self.assertEqual(redimmed.nodes, graph.nodes.redim(x='x2', y='y2')) self.assertEqual(redimmed.edgepaths, graph.edgepaths.redim(x='x2', y='y2'))
def test_basic_constructor(self): graph = Graph(((self.source, self.target), )) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes)
def test_from_networkx_with_edge_attrs(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.375)]) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.dimension_values('weight'), np.array([0.125, 0.75, 1.2, 0.375]))
class GraphTests(ComparisonTestCase): def setUp(self): N = 8 self.nodes = circular_layout(np.arange(N)) self.source = np.arange(N, dtype=np.int32) self.target = np.zeros(N, dtype=np.int32) self.edge_info = np.arange(N) self.graph = Graph(((self.source, self.target), )) def test_basic_constructor(self): graph = Graph(((self.source, self.target), )) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_constructor_with_nodes(self): graph = Graph(((self.source, self.target), self.nodes)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes) def test_graph_edge_segments(self): segments = connect_edges(self.graph) paths = [] nodes = np.column_stack(self.nodes) for start, end in zip(nodes[self.source], nodes[self.target]): paths.append(np.array([start[:2], end[:2]])) self.assertEqual(segments, paths) def test_graph_node_info_no_index(self): node_info = Dataset(np.arange(8), vdims=['Label']) graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(0)) def test_graph_node_info_no_index_mismatch(self): node_info = Dataset(np.arange(6), vdims=['Label']) with self.assertRaises(ValueError): Graph(((self.source, self.target), node_info)) def test_graph_node_info_merge_on_index(self): node_info = Dataset((np.arange(8), np.arange(1, 9)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) self.assertEqual(graph.nodes.dimension_values(3), node_info.dimension_values(1)) @pd_skip def test_graph_node_info_merge_on_index_partial(self): node_info = Dataset((np.arange(5), np.arange(1, 6)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) expected = np.array([1., 2., 3., 4., 5., np.NaN, np.NaN, np.NaN]) self.assertEqual(graph.nodes.dimension_values(3), expected) @pd_skip def test_graph_edge_segments_pd(self): segments = connect_edges_pd(self.graph) paths = [] nodes = np.column_stack(self.nodes) for start, end in zip(nodes[self.source], nodes[self.target]): paths.append(np.array([start[:2], end[:2]])) self.assertEqual(segments, paths) def test_constructor_with_nodes_and_paths(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths graph = Graph(((self.source, self.target), self.nodes, paths.data)) self.assertEqual(graph._edgepaths, paths) def test_constructor_with_nodes_and_paths_dimension_mismatch(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths exception = 'Ensure that the first two key dimensions on Nodes and EdgePaths match: x != x2' with self.assertRaisesRegex(ValueError, exception): Graph( ((self.source, self.target), self.nodes, paths.redim(x='x2'))) def test_graph_clone_static_plot_id(self): self.assertEqual(self.graph.clone()._plot_id, self.graph._plot_id) def test_select_by_node_in_edges_selection_mode(self): graph = Graph(((self.source, self.target), )) selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3])) self.assertEqual(graph.select(index=(1, 3)), selection) def test_select_by_node_in_nodes_selection_mode(self): graph = Graph(((self.source, self.source + 1), self.nodes)) selection = Graph(([(1, 2)], list(zip(*self.nodes))[1:3])) self.assertEqual(graph.select(index=(1, 3), selection_mode='nodes'), selection) def test_select_by_source(self): graph = Graph(((self.source, self.target), )) selection = Graph(([(0, 0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_target(self): graph = Graph(((self.source, self.target), )) selection = Graph(([(0, 0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection) def test_select_by_source_and_target(self): graph = Graph(((self.source, self.source + 1), self.nodes)) selection = Graph(([(0, 1)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 3), end=1), selection) def test_select_by_edge_data(self): graph = Graph(((self.target, self.source, self.edge_info), ), vdims=['info']) selection = Graph(([(0, 0, 0), (0, 1, 1)], list(zip(*self.nodes))[:2]), vdims=['info']) self.assertEqual(graph.select(info=(0, 2)), selection) def test_graph_node_range(self): graph = Graph(((self.target, self.source), )) self.assertEqual(graph.range('x'), (-1, 1)) self.assertEqual(graph.range('y'), (-1, 1)) def test_graph_redim_nodes(self): graph = Graph(((self.target, self.source), )) redimmed = graph.redim(x='x2', y='y2') self.assertEqual(redimmed.nodes, graph.nodes.redim(x='x2', y='y2')) self.assertEqual(redimmed.edgepaths, graph.edgepaths.redim(x='x2', y='y2'))
def test_select_by_node_in_nodes_selection_mode(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(1, 2)], list(zip(*self.nodes))[1:3])) self.assertEqual(graph.select(index=(1, 3), selection_mode='nodes'), selection)
def test_select_by_node_in_edges_selection_mode(self): graph = Graph(((self.source, self.target), )) selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3])) self.assertEqual(graph.select(index=(1, 3)), selection)
def test_select_by_node_in_edges_selection_mode(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3])) self.assertEqual(graph.select(index=(1, 3)), selection)
def test_constructor_with_nodes_and_paths_dimension_mismatch(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths exception = 'Ensure that the first two key dimensions on Nodes and EdgePaths match: x != x2' with self.assertRaisesRegexp(ValueError, exception): Graph(((self.source, self.target), self.nodes, paths.redim(x='x2')))
def test_from_networkx_with_invalid_edge_attrs(self): import networkx as nx FG = nx.Graph() FG.add_weighted_edges_from([(1,2,[]), (1,3,[]), (2,4,[]), (3,4,[])]) graph = Graph.from_networkx(FG, nx.circular_layout) self.assertEqual(graph.vdims, [])
def test_select_by_source_and_target(self): graph = Graph(((self.source, self.source+1), self.nodes)) selection = Graph(([(0,1)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 3), end=1), selection)
def test_select_by_source_and_target(self): graph = Graph(((self.source, self.source + 1), self.nodes)) selection = Graph(([(0, 1)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 3), end=1), selection)
def test_from_networkx_only_nodes(self): import networkx as nx G = nx.Graph() G.add_nodes_from([1, 2, 3]) graph = Graph.from_networkx(G, nx.circular_layout) self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))
def test_select_by_edge_data(self): graph = Graph(((self.target, self.source, self.edge_info), ), vdims=['info']) selection = Graph(([(0, 0, 0), (0, 1, 1)], list(zip(*self.nodes))[:2]), vdims=['info']) self.assertEqual(graph.select(info=(0, 2)), selection)
def test_select_by_target(self): graph = Graph(((self.source, self.target),)) selection = Graph(([(0,0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection)
def test_graph_node_range(self): graph = Graph(((self.target, self.source), )) self.assertEqual(graph.range('x'), (-1, 1)) self.assertEqual(graph.range('y'), (-1, 1))
def make_plot(self): from graphion.session.handler import get_directed # dependency cycle fix if get_directed(self.sid): G = from_pandas_adjacency(df, create_using=DiGraph) else: G = from_pandas_adjacency(df, create_using=Graph) self.nodeCount = number_of_nodes(G) """ Create NetworkX graph layout manager """ if diagramType == "FORCE": layout = spring_layout(G, k=10.42 / sqrt(self.nodeCount), seed=server.config['SEED']) elif diagramType == "HIERARCHICAL": if self.nodeCount > 1: layout = graphviz_layout(Graph([ (u, v, d) for u, v, d in G.edges(data=True) ]), prog='dot') else: layout = circular_layout( G ) # graphviz_layout does not work with one node, just display a "circular_layout" elif diagramType == "RADIAL": layout = circular_layout(G) else: pass # get node and edge information from graph nodes, nodes_coordinates = zip(*sorted(layout.items())) nodes_x, nodes_y = list(zip(*nodes_coordinates)) # calculate centrality centrality = degree_centrality(G) _, nodeCentralities = zip(*sorted(centrality.items())) if self.nodeCount > 1: # get degree information if is_directed(G): inDegreeSize = dict(G.in_degree) inDegree = inDegreeSize.copy() outDegreeSize = dict(G.out_degree) outDegree = outDegreeSize.copy() totalDegreeSize = {} for n in nodes: totalDegreeSize[n] = inDegreeSize[n] + outDegreeSize[n] totalDegree = totalDegreeSize.copy() else: inDegreeSize = dict(G.degree) inDegree = inDegreeSize.copy() outDegreeSize = inDegreeSize.copy() outDegree = inDegreeSize.copy() totalDegreeSize = inDegreeSize.copy() totalDegree = inDegreeSize.copy() # get weight information if is_directed(G): inWeightSize = dict(G.in_degree(weight='weight')) inWeight = inWeightSize.copy() outWeightSize = dict(G.out_degree(weight='weight')) outWeight = outWeightSize.copy() totalWeightSize = {} for n in nodes: totalWeightSize[n] = inWeightSize[n] + outWeightSize[n] totalWeight = totalWeightSize.copy() else: inWeightSize = dict(G.degree(weight='weight')) inWeight = inWeightSize.copy() outWeightSize = inWeightSize.copy() outWeight = inWeightSize.copy() totalWeightSize = inWeightSize.copy() totalWeight = inWeightSize.copy() # Creating a scale to ensure that the node sizes don't go bananas minNodeSize = 0.1 # minNodeSize * maxNodeSize = minimum node size maxIn = -maxsize - 1 minIn = maxsize maxOut = -maxsize - 1 minOut = maxsize maxTot = -maxsize - 1 minTot = maxsize maxInw = -maxsize - 1 minInw = maxsize maxOutw = -maxsize - 1 minOutw = maxsize maxTotw = -maxsize - 1 minTotw = maxsize for n in nodes: ind = inDegreeSize[n] outd = outDegreeSize[n] totd = totalDegreeSize[n] inw = inWeightSize[n] outw = outWeightSize[n] totw = totalWeightSize[n] if ind > maxIn: maxIn = ind elif ind < minIn: minIn = ind if outd > maxOut: maxOut = outd elif outd < minOut: minOut = outd if totd > maxTot: maxTot = totd elif totd < minTot: minTot = totd if inw > maxInw: maxInw = inw elif inw < minInw: minInw = inw if outw > maxOutw: maxOutw = outw elif outw < minOutw: minOutw = outw if totw > maxTotw: maxTotw = totw elif totw < minTotw: minTotw = totw if maxIn == minIn: sameInDegree = True else: sameInDegree = False for n in nodes: result = (inDegreeSize[n] - minIn) / maxIn if result < minNodeSize: inDegreeSize[n] = minNodeSize else: inDegreeSize[n] = result if maxOut == minOut: sameOutDegree = True else: sameOutDegree = False for n in nodes: result = (outDegreeSize[n] - minOut) / maxOut if result < minNodeSize: outDegreeSize[n] = minNodeSize else: outDegreeSize[n] = result if maxTot == minTot: sameTotalDegree = True else: sameTotalDegree = False for n in nodes: result = (totalDegreeSize[n] - minTot) / maxTot if result < minNodeSize: totalDegreeSize[n] = minNodeSize else: totalDegreeSize[n] = result if maxInw == minInw: sameInWeight = True else: sameInWeight = False for n in nodes: result = (inWeightSize[n] - minInw) / maxInw if result < minNodeSize: inWeightSize[n] = minNodeSize else: inWeightSize[n] = result if maxOutw == minOutw: sameOutWeight = True else: sameOutWeight = False for n in nodes: result = (outWeightSize[n] - minOutw) / maxOutw if result < minNodeSize: outWeightSize[n] = minNodeSize else: outWeightSize[n] = result if maxTotw == minTotw: sameTotalWeight = True else: sameTotalWeight = False for n in nodes: result = (totalWeightSize[n] - minTotw) / maxTotw if result < minNodeSize: totalWeightSize[n] = minNodeSize else: totalWeightSize[n] = result # Making a dictionary for all attributes, and ensuring none of the values go crazy. attributes = {} maxNodeSize = 30 for n in nodes: outd = outDegreeSize[n] totd = totalDegreeSize[n] inw = inWeightSize[n] outw = outWeightSize[n] totw = totalWeightSize[n] if sameInDegree: ind = 1 else: ind = inDegreeSize[n] if sameOutDegree: outd = 1 else: outd = outDegreeSize[n] if sameTotalDegree: totd = 1 else: totd = totalDegreeSize[n] if sameInWeight: inw = 1 else: inw = inWeightSize[n] if sameOutWeight: outw = 1 else: outw = outWeightSize[n] if sameTotalWeight: totw = 1 else: totw = totalWeightSize[n] attributes[n] = { 'indegreesize': ind * maxNodeSize, 'outdegreesize': outd * maxNodeSize, 'totaldegreesize': totd * maxNodeSize, 'inweightsize': inw * maxNodeSize, 'outweightsize': outw * maxNodeSize, 'totalweightsize': totw * maxNodeSize, 'indegree': inDegree[n], 'outdegree': outDegree[n], 'totaldegree': totalDegree[n], 'inweight': inWeight[n], 'outweight': outWeight[n], 'totalweight': totalWeight[n], 'count': 0 } set_node_attributes(G, attributes) plot = HVGraph.from_networkx(G, layout).opts( directed=get_directed(self.sid), arrowhead_length=0.01) # disabling displaying all node info on hovering over the node tooltips = [('Index', '@index'), ('In-Degree', '@indegree'), ('Out-Degree', '@outdegree'), ('Total Degree', '@totaldegree'), ('In Edge Weight', '@inweight'), ('Out Edge-Weight', '@outweight'), ('Total Edge-Weight', '@totalweight')] hover = HoverTool(tooltips=tooltips) else: attributes = {} for n in nodes: attributes[n] = { 'indegreesize': 1, 'outdegreesize': 1, 'totaldegreesize': 1, 'inweightsize': 1, 'outweightsize': 1, 'totalweightsize': 1, 'indegree': 0, 'outdegree': 0, 'totaldegree': 0, 'inweight': 0, 'outweight': 0, 'totalweight': 0, 'count': 0 } set_node_attributes(G, attributes) plot = HVGraph.from_networkx(G, layout).opts( directed=get_directed(self.sid), arrowhead_length=0.01) tooltips = [('Index', '@index'), ('In-Degree', '@indegree'), ('Out-Degree', '@outdegree'), ('Total Degree', '@totaldegree'), ('In Edge Weight', '@inweight'), ('Out Edge-Weight', '@outweight'), ('Total Edge-Weight', '@totalweight')] hover = HoverTool(tooltips=tooltips) # Make custom dictionary with color palettes for c in self.colorList: if c == 'cividis': self.colorMap[c] = Cividis256 elif c == 'viridis': self.colorMap[c] = Viridis256 elif c == 'inferno': self.colorMap[c] = Inferno256 else: self.colorMap[c] = palette[c] if max(nodeCentralities) > 0: if datashaded and self.nodeCount > 1: plot = bundle_graph(plot) points = plot.nodes points.opts(cmap=self.colorMap[self.color_palette], color=self.node_color, size=self.node_size, tools=['box_select', 'lasso_select', 'tap', hover], active_tools=['wheel_zoom'], toolbar='above', show_legend=False, width=self.size, height=self.size) plot.opts(node_size=0, node_color=None, node_line_width=0, node_hover_fill_color='green') return plot, points
def test_graph_redim_nodes(self): graph = Graph(((self.target, self.source), )) redimmed = graph.redim(x='x2', y='y2') self.assertEqual(redimmed.nodes, graph.nodes.redim(x='x2', y='y2')) self.assertEqual(redimmed.edgepaths, graph.edgepaths.redim(x='x2', y='y2'))
def test_constructor_with_nodes(self): graph = Graph(((self.source, self.target), self.nodes)) nodes = Nodes(self.nodes) self.assertEqual(graph.nodes, nodes)
def test_graph_redim_nodes(self): graph = Graph(((self.target, self.source),)) redimmed = graph.redim(x='x2', y='y2') self.assertEqual(redimmed.nodes, graph.nodes.redim(x='x2', y='y2')) self.assertEqual(redimmed.edgepaths, graph.edgepaths.redim(x='x2', y='y2'))
def test_graph_node_info_no_index_mismatch(self): node_info = Dataset(np.arange(6), vdims=['Label']) with self.assertRaises(ValueError): Graph(((self.source, self.target), node_info))
def test_graph_node_range(self): graph = Graph(((self.target, self.source),)) self.assertEqual(graph.range('x'), (-1, 1)) self.assertEqual(graph.range('y'), (-1, 1))
def test_graph_node_info_merge_on_index_partial(self): node_info = Dataset((np.arange(5), np.arange(1, 6)), 'index', 'label') graph = Graph(((self.source, self.target), node_info)) expected = np.array([1., 2., 3., 4., 5., np.NaN, np.NaN, np.NaN]) self.assertEqual(graph.nodes.dimension_values(3), expected)
def test_select_by_edge_data(self): graph = Graph(((self.target, self.source, self.edge_info),), vdims=['info']) selection = Graph(([(0, 0, 0), (0, 1, 1)], list(zip(*self.nodes))[:2]), vdims=['info']) self.assertEqual(graph.select(info=(0, 2)), selection)
def test_constructor_with_nodes_and_paths_dimension_mismatch(self): paths = Graph(((self.source, self.target), self.nodes)).edgepaths exception = 'Ensure that the first two key dimensions on Nodes and EdgePaths match: x != x2' with self.assertRaisesRegex(ValueError, exception): Graph( ((self.source, self.target), self.nodes, paths.redim(x='x2')))
def test_select_by_node_in_nodes_selection_mode(self): graph = Graph(((self.source, self.source + 1), self.nodes)) selection = Graph(([(1, 2)], list(zip(*self.nodes))[1:3])) self.assertEqual(graph.select(index=(1, 3), selection_mode='nodes'), selection)
def test_select_by_target(self): graph = Graph(((self.source, self.target), )) selection = Graph(([(0, 0), (1, 0)], list(zip(*self.nodes))[:2])) self.assertEqual(graph.select(start=(0, 2)), selection)