Beispiel #1
0
def _masked_column_median(arr, masked_value):
    """Compute the median of each column in the 2D array arr, ignoring any
    instances of masked_value"""
    mask = _get_mask(arr, masked_value)
    if arr.size == 0:
        return cp.full(arr.shape[1], cp.nan)
    arr_sorted = arr.copy()
    if not cp.isnan(masked_value):
        # If nan is not the missing value, any column with nans should
        # have a median of nan
        nan_cols = cp.any(cp.isnan(arr), axis=0)
        arr_sorted[mask] = cp.nan
    else:
        nan_cols = cp.full(arr.shape[1], False)
    # nans are always sorted to end of array
    arr_sorted = cp.sort(arr_sorted, axis=0)

    count_missing_values = mask.sum(axis=0)
    # Ignore missing values in determining "halfway" index of sorted
    # array
    n_elems = arr.shape[0] - count_missing_values

    # If no elements remain after removing missing value, median for
    # that colum is nan
    nan_cols = cp.logical_or(nan_cols, n_elems <= 0)

    col_index = cp.arange(arr_sorted.shape[1])
    median = (arr_sorted[cp.floor_divide(n_elems - 1, 2), col_index] +
              arr_sorted[cp.floor_divide(n_elems, 2), col_index]) / 2

    median[nan_cols] = cp.nan
    return median
Beispiel #2
0
def my_sum(S1, sig, varargin=None):
    # returns a running sum applied sequentially across a choice of dimensions and bin sizes
    # S1 is the matrix to be filtered
    # sig is either a scalar or a sequence of scalars, one for each axis to be filtered.
    #  it's the plus/minus bin length for the summing filter
    # varargin can be the dimensions to do filtering, if len(sig) != x.shape
    # if sig is scalar and no axes are provided, the default axis is 2
    idims = 1
    if varargin is not None:
        idims = varargin
    idims = _make_vect(idims)
    if _is_vect(idims) and _is_vect(sig):
        sigall = sig
    else:
        sigall = np.tile(sig, len(idims))

    for sig, idim in zip(sigall, idims):
        Nd = S1.ndim
        S1 = cp.transpose(S1, [idim] + list(range(0, idim)) +
                          list(range(idim + 1, Nd)))
        dsnew = S1.shape
        S1 = cp.reshape(S1, (S1.shape[0], -1), order='F')
        dsnew2 = S1.shape
        S1 = cp.concatenate((cp.full(
            (sig, dsnew2[1]), 0), S1, cp.full((sig, dsnew2[1]), 0)),
                            axis=0)
        Smax = S1[:dsnew2[0], :]
        for j in range(1, 2 * sig + 1):
            Smax = Smax + S1[j:j + dsnew2[0], :]
        S1 = cp.reshape(Smax, dsnew, order='F')
        S1 = cp.transpose(
            S1,
            list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd)))
    return S1
def _compute_bispectrum(kind, kn, kcoords, nsamples, sample_thresh, ndim, dim,
                        shape, double, progress, exclude, blocksize,
                        compute_point, *ffts):
    knyq = max(shape) // 2
    shape = [cp.int16(Ni) for Ni in shape]
    if double:
        float, complex = cp.float64, cp.complex128
    else:
        float, complex = cp.float32, cp.complex64
    mempool = cp.get_default_memory_pool()
    pinned_mempool = cp.get_default_pinned_memory_pool()
    bispec = cp.full((dim, dim), cp.nan + 1.j * cp.nan, dtype=complex)
    binorm = cp.full((dim, dim), cp.nan, dtype=float)
    omega = np.zeros((dim, dim), dtype=np.int64)
    counts = cp.zeros((dim, dim), dtype=cp.int64)
    for i in range(dim):
        k1 = kn[i]
        k1ind = kind[i]
        nk1 = k1ind.size
        for j in range(i + 1):
            k2 = kn[j]
            if exclude and k1 + k2 > knyq:
                continue
            k2ind = kind[j]
            nk2 = k2ind.size
            nsamp = nsamples[i, j]
            nsamp = int(nsamp) if type(nsamp) is np.int64 \
                else max(int(nsamp*nk1*nk2), 1)
            if nsamp < nk1 * nk2 or nsamp > sample_thresh:
                samp = cp.random.randint(0,
                                         nk1 * nk2,
                                         size=nsamp,
                                         dtype=cp.int64)
                count = nsamp
            else:
                samp = cp.arange(nk1 * nk2, dtype=cp.int64)
                count = nk1 * nk2
            tpb = blocksize
            bpg = (count + (tpb - 1)) // tpb
            bispecbuf = cp.zeros(count, dtype=complex)
            binormbuf = cp.zeros(count, dtype=float)
            countbuf = cp.zeros(count, dtype=cp.int16)
            compute_point(
                (bpg, ), (tpb, ),
                (k1ind, k2ind, *kcoords, cp.int64(nk1), cp.int64(nk2), *shape,
                 samp, cp.int64(count), bispecbuf, binormbuf, countbuf, *ffts))
            N = countbuf.sum()
            value = bispecbuf.sum()
            norm = binormbuf.sum()
            bispec[i, j], bispec[j, i] = value, value
            binorm[i, j], binorm[j, i] = norm, norm
            omega[i, j], omega[j, i] = nk1 * nk2, nk1 * nk2
            counts[i, j], counts[j, i] = N, N
            del bispecbuf, binormbuf, countbuf, samp
            mempool.free_all_blocks()
            pinned_mempool.free_all_blocks()
        if progress:
            _printProgressBar(i, dim - 1)

    return bispec.get(), binorm.get(), omega, counts.get()
