Skip to content

Commit

Permalink
Replace removed cudf._lib.utils method with pylibcudf APIs (#1505)
Browse files Browse the repository at this point in the history
closes #1504

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #1505
  • Loading branch information
mroeschke authored Dec 18, 2024
1 parent ba4385e commit ad2900d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 55 deletions.
16 changes: 10 additions & 6 deletions python/cuspatial/cuspatial/_lib/distance.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from libcpp.memory cimport make_shared, shared_ptr, unique_ptr
from libcpp.utility cimport move, pair

from cudf._lib.column cimport Column
from cudf._lib.utils cimport columns_from_table_view
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table_view cimport table_view
Expand Down Expand Up @@ -60,12 +60,16 @@ def directed_hausdorff_distance(
)
)

owner = Column.from_unique_ptr(move(result.first), data_ptr_exposed=True)

return columns_from_table_view(
result.second,
owners=[owner] * result.second.num_columns()
owner_col = Column.from_unique_ptr(
move(result.first), data_ptr_exposed=True
)
cdef plc_Table plc_owner_table = plc_Table(
[owner_col.to_pylibcudf(mode="read")] * result.second.num_columns()
)
cdef plc_Table plc_result_table = plc_Table.from_table_view(
result.second, plc_owner_table
)
return [Column.from_pylibcudf(col) for col in plc_result_table.columns()]


def pairwise_point_distance(
Expand Down
8 changes: 6 additions & 2 deletions python/cuspatial/cuspatial/_lib/linestring_bounding_boxes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport columns_from_unique_ptr
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table

Expand All @@ -24,4 +24,8 @@ cpdef linestring_bounding_boxes(Column poly_offsets,
result = move(cpp_linestring_bounding_boxes(
c_poly_offsets, c_x, c_y, R
))
return columns_from_unique_ptr(move(result))
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return [
Column.from_pylibcudf(col)
for col in plc_table.columns()
]
11 changes: 9 additions & 2 deletions python/cuspatial/cuspatial/_lib/points_in_range.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport data_from_unique_ptr
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table

Expand Down Expand Up @@ -38,4 +38,11 @@ cpdef points_in_range(
)
)

return data_from_unique_ptr(move(c_result), column_names=["x", "y"])
cdef plc_Table plc_table = plc_Table.from_libcudf(move(c_result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(["x", "y"], plc_table.columns())
},
None
)
8 changes: 6 additions & 2 deletions python/cuspatial/cuspatial/_lib/polygon_bounding_boxes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport columns_from_unique_ptr
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table

Expand All @@ -25,4 +25,8 @@ cpdef polygon_bounding_boxes(Column poly_offsets,
result = move(cpp_polygon_bounding_boxes(
c_poly_offsets, c_ring_offsets, c_x, c_y
))
return columns_from_unique_ptr(move(result))
cdef plc_Table table_result = plc_Table.from_libcudf(move(result))
return [
Column.from_pylibcudf(col)
for col in table_result.columns()
]
25 changes: 15 additions & 10 deletions python/cuspatial/cuspatial/_lib/quadtree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from libcpp.pair cimport pair
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport data_from_unique_ptr
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table
Expand All @@ -30,16 +30,21 @@ cpdef quadtree_on_points(Column x, Column y,
result = move(cpp_quadtree_on_points(
c_x, c_y, x_min, x_max, y_min, y_max, scale, max_depth, min_size
))
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result.second))
result_names = [
"key",
"level",
"is_internal_node",
"length",
"offset"
]
return (
Column.from_unique_ptr(move(result.first)),
data_from_unique_ptr(
move(result.second),
column_names=[
"key",
"level",
"is_internal_node",
"length",
"offset"
]
(
{
name: Column.from_pylibcudf(col)
for name, col in zip(result_names, plc_table.columns())
},
None
)
)
78 changes: 56 additions & 22 deletions python/cuspatial/cuspatial/_lib/spatial_join.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport data_from_unique_ptr, table_view_from_table
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table, table_view

Expand All @@ -24,20 +24,30 @@ cpdef join_quadtree_and_bounding_boxes(object quadtree,
double y_max,
double scale,
int8_t max_depth):
cdef table_view c_quadtree = table_view_from_table(
quadtree, ignore_index=True)
cdef table_view c_bounding_boxes = table_view_from_table(
bounding_boxes, ignore_index=True)
cdef plc_Table plc_quadtree = plc_Table(
[col.to_pylibcudf(mode="read") for col in quadtree._columns]
)
cdef table_view c_quadtree = plc_quadtree.view()
cdef plc_Table plc_bounding_boxes = plc_Table(
[col.to_pylibcudf(mode="read") for col in bounding_boxes._columns]
)
cdef table_view c_bounding_boxes = plc_bounding_boxes.view()
cdef unique_ptr[table] result
with nogil:
result = move(cpp_join_quadtree_and_bounding_boxes(
c_quadtree,
c_bounding_boxes,
x_min, x_max, y_min, y_max, scale, max_depth
))
return data_from_unique_ptr(
move(result),
column_names=["bbox_offset", "quad_offset"]
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(
["bbox_offset", "quad_offset"], plc_table.columns()
)
},
None
)


Expand All @@ -50,10 +60,14 @@ cpdef quadtree_point_in_polygon(object poly_quad_pairs,
Column ring_offsets,
Column poly_points_x,
Column poly_points_y):
cdef table_view c_poly_quad_pairs = table_view_from_table(
poly_quad_pairs, ignore_index=True)
cdef table_view c_quadtree = table_view_from_table(
quadtree, ignore_index=True)
cdef plc_Table plc_poly_quad_pairs = plc_Table(
[col.to_pylibcudf(mode="read") for col in poly_quad_pairs._columns]
)
cdef table_view c_poly_quad_pairs = plc_poly_quad_pairs.view()
cdef plc_Table plc_quadtree = plc_Table(
[col.to_pylibcudf(mode="read") for col in quadtree._columns]
)
cdef table_view c_quadtree = plc_quadtree.view()
cdef column_view c_point_indices = point_indices.view()
cdef column_view c_points_x = points_x.view()
cdef column_view c_points_y = points_y.view()
Expand All @@ -74,9 +88,15 @@ cpdef quadtree_point_in_polygon(object poly_quad_pairs,
c_poly_points_x,
c_poly_points_y
))
return data_from_unique_ptr(
move(result),
column_names=["polygon_index", "point_index"]
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(
["polygon_index", "point_index"], plc_table.columns()
)
},
None
)


