def longest_contiguous_ones(x):
 x = np.ravel(x)
 if (len(x) == 0):
  return np.array([])
 ind = (x == 0).nonzero()[0]
 if (len(ind) == 0):
  return np.arange(len(x))
 if (len(ind) == len(x)):
  return np.array([])
 y = np.zeros(((len(x) + 2),), x.dtype)
 y[1:(-1)] = x
 dif = np.diff(y)
 up = (dif == 1).nonzero()[0]
 dn = (dif == (-1)).nonzero()[0]
 i = ((dn - up) == max((dn - up))).nonzero()[0][0]
 ind = np.arange(up[i], dn[i])
 return ind

def longest_contiguous_ones ( x ) : x = np . ravel ( x ) if ( len ( x ) == 0 ) : return np . array ( [ ] ) ind = ( x == 0 ) . nonzero ( ) [ 0 ] if ( len ( ind ) == 0 ) : return np . arange ( len ( x ) ) if ( len ( ind ) == len ( x ) ) : return np . array ( [ ] ) y = np . zeros ( ( ( len ( x ) + 2 ) , ) , x . dtype ) y [ 1 : ( - 1 ) ] = x dif = np . diff ( y ) up = ( dif == 1 ) . nonzero ( ) [ 0 ] dn = ( dif == ( - 1 ) ) . nonzero ( ) [ 0 ] i = ( ( dn - up ) == max ( ( dn - up ) ) ) . nonzero ( ) [ 0 ] [ 0 ] ind = np . arange ( up [ i ] , dn [ i ] ) return ind
return the indices of the longest stretch of contiguous ones in *x* .