Beispiel #4
0
def ts_rank(x, window):
    if window > len(x):
        return cp.full(len(x), cp.nan)

    # 只排名了,没有除总数形成分位
    def _rank(x):

        x = cp.where(cp.isinf(x) | cp.isnan(x), cp.inf, x)

        # 此方式,只一次排序,对于长数组更有优势
        temp = x.argsort()
        ranks = cp.empty_like(temp)
        ranks[temp] = cp.arange(len(x))
        ranks = ranks.astype(float)

        # 将nan和inf的位置恢复为nan,不参与rank

        ranks = cp.where(cp.isinf(x), cp.nan, ranks)
        #ranks[cp.isinf(x)] = cp.nan
        # 返回x的最后一个元素的排名
        return (ranks + 1)[-1]

    x_rolling_array = cp_rolling_window(x, window)
    pre_fix = cp.full(window - 1, cp.nan)

    result = cp.zeros(x_rolling_array.shape[0])
    for i in range(x_rolling_array.shape[0]):
        result[i] = _rank(x_rolling_array[i])

    return cp.concatenate((pre_fix, result))
Beispiel #5
0
    def hit(self, r: RayList, t_min: float, t_max: Union[float, cp.ndarray]) \
            -> HitRecordList:
        if isinstance(t_max, (int, float, cp.floating)):
            t_max_list = cp.full(len(r), t_max, cp.float32)
        else:
            t_max_list = t_max

        oc: Vec3List = r.origin() - self.center
        a: cp.ndarray = r.direction().length_squared()
        half_b: cp.ndarray = oc @ r.direction()
        c: cp.ndarray = oc.length_squared() - self.radius**2
        discriminant_list: cp.ndarray = half_b**2 - a*c

        discriminant_condition = discriminant_list > 0
        if not discriminant_condition.any():
            return HitRecordList.new(len(r)).set_compress_info(None)

        # Calculate t
        positive_discriminant_list = (
            discriminant_list * discriminant_condition
        )
        root = cp.sqrt(positive_discriminant_list)
        non_zero_a = a - (a == 0)
        t_0 = (-half_b - root) / non_zero_a
        t_1 = (-half_b + root) / non_zero_a

        # Choose t
        t_0_condition = (
            (t_min < t_0) & (t_0 < t_max_list) & discriminant_condition
        )
        t_1_condition = (
            (t_min < t_1) & (t_1 < t_max_list)
            & (~t_0_condition) & discriminant_condition
        )
        t = cp.where(t_0_condition, t_0, 0)
        t = cp.where(t_1_condition, t_1, t)

        # Compression
        condition = t > 0
        full_rate = condition.sum() / len(t)
        if full_rate > 0.5:
            idx = None
        else:
            idx = cp.where(condition)[0]
            t = t[idx]
            r = RayList(
                Vec3List(r.orig.get_ndarray(idx)),
                Vec3List(r.dir.get_ndarray(idx))
            )

        # Wrap up result
        point = r.at(t)
        outward_normal = (point - self.center) / self.radius

        result = HitRecordList(
            point, t, cp.full(len(r), self.material.idx, dtype=cp.int32)
        ).set_face_normal(r, outward_normal).set_compress_info(idx)

        return result
