コード例 #1
0
def _binary_clf_curve(y_true, y_score):

    if y_true.dtype.kind == 'f' and np.any(y_true != y_true.astype(int)):
        raise ValueError("Continuous format of y_true  " "is not supported.")

    ids = cp.argsort(-y_score)
    sorted_score = y_score[ids]

    ones = y_true[ids].astype('float32')  # for calculating true positives
    zeros = 1 - ones  # for calculating predicted positives

    # calculate groups
    group = _group_same_scores(sorted_score)
    num = int(group[-1])

    tps = cp.zeros(num, dtype='float32')
    fps = cp.zeros(num, dtype='float32')

    tps = _addup_x_in_group(group, ones, tps)
    fps = _addup_x_in_group(group, zeros, fps)

    tps = cp.cumsum(tps)
    fps = cp.cumsum(fps)
    thresholds = cp.unique(y_score)
    return fps, tps, thresholds
コード例 #2
0
def _csr_row_index(rows, Ap, Aj, Ax):
    """Populate indices and data arrays from the given row index

    Args:
        rows (cupy.ndarray): index array of rows to populate
        Ap (cupy.ndarray): indptr array from input sparse matrix
        Aj (cupy.ndarray): indices array from input sparse matrix
        Ax (cupy.ndarray): data array from input sparse matrix

    Returns:
        Bp (cupy.ndarray): indptr array for output sparse matrix
        Bj (cupy.ndarray): indices array of output sparse matrix
        Bx (cupy.ndarray): data array of output sparse matrix

    """
    row_nnz = cupy.diff(Ap)
    Bp = cupy.empty(rows.size + 1, dtype=Ap.dtype)
    Bp[0] = 0
    cupy.cumsum(row_nnz[rows], out=Bp[1:])
    nnz = int(Bp[-1])

    out_rows = cupy.empty(nnz, dtype=numpy.int32)

    # Build a COO row array from output CSR indptr.
    # Calling backend cusparse API directly to avoid
    # constructing a whole COO object.
    handle = device.get_cusparse_handle()
    cusparse.xcsr2coo(handle, Bp.data.ptr, nnz, Bp.size - 1, out_rows.data.ptr,
                      cusparse.CUSPARSE_INDEX_BASE_ZERO)

    Bj, Bx = _csr_row_index_ker(out_rows, rows, Ap, Aj, Ax, Bp)
    return Bp, Bj, Bx
コード例 #3
0
ファイル: compressed.py プロジェクト: Dahlia-Chehata/cupy
    def _major_slice(self, idx, copy=False):
        """Index along the major axis where idx is a slice object.
        """

        if idx == slice(None):
            return self.copy() if copy else self

        M, N = self._swap(*self.shape)
        start, stop, step = idx.indices(M)
        M = len(range(start, stop, step))
        new_shape = self._swap(M, N)
        if M == 0:
            return self.__class__(new_shape)

        row_nnz = cupy.diff(self.indptr)
        idx_dtype = self.indices.dtype
        res_indptr = cupy.zeros(M + 1, dtype=idx_dtype)

        cupy.cumsum(row_nnz[idx], out=res_indptr[1:])

        if step == 1:
            idx_start = self.indptr[start]
            idx_stop = self.indptr[stop]
            res_indices = cupy.array(self.indices[idx_start:idx_stop],
                                     copy=copy)
            res_data = cupy.array(self.data[idx_start:idx_stop], copy=copy)
        else:
            res_indices, res_data = _index._csr_row_slice(
                start, step, self.indptr, self.indices, self.data, res_indptr)

        return self.__class__((res_data, res_indices, res_indptr),
                              shape=new_shape,
                              copy=False)
コード例 #4
0
def _get_csr_submatrix_minor_axis(Ap, Aj, Ax, start, stop):
    """Return a submatrix of the input sparse matrix by slicing minor axis.

    Args:
        Ap (cupy.ndarray): indptr array from input sparse matrix
        Aj (cupy.ndarray): indices array from input sparse matrix
        Ax (cupy.ndarray): data array from input sparse matrix
        start (int): starting index of minor axis
        stop (int): ending index of minor axis

    Returns:
        Bp (cupy.ndarray): indptr array of output sparse matrix
        Bj (cupy.ndarray): indices array of output sparse matrix
        Bx (cupy.ndarray): data array of output sparse matrix

    """
    mask = (start <= Aj) & (Aj < stop)
    mask_sum = cupy.empty(Aj.size + 1, dtype=Aj.dtype)
    mask_sum[0] = 0
    mask_sum[1:] = mask
    cupy.cumsum(mask_sum, out=mask_sum)
    Bp = mask_sum[Ap]
    Bj = Aj[mask] - start
    Bx = Ax[mask]

    return Bp, Bj, Bx
