Python numpy.asfortranarray() Examples

The following are 30 code examples of numpy.asfortranarray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: generate_coco_json.py    From coco-json-converter with GNU General Public License v3.0 14 votes vote down vote up
def __get_annotation__(self, mask, image=None):

        _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        segmentation = []
        for contour in contours:
            # Valid polygons have >= 6 coordinates (3 points)
            if contour.size >= 6:
                segmentation.append(contour.flatten().tolist())
        RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
        RLE = cocomask.merge(RLEs)
        # RLE = cocomask.encode(np.asfortranarray(mask))
        area = cocomask.area(RLE)
        [x, y, w, h] = cv2.boundingRect(mask)

        if image is not None:
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.drawContours(image, contours, -1, (0,255,0), 1)
            cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
            cv2.imshow("", image)
            cv2.waitKey(1)

        return segmentation, [x, y, w, h], area 
Example #2
Source File: coco.py    From dataiku-contrib with Apache License 2.0 7 votes vote down vote up
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks):
    """Arrange resutls to match COCO specs in http://cocodataset.org/#format
    """
    # If no results, return an empty list
    if rois is None:
        return []

    results = []
    for image_id in image_ids:
        # Loop through detections
        for i in range(rois.shape[0]):
            class_id = class_ids[i]
            score = scores[i]
            bbox = np.around(rois[i], 1)
            mask = masks[:, :, i]

            result = {
                "image_id": image_id,
                "category_id": dataset.get_source_class_id(class_id, "coco"),
                "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]],
                "score": score,
                "segmentation": maskUtils.encode(np.asfortranarray(mask))
            }
            results.append(result)
    return results 
Example #3
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def idz_copycols(A, k, idx):
    """
    Reconstruct skeleton matrix from complex ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    return _id.idz_copycols(A, k, idx)


#------------------------------------------------------------------------------
# idz_id2svd.f
#------------------------------------------------------------------------------ 
Example #4
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def idz_reconid(B, idx, proj):
    """
    Reconstruct matrix from complex ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    """
    B = np.asfortranarray(B)
    if proj.size > 0:
        return _id.idz_reconid(B, idx, proj)
    else:
        return B[:, np.argsort(idx)] 
Example #5
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def idzr_id(A, k):
    """
    Compute ID of a complex matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    idx, rnorms = _id.idzr_id(A, k)
    n = A.shape[1]
    proj = A.T.ravel()[:k*(n-k)].reshape((k, n-k), order='F')
    return idx, proj 
Example #6
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def iddr_id(A, k):
    """
    Compute ID of a real matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    idx, rnorms = _id.iddr_id(A, k)
    n = A.shape[1]
    proj = A.T.ravel()[:k*(n-k)].reshape((k, n-k), order='F')
    return idx, proj 
Example #7
Source File: testing.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def build_dataset(n_samples=50, n_features=200, n_targets=1, sparse_X=False):
    """Build samples and observation for linear regression problem."""
    random_state = np.random.RandomState(0)
    if n_targets > 1:
        w = random_state.randn(n_features, n_targets)
    else:
        w = random_state.randn(n_features)

    if sparse_X:
        X = sparse.random(n_samples, n_features, density=0.5, format='csc',
                          random_state=random_state)

    else:
        X = np.asfortranarray(random_state.randn(n_samples, n_features))

    y = X.dot(w)
    return X, y 
Example #8
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def idd_copycols(A, k, idx):
    """
    Reconstruct skeleton matrix from real ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    return _id.idd_copycols(A, k, idx)


#------------------------------------------------------------------------------
# idd_id2svd.f
#------------------------------------------------------------------------------ 
Example #9
Source File: _interpolative_backend.py    From lambda-packs with MIT License 6 votes vote down vote up
def idd_reconid(B, idx, proj):
    """
    Reconstruct matrix from real ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    """
    B = np.asfortranarray(B)
    if proj.size > 0:
        return _id.idd_reconid(B, idx, proj)
    else:
        return B[:, np.argsort(idx)] 
