def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('output',
                        metavar='graph.pickle',
                        help='Ouput graph file')
    parser.add_argument(
        '--connection',
        default='postgresql://*****:*****@localhost/osrm',
        help='Postgres connection string.  Default %(default)s')
    parser.add_argument('--prune',
                        action='store_true',
                        help='Collapse redundant edges, prune tails')

    parser.add_argument('--verbose',
                        action='store_true',
                        help='Increase logging level')

    args = parser.parse_args()

    logging.basicConfig(
        level=logging.INFO if args.verbose else logging.WARNING)

    log.info("Creating DB engine")
    engine = create_engine(args.connection, echo=False)

    log.info("Creating DB session")
    Session = sessionmaker(bind=engine)
    session = Session()

    g = build_graph(session)

    if args.prune:
        log.info("Collapsing unidirectional strings")
        pruned = graphtools.collapse_degree_2_vtxs(g)
        log.info("Removed %i thru-nodes", pruned)
        log.info("Collapsing bidirectional strings")
        pruned = graphtools.collapse_bidirectional_streets(g)
        log.info("Removed %i thru-nodes", pruned)
        log.info("Snipping tails")
        snipped = graphtools.delete_degree_1_vtxs(g)
        log.info("Removed %i tails", snipped)
        loners = graphtools.delete_degree_0_vtxs(g)
        log.info("Removed %i loner-nodes (should be zero)", loners)
        redundancies = graphtools.identify_rendudant_nodes(g)
        log.info("Marked %i nodes as redundant", redundancies)

    log.info("Saving graph to %s", args.output)
    g.save(args.output)
def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('output', metavar='graph.pickle',
                        help='Ouput graph file')
    parser.add_argument(
        '--connection',
        default='postgresql://*****:*****@localhost/osrm',
        help='Postgres connection string.  Default %(default)s'
    )
    parser.add_argument('--prune', action='store_true',
                        help='Collapse redundant edges, prune tails')

    parser.add_argument('--verbose', action='store_true',
                        help='Increase logging level')

    args = parser.parse_args()

    logging.basicConfig(
        level=logging.INFO if args.verbose else logging.WARNING)

    log.info("Creating DB engine")
    engine = create_engine(args.connection, echo=False)

    log.info("Creating DB session")
    Session = sessionmaker(bind=engine)
    session = Session()

    g = build_graph(session)

    if args.prune:
        log.info("Collapsing unidirectional strings")
        pruned = graphtools.collapse_degree_2_vtxs(g)
        log.info("Removed %i thru-nodes", pruned)
        log.info("Collapsing bidirectional strings")
        pruned = graphtools.collapse_bidirectional_streets(g)
        log.info("Removed %i thru-nodes", pruned)
        log.info("Snipping tails")
        snipped = graphtools.delete_degree_1_vtxs(g)
        log.info("Removed %i tails", snipped)
        loners = graphtools.delete_degree_0_vtxs(g)
        log.info("Removed %i loner-nodes (should be zero)", loners)
        redundancies = graphtools.identify_rendudant_nodes(g)
        log.info("Marked %i nodes as redundant", redundancies)

    log.info("Saving graph to %s", args.output)
    g.save(args.output)
def test_delete_degree_1_vtxs():
    g = igraph.Graph(directed=True)
    # a square with a tail
    g.add_vertices(5)
    g.add_edges([
        (0, 1),
        (1, 2),
        (2, 3),
        (3, 0),
        (2, 4)  # the tail
    ])
    eq_(len(g.vs), 5)
    eq_(len(g.es), 5)
    eq_(len(g.vs.select(_degree_eq=1)), 1)
    eq_(len(g.vs.select(_degree_eq=2)), 3)
    eq_(len(g.vs.select(_degree_eq=3)), 1)
    ret = gt.delete_degree_1_vtxs(g)
    eq_(ret, 1)
    # Now it is clean
    eq_(len(g.vs), 4)
    eq_(len(g.es), 4)
    eq_(len(g.vs.select(_degree_eq=1)), 0)
    eq_(len(g.vs.select(_degree_eq=2)), 4)
    eq_(len(g.vs.select(_degree_eq=3)), 0)