コード例 #5
0
    def _perform_insert(self, indices_inserts, data_inserts,
                        rows, row_counts, idx_dtype):
        """Insert new elements into current sparse matrix in sorted order"""
        indptr_diff = cupy.diff(self.indptr)
        indptr_diff[rows] += row_counts

        new_indptr = cupy.empty(self.indptr.shape, dtype=idx_dtype)
        new_indptr[0] = idx_dtype(0)
        new_indptr[1:] = indptr_diff

        # Build output arrays
        cupy.cumsum(new_indptr, out=new_indptr)
        out_nnz = int(new_indptr[-1])

        new_indices = cupy.empty(out_nnz, dtype=idx_dtype)
        new_data = cupy.empty(out_nnz, dtype=self.data.dtype)

        # Build an indexed indptr that contains the offsets for each
        # row but only for in i, j, and x.
        new_indptr_lookup = cupy.zeros(new_indptr.size, dtype=idx_dtype)
        new_indptr_lookup[1:][rows] = row_counts
        cupy.cumsum(new_indptr_lookup, out=new_indptr_lookup)

        _index._insert_many_populate_arrays(
            indices_inserts, data_inserts, new_indptr_lookup,
            self.indptr, self.indices, self.data, new_indptr, new_indices,
            new_data, size=self.indptr.size-1)

        self.indptr = new_indptr
        self.indices = new_indices
        self.data = new_data
コード例 #6
0
ファイル: matfuncs.py プロジェクト: ameyachawla99/cupy
def sinhm(arr):
    """Hyperbolic Sine calculation for a given nXn matrix
    Args:
        arr(cupy.ndarray) :
                            Square matrix with dimension nXn.
    Returns:
        (cupy.ndarray):
                        Hyperbolic sine of given square matrix as input.
    ..seealso:: :func: 'scipy.linalg.sinhm'
    """

    arr = cupy.array(arr)

    # Checking whether the input is a 2D matrix or not
    if (len(arr.shape) != 2):
        raise ValueError("Dimensions of matrix should be 2")

    # Checking whether the input matrix is square matrix or not
    if (arr.shape[0] != arr.shape[1]):
        raise ValueError("Input matrix should be a square matrix")

    # Checking whether the input matrix elements are nan or not
    if (cupy.isnan(cupy.cumsum(arr)[2 * arr.shape[0] - 1])):
        raise ValueError("Input matrix elements cannot be nan")

    # Checking whether the input matrix elements are infinity or not
    if (cupy.isinf(cupy.cumsum(arr)[2 * arr.shape[0] - 1])):
        raise ValueError("Input matrix elements cannot be infinity")

    return 0.5 * (cupy.exp(arr) - cupy.exp(-1 * arr))
