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_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_properties(): """Test network properties.""" net = Network(directed=False) net.add_edge('a', 'b', uid='a-b') net.edges['a-b']['color'] = 'red' assert net.edges['a-b']['color'] == 'red'
def test_network_plot(): """Test the plot function on a network.""" net = Network() net.add_node('a', color='red') net.add_node('b', size=40) net.add_edge('a', 'b', uid='a-b', color='blue') net.plot(filename='simple_plot.html', node_color={'a': 'green'})
def test_mean_degree(): """Test the mean degree calculation.""" # net = pp.generators.Molloy_Reed([2]*500) net = Network(directed=False) net.add_edges(('a', 'b'), ('b', 'c'), ('c', 'a')) mean_degree = pp.statistics.mean_degree(net) assert mean_degree == 2.0
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'
def test_directed(): """Test if a network is directed.""" net = Network() assert net.directed is True net = Network(directed=False) assert net.directed is False
def test_update(): """Test update network attributes.""" net = Network(city='London') assert net['city'] == 'London' net.update(city='Vienna', year='1850') assert net['city'] == 'Vienna' assert net['year'] == '1850'
def test_network_edges(): """Test the edges of a network""" net = Network(directed=False) net.add_edges(('a', 'b'), ('b', 'c'), ('c', 'd')) assert net.number_of_edges() == 3 assert isinstance(list(net.edges), list) # np does not allow to sample from iterables # edge = np.random.choice(list(net.edges.values())) edge = random.choice(list(net.edges)) assert edge in net.edges
def test_adjacency_matrix(): """Test the adjacency matrix of a network.""" net = Network() net.add_edges(('a', 'b'), ('b', 'c')) A1 = net.adjacency_matrix() assert A1[0, 1] == 1.0 assert A1[1, 2] == 1.0 A2 = pp.algorithms.matrices.adjacency_matrix(net) assert A2[0, 1] == 1.0 assert A2[1, 2] == 1.0
def test_uid(): """Test the uid assignment.""" net = Network(uid='test') assert isinstance(net, Network) assert isinstance(net.uid, str) assert net.uid == 'test' net = Network() assert isinstance(net, Network) assert isinstance(net.uid, str)
def test_adjacency_matrix(): """Test the adjacency matrix of a network.""" net = Network() net.add_edges(('a', 'b'), ('b', 'c')) n = net.nodes.index A1 = net.adjacency_matrix() assert A1[n['a'], n['b']] == 1.0 assert A1[n['b'], n['c']] == 1.0 A2 = pp.algorithms.matrices.adjacency_matrix(net) assert A2[n['a'], n['b']] == 1.0 assert A2[n['b'], n['c']] == 1.0
def test_getitem(): """Test the extraction of attributes.""" net = Network(city='London') assert net['city'] == 'London' assert net['attribute not in dict'] == None
def test_setitem(): """Test the assignment of attributes.""" net = Network() net['city'] = 'Zurich' assert net['city'] == 'Zurich'
def __init__(self, paths, max_order=1, model_selection=True): """Initialise MOGen.""" self.paths = {tuple(x.uid for x in p.nodes): paths[p]['frequency'] for p in paths} self.network = Network() for e in paths.edges: self.network.add_edge(e) self.max_order = max_order self.model_selection = model_selection # initialise variables self.optimal_maximum_order = None self.A = None self.T = None self.log_L = None self.dof = None self.AIC = None self.models = collections.defaultdict(lambda: {}) self.log_L_offset = None
def test_largest_connected_component(): """Test to find the largest connected component.""" net = Network(directed=False) net.add_edge('a', 'b') net.add_edge('b', 'c') net.add_edge('x', 'y') lcc = pp.algorithms.components.largest_connected_component(net)
def test_find_connected_components(): """Test to find the connected components.""" net = Network(directed=False) net.add_edge('a', 'b') net.add_edge('b', 'c') net.add_edge('x', 'y') cn = pp.algorithms.components.find_connected_components(net)
def test_network_properties(): """Test network properties.""" net = Network() net.add_edge('a', 'b', uid='a-b') net.add_edge('b', 'c', uid='b-c') net.add_edge('c', 'a', uid='c-a') assert net.successors['c'] == {net.nodes['a']} assert net.incoming['a'] == {net.edges['c-a']} net.remove_edge('c-a') assert net.successors['c'] == set() assert net.incoming['a'] == set()
def test_network_undirected(): """Test undirected networks""" net = Network(directed=False) net.add_edge('a', 'b', timestamp=1, color='red', size=4) net.add_edge('b', 'a', timestamp=3, color='blue', frequency=30) assert net.number_of_edges() == 1 assert net.edges['a', 'b']['color'] == 'blue' assert net.edges['b', 'a']['size'] == 4 assert net.edges['a', 'b']['timestamp'] == 3
def get_metric(network: pp.Network, metric: str): switcher = { 'num_vertices': network.number_of_nodes(), 'num_edges': network.number_of_edges(), 'is_directed': network.directed, 'average_degree': pp.statistics.mean_degree(network), 'degree_assortativity': pp.statistics.degree_assortativity(network), 'global_clustering_coefficient': pp.statistics.avg_clustering_coefficient(network), 'diameter': pp.algorithms.diameter(network), 'edge_reciprocity': pp.statistics.edge_reciprocity(network), 'largest_component_fraction': pp.algorithms.largest_component_size(network) / network.number_of_nodes() } return switcher.get(metric, None)
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_sql_write_network(): """Write network to sql database.""" net = Network() net.add_edges(('a', 'b'), ('a', 'c')) pp.io.sql.write(net, filename='network.db', table='test', if_exists='replace') net = pp.io.sql.read_network(db_file='network.db', table='test') assert isinstance(net, Network) assert net.number_of_nodes() == 3 assert net.number_of_edges() == 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_plot_empty_network_plot(): """Test the plot function on a network.""" net = Network() net.plot()
def test_shape(): """Test the shape of the network.""" net = Network() assert net.shape == (0, 0)
def test_remove_node(): """Test to remove a node from the network.""" net = Network(directed=True) net.add_edge('a', 'b') net.add_edge('a', 'c') net.add_edge('b', 'd') net.add_edge('b', 'e') net.add_edge('d', 'b') net.add_edge('d', 'e') net.add_edge('e', 'd') assert net.shape == (5, 7) net.remove_node('b') assert net.shape == (4, 3)
def test_isub_networks(): """Test to remove a network with isub""" net_1 = Network() net_2 = Network() net_1.add_edge('a', 'b', uid='a-b') net_2.add_edge('c', 'd', uid='c-d') net_1 += net_2 net_2.add_edge('d', 'e', uid='d-e') net_1 -= net_2 assert net_1.number_of_nodes() == 2 assert net_1.number_of_edges() == 1 assert 'a' and 'b' in net_1.nodes assert 'a-b' in net_1.edges assert net_2.number_of_nodes() == 3 assert net_2.number_of_edges() == 2
def test_sub_networks(): """Test to remove a network""" net_1 = Network() net_2 = Network() net_1.add_edge('a', 'b', uid='a-b') net_2.add_edge('c', 'd', uid='c-d') net_1 += net_2 net_2.add_edge('d', 'e', uid='d-e') net_3 = net_1 - net_2 assert net_3.number_of_nodes() == 2 assert net_3.number_of_edges() == 1 assert 'a' and 'b' in net_3.nodes assert 'a-b' in net_3.edges assert net_1.number_of_nodes() == 4 assert net_1.number_of_edges() == 2 assert net_2.number_of_nodes() == 3 assert net_2.number_of_edges() == 2 net_4 = Network() net_4.add_edge('x', 'y', uid='x-y') net_5 = net_3 - net_4 assert net_5.number_of_nodes() == 2 assert net_5.number_of_edges() == 1 assert 'a' and 'b' in net_5.nodes assert 'a-b' in net_5.edges
def test_iadd_networks(): """Test to add networks together""" net_1 = Network() net_1.add_edges(('a', 'b'), ('b', 'c')) net_2 = Network() net_2.add_edges(('x', 'y'), ('y', 'z')) net_1 += net_2 assert net_1.number_of_nodes() == 6 assert net_1.number_of_edges() == 4 assert net_2.number_of_nodes() == 3 assert net_2.number_of_edges() == 2 # test same node objects a = Node('a') b = Node('b') c = Node('c') net_1 = Network() net_2 = Network() net_1.add_edge(a, b) net_2.add_edge(b, c) net_1 += net_2 assert net_1.number_of_nodes() == 3 assert net_1.number_of_edges() == 2 assert net_2.number_of_nodes() == 2 assert net_2.number_of_edges() == 1 # nodes with same uids but different objects net_1 = Network() net_2 = Network() net_1.add_edge(a, b) net_2.add_edge('b', c) with pytest.raises(Exception): net_1 += net_2 # test same edge objects a = Node('a') b = Node('b') c = Node('c') net_1 = Network() net_2 = Network() net_1.add_edge(a, b, uid='e1') net_2.add_edge(a, b, uid='e2') with pytest.raises(Exception): net_1 += net_2 # assert net_1.number_of_edges() == 2 # assert net_1.number_of_nodes() == 2 # assert 'e1' in net_1.edges and 'e2' in net_1.edges # edges with same uids but different objects net_1 = Network() net_2 = Network() net_1.add_edge(a, b, uid='e1') net_2.add_edge(a, b, uid='e1') with pytest.raises(Exception): net_1 += net_2 # add multiple networks net_1 = Network() net_2 = Network() net_3 = Network() net_1.add_edge('a', 'b') net_2.add_edge('c', 'd') net_3.add_edge('e', 'f') net_1 += net_2 + net_3 assert net_1.number_of_edges() == 3 assert net_1.number_of_nodes() == 6
def test_str(): """Test string representations of the network.""" net = Network() assert isinstance(net.summary(), str)