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

Handle larger than 32bit distances when creating Hilbert distance and spatially partitioning. #219

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

bernardpazio
Copy link
Contributor

I have come across an issue with spatially partitioning data where the distance between geometry as calculated by hilbert_distance is greater than a 32bit integer. The meta for the returned series is set to 32bit which causes dask to cast down to 32bit when setting index, this causes an overflow and the resulting divisions being out of order. Here is a minimal example to show the issue.

import geopandas as gpd
import dask_geopandas as dgpd
from shapely.geometry import Point

df = gpd.GeoDataFrame({
    'geometry': [Point(-103152.516, -8942.156), Point(118914.500, 1010032.562)]
})

ddf = dgpd.from_geopandas(df, npartitions=2)
ddf.spatial_shuffle()

setting correctly to int64 before passing to dask fixes this issue.

from dask.dataframe.shuffle import set_index
import numpy as np

by = ddf.hilbert_distance()
set_index(ddf, by.astype(np.int64)).compute()

It should be a simple fix to handle these cases, so I've gone ahead and created a branch for your review.

Bernard Pazio added 6 commits October 3, 2022 13:48
set hilbert distance to dtype to int64
fix formatting
fix formatting
fix formatting
fix formatting
@bernardpazio
Copy link
Contributor Author

Looks like the failures are not related to my changes.

@@ -389,7 +389,7 @@ def hilbert_distance(self, total_bounds=None, level=16):
_hilbert_distance,
total_bounds=total_bounds,
level=level,
meta=pd.Series([], name="hilbert_distance", dtype="uint32"),
meta=pd.Series([], name="hilbert_distance", dtype=np.int64),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an option provided by the user, rather than assuming int64. The default should be uint32 for backwards compatability.

@@ -43,7 +43,9 @@ def _hilbert_distance(gdf, total_bounds=None, level=16):
# Compute distance along hilbert curve
distances = _encode(level, x, y)

return pd.Series(distances, index=gdf.index, name="hilbert_distance")
return pd.Series(
distances, index=gdf.index, name="hilbert_distance", dtype=np.int64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't distances already be a uint32? I suspect that the changes will need to be at a lower level (maybe in _encode), and then when we're constructing a Series here the dtype will just be the dtype of whatever ndarray is passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right, i had missed that this is actually an unsigned int. The reason for the overflow is actually that the value is greater than the max value for int but not uint, and dask simply isn't respecting the dtype.

@bernardpazio
Copy link
Contributor Author

bernardpazio commented Oct 4, 2022

FYI this problem seems to be only an issue with dask<2022.8 and pandas<1.5

@martinfleis
Copy link
Member

FYI this problem seems to be only an issue with dask<2022.8 and pandas<1.5

In this case I suggest we recommend people updating their environment and close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants