Example #1
0
def test_derive_trajectories_many():
    np.random.seed(0)
    object_id = cudf.Series(np.random.randint(0, 10, 10), dtype="int32")
    xs = cudf.Series(np.random.randint(0, 10, 10))
    ys = cudf.Series(np.random.randint(0, 10, 10))
    timestamp = cudf.Series(np.random.randint(0, 10, 10))
    objects, traj_offsets = cuspatial.derive_trajectories(
        object_id, xs, ys, timestamp)

    sorted_idxs = cudf.DataFrame({"id": object_id, "ts": timestamp}).argsort()
    assert_eq(traj_offsets, cudf.Series([0, 1, 2, 5, 6, 8, 9], dtype="int32"))
    print(objects)
    assert_eq(
        objects,
        cudf.DataFrame(
            {
                "object_id":
                object_id.sort_values().reset_index(drop=True),
                "x":
                xs.take(sorted_idxs).reset_index(drop=True).astype("float64"),
                "y":
                ys.take(sorted_idxs).reset_index(drop=True).astype("float64"),
                "timestamp":
                timestamp.take(sorted_idxs).reset_index(
                    drop=True).astype("datetime64[ms]"),
            },
            index=cudf.core.index.RangeIndex(0, 10),
        ),
    )
Example #2
0
def test_trajectory_distances_and_speeds_timestamp_types(timestamp_type):
    objects, traj_offsets = cuspatial.derive_trajectories(
        # object_id
        cudf.Series([0, 0, 1, 1]),
        # xs
        cudf.Series([0.0, 0.001, 0.0, 0.0]),  # 1 meter in x
        # ys
        cudf.Series([0.0, 0.0, 0.0, 0.001]),  # 1 meter in y
        # timestamp
        cudf.Series([0, timestamp_type[1], 0,
                     timestamp_type[1]]).astype(timestamp_type[0]),
    )
    result = cuspatial.trajectory_distances_and_speeds(
        len(traj_offsets),
        objects["object_id"],
        objects["x"],
        objects["y"],
        objects["timestamp"],
    )
    assert_eq(
        result,
        cudf.DataFrame({
            "distance": [1.0, 1.0],
            "speed": [1.0, 1.0]
        }),
        check_names=False,
    )
Example #3
0
def test_derive_two_trajectories_one_meter_one_second():
    objects, traj_offsets = cuspatial.derive_trajectories(
        [0, 0, 1, 1],  # object_id
        [0.0, 0.001, 0.0, 0.0],  # xs
        [0.0, 0.0, 0.0, 0.001],  # ys
        [0, 1000, 0, 1000],  # timestamp
    )
    result = cuspatial.trajectory_distances_and_speeds(
        len(traj_offsets),
        objects["object_id"],
        objects["x"],
        objects["y"],
        objects["timestamp"],
    )
    assert_eq(result["distance"], cudf.Series([1.0, 1.0]), check_names=False)
    assert_eq(result["speed"], cudf.Series([1.0, 1.0]), check_names=False)
Example #4
0
def test_trajectory_distances_and_speeds_ones():
    objects, traj_offsets = cuspatial.derive_trajectories(
        [1],
        [1],
        [1],
        [1],  # object_id  # xs  # ys  # timestamp
    )
    result = cuspatial.trajectory_distances_and_speeds(
        len(traj_offsets),
        objects["object_id"],
        objects["x"],
        objects["y"],
        objects["timestamp"],
    )
    assert_eq(result["distance"], cudf.Series([0.0]), check_names=False)
    assert_eq(result["speed"], cudf.Series([0.0]), check_names=False)
Example #5
0
def test_derive_trajectories_ones():
    objects, traj_offsets = cuspatial.derive_trajectories(
        cudf.Series([1]),  # object_id
        cudf.Series([1]),  # x
        cudf.Series([1]),  # y
        cudf.Series([1]),  # timestamp
    )
    assert_eq(traj_offsets, cudf.Series([0], dtype="int32"))
    assert_eq(
        objects,
        cudf.DataFrame({
            "object_id": cudf.Series([1], dtype="int32"),
            "x": cudf.Series([1], dtype="float64"),
            "y": cudf.Series([1], dtype="float64"),
            "timestamp": cudf.Series([1], dtype="datetime64[ms]"),
        }),
    )
Example #6
0
def test_derive_trajectories_zeros():
    objects, traj_offsets = cuspatial.derive_trajectories(
        cudf.Series([0]),  # object_id
        cudf.Series([0]),  # x
        cudf.Series([0]),  # y
        cudf.Series([0]),  # timestamp
    )
    cudf.testing.assert_series_equal(traj_offsets,
                                     cudf.Series([0], dtype="int32"))
    cudf.testing.assert_frame_equal(
        objects,
        cudf.DataFrame({
            "object_id": cudf.Series([0], dtype="int32"),
            "x": cudf.Series([0], dtype="float64"),
            "y": cudf.Series([0], dtype="float64"),
            "timestamp": cudf.Series([0], dtype="datetime64[ms]"),
        }),
    )
Example #7
0
def test_trajectory_distances_and_speeds_single_trajectory():
    objects, traj_offsets = cuspatial.derive_trajectories(
        [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2],  # object_id
        [1.0, 2.0, 3.0, 5.0, 7.0, 1.0, 2.0, 3.0, 6.0, 0.0, 3.0, 6.0],  # xs
        [0.0, 1.0, 2.0, 3.0, 1.0, 3.0, 5.0, 6.0, 5.0, 4.0, 7.0, 4.0],  # ys
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],  # timestamp
    )
    result = cuspatial.trajectory_distances_and_speeds(
        len(traj_offsets),
        objects["object_id"],
        objects["x"],
        objects["y"],
        objects["timestamp"],
    )
    assert_eq(
        result["distance"],
        cudf.Series([7892.922363, 6812.55908203125, 8485.28125]),
        check_names=False,
    )
    assert_eq(
        result["speed"],
        cudf.Series([1973230.625, 2270853.0, 4242640.5]),
        check_names=False,
    )  # fast!