Exemple #1
0
def test_mult_ab(kernel, benchmark):
    A = sps.random(100, 500, 0.1, format='csr')
    B = sps.random(500, 200, 0.2, format='csr')
    A = CSR.from_scipy(A)
    B = CSR.from_scipy(B)

    # make sure it's compiled
    A.multiply(B)

    def op():
        A.multiply(B)

    benchmark(op)
Exemple #2
0
def test_mult_ab_by_size(kernel, benchmark, size):
    A = sps.random(size, size, 0.1, format='csr')
    B = sps.random(size, size, 0.1, format='csr')
    A = CSR.from_scipy(A)
    B = CSR.from_scipy(B)

    # make sure it's compiled
    A.multiply(B)

    def op():
        A.multiply(B)

    benchmark(op)
Exemple #3
0
def test_mult_abt_by_density(kernel, benchmark, density):
    A = sps.random(100, 100, density, format='csr')
    B = sps.random(100, 100, density, format='csr')
    A = CSR.from_scipy(A)
    B = CSR.from_scipy(B)

    # make sure it's compiled
    A.multiply(B, transpose=True)

    def op():
        A.multiply(B, transpose=True)

    benchmark(op)
Exemple #4
0
def test_sps_to_csr(data, format):
    mat = data.draw(sparse_matrices(format=format))
    nr, nc = mat.shape
    sp_csr: sps.csr_matrix = mat.tocsr()

    csr = CSR.from_scipy(mat)

    assert csr.ncols == nc
    assert csr.nrows == nr
    assert csr.nnz == mat.nnz
    assert np.all(csr.rowptrs == sp_csr.indptr)
    assert np.all(csr.colinds == sp_csr.indices)
    assert np.all(csr.values == sp_csr.data)
Exemple #5
0
def test_mult_vec(kernel, benchmark):
    A = sps.random(100, 100, 0.1, format='csr')
    A = CSR.from_scipy(A)
    x = np.random.randn(100)

    # make sure it's compiled
    y = A.mult_vec(x)
    assert len(y) == A.nrows

    def op():
        A.mult_vec(x)

    benchmark(op)
Exemple #6
0
 def _normalize(self, rmat):
     rmat = rmat.to_scipy()
     # compute column norms
     norms = spla.norm(rmat, 2, axis=0)
     # and multiply by a diagonal to normalize columns
     recip_norms = norms.copy()
     is_nz = recip_norms > 0
     recip_norms[is_nz] = np.reciprocal(recip_norms[is_nz])
     norm_mat = rmat @ sps.diags(recip_norms)
     assert norm_mat.shape[1] == rmat.shape[1]
     # and reset NaN
     norm_mat.data[np.isnan(norm_mat.data)] = 0
     _logger.info('[%s] normalized rating matrix columns', self._timer)
     return CSR.from_scipy(norm_mat, False)
Exemple #7
0
def test_csr_from_sps_csr(smat, copy):
    "Test creating a CSR from a SciPy CSR matrix"
    csr = CSR.from_scipy(smat, copy=copy)
    assert csr.nnz == smat.nnz
    assert csr.nrows == smat.shape[0]
    assert csr.ncols == smat.shape[1]

    assert all(csr.rowptrs == smat.indptr)
    assert all(csr.colinds == smat.indices)
    assert all(csr.values == smat.data)
    assert isinstance(csr.rowptrs, np.ndarray)
    assert isinstance(csr.colinds, np.ndarray)
    if csr.nnz > 0:
        assert isinstance(csr.values, np.ndarray)
Exemple #8
0
def test_csr_row_nnzs(mat):
    nrows, ncols = mat.shape

    # sparsify the matrix
    mat[mat <= 0] = 0
    smat = sps.csr_matrix(mat)
    # make sure it's sparse
    assume(smat.nnz == np.sum(mat > 0))
    csr = CSR.from_scipy(smat)

    nnzs = csr.row_nnzs()
    assert nnzs.sum() == csr.nnz
    for i in range(nrows):
        row = mat[i, :]
        assert nnzs[i] == np.sum(row > 0)
Exemple #9
0
def test_subset_rows(data):
    nrows = data.draw(st.integers(5, 100))
    ncols = data.draw(st.integers(1, 100))
    dens = data.draw(st.floats(0, 1))
    beg = data.draw(st.integers(0, nrows - 1))
    end = data.draw(st.integers(beg, nrows - 1))

    spm = sps.random(nrows, ncols, dens, format='csr')
    csr = CSR.from_scipy(spm)

    m2 = csr.subset_rows(beg, end)
    assert m2.nrows == end - beg

    for i in range(m2.nrows):
        assert all(m2.row_cs(i) == csr.row_cs(beg + i))
        assert all(m2.row_vs(i) == csr.row_vs(beg + i))
Exemple #10
0
def test_csr_transpose_erow():
    "Test transposition with an empty output row"
    nrows = np.random.randint(10, 1000)
    ncols = np.random.randint(10, 500)
    mat = np.random.randn(nrows, ncols)
    mat[mat <= 0] = 0
    mat[:, 0:1] = 0
    smat = sps.csr_matrix(mat)

    csr = CSR.from_scipy(smat)
    csrt = csr.transpose()
    assert csrt.nrows == ncols
    assert csrt.ncols == nrows

    s2 = csrt.to_scipy()
    smat = smat.T.tocsr()
    assert all(smat.indptr == csrt.rowptrs)

    assert np.all(s2.toarray() == smat.toarray())