Python scipy.sparse.spmatrix() Examples

The following are 30 code examples of scipy.sparse.spmatrix(). 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: linear_constraint.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 quadratic_program: Any, name: str,
                 linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
                 sense: ConstraintSense,
                 rhs: float
                 ) -> None:
        """
        Args:
            quadratic_program: The parent quadratic program.
            name: The name of the constraint.
            linear: The coefficients specifying the linear constraint.
            sense: The sense of the constraint.
            rhs: The right-hand-side of the constraint.
        """
        super().__init__(quadratic_program, name, sense, rhs)
        self._linear = LinearExpression(quadratic_program, linear) 
Example #2
Source File: quadratic_expression.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def __init__(self, quadratic_program: Any,
                 coefficients: Union[ndarray, spmatrix, List[List[float]],
                                     Dict[Tuple[Union[int, str], Union[int, str]], float]]) -> None:
        """Creates a new quadratic expression.

        The quadratic expression can be defined via an array, a list, a sparse matrix, or a
        dictionary that uses variable names or indices as keys and stores the values internally as a
        dok_matrix. We stores values in a compressed way, i.e., values at symmetric positions are
        summed up in the upper triangle. For example, {(0, 1): 1, (1, 0): 2} -> {(0, 1): 3}.

        Args:
            quadratic_program: The parent QuadraticProgram.
            coefficients: The (sparse) representation of the coefficients.

        """
        super().__init__(quadratic_program)
        self.coefficients = coefficients 
Example #3
Source File: __init__.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _move_adj_mtx(d):
    """
    Read-time fix for moving adjacency matrices from uns to obsp
    """
    n = d.get("uns", {}).get("neighbors", {})
    obsp = d.setdefault("obsp", {})

    for k in ("distances", "connectivities"):
        if (
            (k in n)
            and isinstance(n[k], (spmatrix, np.ndarray))
            and len(n[k].shape) == 2
        ):
            warn(
                f"Moving element from .uns['neighbors']['{k}'] to .obsp['{k}'].\n\n"
                "This is where adjacency matrices should go now.",
                FutureWarning,
            )
            obsp[k] = n.pop(k) 
Example #4
Source File: quadratic_program.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def maximize(self,
                 constant: float = 0.0,
                 linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]] = None,
                 quadratic: Union[ndarray, spmatrix, List[List[float]],
                                  Dict[Tuple[Union[int, str], Union[int, str]], float]] = None
                 ) -> None:
        """Sets a quadratic objective to be maximized.

        Args:
            constant: the constant offset of the objective.
            linear: the coefficients of the linear part of the objective.
            quadratic: the coefficients of the quadratic part of the objective.

        Returns:
            The created quadratic objective.
        """
        self._objective = QuadraticObjective(self, constant, linear, quadratic,
                                             QuadraticObjective.Sense.MAXIMIZE) 
Example #5
Source File: test_io_conversion.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_dense_to_sparse_memory(tmp_path, spmtx_format, to_convert):
    dense_path = tmp_path / "dense.h5ad"
    orig = gen_adata((50, 50), np.array)
    orig.raw = orig
    orig.write_h5ad(dense_path)
    assert not isinstance(orig.X, sparse.spmatrix)
    assert not isinstance(orig.raw.X, sparse.spmatrix)

    curr = ad.read_h5ad(dense_path, as_sparse=to_convert, as_sparse_fmt=spmtx_format)

    if "X" in to_convert:
        assert isinstance(curr.X, spmtx_format)
    if "raw/X" in to_convert:
        assert isinstance(curr.raw.X, spmtx_format)

    assert_equal(orig, curr) 
Example #6
Source File: h5ad.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_series(dataset) -> Union[np.ndarray, pd.Categorical]:
    if "categories" in dataset.attrs:
        categories = dataset.attrs["categories"]
        if isinstance(categories, h5py.Reference):
            categories_dset = dataset.parent[dataset.attrs["categories"]]
            categories = categories_dset[...]
            ordered = bool(categories_dset.attrs.get("ordered", False))
        else:
            # TODO: remove this code at some point post 0.7
            # TODO: Add tests for this
            warn(
                f"Your file {str(dataset.file.name)!r} has invalid categorical "
                "encodings due to being written from a development version of "
                "AnnData. Rewrite the file ensure you can read it in the future.",
                FutureWarning,
            )
        return pd.Categorical.from_codes(dataset[...], categories, ordered=ordered)
    else:
        return dataset[...]