Example #10
Source File: _utils.py    From dexplo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def concat_data_arrays(data_dict: Dict[str, List[ndarray]]) -> Dict[str, ndarray]:
    new_data: Dict[str, ndarray] = {}
    empty_arrs = create_empty_arrs(data_dict)
    for kind, arrs in data_dict.items():
        if len(arrs) == 1:
            arr = arrs[0]
            if arr.ndim == 1:
                arr = arr.reshape(-1, 1)
            new_data[kind] = np.asfortranarray(arr)
        else:
            data = empty_arrs[kind]
            i = 0
            for arr in arrs:
                if arr.ndim == 1:
                    data[:, i] = arr
                    i += 1
                else:
                    for j in range(arr.shape[1]):
                        data[:, i] = arr[:, j]
                        i += 1
            new_data[kind] = data
    return new_data 
Example #11
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testCopytoExecution(self):
        a = ones((2, 3), chunk_size=1)
        b = tensor([3, -1, 3], chunk_size=2)

        copyto(a, b, where=b > 1)

        res = self.executor.execute_tensor(a, concat=True)[0]
        expected = np.array([[3, 1, 3], [3, 1, 3]])

        np.testing.assert_equal(res, expected)

        a = ones((2, 3), chunk_size=1)
        b = tensor(np.asfortranarray(np.random.rand(2, 3)), chunk_size=2)

        copyto(b, a)

        res = self.executor.execute_tensor(b, concat=True)[0]
        expected = np.asfortranarray(np.ones((2, 3)))

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example #12
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testAstypeExecution(self):
        raw = np.random.random((10, 5))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        np.testing.assert_array_equal(res[0], raw.astype('i8'))

        raw = sps.random(10, 5, density=.2)
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        self.assertTrue(np.array_equal(res[0].toarray(), raw.astype('i8').toarray()))

        raw = np.asfortranarray(np.random.random((10, 5)))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8', order='C')

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        np.testing.assert_array_equal(res, raw.astype('i8'))
        self.assertTrue(res.flags['C_CONTIGUOUS'])
        self.assertFalse(res.flags['F_CONTIGUOUS']) 
Example #13
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testArgwhereExecution(self):
        x = arange(6, chunk_size=2).reshape(2, 3)
        t = argwhere(x > 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(np.arange(6).reshape(2, 3) > 1)

        np.testing.assert_array_equal(res, expected)

        data = np.asfortranarray(np.random.rand(10, 20))
        x = tensor(data, chunk_size=10)

        t = argwhere(x > 0.5)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(data > 0.5)

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example #14
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 6 votes vote down vote up
def testCosOrderExecution(self):
        data = np.asfortranarray(np.random.rand(3, 5))
        x = tensor(data, chunk_size=2)

        t = cos(x)

        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, np.cos(data))
        self.assertFalse(res.flags['C_CONTIGUOUS'])
        self.assertTrue(res.flags['F_CONTIGUOUS'])

        t2 = cos(x, order='C')

        res2 = self.executor.execute_tensor(t2, concat=True)[0]
        np.testing.assert_allclose(res2, np.cos(data, order='C'))
        self.assertTrue(res2.flags['C_CONTIGUOUS'])
        self.assertFalse(res2.flags['F_CONTIGUOUS']) 
Example #15
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def iddp_aid(eps, A):
    """
    Compute ID of a real matrix to a specified relative precision using random
    sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    m, n = A.shape
    n2, w = idd_frmi(m)
    proj = np.empty(n*(2*n2 + 1) + n2 + 1, order='F')
    k, idx, proj = _id.iddp_aid(eps, A, w, proj)
    proj = proj[:k*(n-k)].reshape((k, n-k), order='F')
    return k, idx, proj 
Example #16
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def idzr_svd(A, k):
    """
    Compute SVD of a complex matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    U, V, S, ier = _id.idzr_svd(A, k)
    if ier:
        raise _RETCODE_ERROR
    return U, V, S 
