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

Fix a bug preventing align from working with a basic slice. #850

Merged
merged 7 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
32 changes: 22 additions & 10 deletions python/cuspatial/cuspatial/core/geoseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,34 @@ def align(self, other):
dtype: geometry)

"""
index = (
other.index if len(other.index) >= len(self.index) else self.index
)

# Merge the two indices and sort them
idx1 = cudf.DataFrame({"idx": self.index})
idx2 = cudf.DataFrame({"idx": other.index})
index = idx1.merge(idx2, how="outer").sort_values("idx")["idx"]
index.name = None
thomcom marked this conversation as resolved.
Show resolved Hide resolved

# Align the two GeoSeries
aligned_left = self._align_to_index(index)
aligned_right = other._align_to_index(index)
aligned_right.index = index

# If the two GeoSeries have the same length, keep the original index
# Otherwise, use the union of the two indices
if len(other) == len(aligned_right):
aligned_right.index = other.index
else:
aligned_right.index = index
if len(self) == len(aligned_left):
aligned_left.index = self.index
else:
aligned_left.index = index

# Aligned GeoSeries are sorted by index
aligned_right = aligned_right.sort_index()
aligned_left.index = (
self.index
if len(self.index) == len(aligned_left)
else aligned_right.index
)
aligned_left = aligned_left.sort_index()
return (
aligned_left,
aligned_right.loc[aligned_left.index],
aligned_right,
)

def _gather(
Expand Down
112 changes: 96 additions & 16 deletions python/cuspatial/cuspatial/tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,104 @@
import cuspatial


def test_align():
gpdpdf = gpd.GeoSeries(
def test_align_more_values():
gpdlhs = gpd.GeoSeries(
[
Polygon(((-8, -8), (-8, 8), (8, 8), (8, -8))),
Polygon(((-2, -2), (-2, 2), (2, 2), (2, -2))),
]
)
gpdshort = gpdpdf.iloc[0:1]
pdf = cuspatial.from_geopandas(gpdpdf)
short = cuspatial.from_geopandas(gpdshort)
expected = gpdshort.align(gpdpdf)
got = short.align(pdf)
gpdrhs = gpdlhs.iloc[0:1]
lhs = cuspatial.from_geopandas(gpdlhs)
rhs = cuspatial.from_geopandas(gpdrhs)
expected = gpdrhs.align(gpdlhs)
got = rhs.align(lhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())
expected = gpdlhs.align(gpdrhs)
got = lhs.align(rhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())


def test_align_some_different_values():
gpdlhs = gpd.GeoSeries(
[
Polygon(((1, 2), (3, 4), (5, 6), (7, 8))),
Polygon(((9, 10), (11, 12), (13, 14), (15, 16))),
Polygon(((17, 18), (19, 20), (21, 22), (23, 24))),
Polygon(((25, 26), (27, 28), (29, 30), (31, 32))),
Polygon(((33, 34), (35, 36), (37, 38), (39, 40))),
Polygon(((41, 42), (43, 44), (45, 46), (47, 48))),
]
)
gpdlhs = gpdlhs
gpdlhs.index = [0, 1, 2, 6, 7, 8]
gpdrhs = gpdlhs
gpdrhs.index = [0, 1, 2, 3, 4, 5]
lhs = cuspatial.from_geopandas(gpdlhs)
rhs = cuspatial.from_geopandas(gpdrhs)
expected = gpdlhs.align(gpdrhs)
got = lhs.align(rhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())


def test_align_more_and_some_different_values():
gpdlhs = gpd.GeoSeries(
[
Polygon(((1, 2), (3, 4), (5, 6), (7, 8))),
Polygon(((9, 10), (11, 12), (13, 14), (15, 16))),
Polygon(((17, 18), (19, 20), (21, 22), (23, 24))),
Polygon(((25, 26), (27, 28), (29, 30), (31, 32))),
Polygon(((33, 34), (35, 36), (37, 38), (39, 40))),
Polygon(((41, 42), (43, 44), (45, 46), (47, 48))),
]
)
gpdrhs = gpdlhs[0:3]
gpdrhs.index = [0, 1, 2]
gpdlhs = gpdlhs
gpdlhs.index = [0, 1, 2, 3, 4, 5]
lhs = cuspatial.from_geopandas(gpdlhs)
rhs = cuspatial.from_geopandas(gpdrhs)
expected = gpdlhs.align(gpdrhs)
got = lhs.align(rhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())
expected = gpdrhs.align(gpdlhs)
got = rhs.align(lhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())


def test_align_with_slice():
gpdlhs = gpd.GeoSeries(
[
Polygon(((1, 2), (3, 4), (5, 6), (7, 8))),
Polygon(((9, 10), (11, 12), (13, 14), (15, 16))),
Polygon(((17, 18), (19, 20), (21, 22), (23, 24))),
Polygon(((25, 26), (27, 28), (29, 30), (31, 32))),
Polygon(((33, 34), (35, 36), (37, 38), (39, 40))),
Polygon(((41, 42), (43, 44), (45, 46), (47, 48))),
]
)
lhs = cuspatial.from_geopandas(gpdlhs)
gpdlhs = gpdlhs[0:3]
gpdrhs = gpdlhs[3:6]
lhs = lhs[0:3]
rhs = lhs[3:6]
expected = gpdlhs.align(gpdrhs)
got = lhs.align(rhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())
expected = gpdrhs.align(gpdlhs)
got = rhs.align(lhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())


def test_align_reorder():
gpdalign = gpd.GeoSeries(
def test_align_out_of_orders_values():
gpdlhs = gpd.GeoSeries(
[
None,
None,
Expand All @@ -38,14 +118,14 @@ def test_align_reorder():
Polygon(((41, 42), (43, 44), (45, 46), (47, 48))),
]
)
gpdreordered = gpdalign.iloc[np.random.permutation(len(gpdalign))]
align = cuspatial.from_geopandas(gpdalign)
reordered = cuspatial.from_geopandas(gpdreordered)
expected = gpdalign.align(gpdreordered)
got = align.align(reordered)
gpdrhs = gpdlhs.iloc[np.random.permutation(len(gpdlhs))]
lhs = cuspatial.from_geopandas(gpdlhs)
rhs = cuspatial.from_geopandas(gpdrhs)
expected = gpdlhs.align(gpdrhs)
got = lhs.align(rhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())
expected = gpdreordered.align(gpdalign)
got = reordered.align(align)
expected = gpdrhs.align(gpdlhs)
got = rhs.align(lhs)
pd.testing.assert_series_equal(expected[0], got[0].to_pandas())
pd.testing.assert_series_equal(expected[1], got[1].to_pandas())