Skip to content

asCOO

Convert the input to sparse.COO. Passes through sparse.COO objects as-is.

Parameters:

Name Type Description Default
x Union[SparseArray, spmatrix, ndarray]

The input array to convert.

required
name str

The name of the operation to use in the exception.

'asCOO'
check bool_

Whether to check for a dense input.

True

Returns:

Type Description
COO

The converted sparse.COO array.

Raises:

Type Description
ValueError

If check is true and a dense input is supplied.

Source code in sparse/numba_backend/_coo/common.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def asCOO(x, name="asCOO", check=True):
    """
    Convert the input to [`sparse.COO`][]. Passes through [`sparse.COO`][] objects as-is.

    Parameters
    ----------
    x : Union[SparseArray, scipy.sparse.spmatrix, numpy.ndarray]
        The input array to convert.
    name : str, optional
        The name of the operation to use in the exception.
    check : bool, optional
        Whether to check for a dense input.

    Returns
    -------
    COO
        The converted [`sparse.COO`][] array.

    Raises
    ------
    ValueError
        If `check` is true and a dense input is supplied.
    """
    from .._common import _is_sparse
    from .core import COO

    if check and not _is_sparse(x):
        raise ValueError(f"Performing this operation would produce a dense result: {name}")

    if not isinstance(x, COO):
        x = COO(x)

    return x