Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Switch to using public cudf testing utilities #431

Merged
merged 17 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions python/cuspatial/cuspatial/tests/test_from_geopandas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2020-2021, NVIDIA CORPORATION.

import geopandas as gpd
import pandas as pd
from shapely.geometry import (
LineString,
MultiLineString,
Expand All @@ -11,7 +11,6 @@
)

import cudf
from cudf.testing._utils import assert_eq

import cuspatial

Expand All @@ -24,9 +23,9 @@ def test_geobuffer_len(gs):
def test_mixed_dataframe(gs):
gpdf = gpd.GeoDataFrame({"a": list(range(100, 100 + len(gs))), "b": gs})
cgdf = cuspatial.from_geopandas(gpdf)
assert_eq(gpdf["a"], cgdf["a"].to_pandas())
pd.testing.assert_series_equal(gpdf["a"], cgdf["a"].to_pandas())
assert gpdf["b"].equals(cgdf["b"].to_pandas())
assert_eq(gpdf, cgdf)
pd.testing.assert_frame_equal(gpdf, cgdf.to_pandas())


def test_dataframe_column_access(gs):
Expand All @@ -48,45 +47,53 @@ def test_from_geoseries_complex(gs):
def test_from_geopandas_point():
gs = gpd.GeoSeries(Point(1.0, 2.0))
cugs = cuspatial.from_geopandas(gs)
assert_eq(cugs.points.xy, cudf.Series([1.0, 2.0]))
cudf.testing.assert_series_equal(cugs.points.xy, cudf.Series([1.0, 2.0]))


def test_from_geopandas_multipoint():
gs = gpd.GeoSeries(MultiPoint([(1.0, 2.0), (3.0, 4.0)]))
cugs = cuspatial.from_geopandas(gs)
assert_eq(cugs.multipoints.xy, cudf.Series([1.0, 2.0, 3.0, 4.0]))
assert_eq(cugs.multipoints.offsets, cudf.Series([0, 4]))
cudf.testing.assert_series_equal(
cugs.multipoints.xy, cudf.Series([1.0, 2.0, 3.0, 4.0])
)
cudf.testing.assert_series_equal(
cugs.multipoints.offsets, cudf.Series([0, 4])
)


def test_from_geopandas_linestring():
gs = gpd.GeoSeries(LineString(((4.0, 3.0), (2.0, 1.0))))
cugs = cuspatial.from_geopandas(gs)
assert_eq(cugs.lines.xy, cudf.Series([4.0, 3.0, 2.0, 1.0]))
assert_eq(cugs.lines.offsets, cudf.Series([0, 4]))
cudf.testing.assert_series_equal(
cugs.lines.xy, cudf.Series([4.0, 3.0, 2.0, 1.0])
)
cudf.testing.assert_series_equal(cugs.lines.offsets, cudf.Series([0, 4]))


def test_from_geopandas_multilinestring():
gs = gpd.GeoSeries(
MultiLineString((((1.0, 2.0), (3.0, 4.0)), ((5.0, 6.0), (7.0, 8.0)),))
)
cugs = cuspatial.from_geopandas(gs)
assert_eq(
cudf.testing.assert_series_equal(
cugs.lines.xy, cudf.Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]),
)
assert_eq(cugs.lines.offsets, cudf.Series([0, 4, 8]))
cudf.testing.assert_series_equal(
cugs.lines.offsets, cudf.Series([0, 4, 8])
)


