Skip to content

Commit 38a5ed3

Browse files
committed
Optimize consecutive_true_counts implementation
1 parent ac42e67 commit 38a5ed3

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

micall/utils/overlap_stitcher.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,10 @@ def consecutive_true_counts(arr: np.ndarray) -> np.ndarray:
9292
up to (and including) index i, resetting on False.
9393
"""
9494

95-
n = len(arr)
96-
out = np.empty(n, dtype=np.int64)
97-
count = 0
9895

99-
for i in range(n):
100-
count = (count + 1) * arr[i]
101-
out[i] = count
102-
103-
return out
96+
c = np.cumsum(arr) # running count of 1s
97+
z = np.where(arr == 0, c, 0) # cumsum value at zeros
98+
return (c - np.maximum.accumulate(z)).astype(np.int64)
10499

105100

106101
def exp_accumulate_array_positive(array: np.ndarray) -> np.ndarray:

0 commit comments

Comments
 (0)