Beispiel #6
0
    def __make_velocity_density_field(self, wall_area, sound_speed, density):
        density_arr = cp.full((self.width, self.height),
                              density["air"], dtype=cp.float32)
        density_arr[wall_area] = density["acrylic"]
        velocity_arr = cp.full((self.width, self.height),
                               sound_speed["air"], dtype=cp.float32)
        velocity_arr[wall_area] = sound_speed["acrylic"]

        return velocity_arr, density_arr
Beispiel #7
0
 def make_labels(self, D):
     if self.device >= 0:
         for d in D:
             self.label_true.append(cp.full(d.shape, 1, dtype=cp.float32))
             self.label_false.append(cp.full(d.shape, 0, dtype=cp.float32))
     else:
         for d in D:
             self.label_true = np.full(d.shape, 1, dtype=np.float32)
             self.label_false = np.full(d.shape, 0, dtype=np.float32)
Beispiel #8
0
def ts_sma(x, window):
    if window > len(x):
        return cp.full(len(x), cp.nan)
    pre_fix = cp.full(window - 1, cp.nan)

    x = cp.where(cp.isinf(x), cp.nan, x)
    rolling_array = cp_rolling_window(x, window)

    result = cp.mean(rolling_array, axis=1)
    return cp.concatenate((pre_fix, result))
Beispiel #9
0
    def test_triangular_for_invalid_params(self, param_dtype):
        left = cupy.full(self.left_shape, 1, dtype=param_dtype)
        mode = cupy.full(self.mode_shape, 0, dtype=param_dtype)
        right = cupy.full(self.right_shape, 2, dtype=param_dtype)
        with self.assertRaises(ValueError):
            _distributions.triangular(left,
                                      mode,
                                      right,
                                      size=self.shape,
                                      dtype=self.dtype)

        left = cupy.full(self.left_shape, -2, dtype=param_dtype)
        mode = cupy.full(self.mode_shape, 0, dtype=param_dtype)
        right = cupy.full(self.right_shape, -1, dtype=param_dtype)
        with self.assertRaises(ValueError):
            _distributions.triangular(left,
                                      mode,
                                      right,
                                      size=self.shape,
                                      dtype=self.dtype)

        left = cupy.full(self.left_shape, 0, dtype=param_dtype)
        mode = cupy.full(self.mode_shape, 0, dtype=param_dtype)
        right = cupy.full(self.right_shape, 0, dtype=param_dtype)
        with self.assertRaises(ValueError):
            _distributions.triangular(left,
                                      mode,
                                      right,
                                      size=self.shape,
                                      dtype=self.dtype)
Beispiel #10
0
    def test_noncentral_chisquare_for_invalid_params(self, param_dtype):
        df = cupy.full(self.df_shape, -1, dtype=param_dtype)
        nonc = cupy.full(self.nonc_shape, 1, dtype=param_dtype)
        with pytest.raises(ValueError):
            _distributions.noncentral_chisquare(
                df, nonc, size=self.shape, dtype=self.dtype)

        df = cupy.full(self.df_shape, 1, dtype=param_dtype)
        nonc = cupy.full(self.nonc_shape, -1, dtype=param_dtype)
        with pytest.raises(ValueError):
            _distributions.noncentral_chisquare(
                df, nonc, size=self.shape, dtype=self.dtype)
Beispiel #11
0
def ts_max(x, window):
    if window > len(x):
        return cp.full(len(x), cp.nan)
    # 把空值填充为-inf
    x = cp.where(cp.isinf(x) | cp.isnan(x), -cp.inf, x)

    prefix = cp.full(window - 1, cp.nan)
    x_rolling_array = cp_rolling_window(x, window)
    result = cp.max(x_rolling_array, axis=1)
    # 结果中如果存在-inf,说明此window均为空,返回nan
    result = result.astype(float)
    result = cp.where(cp.isinf(result), cp.nan, result)
    return cp.concatenate((prefix, result))
