Skip to content

Try cleaning up some expected_groups logic #175

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

Merged
merged 3 commits into from
Nov 27, 2022
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
23 changes: 8 additions & 15 deletions flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ def subset_to_blocks(
return dask.array.Array(graph, name, chunks, meta=array)


def _extract_unknown_groups(reduced, group_chunks, dtype) -> tuple[DaskArray]:
def _extract_unknown_groups(reduced, dtype) -> tuple[DaskArray]:
import dask.array
from dask.highlevelgraph import HighLevelGraph

Expand All @@ -1180,7 +1180,7 @@ def _extract_unknown_groups(reduced, group_chunks, dtype) -> tuple[DaskArray]:
dask.array.Array(
HighLevelGraph.from_collections(groups_token, layer, dependencies=[reduced]),
groups_token,
chunks=group_chunks,
chunks=((np.nan,),),
meta=np.array([], dtype=dtype),
),
)
Expand Down Expand Up @@ -1293,14 +1293,7 @@ def dask_groupby_agg(
name=f"{name}-chunk-{token}",
)

if expected_groups is None:
if is_duck_dask_array(by_input):
expected_groups = None
else:
expected_groups = _get_expected_groups(by_input, sort=sort)
group_chunks: tuple[tuple[Union[int, float], ...]] = (
(len(expected_groups),) if expected_groups is not None else (np.nan,),
)
group_chunks: tuple[tuple[Union[int, float], ...]]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Illviljan the only float value that is valid here is np.nan. Is there a way to narrow this to just np.nan? A simple Literal[np.nan] does not work.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I am sad about that as well. :(

Thought a little about this in dask/dask#9255 but came to the conclusion that adding float with a reminder comment was the most practical solution. Here's a relevant typing thread: python/typing#1160

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah too bad. Thanks!


if method in ["map-reduce", "cohorts"]:
combine: Callable[..., IntermediateDict]
Expand Down Expand Up @@ -1333,13 +1326,13 @@ def dask_groupby_agg(
aggregate=partial(aggregate, expected_groups=expected_groups, reindex=reindex),
)
if is_duck_dask_array(by_input) and expected_groups is None:
groups = _extract_unknown_groups(reduced, group_chunks=group_chunks, dtype=by.dtype)
groups = _extract_unknown_groups(reduced, dtype=by.dtype)
group_chunks = ((np.nan,),)
else:
if expected_groups is None:
expected_groups_ = _get_expected_groups(by_input, sort=sort)
else:
expected_groups_ = expected_groups
groups = (expected_groups_.to_numpy(),)
expected_groups = _get_expected_groups(by_input, sort=sort)
groups = (expected_groups.to_numpy(),)
group_chunks = ((len(expected_groups),),)

elif method == "cohorts":
chunks_cohorts = find_group_cohorts(
Expand Down