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

Add sort parameter to points model #672

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
22 changes: 13 additions & 9 deletions src/spatialdata/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,31 +656,35 @@ def _(
)
ndim = len(coordinates)
axes = [X, Y, Z][:ndim]
index_monotonically_increasing = data.index.is_monotonic_increasing
if not isinstance(index_monotonically_increasing, bool):
index_monotonically_increasing = index_monotonically_increasing.compute()
if not index_monotonically_increasing:
if "sort" not in kwargs:
index_monotonically_increasing = data.index.is_monotonic_increasing
if not isinstance(index_monotonically_increasing, bool):
index_monotonically_increasing = index_monotonically_increasing.compute()
sort = index_monotonically_increasing
else:
sort = kwargs["sort"]
if not sort:
warnings.warn(
"The index of the dataframe is not monotonic increasing. It is recommended to sort the data to "
"adjust the order of the index before calling .parse() to avoid possible problems due to unknown "
"divisions",
"adjust the order of the index before calling .parse() (or call `parse(sort=True)`) to avoid possible "
"problems due to unknown divisions.",
UserWarning,
stacklevel=2,
)
if isinstance(data, pd.DataFrame):
table: DaskDataFrame = dd.from_pandas( # type: ignore[attr-defined]
pd.DataFrame(data[[coordinates[ax] for ax in axes]].to_numpy(), columns=axes, index=data.index),
# we need to pass sort=True also when the index is sorted to ensure that the divisions are computed
sort=index_monotonically_increasing,
sort=sort,
**kwargs,
)
# we cannot compute the divisions whne the index is not monotonically increasing and npartitions > 1
if not table.known_divisions and (index_monotonically_increasing or table.npartitions == 1):
if not table.known_divisions and (sort or table.npartitions == 1):
table.divisions = table.compute_current_divisions()
if feature_key is not None:
feature_categ = dd.from_pandas(
data[feature_key].astype(str).astype("category"),
sort=index_monotonically_increasing,
sort=sort,
**kwargs,
) # type: ignore[attr-defined]
table[feature_key] = feature_categ
Expand Down
Loading