/
testgraphelement.py
76 lines (62 loc) · 3.21 KB
/
testgraphelement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Unit tests of Graph Element.
"""
import numpy as np
from holoviews.element.graphs import Graph, Nodes, circular_layout
from holoviews.element.comparison import ComparisonTestCase
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_select_by_node(self):
graph = Graph(((self.source, self.target),))
selection = Graph(([(1, 0), (2, 0)], list(zip(*self.nodes))[0:3]))
print(graph.select(index=(1, 3)).data)
self.assertEqual(graph.select(index=(1, 3)), 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'))