def test_find_endpoints_procedural():
    folder = tempfile.mkdtemp()
    s3 = S3('driverpete-storage')
    filename = s3.download("_testing/testing_merged_1", folder)
    
    data = filter_gps_data(read_compressed_trajectory(filename))

    endpoints_indices_batch = find_endpoints_batch(data)
    assert(len(endpoints_indices_batch) == 2)
    print(endpoints_indices_batch)
    assert(endpoints_indices_batch == [479, 670])

    endpoints = find_endpoints(data)
    assert(len(endpoints) == 2)
    assert((data[endpoints_indices_batch[0]] == endpoints[0]).all())
    assert((data[endpoints_indices_batch[1]] == endpoints[1]).all())
def process_gps_data(filename):
    #stationary_distance_threshold=120
    # clean up data
    data = read_compressed_trajectory(filename)
    print("Length of data: %d" % len(data))
    
    data = filter_gps_data(data)
    print("Length of filtered data: %d" % len(data))
    
    endpoints = find_endpoints(data)
    print("Found %d endpoints:" % len(endpoints))
    for u in endpoints:
        print(trajectory_point_to_str([u], 0))
    
    assert(len(endpoints) == 2)
    AtoB_paths_data, BtoA_paths_data = find_routes(data, endpoints, verbose=False)
    
    def _extract_indices(data, paths):
        result = []
        for p in paths:
            indices = [(data[:, 0] == p[0, 0]).nonzero()[0][0],
                       (data[:, 0] == p[p.shape[0]-1, 0]).nonzero()[0][0]]
            result.append(indices)
        return result

    AtoB_paths_indices = _extract_indices(data, AtoB_paths_data)
    BtoA_paths_indices = _extract_indices(data, BtoA_paths_data)

    print(AtoB_paths_indices)
    print(BtoA_paths_indices)
    
    AtoB_paths = [Path(d) for d in AtoB_paths_data]
    BtoA_paths = [Path(d) for d in BtoA_paths_data]
    
    AtoB_paths = sorted(AtoB_paths, key=lambda x: x.get_duration())
    BtoA_paths = sorted(BtoA_paths, key=lambda x: x.get_duration())

    print("ATOB")
    for p in AtoB_paths:
        print(p.get_duration()/60, str(p.start_time()))
        p.show(str(p.get_duration()/60))

    print("BTOA")
    for p in BtoA_paths:
        print(p.get_duration()/60, str(p.start_time()))
        p.show(str(p.get_duration()/60))
def test_finding_paths():
    folder = tempfile.mkdtemp()
    s3 = S3('driverpete-storage')
    filename = s3.download("_testing/testing_merged_1", folder)
        
    data = filter_gps_data(read_compressed_trajectory(filename))
    
    endpoints = find_endpoints(data)
    assert(len(endpoints) == 2)
    print(trajectory_point_to_str(data, _get_point_index(data, endpoints[0])))
    print(trajectory_point_to_str(data, _get_point_index(data, endpoints[1])))
    assert(_get_point_index(data, endpoints[0]) == 479)
    assert(_get_point_index(data, endpoints[1]) == 670)

    AtoB_paths, BtoA_paths = find_routes(data, endpoints, verbose=False)

    AtoB_paths_indices = _extract_indices(data, AtoB_paths)
    BtoA_paths_indices = _extract_indices(data, BtoA_paths)
    print(AtoB_paths_indices)
    print(BtoA_paths_indices)
    assert(AtoB_paths_indices == [[488, 656], [947, 1117], [1364, 1549], [2216, 2401], [2630, 2898], [4400, 4526]])
    assert(BtoA_paths_indices == [[134, 455], [683, 887], [1141, 1316], [1582, 1783], [2429, 2597], [3975, 4170]])