Exemple #1
0
def test_wilcoxon_tie_correction(reference):
    pbmc = pbmc68k_reduced()

    groups = ['CD14+ Monocyte', 'Dendritic']
    groupby = 'bulk_labels'

    _, groups_masks = select_groups(pbmc, groups, groupby)

    X = pbmc.raw.X[groups_masks[0]].toarray()

    mask_rest = groups_masks[1] if reference else ~groups_masks[0]
    Y = pbmc.raw.X[mask_rest].toarray()

    n_genes = X.shape[1]

    pvals = np.zeros(n_genes)

    for i in range(n_genes):
        try:
            _, pvals[i] = mannwhitneyu(X[:, i], Y[:, i], False, 'two-sided')
        except ValueError:
            pvals[i] = 1

    if reference:
        ref = groups[1]
    else:
        ref = 'rest'
        groups = groups[:1]

    test_obj = _RankGenes(pbmc, groups, groupby, reference=ref)
    test_obj.compute_statistics('wilcoxon', tie_correct=True)

    assert np.allclose(test_obj.stats[groups[0]]['pvals'], pvals)
def test_wilcoxon_symmetry():
    pbmc = pbmc68k_reduced()

    rank_genes_groups(
        pbmc,
        groupby="bulk_labels",
        groups=["CD14+ Monocyte", "Dendritic"],
        reference="Dendritic",
        method='wilcoxon',
        rankby_abs=True,
    )

    stats_mono = (rank_genes_groups_df(
        pbmc, group="CD14+ Monocyte").drop(columns="names").to_numpy())

    rank_genes_groups(
        pbmc,
        groupby="bulk_labels",
        groups=["CD14+ Monocyte", "Dendritic"],
        reference="CD14+ Monocyte",
        method='wilcoxon',
        rankby_abs=True,
    )

    stats_dend = (rank_genes_groups_df(
        pbmc, group="Dendritic").drop(columns="names").to_numpy())

    assert np.allclose(np.abs(stats_mono), np.abs(stats_dend))
def test_emptycat():
    pbmc = pbmc68k_reduced()
    pbmc.obs['louvain'] = pbmc.obs['louvain'].cat.add_categories(['11'])

    with pytest.raises(ValueError,
                       match=rf"Could not calculate statistics.*{'11'}"):
        rank_genes_groups(pbmc, groupby='louvain')
Exemple #4
0
def test_py2rpy2_numpy_pbmc68k():
    """This has some weird metadata"""
    from scanpy.datasets import pbmc68k_reduced

    try:
        anndata2ri.activate()
        with catch_warnings(record=True) as logs:  # type: List[WarningMessage]
            simplefilter("ignore", DeprecationWarning)
            globalenv["adata"] = pbmc68k_reduced()
        assert len(logs) == 0, [m.message for m in logs]
    finally:
        anndata2ri.deactivate()
Exemple #5
0
def test_py2rpy2_numpy_pbmc68k():
    """This has some weird metadata"""
    from scanpy.datasets import pbmc68k_reduced

    try:
        anndata2ri.activate()
        with catch_warnings(record=True) as logs:  # type: List[WarningMessage]
            simplefilter("ignore", DeprecationWarning)
            globalenv["adata"] = pbmc68k_reduced()
        assert len(logs) == 1, [m.message for m in logs]
        assert logs[0].category is NotConvertedWarning
        assert "scipy.sparse.csr.csr_matrix" in str(logs[0].message)
    finally:
        anndata2ri.deactivate()
Exemple #6
0
def test_wilcoxon_tie_correction(reference):
    pbmc = pbmc68k_reduced()

    groups = ['CD14+ Monocyte', 'Dendritic']
    groupby = 'bulk_labels'

    _, groups_masks = select_groups(pbmc, groups, groupby)

    X = pbmc.raw.X[groups_masks[0]].toarray()

    mask_rest = groups_masks[1] if reference else ~groups_masks[0]
    Y = pbmc.raw.X[mask_rest].toarray()

    # Handle scipy versions
    if version.parse(scipy.__version__) >= version.parse("1.7.0"):
        pvals = mannwhitneyu(X,
                             Y,
                             use_continuity=False,
                             alternative='two-sided').pvalue
        pvals[np.isnan(pvals)] = 1.0
    else:
        # Backwards compat, to drop once we drop scipy < 1.7
        n_genes = X.shape[1]
        pvals = np.zeros(n_genes)

        for i in range(n_genes):
            try:
                _, pvals[i] = mannwhitneyu(X[:, i],
                                           Y[:, i],
                                           use_continuity=False,
                                           alternative='two-sided')
            except ValueError:
                pvals[i] = 1

    if reference:
        ref = groups[1]
    else:
        ref = 'rest'
        groups = groups[:1]

    test_obj = _RankGenes(pbmc, groups, groupby, reference=ref)
    test_obj.compute_statistics('wilcoxon', tie_correct=True)

    np.testing.assert_allclose(test_obj.stats[groups[0]]['pvals'], pvals)
Exemple #7
0
def test_filter_rank_genes_groups():
    adata = pbmc68k_reduced()

    # fix filter defaults
    args = {
        'adata': adata,
        'key_added': 'rank_genes_groups_filtered',
        'min_in_group_fraction': 0.25,
        'min_fold_change': 1,
        'max_out_group_fraction': 0.5,
    }

    rank_genes_groups(adata,
                      'bulk_labels',
                      reference='Dendritic',
                      method='wilcoxon',
                      n_genes=5)
    filter_rank_genes_groups(**args)

    assert np.array_equal(
        names_reference,
        np.array(adata.uns['rank_genes_groups_filtered']['names'].tolist()),
    )

    rank_genes_groups(adata, 'bulk_labels', method='wilcoxon', n_genes=5)
    filter_rank_genes_groups(**args)

    assert np.array_equal(
        names_no_reference,
        np.array(adata.uns['rank_genes_groups_filtered']['names'].tolist()),
    )

    rank_genes_groups(adata,
                      'bulk_labels',
                      method='wilcoxon',
                      pts=True,
                      n_genes=5)
    filter_rank_genes_groups(**args)

    assert np.array_equal(
        names_no_reference,
        np.array(adata.uns['rank_genes_groups_filtered']['names'].tolist()),
    )