Exemplo n.º 1
0
def test_GraphBuilder():
    b = network.GraphBuilder()
    b.add_edge(0, 1)
    b.add_edge(1, 2)
    b.add_edge(3, 4)
    g = b.build()
    assert g.number_edges == 3
    assert g.vertices == {0, 1, 2, 3, 4}
    assert g.edges == [(0, 1), (1, 2), (3, 4)]

    b.lengths = [1, 2, 5]
    g = b.build()
    assert g.number_edges == 3
    assert g.vertices == {0, 1, 2, 3, 4}
    assert g.edges == [(0, 1), (1, 2), (3, 4)]
    assert g.length(0) == 1
    assert g.length(1) == 2
    assert g.length(2) == 5

    b.lengths = [1, 2]
    with pytest.raises(ValueError):
        b.build()

    b.vertices.add(7)
    b.remove_unused_vertices()
    assert b.vertices == {4, 3, 2, 1, 0}

    assert list(g.paths_between(0, 2)) == [[0, 1, 2]]
Exemplo n.º 2
0
def test_ordered_segment_cycle():
    graph = network.GraphBuilder().add_edge(0,
                                            1).add_edge(1,
                                                        2).add_edge(0,
                                                                    2).build()
    got = set(tuple(x) for x in network.ordered_segment_graph(graph))
    print("Dodgy test...")
    assert got == {(2, 0, 1, 2)}
Exemplo n.º 3
0
def test_GraphBuilder_duplicate_edges():
    b = network.GraphBuilder()
    b.add_edge(0, 1)
    b.add_edge(1, 2)
    b.add_edge(0, 1)
    b.add_edge(1, 2)
    b.add_edge(3, 4)
    with pytest.raises(ValueError):
        g = b.build()

    b.remove_duplicate_edges()
    g = b.build()
    assert g.number_edges == 3
    assert g.vertices == {0, 1, 2, 3, 4}
Exemplo n.º 4
0
    def __init__(self, points, tolerance=0.1):
        all_nodes = list(set(((x, y) for x, y in points)))
        all_nodes = _np.asarray(all_nodes)
        builder = _network.GraphBuilder()
        index = self._make_index(all_nodes, tolerance)

        d = tolerance / 20
        for i, pt in enumerate(all_nodes):
            builder.vertices.add(i)
            x, y = pt
            for j in index.intersection((x - d, y - d, x + d, y + d)):
                if i < j:
                    builder.add_edge(i, j)
        graph = builder.build()

        self._make_data(graph, all_nodes)
Exemplo n.º 5
0
    def __init__(self, points, tolerance=0.1):
        all_nodes = list(set(((x, y) for x, y in points)))
        all_nodes.sort()
        all_nodes = _np.asarray(all_nodes)
        builder = _network.GraphBuilder()
        idx = _np.arange(all_nodes.shape[0])
        tsq = tolerance**2
        _logger.debug("Merging %s points", len(all_nodes))
        for i, pt in enumerate(all_nodes):
            builder.vertices.add(i)
            distsq = _np.sum((all_nodes[i + 1:] - pt)**2, axis=1)
            for j in idx[i + 1:][distsq <= tsq]:
                builder.add_edge(i, j)
            if i % 1000 == 0:
                _logger.debug("Done %s / %s points", i, len(all_nodes))
        graph = builder.build()

        self._make_data(graph, all_nodes)
Exemplo n.º 6
0
    def _aggregate(self, points):
        _logger.debug("Performing aggregation")
        pl = _ocp_logger.ProgressLogger(points.shape[0], _datetime.timedelta(seconds=15), _logger)
        builder = _network.GraphBuilder()
        for i, choices in enumerate(self._initial_aggregate()):
            edge, t = self._graph_points[i]
            dist = LimitedNetworkDistance(self._graph, edge, t, self._tolerance)
            for j in choices:
                try:
                    if i != j and dist.distance(*self._graph_points[j]) <= self._tolerance:
                        builder.add_edge(i, j)
                except ValueError:
                    pass
            pl.increase_count()

        builder.remove_duplicate_edges()
        builder.vertices.update(range(points.shape[0]))
        g = builder.build()
        _logger.debug("Performing final aggregation...")
        pl = _ocp_logger.ProgressLogger(points.shape[0], _datetime.timedelta(seconds=15), _logger)
        agg_points = []
        lookup = dict()
        agged_graph_points = []
        for com in _network.connected_components(g):
            com = list(com)
            pts = self._agg_points[com]
            centroid = _np.mean(pts, axis=0)
            i = _np.argmin( _np.sum((pts - centroid)**2, axis=1) )
            agg_points.append(pts[i])
            agged_graph_points.append(self._graph_points[com[i]])
            for j in com:
                lookup[j] = len(agg_points) - 1
            pl.add_to_count(len(com))
        self._agg_points = _np.asarray(agg_points)
        self._lookup = [lookup[i] for i in range(points.shape[0])]
        self._graph_points = agged_graph_points
Exemplo n.º 7
0
def graph5():
    b = network.GraphBuilder()
    b.add_edge(0, 1).add_edge(1, 2).add_edge(3, 4).add_edge(4,
                                                            5).add_edge(5, 3)
    b.vertices.add(6)
    return b.build()
Exemplo n.º 8
0
def graph4():
    b = network.GraphBuilder()
    b.add_edge(0, 1).add_edge(1, 2).add_edge(2, 3).add_edge(3, 4)
    b.add_edge(4, 5).add_edge(5, 0).add_edge(0, 6).add_edge(0, 7)
    return b.build()
Exemplo n.º 9
0
def graph3():
    b = network.GraphBuilder()
    b.add_edge(0, 1).add_edge(1, 2).add_edge(2, 3).add_edge(3,
                                                            4).add_edge(4, 5)
    b.add_edge(3, 5).add_edge(5, 6).add_edge(6, 7).add_edge(7, 1)
    return b.build()