Skip to content

Commit

Permalink
perf: faster largest_k if optional dependency fastremap is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Nov 16, 2024
1 parent 4d6e867 commit 77f0bd5
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions cc3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,20 @@ def largest_k(
preserve.sort(key=lambda x: x[1])
preserve = [ x[0] for x in preserve[-k:] ]

shape, dtype = cc_labels.shape, cc_labels.dtype
rns = fastcc3d.runs(cc_labels)
try:
import fastremap
cc_out = fastremap.mask_except(cc_labels, preserve, in_place=True)
fastremap.renumber(cc_out, in_place=True)
except ImportError:
shape, dtype = cc_labels.shape, cc_labels.dtype
rns = fastcc3d.runs(cc_labels)

order = "C" if cc_labels.flags.c_contiguous else "F"
del cc_labels

cc_out = np.zeros(shape, dtype=dtype, order=order)
for i, label in enumerate(preserve):
fastcc3d.draw(i+1, rns[label], cc_out)
order = "C" if cc_labels.flags.c_contiguous else "F"
del cc_labels
cc_out = np.zeros(shape, dtype=dtype, order=order)
for i, label in enumerate(preserve):
fastcc3d.draw(i+1, rns[label], cc_out)

if return_N:
return cc_out, len(preserve)
Expand Down

0 comments on commit 77f0bd5

Please sign in to comment.