LouisFaure / scFates

a scalable python suite for tree inference and advanced pseudotime analysis from scRNAseq data.
https://scfates.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
47 stars 1 forks source link

Errors with scf.pl.trends #4

Closed Qotov closed 2 years ago

Qotov commented 3 years ago

Hello, thanks for your lib, it is very useful.

Starting from v.0.2.2 I get several errors when I try to use scf.pl.trends.

The function works well for the same datasets on v.0.2.0

First error:

 ZeroDivisionError: Weights sum to zero, can't be normalized 
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-100-2b04c2c201f7> in <module>
      1 scf.pl.trends(adata_bf1,module='late', branch='19',annot='milestones',
----> 2               root_milestone="7",milestones=["18","19"], basis='palantir')

~/jupytervenv2/lib/python3.7/site-packages/scFates/plot/features.py in trends(adata, features, highlight_features, n_features, root_milestone, milestones, module, branch, annot, title, feature_cmap, pseudo_cmap, plot_emb, basis, heatmap_space, offset_names, fontsize, ordering, ord_thre, filter_complex, complex_thre, complex_z, fig_heigth, show, save, save_genes, **kwargs)
    491             root_milestone=root_milestone,
    492             milestones=milestones,
--> 493             **kwargs,
    494         )
    495 

~/jupytervenv2/lib/python3.7/site-packages/scFates/plot/trajectory.py in trajectory(adata, basis, root_milestone, milestones, color_seg, cmap_seg, layer_seg, perc_seg, color_cells, scale_path, arrows, arrow_offset, show_info, ax, show, save, **kwargs)
    314         return np.average(vals, weights=R[:, i])
    315 
--> 316     node_vals = np.array(list(map(get_nval, range(R.shape[1]))))
    317     seg_val = node_vals[edges].mean(axis=1)
    318 

~/jupytervenv2/lib/python3.7/site-packages/scFates/plot/trajectory.py in get_nval(i)
    312 
    313     def get_nval(i):
--> 314         return np.average(vals, weights=R[:, i])
    315 
    316     node_vals = np.array(list(map(get_nval, range(R.shape[1]))))

<__array_function__ internals> in average(*args, **kwargs)

~/jupytervenv2/lib/python3.7/site-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
    408         if np.any(scl == 0.0):
    409             raise ZeroDivisionError(
--> 410                 "Weights sum to zero, can't be normalized")
    411 
    412         avg = np.multiply(a, wgt, dtype=result_dtype).sum(axis)/scl

ZeroDivisionError: Weights sum to zero, can't be normalized

Second:

IndexError: too many indices for array: array is 1-dimensional, but 9 were indexed 
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-98-6deca44aaf22> in <module>
      1 scf.pl.trends(adata_bf2_2,basis='palantir',branch='0',annot='milestones',
----> 2               root_milestone="2",milestones=["0","5"])

~/jupytervenv2/lib/python3.7/site-packages/scFates/plot/features.py in trends(adata, features, highlight_features, n_features, root_milestone, milestones, module, branch, annot, title, feature_cmap, pseudo_cmap, plot_emb, basis, heatmap_space, offset_names, fontsize, ordering, ord_thre, filter_complex, complex_thre, complex_z, fig_heigth, show, save, save_genes, **kwargs)
    491             root_milestone=root_milestone,
    492             milestones=milestones,
--> 493             **kwargs,
    494         )
    495 

~/jupytervenv2/lib/python3.7/site-packages/scFates/plot/trajectory.py in trajectory(adata, basis, root_milestone, milestones, color_seg, cmap_seg, layer_seg, perc_seg, color_cells, scale_path, arrows, arrow_offset, show_info, ax, show, save, **kwargs)
    315 
    316     node_vals = np.array(list(map(get_nval, range(R.shape[1]))))
--> 317     seg_val = node_vals[edges].mean(axis=1)
    318 
    319     if perc_seg is not None:

IndexError: too many indices for array: array is 1-dimensional, but 9 were indexed
LouisFaure commented 3 years ago

Thanks for using scFates and raising this issue!

Indeed the strategy to color the edges has been changed and should be much more robust from version v0.2.4, as it uses the soft assignment matrix (denoted R in the code) between cells and nodes of the principal graph to perform weighted average of a given feature. This is calculated per node and then edge value is calculated as the mean of the two nodes that compose it.

The first error tells that one or more node don't seem to be assigned to any cells, which shouldn't happen normally. Could you tell me which method did you used for building the graph, ppt or elpigraph? When running pl.graph, does the graph look in one piece? As for the second error, this looks less clear to me but I would think that this is a similar issue related to the tree, which is affecting the edge indexing.

For both errors I would suggest to re-run tl.tree with different parameter in order to avoid any broken tree or lonely nodes. To verify that the nodes all are assigned to any cells, I would check if the following is True:

all(adata.uns["graph"]["R"].sum(axis=0)>0)

In any case I would also be happy to have a look myself at your two datasets if you could send me anonymised versions of these, so that I can reproduce an debug these errors!

LouisFaure commented 2 years ago

I assume the issue as been resolved, I close it!

vravik commented 1 year ago

Hello,

I keep running into an error when I try to run this function. This is the message I am getting:

IndexError Traceback (most recent call last) Cell In[142], line 2 1 for c in slide.var["cluters"].unique(): ----> 2 scf.pl.trends(slide,features=slide.varnames[slide.var['cluters']==c], 3 basis="pca", save = f'cluster{c}_trends', n_features = 15)

File /nfs/turbo/umms-ukarvind/vravik/cell2loc/lib/python3.9/site-packages/scFates/plot/features.py:210, in trends(adata, features, cluster, highlight_features, n_features, root_milestone, milestones, module, branch, annot, title, feature_cmap, pseudo_cmap, plot_emb, plot_heatmap, wspace, show_segs, basis, heatmap_space, offset_names, fontsize, style, ordering, ord_thre, figsize, axemb, show, output_mean, save, return_genes, **kwargs) 205 order = order + np.array(allpaths[i])[~np.isin(allpaths[i], order)].tolist() 207 order = np.array(order)[1:] 209 order = pd.Series(graph["milestones"].keys(), index=graph["milestones"].values())[ --> 210 np.array(img.vs["name"])[order].astype(int) 211 ] 212 to_mil = graph["pp_seg"].loc[adata.obs.seg.cat.categories.astype(int), "to"] 213 seg_order = pd.Series(range(len(adata.obs.seg.cat.categories)), index=to_mil)[ 214 order[order.index.isin(to_mil)].index 215 ].values

IndexError: arrays used as indices must be of integer (or boolean) type

Could you please tell me what might be going on?