diff --git a/.gitignore b/.gitignore index eeff2cd0e..e5628cf20 100644 --- a/.gitignore +++ b/.gitignore @@ -17,12 +17,12 @@ DartConfiguration.tcl ## Python build directories & artifacts dist/ -cudf.egg-info/ +cuspatial.egg-info/ python/build python/*/build -python/cuspatial/*/bindings/**/*.cpp -python/cuspatial/*/bindings/**/*.h -python/cuspatial/*/bindings/.nfs* +python/cuspatial/*/_lib/**/*.cpp +python/cuspatial/*/_lib/**/*.h +python/cuspatial/*/_lib/.nfs* python/cuspatial/*.ipynb python/cuspatial/.ipynb_checkpoints .Python diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..964fefe2c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,23 @@ +repos: + - repo: https://github.com/timothycrosley/isort + rev: 4.3.21 + hooks: + - id: isort + - repo: https://github.com/ambv/black + rev: stable + hooks: + - id: black + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.7 + hooks: + - id: flake8 + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.7 + hooks: + - id: flake8 + alias: flake8-cython + name: flake8-cython + args: ["--config=python/cuspatial/.flake8.cython"] + types: [cython] +default_language_version: + python: python3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c7f38da8..57556cbc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,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 ## Bug Fixes diff --git a/ci/checks/style.sh b/ci/checks/style.sh index 2d371f812..817732c53 100644 --- a/ci/checks/style.sh +++ b/ci/checks/style.sh @@ -38,3 +38,32 @@ else echo -e "\n\n>>>> PASSED: isort style check\n\n" fi +if [ "$BLACK_RETVAL" != "0" ]; then + echo -e "\n\n>>>> FAILED: black style check; begin output\n\n" + echo -e "$BLACK" + echo -e "\n\n>>>> FAILED: black style check; end output\n\n" +else + echo -e "\n\n>>>> PASSED: black style check\n\n" +fi + +if [ "$FLAKE_RETVAL" != "0" ]; then + echo -e "\n\n>>>> FAILED: flake8 style check; begin output\n\n" + echo -e "$FLAKE" + echo -e "\n\n>>>> FAILED: flake8 style check; end output\n\n" +else + echo -e "\n\n>>>> PASSED: flake8 style check\n\n" +fi + +if [ "$FLAKE_CYTHON_RETVAL" != "0" ]; then + echo -e "\n\n>>>> FAILED: flake8-cython style check; begin output\n\n" + echo -e "$FLAKE_CYTHON" + echo -e "\n\n>>>> FAILED: flake8-cython style check; end output\n\n" +else + echo -e "\n\n>>>> PASSED: flake8-cython style check\n\n" +fi + +RETVALS=($ISORT_RETVAL $BLACK_RETVAL $FLAKE_RETVAL $FLAKE_CYTHON_RETVAL) +IFS=$'\n' +RETVAL=`echo "${RETVALS[*]}" | sort -nr | head -n1` + +exit $RETVAL \ No newline at end of file diff --git a/python/cuspatial/cuspatial/__init__.py b/python/cuspatial/cuspatial/__init__.py index 5a994510d..324ceb4e4 100644 --- a/python/cuspatial/cuspatial/__init__.py +++ b/python/cuspatial/cuspatial/__init__.py @@ -1,9 +1,3 @@ -from .core.trajectory import ( - derive, - distance_and_speed, - spatial_bounds, - subset_trajectory_id -) from .core.gis import ( directed_hausdorff_distance, haversine_distance, @@ -11,10 +5,16 @@ point_in_polygon_bitmap, window_points, ) +from .core.trajectory import ( + derive, + distance_and_speed, + spatial_bounds, + subset_trajectory_id, +) from .io.soa import ( - read_uint, read_its_timestamps, read_points_lonlat, read_points_xy_km, - read_polygon + read_polygon, + read_uint, ) diff --git a/python/cuspatial/cuspatial/_lib/soa_readers.pxd b/python/cuspatial/cuspatial/_lib/soa_readers.pxd index cc8345013..2e9351761 100644 --- a/python/cuspatial/cuspatial/_lib/soa_readers.pxd +++ b/python/cuspatial/cuspatial/_lib/soa_readers.pxd @@ -8,10 +8,26 @@ from cudf._lib.cudf cimport * from libcpp.pair cimport pair cdef extern from "soa_readers.hpp" namespace "cuspatial" nogil: - cdef gdf_column read_uint32_soa(const char *pnt_fn) except + - cdef gdf_column read_timestamp_soa(const char *ts_fn) except + - cdef pair[gdf_column, gdf_column] read_lonlat_points_soa(const char *pnt_fn) except + - cdef pair[gdf_column, gdf_column] read_xy_points_soa(const char *pnt_fn) except + - cdef void read_polygon_soa(const char *ply_fn, gdf_column* ply_fpos, - gdf_column* ply_rpos, gdf_column* ply_x, - gdf_column* ply_y) except + + cdef gdf_column read_uint32_soa( + const char *pnt_fn + ) except + + + cdef gdf_column read_timestamp_soa( + const char *ts_fn + ) except + + + cdef pair[gdf_column, gdf_column] read_lonlat_points_soa( + const char *pnt_fn + ) except + + + cdef pair[gdf_column, gdf_column] read_xy_points_soa( + const char *pnt_fn + ) except + + + cdef void read_polygon_soa( + const char *ply_fn, + gdf_column* ply_fpos, + gdf_column* ply_rpos, + gdf_column* ply_x, + gdf_column* ply_y + ) except + diff --git a/python/cuspatial/cuspatial/_lib/soa_readers.pyx b/python/cuspatial/cuspatial/_lib/soa_readers.pyx index 8f69c6992..b182e8bcb 100644 --- a/python/cuspatial/cuspatial/_lib/soa_readers.pyx +++ b/python/cuspatial/cuspatial/_lib/soa_readers.pyx @@ -1,78 +1,104 @@ +# Copyright (c) 2019, NVIDIA CORPORATION. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + + from cudf.core.column import Column from cudf._lib.cudf import * from libc.stdlib cimport calloc, malloc, free from libcpp.pair cimport pair cpdef cpp_read_uint_soa(soa_file_name): - # print("in cpp_read_id_soa, reading ",soa_file_name) cdef bytes py_bytes = soa_file_name.encode() cdef char* c_string = py_bytes cdef gdf_column c_id + with nogil: - c_id = read_uint32_soa(c_string) + c_id = read_uint32_soa( + c_string + ) + id_data, id_mask = gdf_column_to_column_mem(&c_id) - id=Column.from_mem_views(id_data,id_mask) + id = Column.from_mem_views(id_data, id_mask) return id cpdef cpp_read_ts_soa(soa_file_name): - # print("in cpp_read_ts_soa, reading ",soa_file_name) cdef bytes py_bytes = soa_file_name.encode() cdef char* c_string = py_bytes cdef gdf_column c_ts + with nogil: - c_ts = read_timestamp_soa(c_string) + c_ts = read_timestamp_soa( + c_string + ) + ts_data, ts_mask = gdf_column_to_column_mem(&c_ts) - ts=Column.from_mem_views(ts_data,ts_mask) + ts = Column.from_mem_views(ts_data, ts_mask) return ts cpdef cpp_read_pnt_lonlat_soa(soa_file_name): - # print("in cpp_read_pnt_lonlat_soa, reading ",soa_file_name) cdef bytes py_bytes = soa_file_name.encode() cdef char* c_string = py_bytes cdef pair[gdf_column, gdf_column] columns + with nogil: - columns = read_lonlat_points_soa(c_string) + columns = read_lonlat_points_soa( + c_string + ) + lon_data, lon_mask = gdf_column_to_column_mem(&columns.first) - lon=Column.from_mem_views(lon_data,lon_mask) + lon = Column.from_mem_views(lon_data, lon_mask) lat_data, lat_mask = gdf_column_to_column_mem(&columns.second) - lat=Column.from_mem_views(lat_data,lat_mask) + lat = Column.from_mem_views(lat_data, lat_mask) - return lon,lat + return lon, lat cpdef cpp_read_pnt_xy_soa(soa_file_name): - # print("in cpp_read_pnt_xy_soa, reading ",soa_file_name) cdef bytes py_bytes = soa_file_name.encode() cdef char* c_string = py_bytes cdef pair[gdf_column, gdf_column] columns + with nogil: - columns = read_xy_points_soa(c_string) + columns = read_xy_points_soa( + c_string + ) + x_data, x_mask = gdf_column_to_column_mem(&columns.first) - x=Column.from_mem_views(x_data,x_mask) + x = Column.from_mem_views(x_data, x_mask) y_data, y_mask = gdf_column_to_column_mem(&columns.second) - y=Column.from_mem_views(y_data,y_mask) + y = Column.from_mem_views(y_data, y_mask) - return x,y + return x, y cpdef cpp_read_polygon_soa(soa_file_name): - # print("in cpp_read_polygon_soa, reading ",soa_file_name) cdef bytes py_bytes = soa_file_name.encode() cdef char* c_string = py_bytes - cdef gdf_column* c_ply_fpos=malloc(sizeof(gdf_column)) - cdef gdf_column* c_ply_rpos=malloc(sizeof(gdf_column)) - cdef gdf_column* c_ply_x=malloc(sizeof(gdf_column)) - cdef gdf_column* c_ply_y=malloc(sizeof(gdf_column)) + cdef gdf_column* c_ply_fpos = malloc(sizeof(gdf_column)) + cdef gdf_column* c_ply_rpos = malloc(sizeof(gdf_column)) + cdef gdf_column* c_ply_x = malloc(sizeof(gdf_column)) + cdef gdf_column* c_ply_y = malloc(sizeof(gdf_column)) + with nogil: - read_polygon_soa(c_string,c_ply_fpos, c_ply_rpos, c_ply_x, c_ply_y) + read_polygon_soa( + c_string, + c_ply_fpos, + c_ply_rpos, + c_ply_x, + c_ply_y + ) + f_data, f_mask = gdf_column_to_column_mem(c_ply_fpos) - f_pos=Column.from_mem_views(f_data,f_mask) + f_pos = Column.from_mem_views(f_data, f_mask) r_data, r_mask = gdf_column_to_column_mem(c_ply_rpos) - r_pos=Column.from_mem_views(r_data,r_mask) + r_pos = Column.from_mem_views(r_data, r_mask) x_data, x_mask = gdf_column_to_column_mem(c_ply_x) - x=Column.from_mem_views(x_data,x_mask) + x = Column.from_mem_views(x_data, x_mask) y_data, y_mask = gdf_column_to_column_mem(c_ply_y) - y=Column.from_mem_views(y_data,y_mask) - - return f_pos,r_pos,x,y + y = Column.from_mem_views(y_data, y_mask) + return f_pos, r_pos, x, y diff --git a/python/cuspatial/cuspatial/_lib/spatial.pxd b/python/cuspatial/cuspatial/_lib/spatial.pxd index 177f32ba5..b86de08a1 100644 --- a/python/cuspatial/cuspatial/_lib/spatial.pxd +++ b/python/cuspatial/cuspatial/_lib/spatial.pxd @@ -9,33 +9,44 @@ from cudf._lib.cudf cimport * from libcpp.pair cimport pair cdef extern from "point_in_polygon.hpp" namespace "cuspatial" nogil: - cdef gdf_column point_in_polygon_bitmap(const gdf_column& pnt_x, - const gdf_column& pnt_y, - const gdf_column& ply_fpos, - const gdf_column& ply_rpos, - const gdf_column& ply_x, - const gdf_column& ply_y) except + + cdef gdf_column point_in_polygon_bitmap( + const gdf_column& pnt_x, + const gdf_column& pnt_y, + const gdf_column& ply_fpos, + const gdf_column& ply_rpos, + const gdf_column& ply_x, + const gdf_column& ply_y + ) except + -cdef extern from "coordinate_transform.hpp" namespace "cuspatial" nogil: - cdef pair[gdf_column, gdf_column] lonlat_to_coord(const gdf_scalar cam_x, - const gdf_scalar cam_y, - const gdf_column & in_x, - const gdf_column & in_y) except + +cdef extern from "coordinate_transform.hpp" namespace "cuspatial" nogil: + cdef pair[gdf_column, gdf_column] lonlat_to_coord( + const gdf_scalar& cam_x, + const gdf_scalar& cam_y, + const gdf_column& in_x, + const gdf_column& in_y + ) except + -cdef extern from "haversine.hpp" namespace "cuspatial" nogil: - gdf_column haversine_distance(const gdf_column& x1, const gdf_column& y1, - const gdf_column& x2, const gdf_column& y2) except + +cdef extern from "haversine.hpp" namespace "cuspatial" nogil: + gdf_column haversine_distance( + const gdf_column& x1, + const gdf_column& y1, + const gdf_column& x2, + const gdf_column& y2 + ) except + -cdef extern from "hausdorff.hpp" namespace "cuspatial" nogil: - gdf_column& directed_hausdorff_distance(const gdf_column& coor_x, - const gdf_column& coor_y, - const gdf_column& cnt)except + +cdef extern from "hausdorff.hpp" namespace "cuspatial" nogil: + gdf_column& directed_hausdorff_distance( + const gdf_column& coor_x, + const gdf_column& coor_y, + const gdf_column& cnt + ) except + cdef extern from "query.hpp" namespace "cuspatial" nogil: - cdef pair[gdf_column, gdf_column] spatial_window_points( + cdef pair[gdf_column, gdf_column] spatial_window_points( const gdf_scalar& left, const gdf_scalar& bottom, const gdf_scalar& right, const gdf_scalar& top, const gdf_column& x, - const gdf_column& y) except + + const gdf_column& y + ) except + diff --git a/python/cuspatial/cuspatial/_lib/spatial.pyx b/python/cuspatial/cuspatial/_lib/spatial.pyx index d64c3a099..19c36b738 100644 --- a/python/cuspatial/cuspatial/_lib/spatial.pyx +++ b/python/cuspatial/cuspatial/_lib/spatial.pyx @@ -1,3 +1,11 @@ +# Copyright (c) 2019, NVIDIA CORPORATION. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + + from cudf._lib.cudf import * from cudf._lib.cudf cimport * from cudf.core.column import Column @@ -6,9 +14,9 @@ from libcpp.pair cimport pair from libc.stdlib cimport calloc, malloc, free -cpdef cpp_point_in_polygon_bitmap(points_x, points_y, - poly_fpos, poly_rpos, - poly_x, poly_y): +cpdef cpp_point_in_polygon_bitmap( + points_x, points_y, poly_fpos, poly_rpos, poly_x, poly_y +): points_x = points_x.astype('float64')._column points_y = points_y.astype('float64')._column poly_fpos = poly_fpos.astype('int32')._column @@ -26,9 +34,14 @@ cpdef cpp_point_in_polygon_bitmap(points_x, points_y, cdef gdf_column* result_bitmap = malloc(sizeof(gdf_column)) with nogil: - result_bitmap[0] = point_in_polygon_bitmap(c_points_x[0], c_points_y[0], - c_poly_fpos[0], c_poly_rpos[0], - c_poly_x[0],c_poly_y[0]) + result_bitmap[0] = point_in_polygon_bitmap( + c_points_x[0], + c_points_y[0], + c_poly_fpos[0], + c_poly_rpos[0], + c_poly_x[0], + c_poly_y[0] + ) data, mask = gdf_column_to_column_mem(result_bitmap) free(c_points_x) @@ -42,21 +55,26 @@ cpdef cpp_point_in_polygon_bitmap(points_x, points_y, return bitmap -cpdef cpp_haversine_distance(x1,y1,x2,y2): +cpdef cpp_haversine_distance(x1, y1, x2, y2): x1 = x1.astype('float64')._column y1 = y1.astype('float64')._column x2 = x2.astype('float64')._column y2 = y2.astype('float64')._column - cdef gdf_column* c_x1= column_view_from_column(x1) + cdef gdf_column* c_x1 = column_view_from_column(x1) cdef gdf_column* c_y1 = column_view_from_column(y1) - cdef gdf_column* c_x2= column_view_from_column(x2) + cdef gdf_column* c_x2 = column_view_from_column(x2) cdef gdf_column* c_y2 = column_view_from_column(y2) cdef gdf_column* c_h_dist = malloc(sizeof(gdf_column)) with nogil: - c_h_dist[0] =haversine_distance(c_x1[0],c_y1[0],c_x2[0],c_y2[0]) + c_h_dist[0] = haversine_distance( + c_x1[0], + c_y1[0], + c_x2[0], + c_y2[0] + ) data, mask = gdf_column_to_column_mem(c_h_dist) free(c_x1) @@ -81,8 +99,12 @@ cpdef cpp_lonlat2coord(cam_lon, cam_lat, in_lon, in_lat): cpdef pair[gdf_column, gdf_column] coords with nogil: - coords = lonlat_to_coord(c_cam_lon[0], c_cam_lat[0], - c_in_lon[0], c_in_lat[0]) + coords = lonlat_to_coord( + c_cam_lon[0], + c_cam_lat[0], + c_in_lon[0], + c_in_lat[0] + ) x_data, x_mask = gdf_column_to_column_mem(&coords.first) y_data, y_mask = gdf_column_to_column_mem(&coords.second) @@ -90,12 +112,12 @@ cpdef cpp_lonlat2coord(cam_lon, cam_lat, in_lon, in_lat): free(c_in_lon) free(c_in_lat) - x=Column.from_mem_views(x_data, x_mask) - y=Column.from_mem_views(y_data, y_mask) + x = Column.from_mem_views(x_data, x_mask) + y = Column.from_mem_views(y_data, y_mask) return Series(x), Series(y) -cpdef cpp_directed_hausdorff_distance(coor_x,coor_y,cnt): +cpdef cpp_directed_hausdorff_distance(coor_x, coor_y, cnt): coor_x = coor_x.astype('float64')._column coor_y = coor_y.astype('float64')._column cnt = cnt.astype('int32')._column @@ -104,10 +126,14 @@ cpdef cpp_directed_hausdorff_distance(coor_x,coor_y,cnt): cdef gdf_column* c_cnt = column_view_from_column(cnt) cdef gdf_column* c_dist = malloc(sizeof(gdf_column)) with nogil: - c_dist[0]=directed_hausdorff_distance(c_coor_x[0],c_coor_y[0],c_cnt[0]) + c_dist[0] = directed_hausdorff_distance( + c_coor_x[0], + c_coor_y[0], + c_cnt[0] + ) dist_data, dist_mask = gdf_column_to_column_mem(c_dist) - dist=Column.from_mem_views(dist_data,dist_mask) + dist = Column.from_mem_views(dist_data, dist_mask) return Series(dist) @@ -132,14 +158,19 @@ cpdef cpp_spatial_window_points(left, bottom, right, top, x, y): cdef pair[gdf_column, gdf_column] xy with nogil: - xy = spatial_window_points(c_left[0], c_bottom[0], c_right[0], c_top[0], - c_x[0], c_y[0]) + xy = spatial_window_points( + c_left[0], + c_bottom[0], + c_right[0], + c_top[0], + c_x[0], + c_y[0] + ) outx_data, outx_mask = gdf_column_to_column_mem(&xy.first) outy_data, outy_mask = gdf_column_to_column_mem(&xy.second) - outx=Column.from_mem_views(outx_data, outx_mask) - outy=Column.from_mem_views(outy_data, outy_mask) + outx = Column.from_mem_views(outx_data, outx_mask) + outy = Column.from_mem_views(outy_data, outy_mask) return Series(outx), Series(outy) - diff --git a/python/cuspatial/cuspatial/_lib/trajectory.pxd b/python/cuspatial/cuspatial/_lib/trajectory.pxd index 1d6875fb2..fd09df842 100644 --- a/python/cuspatial/cuspatial/_lib/trajectory.pxd +++ b/python/cuspatial/cuspatial/_lib/trajectory.pxd @@ -10,36 +10,43 @@ from libcpp.pair cimport pair cdef extern from "trajectory.hpp" namespace "cuspatial" nogil: - cdef gdf_size_type derive_trajectories(const gdf_column& coor_x, - const gdf_column& coor_y, - const gdf_column& pid, - const gdf_column& ts, - gdf_column& tid, - gdf_column& len, - gdf_column& pos) except + + cdef gdf_size_type derive_trajectories( + const gdf_column& coor_x, + const gdf_column& coor_y, + const gdf_column& pid, + const gdf_column& ts, + gdf_column& tid, + gdf_column& len, + gdf_column& pos + ) except + - cdef pair[gdf_column, gdf_column] trajectory_distance_and_speed( - const gdf_column& x, - const gdf_column& y, - const gdf_column& ts, - const gdf_column& len, - const gdf_column& pos) except + + cdef pair[gdf_column, gdf_column] trajectory_distance_and_speed( + const gdf_column& x, + const gdf_column& y, + const gdf_column& ts, + const gdf_column& len, + const gdf_column& pos + ) except + - cdef void trajectory_spatial_bounds(const gdf_column& x, - const gdf_column& y, - const gdf_column& len, - const gdf_column& pos, - gdf_column& bbox_x1, - gdf_column& bbox_y1, - gdf_column& bbox_x2, - gdf_column& bbox_y2) except + - - cdef gdf_size_type subset_trajectory_id(const gdf_column& id, - const gdf_column& in_x, - const gdf_column& in_y, - const gdf_column& in_id, - const gdf_column& in_timestamp, - gdf_column& out_x, - gdf_column& out_y, - gdf_column& out_id, - gdf_column& out_timestamp) except + + cdef void trajectory_spatial_bounds( + const gdf_column& x, + const gdf_column& y, + const gdf_column& len, + const gdf_column& pos, + gdf_column& bbox_x1, + gdf_column& bbox_y1, + gdf_column& bbox_x2, + gdf_column& bbox_y2 + ) except + + + cdef gdf_size_type subset_trajectory_id( + const gdf_column& id, + const gdf_column& in_x, + const gdf_column& in_y, + const gdf_column& in_id, + const gdf_column& in_timestamp, + gdf_column& out_x, + gdf_column& out_y, + gdf_column& out_id, + gdf_column& out_timestamp + ) except + diff --git a/python/cuspatial/cuspatial/_lib/trajectory.pyx b/python/cuspatial/cuspatial/_lib/trajectory.pyx index 93fff0ae9..8176efba7 100644 --- a/python/cuspatial/cuspatial/_lib/trajectory.pyx +++ b/python/cuspatial/cuspatial/_lib/trajectory.pyx @@ -1,3 +1,11 @@ +# Copyright (c) 2019, NVIDIA CORPORATION. + +# cython: profile=False +# distutils: language = c++ +# cython: embedsignature = True +# cython: language_level = 3 + + from cudf import Series, DataFrame from cudf.core.column import Column from cudf._lib.cudf import * @@ -19,24 +27,31 @@ cpdef cpp_derive_trajectories(x, y, object_id, timestamp): cdef gdf_column* c_pos = malloc(sizeof(gdf_column)) with nogil: - num_trajectories = derive_trajectories(c_x[0], c_y[0], - c_object_id[0], - c_timestamp[0], - c_trajectory_id[0], - c_length[0], c_pos[0]) + num_trajectories = derive_trajectories( + c_x[0], c_y[0], + c_object_id[0], + c_timestamp[0], + c_trajectory_id[0], + c_length[0], c_pos[0] + ) traj_id_data, traj_id_mask = gdf_column_to_column_mem(c_trajectory_id) length_data, length_mask = gdf_column_to_column_mem(c_length) pos_data, pos_mask = gdf_column_to_column_mem(c_pos) - trajectory_id = Column.from_mem_views(traj_id_data, - traj_id_mask) + trajectory_id = Column.from_mem_views(traj_id_data, traj_id_mask) length = Column.from_mem_views(length_data, length_mask) pos = Column.from_mem_views(pos_data, pos_mask) - return (num_trajectories, - DataFrame({'trajectory_id': Series(trajectory_id), - 'length': Series(length), - 'position': Series(pos)})) + return ( + num_trajectories, + DataFrame( + { + 'trajectory_id': Series(trajectory_id), + 'length': Series(length), + 'position': Series(pos) + } + ) + ) cpdef cpp_trajectory_distance_and_speed(x, y, timestamp, length, pos): @@ -55,7 +70,7 @@ cpdef cpp_trajectory_distance_and_speed(x, y, timestamp, length, pos): with nogil: c_distance_speed = trajectory_distance_and_speed(c_x[0], c_y[0], c_timestamp[0], - c_length[0],c_pos[0]) + c_length[0], c_pos[0]) dist_data, dist_mask = gdf_column_to_column_mem(&c_distance_speed.first) speed_data, speed_mask = gdf_column_to_column_mem(&c_distance_speed.second) @@ -64,7 +79,7 @@ cpdef cpp_trajectory_distance_and_speed(x, y, timestamp, length, pos): return Series(dist), Series(speed) -cpdef cpp_trajectory_spatial_bounds(coor_x,coor_y,length,pos): +cpdef cpp_trajectory_spatial_bounds(coor_x, coor_y, length, pos): coor_x = coor_x.astype('float64')._column coor_y = coor_y.astype('float64')._column length = length.astype('int32')._column @@ -79,18 +94,25 @@ cpdef cpp_trajectory_spatial_bounds(coor_x,coor_y,length,pos): cdef gdf_column* c_y2 = malloc(sizeof(gdf_column)) with nogil: - trajectory_spatial_bounds(c_coor_x[0], c_coor_y[0], - c_length[0], c_pos[0], - c_x1[0], c_y1[0], c_x2[0], c_y2[0]) + trajectory_spatial_bounds( + c_coor_x[0], + c_coor_y[0], + c_length[0], + c_pos[0], + c_x1[0], + c_y1[0], + c_x2[0], + c_y2[0] + ) x1_data, x1_mask = gdf_column_to_column_mem(c_x1) - x1 = Column.from_mem_views(x1_data,x1_mask) + x1 = Column.from_mem_views(x1_data, x1_mask) x2_data, x2_mask = gdf_column_to_column_mem(c_x2) - x2 = Column.from_mem_views(x2_data,x2_mask) + x2 = Column.from_mem_views(x2_data, x2_mask) y1_data, y1_mask = gdf_column_to_column_mem(c_y1) - y1 = Column.from_mem_views(y1_data,y1_mask) + y1 = Column.from_mem_views(y1_data, y1_mask) y2_data, y2_mask = gdf_column_to_column_mem(c_y2) - y2 = Column.from_mem_views(y2_data,y2_mask) + y2 = Column.from_mem_views(y2_data, y2_mask) return DataFrame({'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2}) @@ -112,16 +134,16 @@ cpdef cpp_subset_trajectory_id(ids, in_x, in_y, in_id, in_timestamp): cdef gdf_column* c_out_timestamp = malloc(sizeof(gdf_column)) with nogil: - count = subset_trajectory_id(c_id[0], c_in_x[0], c_in_y[0], c_in_id[0], + count = subset_trajectory_id(c_id[0], c_in_x[0], c_in_y[0], c_in_id[0], c_in_timestamp[0], c_out_x[0], c_out_y[0], c_out_id[0], c_out_timestamp[0]) x_data, x_mask = gdf_column_to_column_mem(c_out_x) - x = Column.from_mem_views(x_data,x_mask) + x = Column.from_mem_views(x_data, x_mask) y_data, y_mask = gdf_column_to_column_mem(c_out_y) - y = Column.from_mem_views(y_data,y_mask) + y = Column.from_mem_views(y_data, y_mask) id_data, id_mask = gdf_column_to_column_mem(c_out_id) - ids = Column.from_mem_views(id_data,id_mask) + ids = Column.from_mem_views(id_data, id_mask) timestamp_data, timestamp_mask = gdf_column_to_column_mem(c_out_timestamp) timestamp = Column.from_mem_views(timestamp_data, timestamp_mask) diff --git a/python/cuspatial/cuspatial/_version.py b/python/cuspatial/cuspatial/_version.py index bb0532a94..2db1c5542 100644 --- a/python/cuspatial/cuspatial/_version.py +++ b/python/cuspatial/cuspatial/_version.py @@ -1,4 +1,3 @@ - # This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build @@ -58,17 +57,20 @@ class NotThisMethod(Exception): def register_vcs_handler(vcs, method): # decorator """Decorator to mark a method as the handler for a particular VCS.""" + def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: HANDLERS[vcs] = {} HANDLERS[vcs][method] = f return f + return decorate -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): +def run_command( + commands, args, cwd=None, verbose=False, hide_stderr=False, env=None +): """Call the given command(s).""" assert isinstance(commands, list) p = None @@ -76,10 +78,13 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, try: dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) + p = subprocess.Popen( + [c] + args, + cwd=cwd, + env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr else None), + ) break except EnvironmentError: e = sys.exc_info()[1] @@ -116,16 +121,22 @@ def versions_from_parentdir(parentdir_prefix, root, verbose): for i in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} + return { + "version": dirname[len(parentdir_prefix) :], + "full-revisionid": None, + "dirty": False, + "error": None, + "date": None, + } else: rootdirs.append(root) root = os.path.dirname(root) # up a level if verbose: - print("Tried directories %s but none started with prefix %s" % - (str(rootdirs), parentdir_prefix)) + print( + "Tried directories %s but none started with prefix %s" + % (str(rootdirs), parentdir_prefix) + ) raise NotThisMethod("rootdir doesn't start with parentdir_prefix") @@ -181,7 +192,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) + tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)]) if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %d @@ -190,7 +201,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) + tags = set([r for r in refs if re.search(r"\d", r)]) if verbose: print("discarding '%s', no digits" % ",".join(refs - tags)) if verbose: @@ -198,19 +209,26 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): for ref in sorted(tags): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] + r = ref[len(tag_prefix) :] if verbose: print("picking %s" % r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} + return { + "version": r, + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": None, + "date": date, + } # no suitable tags, so version is "0+unknown", but full hex is still there if verbose: print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} + return { + "version": "0+unknown", + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": "no suitable tags", + "date": None, + } @register_vcs_handler("git", "pieces_from_vcs") @@ -225,8 +243,9 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) + out, rc = run_command( + GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True + ) if rc != 0: if verbose: print("Directory %s not under git control" % root) @@ -234,10 +253,19 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%s*" % tag_prefix], - cwd=root) + describe_out, rc = run_command( + GITS, + [ + "describe", + "--tags", + "--dirty", + "--always", + "--long", + "--match", + "%s*" % tag_prefix, + ], + cwd=root, + ) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") @@ -260,17 +288,18 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): dirty = git_describe.endswith("-dirty") pieces["dirty"] = dirty if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] + git_describe = git_describe[: git_describe.rindex("-dirty")] # now we have TAG-NUM-gHEX or HEX if "-" in git_describe: # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) + mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) if not mo: # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%s'" - % describe_out) + pieces["error"] = ( + "unable to parse git-describe output: '%s'" % describe_out + ) return pieces # tag @@ -279,10 +308,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): if verbose: fmt = "tag '%s' doesn't start with prefix '%s'" print(fmt % (full_tag, tag_prefix)) - pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" - % (full_tag, tag_prefix)) + pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( + full_tag, + tag_prefix, + ) return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] + pieces["closest-tag"] = full_tag[len(tag_prefix) :] # distance: number of commits since tag pieces["distance"] = int(mo.group(2)) @@ -293,13 +324,15 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): else: # HEX: no tags pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) + count_out, rc = run_command( + GITS, ["rev-list", "HEAD", "--count"], cwd=root + ) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], - cwd=root)[0].strip() + date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[ + 0 + ].strip() pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces @@ -330,8 +363,7 @@ def render_pep440(pieces): rendered += ".dirty" else: # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) + rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" return rendered @@ -445,11 +477,13 @@ def render_git_describe_long(pieces): def render(pieces, style): """Render the given version pieces into the requested style.""" if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} + return { + "version": "unknown", + "full-revisionid": pieces.get("long"), + "dirty": None, + "error": pieces["error"], + "date": None, + } if not style or style == "default": style = "pep440" # the default @@ -469,9 +503,13 @@ def render(pieces, style): else: raise ValueError("unknown style '%s'" % style) - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} + return { + "version": rendered, + "full-revisionid": pieces["long"], + "dirty": pieces["dirty"], + "error": None, + "date": pieces.get("date"), + } def get_versions(): @@ -485,8 +523,9 @@ def get_versions(): verbose = cfg.verbose try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, - verbose) + return git_versions_from_keywords( + get_keywords(), cfg.tag_prefix, verbose + ) except NotThisMethod: pass @@ -495,13 +534,16 @@ def get_versions(): # versionfile_source is the relative path from the top of the source # tree (where the .git directory might live) to this file. Invert # this to find the root from __file__. - for i in cfg.versionfile_source.split('/'): + for i in cfg.versionfile_source.split("/"): root = os.path.dirname(root) except NameError: - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None} + return { + "version": "0+unknown", + "full-revisionid": None, + "dirty": None, + "error": "unable to find root of source tree", + "date": None, + } try: pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) @@ -515,6 +557,10 @@ def get_versions(): except NotThisMethod: pass - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", "date": None} + return { + "version": "0+unknown", + "full-revisionid": None, + "dirty": None, + "error": "unable to compute version", + "date": None, + } diff --git a/python/cuspatial/cuspatial/core/gis.py b/python/cuspatial/cuspatial/core/gis.py index 173e9029b..12977dc09 100644 --- a/python/cuspatial/cuspatial/core/gis.py +++ b/python/cuspatial/cuspatial/core/gis.py @@ -1,6 +1,7 @@ # Copyright (c) 2019, NVIDIA CORPORATION. from cudf import DataFrame + from cuspatial._lib.spatial import ( cpp_directed_hausdorff_distance, cpp_haversine_distance, @@ -9,6 +10,7 @@ cpp_spatial_window_points, ) + def directed_hausdorff_distance(x, y, count): """ Compute the directed Hausdorff distances between any groupings of polygons. @@ -17,7 +19,7 @@ def directed_hausdorff_distance(x, y, count): x: x coordinates y: y coordinates count: size of each polygon - + Parameters ---------- {params} @@ -27,6 +29,7 @@ def directed_hausdorff_distance(x, y, count): """ return cpp_directed_hausdorff_distance(x, y, count) + def haversine_distance(p1_lat, p1_lon, p2_lat, p2_lon): """ Compute the haversine distances between an arbitrary list of lat/lon pairs @@ -36,7 +39,7 @@ def haversine_distance(p1_lat, p1_lon, p2_lat, p2_lon): p1_lon: longitude of first set of coords p2_lat: latitude of second set of coords p2_lon: longitude of second set of coords - + Parameters ---------- {params} @@ -46,7 +49,10 @@ def haversine_distance(p1_lat, p1_lon, p2_lat, p2_lon): """ return cpp_haversine_distance(p1_lat, p1_lon, p2_lat, p2_lon) -def lonlat_to_xy_km_coordinates(camera_lon, camera_lat, lon_coords, lat_coords): + +def lonlat_to_xy_km_coordinates( + camera_lon, camera_lat, lon_coords, lat_coords +): """ Convert lonlat coordinates to km x,y coordinates based on some camera origin. @@ -55,7 +61,7 @@ def lonlat_to_xy_km_coordinates(camera_lon, camera_lat, lon_coords, lat_coords): camera_lat: float64 - latitude camera lon_coords: Series of longitude coords to convert to x lat_coords: Series of latitude coords to convert to y - + Parameters ---------- {params} @@ -64,12 +70,17 @@ def lonlat_to_xy_km_coordinates(camera_lon, camera_lat, lon_coords, lat_coords): DataFrame: 'x', 'y' columns for new km positions of coords """ result = cpp_lonlat2coord(camera_lon, camera_lat, lon_coords, lat_coords) - return DataFrame({'x': result[0], - 'y': result[1] - }) + return DataFrame({"x": result[0], "y": result[1]}) + -def point_in_polygon_bitmap(x_points, y_points, - polygon_ids, polygon_end_indices, polygons_x, polygons_y): +def point_in_polygon_bitmap( + x_points, + y_points, + polygon_ids, + polygon_end_indices, + polygons_x, + polygons_y, +): """ Compute from a set of points and a set of polygons which points fall within which polygons. @@ -91,14 +102,19 @@ def point_in_polygon_bitmap(x_points, y_points, true or false for each of 32 polygons. """ return cpp_point_in_polygon_bitmap( - x_points, y_points, - polygon_ids, polygon_end_indices, polygons_x, polygons_y + x_points, + y_points, + polygon_ids, + polygon_end_indices, + polygons_x, + polygons_y, ) + def window_points(left, bottom, right, top, x, y): """ Return only the subset of coordinates that fall within the numerically closed borders [,] of the defined bounding box. - + params left: x coordinate of window left boundary bottom: y coordinate of window bottom boundary @@ -106,7 +122,7 @@ def window_points(left, bottom, right, top, x, y): top: y coordinate of window top boundary x: Series of x coordinates that may fall within the window y: Series of y coordinates that may fall within the window - + Parameters ---------- {params} @@ -116,4 +132,4 @@ def window_points(left, bottom, right, top, x, y): DataFrame: subset of x, y pairs above that fall within the window """ result = cpp_spatial_window_points(left, bottom, right, top, x, y) - return DataFrame({'x': result[0], 'y': result[1]}) + return DataFrame({"x": result[0], "y": result[1]}) diff --git a/python/cuspatial/cuspatial/core/trajectory.py b/python/cuspatial/cuspatial/core/trajectory.py index 40f058b29..ccd094913 100644 --- a/python/cuspatial/cuspatial/core/trajectory.py +++ b/python/cuspatial/cuspatial/core/trajectory.py @@ -1,29 +1,31 @@ # Copyright (c) 2019, NVIDIA CORPORATION. -import cudf import warnings + +import cudf + from cuspatial._lib.trajectory import ( - cpp_subset_trajectory_id, - cpp_trajectory_spatial_bounds, cpp_derive_trajectories, + cpp_subset_trajectory_id, cpp_trajectory_distance_and_speed, + cpp_trajectory_spatial_bounds, ) warnings.warn("Duplicates cuDF functionality", DeprecationWarning) + + def subset_trajectory_id(trajectory_ids, in_x, in_y, point_ids, timestamps): """ Deprecated """ return cpp_subset_trajectory_id( - trajectory_ids, - in_x, - in_y, - point_ids, - timestamps + trajectory_ids, in_x, in_y, point_ids, timestamps ) + def spatial_bounds( - x_coords, y_coords, trajectory_size, trajectory_end_position): + x_coords, y_coords, trajectory_size, trajectory_end_position +): """ Compute the bounding boxes of sets of trajectories. Parameters @@ -44,12 +46,10 @@ def spatial_bounds( 1 1.0 1.0 3.0 3.0 """ return cpp_trajectory_spatial_bounds( - x_coords, - y_coords, - trajectory_size, - trajectory_end_position + x_coords, y_coords, trajectory_size, trajectory_end_position ) + def derive(x_coords, y_coords, object_ids, timestamps): """ Derive trajectories from points, timestamps, and ids. Parameters @@ -79,12 +79,8 @@ def derive(x_coords, y_coords, object_ids, timestamps): 0 0 2 2 1 1 2 4) """ - return cpp_derive_trajectories( - x_coords, - y_coords, - object_ids, - timestamps - ) + return cpp_derive_trajectories(x_coords, y_coords, object_ids, timestamps) + def distance_and_speed(x_coords, y_coords, timestamps, length, position): """ Compute the distance travelled and speed of sets of trajectories @@ -112,17 +108,8 @@ def distance_and_speed(x_coords, y_coords, timestamps, length, position): 1 1000.0 111111.109375 """ result = cpp_trajectory_distance_and_speed( - x_coords, - y_coords, - timestamps, - length, - position + x_coords, y_coords, timestamps, length, position ) - df = cudf.DataFrame({ - 'meters': result[0], - 'speed': result[1] - }) - df.index.name = 'trajectory_id' + df = cudf.DataFrame({"meters": result[0], "speed": result[1]}) + df.index.name = "trajectory_id" return df - - diff --git a/python/cuspatial/cuspatial/io/soa.py b/python/cuspatial/cuspatial/io/soa.py index fbcadd96f..3925e0987 100644 --- a/python/cuspatial/cuspatial/io/soa.py +++ b/python/cuspatial/cuspatial/io/soa.py @@ -1,43 +1,48 @@ # Copyright (c) 2019, NVIDIA CORPORATION. -from cudf import Series, DataFrame +from cudf import DataFrame, Series + from cuspatial._lib.soa_readers import ( - cpp_read_uint_soa, - cpp_read_ts_soa, cpp_read_pnt_lonlat_soa, cpp_read_pnt_xy_soa, - cpp_read_polygon_soa + cpp_read_polygon_soa, + cpp_read_ts_soa, + cpp_read_uint_soa, ) + def read_uint(filename): """Reads a binary file of uint32s into a `cudf.Series` """ return Series(cpp_read_uint_soa(filename)) + def read_its_timestamps(filename): """Reads a binary formatted its_timestamp file into a Series of uint64s. """ return Series(cpp_read_ts_soa(filename)) + def read_points_lonlat(filename): """Reads a binary file of float64s into a `cudf.DataFrame`""" result = cpp_read_pnt_lonlat_soa(filename) - return DataFrame({'lon': result[0], - 'lat': result[1] - }) + return DataFrame({"lon": result[0], "lat": result[1]}) + def read_points_xy_km(filename): """Reads a binary file of float64s into a `cudf.DataFrame`""" result = cpp_read_pnt_xy_soa(filename) - return DataFrame({'x': result[0], - 'y': result[1] - }) + return DataFrame({"x": result[0], "y": result[1]}) + def read_polygon(filename): """Reads a binary file of float64s into a `cudf.DataFrame""" result = cpp_read_polygon_soa(filename) - return DataFrame({'f_pos': result[0], - 'r_pos': result[1], - 'x': result[2], - 'y': result[3] - }) + return DataFrame( + { + "f_pos": result[0], + "r_pos": result[1], + "x": result[2], + "y": result[3], + } + ) diff --git a/python/cuspatial/cuspatial/tests/test_hausdorff_distance.py b/python/cuspatial/cuspatial/tests/test_hausdorff_distance.py index 8897aeb33..a92ab8d4b 100644 --- a/python/cuspatial/cuspatial/tests/test_hausdorff_distance.py +++ b/python/cuspatial/cuspatial/tests/test_hausdorff_distance.py @@ -1,105 +1,112 @@ # Copyright (c) 2019, NVIDIA CORPORATION. -import pytest import numpy as np -import time +import pytest + import cudf from cudf.tests.utils import assert_eq -from cudf.core import column + import cuspatial + def test_zeros(): distance = cuspatial.directed_hausdorff_distance( - cudf.Series([0.0]), - cudf.Series([0.0]), - cudf.Series([1]) + cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([1]) ) assert cudf.Series(distance)[0] == 0 + def test_empty_x(): with pytest.raises(RuntimeError): - distance = cuspatial.directed_hausdorff_distance( - cudf.Series(), - cudf.Series([0]), - cudf.Series([0]) + distance = cuspatial.directed_hausdorff_distance( # noqa: F841 + cudf.Series(), cudf.Series([0]), cudf.Series([0]) ) + def test_empty_y(): with pytest.raises(RuntimeError): - distance = cuspatial.directed_hausdorff_distance( - cudf.Series([0]), - cudf.Series(), - cudf.Series([0]) + distance = cuspatial.directed_hausdorff_distance( # noqa: F841 + cudf.Series([0]), cudf.Series(), cudf.Series([0]) ) + def test_empty_counts(): with pytest.raises(RuntimeError): - distance = cuspatial.directed_hausdorff_distance( - cudf.Series([0]), - cudf.Series([0]), - cudf.Series() + distance = cuspatial.directed_hausdorff_distance( # noqa: F841 + cudf.Series([0]), cudf.Series([0]), cudf.Series() ) + def test_large(): - in_trajs=[] - in_trajs.append(np.array([[0,0],[1,0]])) - in_trajs.append(np.array([[0,-1],[1,-1]])) - out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs],0) - py_x=np.array(out_trajs[:,0]) - py_y=np.array(out_trajs[:,1]) + in_trajs = [] + in_trajs.append(np.array([[0, 0], [1, 0]])) + in_trajs.append(np.array([[0, -1], [1, -1]])) + out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs], 0) + py_x = np.array(out_trajs[:, 0]) + py_y = np.array(out_trajs[:, 1]) py_cnt = [] for traj in in_trajs: - py_cnt.append(len(traj)) + py_cnt.append(len(traj)) pnt_x = cudf.Series(py_x) pnt_y = cudf.Series(py_y) cnt = cudf.Series(py_cnt) - distance=cuspatial.directed_hausdorff_distance(pnt_x,pnt_y,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) expect = np.array([0, 1, 1, 0]) assert np.allclose(distance.data.to_array(), expect) + def test_count_one(): distance = cuspatial.directed_hausdorff_distance( - cudf.Series([0.0, 0.0]), - cudf.Series([0.0, 1.0]), - cudf.Series([1, 1])) + cudf.Series([0.0, 0.0]), cudf.Series([0.0, 1.0]), cudf.Series([1, 1]) + ) assert_eq(cudf.Series([0, 1.0, 1, 0]), cudf.Series(distance)) + def test_count_two(): distance = cuspatial.directed_hausdorff_distance( - cudf.Series([0.0, 0.0, 1.0, 0.0]), - cudf.Series([0.0, -1.0, 1.0, -1.0]), - cudf.Series([2, 2])) + cudf.Series([0.0, 0.0, 1.0, 0.0]), + cudf.Series([0.0, -1.0, 1.0, -1.0]), + cudf.Series([2, 2]), + ) print(cudf.Series(distance)) - assert_eq(cudf.Series([0.0, 1, 1.4142135623730951, 0]), - cudf.Series(distance)) + assert_eq( + cudf.Series([0.0, 1, 1.4142135623730951, 0]), cudf.Series(distance) + ) + def test_values(): - in_trajs=[] - in_trajs.append(np.array([[1,0],[2,1],[3,2],[5,3],[7,1]])) - in_trajs.append(np.array([[0,3],[2,5],[3,6],[6,5]])) - in_trajs.append(np.array([[1,4],[3,7],[6,4]])) - out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs],0) - py_x=np.array(out_trajs[:,0]) - py_y=np.array(out_trajs[:,1]) + in_trajs = [] + in_trajs.append(np.array([[1, 0], [2, 1], [3, 2], [5, 3], [7, 1]])) + in_trajs.append(np.array([[0, 3], [2, 5], [3, 6], [6, 5]])) + in_trajs.append(np.array([[1, 4], [3, 7], [6, 4]])) + out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs], 0) + py_x = np.array(out_trajs[:, 0]) + py_y = np.array(out_trajs[:, 1]) py_cnt = [] for traj in in_trajs: - py_cnt.append(len(traj)) - pnt_x =cudf.Series(py_x) - pnt_y= cudf.Series(py_y) - cnt= cudf.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) - expect = np.array([ - 0, 4.12310563, 4.0, - 3.60555128, 0.0, 1.41421356, - 4.47213595,1.41421356, 0.0]) + py_cnt.append(len(traj)) + pnt_x = cudf.Series(py_x) + pnt_y = cudf.Series(py_y) + cnt = cudf.Series(py_cnt) + distance = cuspatial.directed_hausdorff_distance(pnt_x, pnt_y, cnt) + + expect = np.array( + [ + 0, + 4.12310563, + 4.0, + 3.60555128, + 0.0, + 1.41421356, + 4.47213595, + 1.41421356, + 0.0, + ] + ) assert np.allclose(distance.data.to_array(), expect) + # def test_count_1(): # def test_count_2(): # def test_mismatched_x_y(): diff --git a/python/cuspatial/cuspatial/tests/test_haversine_distance.py b/python/cuspatial/cuspatial/tests/test_haversine_distance.py index 6f7452e91..7863f2884 100644 --- a/python/cuspatial/cuspatial/tests/test_haversine_distance.py +++ b/python/cuspatial/cuspatial/tests/test_haversine_distance.py @@ -1,12 +1,13 @@ # Copyright (c) 2019, NVIDIA CORPORATION. -import pytest import numpy as np +import pytest + import cudf -from cudf.core import column -from cudf.tests.utils import assert_eq + import cuspatial + def test_zeros(): distance = cuspatial.haversine_distance( cudf.Series([0.0]), @@ -16,49 +17,44 @@ def test_zeros(): ) assert cudf.Series(distance)[0] == 0 + def test_empty_x1(): with pytest.raises(RuntimeError): - distance = cuspatial.haversine_distance( - cudf.Series(), - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), + distance = cuspatial.haversine_distance( # noqa: F841 + cudf.Series(), cudf.Series([0]), cudf.Series([0]), cudf.Series([0]) ) + def test_empty_y1(): with pytest.raises(RuntimeError): - distance = cuspatial.haversine_distance( - cudf.Series([0]), - cudf.Series(), - cudf.Series([0]), - cudf.Series([0]), + distance = cuspatial.haversine_distance( # noqa: F841 + cudf.Series([0]), cudf.Series(), cudf.Series([0]), cudf.Series([0]) ) + def test_empty_x2(): with pytest.raises(RuntimeError): - distance = cuspatial.haversine_distance( - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), - cudf.Series(), + distance = cuspatial.haversine_distance( # noqa: F841 + cudf.Series([0]), cudf.Series([0]), cudf.Series([0]), cudf.Series() ) + def test_empty_y2(): with pytest.raises(RuntimeError): - distance = cuspatial.haversine_distance( - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), - cudf.Series(), + distance = cuspatial.haversine_distance( # noqa: F841 + cudf.Series([0]), cudf.Series([0]), cudf.Series([0]), cudf.Series() ) + def test_triple(): - cities = cudf.DataFrame({ - 'New York': [-74.0060,40.7128], - 'Paris': [2.3522,48.8566], - 'Sydney': [151.2093,-33.8688] - }) - cities = cities.set_index(['lat', 'lon']) + cities = cudf.DataFrame( + { + "New York": [-74.0060, 40.7128], + "Paris": [2.3522, 48.8566], + "Sydney": [151.2093, -33.8688], + } + ) + cities = cities.set_index(["lat", "lon"]) pnt_x1 = [] pnt_y1 = [] pnt_x2 = [] @@ -73,6 +69,21 @@ def test_triple(): cudf.Series(pnt_x1), cudf.Series(pnt_y1), cudf.Series(pnt_x2), - cudf.Series(pnt_y2) + cudf.Series(pnt_y2), + ) + assert np.allclose( + distance.to_array(), + [ + [ + 0.0, + 5.83724090e03, + 1.59887555e04, + 5.83724090e03, + 0.0, + 1.69604974e04, + 1.59887555e04, + 1.69604974e04, + 0.0, + ] + ], ) - assert np.allclose(distance.to_array(), [[0.0, 5.83724090e+03, 1.59887555e+04, 5.83724090e+03, 0.0, 1.69604974e+04, 1.59887555e+04, 1.69604974e+04, 0.0]]) diff --git a/python/cuspatial/cuspatial/tests/test_lonlat2coord.py b/python/cuspatial/cuspatial/tests/test_lonlat2coord.py index 79e379d30..2eb9f737a 100644 --- a/python/cuspatial/cuspatial/tests/test_lonlat2coord.py +++ b/python/cuspatial/cuspatial/tests/test_lonlat2coord.py @@ -1,98 +1,98 @@ # Copyright (c) 2019, NVIDIA CORPORATION. import pytest + import cudf from cudf.tests.utils import assert_eq -import numpy as np + import cuspatial + def test_camera_oob_0(): with pytest.raises(RuntimeError): - result = cuspatial.lonlat_to_xy_km_coordinates(-200, 0, - cudf.Series([0]), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( # noqa: F841 + -200, 0, cudf.Series([0]), cudf.Series([0]) ) + def test_camera_oob_1(): with pytest.raises(RuntimeError): - result = cuspatial.lonlat_to_xy_km_coordinates(200, 0, - cudf.Series([0]), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( # noqa: F841 + 200, 0, cudf.Series([0]), cudf.Series([0]) ) + def test_camera_oob_2(): with pytest.raises(RuntimeError): - result = cuspatial.lonlat_to_xy_km_coordinates(0, -100, - cudf.Series([0]), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( # noqa: F841 + 0, -100, cudf.Series([0]), cudf.Series([0]) ) + def test_camera_oob_3(): with pytest.raises(RuntimeError): - result = cuspatial.lonlat_to_xy_km_coordinates(0, 100, - cudf.Series([0]), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( # noqa: F841 + 0, 100, cudf.Series([0]), cudf.Series([0]) ) + @pytest.mark.parametrize("corner", [0, 1, 2, 3]) def test_camera_corners(corner): x = [-180, 180, -180, 180] y = [-90, 90, 90, -90] - result = cuspatial.lonlat_to_xy_km_coordinates(x[corner], y[corner], - cudf.Series(x[corner]), - cudf.Series(y[corner]) + result = cuspatial.lonlat_to_xy_km_coordinates( + x[corner], y[corner], cudf.Series(x[corner]), cudf.Series(y[corner]) ) - result = cudf.DataFrame({'x': [0], 'y': [0]}) + assert_eq(result, cudf.DataFrame({"x": [0.0], "y": [0.0]})) + def test_longest_distance(): - result = cuspatial.lonlat_to_xy_km_coordinates(-180, -90, - cudf.Series([180]), - cudf.Series([90]) + result = cuspatial.lonlat_to_xy_km_coordinates( + -180, -90, cudf.Series([180]), cudf.Series([90]) ) - assert_eq(result, cudf.DataFrame({'x': [-40000.0], - 'y': [-20000.0] - })) + assert_eq(result, cudf.DataFrame({"x": [-40000.0], "y": [-20000.0]})) + def test_half_distance(): - result = cuspatial.lonlat_to_xy_km_coordinates(-180, -90, - cudf.Series([0]), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( + -180, -90, cudf.Series([0]), cudf.Series([0]) + ) + assert_eq( + result, cudf.DataFrame({"x": [-14142.135623730952], "y": [-10000.0]}) ) - assert_eq(result, cudf.DataFrame({'x': [-14142.135623730952], - 'y': [-10000.0] - })) + def test_missing_coords(): with pytest.raises(RuntimeError): - result = cuspatial.lonlat_to_xy_km_coordinates(-180, -90, - cudf.Series(), - cudf.Series([0]) + result = cuspatial.lonlat_to_xy_km_coordinates( # noqa: F841 + -180, -90, cudf.Series(), cudf.Series([0]) ) + def test_zeros(): result = cuspatial.lonlat_to_xy_km_coordinates( - 0.0, - 0.0, - cudf.Series([0.0]), - cudf.Series([0.0]) + 0.0, 0.0, cudf.Series([0.0]), cudf.Series([0.0]) ) - assert_eq(result, cudf.DataFrame({'x': [0.0], 'y': [0.0]})) + assert_eq(result, cudf.DataFrame({"x": [0.0], "y": [0.0]})) + def test_values(): cam_lon = -90.66511046 cam_lat = 42.49197018 - py_lon=cudf.Series([-90.66518941, -90.66540743, -90.66489239]) - py_lat=cudf.Series([42.49207437, 42.49202408,42.49266787]) + py_lon = cudf.Series([-90.66518941, -90.66540743, -90.66489239]) + py_lat = cudf.Series([42.49207437, 42.49202408, 42.49266787]) - #note: x/y coordinates in killometers -km + # note: x/y coordinates in killometers -km result = cuspatial.lonlat_to_xy_km_coordinates( - cam_lon, - cam_lat, - py_lon, - py_lat + cam_lon, cam_lat, py_lon, py_lat + ) + assert_eq( + result, + cudf.DataFrame( + { + "x": [0.0064683857, 0.024330807, -0.0178664241], + "y": [-0.011576666, -0.00598888, -0.0775211111], + } + ), ) - assert_eq(result, cudf.DataFrame({ - 'x': [0.0064683857, 0.024330807, -0.0178664241], - 'y': [-0.011576666, -0.00598888, -0.0775211111] - })) diff --git a/python/cuspatial/cuspatial/tests/test_pip.py b/python/cuspatial/cuspatial/tests/test_pip.py index 3e1150bdf..e0227b1f1 100644 --- a/python/cuspatial/cuspatial/tests/test_pip.py +++ b/python/cuspatial/cuspatial/tests/test_pip.py @@ -1,77 +1,86 @@ # Copyright (c) 2019, NVIDIA CORPORATION. +import numpy as np import pytest + import cudf from cudf.tests.utils import assert_eq -import numpy as np + import cuspatial + def test_missing_0(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series(), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), - cudf.Series([0.0]) + cudf.Series([0.0]), ) + def test_missing_1(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series([0.0]), cudf.Series(), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), - cudf.Series([0.0]) + cudf.Series([0.0]), ) + def test_missing_2(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series(), cudf.Series([0.0]), cudf.Series([0.0]), - cudf.Series([0.0]) + cudf.Series([0.0]), ) + def test_missing_3(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series(), cudf.Series([0.0]), - cudf.Series([0.0]) + cudf.Series([0.0]), ) + def test_missing_4(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series(), - cudf.Series([0.0]) + cudf.Series([0.0]), ) + def test_missing_5(): with pytest.raises(RuntimeError): - result = cuspatial.point_in_polygon_bitmap( + result = cuspatial.point_in_polygon_bitmap( # noqa: F841 cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), - cudf.Series() + cudf.Series(), ) + def test_zeros(): result = cuspatial.point_in_polygon_bitmap( cudf.Series([0.0]), @@ -79,9 +88,10 @@ def test_zeros(): cudf.Series([0.0]), cudf.Series([0.0]), cudf.Series([0.0]), - cudf.Series([0.0]) + cudf.Series([0.0]), ) - assert_eq(cudf.Series(result), cudf.Series([0]).astype('int32')) + assert_eq(cudf.Series(result), cudf.Series([0]).astype("int32")) + def test_one_point_in(): result = cuspatial.point_in_polygon_bitmap( @@ -90,9 +100,10 @@ def test_one_point_in(): cudf.Series([1]), cudf.Series([3]), cudf.Series([-1, 0, 1]), - cudf.Series([-1, 1, -1]) + cudf.Series([-1, 1, -1]), ) - assert_eq(cudf.Series(result), cudf.Series([1]).astype('int32')) + assert_eq(cudf.Series(result), cudf.Series([1]).astype("int32")) + def test_one_point_out(): result = cuspatial.point_in_polygon_bitmap( @@ -101,9 +112,10 @@ def test_one_point_out(): cudf.Series([1]), cudf.Series([3]), cudf.Series([-1, 0, 1]), - cudf.Series([-1, 1, -1]) + cudf.Series([-1, 1, -1]), ) - assert_eq(cudf.Series(result), cudf.Series([0]).astype('int32')) + assert_eq(cudf.Series(result), cudf.Series([0]).astype("int32")) + def test_dataset(): result = cuspatial.point_in_polygon_bitmap( @@ -117,5 +129,4 @@ def test_dataset(): # The result of point_in_polygon_bitmap is a binary bitmap of # coordinates inside of the polgyon. print(np.binary_repr(result.data.to_array()[0], width=2)) - assert_eq(cudf.Series(result), cudf.Series([3, 1, 2]).astype('int32')) - + assert_eq(cudf.Series(result), cudf.Series([3, 1, 2]).astype("int32")) diff --git a/python/cuspatial/cuspatial/tests/test_traj.py b/python/cuspatial/cuspatial/tests/test_traj.py index e1d78bef5..7a73832a9 100644 --- a/python/cuspatial/cuspatial/tests/test_traj.py +++ b/python/cuspatial/cuspatial/tests/test_traj.py @@ -1,11 +1,13 @@ # Copyright (c) 2019, NVIDIA CORPORATION. -import pytest +import numpy as np + import cudf from cudf.tests.utils import assert_eq -import numpy as np + import cuspatial + def test_subset_id_zeros(): result = cuspatial.subset_trajectory_id( cudf.Series([0]), @@ -14,9 +16,18 @@ def test_subset_id_zeros(): cudf.Series([0]), cudf.Series([0]), ) - assert_eq(result, cudf.DataFrame({'x': [0.0], 'y': [0.0], - 'ids': cudf.Series([0]).astype('int32'), - 'timestamp': cudf.Series([0]).astype('datetime64[ms]')})) + assert_eq( + result, + cudf.DataFrame( + { + "x": [0.0], + "y": [0.0], + "ids": cudf.Series([0]).astype("int32"), + "timestamp": cudf.Series([0]).astype("datetime64[ms]"), + } + ), + ) + def test_subset_id_ones(): result = cuspatial.subset_trajectory_id( @@ -26,9 +37,18 @@ def test_subset_id_ones(): cudf.Series([1]), cudf.Series([1]), ) - assert_eq(result, cudf.DataFrame({'x': [1.0], 'y': [1.0], - 'ids': cudf.Series([1]).astype('int32'), - 'timestamp': cudf.Series([1]).astype('datetime64[ms]')})) + assert_eq( + result, + cudf.DataFrame( + { + "x": [1.0], + "y": [1.0], + "ids": cudf.Series([1]).astype("int32"), + "timestamp": cudf.Series([1]).astype("datetime64[ms]"), + } + ), + ) + def test_subset_id_random(): np.random.seed(0) @@ -39,39 +59,40 @@ def test_subset_id_random(): cudf.Series(np.random.randint(0, 10, 10)), cudf.Series(np.random.randint(0, 10, 10)), ) - assert_eq(result, cudf.DataFrame({ - 'x': [7.0, 6, 1, 6, 7, 7, 8], - 'y': [5.0, 9, 4, 3, 0, 3, 5], - 'ids': cudf.Series([2, 3, 3, 3, 3, 7, 0]).astype('int32'), - 'timestamp': cudf.Series( - [9, 9, 7, 3, 2, 7, 2] - ).astype('datetime64[ms]')})) + assert_eq( + result, + cudf.DataFrame( + { + "x": [7.0, 6, 1, 6, 7, 7, 8], + "y": [5.0, 9, 4, 3, 0, 3, 5], + "ids": cudf.Series([2, 3, 3, 3, 3, 7, 0]).astype("int32"), + "timestamp": cudf.Series([9, 9, 7, 3, 2, 7, 2]).astype( + "datetime64[ms]" + ), + } + ), + ) + def test_spatial_bounds_zeros(): result = cuspatial.spatial_bounds( - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), + cudf.Series([0]), cudf.Series([0]), cudf.Series([0]), cudf.Series([0]) + ) + assert_eq( + result, + cudf.DataFrame({"x1": [0.0], "y1": [0.0], "x2": [0.0], "y2": [0.0]}), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0], - 'y1': [0.0], - 'x2': [0.0], - 'y2': [0.0] - })) + def test_spatial_bounds_ones(): result = cuspatial.spatial_bounds( - cudf.Series([1]), - cudf.Series([1]), - cudf.Series([1]), - cudf.Series([1]), + cudf.Series([1]), cudf.Series([1]), cudf.Series([1]), cudf.Series([1]) ) - assert_eq(result, cudf.DataFrame({'x1': [1.0], - 'y1': [1.0], - 'x2': [1.0], - 'y2': [1.0] - })) + assert_eq( + result, + cudf.DataFrame({"x1": [1.0], "y1": [1.0], "x2": [1.0], "y2": [1.0]}), + ) + def test_spatial_bounds_zero_to_one(): result = cuspatial.spatial_bounds( @@ -80,11 +101,11 @@ def test_spatial_bounds_zero_to_one(): cudf.Series([2]), cudf.Series([2]), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0], - 'y1': [0.0], - 'x2': [0.0], - 'y2': [1.0] - })) + assert_eq( + result, + cudf.DataFrame({"x1": [0.0], "y1": [0.0], "x2": [0.0], "y2": [1.0]}), + ) + def test_spatial_bounds_zero_to_one_xy(): result = cuspatial.spatial_bounds( @@ -93,11 +114,11 @@ def test_spatial_bounds_zero_to_one_xy(): cudf.Series([2]), cudf.Series([2]), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0], - 'y1': [0.0], - 'x2': [1.0], - 'y2': [1.0] - })) + assert_eq( + result, + cudf.DataFrame({"x1": [0.0], "y1": [0.0], "x2": [1.0], "y2": [1.0]}), + ) + def test_spatial_bounds_subsetted(): result = cuspatial.spatial_bounds( @@ -106,11 +127,18 @@ def test_spatial_bounds_subsetted(): cudf.Series([2, 2]), cudf.Series([2, 4]), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0, -1.0], - 'y1': [0.0, -1.0], - 'x2': [1.0, 2.0], - 'y2': [1.0, 2.0] - })) + assert_eq( + result, + cudf.DataFrame( + { + "x1": [0.0, -1.0], + "y1": [0.0, -1.0], + "x2": [1.0, 2.0], + "y2": [1.0, 2.0], + } + ), + ) + def test_spatial_bounds_intersected(): result = cuspatial.spatial_bounds( @@ -119,11 +147,18 @@ def test_spatial_bounds_intersected(): cudf.Series([2, 2]), cudf.Series([2, 4]), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0, 1.0], - 'y1': [0.0, 1.0], - 'x2': [2.0, 3.0], - 'y2': [2.0, 3.0] - })) + assert_eq( + result, + cudf.DataFrame( + { + "x1": [0.0, 1.0], + "y1": [0.0, 1.0], + "x2": [2.0, 3.0], + "y2": [2.0, 3.0], + } + ), + ) + def test_spatial_bounds_two_and_three(): result = cuspatial.spatial_bounds( @@ -132,40 +167,52 @@ def test_spatial_bounds_two_and_three(): cudf.Series([2, 3]), cudf.Series([2, 5]), ) - assert_eq(result, cudf.DataFrame({'x1': [0.0, 1.0], - 'y1': [0.0, 1.0], - 'x2': [2.0, 3.0], - 'y2': [2.0, 3.0] - })) + assert_eq( + result, + cudf.DataFrame( + { + "x1": [0.0, 1.0], + "y1": [0.0, 1.0], + "x2": [2.0, 3.0], + "y2": [2.0, 3.0], + } + ), + ) def test_derive_trajectories_zeros(): num_trajectories = cuspatial.derive( - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), - cudf.Series([0]), + cudf.Series([0]), cudf.Series([0]), cudf.Series([0]), cudf.Series([0]) ) assert num_trajectories[0] == 1 - assert_eq(num_trajectories[1], cudf.DataFrame({ - 'trajectory_id': cudf.Series([0]).astype('int32'), - 'length': cudf.Series([1]).astype('int32'), - 'position': cudf.Series([1]).astype('int32'), - })) + assert_eq( + num_trajectories[1], + cudf.DataFrame( + { + "trajectory_id": cudf.Series([0]).astype("int32"), + "length": cudf.Series([1]).astype("int32"), + "position": cudf.Series([1]).astype("int32"), + } + ), + ) + def test_derive_trajectories_ones(): num_trajectories = cuspatial.derive( - cudf.Series([1]), - cudf.Series([1]), - cudf.Series([1]), - cudf.Series([1]), + cudf.Series([1]), cudf.Series([1]), cudf.Series([1]), cudf.Series([1]) ) assert num_trajectories[0] == 1 - assert_eq(num_trajectories[1], cudf.DataFrame({ - 'trajectory_id': cudf.Series([1]).astype('int32'), - 'length': cudf.Series([1]).astype('int32'), - 'position': cudf.Series([1]).astype('int32'), - })) + assert_eq( + num_trajectories[1], + cudf.DataFrame( + { + "trajectory_id": cudf.Series([1]).astype("int32"), + "length": cudf.Series([1]).astype("int32"), + "position": cudf.Series([1]).astype("int32"), + } + ), + ) + def test_derive_trajectories_two(): num_trajectories = cuspatial.derive( @@ -175,11 +222,17 @@ def test_derive_trajectories_two(): cudf.Series([0, 1]), ) assert num_trajectories[0] == 2 - assert_eq(num_trajectories[1], cudf.DataFrame({ - 'trajectory_id': cudf.Series([0, 1]).astype('int32'), - 'length': cudf.Series([1, 1]).astype('int32'), - 'position': cudf.Series([1, 2]).astype('int32') - })) + assert_eq( + num_trajectories[1], + cudf.DataFrame( + { + "trajectory_id": cudf.Series([0, 1]).astype("int32"), + "length": cudf.Series([1, 1]).astype("int32"), + "position": cudf.Series([1, 2]).astype("int32"), + } + ), + ) + def test_derive_trajectories_many(): np.random.seed(0) @@ -190,11 +243,19 @@ def test_derive_trajectories_many(): cudf.Series(np.random.randint(0, 10, 10)), ) assert num_trajectories[0] == 6 - assert_eq(num_trajectories[1], cudf.DataFrame({ - 'trajectory_id': cudf.Series([0, 3, 4, 5, 8, 9]).astype('int32'), - 'length': cudf.Series([2, 2, 1, 2, 1, 2]).astype('int32'), - 'position': cudf.Series([2, 4, 5, 7, 8, 10]).astype('int32'), - })) + assert_eq( + num_trajectories[1], + cudf.DataFrame( + { + "trajectory_id": cudf.Series([0, 3, 4, 5, 8, 9]).astype( + "int32" + ), + "length": cudf.Series([2, 2, 1, 2, 1, 2]).astype("int32"), + "position": cudf.Series([2, 4, 5, 7, 8, 10]).astype("int32"), + } + ), + ) + def test_distance_and_speed_zeros(): result = cuspatial.distance_and_speed( @@ -204,8 +265,9 @@ def test_distance_and_speed_zeros(): cudf.Series([0]), cudf.Series([0]), ) - assert_eq(result['meters'], cudf.Series([-2.0]), check_names=False) - assert_eq(result['speed'], cudf.Series([-2.0]), check_names=False) + assert_eq(result["meters"], cudf.Series([-2.0]), check_names=False) + assert_eq(result["speed"], cudf.Series([-2.0]), check_names=False) + def test_distance_and_speed_ones(): result = cuspatial.distance_and_speed( @@ -215,8 +277,9 @@ def test_distance_and_speed_ones(): cudf.Series([1]), cudf.Series([1]), ) - assert_eq(result['meters'], cudf.Series([-2.0]), check_names=False) - assert_eq(result['speed'], cudf.Series([-2.0]), check_names=False) + assert_eq(result["meters"], cudf.Series([-2.0]), check_names=False) + assert_eq(result["speed"], cudf.Series([-2.0]), check_names=False) + def test_one_one_meter_one_second(): result = cuspatial.distance_and_speed( @@ -226,8 +289,9 @@ def test_one_one_meter_one_second(): cudf.Series([2]), cudf.Series([2]), ) - assert_eq(result['meters'], cudf.Series([1.0]), check_names=False) - assert_eq(result['speed'], cudf.Series([1.0]), check_names=False) + assert_eq(result["meters"], cudf.Series([1.0]), check_names=False) + assert_eq(result["speed"], cudf.Series([1.0]), check_names=False) + def test_two_trajectories_one_meter_one_second(): result = cuspatial.distance_and_speed( @@ -237,20 +301,29 @@ def test_two_trajectories_one_meter_one_second(): cudf.Series([2, 2]), cudf.Series([2, 4]), ) - assert_eq(result['meters'], cudf.Series([1.0, 1.0]), check_names=False) - assert_eq(result['speed'], cudf.Series([1.0, 1.0]), check_names=False) + assert_eq(result["meters"], cudf.Series([1.0, 1.0]), check_names=False) + assert_eq(result["speed"], cudf.Series([1.0, 1.0]), check_names=False) + def test_distance_and_speed_single_trajectory(): result = cuspatial.distance_and_speed( - cudf.Series([ - 1.0, 2.0, 3.0, 5.0, 7.0, 1.0, 2.0, 3.0, 6.0, 0.0, 3.0, 6.0]), - cudf.Series([ - 0.0, 1.0, 2.0, 3.0, 1.0, 3.0, 5.0, 6.0, 5.0, 4.0, 7.0, 4.0]), + cudf.Series( + [1.0, 2.0, 3.0, 5.0, 7.0, 1.0, 2.0, 3.0, 6.0, 0.0, 3.0, 6.0] + ), + cudf.Series( + [0.0, 1.0, 2.0, 3.0, 1.0, 3.0, 5.0, 6.0, 5.0, 4.0, 7.0, 4.0] + ), cudf.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), cudf.Series([5, 4, 3]), cudf.Series([5, 9, 12]), ) - assert_eq(result['meters'], cudf.Series( - [7892.922363, 6812.55908203125, 8485.28125]), check_names=False) - assert_eq(result['speed'], cudf.Series( - [1973230.625, 2270853., 4242640.5]), check_names=False) # fast! + assert_eq( + result["meters"], + 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! diff --git a/python/cuspatial/cuspatial/tests/test_window_points.py b/python/cuspatial/cuspatial/tests/test_window_points.py index 701055d2b..be4e9eabe 100644 --- a/python/cuspatial/cuspatial/tests/test_window_points.py +++ b/python/cuspatial/cuspatial/tests/test_window_points.py @@ -1,61 +1,67 @@ # Copyright (c) 2019, NVIDIA CORPORATION. import pytest + import cudf from cudf.tests.utils import assert_eq -import numpy as np + import cuspatial def test_zeros(): with pytest.raises(RuntimeError): - result = cuspatial.window_points( + result = cuspatial.window_points( # noqa: F841 0, 0, 0, 0, cudf.Series([0.0]), cudf.Series([0.0]) ) + def test_ones(): with pytest.raises(RuntimeError): - result = cuspatial.window_points( + result = cuspatial.window_points( # noqa: F841 0, 0, 0, 0, cudf.Series([0.0]), cudf.Series([0.0]) ) + def test_centered(): result = cuspatial.window_points( -1, -1, 1, 1, cudf.Series([0.0]), cudf.Series([0.0]) ) - assert_eq(result, cudf.DataFrame({'x': [0.0], 'y': [0.0]})) - -@pytest.mark.parametrize('coords', [ - (-1.0, -1.0), - (-1.0, 1.0), - (1.0, -1.0), - (1.0, 1.0) -]) + assert_eq(result, cudf.DataFrame({"x": [0.0], "y": [0.0]})) + + +@pytest.mark.parametrize( + "coords", [(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)] +) def test_corners(coords): x, y = coords result = cuspatial.window_points( -1.1, -1.1, 1.1, 1.1, cudf.Series([x]), cudf.Series([y]) ) - assert_eq(result, cudf.DataFrame({'x': [x], 'y': [y]})) + assert_eq(result, cudf.DataFrame({"x": [x], "y": [y]})) + def test_pair(): result = cuspatial.window_points( -1.1, -1.1, 1.1, 1.1, cudf.Series([0, 1]), cudf.Series([1, 0]) ) - assert_eq(result, cudf.DataFrame({'x': [0.0, 1], 'y': [1, 0.0]})) + assert_eq(result, cudf.DataFrame({"x": [0.0, 1], "y": [1, 0.0]})) + def test_oob(): result = cuspatial.window_points( -1, -1, 1, 1, cudf.Series([-2, 2]), cudf.Series([2, -2]) ) - assert_eq(result, cudf.DataFrame({'x': [], 'y': []})) + assert_eq(result, cudf.DataFrame({"x": [], "y": []})) + def test_half(): result = cuspatial.window_points( - -2, -2, 2, 2, + -2, + -2, + 2, + 2, cudf.Series([-1.0, 1.0, 3.0, -3.0]), - cudf.Series([1.0, -1.0, 3.0, -3.0]) + cudf.Series([1.0, -1.0, 3.0, -3.0]), ) print(result) - assert_eq(result, cudf.DataFrame({'x': [-1.0, 1.0], 'y': [1.0, -1.0]})) - + assert_eq(result, cudf.DataFrame({"x": [-1.0, 1.0], "y": [1.0, -1.0]})) diff --git a/python/cuspatial/cuspatial/utils/traj_utils.py b/python/cuspatial/cuspatial/utils/traj_utils.py index 688122f97..39d1e1633 100644 --- a/python/cuspatial/cuspatial/utils/traj_utils.py +++ b/python/cuspatial/cuspatial/utils/traj_utils.py @@ -1,23 +1,22 @@ def get_ts_struct(ts): - y=ts&0x3f - ts=ts>>6 - m=ts&0xf - ts=ts>>4 - d=ts&0x1f - ts=ts>>5 - hh=ts&0x1f - ts=ts>>5 - mm=ts&0x3f - ts=ts>>6 - ss=ts&0x3f - ts=ts>>6 - wd=ts&0x8 - ts=ts>>3 - yd=ts&0x1ff - ts=ts>>9 - ms=ts&0x3ff - ts=ts>>10 - pid=ts&0x3ff - - return y,m,d,hh,mm,ss,wd,yd,ms,pid + y = ts & 0x3F + ts = ts >> 6 + m = ts & 0xF + ts = ts >> 4 + d = ts & 0x1F + ts = ts >> 5 + hh = ts & 0x1F + ts = ts >> 5 + mm = ts & 0x3F + ts = ts >> 6 + ss = ts & 0x3F + ts = ts >> 6 + wd = ts & 0x8 + ts = ts >> 3 + yd = ts & 0x1FF + ts = ts >> 9 + ms = ts & 0x3FF + ts = ts >> 10 + pid = ts & 0x3FF + return y, m, d, hh, mm, ss, wd, yd, ms, pid diff --git a/python/cuspatial/demos/hausdorff_clustering_test_toy.py b/python/cuspatial/demos/hausdorff_clustering_test_toy.py index 7f49e28dc..77503e7c7 100644 --- a/python/cuspatial/demos/hausdorff_clustering_test_toy.py +++ b/python/cuspatial/demos/hausdorff_clustering_test_toy.py @@ -1,46 +1,49 @@ """ -A toy example to demonstrate how to convert python arrays into cuSpatial inputs, -invoke the GPU accelerated directed Hausdorff distance computing function in cuSpatial, -convert the results back to python array(s) again to be feed into scipy clustering APIs -For the toy example, by desgin, both AgglomerativeClustering and DBSCAN cluster the 2nd and thrid -trajectories into one cluster while leaving the first trajectory as the sconed cluster. - -To run the demo, first install scipy and scikit-learn -by "conda install -c conda-forge scipy scikit-learn" under cudf_dev environment +A toy example to demonstrate how to convert python arrays into cuSpatial +inputs, invoke the GPU accelerated directed Hausdorff distance computing +function in cuSpatial, convert the results back to python array(s) again to be +feed into scipy clustering APIs. For the toy example, by design, both +AgglomerativeClustering and DBSCAN cluster the 2nd and thied trajectories into +one cluster while leaving the first trajectory as the sconed cluster. + +To run the demo, first install scipy and scikit-learn by `conda install -c +conda-forge scipy scikit-learn` under cudf_dev environment """ import numpy as np -import time +from sklearn.cluster import DBSCAN, AgglomerativeClustering + from cudf.core import column + import cuspatial._lib.spatial as gis -from scipy.spatial.distance import directed_hausdorff -from sklearn.cluster import AgglomerativeClustering,DBSCAN - -in_trajs=[] -in_trajs.append(np.array([[1,0],[2,1],[3,2],[5,3],[7,1]])) -in_trajs.append(np.array([[0,3],[2,5],[3,6],[6,5]])) -in_trajs.append(np.array([[1,4],[3,7],[6,4]])) -out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs],0) -py_x=np.array(out_trajs[:,0]) -py_y=np.array(out_trajs[:,1]) + +in_trajs = [] +in_trajs.append(np.array([[1, 0], [2, 1], [3, 2], [5, 3], [7, 1]])) +in_trajs.append(np.array([[0, 3], [2, 5], [3, 6], [6, 5]])) +in_trajs.append(np.array([[1, 4], [3, 7], [6, 4]])) +out_trajs = np.concatenate([np.asarray(traj) for traj in in_trajs], 0) +py_x = np.array(out_trajs[:, 0]) +py_y = np.array(out_trajs[:, 1]) 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) - -num_set=len(cnt) -matrix=distance.data.to_array().reshape(num_set,num_set) - -#clustering using AgglomerativeClustering -agg1= AgglomerativeClustering(n_clusters=2, affinity='precomputed', linkage = 'average') -label1=agg1.fit(matrix) + 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) + +num_set = len(cnt) +matrix = distance.data.to_array().reshape(num_set, num_set) + +# clustering using AgglomerativeClustering +agg1 = AgglomerativeClustering( + n_clusters=2, affinity="precomputed", linkage="average" +) +label1 = agg1.fit(matrix) print("AgglomerativeClustering results={}".format(label1.labels_)) -#clustering using DBSCAN; as the minimum distanance is ~1.4, -#using eps=1.5 will generate the same two clasters as AgglomerativeClustering -agg2=DBSCAN(eps=1.5,min_samples=1,metric='precomputed') -label2=agg2.fit(matrix) +# clustering using DBSCAN; as the minimum distanance is ~1.4, +# using eps=1.5 will generate the same two clasters as AgglomerativeClustering +agg2 = DBSCAN(eps=1.5, min_samples=1, metric="precomputed") +label2 = agg2.fit(matrix) print("DBSCAN clustering results={}".format(label2.labels_)) diff --git a/python/cuspatial/demos/hausdorff_distance_verify_locust256.py b/python/cuspatial/demos/hausdorff_distance_verify_locust256.py index f05f03286..bb1ea0312 100644 --- a/python/cuspatial/demos/hausdorff_distance_verify_locust256.py +++ b/python/cuspatial/demos/hausdorff_distance_verify_locust256.py @@ -1,20 +1,20 @@ """ -Demo code to verify the correctness of GPU-accelerated computing directed Hausdorff distance -on Locust trajectory dataset for 592 trajectories whose numbers of vertcies are between [256,1024) -By comparing with scipy results. +Demo code to verify the correctness of GPU-accelerated computing directed +Hausdorff distance on Locust trajectory dataset for 592 trajectories whose +numbers of vertcies are between [256,1024) by comparing with scipy results. -To run the demo, first install scipy by "conda install -c conda-forge scipy" -under cudef_dev environment +To run the demo, first install scipy by `conda install -c conda-forge scipy` +under cudf_dev environment """ -import numpy as np -import time import sys -import pickle +import time +import numpy as np from scipy.spatial.distance import directed_hausdorff -import cuspatial._lib.spatial as gis + import cuspatial._lib.soa_readers as readers +import cuspatial._lib.spatial as gis data_dir = "/home/jianting/trajcode/" data_set = "locust256" @@ -42,13 +42,16 @@ start = time.time() dist = gis.cpp_directed_hausdorff_distance(pnt_x, pnt_y, cnt) -print("dis.size={} num_traj*num_traj={}".format(dist.data.size, num_traj * num_traj)) +print( + "dis.size={} num_traj*num_traj={}".format( + dist.data.size, num_traj * num_traj + ) +) end = time.time() print(end - start) print( - "python Directed Hausdorff distance GPU end-to-end time in ms (end-to-end)={}".format( - (end - start) * 1000 - ) + "python Directed Hausdorff distance GPU end-to-end time in ms " + "(end-to-end)={}".format((end - start) * 1000) ) start = time.time() @@ -103,9 +106,8 @@ print("mis_match={}".format(mis_match)) end = time.time() print( - "python Directed Hausdorff distance cpu end-to-end time in ms (end-to-end)={}".format( - (end - start) * 1000 - ) + "python Directed Hausdorff distance cpu end-to-end time in ms " + "(end-to-end)={}".format((end - start) * 1000) ) # for val in d[0]: diff --git a/python/cuspatial/demos/haversine_distance_test_nyctaxi.py b/python/cuspatial/demos/haversine_distance_test_nyctaxi.py index 5f5a7f236..a86e0b3cd 100644 --- a/python/cuspatial/demos/haversine_distance_test_nyctaxi.py +++ b/python/cuspatial/demos/haversine_distance_test_nyctaxi.py @@ -1,11 +1,16 @@ import time + import cudf from cudf.core import column + import cuspatial._lib.spatial as gis 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") +# 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" +) end = time.time() print("data ingesting time (from SSD) in ms={}".format((end - start) * 1000)) df.head().to_pandas().columns @@ -16,7 +21,11 @@ x2 = column.as_column(df["End_Lon"]) y2 = column.as_column(df["End_Lat"]) end = time.time() -print("data frame to gdf column conversion time in ms={}".format((end - start) * 1000)) +print( + "data frame to gdf column conversion time in ms={}".format( + (end - start) * 1000 + ) +) start = time.time() h_dist = gis.cpp_haversine_distance(x1, y1, x2, y1) diff --git a/python/cuspatial/demos/pip_test_gdal_locust.py b/python/cuspatial/demos/pip_test_gdal_locust.py index 4b08a6e0d..be740c27a 100644 --- a/python/cuspatial/demos/pip_test_gdal_locust.py +++ b/python/cuspatial/demos/pip_test_gdal_locust.py @@ -1,7 +1,7 @@ """ -pip demo directly using gdal/ogr for python; not for performance comparisons. -To run the demo, first install python-gdal by "conda install -c conda-forge gdal" -under cudef_dev environment +pip demo directly using gdal/ogr for python; not for performance comparisons. +To run the demo, first install python-gdal by `conda install -c conda-forge +gdal` under cudf_dev environment """ import numpy as np @@ -15,10 +15,12 @@ pt = ogr.Geometry(ogr.wkbPoint) pt.AssignSpatialReference(spatialReference) pnt_x = np.array( - [-90.666418409895840, -90.665136925928721, -90.671840534675397], dtype=np.float64 + [-90.666418409895840, -90.665136925928721, -90.671840534675397], + dtype=np.float64, ) pnt_y = np.array( - [42.492199401857071, 42.492104092138952, 42.490649501411141], dtype=np.float64 + [42.492199401857071, 42.492104092138952, 42.490649501411141], + dtype=np.float64, ) for i in range(3): diff --git a/python/cuspatial/demos/pip_test_shapely_locust.py b/python/cuspatial/demos/pip_test_shapely_locust.py index 5f36f586f..d46efa5e2 100644 --- a/python/cuspatial/demos/pip_test_shapely_locust.py +++ b/python/cuspatial/demos/pip_test_shapely_locust.py @@ -1,15 +1,14 @@ """ -PIP demo directly using shapely, more efficient than using python gdal/ogr directly -poygons are created only once and stored for reuse +PIP demo directly using shapely, more efficient than using python gdal/ogr +directly polygons are created only once and stored for reuse -To run the demo, first install python gdal and pyshp by "conda install -c conda-forge gdal pyshp" -under cudef_dev environment +To run the demo, first install python gdal and pyshp by `conda install -c +conda-forge gdal pyshp` under cudf_dev environment """ import numpy as np -from shapely.geometry import Polygon, Point -from shapely.geometry import shape import shapefile +from shapely.geometry import Point, Polygon data_dir = "/home/jianting/cuspatial/data/" @@ -20,10 +19,12 @@ plys.append(Polygon(shape.points)) pnt_x = np.array( - [-90.666418409895840, -90.665136925928721, -90.671840534675397], dtype=np.float64 + [-90.666418409895840, -90.665136925928721, -90.671840534675397], + dtype=np.float64, ) pnt_y = np.array( - [42.492199401857071, 42.492104092138952, 42.490649501411141], dtype=np.float64 + [42.492199401857071, 42.492104092138952, 42.490649501411141], + dtype=np.float64, ) for i in range(3): diff --git a/python/cuspatial/demos/pip_verify_shapely_locust.py b/python/cuspatial/demos/pip_verify_shapely_locust.py index 62745f717..e0ec0ef1d 100644 --- a/python/cuspatial/demos/pip_verify_shapely_locust.py +++ b/python/cuspatial/demos/pip_verify_shapely_locust.py @@ -1,17 +1,17 @@ """ -verify the correctness of GPU-based implementation by comparing with shapely python package -GPU C++ kernel time 0.966ms, GPU C++ libcuspatial end-to-end time 1.104ms, GPU python cuspaital end-to-end time 1.270ms -shapely python end-to-end time 127659.4, 100,519X speedup (127659.4/1.27) +verify the correctness of GPU-based implementation by comparing with shapely +python package GPU C++ kernel time 0.966ms, GPU C++ libcuspatial end-to-end +time 1.104ms, GPU python cuspaital end-to-end time 1.270ms shapely python +end-to-end time 127659.4, 100,519X speedup (127659.4/1.27) """ -import numpy as np import time -import cuspatial._lib.spatial as gis -import cuspatial._lib.soa_readers as readers -from cudf.core import column -from shapely.geometry import Polygon, Point -from shapely.geometry import shape + import shapefile +from shapely.geometry import Point, Polygon + +import cuspatial._lib.soa_readers as readers +import cuspatial._lib.spatial as gis data_dir = "/home/jianting/cuspatial/data/" plyreader = shapefile.Reader(data_dir + "its_4326_roi.shp") @@ -20,7 +20,9 @@ for shape in polygon: plys.append(Polygon(shape.points)) -pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa(data_dir + "locust.location") +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") start = time.time() @@ -46,6 +48,10 @@ end = time.time() print(end - start) -print("python(shapely) CPU Time in ms (end-to-end)={}".format((end - start) * 1000)) +print( + "python(shapely) CPU Time in ms (end-to-end)={}".format( + (end - start) * 1000 + ) +) print("CPU and GPU results mismatch={}".format(mis_match)) diff --git a/python/cuspatial/demos/stq_test_soa_locust.py b/python/cuspatial/demos/stq_test_soa_locust.py index 52fcea6b8..49106122d 100644 --- a/python/cuspatial/demos/stq_test_soa_locust.py +++ b/python/cuspatial/demos/stq_test_soa_locust.py @@ -1,21 +1,25 @@ """ GPU-based spatial window query demo using 1.3 million points read from file -and (x1,x2,y1,y2)=[-180,180,-90,90] as the query window -num should be the same as x.data.size, both are 1338671 +and (x1,x2,y1,y2)=[-180,180,-90,90] as the query window num should be the same +as x.data.size, both are 1338671 """ import numpy as np -import pandas as pd -import cuspatial._lib.trajectory as traj -import cuspatial._lib.stq as stq + import cuspatial._lib.soa_readers as readers -from cudf.core import column -import cudf +import cuspatial._lib.stq as stq data_dir = "/home/jianting/cuspatial/data/" -pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa(data_dir + "locust.location") +pnt_lon, pnt_lat = readers.cpp_read_pnt_lonlat_soa( + data_dir + "locust.location" +) num, nlon, nlat = stq.cpp_sw_xy( - np.double(-180), np.double(180), np.double(-90), np.double(90), pnt_lon, pnt_lat + np.double(-180), + np.double(180), + np.double(-90), + np.double(90), + pnt_lon, + pnt_lat, ) print(num) print(pnt_lon.data.size) diff --git a/python/cuspatial/demos/traj_test_soa_locust.py b/python/cuspatial/demos/traj_test_soa_locust.py index c4a4bbb66..bf17d02e8 100644 --- a/python/cuspatial/demos/traj_test_soa_locust.py +++ b/python/cuspatial/demos/traj_test_soa_locust.py @@ -1,13 +1,16 @@ """ -GPU-based coordinate transformation demo: (log/lat)==>(x/y), relative to a camera origin +GPU-based coordinate transformation demo: (log/lat)==>(x/y), relative to a +camera origin + Note: camera configuration is read from a CSV file using Panda """ 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._lib.soa_readers as readers import cuspatial.utils.traj_utils as tools data_dir = "./" @@ -16,7 +19,9 @@ 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") +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") @@ -33,8 +38,12 @@ num_traj, trajectories = traj.cpp_derive_trajectories(pnt_x, pnt_y, id, 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']) +dist, speed = traj.cpp_trajectory_distance_and_speed( + pnt_x, pnt_y, ts, trajectories["length"], trajectories["position"] +) print(dist.data.to_array()[0], speed.data.to_array()[0]) -boxes = traj.cpp_trajectory_spatial_bounds(pnt_x, pnt_y, trajectories['length'], trajectories['position']) +boxes = traj.cpp_trajectory_spatial_bounds( + pnt_x, pnt_y, trajectories["length"], trajectories["position"] +) print(boxes.head()) diff --git a/python/cuspatial/setup.py b/python/cuspatial/setup.py index 04458210c..6222c1e73 100644 --- a/python/cuspatial/setup.py +++ b/python/cuspatial/setup.py @@ -32,7 +32,10 @@ setup( name="cuspatial", version=versioneer.get_version(), - description="cuSpatial: GPU-Accelerated Spatial and Trajectory Data Management and Analytics Library", + description=( + "cuSpatial: GPU-Accelerated Spatial and Trajectory Data Management and" + " Analytics Library" + ), url="https://github.com/rapidsai/cuspatial", author="NVIDIA Corporation", license="Apache 2.0",