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

Make polygon fixture data valid #1472

Merged
merged 14 commits into from
Oct 17, 2024
15 changes: 15 additions & 0 deletions python/cuspatial/cuspatial/testing/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
from itertools import chain

from shapely import get_coordinates


def geometry_to_coords(geom, geom_types):
points_list = geom[geom.apply(lambda x: isinstance(x, geom_types))]
# flatten multigeometries, then geometries, then coordinates
points = list(chain(points_list.apply(get_coordinates)))
coords_list = list(chain(*points))
xy = list(chain(*coords_list))
x = xy[::2]
y = xy[1::2]
return xy, x, y
93 changes: 17 additions & 76 deletions python/cuspatial/cuspatial/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,46 @@
@pytest.fixture
def gs():
g0 = Point(-1, 0)
g1 = MultiPoint(((1, 2), (3, 4)))
g2 = MultiPoint(((5, 6), (7, 8)))
g3 = Point(9, 10)
g1 = Point(9, 10)
g2 = MultiPoint(((1, 2), (3, 4)))
g3 = MultiPoint(((5, 6), (7, 8)))
g4 = LineString(((11, 12), (13, 14)))
g5 = MultiLineString((((15, 16), (17, 18)), ((19, 20), (21, 22))))
g6 = MultiLineString((((23, 24), (25, 26)), ((27, 28), (29, 30))))
g7 = LineString(((31, 32), (33, 34)))
g8 = Polygon(
((35, 36), (37, 38), (39, 40), (41, 42)),
((35, 36), (38, 36), (41, 39), (41, 42)),
)
# TODO: g9, g10, g11 are invalid
# https://github.com/libgeos/geos/issues/1177
g9 = MultiPolygon(
[
(
((43, 44), (45, 46), (47, 48)),
[((49, 50), (51, 52), (53, 54))],
((43, 44), (48, 44), (47, 48)),
[((45, 45), (46, 46), (47, 45))],
),
(
((55, 56), (57, 58), (59, 60)),
[((61, 62), (63, 64), (65, 66))],
((55, 56), (60, 56), (59, 60)),
[((57, 57), (58, 58), (59, 57))],
),
]
)
g10 = MultiPolygon(
[
(
((67, 68), (69, 70), (71, 72)),
[((73, 74), (75, 76), (77, 78))],
((67, 68), (72, 68), (71, 72)),
[((69, 69), (70, 70), (71, 69))],
),
(
((79, 80), (81, 82), (83, 84)),
((79, 80), (90, 82), (83, 90)),
[
((85, 86), (87, 88), (89, 90)),
((91, 92), (93, 94), (95, 96)),
((80, 81), (82, 84), (84, 82)),
((85, 85), (88, 82), (86, 82)),
],
),
]
)
g11 = Polygon(
((97, 98), (99, 101), (102, 103), (101, 108)),
[((106, 107), (108, 109), (110, 111), (113, 108))],
[((99, 102), (100, 103), (101, 103), (100, 102))],
)
gs = gpd.GeoSeries([g0, g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11])
return gs
Expand All @@ -72,8 +70,10 @@ def gpdf(gs):
random_col = int_col
np.random.shuffle(random_col)
str_col = [str(x) for x in int_col]
key_col = np.repeat(np.arange(4), len(int_col) // 4)
key_col = np.repeat(np.arange(4), (len(int_col) // 4) + 1)
key_col = key_col[: len(int_col)]
np.random.shuffle(key_col)

result = gpd.GeoDataFrame(
{
"geometry": gs,
Expand All @@ -87,65 +87,6 @@ def gpdf(gs):
return result


@pytest.fixture
def polys():
return np.array(
(
(35, 36),
(37, 38),
(39, 40),
(41, 42),
(35, 36),
(43, 44),
(45, 46),
(47, 48),
(43, 44),
(49, 50),
(51, 52),
(53, 54),
(49, 50),
(55, 56),
(57, 58),
(59, 60),
(55, 56),
(61, 62),
(63, 64),
(65, 66),
(61, 62),
(67, 68),
(69, 70),
(71, 72),
(67, 68),
(73, 74),
(75, 76),
(77, 78),
(73, 74),
(79, 80),
(81, 82),
(83, 84),
(79, 80),
(85, 86),
(87, 88),
(89, 90),
(85, 86),
(91, 92),
(93, 94),
(95, 96),
(91, 92),
(97, 98),
(99, 101),
(102, 103),
(101, 108),
(97, 98),
(106, 107),
(108, 109),
(110, 111),
(113, 108),
(106, 107),
)
)


@pytest.fixture
def gs_sorted(gs):
result = pd.concat(
Expand Down
9 changes: 0 additions & 9 deletions python/cuspatial/cuspatial/tests/test_cudf_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest

import cuspatial

reason = (
"gs fixture contains invalid Polygons/MultiPolygons: "
"https://github.com/libgeos/geos/issues/1177"
)


@pytest.mark.xfail(reason=reason)
def test_sort_index_series(gs):
gs.index = np.random.permutation(len(gs))
cugs = cuspatial.from_geopandas(gs)
Expand All @@ -21,7 +14,6 @@ def test_sort_index_series(gs):
gpd.testing.assert_geoseries_equal(got, expected)


@pytest.mark.xfail(reason=reason)
def test_sort_index_dataframe(gpdf):
gpdf.index = np.random.permutation(len(gpdf))
cugpdf = cuspatial.from_geopandas(gpdf)
Expand All @@ -30,7 +22,6 @@ def test_sort_index_dataframe(gpdf):
gpd.testing.assert_geodataframe_equal(got, expected)


@pytest.mark.xfail(reason=reason)
def test_sort_values(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
expected = gpdf.sort_values("random")
Expand Down
11 changes: 2 additions & 9 deletions python/cuspatial/cuspatial/tests/test_from_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@ def test_dataframe_column_access(gs):

def test_from_geoseries_complex(gs):
cugs = cuspatial.from_geopandas(gs)
assert cugs.points.xy.sum() == 18
assert cugs.lines.xy.sum() == 540
assert cugs.multipoints.xy.sum() == 36
assert cugs.polygons.xy.sum() == 7436
assert cugs._column.polygons._column.base_children[0].sum() == 15
assert (
cugs._column.polygons._column.base_children[1].base_children[0].sum()
== 38
)
gs_roundtrip = cugs.to_geopandas()
gpd.testing.assert_geoseries_equal(gs_roundtrip, gs)


def test_from_geopandas_point():
Expand Down
16 changes: 10 additions & 6 deletions python/cuspatial/cuspatial/tests/test_geodataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import cudf

import cuspatial
from cuspatial.testing.helpers import geometry_to_coords

np.random.seed(0)

Expand Down Expand Up @@ -114,7 +115,7 @@ def test_type_persistence(gpdf):
assert type(cugpdf["geometry"]) is cuspatial.GeoSeries


def test_interleaved_point(gpdf, polys):
def test_interleaved_point(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
cugs = cugpdf["geometry"]
gs = gpdf["geometry"]
Expand All @@ -128,7 +129,7 @@ def test_interleaved_point(gpdf, polys):
)


def test_interleaved_multipoint(gpdf, polys):
def test_interleaved_multipoint(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
cugs = cugpdf["geometry"]
gs = gpdf["geometry"]
Expand Down Expand Up @@ -156,7 +157,7 @@ def test_interleaved_multipoint(gpdf, polys):
)


def test_interleaved_lines(gpdf, polys):
def test_interleaved_lines(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
cugs = cugpdf["geometry"]
cudf.testing.assert_series_equal(
Expand All @@ -175,16 +176,19 @@ def test_interleaved_lines(gpdf, polys):
)


def test_interleaved_polygons(gpdf, polys):
def test_interleaved_polygons(gpdf):
cugpdf = cuspatial.from_geopandas(gpdf)
cugs = cugpdf["geometry"]
gs = gpdf["geometry"]
xy, x, y = geometry_to_coords(gs, (Polygon, MultiPolygon))

cudf.testing.assert_series_equal(
cudf.Series.from_arrow(cugs.polygons.x.to_arrow()),
cudf.Series(polys[:, 0], dtype="float64"),
cudf.Series(x, dtype="float64"),
)
cudf.testing.assert_series_equal(
cudf.Series.from_arrow(cugs.polygons.y.to_arrow()),
cudf.Series(polys[:, 1], dtype="float64"),
cudf.Series(y, dtype="float64"),
)


Expand Down
Loading
Loading