Python scipy.sparse.csr_matrix() Examples

The following are 30 code examples of scipy.sparse.csr_matrix(). 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 scipy.sparse , or try the search function .
Example #1
Source File: nao.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def comp_aos_csr(self, coords, tol=1e-8, ram=160e6):
    """ 
          Compute the atomic orbitals for a given set of (Cartesian) coordinates.
        The sparse format CSR is used for output and the computation is organized block-wise.
        Thence, larger molecules can be tackled right away
          coords :: set of Cartesian coordinates
          tol :: tolerance for dropping the values 
          ram :: size of the allowed block (in bytes)
        Returns 
          co2v :: CSR matrix of shape (coordinate, atomic orbital) 
    """
    from pyscf.nao.m_aos_libnao import aos_libnao
    from pyscf import lib
    from scipy.sparse import csr_matrix
    if not self.init_sv_libnao_orbs : raise RuntimeError('not self.init_sv_libnao')
    assert coords.shape[-1] == 3
    nc,no = len(coords), self.norbs
    bsize = int(min(max(ram / (no*8.0), 1), nc))
    co2v = csr_matrix((nc,no))
    for s,f in lib.prange(0,nc,bsize):
      ca2o = aos_libnao(coords[s:f], no) # compute values of atomic orbitals
      ab = np.where(abs(ca2o)>tol)
      co2v += csr_matrix((ca2o[ab].reshape(-1), (ab[0]+s, ab[1])), shape=(nc,no))
    return co2v 
Example #2
Source File: test_sparse_ndarray.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_create_sparse_nd_from_dense():
    def check_create_from_dns(shape, f, dense_arr, dtype, default_dtype, ctx):
        arr = f(dense_arr, dtype=dtype, ctx=ctx)
        assert(same(arr.asnumpy(), np.ones(shape)))
        assert(arr.dtype == dtype)
        assert(arr.context == ctx)
        # verify the default dtype inferred from dense arr
        arr2 = f(dense_arr)
        assert(arr2.dtype == default_dtype)
        assert(arr2.context == Context.default_ctx)
    shape = rand_shape_2d()
    dtype = np.int32
    src_dtype = np.float64
    ctx = mx.cpu(1)
    dense_arrs = [mx.nd.ones(shape, dtype=src_dtype), np.ones(shape, dtype=src_dtype), \
                  np.ones(shape, dtype=src_dtype).tolist()]
    for f in [mx.nd.sparse.csr_matrix, mx.nd.sparse.row_sparse_array]:
        for dense_arr in dense_arrs:
            default_dtype = dense_arr.dtype if isinstance(dense_arr, (NDArray, np.ndarray)) \
                            else np.float32
            check_create_from_dns(shape, f, dense_arr, dtype, default_dtype, ctx) 
Example #3
Source File: matrixtools.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def safereal(A, inplace=False, check=False):
    """
    Returns the real-part of `A` correctly when `A` is either a dense array or
    a sparse matrix
    """
    if check:
        assert(safenorm(A, 'imag') < 1e-6), "Check failed: taking real-part of matrix w/nonzero imaginary part"
    if _sps.issparse(A):
        if _sps.isspmatrix_csr(A):
            if inplace:
                ret = _sps.csr_matrix((_np.real(A.data), A.indices, A.indptr), shape=A.shape, dtype='d')
            else:  # copy
                ret = _sps.csr_matrix((_np.real(A.data).copy(), A.indices.copy(),
                                       A.indptr.copy()), shape=A.shape, dtype='d')
            ret.eliminate_zeros()
            return ret
        else:
            raise NotImplementedError("safereal() doesn't work with %s matrices yet" % str(type(A)))
    else:
        return _np.real(A) 
Example #4
Source File: matrixtools.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def safeimag(A, inplace=False, check=False):
    """
    Returns the imaginary-part of `A` correctly when `A` is either a dense array
    or a sparse matrix
    """
    if check:
        assert(safenorm(A, 'real') < 1e-6), "Check failed: taking imag-part of matrix w/nonzero real part"
    if _sps.issparse(A):
        if _sps.isspmatrix_csr(A):
            if inplace:
                ret = _sps.csr_matrix((_np.imag(A.data), A.indices, A.indptr), shape=A.shape, dtype='d')
            else:  # copy
                ret = _sps.csr_matrix((_np.imag(A.data).copy(), A.indices.copy(),
                                       A.indptr.copy()), shape=A.shape, dtype='d')
            ret.eliminate_zeros()
            return ret
        else:
            raise NotImplementedError("safereal() doesn't work with %s matrices yet" % str(type(A)))
    else:
        return _np.imag(A) 
