예제 #1
0
파일: _morphology.py 프로젝트: takagi/cupy
def black_tophat(
    input,
    size=None,
    footprint=None,
    structure=None,
    output=None,
    mode='reflect',
    cval=0.0,
    origin=0,
):
    """
    Multidimensional black tophat filter.

    Args:
        input (cupy.ndarray): The input array.
        size (tuple of ints): Shape of a flat and full structuring element used
            for the black tophat. Optional if ``footprint`` or ``structure`` is
            provided.
        footprint (array of ints): Positions of non-infinite elements of a flat
            structuring element used for the black tophat. Non-zero values
            give the set of neighbors of the center over which opening is
            chosen.
        structure (array of ints): Structuring element used for the black
            tophat. ``structure`` may be a non-flat structuring element.
        output (cupy.ndarray, dtype or None): The array in which to place the
            output.
        mode (str): The array borders are handled according to the given mode
            (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``,
            ``'wrap'``). Default is ``'reflect'``.
        cval (scalar): Value to fill past edges of input if mode is
            ``constant``. Default is ``0.0``.
        origin (scalar or tuple of scalar): The origin parameter controls the
            placement of the filter, relative to the center of the current
            element of the input. Default of 0 is equivalent to
            ``(0,)*input.ndim``.

    Returns:
        cupy.ndarry : Result of the filter of ``input`` with ``structure``.

    .. seealso:: :func:`scipy.ndimage.black_tophat`
    """
    if (size is not None) and (footprint is not None):
        warnings.warn(
            'ignoring size because footprint is set', UserWarning, stacklevel=2
        )
    tmp = grey_dilation(
        input, size, footprint, structure, None, mode, cval, origin
    )
    tmp = grey_erosion(
        tmp, size, footprint, structure, output, mode, cval, origin
    )
    if input.dtype == numpy.bool_ and tmp.dtype == numpy.bool_:
        cupy.bitwise_xor(tmp, input, out=tmp)
    else:
        cupy.subtract(tmp, input, out=tmp)
    return tmp
예제 #2
0
    def transform(self, X):
        """[summary].

        Args:
            X (cupy.ndarray): [description].
        Returns:
            cupy.ndarray: [description].
        """
        check_is_fitted(self, "class_means_")
        # TODO(smly):
        # X = column_or_1d(X, warn=True)

        # Label encoding if necessary
        if self._label_encoding_uniques is not None:
            X = self._label_encoding_uniques.get_indexer(X.to_pandas())
        X = cupy.asarray(X)

        missing_mask = cupy.isnan(X)
        encode_mask = cupy.invert(missing_mask)
        unseen_mask = cupy.bitwise_xor(
            cupy.isin(X, self.classes_, invert=True), missing_mask)

        X = X.copy()
        X[unseen_mask] = cupy.max(self.classes_)

        indices = _get_index_cupy(self.classes_, X[encode_mask])

        _classes_index_list = cupy.searchsorted(self.lut_[:, 0], self.classes_)
        encoded_values = cupy.zeros(X.shape[0], dtype=cupy.float32)
        encoded_values[encode_mask] = cupy.take(
            self.lut_[:, 1], cupy.take(_classes_index_list, indices))

        encoded_values[unseen_mask] = self.default_unseen_
        return encoded_values
예제 #3
0
파일: ngen.py 프로젝트: calper-ql/NGEN
def int_value_noise_3d(x, y, z, seed):
    x = x.astype(int)
    y = y.astype(int)
    z = z.astype(int)
    c = (__BN_X_NOISE_GEN * x) + (__BN_Y_NOISE_GEN * y) + (
        __BN_Z_NOISE_GEN * z) + __BN_SEED_NOISE_GEN * seed
    c = cp.bitwise_and(c, 0x7fffffff)
    c = cp.bitwise_xor(cp.right_shift(c, 13), c)
    c = cp.bitwise_and((c * (c * c * 60493 + 19990303) + 1376312589),
                       0x7fffffff)
    return c
