Example #1
0
def test_combine_batches():
    X = data.load_10X()
    Y = pd.concat([X, scprep.utils.select_rows(X, np.arange(X.shape[0] // 2))])
    Y2, sample_labels = scprep.utils.combine_batches(
        [X, scprep.utils.select_rows(X, np.arange(X.shape[0] // 2))],
        batch_labels=[0, 1])
    assert utils.assert_matrix_class_equivalent(Y, Y2)
    utils.assert_all_equal(Y, Y2)
    assert np.all(Y.index == Y2.index)
    assert np.all(sample_labels == np.concatenate(
        [np.repeat(0, X.shape[0]),
         np.repeat(1, X.shape[0] // 2)]))
    Y2, sample_labels = scprep.utils.combine_batches(
        [X, scprep.utils.select_rows(X, np.arange(X.shape[0] // 2))],
        batch_labels=[0, 1],
        append_to_cell_names=True)
    assert np.all(Y.index == np.array([i[:-2] for i in Y2.index]))
    assert np.all(
        np.core.defchararray.add("_", sample_labels.astype(str)) == np.array(
            [i[-2:] for i in Y2.index], dtype=str))
    transform = lambda X: scprep.utils.combine_batches(
        [X, scprep.utils.select_rows(X, np.arange(X.shape[0] // 2))],
        batch_labels=[0, 1])[0]
    matrix.test_matrix_types(X,
                             utils.assert_transform_equals,
                             matrix._indexable_matrix_types,
                             Y=Y,
                             transform=transform,
                             check=utils.assert_all_equal)
Example #2
0
 def test_singular_values_dense(self):
     utils.assert_all_equal(
         self.S_random,
         scprep.reduce.pca(self.X,
                           n_components=100,
                           seed=42,
                           return_singular_values=True)[1],
     )
Example #3
0
 def test_setup(self):
     assert self.Y.shape[0] == self.X_dense.shape[0]
     utils.assert_all_equal(
         self.Y, scprep.select.select_cols(self.X_dense, idx="Arl8b")
     )
Example #4
0
def test_combine_batches():
    X = data.load_10X()
    Y = pd.concat(
        [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
        axis=0,
        sort=True,
    )
    Y2, sample_labels = scprep.utils.combine_batches(
        [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
        batch_labels=[0, 1],
        append_to_cell_names=False,
    )
    assert utils.assert_matrix_class_equivalent(Y, Y2)
    utils.assert_all_equal(Y, Y2)
    assert np.all(Y.index == Y2.index)
    assert np.all(
        sample_labels
        == np.concatenate([np.repeat(0, X.shape[0]), np.repeat(1, X.shape[0] // 2)])
    )
    assert np.all(sample_labels.index == Y2.index)
    assert sample_labels.name == "sample_labels"
    Y2, sample_labels = scprep.utils.combine_batches(
        [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
        batch_labels=[0, 1],
        append_to_cell_names=True,
    )
    assert np.all(Y.index == np.array([i[:-2] for i in Y2.index]))
    assert np.all(
        np.core.defchararray.add("_", sample_labels.astype(str))
        == np.array([i[-2:] for i in Y2.index], dtype=str)
    )
    assert np.all(sample_labels.index == Y2.index)
    assert sample_labels.name == "sample_labels"

    def transform(X):
        return scprep.utils.combine_batches(
            [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
            batch_labels=[0, 1],
        )[0]

    matrix.test_matrix_types(
        X,
        utils.assert_transform_equals,
        matrix._pandas_matrix_types,
        Y=Y,
        transform=transform,
        check=utils.assert_all_equal,
    )
    # don't sort for non pandas
    Y = pd.concat(
        [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
        axis=0,
        sort=False,
    )
    matrix.test_matrix_types(
        X,
        utils.assert_transform_equals,
        matrix._scipy_indexable_matrix_types + matrix._numpy_matrix_types,
        Y=Y,
        transform=transform,
        check=utils.assert_all_equal,
    )

    def test_fun(X):
        Y, sample_labels = scprep.utils.combine_batches(
            [X, scprep.select.select_rows(X, idx=np.arange(X.shape[0] // 2))],
            batch_labels=[0, 1],
        )
        assert np.all(sample_labels.index == Y.index)
        assert sample_labels.name == "sample_labels"

    matrix.test_pandas_matrix_types(X, test_fun)