Example #5
Source File: sparse.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def asscipy(self):
        """Returns a ``scipy.sparse.csr.csr_matrix`` object with value copied from this array

        Examples
        --------
        >>> x = mx.nd.sparse.zeros('csr', (2,3))
        >>> y = x.asscipy()
        >>> type(y)
        <type 'scipy.sparse.csr.csr_matrix'>
        >>> y
        <2x3 sparse matrix of type '<type 'numpy.float32'>'
        with 0 stored elements in Compressed Sparse Row format>
        """
        data = self.data.asnumpy()
        indices = self.indices.asnumpy()
        indptr = self.indptr.asnumpy()
        if not spsp:
            raise ImportError("scipy is not available. \
                               Please check if the scipy python bindings are installed.")
        return spsp.csr_matrix((data, indices, indptr), shape=self.shape, dtype=self.dtype)

# pylint: disable=abstract-method 
Example #6
Source File: process.py    From scanorama with MIT License 6 votes vote down vote up
def load_names(data_names, norm=True, log1p=False, verbose=True):
    # Load datasets.
    datasets = []
    genes_list = []
    n_cells = 0
    for name in data_names:
        X_i, genes_i = load_data(name)
        if norm:
            X_i = normalize(X_i, axis=1)
        if log1p:
            X_i = np.log1p(X_i)
        X_i = csr_matrix(X_i)
            
        datasets.append(X_i)
        genes_list.append(genes_i)
        n_cells += X_i.shape[0]
        if verbose:
            print('Loaded {} with {} genes and {} cells'.
                  format(name, X_i.shape[1], X_i.shape[0]))
    if verbose:
        print('Found {} cells among all datasets'
              .format(n_cells))

    return datasets, genes_list, n_cells 
Example #7
Source File: slowreplib.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def __init__(self, errgen_rep,
                 mu, eta, m_star, s, unitarypost_data,
                 unitarypost_indices, unitarypost_indptr):
        dim = errgen_rep.dim
        self.errgen_rep = errgen_rep
        if len(unitarypost_data) > 0:  # (nnz > 0)
            self.unitary_postfactor = _sps.csr_matrix(
                (unitarypost_data, unitarypost_indices,
                 unitarypost_indptr), shape=(dim, dim))
        else:
            self.unitary_postfactor = None  # no unitary postfactor

        self.mu = mu
        self.eta = eta
        self.m_star = m_star
        self.s = s
        super(DMOpRep_Lindblad, self).__init__(dim) 
Example #8
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def jacobian(self, p, into=None):
        # transpose to be 3 x 2 x n
        p = np.transpose(np.reshape(p, (-1, 3, 2)), (1,2,0))
        # First, get the two legs...
        (dx_ab, dy_ab) = p[1] - p[0]
        (dx_ac, dy_ac) = p[2] - p[0]
        (dx_bc, dy_bc) = p[2] - p[1]
        # now, the area is half the z-value of the cross-product...
        sarea0 = 0.5 * (dx_ab*dy_ac - dx_ac*dy_ab)
        # but we want to abs it
        dsarea0 = np.sign(sarea0)
        z = np.transpose([[-dy_bc,dx_bc], [dy_ac,-dx_ac], [-dy_ab,dx_ab]], (2,0,1))
        z = times(0.5*dsarea0, z)
        m = numel(p)
        n = p.shape[2]
        ii = (np.arange(n) * np.ones([6, n])).T.flatten()
        z = sps.csr_matrix((z.flatten(), (ii, np.arange(len(ii)))), shape=(n, m))
        return safe_into(into, z) 
Example #9
Source File: process.py    From scanorama with MIT License 6 votes vote down vote up
def load_mtx(dname):
    with open(dname + '/matrix.mtx', 'r') as f:
        while True:
            header = f.readline()
            if not header.startswith('%'):
                break
        header = header.rstrip().split()
        n_genes, n_cells = int(header[0]), int(header[1])

        data, i, j = [], [], []
        for line in f:
            fields = line.rstrip().split()
            data.append(float(fields[2]))
            i.append(int(fields[1])-1)
            j.append(int(fields[0])-1)
        X = csr_matrix((data, (i, j)), shape=(n_cells, n_genes))

    genes = []
    with open(dname + '/genes.tsv', 'r') as f:
        for line in f:
            fields = line.rstrip().split()
            genes.append(fields[1])
    assert(len(genes) == n_genes)

    return X, np.array(genes) 
