Python 把尾随的1放在那里。我可能不得不改变解决方案中的一些东西。那么,再次尝试理解这个问题,假设mask=[1,0,0,0,0,1,1,0,0,0,0,1,0],那么输出必须是什么呢?那将是[1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1]。

Python 把尾随的1放在那里。我可能不得不改变解决方案中的一些东西。那么,再次尝试理解这个问题,假设mask=[1,0,0,0,0,1,1,0,0,0,0,1,0],那么输出必须是什么呢?那将是[1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1]。,python,arrays,numpy,Python,Arrays,Numpy,把尾随的1放在那里。我可能不得不改变解决方案中的一些东西。那么,再次尝试理解这个问题,假设mask=[1,0,0,0,0,1,1,0,0,0,0,1,0],那么输出必须是什么呢?那将是[1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1]。如果阈值为2、3或4,因此要求至少2、3或4个连续零才能保持不变。@LukasBrunner和if mask=[0,0,1,0,0,0,0,0,0,1,1,0,0,0,1,0,0],这与问题中列出的输入相同,但是,如果在开始处有两个额外的零,并


把尾随的
1
放在那里。我可能不得不改变解决方案中的一些东西。那么,再次尝试理解这个问题,假设mask=
[1,0,0,0,0,1,1,0,0,0,0,1,0]
,那么输出必须是什么呢?那将是
[1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1]
。如果
阈值
为2、3或4,因此要求至少2、3或4个连续零才能保持不变。@LukasBrunner和if mask=
[0,0,1,0,0,0,0,0,0,1,1,0,0,0,1,0,0]
,这与问题中列出的输入相同,但是,如果在开始处有两个额外的零,并且再次假设
阈值=3
,那么输出必须是什么?那将是
[1,1,1,0,0,0,1,1,1,1]
new_mask = find_consecutive(mask, threshold=3)
mask[:, i_lon, i_lat]
# [1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0]
new_mask[:, i_lon, i_lat]
# [1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
from scipy.ndimage import measurements
structure = np.zeros((3, 3, 3))
structure[:, 1, 1] = 1
labels, nr_labels = measurements.label(1 - mask, structure=structure)
_, counts = np.unique(labels, return_counts=True)
labels_selected = [i_count for i_count, count in enumerate(counts)
                   if count >= threshold]
from scipy.ndimage import binary_closing
out = mask | binary_closing(mask, structure=np.ones(threshold))
def numpy_binary_closing(mask,threshold):

    # Define kernel
    K = np.ones(threshold)

    # Perform dilation and threshold at 1
    dil = np.convolve(mask,K,mode='same')>=1

    # Perform erosion on the dilated mask array and threshold at given threshold
    dil_erd = np.convolve(dil,K,mode='same')>= threshold
    return dil_erd
In [133]: mask
Out[133]: 
array([ True, False, False, False, False,  True,  True, False, False,
        True, False], dtype=bool)

In [134]: threshold = 3

In [135]: binary_closing(mask, structure=np.ones(threshold))
Out[135]: 
array([False, False, False, False, False,  True,  True,  True,  True,
        True, False], dtype=bool)

In [136]: numpy_binary_closing(mask,threshold)
Out[136]: 
array([False, False, False, False, False,  True,  True,  True,  True,
        True, False], dtype=bool)

In [137]: mask | binary_closing(mask, structure=np.ones(threshold))
Out[137]: 
array([ True, False, False, False, False,  True,  True,  True,  True,
        True, False], dtype=bool)

In [138]: mask| numpy_binary_closing(mask,threshold)
Out[138]: 
array([ True, False, False, False, False,  True,  True,  True,  True,
        True, False], dtype=bool)
In [163]: mask = np.random.rand(10000) > 0.5

In [164]: threshold = 3

In [165]: %timeit binary_closing(mask, structure=np.ones(threshold))
1000 loops, best of 3: 582 µs per loop

In [166]: %timeit numpy_binary_closing(mask,threshold)
10000 loops, best of 3: 178 µs per loop

In [167]: out1 = binary_closing(mask, structure=np.ones(threshold))

In [168]: out2 = numpy_binary_closing(mask,threshold)

In [169]: np.allclose(out1,out2) # Verify outputs
Out[169]: True
In [176]: mask = np.random.rand(10000) > 0.8

In [177]: threshold = 11

In [178]: %timeit binary_closing(mask, structure=np.ones(threshold))
1000 loops, best of 3: 823 µs per loop

In [179]: %timeit numpy_binary_closing(mask,threshold)
1000 loops, best of 3: 331 µs per loop

In [180]: out1 = binary_closing(mask, structure=np.ones(threshold))

In [181]: out2 = numpy_binary_closing(mask,threshold)

In [182]: np.allclose(out1,out2) # Verify outputs
Out[182]: True
mask_ext = np.pad(mask,1,'constant',constant_values=(1))
out = mask_ext | binary_closing(mask_ext, structure=np.ones(threshold))
out = out[1:-1]
In [369]: mask
Out[369]: 
array([False, False,  True, False, False, False, False,  True,  True,
       False, False,  True, False], dtype=bool)

In [370]: threshold = 3

In [371]: mask_ext = np.pad(mask,1,'constant',constant_values=(1))
     ...: out = mask_ext | binary_closing(mask_ext, structure=np.ones(threshold))
     ...: out = out[1:-1]
     ...: 

In [372]: out
Out[372]: 
array([ True,  True,  True, False, False, False, False,  True,  True,
        True,  True,  True,  True], dtype=bool)
import numpy as np
from scipy.ndimage import measurements

def select_consecutive(mask, threshold):

    structure = np.zeros((3, 3, 3))
    structure[:, 1, 1] = 1
    labels, _ = measurements.label(1 - mask, structure=structure)

    # find positions of all unmasked values
    # object_slices = measurements.find_objects(labels)

    _, counts = np.unique(labels, return_counts=True)
    labels_selected = [i_count for i_count, count in enumerate(counts)
                       if count >= threshold and i_count != 0]
    ind = np.in1d(labels.flatten(), labels_selected).reshape(mask.shape)

    mask_new = np.ones_like(mask)
    mask_new[ind] = 0

    return mask_new