def test_PathCollection_remove_edges(): """Remove edge path from the path collection.""" a = Node('a') b = Node('b') c = Node('c') e = Edge(a, b, uid='e') f = Edge(b, c, uid='f') paths = PathCollection() paths.add(e, f, uid='p1') paths.remove(e, f) assert len(paths) == 0 assert 'p1' not in paths paths.add(e, f, uid='p1') paths.remove('p1') assert len(paths) == 0 paths.add(e, f, uid='p1') paths.remove(e, f) assert len(paths) == 0 paths.add(e, f, uid='p1') paths.remove('e', 'f') assert len(paths) == 0
def test_add_node(): """Test the node assignment.""" # add string and create new node in the network net = Network() net.add_node('v', color='red') assert len(net.nodes) == 1 assert 'v' in net.nodes.uids assert net.nodes['v']['color'] == 'red' assert net.nodes.index['v'] == 0 assert net.number_of_nodes() == 1 assert isinstance(net.nodes['v'], Node) assert net.nodes['v'].uid == 'v' w = Node('w', color='green') net.add_node(w) assert net.number_of_nodes() == 2 assert isinstance(net.nodes['w'], Node) assert net.nodes['w'].uid == 'w' assert net.nodes['w']['color'] == 'green' v = Node('v', color='blue') with pytest.raises(Exception): net.add_node(v)
def test_PathCollection_add_path(): """Add path to the path collection.""" a = Node('a') b = Node('b') c = Node('c') e = Edge(a, b, uid='e') f = Edge(b, c, uid='f') p1 = Path(e, f, uid='p1') p2 = Path(e, uid='p2') p3 = Path(a, uid='p3') paths = PathCollection() paths.add(p1) paths.add(p1) assert paths.counter['p1'] == 2 assert len(paths.nodes) == 2 # assert len(paths.edges) == 2 assert len(paths) == 1 assert p1 in paths paths = PathCollection() paths.add(p1, p2) assert p1 in paths assert p2 in paths
def test_PathCollection_add_edges(): """Add edge path to the path collection.""" a = Node('a') b = Node('b') c = Node('c') e = Edge(a, b, uid='e') f = Edge(b, c, uid='f') paths = PathCollection() paths.add(e, f, uid='p1') assert len(paths.nodes) == 2 # assert len(paths.edges) == 2 assert len(paths) == 1 assert 'p1' in paths paths.add(e, f, uid='p1') assert paths.counter['p1'] == 2 paths.add(e, f) assert paths.counter['p1'] == 3 with pytest.raises(Exception): paths.add(e, f, uid='p2') assert paths.counter['p1'] == 3
def test_add_edges(): """Test assigning edges form a list.""" net = Network() ab = Edge(Node('a'), Node('b')) net.add_edges(ab, ('b', 'c')) assert net.number_of_edges() == 2 net = Network() edges = [("A", "B"), ("B", "C")] net.add_edges(edges) assert net.number_of_edges() == 2 assert net.number_of_nodes() == 3 net = Network() edges = [("a", "b"), ("b", "c"), ("c", "d"), ("c", "e")] edges = [tuple(Node(x) for x in e) for e in edges] # with pytest.raises(Exception): # net.add_edges(edges) net = Network() net.add_edges(('a', 'b'), ('b', 'c'))
def test_EdgeCollection_hyperedges(): """Test the EdgeCollection with hyperedges.""" edges = EdgeCollection(hyperedges=True) a = Node('a') b = Node('b') c = Node('c') d = Node('d') e = Edge({a, b}, {c, d}, uid='ab-cd', hyperedge=True) edges = EdgeCollection(hyperedges=True) edges.add(e) with pytest.raises(Exception): edges.add(e) with pytest.raises(Exception): edges.add({a, b}, {c, d}) assert edges[{a, 'b'}, {c, 'd'}] == e assert edges[{'b', a}, {'d', c}] == e with pytest.raises(Exception): print(edges[{a, b}, {c, 'x'}]) with pytest.raises(Exception): print(edges[{c, d}, {a, b}])
def test_higher_order_node(): """Test higher order nodes.""" a = Node('a', color='azure') b = Node('b', color='blue') c = Node('c', color='cyan') ab = Edge(a, b, uid='a-b') bc = Edge(b, c, uid='b-c') abc = HigherOrderNode(ab, bc, uid='abc') nodes = HigherOrderNodeCollection() nodes.add(abc) assert nodes[abc] == abc assert nodes['abc'] == abc assert nodes[a, b, c] == abc assert nodes['a', 'b', 'c'] == abc assert nodes[ab, bc] == abc assert nodes['a-b', 'b-c'] == abc assert abc in nodes assert 'abc' in nodes assert (a, b, c) in nodes assert ('a', 'b', 'c') in nodes assert (ab, bc) in nodes assert ('a-b', 'b-c') in nodes
def test_path(): """Test basic path""" a = Node('a') b = Node('b') c = Node('c') e = Edge(a, b) f = Edge(b, c) p = Path(e, f)
def test_NodeCollection(): """Test node collection""" nodes = NodeCollection() assert len(nodes) == 0 a = Node('a') nodes.add(a) assert len(nodes) == 1 assert nodes['a'] == a assert nodes[a] == a assert 'a' in nodes assert a in nodes assert 'a' in nodes.uids assert 'a' in nodes.keys() assert a in nodes.values() assert ('a', a) in nodes.items() assert {'a': a} == nodes.dict nodes.add(('b', 'c')) print(nodes) assert len(nodes) == 3 with pytest.raises(Exception): nodes.add('a') with pytest.raises(Exception): nodes.add(a) d = Node('d', color='blue') nodes.add(d) assert nodes['d']['color'] == 'blue' d['color'] = 'red' assert nodes['d']['color'] == 'red' nodes.add(['e', ('f', 'g'), ['h', 'i']]) assert len(nodes) == 9 nodes.remove(a) assert len(nodes) == 8 assert a not in nodes nodes.remove('b') assert len(nodes) == 7 assert 'b' not in nodes nodes.remove([('e', 'f', 'g'), 'h', ['i']]) assert len(nodes) == 2
def test_remove_edge(): """Test to remove an edge from the network.""" net = Network() a = Node('a') b = Node('b') c = Node('c') e = Edge(a, b, uid='e') f = Edge(b, a, uid='f') g = Edge(b, c, uid='g') net.add_edge(e) net.add_edge(f) net.remove_edge(g) assert net.number_of_edges() == 2 assert isinstance(net.edges['e'], Edge) assert g not in net.edges assert net.edges['a', 'b'] in net.edges assert net.successors['a'] == {b} assert net.outgoing['a'] == {e} assert net.incident_edges['a'] == {e, f} net.remove_edge(e) assert net.number_of_edges() == 1 assert net.successors['a'] == set() assert net.outgoing['a'] == set() assert net.incident_edges['a'] == {f} net.remove_edge('f') assert net.number_of_edges() == 0 assert net.incident_edges['a'] == set() a = Node('a') b = Node('b') e = Edge(a, b, uid='e') f = Edge(a, b, uid='f') g = Edge(a, b, uid='g') net = Network(multiedges=True) net.add_edges(e, f, g) assert net.number_of_edges() == 3 assert e and f and g in net.edges['a', 'b'] net.remove_edge('a', 'b', uid='g') assert net.number_of_edges() == 2 assert g not in net.edges['a', 'b'] net.remove_edge('a', 'b') assert net.number_of_edges() == 0 assert len(net.edges['a', 'b']) == 0
def test_update(): """Test update node attributes.""" u = Node('u', color='red') assert u['color'] == 'red' u.update(color='green', shape='rectangle') assert u['color'] == 'green' assert u['shape'] == 'rectangle'
def test_HyperEdge(): """Test an hyperedge.""" a = Node('a') b = Node('b') c = Node('c') d = Node('d') e = Node('e') h0 = HyperEdge(a, b, c, uid='h0') h1 = HyperEdge(a, c, d, uid='h1') h2 = HyperEdge(b, d, e, uid='h2')
def create_nodes_attributes(pathpy=True, number=5): """ Create Nodes with attributes. """ attr = {str(a): a for a in range(number)} if pathpy: a = Node('a', **attr) else: a = {} a.update(uid='a', attributes={}) a['attributes'].update(**attr) return True
def test_hash(): """Test the hash of a node""" a = Node() b = Node('b') c = Node('b') # different objects assert a.__hash__() != b.__hash__() # different objects but same uid assert b.__hash__() != c.__hash__()
def test_higher_order_edge(): """Test higher order edges.""" a = Node('a', color='azure') b = Node('b', color='blue') c = Node('c', color='cyan') d = Node('d', color='desert') abc = HigherOrderNode(a, b, c, uid='abc') bcd = HigherOrderNode(b, c, d, uid='bcd') e1 = HigherOrderEdge(abc, bcd, uid='abc-bcd')
def add_nodes(pathpy=True, numbers=1000): """Add nodes to NodeCollection""" if pathpy: nodes = NodeCollection() for n in range(numbers): nodes << Node(n) else: nodes = {} for n in range(numbers): nodes[str(n)] = Node(n) return True
def test_copy(): """Test to make a copy of a node.""" u = Node('u', color='blue') v = u.copy() # same uid and attribtes assert v.uid == u.uid == 'u' assert v['color'] == 'blue' # different objects assert u != v
def test_fit_path_collection(): """Fit PathCollection to a HON""" paths = PathCollection() a = Node('a') b = Node('b') c = Node('c') d = Node('d') e = Node('e') # paths.add(a, c, d, uid='acd', frequency=10) # paths.add(b, c, e, uid='bce', frequency=10) paths.add('a', c, 'd', 'f', uid='acd', count=10) paths.add('b', c, 'e', 'g', uid='bce', count=10) # paths.add('a', 'c', 'd', uid='acd', frequency=10) # paths.add('b', 'c', 'e', uid='bce', frequency=10) # print(paths.counter) # hon = HigherOrderNetwork() # hon.fit(paths, order=3) # print(hon.nodes['xxx'].objects) # print(hon.nodes.counter) # for e in hon.edges: # print(e.first_order_relations) # print() # break paths = PathCollection() paths.add('a', 'c', 'b', uid='acb', count=10) paths.add('c', 'b', 'a', 'c', uid='cba', count=20) paths.add('a', 'b', 'a', 'c', uid='abac', count=30) # paths.add(a, 'b', 'c', 'd', 'e', 'f', uid='p1') # paths.add(a, 'b', 'c', 'd', 'e', 'x') hon = HigherOrderNetwork(uid='hon') hon.fit(paths, order=1) print(hon) # for n in hon.nodes: # print((n, hon.indegrees()[n.uid], hon.outdegrees()[n.uid])) ##p = paths['p1'].subpaths(min_length=0, max_length=None, paths=True) # # print(paths) # print(hon.observed) # print(hon.subpaths) # print(hon.edges.counter) print('no log', hon.likelihood(paths, log=False)) print('log', hon.likelihood(paths, log=True)) import numpy as np print(np.exp(hon.likelihood(paths, log=True)))
def test_hash(): """Test the hash of an edge""" a = Node('a') b = Node('b') c = Node('c') e1 = Edge(a, b) e2 = Edge(b, c) e3 = Edge(a, b) # different objects assert e1.__hash__() != e2.__hash__() # different objects but same uid assert e1.__hash__() != e3.__hash__()
def test_higher_order_edge_collection(): """Test HigherOrderEdgeCollection.""" a = Node('a', color='azure') b = Node('b', color='blue') c = Node('c', color='cyan') d = Node('d', color='desert') e = Node('e', color='desert') ab = Edge(a, b, uid='a-b') bc = Edge(b, c, uid='b-c') cd = Edge(c, d, uid='c-d') de = Edge(d, e, uid='d-e') abc = HigherOrderNode(ab, bc, uid='abc') bcd = HigherOrderNode(bc, cd, uid='bcd') cde = HigherOrderNode(cd, de, uid='cde') abc_bcd = HigherOrderEdge(abc, bcd, uid='abc-bcd') bcd_cde = HigherOrderEdge(bcd, cde, uid='bcd-cde') edges = HigherOrderEdgeCollection() edges.add(abc_bcd) assert edges[abc_bcd] == abc_bcd assert edges['abc-bcd'] == abc_bcd assert edges[abc, bcd] == abc_bcd assert edges['abc', 'bcd'] == abc_bcd assert edges[(a, b, c), (b, c, d)] == abc_bcd assert edges[('a', 'b', 'c'), ('b', 'c', 'd')] == abc_bcd assert edges[(ab, bc), (bc, cd)] == abc_bcd assert edges[('a-b', 'b-c'), ('b-c', 'c-d')] == abc_bcd # NotImplemented # edges['a','b','c','d'] # path node uids # edges['a-b','b-c','c-d'] # path edge uids # edges[a,b,c,d] # node objects # edges[ab,bc,cd] # edge objects assert abc_bcd in edges assert 'abc-bcd' in edges assert (abc, bcd) in edges assert ('abc', 'bcd') in edges assert ((a, b, c), (b, c, d)) in edges assert (('a', 'b', 'c'), ('b', 'c', 'd')) in edges assert ((ab, bc), (bc, cd)) in edges assert (('a-b', 'b-c'), ('b-c', 'c-d')) in edges
def test_setitem(): """Test the assignment of attributes.""" u = Node('u') u['color'] = 'blue' assert u['color'] == 'blue'
def create_edges(pathpy=True, iterations=1000): """ Create nodes without attributes. """ a = Node('a') b = Node('b') if pathpy: for i in range(iterations): e = Edge(a, b) else: for i in range(iterations): e = {} e.update(uid=None, v=a, w=b, nodes=set()) e['nodes'].add(a) e['nodes'].add(b) return True
def test_network_degrees(): """Test node degrees of a network""" a = Node('a') net = Network(directed=False) net.add_edges((a, 'b'), ('b', 'c'), ('c', 'a')) assert isinstance(list(net.nodes.keys())[0], (str, int))
def test_getitem(): """Test the extraction of attributes.""" u = Node('u', color='blue') assert u['color'] == 'blue' assert u['attribute not in dict'] is None
def test_EdgeCollection_for_HyperEdges(): """Test the EdgeCollection with hyperedges.""" a = Node('a') b = Node('b') c = Node('c') d = Node('d') e = HyperEdge({a, b}, {c, d}, uid='ab-cd') edges = EdgeCollection(hyperedges=False) with pytest.raises(Exception): edges.add(e) with pytest.raises(Exception): edges.add({a, b}, {c, d}) edges = EdgeCollection(hyperedges=True) edges.add(e) assert len(edges) == 1 assert e in edges assert len(edges.nodes) == 4 assert a and b and c and d in edges.nodes assert ({'a', 'b'}, {'c', 'd'}) in edges assert ({'b', 'a'}, {'c', 'd'}) in edges assert ({'a', 'b'}, {'d', 'c'}) in edges assert ({'b', 'a'}, {'d', 'c'}) in edges assert edges[{'a', 'b'}, {'c', 'd'}] == e assert edges[{'b', 'a'}, {'c', 'd'}] == e assert edges[{'a', 'b'}, {'d', 'c'}] == e assert edges[{'b', 'a'}, {'d', 'c'}] == e edges.add({a, 'c'}, {'b', d}, uid='ac-bd') assert len(edges) == 2 assert ({'c', 'a'}, {'b', 'd'}) in edges edges.remove({'a', 'b'}, {'c', 'd'}) assert len(edges) == 1 assert 'ab-cd' not in edges edges.remove('ac-bd') assert len(edges) == 0
def test_EdgeCollection_multiedges(): """Test the EdgeCollection""" edges = EdgeCollection(multiedges=True) assert len(edges) == 0 a = Node('a') b = Node('b') ab = Edge(a, b, uid='a-b') edges.add(ab) edges.add(a, b, uid='new') assert len(edges) == 2 assert edges['a-b'] == ab assert len(edges['a', 'b']) == 2 assert len(edges[a, b]) == 2
def test_get_edge(): """Test to get edges.""" net = Network(directed=False) net.add_edge('a', 'b') assert (('a', 'b') in net.edges) is True assert (('b', 'a') in net.edges) is True assert (('a', 'c') in net.edges) is False a = Node('a') b = Node('b') e = Edge(a, b) net = Network(directed=True) net.add_edge(e) assert ((a, b) in net.edges) is True assert (e in net.edges) is True assert (('a', b) in net.edges) is True assert ((b, a) in net.edges) is False
def test_uid(): """Test the uid assignment.""" u = Node('u') assert isinstance(u, Node) assert isinstance(u.uid, str) assert u.uid == 'u' v = Node(1) assert v.uid == '1' w = Node() assert isinstance(w, Node) assert isinstance(w.uid, str) assert w.uid == hex(id(w))
def test_PathCollection_add_nodes(): """Add node path to the path collection.""" a = Node('a') b = Node('b') c = Node('c') paths = PathCollection() paths.add(a, b, c, uid='p1') assert len(paths.nodes) == 3 # assert len(paths.edges) == 2 assert len(paths) == 1 assert 'p1' in paths paths.add(a, b, c, uid='p1') assert paths.counter['p1'] == 2 paths.add(a, b, c) assert paths.counter['p1'] == 3
def test_add_nodes(): """Test assigning notes form a list.""" net = Network() u = Node('u', color='blue') net.add_nodes(u, 'v', 'w', color='green') assert net.number_of_nodes() == 3 assert net.nodes['u']['color'] == 'green' assert net.nodes['v']['color'] == 'green' assert net.nodes['w']['color'] == 'green'