def test_batch_mean_center(): X = data.generate_positive_sparse_matrix() sample_idx = np.random.choice([0, 1], X.shape[0], replace=True) X[sample_idx == 1] += 1 Y = X.copy() Y[sample_idx == 0] -= np.mean(Y[sample_idx == 0], axis=0)[None, :] Y[sample_idx == 1] -= np.mean(Y[sample_idx == 1], axis=0)[None, :] utils.assert_all_close(np.mean(Y[sample_idx == 0], axis=0), 0) utils.assert_all_close(np.mean(Y[sample_idx == 1], axis=0), 0) matrix.test_dense_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=partial( scprep.normalize.batch_mean_center, sample_idx=sample_idx)) matrix.test_sparse_matrix_types( X, utils.assert_transform_raises, transform=partial( scprep.normalize.batch_mean_center, sample_idx=sample_idx), exception=ValueError) X = data.generate_positive_sparse_matrix() Y = X.copy() Y -= np.mean(Y, axis=0)[None, :] matrix.test_dense_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=partial( scprep.normalize.batch_mean_center)) matrix.test_sparse_matrix_types( X, utils.assert_transform_raises, transform=partial( scprep.normalize.batch_mean_center), exception=ValueError)
def test_batch_mean_center_one_sample(self): Y = self.X.copy() Y -= np.mean(Y, axis=0)[None, :] matrix.test_dense_matrix_types( self.X, utils.assert_transform_equivalent, Y=Y, transform=scprep.normalize.batch_mean_center)
def test_batch_mean_center(self): X = self.X.copy() X[self.sample_idx == 1] += 1 Y = X.copy() Y[self.sample_idx == 0] -= np.mean(Y[self.sample_idx == 0], axis=0)[None, :] Y[self.sample_idx == 1] -= np.mean(Y[self.sample_idx == 1], axis=0)[None, :] utils.assert_all_close(np.mean(Y[self.sample_idx == 0], axis=0), 0) utils.assert_all_close(np.mean(Y[self.sample_idx == 1], axis=0), 0) matrix.test_dense_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=scprep.normalize.batch_mean_center, sample_idx=self.sample_idx)
def test_log_transform(): X = data.generate_positive_sparse_matrix() Y = np.log10(X + 1) matrix.test_all_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=10, ) Y = np.log(X + 1) matrix.test_all_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base="e", ) Y = np.log2(X + 1) matrix.test_all_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=2, ) Y = np.log2(X + 5) def test_fun(X): utils.assert_warns_message( RuntimeWarning, "log transform on sparse data requires pseudocount = 1", scprep.transform.log, data=X, base=2, pseudocount=5, ) matrix.test_sparse_matrix_types(X, test_fun) matrix.test_dense_matrix_types( X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=2, pseudocount=5, )
def test_dense(self): matrix.test_dense_matrix_types( self.X, utils.assert_transform_equals, Y=self.Y_random, transform=scprep.reduce.pca, n_components=100, seed=42, ) matrix.test_all_matrix_types( self.X, utils.assert_transform_equals, Y=self.Y_random, transform=scprep.reduce.pca, n_components=100, seed=42, method="dense", check=partial(utils.assert_all_close, atol=1e-10), )
def test_pca(): X = data.generate_positive_sparse_matrix(shape=[100, 1000]) Y = decomposition.PCA(100, random_state=42).fit_transform(X) matrix.test_dense_matrix_types(X, utils.assert_transform_equals, Y=Y, transform=scprep.reduce.pca, n_pca=100, seed=42) Y = decomposition.PCA(50, svd_solver='full').fit_transform(X) matrix.test_sparse_matrix_types(X, utils.assert_transform_equals, Y=Y, transform=scprep.reduce.pca, check=partial(utils.assert_all_close, rtol=1e-3, atol=1e-5), n_pca=50, svd_multiples=8, seed=42)
def test_log_transform(): X = data.generate_positive_sparse_matrix() Y = np.log10(X + 1) matrix.test_all_matrix_types(X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=10) Y = np.log(X + 1) matrix.test_all_matrix_types(X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base='e') Y = np.log2(X + 1) matrix.test_all_matrix_types(X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=2) Y = np.log2(X + 5) assert_warns_message( RuntimeWarning, "log transform on sparse data requires pseudocount = 1", scprep.transform.log, data=sparse.csr_matrix(X), base=2, pseudocount=5) assert_warns_message( RuntimeWarning, "log transform on sparse data requires pseudocount = 1", scprep.transform.log, data=pd.SparseDataFrame(X, default_fill_value=0.0), base=2, pseudocount=5) matrix.test_dense_matrix_types(X, utils.assert_transform_equivalent, Y=Y, transform=scprep.transform.log, base=2, pseudocount=5)