예제 #1
0
def test_anyhashableg_as_unweighted():
    g = create_graph(
        directed=True,
        allowing_self_loops=True,
        allowing_multiple_edges=False,
        weighted=True,
        any_hashable=True,
    )

    g.add_vertex("0")
    g.add_vertex("1")
    g.add_edge("0", "1", edge="e1")
    g.set_edge_weight("e1", 100.0)

    assert g.get_edge_weight("e1") == 100.0

    ug = as_unweighted(g)

    assert g.type.directed == ug.type.directed
    assert g.type.allowing_self_loops == ug.type.allowing_self_loops
    assert g.type.allowing_multiple_edges == ug.type.allowing_multiple_edges
    assert g.type.weighted != ug.type.weighted

    assert g.get_edge_weight("e1") == 100.0
    assert ug.get_edge_weight("e1") == 1.0
예제 #2
0
def test_as_unweighted():
    g = create_graph(
        directed=True,
        allowing_self_loops=True,
        allowing_multiple_edges=False,
        weighted=True,
    )

    g.add_vertex(0)
    v1 = 0
    g.add_vertex(1)
    v2 = 1
    g.add_vertex(2)
    v3 = 2
    g.add_vertex(3)
    v4 = 3
    g.add_vertex(4)
    v5 = 4
    g.add_edge(v1, v2)
    g.add_edge(v2, v3)
    g.add_edge(v1, v4)
    g.add_edge(v1, v1)
    e45 = g.add_edge(v4, v5)
    g.add_edge(v5, v1)

    g.set_edge_weight(e45, 100.0)

    g1 = as_unweighted(g)

    assert g.type.directed == g1.type.directed
    assert g.type.allowing_self_loops == g1.type.allowing_self_loops
    assert g.type.allowing_multiple_edges == g1.type.allowing_multiple_edges
    assert g.type.weighted != g1.type.weighted
    assert g.get_edge_weight(e45) == 100.0
    assert g1.get_edge_weight(e45) == 1.0
예제 #3
0
def test_as_unweighted_on_property_graphs():
    g = create_graph(
        directed=True,
        allowing_self_loops=True,
        allowing_multiple_edges=False,
        weighted=True,
        any_hashable=True,
        vertex_supplier=create_vertex_supplier(),
        edge_supplier=create_edge_supplier(),
    )

    g.add_vertex()
    g.add_vertex()
    g.add_vertex()
    g.add_vertex()
    g.add_edge('v0', 'v1')
    g.add_edge('v1', 'v2')
    g.add_edge('v2', 'v3')

    g.set_edge_weight('e0', 100.0)
    g.set_edge_weight('e1', 50.0)
    g.set_edge_weight('e2', 25.0)

    g.vertex_attrs['v0']['before'] = 'v0'
    g.vertex_attrs['v1']['before'] = 'v1'
    g.edge_attrs['e0']['before'] = 'e0'
    g.edge_attrs['e1']['before'] = 'e1'

    g1 = as_unweighted(g)

    assert g.type.directed == g1.type.directed
    assert g.type.allowing_self_loops == g1.type.allowing_self_loops
    assert g.type.allowing_multiple_edges == g1.type.allowing_multiple_edges
    assert g.type.weighted != g1.type.weighted

    assert g1.get_edge_weight('e0') == 1.0
    assert g1.get_edge_weight('e1') == 1.0
    assert g1.get_edge_weight('e2') == 1.0

    # test that properties still exist
    assert g.vertex_attrs['v0']['before'] == 'v0'
    assert g.vertex_attrs['v1']['before'] == 'v1'
    assert g.edge_attrs['e0']['before'] == 'e0'
    assert g.edge_attrs['e1']['before'] == 'e1'

    assert g1.vertex_attrs['v0']['before'] == 'v0'
    assert g1.vertex_attrs['v1']['before'] == 'v1'
    assert g1.edge_attrs['e0']['before'] == 'e0'
    assert g1.edge_attrs['e1']['before'] == 'e1'

    # test adding a property in g
    g.vertex_attrs['v0']['after'] = 'v0'
    assert g1.vertex_attrs['v0']['after'] == 'v0'

    # test adding a property in g1
    g1.edge_attrs['e0']['after'] = 'e0'
    assert g.edge_attrs['e0']['after'] == 'e0'

    # test deleting a property from g
    del g.vertex_attrs['v1']['before']
    with pytest.raises(KeyError):
        g1.vertex_attrs['v1']['before']

    with pytest.raises(ValueError):
        g1.edge_attrs['e0']['weight'] = 200.0

    g.edge_attrs['e0']['weight'] = 200.0
    assert g.edge_attrs['e0']['weight'] == 200.0
    assert g1.edge_attrs['e0']['weight'] == 1.0