Parameters
        ----------
        X : array-like, shape [n x m]
            The mask in form of n x m array.
426731
def transform(self, X):
        
        if self.mode_ == 'target':
            return np.apply_along_axis(self._target, 1, np.reshape(X, (X.shape[0], X.shape[1] * X.shape[2])))

        if self.mode_ == 'majority':
            return np.apply_along_axis(self._majority, 1, np.reshape(X, (X.shape[0], X.shape[1] * X.shape[2])))

        print('Invalid mode! Set mode to majority or target. Returning input.')
        return X
519696
def get_region_mask(z, delta, xy=None):
    """"""

    if xy is None:
        ix, iy = np.unravel_index(np.argmax(z), z.shape)
    else:
        ix, iy = xy

    mz = (z > z[ix, iy] - delta)
    labels = label(mz)[0]
    mz &= labels == labels[ix, iy]
    return mz
1257371
def batch_flatten(x):
    
    shape = x.get_shape().as_list()[1:]
    if None not in shape:
        return tf.reshape(x, [-1, int(np.prod(shape))])
    return tf.reshape(x, tf.stack([tf.shape(x)[0], -1]))
567342
def asMaskedArray(self):
        
        return ma.masked_array(data=self.data, mask=self.mask, fill_value=self.fill_value)
314054
def thin(image, mask=None, iterations=1):
    '''
    '''
    global thin_table, eight_connect
    if thin_table is None:
        thin_table = np.zeros((2,512),bool)
        for i in range(512):
            if (i & 16) == 0:
                # All zeros -> 0
                continue
            pat = pattern_of(i & ~ 16)
            ipat = pat.astype(int)
            if scind.label(pat, eight_connect)[1] != 1:
                thin_table[:,i] = True
                continue
            n1 = ((ipat[0,0] or ipat[0,1]) + (ipat[0,2] or ipat[1,2])+
                  (ipat[2,2] or ipat[2,1]) + (ipat[2,0] or ipat[1,0]))
            n2 = ((ipat[0,1] or ipat[0,2]) + (ipat[1,2] or ipat[2,2])+
                  (ipat[2,1] or ipat[2,0]) + (ipat[1,0] or ipat[0,0]))
            if min(n1,n2) not in (2,3):
                thin_table[:,i] = True
                continue
            thin_table[0,i] = ((pat[0,1] or pat[0,2] or not pat[2,2]) and 
                               pat[1,2])
            thin_table[1,i] = ((pat[2,1] or pat[2,0] or not pat[0,0]) and
                               pat[1,0])
    if mask is None:
        masked_image = image.copy()
    else:
        masked_image = image.copy()
        masked_image[~mask] = False
    index_i, index_j, masked_image = prepare_for_index_lookup(masked_image, False)
    if iterations is None:
        iterations = len(index_i)
    for i in range(iterations):
        hit_count = len(index_i)
        for j in range(2):
            index_i, index_j, = index_lookup(index_i, index_j, 
                                             masked_image,
                                             thin_table[j], 1)
        if hit_count == len(index_i):
            break
    masked_image = extract_from_image_lookup(image, index_i, index_j)
    if not mask is None:
        masked_image[~mask] = masked_image[~mask]
    return masked_image
983643
def sample_dropout_mask(x, dropout_probability=.5, columns=None, stream=None, target=None,
                        dropout_mask=None, dropout_prob_array=None):
    

    assert x.flags.c_contiguous

    if columns is not None:
        assert len(columns) == 2
        x_tmp = x
        x = extract_columns(x, columns[0], columns[1])

    shape = x.shape

    if dropout_prob_array is None:
        dropout_prob_array = gpuarray.empty(shape, x.dtype, allocator=memory_pool.allocate)
    sampler.fill_uniform(dropout_prob_array, stream)

    if dropout_mask is None:
        dropout_mask = gpuarray.empty(shape, np.int8, allocator=memory_pool.allocate)

    if target is None: target = x
    
    all_kernels['sample_dropout_mask'](
        x, target, dropout_mask, dropout_prob_array,
        np.float32(dropout_probability))

    if columns is not None:
        insert_columns(x, x_tmp, columns[0])

    return dropout_mask
750446
function(_mask) {
      var raw = [];
      for(var i = 0, l = this.length; i < l; i++) {
        raw.push(this[i].$encode(_mask));
      }

      this.$dispatch('before-render-many', [raw]);
      return raw;
    }
478060
def mask( m )
      check_shape m
      left = MultiArray.new typecode, *( shape.first( dimension - m.dimension ) +
                                         [ m.size ] )
      index = Hornetseye::Pointer( INT ).new
      index.store INT.new( 0 )
      block = Mask.new left, self, m, index
      if block.compilable?
        GCCFunction.run block
      else
        block.demand
      end
      left[0 ... index[]].roll
    end
983401
def slice_hidden(self, x):
    
    x_sliced = tf.reshape(
        x, shape=[-1, self.hparams.num_blocks, self.hparams.block_dim])
    return x_sliced
