def test_vertices_in_e_not_v(self): e = pd.DataFrame([['ING', 'ABN'], ['BNP', 'ABN'], ['RAB', 'ABN'], ['ABN', 'BNP'], ['BNP', 'ING']], columns=['creditor', 'debtor']) with pytest.raises(Exception) as e_info: ge.Graph(self.v, e, v_id='name', src='creditor', dst='debtor') msg = 'Some source vertices are not in v.' assert e_info.value.args[0] == msg e = pd.DataFrame([['ING', 'ABN'], ['BNP', 'ABN'], ['ING', 'RAB'], ['ABN', 'BNP'], ['BNP', 'ING']], columns=['creditor', 'debtor']) with pytest.raises(Exception) as e_info: ge.Graph(self.v, e, v_id='name', src='creditor', dst='debtor') msg = 'Some destination vertices are not in v.' assert e_info.value.args[0] == msg
def test_vertices_with_no_edge(self): v = pd.DataFrame([['ING'], ['ABN'], ['BNP'], ['RAB']], columns=['name']) with pytest.warns(UserWarning, match='RAB vertex has no edges.'): ge.Graph(v, self.e, v_id='name', src='creditor', dst='debtor') v = pd.DataFrame([['ING'], ['ABN'], ['BNP'], ['RAB'], ['UBS']], columns=['name']) with pytest.warns(UserWarning, match=r' vertices have no edges.'): ge.Graph(v, self.e, v_id='name', src='creditor', dst='debtor')
def test_init_class(self): g = ge.Graph(self.v_s, self.e_s, v_id='name', src='creditor', dst='debtor', edge_label='type') assert isinstance(g, ge.sGraph) assert isinstance(g, ge.LabelGraph) assert g.num_edges == 4
def test_strength_by_label(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') s = np.rec.array([(0, 0, 1e6), (0, 1, 1e4 + 1e6 + 3e3), (0, 2, 1e4), (0, 3, 3e3), (1, 1, 2.3e7), (1, 2, 2.3e7), (2, 1, 7e5), (2, 3, 7e5), (3, 0, 4e5), (3, 1, 4e5)], dtype=[('label', np.uint8), ('id', np.uint8), ('value', np.float64)]) s_test = g.strength_by_label(get=True) assert np.all(s_test == s), s_test assert np.all(g.lv.strength == s), g.v.strength
def test_to_networkx_orig(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value', v_group='country') v = [(('ING', 'NL'), {'group': 'NL'}), (('ABN', 'NL'), {'group': 'NL'}), (('BNP', 'FR'), {'group': 'FR'}), (('BNP', 'IT'), {'group': 'IT'})] e = [(('ING', 'NL'), ('ABN', 'NL'), {'weight': 1e6, 'label': ('interbank', False)}), (('ABN', 'NL'), ('BNP', 'FR'), {'weight': 1e4, 'label': ('interbank', False)}), (('ABN', 'NL'), ('ING', 'NL'), {'weight': 4e5, 'label': ('external', True)}), (('BNP', 'FR'), ('ABN', 'NL'), {'weight': 2.3e7, 'label': ('external', False)}), (('BNP', 'IT'), ('ABN', 'NL'), {'weight': 3e3, 'label': ('interbank', False)}), (('BNP', 'IT'), ('ABN', 'NL'), {'weight': 7e5, 'label': ('interbank', True)})] gx = g.to_networkx(original=True) assert np.all(list(gx.nodes(data=True)) == v) assert np.all(list(gx.edges(data=True)) == e)
def test_num_edges_by_label(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR']) test_num = np.array([3, 1, 1, 1], dtype='u1') assert np.all(g.num_edges_label == test_num)
def test_init(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor', weight='value') assert isinstance(g, ge.sGraph) assert isinstance(g, ge.WeightedGraph) assert np.all(g.e == self._e), g.e
def test_to_sparse(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR']) mat = [np.array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 0]]), np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]), np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0]]), np.array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])] for i in range(len(mat)): assert np.all(g.adjacency_matrix()[i].toarray() == mat[i])
def test_multi_id_init(self): v = pd.DataFrame([['ING', 'NL'], ['ABN', 'NL'], ['BNP', 'FR'], ['BNP', 'IT']], columns=['name', 'country']) e = pd.DataFrame([['ING', 'NL', 'ABN', 'NL'], ['BNP', 'FR', 'ABN', 'NL'], ['ABN', 'NL', 'BNP', 'IT'], ['BNP', 'IT', 'ING', 'NL']], columns=['creditor', 'c_country', 'debtor', 'd_country']) g = ge.Graph(v, e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country']) test_e = np.sort(np.rec.array([(0, 1), (2, 1), (1, 3), (3, 0)], dtype=[('src', np.uint8), ('dst', np.uint8)])) test_dict = {('ING', 'NL'): 0, ('ABN', 'NL'): 1, ('BNP', 'FR'): 2, ('BNP', 'IT'): 3} assert np.all(g.e == test_e), g.e assert test_dict == g.id_dict
def test_in_strength(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor', weight='value') s_in = np.array([0, 1e6 + 1.7e5, 1e4]) s_test = g.in_strength(get=True) assert np.all(s_test == s_in), s_test assert np.all(g.v.in_strength == s_in), g.v.in_strength
def test_out_strength(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor', weight='value') s_out = np.array([1e6, 1e4, 1.7e5]) s_test = g.out_strength(get=True) assert np.all(s_test == s_out), s_test assert np.all(g.v.out_strength == s_out), g.v.out_strength
def test_to_sparse(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor', weight='value') mat = np.array([[0, 1e6, 0], [0, 0, 1e4], [0, 1.7e5, 0]]) assert np.all(g.adjacency_matrix().toarray() == mat)
def test_out_degree(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor') d_out = np.array([1, 1, 2]) d_test = g.out_degree(get=True) assert np.all(d_test == d_out), d_test assert np.all(g.v.out_degree == d_out), g.v.out_degree
def test_in_degree(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor') d_in = np.array([1, 2, 1]) d_test = g.in_degree(get=True) assert np.all(d_test == d_in), d_test assert np.all(g.v.in_degree == d_in), g.v.in_degree
def test_total_weight(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') assert g.total_weight == 1e6 + 2.3e7 + 7e5 + 3e3 + 1e4 + 4e5
def test_degree_init(self): v = pd.DataFrame([['ING'], ['ABN'], ['BNP'], ['RAB'], ['UBS']], columns=['name']) d = np.array([2, 2, 2, 0, 0]) with pytest.warns(UserWarning): g = ge.Graph(v, self.e, v_id='name', src='creditor', dst='debtor') assert np.all(g.v.degree == d), g.v.degree
def test_init(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor') test_e = np.sort(np.rec.array([(0, 1), (2, 1), (1, 2), (2, 0)], dtype=[('src', np.uint8), ('dst', np.uint8)])) assert isinstance(g, ge.sGraph) assert isinstance(g, ge.DirectedGraph) assert np.all(g.e == test_e), g.e
def test_vertex_group(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor', weight='value', v_group='country') test_v = np.array([0, 0, 1], dtype=np.uint8) assert isinstance(g, ge.sGraph) assert isinstance(g, ge.WeightedGraph) assert np.all(g.v.group == test_v), g.v.group
def test_duplicated_vertices(self): v = pd.DataFrame([['ING'], ['ABN'], ['BNP'], ['ABN']], columns=['name']) with pytest.raises(Exception) as e_info: ge.Graph(v, self.e, v_id='name', src='creditor', dst='debtor') msg = 'There is at least one repeated id in the vertex dataframe.' assert e_info.value.args[0] == msg
def test_total_weight_by_label(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') w_by_l = np.array([1e6 + 3e3 + 1e4, 2.3e7, 7e5, 4e5], dtype='f8') test = g.total_weight_label assert np.all(test == w_by_l), test
def test_in_degree(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR']) d_in = np.array([1, 3, 1, 0]) d_test = g.in_degree(get=True) assert np.all(d_test == d_in), d_test assert np.all(g.v.in_degree == d_in), g.v.in_degree
def test_in_strength(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') s_in = np.array([4e5, 1e6 + 2.3e7 + 7e5 + 3e3, 1e4, 0]) s_test = g.in_strength(get=True) assert np.all(s_test == s_in), s_test assert np.all(g.v.in_strength == s_in), g.v.in_strength
def test_out_strength(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') s_out = np.array([1e6, 1e4 + 4e5, 2.3e7, 7e5 + 3e3]) s_test = g.out_strength(get=True) assert np.all(s_test == s_out), s_test assert np.all(g.v.out_strength == s_out), g.v.out_strength
def test_to_sparse_compressed(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR']) mat = np.array([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 1, 0, 0]], dtype=float) assert np.all(g.adjacency_matrix(compressed=True).toarray() == mat)
def test_to_sparse_compressed(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], weight='value') mat = np.array([[0, 1e6, 0, 0], [4e5, 0, 1e4, 0], [0, 2.3e7, 0, 0], [0, 7e5 + 3e3, 0, 0]]) assert np.all(g.adjacency_matrix(compressed=True).toarray() == mat)
def test_vertex_group(self): v = pd.DataFrame([['ING', 'NL'], ['ABN', 'NL'], ['BNP', 'FR']], columns=['name', 'country']) g = ge.Graph(v, self.e, v_id='name', src='creditor', dst='debtor', v_group='country') test_v = np.array([0, 0, 1], dtype=np.uint8) assert isinstance(g, ge.sGraph) assert isinstance(g, ge.DirectedGraph) assert np.all(g.v.group == test_v), g.v.group
def test_to_networkx(self): g = ge.Graph(self.v, self.e, v_id='name', src='creditor', dst='debtor') v = [0, 1, 2] e = [(0, 1), (1, 2), (2, 0), (2, 1)] adj = {0: {1: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}} gx = g.to_networkx() assert np.all(list(gx.nodes) == v) assert np.all(list(gx.edges) == e) assert gx.adj == adj
def test_vertex_group(self): g = ge.Graph(self.v, self.e, v_id=['name', 'country'], src=['creditor', 'c_country'], dst=['debtor', 'd_country'], edge_label=['type', 'EUR'], v_group='country') test_v = np.array([0, 0, 1, 2], dtype=np.uint8) assert isinstance(g, ge.sGraph) assert isinstance(g, ge.LabelGraph) assert np.all(g.v.group == test_v), g.v.group
def test_init_edges(self): g = ge.Graph(self.v_s, self.e_s, v_id='name', src='creditor', dst='debtor', edge_label='type') test_e = np.sort(np.rec.array([(0, 0, 1), (1, 2, 1), (0, 2, 1), (0, 1, 2), (1, 1, 0)], dtype=[('label', np.uint8), ('src', np.uint8), ('dst', np.uint8)])) assert np.all(g.e == test_e), g.e
def test_duplicated_edges(self): e = pd.DataFrame([['ING', 'ABN'], ['BNP', 'ABN'], ['ING', 'ABN'], ['ABN', 'BNP'], ['BNP', 'ING']], columns=['creditor', 'debtor']) with pytest.raises(Exception) as e_info: ge.Graph(self.v, e, v_id='name', src='creditor', dst='debtor') msg = 'There are repeated edges.' assert e_info.value.args[0] == msg