Example #10
Source File: testAdvancedGatesetParameterization.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def test_sparse_lindblad_param(self):
        #Test sparse Lindblad gates

        print("\nGate Test:")
        SparseId = sps.identity(4**2,'d','csr')
        gate = LindbladDenseOp.from_operation_matrix( np.identity(4**2,'d') )
        print("gate Errgen type (should be dense):",type(gate.errorgen.todense()))
        self.assertIsInstance(gate.errorgen.todense(), np.ndarray)
        sparseOp = LindbladOp.from_operation_matrix( SparseId )
        print("spareGate Errgen type (should be sparse):",type(sparseOp.errorgen.tosparse()))
        self.assertIsInstance(sparseOp.errorgen.tosparse(), sps.csr_matrix)
        self.assertArraysAlmostEqual(gate.errorgen.todense(),sparseOp.errorgen.todense())

        perfectG = std2Q_XYICNOT.target_model().operations['Gix'].copy()
        noisyG = std2Q_XYICNOT.target_model().operations['Gix'].copy()
        noisyG.depolarize(0.9)
        Sparse_noisyG = sps.csr_matrix(noisyG,dtype='d')
        Sparse_perfectG = sps.csr_matrix(perfectG,dtype='d')
        op2 = LindbladDenseOp.from_operation_matrix( noisyG, perfectG )
        sparseGate2 = LindbladOp.from_operation_matrix( Sparse_noisyG, Sparse_perfectG )
        print("spareGate2 Errgen type (should be sparse):",type(sparseGate2.errorgen.tosparse()))
        self.assertIsInstance(sparseGate2.errorgen.tosparse(), sps.csr_matrix)
        #print("errgen = \n"); pygsti.tools.print_mx(op2.err_gen,width=4,prec=1)
        #print("sparse errgen = \n"); pygsti.tools.print_mx(sparseGate2.err_gen.toarray(),width=4,prec=1)
        self.assertArraysAlmostEqual(op2.errorgen.todense(),sparseGate2.errorgen.todense()) 
