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
50 stars 2 forks source link

scf.pl.trends error on jupyter notebook #21

Closed hw1804 closed 1 year ago

hw1804 commented 1 year ago
Screenshot 2023-08-18 at 17 01 27

Hi, I was trying to plot a series of graph using scf.pl.trends and I have done other procedures in the basic curved trajectory analysis tutorial "fitting&clustering significant features" part. I am using the tutorial code and it gave the error in the screenshot: "arrays used as indices must be of integer (or boolean) type". Is there any work-around of this issue?

LouisFaure commented 1 year ago

This is intriguing, I am not able to reproduce that error.

First of all, I am not sure why the number of clusters generated does not match (in my case I get 6 clusters), be sure to have updated scFates v1.0.4 and are using the latest tutorial, as I updated both of them lately.

If it still fails, can you run the following cell before the cell of pl.trends causing the error:

import igraph
import numpy as np
graph=adata.uns["graph"]
dct = graph["milestones"]
keys = np.array(list(dct.keys()))
vals = np.array(list(dct.values()))
edges = graph["pp_seg"][["from", "to"]].astype(str).apply(tuple, axis=1).values
img = igraph.Graph(directed=True)
img.add_vertices(vals.astype(str))
img.add_edges(edges)

allpaths = img.get_all_shortest_paths(
    str(graph["root"]), to=graph["tips"].astype(str)
)

allpaths = np.array(allpaths, dtype=object)[
    np.argsort(np.array([len(p) for p in allpaths]))
]

order = allpaths[0]
for i in range(1, len(allpaths)):
    order = order + np.array(allpaths[i])[~np.isin(allpaths[i], order)].tolist()

order = np.array(order)[1:]
print(order)
print(order.dtype)

What output do you see? In my case it is:

[0]
int64
hw1804 commented 1 year ago

I was running my own dataset using the tutorial, that's probably why it has got 9 clusters. I ran the test code, it gave [1] object instead of [0] int. One of my friend thinks it may be because of the python version I am using. I am using python 3.11.4. I am not sure why my python or notepad interact differently.

Screenshot 2023-08-22 at 22 11 27
LouisFaure commented 1 year ago

I just tried with python 3.11 and I am not getting any issue (and order is numpy array with int64), this could be dataset related. Do you see the same error by reproducing exactly the example notebook?

hw1804 commented 1 year ago

I ran the tutorial with your dataset and was able to get the scf.pl.trends to work. I ran the same code again using my dataset and it still gave the same error. My dataset was originally a seurat object, and I converted it to h5ad file for python packages to read. Any pointers on how can I fix my dataset? I could plot a lot of graphs fine, just not the scf.pl.trends, which I really want to show for publication.

Screenshot 2023-08-23 at 12 14 43 Screenshot 2023-08-23 at 12 15 00
hw1804 commented 1 year ago

update: I can plot scf.pl.single_trend just fine. But scf.pl.trends not working. I have converted my seurat file to loom and still the same error. Here is what my dataset look like:

Screenshot 2023-08-23 at 15 12 23 Screenshot 2023-08-23 at 15 10 57
LouisFaure commented 1 year ago

Could you try this code?

import igraph
import numpy as np
import pandas as pd
graph=adata.uns["graph"]
dct = graph["milestones"]
keys = np.array(list(dct.keys()))
vals = np.array(list(dct.values()))
edges = graph["pp_seg"][["from", "to"]].astype(str).apply(tuple, axis=1).values
img = igraph.Graph(directed=True)
img.add_vertices(vals.astype(str))
img.add_edges(edges)

allpaths = img.get_all_shortest_paths(
    str(graph["root"]), to=graph["tips"].astype(str)
)

allpaths = np.array(allpaths, dtype=object)[
    np.argsort(np.array([len(p) for p in allpaths]))
]

order = allpaths[0]
for i in range(1, len(allpaths)):
    order = order + np.array(allpaths[i])[~np.isin(allpaths[i], order)].tolist()

order = np.array(order)[1:]
print(pd.Series(graph["milestones"].keys(), index=graph["milestones"].values())[
    [np.array(img.vs["name"])[o].astype(int) for o in order]
])

If it succeed I will probably be able to change the part of the function that was causing the error, avoid the check for int or booleans

hw1804 commented 1 year ago
Screenshot 2023-08-23 at 22 01 10
LouisFaure commented 1 year ago

I think we are getting there! Now you try to use pl.trends using that version of scFates:

pip install git+https://github.com/LouisFaure/scFates@v1.0.5dev
hw1804 commented 1 year ago

Thank you so much for your help. The pl.trends works now for my dataset as well with the dev version.

LouisFaure commented 1 year ago

Great! I have now included the changes to v1.0.5