Beispiel #12
0
def delay(x, period):
    # window是>
    if abs(period) >= len(x):
        return cp.full(len(x), cp.nan)

    #period 和 window的区别,这里不再减1
    prefix = cp.full(abs(period), cp.nan)
    # 只有float才能加cp.nan, 否则int就会变成-2147483648
    result = cp.roll(x, period).astype(float)
    if period >= 0:
        result[:period] = prefix
    else:
        result[period:] = prefix
    return result
Beispiel #13
0
def ts_covariance(x, y, window):
    if x.shape[0] != y.shape[0]:
        raise ValueError("The length of x and y must be the same")
    if window > len(x):
        return cp.full(len(x), cp.nan)

    pre_fix = cp.full(window - 1, cp.nan)
    x_rolling_array = cp_rolling_window(x, window)
    y_rolling_array = cp_rolling_window(y, window)

    result = cp.zeros(x_rolling_array.shape[0])
    for i in range(x_rolling_array.shape[0]):
        result[i] = covariance(x_rolling_array[i], y_rolling_array[i])
    return cp.concatenate((pre_fix, result))
Beispiel #14
0
 def __setitem__(self, key, value):
     if len(self) == 0:
         self._hashtable_keys = cp.full(key.shape[0] + 1,
                                        _EMPTY_KEY,
                                        dtype=cp.int32)
         self._hashtable_values = cp.full(key.shape[0] + 1,
                                          self.default,
                                          dtype=value.dtype)
     else:
         raise NotImplementedError(
             'Trying to update CudaDict, not yet supported')
     cuda_hashtable_insert[self.tpb,
                           self.bpg](key, value, self._hashtable_keys,
                                     self._hashtable_values)
Beispiel #15
0
    def initialize_sparse_weights(self,
                                  density,
                                  init_method='Xavier',
                                  bias_constant=None):
        """ This initializes all weights, I might add a module to initialize based on a list of
        values(normal SDs), one for each layer, but for now I am using Xavier initialization for everything."""
        print('Initalizing sparse weights')
        for i in range(len(self._layers)):
            if i == 0:
                shape = (self._layers[i]._size, self._input_size)
            else:
                shape = (self._layers[i]._size, self._layers[i - 1]._size)
            if init_method == 'Xavier':
                self._layers[i]._weights = csr_matrix(
                    scipy.sparse.random(shape[0],
                                        shape[1],
                                        density=density,
                                        format='csr',
                                        dtype=np.float32,
                                        data_rvs=np.random.randn) *
                    np.sqrt(3. / sum(shape)))
                self._layers[i]._biases = cp.full(self._layers[i]._size,
                                                  bias_constant)
            # This is just rescaling the initialization by the density.
            if init_method == 'Xavier_2':
                self._layers[i]._weights = csr_matrix(
                    scipy.sparse.random(shape[0],
                                        shape[1],
                                        density=density,
                                        format='csr',
                                        dtype=np.float32,
                                        data_rvs=np.random.randn) *
                    np.sqrt(3. / sum(shape)) / density)
                self._layers[i]._biases = cp.full(self._layers[i]._size,
                                                  bias_constant)
            if type(init_method) == float:
                self._layers[i]._weights = csr_matrix(
                    scipy.sparse.random(shape[0],
                                        shape[1],
                                        density=density,
                                        format='csr',
                                        dtype=np.float32,
                                        data_rvs=np.random.randn) *
                    init_method)
                self._layers[i]._biases = cp.full(self._layers[i]._size,
                                                  bias_constant)

        for layer in self.layers:
            layer.get_coordinates()
        return
Beispiel #16
0
def ts_argmin(x, window):
    if window > len(x):
        return cp.full(len(x), cp.nan)
    # 将nan及-inf填充为inf
    x = cp.where(cp.isinf(x) | cp.isnan(x), cp.inf, x)

    prefix = cp.full(window - 1, cp.nan)
    x_rolling_array = cp_rolling_window(x, window)
    result = cp.argmin(x_rolling_array, axis=1)

    # 找到window中全为-inf的情况,填充为nan
    result = result.astype(float)
    result = cp.where(cp.isinf(x_rolling_array).all(axis=1), cp.nan, result)
    result += 1
    return cp.concatenate((prefix, result))