Example #11
Source File: utils.py    From OpenNE with MIT License 6 votes vote down vote up
def chebyshev_polynomials(adj, k):
    """Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
    print("Calculating Chebyshev polynomials up to order {}...".format(k))

    adj_normalized = normalize_adj(adj)
    laplacian = sp.eye(adj.shape[0]) - adj_normalized
    largest_eigval, _ = eigsh(laplacian, 1, which='LM')
    scaled_laplacian = (
        2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])

    t_k = list()
    t_k.append(sp.eye(adj.shape[0]))
    t_k.append(scaled_laplacian)

    def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
        s_lap = sp.csr_matrix(scaled_lap, copy=True)
        return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two

    for i in range(2, k+1):
        t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))

    return sparse_to_tuple(t_k) 
Example #12
Source File: serve_utils.py    From sagemaker-xgboost-container with Apache License 2.0 6 votes vote down vote up
def _get_sparse_matrix_from_libsvm(payload):
    pylist = map(lambda x: x.split(' '), payload.split('\n'))
    colon = ':'
    row = []
    col = []
    data = []
    for row_idx, line in enumerate(pylist):
        for item in line:
            if colon in item:
                col_idx = item.split(colon)[0]
                val = item.split(colon)[1]
                row.append(row_idx)
                col.append(col_idx)
                data.append(val)

    row = np.array(row)
    col = np.array(col).astype(np.int)
    data = np.array(data).astype(np.float)
    if not (len(row) == len(col) and len(col) == len(data)):
        raise RuntimeError("Dimension checking failed when transforming sparse matrix.")

    return csr_matrix((data, (row, col))) 
Example #13
Source File: test_ftrl.py    From Kaggler with MIT License 6 votes vote down vote up
def main():
    print('create y...')
    y = np.random.randint(2, size=N_OBS)
    print('create X...')
    row = np.random.randint(N_OBS, size=N_VALUE)
    col = np.random.randint(N_FEATURE, size=N_VALUE)
    data = np.ones(N_VALUE)
    X = sparse.csr_matrix((data, (row, col)), dtype=np.int8)

    print('train...')
    profiler = cProfile.Profile(subcalls=True, builtins=True, timeunit=0.001,)
    clf = FTRL(interaction=False)
    profiler.enable()
    clf.fit(X, y)
    profiler.disable()
    profiler.print_stats()

    p = clf.predict(X)
    print('AUC: {:.4f}'.format(auc(y, p)))

    assert auc(y, p) > .5 
Example #14
Source File: test_model.py    From modelforge with Apache License 2.0 6 votes vote down vote up
def test_disassemble_sparse_matrix(self):
        arr = numpy.zeros((10, 10), dtype=numpy.float32)
        numpy.random.seed(0)
        arr[numpy.random.randint(0, 10, (50, 2))] = 1
        mat = csr_matrix(arr)
        dis = disassemble_sparse_matrix(mat)
        self.assertIsInstance(dis, dict)
        self.assertIn("shape", dis)
        self.assertIn("format", dis)
        self.assertIn("data", dis)
        self.assertEqual(dis["shape"], arr.shape)
        self.assertEqual(dis["format"], "csr")
        self.assertIsInstance(dis["data"], list)
        self.assertEqual(len(dis["data"]), 3)
        self.assertTrue((dis["data"][0] == mat.data).all())
        self.assertTrue((dis["data"][1] == mat.indices).all())
        self.assertTrue((dis["data"][2] == [0] + list(numpy.diff(mat.indptr))).all())
        self.assertEqual(dis["data"][2].dtype, numpy.uint8) 
Example #15
Source File: element.py    From StructEngPy with MIT License 6 votes vote down vote up
def __init__(self,node_i,node_j,A,rho,dof,name=None,mass='conc',tol=1e-6):
        super(Line,self).__init__(1,dof,name)
        self._nodes=[node_i,node_j]
        #Initialize local CSys
        o = [ node_i.x, node_i.y, node_i.z ]
        pt1 = [ node_j.x, node_j.y, node_j.z ]
        pt2 = [ node_i.x, node_i.y, node_i.z ]
        if abs(node_i.x - node_j.x) < tol and abs(node_i.y - node_j.y) < tol:
            pt2[0] += 1
        else:
            pt2[2] += 1
        self._local_csys = Cartisian(o, pt1, pt2)

        T=np.zeros((12,12))
        V=self._local_csys.transform_matrix
        T[:3,:3] =T[3:6,3:6]=T[6:9,6:9]=T[9:,9:]= V
        self._T=spr.csr_matrix(T)

        self._length=((node_i.x - node_j.x)**2 + (node_i.y - node_j.y)**2 + (node_i.z - node_j.z)**2)**0.5
        self._mass=rho*A*self.length 
Example #16
Source File: graph.py    From EDeN with MIT License 6 votes vote down vote up
def _convert_dict_to_sparse_matrix(self, feature_rows):
        if len(feature_rows) == 0:
            raise Exception('ERROR: something went wrong, empty features.')
        data, row, col = [], [], []
        for i, feature_row in enumerate(feature_rows):
            if len(feature_row) == 0:
                # case of empty feature set for a specific instance
                row.append(i)
                col.append(0)
                data.append(0)
            else:
                for feature in feature_row:
                    row.append(i)
                    col.append(feature)
                    data.append(feature_row[feature])
        shape = (max(row) + 1, self.feature_size)
        data_matrix = csr_matrix((data, (row, col)),
                                 shape=shape, dtype=np.float64)
        return data_matrix 
Example #17
Source File: test_matrixtools.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def test_fast_expm_raises_on_non_square(self):
        nonSq = np.array([[1, 2, 4],
                          [2, 3, 5]], 'd')
        N = sps.csr_matrix(nonSq)

        with self.assertRaises(ValueError):
            mt.expm_multiply_prep(N) 
Example #18
Source File: io.py    From jwalk with Apache License 2.0 5 votes vote down vote up
def save_graph(filename, csr_matrix, labels=None):
    np.savez(filename,
             data=csr_matrix.data,
             indices=csr_matrix.indices,
             indptr=csr_matrix.indptr,
             shape=csr_matrix.shape,
             labels=labels)
    return filename 
Example #19
Source File: graph.py    From jwalk with Apache License 2.0 5 votes vote down vote up
def make_undirected(csr_matrix):
    """Make CSR matrix undirected."""
    return csr_matrix + csr_matrix.T 
Example #20
Source File: graph.py    From jwalk with Apache License 2.0 5 votes vote down vote up
def build_adjacency_matrix(edges, undirected=False):
    """Build adjacency matrix.

    Args:
        edges (np.ndarray): a 2 or 3 dim array of the form [src, tgt, [weight]]
        undirected (bool): if True, add matrix with its transpose

    Returns:
        scipy.sparse.csr_matrix: adjacency matrix, np.ndarray: labels
    """
    assert edges.shape[1] in [2, 3], "Input must contain 2 or 3 columns"

    if edges.shape[1] == 2:  # if no weights
        logger.info("Weight column not found. Defaulting to value 1.")
        weights = np.ones(edges.shape[0], dtype='float')
    else:
        weights = edges[:, 2].astype('float')

    edges = edges[:, :2]
    nodes = np.unique(edges)  # returns sorted
    num_nodes = nodes.shape[0]

    encoded = encode_edges(edges, nodes)

    sp = sps.csr_matrix((weights, encoded.T), shape=(num_nodes, num_nodes))

    if undirected:
        sp = make_undirected(sp)
    return sp, nodes 
Example #21
Source File: load_w2v.py    From tartarus with MIT License 5 votes vote down vote up
def load_sparse_csr(filename):
    loader = np.load(filename)
    return csr_matrix((  loader['data'], loader['indices'], loader['indptr']),
                         shape = loader['shape']) 
Example #22
Source File: test_jwalk.py    From jwalk with Apache License 2.0 5 votes vote down vote up
def test_build_adjacency_matrix():
    edges = np.array([['A', 'B'],
                      ['A', 'C'],
                      ['B', 'C'],
                      ['B', 'E']])
    csr_matrix, id2item = graph.build_adjacency_matrix(edges)
    assert len(id2item) == 4
    assert np.array_equal(csr_matrix.todense(), [[0., 1., 1., 0.],
                                                 [0., 0., 1., 1.],
                                                 [0., 0., 0., 0.],
                                                 [0., 0., 0., 0.]]) 
Example #23
Source File: test_fem.py    From simnibs with GNU General Public License v3.0 5 votes vote down vote up
def test_multiple_rhs(self):
        np.random.seed(0)
        n = 5
        A = np.random.random((n, n))
        A += A.T
        A = sparse.csr_matrix(A)
        b = np.random.random((n, 3))
        #b = np.ones((n, 3))
        options = '-ksp_type gmres -pc_type ilu -ksp_rtol 1e-10'
        x = petsc_solver.petsc_solve(options, A, b)
        assert np.allclose(A.dot(x), b) 
Example #24
Source File: test_fem.py    From simnibs with GNU General Public License v3.0 5 votes vote down vote up
def test_solve_petsc_random(self):
        np.random.seed(0)
        n = 5
        A = np.random.random((5, 5))
        A += A.T
        A = sparse.csr_matrix(A)
        b = np.ones(n)
        options = '-ksp_type gmres -pc_type ilu -ksp_rtol 1e-10'
        x = petsc_solver.petsc_solve(options, A, b).squeeze()
        assert np.allclose(A.dot(x), b) 
Example #25
Source File: pardiso.py    From simnibs with GNU General Public License v3.0 5 votes vote down vote up
def __del__(self):
        self._A = sp.csr_matrix((0,0))
        b = np.zeros(0)
        self._call_pardiso(b, 0) 
Example #26
Source File: predict.py    From tartarus with MIT License 5 votes vote down vote up
def load_sparse_csr(filename):
    loader = np.load(filename)
    return csr_matrix((  loader['data'], loader['indices'], loader['indptr']),
                         shape = loader['shape']) 
Example #27
Source File: eval.py    From tartarus with MIT License 5 votes vote down vote up
def load_sparse_csr(filename):
    loader = np.load(filename)
    return csr_matrix((  loader['data'], loader['indices'], loader['indptr']),
                         shape = loader['shape']) 
Example #28
Source File: create_patches.py    From tartarus with MIT License 5 votes vote down vote up
def load_sparse_csr(filename):
    loader = np.load(filename)
    return csr_matrix((  loader['data'], loader['indices'], loader['indptr']),
                         shape = loader['shape']) 
Example #29
Source File: load_vsm.py    From tartarus with MIT License 5 votes vote down vote up
def load_sparse_csr(filename):
    loader = np.load(filename)
    return csr_matrix((  loader['data'], loader['indices'], loader['indptr']),
                         shape = loader['shape']) 
Example #30
Source File: test_model.py    From modelforge with Apache License 2.0 5 votes vote down vote up
def test_disassemble_sparse_matrix_empty(self):
        arr = numpy.zeros((10, 10), dtype=numpy.float32)
        mat = csr_matrix(arr)
        dis = disassemble_sparse_matrix(mat)
        self.assertIsInstance(dis, dict)
        self.assertIn("shape", dis)
        self.assertIn("format", dis)
        self.assertIn("data", dis)
        self.assertEqual(dis["shape"], arr.shape)
        self.assertEqual(dis["format"], "csr")
        self.assertIsInstance(dis["data"], list)
        self.assertEqual(len(dis["data"]), 3)
        self.assertEqual(dis["data"][0].size, 0)
        self.assertEqual(dis["data"][1].size, 0)
        self.assertTrue((dis["data"][2] == 0).all())