コード例 #7
0
def threshold_yen(image=None, nbins=256, *, hist=None):
    """Return threshold value based on Yen's method.
    Either image or hist must be provided. In case hist is given, the actual
    histogram of the image is ignored.

    Parameters
    ----------
    image : (N, M) ndarray, optional
        Input image.
    nbins : int, optional
        Number of bins used to calculate histogram. This value is ignored for
        integer arrays.
    hist : array, or 2-tuple of arrays, optional
        Histogram from which to determine the threshold, and optionally a
        corresponding array of bin center intensities.
        An alternative use of this function is to pass it only hist.

    Returns
    -------
    threshold : float
        Upper threshold value. All pixels with an intensity higher than
        this value are assumed to be foreground.

    References
    ----------
    .. [1] Yen J.C., Chang F.J., and Chang S. (1995) "A New Criterion
           for Automatic Multilevel Thresholding" IEEE Trans. on Image
           Processing, 4(3): 370-378. :DOI:`10.1109/83.366472`
    .. [2] Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
           Techniques and Quantitative Performance Evaluation" Journal of
           Electronic Imaging, 13(1): 146-165, :DOI:`10.1117/1.1631315`
           http://www.busim.ee.boun.edu.tr/~sankur/SankurFolder/Threshold_survey.pdf
    .. [3] ImageJ AutoThresholder code, http://fiji.sc/wiki/index.php/Auto_Threshold

    Examples
    --------
    >>> from skimage.data import camera
    >>> image = camera()
    >>> thresh = threshold_yen(image)
    >>> binary = image <= thresh
    """
    counts, bin_centers = _validate_image_histogram(image, hist, nbins)

    # On blank images (e.g. filled with 0) with int dtype, `histogram()`
    # returns ``bin_centers`` containing only one value. Speed up with it.
    if bin_centers.size == 1:
        return bin_centers[0]

    # Calculate probability mass function
    pmf = counts.astype(cp.float32) / counts.sum()
    P1 = cp.cumsum(pmf)  # Cumulative normalized histogram
    P1_sq = cp.cumsum(pmf * pmf)
    # Get cumsum calculated from end of squared array:
    P2_sq = cp.cumsum(pmf[::-1]**2)[::-1]
    # P2_sq indexes is shifted +1. I assume, with P1[:-1] it's help avoid
    # '-inf' in crit. ImageJ Yen implementation replaces those values by zero.
    crit = cp.log(
        ((P1_sq[:-1] * P2_sq[1:])**-1) * (P1[:-1] * (1.0 - P1[:-1]))**2)
    return bin_centers[crit.argmax()]
コード例 #8
0
ファイル: clist_sort.py プロジェクト: Shirui816/yamdsp
 def update(self):
     with cuda.gpus[self.gpu]:
         self.d_cell_counts.fill(0)
         self.cu_cell_list[self.bpg_part,
                           self.tpb](self.system.d_x, self.system.d_box,
                                     self.d_ibox, self.d_cells,
                                     self.d_cell_counts)
         cupy.cumsum(self.d_cell_counts, out=self.d_cell_counts)
         self.d_cell_list = cupy.argsort(self.d_cells)
コード例 #9
0
    def moving_mean_std_gpu(a, w):
        s = cp.concatenate([cp.array([0]), cp.cumsum(a)])
        sSq = cp.concatenate([cp.array([0]), cp.cumsum(a**2)])
        segSum = s[w:] - s[:-w]
        segSumSq = sSq[w:] - sSq[:-w]

        movmean = segSum / w
        movstd = cp.sqrt(segSumSq / w - (segSum / w)**2)

        return (movmean, movstd)
コード例 #10
0
ファイル: template.py プロジェクト: mritools/cupyimg
def _window_sum_2d(image, window_shape):

    window_sum = cp.cumsum(image, axis=0)
    window_sum = (window_sum[window_shape[0]:-1] -
                  window_sum[:-window_shape[0] - 1])

    window_sum = cp.cumsum(window_sum, axis=1)
    window_sum = (window_sum[:, window_shape[1]:-1] -
                  window_sum[:, :-window_shape[1] - 1])

    return window_sum
コード例 #11
0
 def set_attractivities(self, attractivities):
     self.square_sampling_probas = get_square_sampling_probas(attractivities, 
                                                     self.square_ids_cells, 
                                                     self.coords_squares,  
                                                     self.dscale)
     mask_eligible = cp.where(attractivities > 0)[0]  # only cells with attractivity > 0 are eligible for a move
     self.eligible_cells = self.cell_ids[mask_eligible]
     # Compute square to cell transition matrix
     self.cell_sampling_probas, self.cell_index_shift = get_cell_sampling_probas(attractivities[mask_eligible], self.square_ids_cells[mask_eligible])
     # Compute upfront cumulated sum of sampling matrices
     self.square_sampling_probas = cp.cumsum(self.square_sampling_probas, axis=1)
     self.cell_sampling_probas = cp.cumsum(self.cell_sampling_probas, axis=1)
コード例 #12
0
def _window_sum_2d(image, window_shape):

    # TODO: remove copy in line below once the following issue is resolved
    #       https://github.com/cupy/cupy/issues/4456
    window_sum = cp.cumsum(image, axis=0)
    window_sum = (window_sum[window_shape[0]:-1] -
                  window_sum[:-window_shape[0] - 1])

    window_sum = cp.cumsum(window_sum, axis=1)
    window_sum = (window_sum[:, window_shape[1]:-1] -
                  window_sum[:, :-window_shape[1] - 1])

    return window_sum