예제 #4
0
def bitwise_xor(x1: Array, x2: Array, /) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.bitwise_xor <numpy.bitwise_xor>`.

    See its docstring for more information.
    """
    if (x1.dtype not in _integer_or_boolean_dtypes
            or x2.dtype not in _integer_or_boolean_dtypes):
        raise TypeError(
            "Only integer or boolean dtypes are allowed in bitwise_xor")
    # Call result type here just to raise on disallowed type combinations
    _result_type(x1.dtype, x2.dtype)
    x1, x2 = Array._normalize_two_args(x1, x2)
    return Array._new(np.bitwise_xor(x1._array, x2._array))
예제 #5
0
def ECP(error_weight, orig_weight, set_map):
    orig_shape = error_weight.shape
    error_weight, orig_weight = cp.ravel(error_weight.view(
        cp.uint32)), cp.ravel(orig_weight.view(cp.uint32))
    shape = (int(error_weight.__len__() / 16), 16)
    # Reshape 64 bit in one row
    error_weight, orig_weight = cp.reshape(error_weight, shape), cp.reshape(
        orig_weight, shape)
    # Calculate stuck bits
    stuck_bits = cp.bitwise_xor(error_weight, orig_weight)
    stuck_bits_sum = cp.sum(stuck_bits, axis=1)
    error = cp.concatenate(cp.in1d(stuck_bits_sum, set_map).nonzero())

    if error.__len__() == 0:
        return cp.reshape(error_weight, orig_shape).view(cp.float32)
    else:
        error_weight[error, :] = orig_weight[error, :]
        return cp.reshape(error_weight, orig_shape).view(cp.float32)
예제 #6
0
파일: ngen.py 프로젝트: calper-ql/NGEN
def gradient_noise_3d(fx, fy, fz, ix, iy, iz, seed):
    vi = (__BN_X_NOISE_GEN * ix) + (__BN_Y_NOISE_GEN * iy) + (
        __BN_Z_NOISE_GEN * iz) + __BN_SEED_NOISE_GEN * seed
    vi = cp.bitwise_and(vi, 0xffffffff)
    vi = cp.bitwise_xor(vi, cp.right_shift(vi, __BN_SHIFT_NOISE_GEN))
    vi = cp.bitwise_and(vi, 0xff)

    vi_l2 = cp.left_shift(vi, 2)

    xvGrad = g_randomVectors[vi_l2]
    yvGrad = g_randomVectors[vi_l2 + 1]
    zvGrad = g_randomVectors[vi_l2 + 2]

    xvPoint = fx - ix
    yvPoint = fy - iy
    zvPoint = fz - iz

    return ((xvGrad * xvPoint) + (yvGrad * yvPoint) +
            (zvGrad * zvPoint)) * 2.12
예제 #7
0
            s = time.time()
            Wb = _preprocess()(Wb,
                               Wb.shape[0],
                               cupy.zeros((Wb.shape[0], Wb.shape[1]//32)).astype("int32"),
                               size=Wb.shape[1]
                              )
            preprocess_time += time.time()-s
            s = time.time()
            xb = _preprocess_vec()(xb,
                                   cupy.zeros((xb.shape[0]//32)).astype("int32"),
                                   size=xb.shape[0]
                                  )
            preprocess_vec_time += time.time()-s
            
            s          = time.time()
            yb         = cupy.invert(cupy.bitwise_xor(Wb, xb))
            xnor_time += time.time()-s
            
            s              = time.time()
            yb             = _popcount()(yb, axis=1)
            popcount_time += time.time()-s
        
        print "binarize_time: {0}".format(binarize_time)
        print "preprocess_time: {0}".format(preprocess_time)
        print "preprocess_vec_time: {0}".format(preprocess_vec_time)
        print "xnor_time: {0}".format(xnor_time)
        print "popcount_time: {0}".format(popcount_time)

        binarize_log.append(binarize_time)
        preprocess_log.append(preprocess_time)
        preprocess_vec_log.append(preprocess_vec_time)
예제 #8
0
 def __rxor__(self, other):
     return cupy.bitwise_xor(other, self)
예제 #9
0
 def __xor__(self, other):
     return cupy.bitwise_xor(self, other)
예제 #10
0
            s = time.time()
            Wb = _preprocess()(Wb,
                               Wb.shape[0],
                               cupy.zeros((Wb.shape[0],
                                           Wb.shape[1] // 32)).astype("int32"),
                               size=Wb.shape[1])
            preprocess_time += time.time() - s
            s = time.time()
            xb = _preprocess_vec()(xb,
                                   cupy.zeros(
                                       (xb.shape[0] // 32)).astype("int32"),
                                   size=xb.shape[0])
            preprocess_vec_time += time.time() - s

            s = time.time()
            yb = cupy.invert(cupy.bitwise_xor(Wb, xb))
            xnor_time += time.time() - s

            s = time.time()
            yb = _popcount()(yb, axis=1)
            popcount_time += time.time() - s

        print "binarize_time: {0}".format(binarize_time)
        print "preprocess_time: {0}".format(preprocess_time)
        print "preprocess_vec_time: {0}".format(preprocess_vec_time)
        print "xnor_time: {0}".format(xnor_time)
        print "popcount_time: {0}".format(popcount_time)

        binarize_log.append(binarize_time)
        preprocess_log.append(preprocess_time)
        preprocess_vec_log.append(preprocess_vec_time)