Beispiel #17
0
def test_normalization():
    """Test that `match_template` gives the correct normalization.

    Normalization gives 1 for a perfect match and -1 for an inverted-match.
    This test adds positive and negative squares to a zero-array and matches
    the array with a positive template.
    """
    n = 5
    N = 20
    ipos, jpos = (2, 3)
    ineg, jneg = (12, 11)
    image = cp.full((N, N), 0.5)
    image[ipos:ipos + n, jpos:jpos + n] = 1
    image[ineg:ineg + n, jneg:jneg + n] = 0

    # white square with a black border
    template = cp.zeros((n + 2, n + 2))
    template[1:1 + n, 1:1 + n] = 1

    result = match_template(image, template)

    # get the max and min results.
    sorted_result = cp.argsort(result.ravel())
    iflat_min = cp.asnumpy(sorted_result[0])
    iflat_max = cp.asnumpy(sorted_result[-1])
    min_result = np.unravel_index(iflat_min, result.shape)
    max_result = np.unravel_index(iflat_max, result.shape)

    # shift result by 1 because of template border
    assert np.all((np.array(min_result) + 1) == (ineg, jneg))
    assert np.all((np.array(max_result) + 1) == (ipos, jpos))

    assert cp.allclose(result.ravel()[iflat_min], -1)
    assert cp.allclose(result.ravel()[iflat_max], 1)
Beispiel #18
0
def _prepend_const(narray, pad_amount, value, axis=-1):
    if pad_amount == 0:
        return narray
    padshape = tuple([x if i != axis else pad_amount
                      for i, x in enumerate(narray.shape)])
    return cupy.concatenate((cupy.full(padshape, value, narray.dtype),
                             narray), axis=axis)
Beispiel #19
0
def _append_reflect(narray, pad_amount, value, axis=-1):
    if pad_amount == 0:
        return narray
    padshape = tuple(x if i != axis else pad_amount
                     for i, x in enumerate(narray.shape))
    return cupy.concatenate((narray, cupy.full(padshape, value, narray.dtype)),
                            axis=axis)
Beispiel #20
0
    def _validate_n_bins(self, n_features):
        """Returns n_bins_, the number of bins per feature.
        """
        orig_bins = self.n_bins
        if isinstance(orig_bins, numbers.Number):
            if not isinstance(orig_bins, numbers.Integral):
                raise ValueError("{} received an invalid n_bins type. "
                                 "Received {}, expected int."
                                 .format(KBinsDiscretizer.__name__,
                                         type(orig_bins).__name__))
            if orig_bins < 2:
                raise ValueError("{} received an invalid number "
                                 "of bins. Received {}, expected at least 2."
                                 .format(KBinsDiscretizer.__name__, orig_bins))
            return np.full(n_features, orig_bins, dtype=int)

        n_bins = check_array(orig_bins, dtype=np.int, copy=True,
                             ensure_2d=False)

        if n_bins.ndim > 1 or n_bins.shape[0] != n_features:
            raise ValueError("n_bins must be a scalar or array "
                             "of shape (n_features,).")

        bad_nbins_value = (n_bins < 2) | (n_bins != orig_bins)

        violating_indices = np.where(bad_nbins_value)[0]
        if violating_indices.shape[0] > 0:
            indices = ", ".join(str(i) for i in violating_indices)
            raise ValueError("{} received an invalid number "
                             "of bins at indices {}. Number of bins "
                             "must be at least 2, and must be an int."
                             .format(KBinsDiscretizer.__name__, indices))
        return n_bins
Beispiel #21
0
 def __init__(self, w, s):
     self.s = s
     self.w = w
     self.W = cp.random.normal(0, 0.1, w * s).astype(DT).reshape(s, w)
     self.b = cp.full((s, 1), 0.5, dtype=DT)
     self.dW = cp.zeros((self.s, self.w), dtype=DT)
     self.db = cp.zeros((self.s, 1), dtype=DT)
Beispiel #22
0
    def __init__(self,
                 state_size,
                 state_transfer,
                 measurement_size,
                 measurement_transfer,
                 time_step,
                 sigma_spread,
                 difference=lambda x, y: x - y):

        self.x_hat = np.zeros(state_size)
        self.Nx = state_size
        self.P = np.eye(state_size)
        self.F = state_transfer
        self.Nz = measurement_size
        self.R = np.eye(measurement_size)
        self.H = measurement_transfer
        self.Q = np.eye(state_size)
        self.kappa = sigma_spread
        self.weights = np.full(2 * state_size + 1,
                               .5 / (state_size + sigma_spread))
        self.weights[0] = sigma_spread / (state_size + sigma_spread)
        self.difference = difference
        self.num_sp = 2 * state_size + 1
        self.sp = np.zeros((self.num_sp, self.Nx))
        self.sp_f = np.zeros((self.num_sp, self.Nx))
        self.sp_h = np.zeros((self.num_sp, self.Nz))
        self.dt = time_step
