Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all demos with python API. #44

Merged
merged 5 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- PR #31 Add Github CODEOWNERS
- PR #39 Add cython headers to install, python / cmake packaging cleanup
- PR #41 Python and Cython style cleanup, pre-commit hook
- PR #44 Update all demos with Python API

## Bug Fixes

Expand Down
12 changes: 6 additions & 6 deletions python/cuspatial/demos/hausdorff_clustering_test_toy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import numpy as np
from sklearn.cluster import DBSCAN, AgglomerativeClustering

from cudf.core import column
from cudf import Series

import cuspatial._lib.spatial as gis
import cuspatial

in_trajs = []
in_trajs.append(np.array([[1, 0], [2, 1], [3, 2], [5, 3], [7, 1]]))
Expand All @@ -27,10 +27,10 @@
py_cnt = []
for traj in in_trajs:
py_cnt.append(len(traj))
pnt_x = column.as_column(py_x, dtype=np.float64)
pnt_y = column.as_column(py_y, dtype=np.float64)
cnt = column.as_column(py_cnt, dtype=np.int32)
distance = gis.cpp_directed_hausdorff_distance(pnt_x, pnt_y, cnt)
pnt_x = Series(py_x)
pnt_y = Series(py_y)
cnt = Series(py_cnt)
distance = cuspatial.directed_hausdorff_distance(pnt_x, pnt_y, cnt)

num_set = len(cnt)
matrix = distance.data.to_array().reshape(num_set, num_set)
Expand Down
13 changes: 6 additions & 7 deletions python/cuspatial/demos/hausdorff_distance_verify_locust256.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import numpy as np
from scipy.spatial.distance import directed_hausdorff

import cuspatial._lib.soa_readers as readers
import cuspatial._lib.spatial as gis
import cuspatial

data_dir = "/home/jianting/trajcode/"
data_set = "locust256"
Expand All @@ -30,18 +29,18 @@
data_set = sys.argv[1]

# reading poing xy coordinate data (relative to a camera origin)
pnt_x, pnt_y = readers.cpp_read_pnt_xy_soa(data_dir + data_set + ".coor")
pnt_x, pnt_y = cuspatial.read_points_xy_km(data_dir + data_set + ".coor")
# reading numbers of points in trajectories
cnt = readers.cpp_read_uint_soa(data_dir + data_set + ".objcnt")
cnt = cuspatial.read_uint(data_dir + data_set + ".objcnt")
# reading object(vehicle) id
id = readers.cpp_read_uint_soa(data_dir + data_set + ".objectid")
id = cuspatial.read_uint(data_dir + data_set + ".objectid")

num_traj = cnt.data.size
dist0 = gis.cpp_directed_hausdorff_distance(pnt_x, pnt_y, cnt)
dist0 = cuspatial.directed_hausdorff_distance(pnt_x, pnt_y, cnt)
cuspatial_dist0 = dist0.data.to_array().reshape((num_traj, num_traj))

