

def _ensure_sparse_format(spmatrix, accept_sparse, dtype, order, copy, force_all_finite):
    "Convert a sparse matrix to a given format.\n    Checks the sparse format of spmatrix and converts if necessary.\n    Parameters\n    ----------\n    spmatrix : scipy sparse matrix\n        Input to validate and convert.\n    accept_sparse : string, list of string or None (default=None)\n        String[s] representing allowed sparse matrix formats ('csc',\n        'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). None means that sparse\n        matrix input will raise an error.  If the input is sparse but not in\n        the allowed format, it will be converted to the first listed format.\n    dtype : string, type or None (default=none)\n        Data type of result. If None, the dtype of the input is preserved.\n    order : 'F', 'C' or None (default=None)\n        Whether an array will be forced to be fortran or c-style.\n    copy : boolean (default=False)\n        Whether a forced copy will be triggered. If copy=False, a copy might\n        be triggered by a conversion.\n    force_all_finite : boolean (default=True)\n        Whether to raise an error on np.inf and np.nan in X.\n    Returns\n    -------\n    spmatrix_converted : scipy sparse matrix.\n        Matrix that is ensured to have an allowed type.\n    "
    if (accept_sparse is None):
        raise TypeError('A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.')
    sparse_type = spmatrix.format
    if (dtype is None):
        dtype = spmatrix.dtype
    if (sparse_type in accept_sparse):
        if (dtype == spmatrix.dtype):
            if copy:
                spmatrix = spmatrix.copy()
        else:
            spmatrix = spmatrix.astype(dtype)
    else:
        spmatrix = spmatrix.asformat(accept_sparse[0]).astype(dtype)
    if force_all_finite:
        if (not hasattr(spmatrix, 'data')):
            warnings.warn(("Can't check %s sparse matrix for nan or inf." % spmatrix.format))
        else:
            assert_all_finite(spmatrix.data)
    if hasattr(spmatrix, 'data'):
        spmatrix.data = np.array(spmatrix.data, copy=False, order=order)
    return spmatrix

dataset/ETHPy150Open ibayer/fastFM/fastFM/validation.py _ensure_sparse_format/original