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

Optimize bitmask finding some more. #326

Merged
merged 1 commit into from
Jan 20, 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
6 changes: 2 additions & 4 deletions flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,11 @@ def _compute_label_chunk_bitmask(labels, chunks, nlabels):

labels = np.broadcast_to(labels, shape[-labels.ndim :])

rows = []
cols = []
# Add one to handle the -1 sentinel value
label_is_present = np.zeros((nlabels + 1,), dtype=bool)
ilabels = np.arange(nlabels)
for idx, region in enumerate(slices_from_chunks(chunks)):
for region in slices_from_chunks(chunks):
# This is a quite fast way to find unique integers, when we know how many there are
# inspired by a similar idea in numpy_groupies for first, last
# instead of explicitly finding uniques, repeatedly write True to the same location
Expand All @@ -259,10 +258,9 @@ def _compute_label_chunk_bitmask(labels, chunks, nlabels):
# skip the -1 sentinel by slicing
# Faster than np.argwhere by a lot
uniques = ilabels[label_is_present[:-1]]
rows.append(np.full_like(uniques, idx))
cols.append(uniques)
label_is_present[:] = False
rows_array = np.concatenate(rows)
rows_array = np.repeat(np.arange(nchunks), tuple(len(col) for col in cols))
cols_array = np.concatenate(cols)
data = np.broadcast_to(np.array(1, dtype=np.uint8), rows_array.shape)
bitmask = csc_array((data, (rows_array, cols_array)), dtype=bool, shape=(nchunks, nlabels))
Expand Down
Loading