def test_filter_gps():
    original_data = _get_test_data()
    assert len(original_data) == 793
    data = filter_gps_data(original_data)
    print(len(data))
    assert len(data) == 780
    print(len(original_data) - len(data))
    assert len(original_data) - len(data) == 13
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_velocity_filter_decay():
    """
    Sometimes points between outliers are removed by other filters, so VelocityOutliersFilter
    starts to compare outliers with earlier and earlier points that makes velocity to go
    down so they are not considered as outliers anymore
    """
    timestamps = np.array(
        [
            735856.225625,
            735856.22609954,
            735856.2265625,
            735856.22701389,
            735856.25675926,
            735856.2572338,
            735856.25731481,
            735856.41100694,
        ]
    )
    coords = np.array(
        [
            [3.30049220e01, -1.17060744e02],
            [3.29771600e01, -1.17078251e02],
            [3.29771580e01, -1.17078254e02],
            [3.30049220e01, -1.17060744e02],
            [3.30049220e01, -1.17060744e02],
            [3.29771590e01, -1.17078258e02],
            [3.30049230e01, -1.17060575e02],
            [3.30049410e01, -1.17060618e02],
        ]
    )
    data = np.hstack([timestamps[:, None], coords])

    filtered_coords = filter_gps_data(data)[:, 1:]
    print(filtered_coords)

    np.testing.assert_array_almost_equal(
        filtered_coords,
        [
            [3.30049220e01, -1.17060744e02],
            [3.30049220e01, -1.17060744e02],
            [3.30049220e01, -1.17060744e02],
            [3.30049230e01, -1.17060575e02],
            [3.30049410e01, -1.17060618e02],
        ],
    )
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]])