コード例 #13
0
def perform_cumsum(input_array, use_cuda):
    """
    Return an array containing the cumulative sum of the 1darray `input_array`

    (The returned array has one more element than `input_array`; its first
    element is 0 and its last element is the total sum of `input_array`)
    """
    if use_cuda:
        cumulative_array = cupy.zeros(len(input_array) + 1, dtype=cupy.int64)
        cupy.cumsum(input_array, out=cumulative_array[1:])
    else:
        cumulative_array = np.zeros(len(input_array) + 1, dtype=np.int64)
        np.cumsum(input_array, out=cumulative_array[1:])
    return (cumulative_array)
コード例 #14
0
def sum_by_group(values1, values2, groups):
    order = cp.argsort(groups)
    groups = groups[order]
    values1 = values1[order]
    values2 = values2[order]
    cp.cumsum(values1, out=values1, axis=0)
    cp.cumsum(values2, out=values2, axis=0)
    index = cp.ones(len(groups), 'bool')
    index[:-1] = groups[1:] != groups[:-1]
    values1 = values1[index]
    values2 = values2[index]
    groups = groups[index]
    values1[1:] = values1[1:] - values1[:-1]
    values2[1:] = values2[1:] - values2[:-1]
    return values1, values2, groups
コード例 #15
0
def dense2csr(a):
    if a.dtype.char in 'fdFD':
        return cusparse.dense2csr(a)
    m, n = a.shape
    a = cupy.ascontiguousarray(a)
    indptr = cupy.zeros(m + 1, dtype=numpy.int32)
    info = cupy.zeros(m * n + 1, dtype=numpy.int32)
    cupy_dense2csr_step1()(m, n, a, indptr, info)
    indptr = cupy.cumsum(indptr, dtype=numpy.int32)
    info = cupy.cumsum(info, dtype=numpy.int32)
    nnz = int(indptr[-1])
    indices = cupy.empty(nnz, dtype=numpy.int32)
    data = cupy.empty(nnz, dtype=a.dtype)
    cupy_dense2csr_step2()(m, n, a, info, indices, data)
    return csr_matrix((data, indices, indptr), shape=(m, n))
コード例 #16
0
ファイル: data_gen.py プロジェクト: bschifferer/NVTabular
 def create_cats(self, size, cats_rep, entries=False):
     """
     size = number of rows
     num_cols = how many columns you want produced
     cat_rep = a list of tuple values (cardinality, min, max) representing the cardinality,
               minimum and maximum categorical string length
     """
     # should alpha also be exposed? related to dist... should be part of that
     df = cudf.DataFrame()
     for col in cats_rep:
         # if mh resets size
         col_size = size
         offs = None
         dist = col.distro or self.dist
         if col.multi_min and col.multi_max:
             entrys_lens = dist.create_col(
                 col_size + 1, dtype=np.long, min_val=col.multi_min, max_val=col.multi_max
             ).ceil()
             # sum returns numpy dtype
             col_size = int(entrys_lens.sum())
             offs = cupy.cumsum(entrys_lens.values)
         ser = dist.create_col(
             col_size, dtype=np.long, min_val=0, max_val=col.cardinality
         ).ceil()
         if entries:
             cat_names = self.create_cat_entries(
                 col.cardinality, min_size=col.min_entry_size, max_size=col.max_entry_size
             )
             ser, _ = self.merge_cats_encoding(ser, cat_names)
         if offs is not None:
             # create multi_column from offs and ser
             ser = self.create_multihot_col(offs, ser)
         ser.name = col.name
         df = cudf.concat([df, ser], axis=1)
     return df
コード例 #17
0
def _match_cumulative_cdf(source, template):
    """
    Return modified source array so that the cumulative density function of
    its values matches the cumulative density function of the template.
    """
    src_values, src_unique_indices, src_counts = cp.unique(source.ravel(),
                                                           return_inverse=True,
                                                           return_counts=True)
    tmpl_values, tmpl_counts = cp.unique(template.ravel(), return_counts=True)

    # calculate normalized quantiles for each array
    src_quantiles = cp.cumsum(src_counts) / source.size
    tmpl_quantiles = cp.cumsum(tmpl_counts) / template.size

    interp_a_values = cp.interp(src_quantiles, tmpl_quantiles, tmpl_values)
    return interp_a_values[src_unique_indices].reshape(source.shape)