Beispiel #23
0
def decay_linear(x, window):
    if window > len(x):
        return cp.full(len(x), cp.nan)
    prefix = cp.full(window - 1, cp.nan)

    x = cp.where(cp.isinf(x), cp.nan, x)

    x_rolling_array = cp_rolling_window(x, window)
    # 等差数列求和公式
    denominator = (window + 1) * window / 2
    # window各元素权重
    weight = (cp.arange(window) + 1) * 1.0 / denominator
    result = cp.zeros(x_rolling_array.shape[0])
    for i in range(x_rolling_array.shape[0]):
        result[i] = x_rolling_array[i].dot(weight)
    return cp.concatenate((prefix, result))
 def get_W_matrix(self):
     bias = self.w[0]
     W = cp.full(self.filter_shape, cp.nan)
     for row_number, i in enumerate(
             range(1, len(self.w), self.filter_shape[1])):
         W[row_number, :] = self.w[i:i + self.filter_shape[1]]
     return W, bias
Beispiel #25
0
 def _assign_uniform_weight(input_graph: cugraph.Graph,
                            default_value: Any) -> cugraph.Graph:
     g = cugraph.DiGraph() if input_graph.is_directed() else cugraph.Graph()
     if input_graph.edgelist is not None:
         # TODO avoid copying the 'src' and 'dst' columns twice
         gdf = input_graph.view_edge_list().copy()
         gdf["weight"] = cupy.full(len(gdf), default_value)
         g.from_cudf_edgelist(gdf,
                              source="src",
                              destination="dst",
                              edge_attr="weight")
     else:
         offset_col, index_col, _ = g.view_adj_list()
         value_col = cudf.Series(cupy.full(len(offset_col), default_value))
         g.from_cudf_adjlist(offset_col, index_col, value_col)
     return g
Beispiel #26
0
def inject_error(weight, mask0, mask1, num_bits=32):
    if num_bits == 32:
        dtype = cp.uint32
        ftype = cp.float32
    shape = weight.shape
    weight_flatten = cp.ravel(weight).view(dtype)
    mask0, mask0_bit = mask0
    mask1, mask1_bit = mask1
    zero = cp.zeros(1, dtype=dtype)

    if (mask0.__len__() is not 0) or (mask1.__len__() is not 0):
        for b in range(num_bits):
            fault = cp.full(weight_flatten.size, 2**b, dtype=dtype)
            bit_loc0 = cp.where(mask0_bit == b, mask0, zero).nonzero()[0]
            bit_loc1 = cp.where(mask1_bit == b, mask1, zero).nonzero()[0]
            uniform0 = cp.zeros(weight_flatten.size, dtype=dtype)
            uniform1 = cp.zeros(weight_flatten.size, dtype=dtype)
            # Inject bit error
            if bit_loc0.__len__() > 0:
                cp.put(uniform0, mask0[bit_loc0], fault)
                cp.put(uniform1, mask1[bit_loc1], fault)
                # Stuck at 0
                not_mask0 = cp.invert(uniform0)
                weight_flatten = cp.bitwise_and(weight_flatten, not_mask0)
                # Stuck at 1
                weight_flatten = cp.bitwise_or(weight_flatten, uniform1)
        weight_float = weight_flatten.view(ftype)
        return cp.reshape(weight_float, shape)
    else:
        return weight
Beispiel #27
0
def _prepend_const(narray, pad_amount, value, axis=-1):
    if pad_amount == 0:
        return narray
    padshape = tuple(x if i != axis else pad_amount
                     for i, x in enumerate(narray.shape))
    return cupy.concatenate((cupy.full(padshape, value, narray.dtype),
                             narray), axis=axis)
