def test_block_svd(shape, density, sort, full_matrices, dim_keep, return_dwt): if dim_keep >= np.min(shape): dim_keep = None #np.random.seed(2) np.set_printoptions(3, linewidth = 1000, suppress = True) x = sparse.random(shape, density, format='coo') y = x.todense() if return_dwt: u, s, vt, dwt = core.block_svd(x, sort = sort, full_matrices = full_matrices, dim_keep = dim_keep, return_dwt = return_dwt) else: u, s, vt = core.block_svd(x, sort = sort, full_matrices = full_matrices, dim_keep = dim_keep) xnew = u.dot(s).dot(vt) u_np, s_np, vt_np = np.linalg.svd(y, full_matrices = False) if (dim_keep is not None) and (sort == True) and (full_matrices == False): x = COO(u_np[:, :dim_keep].dot(np.diag(s_np[:dim_keep])).dot(vt_np[:dim_keep, :])) assert np.allclose(x.todense(), xnew.todense()) if not sort: s_sort = -np.sort(-s.data) else: s_sort = s.data assert np.allclose(s_sort, s_np[:len(s_sort)]) if return_dwt: assert(np.allclose(dwt, s_np[len(s_sort):].sum()))
def test_prune_coo(): coords = np.array([[0, 1, 2, 3]]) data = np.array([1, 0, 1, 2]) s1 = COO(coords, data) s2 = COO(coords, data, prune=True) assert s2.nnz == 3 # Densify s1 because it isn't canonical assert_eq(s1.todense(), s2, check_nnz=False)
def test_large_concat_stack(): data = np.array([1], dtype=np.uint8) coords = np.array([[255]], dtype=np.uint8) xs = COO(coords, data, shape=(256, ), has_duplicates=False, sorted=True) x = xs.todense() assert_eq(np.stack([x, x]), sparse.stack([xs, xs])) assert_eq(np.concatenate((x, x)), sparse.concatenate((xs, xs)))
def _array_select(clr, i0, i1, j0, j1, field, sparse_array): is_upper = clr._is_symm_upper with clr.open("r") as h5: dtype = h5['pixels'][field].dtype reader = CSRReader(h5, field, max_chunk=500000000) if is_upper: i, j, v = query_rect(reader.query, i0, i1, j0, j1, duplex=True) else: i, j, v = reader.query(i0, i1, j0, j1) if not len(v): v = v.astype(dtype) arr = COO((i - i0, j - j0), v, shape=(i1 - i0, j1 - j0)) if not sparse_array: arr = arr.todense() return arr