# @report_read_key_on_error
# def read_sparse_dataset_backed(group: h5py.Group) -> sparse.spmatrix:
#     return SparseDataset(group) 
Example #7
Source File: _utils.py    From scanpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def sparse_mean_variance_axis(mtx: sparse.spmatrix, axis: int):
    """
    This code and internal functions are based on sklearns
    `sparsefuncs.mean_variance_axis`.

    Modifications:
    * allow deciding on the output type, which can increase accuracy when calculating the mean and variance of 32bit floats.
    * This doesn't currently implement support for null values, but could.
    * Uses numba not cython
    """
    assert axis in (0, 1)
    if isinstance(mtx, sparse.csr_matrix):
        ax_minor = 1
        shape = mtx.shape
    elif isinstance(mtx, sparse.csc_matrix):
        ax_minor = 0
        shape = mtx.shape[::-1]
    else:
        raise ValueError("This function only works on sparse csr and csc matrices")
    if axis == ax_minor:
        return sparse_mean_var_major_axis(
            mtx.data, mtx.indices, mtx.indptr, *shape, np.float64
        )
    else:
        return sparse_mean_var_minor_axis(mtx.data, mtx.indices, *shape, np.float64) 
Example #8
Source File: h5ad.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_group(group: h5py.Group) -> Union[dict, pd.DataFrame, sparse.spmatrix]:
    if "h5sparse_format" in group.attrs:  # Backwards compat
        return SparseDataset(group).to_memory()

    encoding_type = group.attrs.get("encoding-type")
    if encoding_type:
        EncodingVersions[encoding_type].check(
            group.name, group.attrs["encoding-version"]
        )
    if encoding_type in {None, "raw"}:
        pass
    elif encoding_type == "dataframe":
        return read_dataframe(group)
    elif encoding_type in {"csr_matrix", "csc_matrix"}:
        return SparseDataset(group).to_memory()
    else:
        raise ValueError(f"Unfamiliar `encoding-type`: {encoding_type}.")
    d = dict()
    for sub_key, sub_value in group.items():
        d[sub_key] = read_attribute(sub_value)
    return d 
Example #9
Source File: quadratic_program.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def minimize(self,
                 constant: float = 0.0,
                 linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]] = None,
                 quadratic: Union[ndarray, spmatrix, List[List[float]],
                                  Dict[Tuple[Union[int, str], Union[int, str]], float]] = None
                 ) -> None:
        """Sets a quadratic objective to be minimized.

        Args:
            constant: the constant offset of the objective.
            linear: the coefficients of the linear part of the objective.
            quadratic: the coefficients of the quadratic part of the objective.

        Returns:
            The created quadratic objective.
        """
        self._objective = QuadraticObjective(self, constant, linear, quadratic,
                                             QuadraticObjective.Sense.MINIMIZE) 
Example #10
Source File: zarr.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write_zarr(
    store: Union[MutableMapping, str, Path],
    adata: AnnData,
    chunks=None,
    **dataset_kwargs,
) -> None:
    if isinstance(store, Path):
        store = str(store)
    adata.strings_to_categoricals()
    if adata.raw is not None:
        adata.strings_to_categoricals(adata.raw.var)
    f = zarr.open(store, mode="w")
    if chunks is not None and not isinstance(adata.X, sparse.spmatrix):
        write_attribute(f, "X", adata.X, dict(chunks=chunks, **dataset_kwargs))
    else:
        write_attribute(f, "X", adata.X, dataset_kwargs)
    write_attribute(f, "obs", adata.obs, dataset_kwargs)
    write_attribute(f, "var", adata.var, dataset_kwargs)
    write_attribute(f, "obsm", adata.obsm, dataset_kwargs)
    write_attribute(f, "varm", adata.varm, dataset_kwargs)
    write_attribute(f, "obsp", adata.obsp, dataset_kwargs)
    write_attribute(f, "varp", adata.varp, dataset_kwargs)
    write_attribute(f, "layers", adata.layers, dataset_kwargs)
    write_attribute(f, "uns", adata.uns, dataset_kwargs)
    write_attribute(f, "raw", adata.raw, dataset_kwargs) 
Example #11
Source File: numpy_eigen_solver.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def _eval_aux_operators(self, wavefn, threshold: float = 1e-12) -> np.ndarray:
        values = []  # type: List[Tuple[float, int]]
        for operator in self._aux_operators:
            if operator is None:
                values.append(None)
                continue
            value = 0.0
            if operator.coeff != 0:
                mat = operator.to_spmatrix()
                # Terra doesn't support sparse yet, so do the matmul directly if so
                # This is necessary for the particle_hole and other chemistry tests because the
                # pauli conversions are 2^12th large and will OOM error if not sparse.
                if isinstance(mat, scisparse.spmatrix):
                    value = mat.dot(wavefn).dot(np.conj(wavefn))
                else:
                    value = StateFn(operator, is_measurement=True).eval(wavefn)
                value = value.real if abs(value.real) > threshold else 0.0
            values.append((value, 0))
        return np.asarray(values) 