コード例 #18
0
def unwrap(p, discont=numpy.pi, axis=-1):
    """Unwrap by changing deltas between values to 2*pi complement.

    Args:
        p (cupy.ndarray): Input array.
        discont (float): Maximum discontinuity between values, default is
            ``pi``.
        axis (int): Axis along which unwrap will operate, default is the last
            axis.
    Returns:
        cupy.ndarray: The result array.

    .. seealso:: :func:`numpy.unwrap`
    """

    p = cupy.asarray(p)
    nd = p.ndim
    dd = sumprod.diff(p, axis=axis)
    slice1 = [slice(None, None)]*nd     # full slices
    slice1[axis] = slice(1, None)
    slice1 = tuple(slice1)
    ph_correct = _unwrap_correct(dd, discont)
    up = cupy.array(p, copy=True, dtype='d')
    up[slice1] = p[slice1] + cupy.cumsum(ph_correct, axis=axis)
    return up
コード例 #19
0
ファイル: dia.py プロジェクト: kmatsuura/cupy
    def tocsc(self, copy=False):
        """Converts the matrix to Compressed Sparse Column format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible. Actually this option is ignored because all
                arrays in a matrix cannot be shared in dia to csc conversion.

        Returns:
            cupy.sparse.csc_matrix: Converted matrix.

        """
        if self.data.size == 0:
            return csc.csc_matrix(self.shape, dtype=self.dtype)

        num_rows, num_cols = self.shape
        num_offsets, offset_len = self.data.shape

        row, mask = core.ElementwiseKernel(
            'int32 offset_len, int32 offsets, int32 num_rows, '
            'int32 num_cols, T data', 'int32 row, bool mask', '''
            int offset_inds = i % offset_len;
            row = offset_inds - offsets;
            mask = (row >= 0 && row < num_rows && offset_inds < num_cols
                    && data != 0);
            ''', 'dia_tocsc')(offset_len, self.offsets[:, None], num_rows,
                              num_cols, self.data)
        indptr = cupy.zeros(num_cols + 1, dtype='i')
        indptr[1:offset_len + 1] = cupy.cumsum(mask.sum(axis=0))
        indptr[offset_len + 1:] = indptr[offset_len]
        indices = row.T[mask.T].astype('i', copy=False)
        data = self.data.T[mask.T]
        return csc.csc_matrix((data, indices, indptr),
                              shape=self.shape,
                              dtype=self.dtype)
コード例 #20
0
    def test_args(self):
        dx = cp.cumsum(cp.ones(5))
        dx_uneven = [1.0, 2.0, 5.0, 9.0, 11.0]
        f_2d = cp.arange(25).reshape(5, 5)

        # distances must be scalars or have size equal to gradient[axis]
        gradient(cp.arange(5), 3.0)
        gradient(cp.arange(5), cp.array(3.0))
        gradient(cp.arange(5), dx)
        # dy is set equal to dx because scalar
        gradient(f_2d, 1.5)
        gradient(f_2d, cp.array(1.5))

        gradient(f_2d, dx_uneven, dx_uneven)
        # mix between even and uneven spaces and
        # mix between scalar and vector
        gradient(f_2d, dx, 2)

        # 2D but axis specified
        gradient(f_2d, dx, axis=1)

        # 2d coordinate arguments are not yet allowed
        assert_raises_regex(
            ValueError,
            ".*scalars or 1d",
            gradient,
            f_2d,
            cp.stack([dx] * 2, axis=-1),
            1,
        )
コード例 #21
0
ファイル: mc-cupy.py プロジェクト: AndreyKolev/MLLibMark
def pricepaths(S, tau, r, q, v, M, N):
    dt = tau / M
    g1 = (r - q - v / 2) * dt
    g2 = math.sqrt(v * dt)
    aux = math.log(S) + cp.cumsum(
        g1 + g2 * cp.random.randn(M, N, dtype=cp.float32), 0)
    return cp.exp(aux)
