Esempio n. 1
0
 def populate_new_shape(i, j, new_shape, shuffle_shape):
     if i in augment:
         new_shape = tuple_setitem(new_shape, i, 1)
         return j, new_shape
     else:
         new_shape = tuple_setitem(new_shape, i, shuffle_shape[j])
         return j + 1, new_shape
Esempio n. 2
0
    def impl(data, wavelet, mode, axis):
        coeff_len = dwt_coeff_length(
            data.shape[axis], len(
                wavelet.dec_hi), mode)
        out_shape = tuple_setitem(data.shape, axis, coeff_len)

        if axis < 0 or axis >= data.ndim:
            raise ValueError("0 <= axis < data.ndim failed")

        ca = np.empty(out_shape, dtype=data.dtype)
        cd = np.empty(out_shape, dtype=data.dtype)

        # Iterate over all points except along the slicing axis
        for idx in np.ndindex(*tuple_setitem(data.shape, axis, 1)):
            initial_in_row = slice_axis(data, idx, axis, None)
            initial_ca_row = slice_axis(ca, idx, axis, None)
            initial_cd_row = slice_axis(cd, idx, axis, None)

            # The numba array type returned by slice_axis assumes
            # non-contiguity in the general case.
            # However, the slice may actually be contiguous in layout
            # If so, cast the array type to obtain type contiguity
            # else, copy the slice to obtain contiguity in
            # both type and layout
            if initial_in_row.flags.c_contiguous:
                in_row = force_type_contiguity(initial_in_row)
            else:
                in_row = initial_in_row.copy()

            if initial_ca_row.flags.c_contiguous:
                ca_row = force_type_contiguity(initial_ca_row)
            else:
                ca_row = initial_ca_row.copy()

            if initial_cd_row.flags.c_contiguous:
                cd_row = force_type_contiguity(initial_cd_row)
            else:
                cd_row = initial_cd_row.copy()

            # Compute the approximation and detail coefficients
            downsampling_convolution(in_row, ca_row, wavelet.dec_lo, mode, 2)
            downsampling_convolution(in_row, cd_row, wavelet.dec_hi, mode, 2)

            # If necessary, copy back into the output
            if not initial_ca_row.flags.c_contiguous:
                initial_ca_row[:] = ca_row[:]

            if not initial_cd_row.flags.c_contiguous:
                initial_cd_row[:] = cd_row[:]

        return ca, cd
Esempio n. 3
0
    def fn(A):
        for axis in range(A.ndim):
            for i in np.ndindex(*tuple_setitem(A.shape, axis, 1)):
                S = slice_axis(A, i, axis, None)

                if S.flags.c_contiguous != (S.itemsize == S.strides[0]):
                    raise ValueError("contiguity flag doesn't match layout")
Esempio n. 4
0
    def reshape(x, shape):

        new_shape = create_zeros_tuple()

        for i in numba.literal_unroll(range(ndim)):
            new_shape = tuple_setitem(new_shape, i, shape[i])

        return np.reshape(x, new_shape)
def dec(tup: Tuple[int], i: int) -> Tuple[int, ...]:  # pragma: no cover
    r"""returns a copy of the given tuple of integers where the ith element has been decreased by 1
    Args:
        tup (Tuple[int]): the given tuple
        i (int): the position of the element to be decreased
    Returns:
        Tuple[int,...]: the new tuple with the decrease on i-th element by 1
    """
    copy = tup[:]
    return tuple_setitem(copy, i, tup[i] - 1)
Esempio n. 6
0
 def impl(array, length, empty_tuple):
     out = empty_tuple
     for i in range(length):
         out = tuple_setitem(out, i, array[i])
     return out
Esempio n. 7
0
 def populate_new_shape(i, j, new_shape, shuffle_shape):
     new_shape = tuple_setitem(new_shape, i, 1)
     return j, new_shape
Esempio n. 8
0
 def foo(tup, idxs, vals):
     out_tup = tup
     for i, v in zip(idxs, vals):
         out_tup = tuple_setitem(out_tup, i, v)
     return tup, out_tup
Esempio n. 9
0
    def impl(approx_coeffs, detail_coeffs,
             wavelet, mode, axis):

        if have_approx and have_detail:
            coeff_shape = approx_coeffs.shape
            it = enumerate(zip(approx_coeffs.shape, detail_coeffs.shape))

            # NOTE(sjperkins)
            # Clip the coefficient dimensions to the smallest dimensions
            # pywt clips in waverecn and fails in idwt and idwt_axis
            # on heterogenous coefficient shapes.
            # The actual clipping is performed in slice_axis
            for i, (asize, dsize) in it:
                size = asize if asize < dsize else dsize
                coeff_shape = tuple_setitem(coeff_shape, i, size)

        elif have_approx:
            coeff_shape = approx_coeffs.shape
        elif have_detail:
            coeff_shape = detail_coeffs.shape
        else:
            raise ValueError("Either approximation or detail must be present")

        if not (0 <= axis < len(coeff_shape)):
            raise ValueError(("0 <= axis < coeff.ndim does not hold"))

        idwt_len = idwt_buffer_length(coeff_shape[axis],
                                      wavelet.rec_lo.shape[0],
                                      mode)
        out_shape = tuple_setitem(coeff_shape, axis, idwt_len)
        output = np.empty(out_shape, dtype=out_dtype)

        # Iterate over all points except along the slicing axis
        for idx in np.ndindex(*tuple_setitem(output.shape, axis, 1)):
            initial_out_row = slice_axis(output, idx, axis, None)

            # Zero if we have a contiguous slice, else allocate
            if initial_out_row.flags.c_contiguous:
                out_row = force_type_contiguity(initial_out_row)
                out_row[:] = 0
            else:
                out_row = np.zeros_like(initial_out_row)

            # Apply approximation coefficients if they exist
            if approx_coeffs is not None:
                initial_ca_row = slice_axis(approx_coeffs, idx,
                                            axis, coeff_shape[axis])

                if initial_ca_row.flags.c_contiguous:
                    ca_row = force_type_contiguity(initial_ca_row)
                else:
                    ca_row = initial_ca_row.copy()

                upsampling_convolution_valid_sf(ca_row, wavelet.rec_lo,
                                                out_row, mode)

            # Apply detail coefficients if they exist
            if detail_coeffs is not None:
                initial_cd_row = slice_axis(detail_coeffs, idx,
                                            axis, coeff_shape[axis])

                if initial_cd_row.flags.c_contiguous:
                    cd_row = force_type_contiguity(initial_cd_row)
                else:
                    cd_row = initial_cd_row.copy()

                upsampling_convolution_valid_sf(cd_row, wavelet.rec_hi,
                                                out_row, mode)

            # Copy back output row if the output space was non-contiguous
            if not initial_out_row.flags.c_contiguous:
                initial_out_row[:] = out_row

        return output
Esempio n. 10
0
def remove(pattern:tuple) -> Tuple[int, Tuple[int]]:
    for p,n in enumerate(pattern):
        copy = pattern[:]
        if n > 0:
            yield p, tuple_setitem(copy, p, pattern[p] - 1) # pylint: disable=no-value-for-parameter