def bin_kb_sparse(M, positions, length=<NUM_LIT:10>):
try:<EOL><INDENT>from scipy.sparse import coo_matrix<EOL><DEDENT>except ImportError as e:<EOL><INDENT>print(str(e))<EOL>print("<STR_LIT>")<EOL>return bin_kb_dense(M.todense(), positions=positions)<EOL><DEDENT>r = M.tocoo()<EOL>unit = <NUM_LIT:10>**<NUM_LIT:3><EOL>ul = unit * length<EOL>units = positions / ul<EOL>n = len(positions)<EOL>indices = np.floor(units)<EOL>row = [indices[np.floor(i)] for i in r.row / ul]<EOL>col = [indices[np.floor(j)] for j in r.col / ul]<EOL>binned_indices = positions[<EOL>[i for i in range(n - <NUM_LIT:1>) if np.ceil(units[i]) < np.ceil(units[i + <NUM_LIT:1>])]]<EOL>return coo_matrix((r.data, (row, col))), binned_indices<EOL>
Perform the exact kb-binning procedure on a sparse matrix.