Ejemplo n.º 1
0
def test_edgedata_io(graph):
    graph.update_default_edge_attributes({'index': 0})
    for index, (u, v) in enumerate(graph.edges()):
        graph.edge_attribute((u, v), 'index', index)
    other = Graph.from_data(graph.data)
    assert all(
        other.edge_attribute(edge, 'index') == index
        for index, edge in enumerate(other.edges()))
Ejemplo n.º 2
0
def test_default_edge_attributes():
    graph = Graph(name='test', default_edge_attributes={'a': 1, 'b': 2})
    for edge in graph.edges():
        assert graph.edge_attribute(edge, name='a') == 1
        assert graph.edge_attribute(edge, name='b') == 2
        graph.edge_attribute(edge, name='a', value=3)
        assert graph.edge_attribute(edge, name='a') == 3
Ejemplo n.º 3
0
def test_default_node_attributes():
    graph = Graph(name='test', default_node_attributes={'a': 1, 'b': 2})
    for node in graph.nodes():
        assert graph.node_attribute(node, name='a') == 1
        assert graph.node_attribute(node, name='b') == 2
        graph.node_attribute(node, name='a', value=3)
        assert graph.node_attribute(node, name='a') == 3
Ejemplo n.º 4
0
def test_astar_lightest_path():
    g = Graph()
    for i in range(4):
        g.add_node(i)
    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(1, 3)
    g.add_edge(2, 3)
    weights = {
        (0, 1): 1,
        (0, 2): 1,
        (1, 3): 2,
        (2, 3): 1,
    }
    heuristic = {i: 1 for i in range(4)}
    path = astar_lightest_path(g.adjacency, weights, heuristic, 0, 3)
    assert path == [0, 2, 3]
Ejemplo n.º 5
0
def graph():
    edges = [[0, 1], [0, 2], [0, 3], [0, 4]]
    graph = Graph()
    for u, v in edges:
        graph.add_edge(u, v)
    return graph
Ejemplo n.º 6
0
def test_graph_networkx_conversion():
    if compas.IPY:
        return

    g = Graph()
    g.attributes['name'] = 'DiGraph'
    g.attributes['val'] = (0, 0, 0)
    g.add_node(0)
    g.add_node(1, weight=1.2, height='test')
    g.add_node(2, x=1, y=1, z=0)

    g.add_edge(0, 1, attr_value=10)
    g.add_edge(1, 2)

    nxg = g.to_networkx()

    assert nxg.graph['name'] == 'DiGraph', "Graph attributes must be preserved"
    assert nxg.graph['val'] == (0, 0, 0), "Graph attributes must be preserved"
    assert set(nxg.nodes()) == set(g.nodes()), "Node sets must match"
    assert nxg.nodes[1]['weight'] == 1.2, "Node attributes must be preserved"
    assert nxg.nodes[1][
        'height'] == "test", "Node attributes must be preserved"
    assert nxg.nodes[2]['x'] == 1, "Node attributes must be preserved"

    assert set(nxg.edges()) == set(((0, 1), (1, 2))), "Edge sets must match"
    assert nxg.edges[
        0, 1]['attr_value'] == 10, "Edge attributes must be preserved"

    g2 = Graph.from_networkx(nxg)

    assert g.number_of_nodes() == g2.number_of_nodes()
    assert g.number_of_edges() == g2.number_of_edges()
    assert g2.edge_attribute((0, 1), 'attr_value') == 10
    assert g2.attributes[
        'name'] == 'DiGraph', "Graph attributes must be preserved"
    assert g2.attributes['val'] == (0, 0,
                                    0), "Graph attributes must be preserved"