Beispiel #28
0
def ts_correlation(x, y, window):
    if x.shape[0] != y.shape[0]:
        raise ValueError("The length of x and y must be the same")
        # window是>
    if window > len(x):
        return cp.full(len(x), cp.nan)

    pre_fix = cp.full(window - 1, cp.nan)
    x_array = cp_rolling_window(x, window)
    y_array = cp_rolling_window(y, window)

    # 这里没有使用reuslt = [], 考虑并行计算append可能会导致结果乱序
    result = cp.zeros(x_array.shape[0])
    for i in range(x_array.shape[0]):
        result[i] = correlation(x_array[i], y_array[i])
    return cp.concatenate((pre_fix, result))
Beispiel #29
0
 def test(self):
     # Elementwise
     a = cupy.full((1, self.size), 7, dtype=cupy.int8)
     # Reduction
     result = a.sum(axis=0, dtype=cupy.int8)
     # Explicitly specify the dtype to absorb Linux/Windows difference.
     assert result.sum(dtype=cupy.int64) == self.size * 7
Beispiel #30
0
def full(
    shape: Union[int, Tuple[int, ...]],
    fill_value: Union[int, float],
    *,
    dtype: Optional[Dtype] = None,
    device: Optional[Device] = None,
) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.full <numpy.full>`.

    See its docstring for more information.
    """
    from ._array_object import Array

    _check_valid_dtype(dtype)
    if device is not None and not isinstance(device, _Device):
        raise ValueError(f"Unsupported device {device!r}")
    if device is None:
        device = _Device()  # current device
    if isinstance(fill_value, Array) and fill_value.ndim == 0:
        fill_value = fill_value._array
    prev_device = runtime.getDevice()
    try:
        runtime.setDevice(device.id)
        res = np.full(shape, fill_value, dtype=dtype)
    finally:
        runtime.setDevice(prev_device)
    if res.dtype not in _all_dtypes:
        # This will happen if the fill value is not something that NumPy
        # coerces to one of the acceptable dtypes.
        raise TypeError("Invalid input to full")
    return Array._new(res)
Beispiel #31
0
def resize(a, new_shape):
    """Return a new array with the specified shape.

    If the new array is larger than the original array, then the new
    array is filled with repeated copies of ``a``.  Note that this behavior
    is different from a.resize(new_shape) which fills with zeros instead
    of repeated copies of ``a``.

    Args:
        a (array_like): Array to be resized.
        new_shape (int or tuple of int): Shape of resized array.

    Returns:
        cupy.ndarray:
            The new array is formed from the data in the old array, repeated
            if necessary to fill out the required number of elements.  The
            data are repeated in the order that they are stored in memory.

    .. seealso:: :func:`numpy.resize`
    """
    if numpy.isscalar(a):
        return cupy.full(new_shape, a)
    a = cupy.asarray(a)
    if a.size == 0:
        return cupy.zeros(new_shape, dtype=a.dtype)
    out = cupy.empty(new_shape, a.dtype)
    _resize_kernel(a, a.size, out)
    return out
Beispiel #32
0
def full(shape, fill_value, dtype=numpy.float32, stream=None):
    """Creates a constant-filled cupy.ndarray object.

    Args:
        shape (tuple of ints): The shape of array.
        fill_value: Constant to fill the array by.
        dtype (numpy.dtype): Element type.
        stream (cupy.cuda.Stream): CUDA stream.

    Returns:
        cupy.ndarray: Constant-filled GPU array allocated by the memory pool.

    """
    warnings.warn("chainer.cuda.full is deprecated. Use cupy.full instead.", DeprecationWarning)
    check_cuda_available()
    assert stream is None
    return cupy.full(shape, fill_value, dtype=dtype)
Beispiel #33
0
def full_like(array, fill_value, stream=None):
    """Creates a constant-filled cupy.ndarray object like the given array.

    Args:
        array (cupy.ndarray or numpy.ndarray): Base array.
        fill_value: Constant value to fill the array by.
        stream (cupy.cuda.Stream): CUDA stream.

    Returns:
        cupy.ndarray: Constant-filled array.

    """
    warnings.warn("chainer.cuda.full_like is deprecated. Use cupy.full_like instead.", DeprecationWarning)
    check_cuda_available()
    assert stream is None
    if isinstance(array, cupy.ndarray):
        return cupy.full_like(array, fill_value)
    return cupy.full(array.shape, fill_value, dtype=array.dtype)