def initialize_means_weights(data, clusters, init_means=None, init_weights=None, initialization='<STR_LIT>', max_assign_weight=<NUM_LIT>):
genes, cells = data.shape<EOL>if init_means is None:<EOL><INDENT>if init_weights is not None:<EOL><INDENT>if len(init_weights.shape)==<NUM_LIT:1>:<EOL><INDENT>means = initialize_means(data, init_weights, clusters)<EOL><DEDENT>else:<EOL><INDENT>means = initialize_means(data, init_weights.argmax(<NUM_LIT:0>),<EOL>clusters, max_assign_weight=max_assign_weight)<EOL><DEDENT><DEDENT>elif initialization=='<STR_LIT>':<EOL><INDENT>assignments, means = poisson_cluster(data, clusters)<EOL>if init_weights is None:<EOL><INDENT>init_weights = initialize_from_assignments(assignments, clusters,<EOL>max_assign_weight=max_assign_weight)<EOL><DEDENT><DEDENT>elif initialization=='<STR_LIT>':<EOL><INDENT>means, assignments = kmeans_pp(data, clusters)<EOL><DEDENT>elif initialization=='<STR_LIT>':<EOL><INDENT>km = KMeans(clusters)<EOL>assignments = km.fit_predict(log1p(cell_normalize(data)).T)<EOL>init_weights = initialize_from_assignments(assignments, clusters,<EOL>max_assign_weight)<EOL>means = initialize_means(data, assignments, clusters)<EOL><DEDENT>elif initialization=='<STR_LIT>':<EOL><INDENT>n_components = min(<NUM_LIT:50>, genes-<NUM_LIT:1>)<EOL>km = KMeans(clusters)<EOL>U, Sigma, VT = randomized_svd(log1p(cell_normalize(data)).T,<EOL>n_components)<EOL>data_reduced = U*Sigma<EOL>assignments = km.fit_predict(data_reduced)<EOL>init_weights = initialize_from_assignments(assignments, clusters,<EOL>max_assign_weight)<EOL>means = initialize_means(data, assignments, clusters)<EOL><DEDENT>elif initialization == '<STR_LIT>' or initialization == '<STR_LIT>':<EOL><INDENT>selected_cells = np.random.choice(range(cells), size=clusters,<EOL>replace=False)<EOL>means = data[:, selected_cells]<EOL>if sparse.issparse(means):<EOL><INDENT>means = means.toarray()<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>means = init_means.copy()<EOL><DEDENT>means = means.astype(float)<EOL>if init_weights is None:<EOL><INDENT>if init_means is not None:<EOL><INDENT>if initialization == '<STR_LIT>':<EOL><INDENT>assignments, means = poisson_cluster(data, clusters,<EOL>init=init_means, max_iters=<NUM_LIT:1>)<EOL>w_init = initialize_from_assignments(assignments, clusters,<EOL>max_assign_weight)<EOL><DEDENT>elif initialization == '<STR_LIT>':<EOL><INDENT>km = KMeans(clusters, init=log1p(init_means.T), max_iter=<NUM_LIT:1>)<EOL>assignments = km.fit_predict(log1p(cell_normalize(data)).T)<EOL>w_init = initialize_from_assignments(assignments, clusters,<EOL>max_assign_weight)<EOL><DEDENT>else:<EOL><INDENT>w_init = np.random.random((clusters, cells))<EOL>w_init = w_init/w_init.sum(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>w_init = np.random.random((clusters, cells))<EOL>w_init = w_init/w_init.sum(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(init_weights.shape)==<NUM_LIT:1>:<EOL><INDENT>init_weights = initialize_from_assignments(init_weights, clusters,<EOL>max_assign_weight)<EOL><DEDENT>w_init = init_weights.copy()<EOL><DEDENT>return means, w_init<EOL>
Generates initial means and weights for state estimation.