axelalmet / flowsig

Python package to find communication-driven intercellular flows from single-cell RNA-sequencing and spatial transcriptomics data.
MIT License
56 stars 0 forks source link

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). #18

Open 22219098 opened 1 month ago

22219098 commented 1 month ago

Hi, we met the problem when we ran code with our own adata: AnnData object with n_obs × n_vars = 28374 × 25086 obs: 'cell_subtype', 'batch', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt' var: 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts' uns: 'cellchat_output', 'log1p', 'pyliger_info', 'flowsig_network', 'flowsig_network_orig' obsm: 'X_gem', 'X_flow', 'X_flow_orig' layers: 'counts'

fs.tl.apply_biological_flow(adata,
                            flowsig_network_key = 'flowsig_network',
                            adjacency_key = 'adjacency',
                            validated_key = 'validated')

This is the error report:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_83050/680968092.py in ?()
----> 1 fs.tl.apply_biological_flow(adata,
      2                             flowsig_network_key = 'flowsig_network',
      3                             adjacency_key = 'adjacency',
      4                             validated_key = 'validated')

~/.conda/envs/flowsigenv/lib/python3.8/site-packages/flowsig/tools/_validate_network.py in ?(adata, flowsig_network_key, adjacency_key, validated_key)
    183         # Define the edge because we may need to reverse it
    184         edge = (node_1, node_2)
    185 
    186         # If there's a link from received morphogen to a TF
--> 187         if ( (node_1_type == 'inflow')&(node_2_type == 'module') ):
    188 
    189             add_edge = True
    190 

~/.conda/envs/flowsigenv/lib/python3.8/site-packages/pandas/core/generic.py in ?(self)
   1464     @final
   1465     def __nonzero__(self) -> NoReturn:
-> 1466         raise ValueError(
   1467             f"The truth value of a {type(self).__name__} is ambiguous. "
   1468             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1469         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

But when we tried to run tutorial on example burkhardt21_merged.h5ad, this error did not occur. AnnData object with n_obs × n_vars = 5305 × 18027 obs: 'sample_labels', 'Donor', 'Condition', 'library_size', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'doublet_score', 'predicted_doublet', 'n_counts', 'log_counts', 'n_genes', 'leiden', 'Type' var: 'gene_symbols', 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells', 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'highly_variable_nbatches', 'highly_variable_intersection' uns: 'Condition', 'Condition_colors', 'Donor_colors', 'NMF_10', 'NMF_CV', 'Type', 'base_networks', 'base_networks_leiden', 'causal_networks', 'causal_networks_leiden', 'cellchat_output', 'cpdb_Type', 'flowsig_network', 'flowsig_network_cpdb', 'flowsig_network_cpdb_orig', 'flowsig_network_orig', 'hvg', 'learned_networks', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'pyliger', 'pyliger_10', 'pyliger_11', 'pyliger_12', 'pyliger_15', 'pyliger_20', 'pyliger_3', 'pyliger_5', 'pyliger_8', 'pyliger_9', 'pyliger_info', 'pyliger_vars', 'sample_labels_colors', 'scrublet', 'umap' obsm: 'X_SC', 'X_celltype_ligand', 'X_celltype_ligand_leiden', 'X_flow', 'X_flow_cpdb', 'X_flow_cpdb_orig', 'X_flow_orig', 'X_gem', 'X_pca', 'X_umap' varm: 'PCs' layers: 'counts', 'normalized' obsp: 'connectivities', 'distances'

My environment is : Package Version


absl-py 2.1.0 adjustText 1.2.0 aiohappyeyeballs 2.4.0 aiohttp 3.10.5 aiosignal 1.3.1 anndata 0.9.2 annoy 1.17.3 asciitree 0.3.3 asttokens 2.0.5 astunparse 1.6.3 async-timeout 4.0.3 attrs 24.2.0 backcall 0.2.0 backports.zoneinfo 0.2.1 biothings-client 0.3.1 bleach 6.1.0 bokeh 3.1.1 cachetools 5.5.0 causaldag 0.1a163 certifi 2024.8.30 charset-normalizer 3.3.2 click 8.1.7 cloudpickle 3.0.0 colorcet 3.1.0 comm 0.2.1 conditional-independence 0.1a6 contourpy 1.1.1 cycler 0.12.1 dask 2023.5.0 dask-image 2023.3.0 dataclasses 0.6 datashader 0.15.2 datashape 0.5.2 debugpy 1.6.7 decorator 5.1.1 dill 0.3.8 dm-tree 0.1.8 docopt 0.6.2 docrep 0.3.2 einops 0.8.0 et-xmlfile 1.1.0 executing 0.8.3 fasteners 0.19 filelock 3.16.1 flatbuffers 24.3.25 flowsig 0.1.1 fonttools 4.54.0 frozendict 2.4.4 frozenlist 1.4.1 fsspec 2024.9.0 ftpretty 0.4.0 gast 0.4.0 get-annotations 0.1.2 goatools 1.4.12 google-ai-generativelanguage 0.1.0 google-api-core 2.20.0 google-auth 2.35.0 google-auth-oauthlib 1.0.0 google-generativeai 0.1.0rc1 google-pasta 0.2.0 googleapis-common-protos 1.65.0 graphical-model-learning 0.1a8 graphical-models 0.1a21 grpcio 1.66.1 grpcio-status 1.62.3 h5py 3.11.0 h5sparse 0.1.0 holoviews 1.17.1 idna 3.10 igraph 0.11.6 imageio 2.35.1 importlib-metadata 7.0.1 importlib_resources 6.4.5 inflect 7.0.0 ipdb 0.13.13 ipykernel 6.28.0 ipython 8.12.2 jedi 0.19.1 Jinja2 3.1.4 joblib 1.4.2 jupyter_client 8.6.0 jupyter_core 5.7.2 keras 2.13.1 kiwisolver 1.4.7 lazy_loader 0.4 leidenalg 0.10.2 libclang 18.1.1 linkify-it-py 2.0.3 llvmlite 0.41.1 locket 1.0.0 louvain 0.8.2 Markdown 3.7 markdown-it-py 3.0.0 MarkupSafe 2.1.5 matplotlib 3.7.5 matplotlib-inline 0.1.6 matplotlib-scalebar 0.8.1 mdit-py-plugins 0.4.2 mdurl 0.1.2 mizani 0.9.3 mpmath 1.3.0 multidict 6.1.0 multipledispatch 1.0.0 mygene 3.2.2 natsort 8.4.0 nest-asyncio 1.6.0 networkx 3.1 numba 0.58.1 numcodecs 0.12.1 numexpr 2.8.6 numpy 1.24.3 nvidia-cublas-cu12 12.1.3.1 nvidia-cuda-cupti-cu12 12.1.105 nvidia-cuda-nvrtc-cu12 12.1.105 nvidia-cuda-runtime-cu12 12.1.105 nvidia-cudnn-cu12 8.9.2.26 nvidia-cufft-cu12 11.0.2.54 nvidia-curand-cu12 10.3.2.106 nvidia-cusolver-cu12 11.4.5.107 nvidia-cusparse-cu12 12.1.0.106 nvidia-nccl-cu12 2.18.1 nvidia-nvjitlink-cu12 12.6.68 nvidia-nvtx-cu12 12.1.105 oauthlib 3.2.2 omnipath 1.0.8 openpyxl 3.1.5 opt-einsum 3.3.0 packaging 24.1 pandas 2.0.3 panel 1.2.3 param 2.1.1 parso 0.8.3 partd 1.4.1 patsy 0.5.6 pexpect 4.8.0 pgmpy 0.1.26 pickleshare 0.7.5 pillow 10.4.0 PIMS 0.7 pip 24.2 platformdirs 3.10.0 plotnine 0.12.4 progressbar2 4.5.0 prompt-toolkit 3.0.43 proto-plus 1.24.0 protobuf 4.25.5 psutil 5.9.0 ptyprocess 0.7.0 pure-eval 0.2.2 pyasn1 0.6.1 pyasn1_modules 0.4.1 pyct 0.5.0 pydantic 1.10.18 pydot 3.0.1 pygam 0.9.1 Pygments 2.15.1 pyliger 0.2.0 pynndescent 0.5.13 pyparsing 3.1.4 python-dateutil 2.9.0.post0 python-igraph 0.11.6 python-utils 3.8.2 pytz 2024.2 pyviz_comms 3.0.3 PyWavelets 1.4.1 PyYAML 6.0.2 pyzmq 25.1.2 requests 2.32.3 requests-oauthlib 2.0.0 rich 13.8.1 rsa 4.9 scanpy 1.9.8 scikit-image 0.21.0 scikit-learn 1.3.2 scipy 1.10.1 seaborn 0.13.2 session-info 1.0.0 setuptools 75.1.0 six 1.16.0 slicerator 1.1.0 spatial-factorization 0.0.1 squidpy 1.2.2 stack-data 0.2.0 statsmodels 0.14.1 stdlib-list 0.10.0 sympy 1.13.3 tensorboard 2.13.0 tensorboard-data-server 0.7.2 tensorflow 2.13.1 tensorflow-estimator 2.13.0 tensorflow-io-gcs-filesystem 0.34.0 tensorflow-probability 0.21.0 termcolor 2.4.0 texttable 1.7.0 threadpoolctl 3.5.0 tifffile 2023.7.10 tomli 2.0.1 toolz 0.12.1 torch 2.1.2 tornado 6.4.1 tqdm 4.66.5 traitlets 5.14.3 triton 2.1.0 typing 3.7.4.3 typing_extensions 4.5.0 tzdata 2024.2 uc-micro-py 1.0.3 umap-learn 0.5.6 urllib3 2.2.3 validators 0.34.0 wcwidth 0.2.5 webencodings 0.5.1 Werkzeug 3.0.4 wheel 0.44.0 wrapt 1.16.0 xarray 2023.1.0 xgboost 2.1.1 XlsxWriter 3.2.0 xyzservices 2024.9.0 yarl 1.12.1 zarr 2.16.1 zipp 3.17.0

Thanks and best wishes

Zhaowado commented 3 weeks ago

Yes, I also encountered this problem.Have you solved it? This error is usually caused by the inconsistency of the data types used for comparison.I guess it may be because the input node_1_type or node_2_type are not all str types. However, I still haven't solved it, hope to get a reply too.

zhangpan-2018 commented 3 weeks ago

I also encountered a similar issue where the node_1_type/node_2_type was not a string, and it could be both an inflow and outflow at the same time, which caused the error. I later modified it to only take the first one, which solved the problem. Here is the specific modification:

I changed the part of the construct_intercellular_flow_network function where: node_1_type = flow_var_info.loc[node_1]['Type'] node_2_type = flow_var_info.loc[node_2]['Type']

to:

Classify node types

temp_type1 = flow_var_info.loc[node_1]['Type'] node_1_type = temp_type1.values[0] if not isinstance(temp_type1, str) else temp_type1 temp_type2 = flow_var_info.loc[node_2]['Type'] node_2_type = temp_type2.values[0] if not isinstance(temp_type2, str) else temp_type2

shisang7 commented 1 week ago

I seem to have solved this problem by the definition of apply_biological_flow