Example #12
Source File: linear_expression.py    From qiskit-aqua with Apache License 2.0 6 votes vote down vote up
def __init__(self, quadratic_program: Any,
                 coefficients: Union[ndarray, spmatrix, List[float],
                                     Dict[Union[int, str], float]]) -> None:
        """Creates a new linear expression.

        The linear expression can be defined via an array, a list, a sparse matrix, or a dictionary
        that uses variable names or indices as keys and stores the values internally as a
        dok_matrix.

        Args:
            quadratic_program: The parent QuadraticProgram.
            coefficients: The (sparse) representation of the coefficients.

        """
        super().__init__(quadratic_program)
        self.coefficients = coefficients 
Example #13
Source File: perturbation_attack.py    From node_embedding_attack with MIT License 6 votes vote down vote up
def estimate_delta_eigvals(candidates, adj_matrix, vals_org, vecs_org):
    """Computes the estimated change in the eigenvalues for every candidate edge flip.

    :param candidates: np.ndarray, shape [?, 2]
        Candidate set of edge flips
    :param adj_matrix: sp.spmatrix
        The graph represented as a sparse scipy matrix
    :param vals_org: np.ndarray, shape [n]
        The generalized eigenvalues of the clean graph
    :param vecs_org: np.ndarray, shape [n, n]
        The generalized eigenvectors of the clean graph
    :return: np.ndarray, shape [?, n]
        Estimated change in the eigenvalues for all candidate edge flips
    """
    # vector indicating whether we are adding an edge (+1) or removing an edge (-1)
    delta_w = 1 - 2 * adj_matrix[candidates[:, 0], candidates[:, 1]].A1

    delta_eigvals = delta_w[:, None] * (2 * vecs_org[candidates[:, 0]] * vecs_org[candidates[:, 1]]
                                        - vals_org * (
                                                vecs_org[candidates[:, 0]] ** 2 + vecs_org[candidates[:, 1]] ** 2))

    return delta_eigvals 
Example #14
Source File: utils.py    From graph2gauss with MIT License 6 votes vote down vote up
def sparse_feeder(M):
    """
    Prepares the input matrix into a format that is easy to feed into tensorflow's SparseTensor

    Parameters
    ----------
    M : scipy.sparse.spmatrix
        Matrix to be fed

    Returns
    -------
    indices : array-like, shape [n_edges, 2]
        Indices of the sparse elements
    values : array-like, shape [n_edges]
        Values of the sparse elements
    shape : array-like
        Shape of the matrix
    """
    M = sp.coo_matrix(M)
    return np.vstack((M.row, M.col)).T, M.data, M.shape 
Example #15
Source File: raw.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def X(self) -> Union[SparseDataset, np.ndarray, sparse.spmatrix]:
        # TODO: Handle unsorted array of integer indices for h5py.Datasets
        if not self._adata.isbacked:
            return self._X
        if not self._adata.file.is_open:
            self._adata.file.open()
        # Handle legacy file formats:
        if "raw/X" in self._adata.file:
            X = self._adata.file["raw/X"]
        elif "raw.X" in self._adata.file:
            X = self._adata.file["raw.X"]  # Backwards compat
        else:
            raise AttributeError(
                f"Could not find dataset for raw X in file: "
                f"{self._adata.file.filename}."
            )
        if isinstance(X, h5py.Group):
            X = SparseDataset(X)
        # Check if we need to subset
        if self._adata.is_view:
            # TODO: As noted above, implement views of raw
            #       so we can know if we need to subset by var
            return X[self._adata._oidx, slice(None)]
        else:
            return X 
Example #16
Source File: gaussian.py    From edm2016 with Apache License 2.0 6 votes vote down vote up
def get_dependent_vars(self, var_idx):
        """ Given the indices of the query variables, var_idx, returns the set of dependent
        variables (including var_idx); i.e., the smallest set S containing var_idx for which
        (complement of S) indep of (S).  This is done by finding all non-zero columns of the
        `var_idx` rows of the precision matrix.

        :param np.ndarray[int]|np.ndarray[bool] var_idx: indices of the query variables
        :return: indices of the dependent variables
        :rtype: np.ndarray[int]
        """
        if isinstance(self.precision, sp.spmatrix):
            prec = self.precision.tocsr()
        elif np.isscalar(self.precision):
            return var_idx
        else:
            prec = self.precision
        return np.unique(np.nonzero(prec[var_idx, :])[1]) 