start = time.time()
dist = gis.cpp_directed_hausdorff_distance(pnt_x, pnt_y, cnt)
dist = cuspatial.directed_hausdorff_distance(pnt_x, pnt_y, cnt)
print(
"dis.size={} num_traj*num_traj={}".format(
dist.data.size, num_traj * num_traj
Expand Down
19 changes: 8 additions & 11 deletions python/cuspatial/demos/haversine_distance_test_nyctaxi.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import time

import cudf
from cudf.core import column
from cudf import Series, read_csv

import cuspatial._lib.spatial as gis
import cuspatial

start = time.time()
# data dowloaded from
# https://s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2009-01.csv
df = cudf.read_csv(
"/home/jianting/hardbd19/data/nyctaxi/yellow_tripdata_2009-01.csv"
)
df = read_csv("data/yellow_tripdata_2009-01.csv")
end = time.time()
print("data ingesting time (from SSD) in ms={}".format((end - start) * 1000))
df.head().to_pandas().columns

start = time.time()
x1 = column.as_column(df["Start_Lon"])
y1 = column.as_column(df["Start_Lat"])
x2 = column.as_column(df["End_Lon"])
y2 = column.as_column(df["End_Lat"])
x1 = Series(df["Start_Lon"])
y1 = Series(df["Start_Lat"])
x2 = Series(df["End_Lon"])
y2 = Series(df["End_Lat"])
end = time.time()
print(
"data frame to gdf column conversion time in ms={}".format(
Expand All @@ -28,7 +25,7 @@
)

start = time.time()
h_dist = gis.cpp_haversine_distance(x1, y1, x2, y1)
h_dist = cuspatial.haversine_distance(x1, y1, x2, y2)
end = time.time()
print("python computing distance time in ms={}".format((end - start) * 1000))
# h_dist.data.to_array()
13 changes: 6 additions & 7 deletions python/cuspatial/demos/pip_verify_shapely_locust.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import shapefile
from shapely.geometry import Point, Polygon

import cuspatial._lib.soa_readers as readers
import cuspatial._lib.spatial as gis
import cuspatial

data_dir = "/home/jianting/cuspatial/data/"
plyreader = shapefile.Reader(data_dir + "its_4326_roi.shp")
Expand All @@ -20,13 +19,13 @@
for shape in polygon:
plys.append(Polygon(shape.points))

pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa(
data_dir + "locust.location"
)
fpos, rpos, plyx, plyy = readers.cpp_read_ply_soa(data_dir + "itsroi.ply")
pnt_lon, pnt_lat = cuspatial.read_points_lonlat(data_dir + "locust.location")
fpos, rpos, plyx, plyy = cuspatial.read_polygon(data_dir + "itsroi.ply")

start = time.time()
bm = gis.cpp_pip_bm(pnt_lon, pnt_lat, fpos, rpos, plyx, plyy)
bm = cuspatial.point_in_polygon_bitmap(
pnt_lon, pnt_lat, fpos, rpos, plyx, plyy
)
end = time.time()
print("Python GPU Time in ms (end-to-end)={}".format((end - start) * 1000))

Expand Down
23 changes: 7 additions & 16 deletions python/cuspatial/demos/stq_test_soa_locust.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@
as x.data.size, both are 1338671
"""

import numpy as np
import cuspatial

import cuspatial._lib.soa_readers as readers
import cuspatial._lib.stq as stq
data_dir = "./data/"
data = cuspatial.read_points_lonlat(data_dir + "locust.location")

data_dir = "/home/jianting/cuspatial/data/"
pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa(
data_dir + "locust.location"
points_inside = cuspatial.window_points(
-180, -90, 180, 90, data["lon"], data["lat"]
)
num, nlon, nlat = stq.cpp_sw_xy(
np.double(-180),
np.double(180),
np.double(-90),
np.double(90),
pnt_lon,
pnt_lat,
)
print(num)
print(pnt_lon.data.size)
print(points_inside.shape[0])
assert points_inside.shape[0] == data.shape[0]
27 changes: 27 additions & 0 deletions python/cuspatial/demos/traj_demo_derive_subset_locus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
demo of chaining three APIs: derive_trajectories+subset_trajectory(by ID)
+hausdorff_distance also serves as an example to integrate cudf and cuspatial
"""

import cuspatial

data_dir = "./data/"
lonlats = cuspatial.read_points_lonlat(data_dir + "locust.location")
ids = cuspatial.read_uint(data_dir + "locust.objectid")
ts = cuspatial.read_its_timestamps(data_dir + "locust.time")

num_traj, trajectories = cuspatial.derive(
lonlats["lon"], lonlats["lat"], ids, ts
)
df = trajectories.query("length>=256")
query_ids = df["trajectory_id"]
query_cnts = df["length"]
new_trajs = cuspatial.subset_trajectory_id(
query_ids, lonlats["lon"], lonlats["lat"], ids, ts
)
new_lon = new_trajs["x"]
new_lat = new_trajs["y"]
num_traj = df.count()[0]
dist = cuspatial.directed_hausdorff_distance(new_lon, new_lat, query_cnts)
cuspatial_dist0 = dist.data.to_array().reshape((num_traj, num_traj))
print(cuspatial_dist0)
30 changes: 14 additions & 16 deletions python/cuspatial/demos/traj_test_soa_locust.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
import numpy as np
import pandas as pd

import cuspatial._lib.soa_readers as readers
import cuspatial._lib.spatial as gis
import cuspatial._lib.trajectory as traj
import cuspatial
import cuspatial.utils.traj_utils as tools

data_dir = "./"
data_dir = "./data/"
df = pd.read_csv(data_dir + "its_camera_2.csv")
this_cam = df.loc[df["cameraIdString"] == "HWY_20_AND_LOCUST"]
cam_lon = np.double(this_cam.iloc[0]["originLon"])
cam_lat = np.double(this_cam.iloc[0]["originLat"])

pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa(
data_dir + "locust.location"
)
id = readers.cpp_read_uint_soa(data_dir + "locust.objectid")
ts = readers.cpp_read_ts_soa(data_dir + "locust.time")
lonlats = cuspatial.read_points_lonlat(data_dir + "locust.location")
ids = cuspatial.read_uint(data_dir + "locust.objectid")
ts = cuspatial.read_its_timestamps(data_dir + "locust.time")

# examine binary representatons
ts_0 = ts.data.to_array()[0]
Expand All @@ -34,16 +30,18 @@

y, m, d, hh, mm, ss, wd, yd, ms, pid = tools.get_ts_struct(ts_0)

pnt_x, pnt_y = gis.cpp_lonlat2coord(cam_lon, cam_lat, pnt_lon, pnt_lat)
num_traj, trajectories = traj.cpp_derive_trajectories(pnt_x, pnt_y, id, ts)
xys = cuspatial.lonlat_to_xy_km_coordinates(
cam_lon, cam_lat, lonlats["lon"], lonlats["lat"]
)
num_traj, trajectories = cuspatial.derive(xys["x"], xys["y"], ids, ts)
# = num_traj, tid, len, pos =
y, m, d, hh, mm, ss, wd, yd, ms, pid = tools.get_ts_struct(ts_0)
dist, speed = traj.cpp_trajectory_distance_and_speed(
pnt_x, pnt_y, ts, trajectories["length"], trajectories["position"]
distspeed = cuspatial.distance_and_speed(
xys["x"], xys["y"], ts, trajectories["length"], trajectories["position"]
)
print(dist.data.to_array()[0], speed.data.to_array()[0])
print(distspeed)

boxes = traj.cpp_trajectory_spatial_bounds(
pnt_x, pnt_y, trajectories["length"], trajectories["position"]
boxes = cuspatial.spatial_bounds(
xys["x"], xys["y"], trajectories["length"], trajectories["position"]
)
print(boxes.head())