scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
424 stars 76 forks source link

Ligand-receptor plot always report Error #871

Open ryrl970311 opened 1 month ago

ryrl970311 commented 1 month ago

Description

When I use sq.pl.ligrec after sq.gr.ligrec( adata, n_perms=100, cluster_key="cluster", clusters=["Polydendrocytes", "Oligodendrocytes"], ) , it always report an error 'ValueError: After removing rows with only NaN interactions, none remain'. Is thes normal? I use the data fro tutorials adata = sq.datasets.seqfish() or adata = sq.datasets.slideseqv2(), which do not raise error in examples. How should I solve it? Thanks.

Minimal reproducible example

adata = sq.datasets.seqfish() or adata = sq.datasets.slideseqv2() ...

sq.gr.ligrec( adata, n_perms=100, cluster_key="cluster", clusters=["Polydendrocytes", "Oligodendrocytes"], )

sq.pl.ligrec( adata, cluster_key="cluster", source_groups="Oligodendrocytes", target_groups=["Polydendrocytes"], pvalue_threshold=0.05, swap_axes=True, )

Traceback

ValueError Traceback (most recent call last) Cell In[56], line 1 ----> 1 sq.pl.ligrec( 2 adata, 3 cluster_key="cluster", 4 source_groups="Oligodendrocytes", 5 target_groups=["Polydendrocytes"], 6 pvalue_threshold=0.05, 7 swap_axes=True, 8 )

287 pvals = pvals[pvals <= pvalue_threshold]
289 if remove_empty_interactions:

--> 290 pvals, means = filter_values(pvals, means, mask=~(pd.isnull(means) | pd.isnull(pvals)), kind="NaN") 291 if remove_nonsig_interactions and alpha is not None: 292 pvals, means = filter_values(pvals, means, mask=pvals <= alpha, kind="non-significant")

197 means = means.loc[mask_rows]
199 if pvals.empty:

--> 200 raise ValueError(f"After removing rows with only {kind} interactions, none remain.") 202 mask_cols = mask.any(axis=0) 203 pvals = pvals.loc[:, mask_cols]

ValueError: After removing rows with only NaN interactions, none remain.

Version

python 3.12.4 squidpy==1.6.0

...

giovp commented 3 weeks ago

hi @ryrl970311 did you normalize and log1p the data before using this function?

noob000007 commented 3 weeks ago

I also encountered this error. In this new version, even the official example tutorial encounters the same issue.

TTTPOB commented 1 week ago

update: tried 1.5.0 from conda-forge and 1.6.1 self-built conda package (from pypi source) got the same error

same here, my code

# hidden code for read in the data
# then preprocess using standard scanpy pipeline
# standard scanpy pipeline for slice1
sc.pp.filter_cells(slice1, min_genes=200)
sc.pp.filter_genes(slice1, min_cells=3)
slice1.raw = slice1
slice1.layers["counts"] = slice1.X.copy()
sc.pp.normalize_total(slice1)
sc.pp.log1p(slice1)
sc.pp.highly_variable_genes(slice1, n_top_genes=2000)
sc.tl.pca(slice1)
sc.pp.neighbors(slice1)
sc.tl.umap(slice1)
sc.tl.leiden(slice1)

# then spatial scatterplot
# see it on the spatial image
sq.pl.spatial_scatter(
    slice1,
    img_res_key="lowres",
    color=["leiden"],
    size=12000,
)

image

# then do the ligand receptor analysis
sq.gr.ligrec(
    slice1,
    n_perms=100,
    cluster_key="leiden",
    use_raw=True,
)
# above runs normally,

# plot fails
sq.pl.ligrec(
    slice1,
    cluster_key="leiden",
    source_groups=["4"],
    target_groups=["1", "2"],
    means_range=(0.1, np.inf),
    alpha=1e-4,
    swap_axes=True,
)

traceback image

TTTPOB commented 1 week ago

update, running the code from the tutorial would fail as well (the dataset is from this tutorial as well) https://squidpy.readthedocs.io/en/stable/notebooks/tutorials/tutorial_visium_hne.html#import-packages-data

sq.gr.ligrec(
    adata,
    n_perms=100,
    cluster_key="cluster",
)
sq.pl.ligrec(
    adata,
    cluster_key="cluster",
    source_groups="Hippocampus",
    target_groups=["Pyramidal_layer", "Pyramidal_layer_dentate_gyrus"],
    means_range=(3, np.inf),
    alpha=1e-4,
    swap_axes=True,
)