I have a (big) boolean array and I'm looking for a way to fill True where it merges two sequences of True with minimal length.
For example:
a = np.array([True] *3 + [False] + [True] *4 + [False] *2 + [True] *2)
# a == array([ True, True, True, False, True, True, True, True, False, False, True, True])
closed_a = close(a, min_merge_size=2)
# closed_a == array([ True, True, True, True, True, True, True, True, False, False, True, True])
Here the False value in index [3] is converted to True because on both sides it has a sequence of at least 2 True elements. Conversely, elements [8] and [9] remain False because the don't have such a sequence on both sides.
I tried using scipy.ndimage.binary_closing with structure=[True True False True True] (and with False in the middle) but it doesn't give me what I need.
Any ideas?
This one was tough, but I was able to come up with something using itertools and more_itertools. Similar to what you had, essentially, the idea is to take consecutive windows on the array, and just directly check if that window contains the indicator sequence of
n * True, False, n * True.You should probably write some tests to check for corner cases, though it does seem to work on this test array:
Of course if you want to mutate the original array instead of returning a copy you can do that too.