def test_from_geopandas_polygon():
gs = gpd.GeoSeries(
Polygon(((0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (0.0, 0.0)),)
)
cugs = cuspatial.from_geopandas(gs)
assert_eq(
cudf.testing.assert_series_equal(
cugs.polygons.xy,
cudf.Series([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
)
assert_eq(cugs.polygons.polys, cudf.Series([0, 1]))
assert_eq(cugs.polygons.rings, cudf.Series([0, 8]))
cudf.testing.assert_series_equal(cugs.polygons.polys, cudf.Series([0, 1]))
cudf.testing.assert_series_equal(cugs.polygons.rings, cudf.Series([0, 8]))


def test_from_geopandas_polygon_hole():
Expand All @@ -97,7 +104,7 @@ def test_from_geopandas_polygon_hole():
)
)
cugs = cuspatial.from_geopandas(gs)
assert_eq(
cudf.testing.assert_series_equal(
cugs.polygons.xy,
cudf.Series(
[
Expand All @@ -120,8 +127,10 @@ def test_from_geopandas_polygon_hole():
]
),
)
assert_eq(cugs.polygons.polys, cudf.Series([0, 2]))
assert_eq(cugs.polygons.rings, cudf.Series([0, 8, 16]))
cudf.testing.assert_series_equal(cugs.polygons.polys, cudf.Series([0, 2]))
cudf.testing.assert_series_equal(
cugs.polygons.rings, cudf.Series([0, 8, 16])
)


def test_from_geopandas_multipolygon():
Expand All @@ -136,7 +145,7 @@ def test_from_geopandas_multipolygon():
)
)
cugs = cuspatial.from_geopandas(gs)
assert_eq(
cudf.testing.assert_series_equal(
cugs.polygons.xy,
cudf.Series(
[
Expand All @@ -159,5 +168,7 @@ def test_from_geopandas_multipolygon():
]
),
)
assert_eq(cugs.polygons.polys, cudf.Series([0, 2]))
assert_eq(cugs.polygons.rings, cudf.Series([0, 8, 16]))
cudf.testing.assert_series_equal(cugs.polygons.polys, cudf.Series([0, 2]))
cudf.testing.assert_series_equal(
cugs.polygons.rings, cudf.Series([0, 8, 16])
)
47 changes: 28 additions & 19 deletions python/cuspatial/cuspatial/tests/test_geoarrowbuffers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2021, NVIDIA CORPORATION.

import numpy as np
import pandas as pd
from geopandas import GeoSeries as gpGeoSeries
from shapely.geometry import (
LineString,
Expand All @@ -12,29 +12,34 @@
)

import cudf
from cudf.testing._utils import assert_eq

from cuspatial import GeoArrowBuffers, GeoSeries
from cuspatial.geometry.geocolumn import GeoColumn


def test_points():
buffers = GeoArrowBuffers({"points_xy": [0, 1, 2, 3]})
assert_eq(cudf.Series([0, 1, 2, 3]), buffers.points.xy)
cudf.testing.assert_series_equal(
cudf.Series([0, 1, 2, 3]), buffers.points.xy
)
assert len(buffers.points) == 2
column = GeoColumn(buffers)
assert_eq(GeoSeries(column), gpGeoSeries([Point(0, 1), Point(2, 3)]))
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(), gpGeoSeries([Point(0, 1), Point(2, 3)])
)


def test_multipoints():
buffers = GeoArrowBuffers(
{"mpoints_xy": np.arange(0, 16), "mpoints_offsets": [0, 4, 8, 12, 16]}
)
assert_eq(cudf.Series(np.arange(0, 16)), buffers.multipoints.xy)
cudf.testing.assert_series_equal(
cudf.Series(np.arange(0, 16)), buffers.multipoints.xy
)
assert len(buffers.multipoints) == 4
column = GeoColumn(buffers)
assert_eq(
GeoSeries(column),
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(),
gpGeoSeries(
[
MultiPoint([Point([0, 1]), Point([2, 3])]),
Expand All @@ -50,11 +55,11 @@ def test_homogeneous_lines():
buffers = GeoArrowBuffers(
{"lines_xy": range(24), "lines_offsets": np.array(range(5)) * 6}
)
assert_eq(cudf.Series(range(24)), buffers.lines.xy)
cudf.testing.assert_series_equal(cudf.Series(range(24)), buffers.lines.xy)
assert len(buffers.lines) == 4
column = GeoColumn(buffers)
assert_eq(
GeoSeries(column),
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(),
gpGeoSeries(
[
LineString([[0, 1], [2, 3], [4, 5]]),
Expand All @@ -74,11 +79,11 @@ def test_mixed_lines():
"mlines": [1, 3],
}
)
assert_eq(cudf.Series(range(24)), buffers.lines.xy)
cudf.testing.assert_series_equal(cudf.Series(range(24)), buffers.lines.xy)
assert len(buffers.lines) == 3
column = GeoColumn(buffers)
assert_eq(
GeoSeries(column),
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(),
gpGeoSeries(
[
LineString([[0, 1], [2, 3], [4, 5]]),
Expand Down Expand Up @@ -108,11 +113,13 @@ def test_homogeneous_polygons():
"polygons_rings": np.arange(11) * 8,
}
)
assert_eq(cudf.Series(polygons_xy.flatten()), buffers.polygons.xy)
cudf.testing.assert_series_equal(
cudf.Series(polygons_xy.flatten()), buffers.polygons.xy
)
assert len(buffers.polygons) == 6
column = GeoColumn(buffers)
assert_eq(
GeoSeries(column),
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(),
gpGeoSeries(
[
Polygon(((0, 1), (2, 3), (4, 5))),
Expand Down Expand Up @@ -153,11 +160,13 @@ def test_polygons():
"mpolygons": [2, 4],
}
)
assert_eq(cudf.Series(polygons_xy.flatten()), buffers.polygons.xy)
cudf.testing.assert_series_equal(
cudf.Series(polygons_xy.flatten()), buffers.polygons.xy
)
assert len(buffers.polygons) == 5
column = GeoColumn(buffers)
assert_eq(
GeoSeries(column),
pd.testing.assert_series_equal(
GeoSeries(column).to_pandas(),
gpGeoSeries(
[
Polygon(((0, 1), (2, 3), (4, 5))),
Expand Down
61 changes: 30 additions & 31 deletions python/cuspatial/cuspatial/tests/test_geodataframe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright (c) 2020-2021, NVIDIA CORPORATION.

import geopandas as gpd
import numpy as np
import pandas as pd
Expand All @@ -14,7 +13,7 @@
Polygon,
)

from cudf.testing._utils import assert_eq
import cudf

import cuspatial

Expand Down Expand Up @@ -80,18 +79,6 @@ def assert_eq_multipoint(p1, p2):
assert_eq_point(p1[i], p2[i])


def assert_eq_linestring(p1, p2):
assert type(p1) == type(p2)
assert len(p1.coords) == len(p2.coords)
for i in range(len(p1.coords)):
assert_eq(p1.coords[i], p2.coords[i])


def assert_eq_multilinestring(p1, p2):
for i in range(len(p1)):
assert_eq_linestring(p1[i], p2[i])


def assert_eq_polygon(p1, p2):
if not p1.equals(p2):
raise ValueError
Expand All @@ -112,21 +99,23 @@ def assert_eq_geo_df(geo1, geo2):

def test_select_multiple_columns(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
assert_eq(cugpdf[["geometry", "key"]], gpdf[["geometry", "key"]])
pd.testing.assert_frame_equal(
cugpdf[["geometry", "key"]].to_pandas(), gpdf[["geometry", "key"]]
)


def test_sort_values(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
sort_gpdf = gpdf.sort_values("random")
sort_cugpdf = cugpdf.sort_values("random")
assert_eq(sort_gpdf, sort_cugpdf)
sort_cugpdf = cugpdf.sort_values("random").to_pandas()
pd.testing.assert_frame_equal(sort_gpdf, sort_cugpdf)


def test_groupby(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
assert_eq(
pd.testing.assert_frame_equal(
gpdf.groupby("key").min().sort_index(),
cugpdf.groupby("key").min().sort_index(),
cugpdf.groupby("key").min().sort_index().to_pandas(),
)


Expand All @@ -139,34 +128,44 @@ def test_interleaved_point(gpdf, polys):
cugpdf = cuspatial.from_geopandas(gpdf)
cugs = cugpdf["geometry"]
gs = gpdf["geometry"]
assert_eq(cugs.points.x, gs[gs.type == "Point"].x.reset_index(drop=True))
assert_eq(cugs.points.y, gs[gs.type == "Point"].y.reset_index(drop=True))
assert_eq(
pd.testing.assert_series_equal(
cugs.points.x.to_pandas(),
gs[gs.type == "Point"].x.reset_index(drop=True),
)
pd.testing.assert_series_equal(
cugs.points.y.to_pandas(),
gs[gs.type == "Point"].y.reset_index(drop=True),
)
cudf.testing.assert_series_equal(
cugs.multipoints.x,
pd.Series(
cudf.Series(
np.array(
[np.array(p)[:, 0] for p in gs[gs.type == "MultiPoint"]]
).flatten()
),
)
assert_eq(
cudf.testing.assert_series_equal(
cugs.multipoints.y,
pd.Series(
cudf.Series(
np.array(
[np.array(p)[:, 1] for p in gs[gs.type == "MultiPoint"]]
).flatten()
),
)
assert_eq(
cudf.testing.assert_series_equal(
cugs.lines.x,
pd.Series(np.array([range(11, 34, 2)]).flatten(), dtype="float64",),
cudf.Series(np.array([range(11, 34, 2)]).flatten(), dtype="float64",),
)
assert_eq(
cudf.testing.assert_series_equal(
cugs.lines.y,
pd.Series(np.array([range(12, 35, 2)]).flatten(), dtype="float64",),
cudf.Series(np.array([range(12, 35, 2)]).flatten(), dtype="float64",),
)
cudf.testing.assert_series_equal(
cugs.polygons.x, cudf.Series(polys[:, 0], dtype="float64")
)
cudf.testing.assert_series_equal(
cugs.polygons.y, cudf.Series(polys[:, 1], dtype="float64")
)
assert_eq(cugs.polygons.x, pd.Series(polys[:, 0], dtype="float64"))
assert_eq(cugs.polygons.y, pd.Series(polys[:, 1], dtype="float64"))


def test_to_shapely_random():
Expand Down
Loading