Beispiel #1
0
def test_add_vertex_attributes():
    g = Graph(vertices=[0, 1])
    g.add_vertex_attributes()
    assert g.vertex_attributes == []
    g.add_vertex_attributes(vertex_attributes='name')
    assert g.vertex_attributes == ['name']
    g.add_vertex_attributes(vertex_attributes='level')
    assert compare_lists_unordered(g.vertex_attributes, ['name', 'level'])

    g = Graph(vertices=[0, 1])
    g.add_vertex_attributes(vertex_attributes={'name': 'test', 'level': 0})
    assert compare_lists_unordered(g.vertex_attributes, ['name', 'level'])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame({'name': ['test']*2, 'level': [0]*2},
                                                      index=make_vertex_index([0, 1])))
    g.add_vertex_attributes(vertex_attributes={'name': 'change_test', 'new_level': 1})
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame({'name': ['change_test']*2, 'level': [0]*2, 'new_level': [1]*2},
                                                      index=make_vertex_index([0, 1])))
Beispiel #2
0
def test_remove_vertices():
    g = Graph()
    g.remove_vertices()
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])

    g.remove_vertices(vertices=[0, 1])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])

    g = Graph(vertices=[0, 1, 2])
    g.remove_vertices(vertices=[0, 1])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([2])))
    assert g.vertices == [2]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [2])

    g.remove_vertices(vertices=2)
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])

    g = Graph(n_vertices=10)
    g.remove_vertices(vertices=list(range(10)))
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])

    g = Graph(n_vertices=10)
    g.remove_vertices(vertices=list(range(100)))
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])
Beispiel #3
0
def test_add_vertices():
    g = Graph()
    g.add_vertices()
    assert g.vertices == []
    assert compare_lists_unordered(list(g.nx_graph.nodes), [])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index()))
    g.add_vertices([0])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0])))
    assert g.vertices == [0]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0])
    g.add_vertices(1)
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1])))
    assert g.vertices == [0, 1]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1])
    g.add_vertices([3, 2])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1, 2, 3])))
    assert g.vertices == [0, 1, 2, 3]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1, 2, 3])
    g.add_vertices(3)
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1, 2, 3])))
    assert g.vertices == [0, 1, 2, 3]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1, 2, 3])

    g = Graph(vertices=[0, 1, 2])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1, 2])))
    assert g.vertices == [0, 1, 2]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1, 2])

    g = Graph()
    g.add_vertices(n_vertices=3)
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1, 2])))
    assert g.vertices == [0, 1, 2]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1, 2])

    g = Graph(vertices=[0])
    g.add_vertices(n_vertices=3)
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame(index=make_vertex_index([0, 1, 2, 3])))
    assert g.vertices == [0, 1, 2, 3]
    assert compare_lists_unordered(list(g.nx_graph.nodes), [0, 1, 2, 3])
Beispiel #4
0
def test_get_second_degree():
    g = Graph(vertices=[0, 1, 2, 3, 4], edges=[(0, 1), (1, 2)])
    g.get_second_degree()
    expected_df = pd.DataFrame({'second_degree': [1, 0, 1, 0, 0]}, index=make_vertex_index(list(range(5))))
    pd.testing.assert_series_equal(g.vdf['second_degree'], expected_df['second_degree'])
Beispiel #5
0
def test_get_second_neighbours():
    g = Graph(vertices=[0, 1, 2, 3, 4], edges=[(0, 1), (1, 2)])
    g.get_second_neighbours()
    expected_df = pd.DataFrame({'second_neighbours': [[2], [], [0], [], []]}, index=make_vertex_index(list(range(5))))
    pd.testing.assert_series_equal(g.vdf['second_neighbours'], expected_df['second_neighbours'])
Beispiel #6
0
def test_add_vertices_with_vertex_attributes():
    g = Graph(vertices=[0, 1], vertex_attributes=['name', 'level'])
    g.add_vertices(vertices=[2, 3])
    pd.testing.assert_frame_equal(g.vdf, pd.DataFrame({'name': [None]*4, 'level': [None]*4},
                                                      index=make_vertex_index([0, 1, 2, 3])))