Example #17
Source File: raw.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(
        self,
        adata: "anndata.AnnData",
        X: Union[np.ndarray, sparse.spmatrix, None] = None,
        var: Union[pd.DataFrame, Mapping[str, Sequence], None] = None,
        varm: Union[AxisArrays, Mapping[str, np.ndarray], None] = None,
    ):
        from .anndata import _gen_dataframe

        self._adata = adata
        self._n_obs = adata.n_obs
        # construct manually
        if adata.isbacked == (X is None):
            self._X = X
            self._var = _gen_dataframe(var, self.X.shape[1], ["var_names"])
            self._varm = AxisArrays(self, 1, varm)
        elif X is None:  # construct from adata
            self._X = adata.X.copy()
            self._var = adata.var.copy()
            self._varm = AxisArrays(self, 1, adata.varm.copy())
        elif adata.isbacked:
            raise ValueError("Cannot specify X if adata is backed") 
Example #18
Source File: gaussian.py    From edm2016 with Apache License 2.0 6 votes vote down vote up
def hessian_wrt_mean(self):
        """ The Hessian of the multivariate Gaussian w.r.t. its mean, potentially including the
        linear projection.

        :return: The Hessian w.r.t. the mean
        :rtype: float|np.ndarray|sp.spmatrix
        """
        if self.hessian_mean_cache is None:
            hessian = self.hessian
            if self.mean_lin_op is not None:
                if np.isscalar(hessian) and isinstance(self.mean_lin_op, IndexOperator):
                    # index operator preserves diagonality
                    hessian = sp.diags(self.mean_lin_op.rmatvec(hessian * np.ones(self.dim)), 0)
                elif np.isscalar(hessian):
                    hessian = hessian * np.eye(self.dim)
                    hessian = rmatvec_nd(self.mean_lin_op, hessian)
                else:
                    hessian = rmatvec_nd(self.mean_lin_op, hessian)
            self.hessian_mean_cache = hessian
        return self.hessian_mean_cache 
Example #19
Source File: gaussian.py    From edm2016 with Apache License 2.0 6 votes vote down vote up
def __init__(self, dim=None, mean=None, precision=None, mean_lin_op=None, support=None):
        """
        :param int dim: dimensionality of the distribution. If None, inferred from mean or precision
        :param float|np.ndarray|None mean: the mean, float or 1D array. If not supplied on init or
            call-time, assumed to be 0.  If scalar, assumes the mean is the same for all dimensions.
        :param float|np.ndarray|sp.spmatrix|None precision: the precision matrix.  If not supplied
            on init or at call-time, assumed to be the identity.
            If 1D, assumes that the diagonal of the precision matrix is passed in; if scalar
            and CPD dimensionality > 1, assumes that the precision matrix is precision * identity
            matrix.
        :param None|IndexOperator mean_lin_op: linear transform operator for the mean parameter,
            whose is shape is (dim, len(mean_vector))
        :param tuple(float) support: Defines the support for the probability distribution. Passed
            to solver pars updater to prevent the parameter from being set outside the range of the
            supports.
        """
        mean, precision, const = self._validate_args(dim=dim, mean=mean, precision=precision)
        self.mean = mean.ravel() if self.dim > 1 else mean
        self.precision = precision
        self.hessian_cache = None
        self.hessian_mean_cache = None
        self.const = const
        self.mean_lin_op = mean_lin_op
        self.support = support 
Example #20
Source File: linear_operators.py    From edm2016 with Apache License 2.0 6 votes vote down vote up
def rmatvec_nd(lin_op, x):
    """
    Project a 1D or 2D numpy or sparse array using rmatvec. This is different from rmatvec
    because it applies rmatvec to each row and column. If x is n x n and lin_op is n x k,
    the result will be k x k.

    :param LinearOperator lin_op: The linear operator to apply to x
    :param np.ndarray|sp.spmatrix x: array/matrix to be projected
    :return: the projected array
    :rtype: np.ndarray|sp.spmatrix
    """
    if x is None or lin_op is None:
        return x
    if isinstance(x, sp.spmatrix):
        y = x.toarray()
    elif np.isscalar(x):
        y = np.array(x, ndmin=1)
    else:
        y = np.copy(x)
    proj_func = lambda z: lin_op.rmatvec(z)
    for j in range(y.ndim):
        if y.shape[j] == lin_op.shape[0]:
            y = np.apply_along_axis(proj_func, j, y)
    return y 