413888
def maskedFilter(arr, mask, ksize=30, fill_mask=True,
                 fn='median'):
    '''
    
    '''

    if fill_mask:
        mask1 = mask
        out = arr
    else:
        mask1 = ~mask
        out = np.full_like(arr, fill_value=np.nan)
    mask2 = ~mask

    if fn == 'mean':
        _calcMean(arr, mask1, mask2, out, ksize // 2)
    else:
        buff = np.empty(shape=(ksize * ksize), dtype=arr.dtype)
        _calcMedian(arr, mask1, mask2, out, ksize // 2, buff)
    return out
877475
def plot_mask_cells(mask_cells, padding=16):
    
    fig, axes = plt.subplots(len(mask_cells), 3, figsize=(12, 10))
    for idx, (axes, mask_cell) in enumerate(zip(axes, mask_cells), 1):
        ax1, ax2, ax3 = axes
        true_mask, predicted_mask, cell = mask_cell
        plot_mask_cell(
                true_mask, predicted_mask, cell,
                'Type {}'.format(idx),
                ax1, ax2, ax3,
                padding=padding)
    fig.tight_layout()
    return fig, axes
193799
def get_mask(self, m):
        """"""
        mask = torch.ones((m, m, m, self.k_lf, self.k_lf, self.k_lf)).byte()
        for i, j, k in product(range(m), repeat=3):
            if len(set((i, j, k))) < 3:
                mask[i, j, k, :, :, :] = 0
        return mask
360430
def _M2_symmetric(Xvar, Yvar, mask_X=None, mask_Y=None, xsum=0, xconst=0, ysum=0, yconst=0, weights=None,
                  column_selection=None, diag_only=False):
    
    if mask_X is None and mask_Y is None:
        if column_selection is None:
            Xvark = Xvar
            Yvark = Yvar
        else:
            Xvark = Xvar[:, column_selection]
            Yvark = Yvar[:, column_selection]
        Cxxyy = _M2_dense(Xvar, Xvark, weights=weights, diag_only=diag_only) \
                + _M2_dense(Yvar, Yvark, weights=weights, diag_only=diag_only)
        Cxy = _M2_dense(Xvar, Yvark, weights=weights, diag_only=diag_only)
        Cyx = _M2_dense(Yvar, Xvark, weights=weights, diag_only=diag_only)
        Cxyyx = Cxy + Cyx
    else:
        # Check if one of the masks is not None, modify it and also adjust the constant columns:
        if mask_X is None:
            mask_X = np.ones(Xvar.shape[1], dtype=np.bool)
            xconst = np.ones(0, dtype=float)
        if mask_Y is None:
            mask_Y = np.ones(Yvar.shape[1], dtype=np.bool)
            yconst = np.ones(0, dtype=float)
        if _is_zero(xsum) and _is_zero(ysum) or _is_zero(xconst) and _is_zero(yconst):
            Cxxyy, Cxyyx = _M2_sparse_sym(Xvar, mask_X, Yvar, mask_Y, weights=weights, column_selection=column_selection)
        else:
            xvarsum = xsum[mask_X]  # to variable part
            yvarsum = ysum[mask_Y]  # to variable part
            if column_selection is None:
                Xvark = Xvar
                mask_Xk = mask_X
                xkvarsum = xvarsum
                xkconst = xconst
                Yvark = Yvar
                mask_Yk = mask_Y
                ykvarsum = yvarsum
                ykconst = yconst
            else:
                Xvark = Xvar[:, _filter_variable_indices(mask_X, column_selection)]
                mask_Xk = mask_X[column_selection]
                xksum = xsum[column_selection]
                xkvarsum = xksum[mask_Xk]
                xkconst = xconst[_filter_variable_indices(~mask_X, column_selection)]
                Yvark = Yvar[:, _filter_variable_indices(mask_Y, column_selection)]
                mask_Yk = mask_Y[column_selection]
                yksum = ysum[column_selection]
                ykvarsum = yksum[mask_Yk]
                ykconst = yconst[_filter_variable_indices(~mask_Y, column_selection)]
            Cxxyy = _M2_const(Xvar, mask_X, xvarsum, xconst, Xvark, mask_Xk, xkvarsum, xkconst, weights=weights) \
                    + _M2_const(Yvar, mask_Y, yvarsum, yconst, Yvark, mask_Yk, ykvarsum, ykconst, weights=weights)
            Cxy = _M2_const(Xvar, mask_X, xvarsum, xconst, Yvark, mask_Yk, ykvarsum, ykconst, weights=weights)
            Cyx = _M2_const(Yvar, mask_Y, yvarsum, yconst, Xvark, mask_Xk, xkvarsum, xkconst, weights=weights)
            Cxyyx = Cxy + Cyx
    return Cxxyy, Cxyyx
15336
def _xy_locs(mask):
  """"""
  y, x = mask.nonzero()
  return list(zip(x, y))
372559
def segmentation_to_mask(polys, height, width):
    
    polys = [p.flatten().tolist() for p in polys]
    assert len(polys) > 0, "Polygons are empty!"

    import pycocotools.mask as cocomask
    rles = cocomask.frPyObjects(polys, height, width)
    rle = cocomask.merge(rles)
    return cocomask.decode(rle)
261136
def get_submask(self, *args, **kwargs) -> masktools.CustomMask:
        
        if args or kwargs:
            masks = self.availablemasks
            mask = masktools.CustomMask(numpy.full(self.shape, False))
            for arg in args:
                mask = mask + self._prepare_mask(arg, masks)
            for key, value in kwargs.items():
                mask = mask + self._prepare_mask(key, masks, **value)
            if mask not in self.mask:
                raise ValueError(
                    f'Based on the arguments `{args}` and `{kwargs}` '
                    f'the mask `{repr(mask)}` has been determined, '
                    f'which is not a submask of `{repr(self.mask)}`.')
        else:
            mask = self.mask
        return mask
847310
def mask_reduce(mask):
    
    mask = mask.copy()
    for i in range(len(mask.shape)):
        a = mask.copy()
        j = list(range(len(mask.shape)))
        j.remove(i)
        j = tuple(j)
        a = a.max(axis=j, keepdims=True)
        idx = [slice(None)] * len(mask.shape)
        a = a.flatten()
        idx[i] = [k for k in range(len(a)) if a[k]]
        mask = mask[tuple(idx)]
    return mask