コード例 #22
0
    def transform(self, columns: ColumnNames, df: DataFrameType) -> DataFrameType:
        on_cpu = _is_cpu_object(df)
        ret = type(df)()
        for col in columns:
            # handle CPU via normal python slicing (not very efficient)
            if on_cpu:
                ret[col] = [row[self.start : self.end] for row in df[col]]
            else:
                # figure out the size of each row from the list offsets
                c = df[col]._column
                offsets = c.offsets.values
                elements = c.elements.values

                # figure out the size of each row after slicing start/end
                new_offsets = cp.zeros(offsets.size, dtype=offsets.dtype)
                threads = 32
                blocks = (offsets.size + threads - 1) // threads

                # calculate new row offsets after slicing
                _calculate_row_sizes[blocks, threads](self.start, self.end, offsets, new_offsets)
                new_offsets = cp.cumsum(new_offsets).astype(offsets.dtype)

                # create a new array for the sliced elements
                new_elements = cp.zeros(new_offsets[-1].item(), dtype=elements.dtype)
                if new_elements.size:
                    _slice_rows[blocks, threads](
                        self.start, offsets, elements, new_offsets, new_elements
                    )

                # build up a list column with the sliced values
                ret[col] = _build_cudf_list_column(new_elements, new_offsets)

        return ret
コード例 #23
0
ファイル: _rank_order.py プロジェクト: mritools/cupyimg
def rank_order(image):
    """Return an image of the same shape where each pixel is the
    index of the pixel value in the ascending order of the unique
    values of ``image``, aka the rank-order value.

    Parameters
    ----------
    image : ndarray

    Returns
    -------
    labels : ndarray of type np.uint32, of shape image.shape
        New array where each pixel has the rank-order value of the
        corresponding pixel in ``image``. Pixel values are between 0 and
        n - 1, where n is the number of distinct unique values in
        ``image``.
    original_values : 1-D ndarray
        Unique original values of ``image``

    Examples
    --------
    >>> a = cp.asarray([[1, 4, 5], [4, 4, 1], [5, 1, 1]])
    >>> a
    array([[1, 4, 5],
           [4, 4, 1],
           [5, 1, 1]])
    >>> rank_order(a)
    (array([[0, 1, 2],
           [1, 1, 0],
           [2, 0, 0]], dtype=uint32), array([1, 4, 5]))
    >>> b = cp.asarray([-1., 2.5, 3.1, 2.5])
    >>> rank_order(b)
    (array([0, 1, 2, 1], dtype=uint32), array([-1. ,  2.5,  3.1]))
    """
    flat_image = image.ravel()
    sort_order = flat_image.argsort().astype(cp.uint32)
    flat_image = flat_image[sort_order]
    sort_rank = cp.zeros_like(sort_order)
    is_different = flat_image[:-1] != flat_image[1:]
    cp.cumsum(is_different, out=sort_rank[1:])
    original_values = cp.zeros((sort_rank[-1].get() + 1, ), image.dtype)
    original_values[0] = flat_image[0]
    original_values[1:] = flat_image[1:][is_different]
    int_image = cp.zeros_like(sort_order)
    int_image[sort_order] = sort_rank
    return (int_image.reshape(image.shape), original_values)
コード例 #24
0
ファイル: _generator.py プロジェクト: viantirreau/cupy
 def _get_indices(self, sample, upper_limit, cond):
     dtype = numpy.uint32 if sample.size < 2**32 else numpy.uint64
     flags = (sample <= upper_limit) if cond else (sample > upper_limit)
     csum = cupy.cumsum(flags, dtype=dtype)
     del flags
     indices = cupy.empty((int(csum[-1]), ), dtype=dtype)
     self._kernel_get_indices(csum, indices, size=csum.size)
     return indices