Example #21
Source File: perturbation_attack.py    From node_embedding_attack with MIT License 6 votes vote down vote up
def baseline_eigencentrality_top_flips(adj_matrix, candidates, n_flips):
    """Selects the top (n_flips) number of flips using eigencentrality score of the edges.
    Applicable only when removing edges.

    :param adj_matrix: sp.spmatrix
        The graph represented as a sparse scipy matrix
    :param candidates: np.ndarray, shape [?, 2]
        Candidate set of edge flips
    :param n_flips: int
        Number of flips to select
    :return: np.ndarray, shape [?, 2]
        The top edge flips from the candidate set
    """
    edges = np.column_stack(sp.triu(adj_matrix, 1).nonzero())
    line_graph = construct_line_graph(adj_matrix)
    eigcentrality_scores = nx.eigenvector_centrality_numpy(nx.Graph(line_graph))
    eigcentrality_scores = {tuple(edges[k]): eigcentrality_scores[k] for k, v in eigcentrality_scores.items()}
    eigcentrality_scores = np.array([eigcentrality_scores[tuple(cnd)] for cnd in candidates])

    scores_argsrt = eigcentrality_scores.argsort()

    return candidates[scores_argsrt[-n_flips:]] 
Example #22
Source File: utils.py    From node_embedding_attack with MIT License 6 votes vote down vote up
def construct_line_graph(adj_matrix):
    """Construct a line graph from an undirected original graph.

    Parameters
    ----------
    adj_matrix : sp.spmatrix [n_samples ,n_samples]
        Symmetric binary adjacency matrix.

    Returns
    -------
    L : sp.spmatrix, shape [A.nnz/2, A.nnz/2]
        Symmetric binary adjacency matrix of the line graph.
    """
    N = adj_matrix.shape[0]
    edges = np.column_stack(sp.triu(adj_matrix, 1).nonzero())
    e1, e2 = edges[:, 0], edges[:, 1]

    I = sp.eye(N).tocsr()
    E1 = I[e1]
    E2 = I[e2]

    L = E1.dot(E1.T) + E1.dot(E2.T) + E2.dot(E1.T) + E2.dot(E2.T)

    return L - 2 * sp.eye(L.shape[0]) 
Example #23
Source File: sparse_dataset.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __getitem__(self, index: Union[Index, Tuple[()]]) -> Union[float, ss.spmatrix]:
        row, col = self._normalize_index(index)
        mtx = self.to_backed()
        return mtx[row, col] 
Example #24
Source File: sparse_dataset.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def value(self) -> ss.spmatrix:
        return self.to_memory() 
Example #25
Source File: sparse_dataset.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_format_str(data: ss.spmatrix) -> str:
    for fmt, _, memory_class in FORMATS:
        if isinstance(data, memory_class):
            return fmt
    raise ValueError(f"Data type {type(data)} is not supported.") 
Example #26
Source File: sparse_dataset.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def to_memory(self) -> ss.spmatrix:
        format_class = get_memory_class(self.format_str)
        mtx = format_class(self.shape, dtype=self.dtype)
        mtx.data = self.group["data"][...]
        mtx.indices = self.group["indices"][...]
        mtx.indptr = self.group["indptr"][...]
        return mtx 
Example #27
Source File: index.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _subset_spmatrix(a: spmatrix, subset_idx: Index):
    # Correcting for indexing behaviour of sparse.spmatrix
    if len(subset_idx) > 1 and all(isinstance(x, cabc.Iterable) for x in subset_idx):
        subset_idx = (subset_idx[0].reshape(-1, 1), *subset_idx[1:])
    return a[subset_idx] 
Example #28
Source File: test_base.py    From cupy with MIT License 5 votes vote down vote up
def test_instantiation(self):
        for sp in (scipy.sparse, sparse):
            with pytest.raises(ValueError):
                sp.spmatrix() 
Example #29
Source File: sparse_dataset.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def copy(self) -> ss.spmatrix:
        if isinstance(self.data, h5py.Dataset):
            return SparseDataset(self.data.parent).to_memory()
        else:
            return super().copy() 
Example #30
Source File: __init__.py    From anndata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _find_sparse_matrices(d: Mapping, n: int, keys: tuple, paths: list):
    """Find paths to sparse matrices with shape (n, n)."""
    for k, v in d.items():
        if isinstance(v, Mapping):
            _find_sparse_matrices(v, n, (*keys, k), paths)
        elif isinstance(v, spmatrix) and v.shape == (n, n):
            paths.append((*keys, k))
    return paths