Example #17
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def idzp_aid(eps, A):
    """
    Compute ID of a complex matrix to a specified relative precision using
    random sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    m, n = A.shape
    n2, w = idz_frmi(m)
    proj = np.empty(n*(2*n2 + 1) + n2 + 1, dtype='complex128', order='F')
    k, idx, proj = _id.idzp_aid(eps, A, w, proj)
    proj = proj[:k*(n-k)].reshape((k, n-k), order='F')
    return k, idx, proj 
Example #18
Source File: test_linalg.py    From mars with Apache License 2.0 5 votes vote down vote up
def testMatmul(self):
        t1 = tensor([[0, 1, 0], [1, 0, 0]], chunk_size=2).tosparse()
        t2 = t1.T

        t3 = matmul(t1, t2, out=empty((2, 2), dtype=t1.dtype, order='F'))
        self.assertEqual(t3.order.value, 'F')

        with self.assertRaises(TypeError):
            matmul(t1, t2, out=1)

        with self.assertRaises(TypeError):
            matmul(t1, t2, out=empty((2, 2), dtype='?'))

        with self.assertRaises(ValueError):
            matmul(t1, t2, out=empty((3, 2), dtype=t1.dtype))

        raw1 = np.asfortranarray(np.random.rand(3, 3))
        raw2 = np.asfortranarray(np.random.rand(3, 3))
        raw3 = np.random.rand(3, 3)

        self.assertEqual(matmul(tensor(raw1), tensor(raw2)).flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw2).flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw2)).flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw2).flags['F_CONTIGUOUS'])

        self.assertEqual(matmul(tensor(raw1), tensor(raw2), order='A').flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw2, order='A').flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw2), order='A').flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw2, order='A').flags['F_CONTIGUOUS'])

        self.assertEqual(matmul(tensor(raw1), tensor(raw3), order='A').flags['C_CONTIGUOUS'],
                         np.matmul(raw1, raw3, order='A').flags['C_CONTIGUOUS'])
        self.assertEqual(matmul(tensor(raw1), tensor(raw3), order='A').flags['F_CONTIGUOUS'],
                         np.matmul(raw1, raw3, order='A').flags['F_CONTIGUOUS']) 
Example #19
Source File: test_arithmetic.py    From mars with Apache License 2.0 5 votes vote down vote up
def testNegativeOrder(self):
        raw1 = np.random.rand(4, 2)
        raw2 = np.asfortranarray(np.random.rand(4, 2))
        t1 = tensor(raw1)
        t2 = tensor(raw2)
        t3 = tensor(raw1)
        t4 = tensor(raw2)

        # C
        self.assertEqual(negative(t1).flags['C_CONTIGUOUS'], np.negative(raw1).flags['C_CONTIGUOUS'])
        self.assertEqual(negative(t1).flags['F_CONTIGUOUS'], np.negative(raw1).flags['F_CONTIGUOUS'])
        # F
        self.assertEqual(negative(t2).flags['C_CONTIGUOUS'], np.negative(raw2).flags['C_CONTIGUOUS'])
        self.assertEqual(negative(t2).flags['F_CONTIGUOUS'], np.negative(raw2).flags['F_CONTIGUOUS'])
        # C + out
        self.assertEqual(negative(t1, out=t4).flags['C_CONTIGUOUS'],
                         np.negative(raw1, out=np.empty((4, 2), order='F')).flags['C_CONTIGUOUS'])
        self.assertEqual(negative(t1, out=t4).flags['F_CONTIGUOUS'],
                         np.negative(raw1, out=np.empty((4, 2), order='F')).flags['F_CONTIGUOUS'])
        # F + out
        self.assertEqual(negative(t2, out=t3).flags['C_CONTIGUOUS'],
                         np.negative(raw1, out=np.empty((4, 2), order='C')).flags['C_CONTIGUOUS'])
        self.assertEqual(negative(t2, out=t3).flags['F_CONTIGUOUS'],
                         np.negative(raw1, out=np.empty((4, 2), order='C')).flags['F_CONTIGUOUS'])

        with self.assertRaises(TypeError):
            negative(t1, order='B') 
Example #20
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def idz_estrank(eps, A):
    """
    Estimate rank of a complex matrix to a specified relative precision using
    random sampling.

    The output rank is typically about 8 higher than the actual rank.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank estimate.
    :rtype: int
    """
    A = np.asfortranarray(A)
    m, n = A.shape
    n2, w = idz_frmi(m)
    ra = np.empty(n*n2 + (n + 1)*(n2 + 1), dtype='complex128', order='F')
    k, ra = _id.idz_estrank(eps, A, w, ra)
    return k


#------------------------------------------------------------------------------
# idzp_asvd.f
#------------------------------------------------------------------------------ 
Example #21
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def idd_estrank(eps, A):
    """
    Estimate rank of a real matrix to a specified relative precision using
    random sampling.

    The output rank is typically about 8 higher than the actual rank.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank estimate.
    :rtype: int
    """
    A = np.asfortranarray(A)
    m, n = A.shape
    n2, w = idd_frmi(m)
    ra = np.empty(n*n2 + (n + 1)*(n2 + 1), order='F')
    k, ra = _id.idd_estrank(eps, A, w, ra)
    return k


#------------------------------------------------------------------------------
# iddp_asvd.f
#------------------------------------------------------------------------------ 
Example #22
Source File: test_arithmetic.py    From mars with Apache License 2.0 5 votes vote down vote up
def testFrexpOrder(self):
        raw1 = np.asfortranarray(np.random.rand(2, 4))
        t = tensor(raw1)
        o1 = tensor(np.random.rand(2, 4))

        o1, o2 = frexp(t, out1=o1)

        self.assertEqual(o1.flags['C_CONTIGUOUS'],
                         np.frexp(raw1, np.empty((2, 4)))[0].flags['C_CONTIGUOUS'])
        self.assertEqual(o1.flags['F_CONTIGUOUS'],
                         np.frexp(raw1, np.empty((2, 4)))[0].flags['F_CONTIGUOUS'])
        self.assertEqual(o2.flags['C_CONTIGUOUS'],
                         np.frexp(raw1)[1].flags['C_CONTIGUOUS'])
        self.assertEqual(o2.flags['F_CONTIGUOUS'],
                         np.frexp(raw1)[1].flags['F_CONTIGUOUS']) 
Example #23
Source File: test_arithmetic.py    From mars with Apache License 2.0 5 votes vote down vote up
def testAddOrder(self):
        raw_a = np.random.rand(4, 2)
        raw_b = np.asfortranarray(np.random.rand(4, 2))
        t1 = tensor(raw_a)
        t2 = tensor(raw_b)
        out = tensor(raw_b)

        # C + scalar
        self.assertEqual((t1 + 1).flags['C_CONTIGUOUS'], (raw_a + 1).flags['C_CONTIGUOUS'])
        self.assertEqual((t1 + 1).flags['F_CONTIGUOUS'], (raw_a + 1).flags['F_CONTIGUOUS'])
        # C + C
        self.assertEqual((t1 + t1).flags['C_CONTIGUOUS'], (raw_a + raw_a).flags['C_CONTIGUOUS'])
        self.assertEqual((t1 + t1).flags['F_CONTIGUOUS'], (raw_a + raw_a).flags['F_CONTIGUOUS'])
        # F + scalar
        self.assertEqual((t2 + 1).flags['C_CONTIGUOUS'], (raw_b + 1).flags['C_CONTIGUOUS'])
        self.assertEqual((t2 + 1).flags['F_CONTIGUOUS'], (raw_b + 1).flags['F_CONTIGUOUS'])
        # F + F
        self.assertEqual((t2 + t2).flags['C_CONTIGUOUS'], (raw_b + raw_b).flags['C_CONTIGUOUS'])
        self.assertEqual((t2 + t2).flags['F_CONTIGUOUS'], (raw_b + raw_b).flags['F_CONTIGUOUS'])
        # C + F
        self.assertEqual((t1 + t2).flags['C_CONTIGUOUS'], (raw_a + raw_b).flags['C_CONTIGUOUS'])
        self.assertEqual((t1 + t2).flags['F_CONTIGUOUS'], (raw_a + raw_b).flags['F_CONTIGUOUS'])
        # C + C + out
        self.assertEqual(add(t1, t1, out=out).flags['C_CONTIGUOUS'],
                         np.add(raw_a, raw_a, out=np.empty((4, 2), order='F')).flags['C_CONTIGUOUS'])
        self.assertEqual(add(t1, t1, out=out).flags['F_CONTIGUOUS'],
                         np.add(raw_a, raw_a, out=np.empty((4, 2), order='F')).flags['F_CONTIGUOUS'])

        with self.assertRaises(TypeError):
            add(t1, 1, order='B') 
Example #24
Source File: separator.py    From spleeter with MIT License 5 votes vote down vote up
def _stft(self, data, inverse=False, length=None):
        """
        Single entrypoint for both stft and istft. This computes stft and istft with librosa on stereo data. The two
        channels are processed separately and are concatenated together in the result. The expected input formats are:
        (n_samples, 2) for stft and (T, F, 2) for istft.
        :param data: np.array with either the waveform or the complex spectrogram depending on the parameter inverse
        :param inverse: should a stft or an istft be computed.
        :return: Stereo data as numpy array for the transform. The channels are stored in the last dimension
        """
        assert not (inverse and length is None)
        data = np.asfortranarray(data)
        N = self._params["frame_length"]
        H = self._params["frame_step"]
        win = hann(N, sym=False)
        fstft = istft if inverse else stft
        win_len_arg = {"win_length": None, "length": length} if inverse else {"n_fft": N}
        n_channels = data.shape[-1]
        out = []
        for c in range(n_channels):
            d = data[:, :, c].T if inverse else data[:, c]
            s = fstft(d, hop_length=H, window=win, center=False, **win_len_arg)
            s = np.expand_dims(s.T, 2-inverse)
            out.append(s)
        if len(out) == 1:
            return out[0]
        return np.concatenate(out, axis=2-inverse) 
Example #25
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 5 votes vote down vote up
def testClipOrderExecution(self):
        a_data = np.asfortranarray(np.random.rand(4, 8))

        a = tensor(a_data, chunk_size=3)

        b = clip(a, 0.2, 0.8)

        res = self.executor.execute_tensor(b, concat=True)[0]
        expected = np.clip(a_data, 0.2, 0.8)

        np.testing.assert_allclose(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example #26
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 5 votes vote down vote up
def testBaseOrderExecution(self):
        raw = np.asfortranarray(np.random.rand(5, 6))
        arr = tensor(raw, chunk_size=3)

        res = self.executor.execute_tensor(arr + 1, concat=True)[0]
        np.testing.assert_array_equal(res, raw + 1)
        self.assertFalse(res.flags['C_CONTIGUOUS'])
        self.assertTrue(res.flags['F_CONTIGUOUS'])

        res2 = self.executor.execute_tensor(add(arr, 1, order='C'), concat=True)[0]
        np.testing.assert_array_equal(res2, np.add(raw, 1, order='C'))
        self.assertTrue(res2.flags['C_CONTIGUOUS'])
        self.assertFalse(res2.flags['F_CONTIGUOUS']) 
Example #27
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testCumReduction(self):
        raw = np.random.randint(5, size=(8, 8, 8))

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True)
        res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True)
        expected1 = raw.cumsum(axis=1)
        expected2 = raw.cumprod(axis=1)
        np.testing.assert_array_equal(res1[0], expected1)
        np.testing.assert_array_equal(res2[0], expected2)

        raw = sps.random(8, 8, density=.1)

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True)
        res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True)
        expected1 = raw.A.cumsum(axis=1)
        expected2 = raw.A.cumprod(axis=1)
        self.assertTrue(np.allclose(res1[0], expected1))
        self.assertTrue(np.allclose(res2[0], expected2))

        # test order
        raw = np.asfortranarray(np.random.rand(10, 20, 30))
        arr = tensor(raw, chunk_size=13)
        arr2 = arr.cumsum(axis=-1)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.cumsum(axis=-1)
        np.testing.assert_allclose(res, expected)
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS']) 
Example #28
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testArgReduction(self):
        raw = np.random.random((20, 20, 20))

        arr = tensor(raw, chunk_size=3)

        self.assertEqual(raw.argmax(),
                         self.executor.execute_tensor(arr.argmax())[0])
        self.assertEqual(raw.argmin(),
                         self.executor.execute_tensor(arr.argmin())[0])

        np.testing.assert_array_equal(
            raw.argmax(axis=0), self.executor.execute_tensor(arr.argmax(axis=0), concat=True)[0])
        np.testing.assert_array_equal(
            raw.argmin(axis=0), self.executor.execute_tensor(arr.argmin(axis=0), concat=True)[0])

        raw_format = sps.random(20, 20, density=.1, format='lil')

        random_min = np.random.randint(0, 200)
        random_max = np.random.randint(200, 400)
        raw_format[np.unravel_index(random_min, raw_format.shape)] = -1
        raw_format[np.unravel_index(random_max, raw_format.shape)] = 2

        raw = raw_format.tocoo()
        arr = tensor(raw, chunk_size=3)

        self.assertEqual(raw.argmax(),
                         self.executor.execute_tensor(arr.argmax())[0])
        self.assertEqual(raw.argmin(),
                         self.executor.execute_tensor(arr.argmin())[0])

        # test order
        raw = np.asfortranarray(np.random.rand(10, 20, 30))
        arr = tensor(raw, chunk_size=13)
        arr2 = arr.argmax(axis=-1)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.argmax(axis=-1)
        np.testing.assert_allclose(res, expected)
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS']) 
Example #29
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testSumProdExecution(self):
        arr = ones((10, 8), chunk_size=3)
        self.assertEqual([80], self.executor.execute_tensor(arr.sum()))
        self.assertEqual((10,) * 8,
                         tuple(np.concatenate(self.executor.execute_tensor(arr.sum(axis=0)))))

        arr = ones((3, 3), chunk_size=2)
        self.assertEqual([512], self.executor.execute_tensor((arr * 2).prod()))
        self.assertEqual((8,) * 3,
                         tuple(np.concatenate(self.executor.execute_tensor((arr * 2).prod(axis=0)))))

        raw = sps.random(10, 20, density=.1)
        arr = tensor(raw, chunk_size=3)
        res = self.executor.execute_tensor(arr.sum())[0]

        self.assertAlmostEqual(res, raw.sum())

        # test order
        raw = np.asfortranarray(np.random.rand(10, 20, 30))
        arr = tensor(raw, chunk_size=13)
        arr2 = arr.sum(axis=-1)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.sum(axis=-1)
        np.testing.assert_allclose(res, expected)
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS']) 
Example #30
Source File: _interpolative_backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def idz_id2svd(B, idx, proj):
    """
    Convert complex ID to SVD.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    """
    B = np.asfortranarray(B)
    U, V, S, ier = _id.idz_id2svd(B, idx, proj)
    if ier:
        raise _RETCODE_ERROR
    return U, V, S


#------------------------------------------------------------------------------
# idz_snorm.f
#------------------------------------------------------------------------------