Skip to content

Commit

Permalink
Bugfix for cohorts where not all expected_groups are present (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian authored Jan 10, 2024
1 parent 8ea0cd1 commit 676c0f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,10 @@ def invert(x) -> tuple[np.ndarray, ...]:
# TODO: we can optimize this to loop over chunk_cohorts instead
# by zeroing out rows that are already in a cohort
for rowidx in order:
cohort_ = containment.indices[
cohidx = containment.indices[
slice(containment.indptr[rowidx], containment.indptr[rowidx + 1])
]
cohort_ = present_labels[cohidx]
cohort = [elem for elem in cohort_ if elem not in merged_keys]
if not cohort:
continue
Expand Down
10 changes: 10 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,16 @@ def test_find_group_cohorts(expected, labels, chunks: tuple[int]) -> None:
assert actual == expected, (actual, expected)


@requires_dask
def test_find_cohorts_missing_groups():
by = np.array([np.nan, np.nan, np.nan, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0, np.nan, np.nan])
kwargs = {"func": "sum", "expected_groups": [0, 1, 2], "fill_value": 123}
array = dask.array.ones_like(by, chunks=(3,))
actual, _ = groupby_reduce(array, by, method="cohorts", **kwargs)
expected, _ = groupby_reduce(array.compute(), by, **kwargs)
assert_equal(expected, actual)


@pytest.mark.parametrize("chunksize", [12, 13, 14, 24, 36, 48, 72, 71])
def test_verify_complex_cohorts(chunksize: int) -> None:
time = pd.Series(pd.date_range("2016-01-01", "2018-12-31 23:59", freq="H"))
Expand Down

0 comments on commit 676c0f0

Please sign in to comment.