def downsample(data, percent):
n_genes = data.shape[<NUM_LIT:0>]<EOL>n_cells = data.shape[<NUM_LIT:1>]<EOL>new_data = data.copy()<EOL>total_count = float(data.sum())<EOL>to_remove = total_count*percent<EOL>cell_sums = data.sum(<NUM_LIT:0>).astype(float)<EOL>cell_gene_probs = data/cell_sums<EOL>cell_probs = np.array(cell_sums/total_count).flatten()<EOL>cells_selected = np.random.multinomial(to_remove, pvals=cell_probs)<EOL>for i, num_selected in enumerate(cells_selected):<EOL><INDENT>cell_gene = np.array(cell_gene_probs[:,i]).flatten()<EOL>genes_selected = np.random.multinomial(num_selected, pvals=cell_gene)<EOL>if sparse.issparse(data):<EOL><INDENT>genes_selected = sparse.csc_matrix(genes_selected).T<EOL><DEDENT>new_data[:,i] -= genes_selected<EOL><DEDENT>new_data[new_data < <NUM_LIT:0>] = <NUM_LIT:0><EOL>return new_data<EOL>
downsample the data by removing a given percentage of the reads.

Args:
    data: genes x cells array or sparse matrix
    percent: float between 0 and 1