Expand All @@ -88,10 +108,17 @@ cpdef quadtree_point_to_nearest_linestring(object linestring_quad_pairs,
Column linestring_offsets,
Column linestring_points_x,
Column linestring_points_y):
cdef table_view c_linestring_quad_pairs = table_view_from_table(
linestring_quad_pairs, ignore_index=True)
cdef table_view c_quadtree = table_view_from_table(
quadtree, ignore_index=True)
cdef plc_Table plc_quad_pairs = plc_Table(
[
col.to_pylibcudf(mode="read")
for col in linestring_quad_pairs._columns
]
)
cdef table_view c_linestring_quad_pairs = plc_quad_pairs.view()
cdef plc_Table plc_quadtree = plc_Table(
[col.to_pylibcudf(mode="read") for col in quadtree._columns]
)
cdef table_view c_quadtree = plc_quadtree.view()
cdef column_view c_point_indices = point_indices.view()
cdef column_view c_points_x = points_x.view()
cdef column_view c_points_y = points_y.view()
Expand All @@ -110,7 +137,14 @@ cpdef quadtree_point_to_nearest_linestring(object linestring_quad_pairs,
c_linestring_points_x,
c_linestring_points_y
))
return data_from_unique_ptr(
move(result),
column_names=["point_index", "linestring_index", "distance"]
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(
["point_index", "linestring_index", "distance"],
plc_table.columns()
)
},
None
)
39 changes: 28 additions & 11 deletions python/cuspatial/cuspatial/_lib/trajectory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from libcpp.pair cimport pair
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.utils cimport data_from_unique_ptr
from pylibcudf cimport Table as plc_Table
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.table.table cimport table
Expand All @@ -27,10 +27,17 @@ cpdef derive_trajectories(Column object_id, Column x,
cdef pair[unique_ptr[table], unique_ptr[column]] result
with nogil:
result = move(cpp_derive_trajectories(c_id, c_x, c_y, c_ts))
return data_from_unique_ptr(
move(result.first),
column_names=["object_id", "x", "y", "timestamp"]
), Column.from_unique_ptr(move(result.second))
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result.first))
first_result = (
{
name: Column.from_pylibcudf(col)
for name, col in zip(
["object_id", "x", "y", "timestamp"], plc_table.columns()
)
},
None
)
return first_result, Column.from_unique_ptr(move(result.second))


cpdef trajectory_bounding_boxes(size_type num_trajectories,
Expand All @@ -43,9 +50,15 @@ cpdef trajectory_bounding_boxes(size_type num_trajectories,
result = move(cpp_trajectory_bounding_boxes(
num_trajectories, c_id, c_x, c_y
))
return data_from_unique_ptr(
move(result),
column_names=["x_min", "y_min", "x_max", "y_max"]
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(
["x_min", "y_min", "x_max", "y_max"], plc_table.columns()
)
},
None
)


Expand All @@ -61,7 +74,11 @@ cpdef trajectory_distances_and_speeds(size_type num_trajectories,
result = move(cpp_trajectory_distances_and_speeds(
num_trajectories, c_id, c_x, c_y, c_ts
))
return data_from_unique_ptr(
move(result),
column_names=["distance", "speed"]
cdef plc_Table plc_table = plc_Table.from_libcudf(move(result))
return (
{
name: Column.from_pylibcudf(col)
for name, col in zip(["distance", "speed"], plc_table.columns())
},
None
)

0 comments on commit ad2900d

Please sign in to comment.