From b6fe047c7391630df457d790a880d9fd683715c3 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sun, 17 Nov 2024 02:15:38 -0500 Subject: [PATCH] perf(largest_k): use faster k-largest algorithm --- cc3d/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cc3d/__init__.py b/cc3d/__init__.py index ad47663..ef1961e 100644 --- a/cc3d/__init__.py +++ b/cc3d/__init__.py @@ -119,9 +119,11 @@ def largest_k( return cc_out, 1 return cc_out - preserve = [ (i,ct) for i,ct in enumerate(cts) if i > 0 ] + preserve = np.argpartition(cts[1:], len(cts) - k - 1)[-k:] + preserve += 1 + preserve = [ (label,cts[label]) for label in preserve ] preserve.sort(key=lambda x: x[1]) - preserve = [ x[0] for x in preserve[-k:] ] + preserve = [ int(x[0]) for x in preserve ] try: import fastremap