def truncate_mask(m, stop):
    m = m.astype(bool, copy=True)
m[np.searchsorted(np.cumsum(m), stop):] = 0
return m
How to replace only the first n elements in a numpy array that are larger than a certain value?