コード例 #25
0
    def assemble_sparse(self, ke, tri, perm, n_pts, ref=0):
        '''
        function that assembles the global stiffness matrix from all element stiffness matrices

        takes:

        ke - stiffness on each element matrix - array shape (n_triangles, n_vertices, n_vertices)
        tri - array with all indices (in pts array) of triangle vertices - shape (num_triangles, 3)
        perm - array with permittivity in each element - array shape (num_triangles,)
        n_pts - number of nodes - int
        ref - electrode on which reference value is placed

        returns: 

        K - global stiffness matrix - (n_pts, n_pts)
        '''
        n_tri, n_vertices = tri.shape
        row = cp.tile(tri, (1, n_vertices))
        i = cp.array([0, 3, 6, 1, 4, 7, 2, 5, 8])
        row = row[:, i].ravel()
        col = cp.tile(tri, (n_vertices)).reshape(
            (tri.shape[0] * tri.shape[1] * n_vertices))
        admittanceMatrixC2 = self.admittanceMatrixC2()
        data = cp.multiply(ke[:], perm[:, None, None])
        indexElectrode = cp.sort(self.tri[self.twoFromElectrode][self.isValid],
                                 axis=1)[:, 0] // self.n_per_el
        data[self.twoFromElectrode][self.isValid] = (
            data[self.twoFromElectrode][self.isValid] +
            ((1 / self.z[indexElectrode]))[:, None, None] * admittanceMatrixC2)
        data = data.ravel()
        ind = cp.argsort(row)
        row = row[ind]
        col = col[ind]
        data = data[ind]
        unique, counts = cp.unique(row, return_counts=True)
        index_pointer = cp.zeros(n_pts + 1)
        sum_count = cp.cumsum(counts)
        index_pointer[unique[:] + 1] = sum_count[:]

        K = sp.csr_matrix((data, col, index_pointer),
                          shape=(n_pts, n_pts),
                          dtype=perm.dtype)

        K = K.toarray()

        A = cp.empty((self.n_pts + self.ne, self.n_pts + self.ne), dtype='f8')

        if 0 <= self.ref < n_pts:
            K[self.ref, :] = 0.
            K[:, self.ref] = 0.
            K[self.ref, self.ref] = 1.

        A[:self.n_pts, :self.n_pts] = K[:]
        admittanceMatrixE = self.admittanceMatrixE()
        A[self.n_pts:, :self.n_pts] = admittanceMatrixE.T
        A[:self.n_pts, self.n_pts:] = admittanceMatrixE
        A[self.n_pts:, self.n_pts:] = self.admittanceMatrixD()
        return A
コード例 #26
0
ファイル: coo.py プロジェクト: wotulong/cupy
    def sum_duplicates(self):
        """Eliminate duplicate matrix entries by adding them together.

        .. seealso::
           :func:`scipy.sparse.coo_matrix.sum_duplicates`

        """
        if self._has_canonical_format:
            return
        if self.data.size == 0:
            self._has_canonical_format = True
            return
        keys = cupy.stack([self.row, self.col])
        order = cupy.lexsort(keys)
        src_data = self.data[order]
        src_row = self.row[order]
        src_col = self.col[order]
        diff = cupy.ElementwiseKernel(
            'raw int32 row, raw int32 col',
            'int32 diff',
            '''
            int index;
            if (i == 0 || row[i - 1] == row[i] && col[i - 1] == col[i]) {
              diff = 0;
            } else {
              diff = 1;
            }
            ''',
            'sum_duplicates_diff'
        )(src_row, src_col, size=self.row.size)

        if diff[1:].all():
            # All elements have different indices.
            data = src_data
            row = src_row
            col = src_col
        else:
            index = cupy.cumsum(diff, dtype='i')
            size = int(index[-1]) + 1
            data = cupy.zeros(size, dtype=self.data.dtype)
            row = cupy.empty(size, dtype='i')
            col = cupy.empty(size, dtype='i')
            cupy.ElementwiseKernel(
                'T src_data, int32 src_row, int32 src_col, int32 index',
                'raw T data, raw int32 row, raw int32 col',
                '''
                atomicAdd(&data[index], src_data);
                row[index] = src_row;
                col[index] = src_col;
                ''',
                'sum_duplicates_assign',
                preamble=util._preamble_atomic_add
            )(src_data, src_row, src_col, index, data, row, col)

        self.data = data
        self.row = row
        self.col = col
        self._has_canonical_format = True
コード例 #27
0
ファイル: kernel_density.py プロジェクト: rapidsai/cuml
    def sample(self, n_samples=1, random_state=None):
        """
        Generate random samples from the model.
        Currently, this is implemented only for gaussian and tophat kernels,
        and the Euclidean metric.

        Parameters
        ----------
        n_samples : int, default=1
            Number of samples to generate.
        random_state : int, cupy RandomState instance or None, default=None

        Returns
        -------
        X : cupy array of shape (n_samples, n_features)
            List of samples.
        """
        if not hasattr(self, "X_"):
            raise NotFittedError()

        supported_kernels = ["gaussian", "tophat"]
        if (self.kernel not in supported_kernels
                or self.metric != "euclidean"):
            raise NotImplementedError(
                "Only {} kernels, and the euclidean"
                " metric are supported.".format(supported_kernels))

        if isinstance(random_state, cp.random.RandomState):
            rng = random_state
        else:
            rng = cp.random.RandomState(random_state)

        u = rng.uniform(0, 1, size=n_samples)
        if self.sample_weight_ is None:
            i = (u * self.X_.shape[0]).astype(np.int64)
        else:
            cumsum_weight = cp.cumsum(self.sample_weight_)
            sum_weight = cumsum_weight[-1]
            i = cp.searchsorted(cumsum_weight, u * sum_weight)
        if self.kernel == "gaussian":
            return cp.atleast_2d(rng.normal(self.X_[i], self.bandwidth))

        elif self.kernel == "tophat":
            # we first draw points from a d-dimensional normal distribution,
            # then use an incomplete gamma function to map them to a uniform
            # d-dimensional tophat distribution.
            has_scipy(raise_if_unavailable=True)
            dim = self.X_.shape[1]
            X = rng.normal(size=(n_samples, dim))
            s_sq = cp.einsum("ij,ij->i", X, X).get()

            # do this on the CPU becaause we don't have
            # a gammainc function  readily available
            correction = cp.array(
                gammainc(0.5 * dim, 0.5 * s_sq)**(1.0 / dim) * self.bandwidth /
                np.sqrt(s_sq))
            return self.X_[i] + X * correction[:, np.newaxis]
