Perform the exact kb-binning procedure on a sparse matrix.

def bin_kb_sparse(M, positions, length=10):
    """Perform the exact kb-binning procedure on a sparse matrix.
    """

    try:
        from scipy.sparse import coo_matrix
    except ImportError as e:
        print(str(e))
        print("I am peforming dense normalization by default.")
        return bin_kb_dense(M.todense(), positions=positions)
    r = M.tocoo()
    unit = 10**3
    ul = unit * length
    units = positions / ul
    n = len(positions)
    indices = np.floor(units)
    row = [indices[np.floor(i)] for i in r.row / ul]
    col = [indices[np.floor(j)] for j in r.col / ul]
    binned_indices = positions[
        [i for i in range(n - 1) if np.ceil(units[i]) < np.ceil(units[i + 1])]]
    return coo_matrix((r.data, (row, col))), binned_indices