コード例 #28
0
ファイル: template.py プロジェクト: mritools/cupyimg
def _window_sum_3d(image, window_shape):

    window_sum = _window_sum_2d(image, window_shape)

    window_sum = cp.cumsum(window_sum, axis=2)
    window_sum = (window_sum[:, :, window_shape[2]:-1] -
                  window_sum[:, :, :-window_shape[2] - 1])

    return window_sum
コード例 #29
0
def cumulative_distribution(data, bins):
    assert cup.min(data) >= 0.0 and cup.max(data) <= 1.0
    hg_av, hg_a = cup.unique(cup.floor(data * (bins - 1)), return_index=True)
    hg_a = cup.float32(hg_a)
    hgs = cup.sum(hg_a)
    hg_a /= hgs
    res = cup.zeros((bins, ))
    res[cup.int64(hg_av)] = hg_a
    return cup.cumsum(res)
コード例 #30
0
ファイル: csr.py プロジェクト: Dahlia-Chehata/cupy
def multiply_by_csr(a, b):
    a_m, a_n = a.shape
    b_m, b_n = b.shape
    if not (a_m == b_m or a_m == 1 or b_m == 1):
        raise ValueError('inconsistent shape')
    if not (a_n == b_n or a_n == 1 or b_n == 1):
        raise ValueError('inconsistent shape')
    m, n = max(a_m, b_m), max(a_n, b_n)
    a_nnz = a.nnz * (m // a_m) * (n // a_n)
    b_nnz = b.nnz * (m // b_m) * (n // b_n)
    if a_nnz > b_nnz:
        return multiply_by_csr(b, a)
    c_nnz = a_nnz
    dtype = numpy.promote_types(a.dtype, b.dtype)
    c_data = cupy.empty(c_nnz, dtype=dtype)
    c_indices = cupy.empty(c_nnz, dtype=a.indices.dtype)
    if m > a_m:
        if n > a_n:
            c_indptr = cupy.arange(0, c_nnz+1, n, dtype=a.indptr.dtype)
        else:
            c_indptr = cupy.arange(0, c_nnz+1, a.nnz, dtype=a.indptr.dtype)
    else:
        c_indptr = a.indptr.copy()
        if n > a_n:
            c_indptr *= n
    flags = cupy.zeros(c_nnz+1, dtype=a.indices.dtype)
    nnz_each_row = cupy.zeros(m+1, dtype=a.indptr.dtype)

    # compute c = a * b where necessary and get sparsity pattern of matrix d
    cupy_multiply_by_csr_step1()(
        a.data, a.indptr, a.indices, a_m, a_n,
        b.data, b.indptr, b.indices, b_m, b_n,
        c_indptr, m, n, c_data, c_indices, flags, nnz_each_row)

    flags = cupy.cumsum(flags, dtype=a.indptr.dtype)
    d_indptr = cupy.cumsum(nnz_each_row, dtype=a.indptr.dtype)
    d_nnz = int(d_indptr[-1])
    d_data = cupy.empty(d_nnz, dtype=dtype)
    d_indices = cupy.empty(d_nnz, dtype=a.indices.dtype)

    # remove zero elements in matric c
    cupy_multiply_by_csr_step2()(c_data, c_indices, flags, d_data, d_indices)

    return csr_matrix((d_data, d_indices, d_indptr), shape=(m, n))