BiomedicalMachineLearning / stLearn

A novel machine learning pipeline to analyse spatial transcriptomics data
Other
190 stars 25 forks source link

error when using st.spatial.trajectory.pseudotimespace_local #275

Closed cynthier closed 8 months ago

cynthier commented 8 months ago

Hi, it works when using st.spatial.trajectory.pseudotimespace_global, but there is an error when I run st.spatial.trajectory.pseudotimespace_local for the same clusters. I want to plot only the spots of selected cluster when using st.pl.trajectory.pseudotime_plot and I guess I should run the st.spatial.trajectory.pseudotimespace_local ? Could you please help me?

data.uns["iroot"] = np.flatnonzero(data.obs["region_num"] == str(1))[0]
data.obs["region_num"] = pd.Categorical(meta["region_num"])
st.spatial.trajectory.pseudotime(data,eps=30,use_rep="X_pca",use_label="region_num", threshold_spots=4)
st.spatial.trajectory.pseudotimespace_global(data,use_label="region_num",list_clusters=[1,2]) # 
st.spatial.trajectory.pseudotimespace_local(data, use_label="region_num", cluster=[1,2])

All available trajectory paths are stored in adata.uns['available_paths'] with length < 4 nodes Start to construct the trajectory: 1 -> 2 Screening PTS local graph...

Screening: 0%| [ time left: ? ]


IndexError Traceback (most recent call last) Cell In[116], line 5 3 st.spatial.trajectory.pseudotime(data,eps=30,use_rep="X_pca",use_label="region_num", threshold_spots=4) 4 st.spatial.trajectory.pseudotimespace_global(data,use_label="region_num",list_clusters=[1,2]) # (dp,dI) ----> 5 st.spatial.trajectory.pseudotimespace_local(data, use_label="region_num", cluster=[1,2])

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/stlearn/spatials/trajectory/pseudotimespace.py:91, in pseudotimespace_local(adata, use_label, cluster, w) 72 """\ 73 Perform pseudo-time-space analysis with local level. 74 (...) 87 Anndata 88 """ 90 if w is None: ---> 91 w = weight_optimizing_local(adata, use_label=use_label, cluster=cluster) 93 local_level(adata, use_label=use_label, cluster=cluster, w=w)

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/stlearn/spatials/trajectory/weight_optimization.py:110, in weight_optimizing_local(adata, use_label, cluster, step) 102 with tqdm( 103 total=int(1 / step + 1), 104 desc="Screening", 105 bar_format="{l_bar}{bar} [ time left: {remaining} ]", 106 ) as pbar: 107 for i in range(0, int(1 / step + 1)): 109 Gs.append( --> 110 local_level( 111 adata, 112 use_label=use_label, 113 cluster=cluster, 114 w=round(j, 2), 115 verbose=False, 116 return_matrix=True, 117 ) 118 ) 120 j = j + step 121 pbar.update(1)

File //lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/stlearn/spatials/trajectory/local_level.py:46, in local_level(adata, use_label, cluster, w, return_matrix, verbose, copy) 43 print("Start construct trajectory for subcluster " + str(cluster)) 45 tmp = adata.obs[adata.obs[use_label] == str(cluster)] ---> 46 cluster_data = adata[list(tmp.index)] 48 list_cluster = cluster_data.obs["sub_cluster_labels"].unique() 49 dpt = []

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/anndata/_core/anndata.py:1109, in AnnData.getitem(self, index) 1107 """Returns a sliced view of the object.""" 1108 oidx, vidx = self._normalize_indices(index) -> 1109 return AnnData(self, oidx=oidx, vidx=vidx, asview=True)

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/anndata/_core/anndata.py:289, in AnnData.init(self, X, obs, var, uns, obsm, varm, layers, raw, dtype, shape, filename, filemode, asview, obsp, varp, oidx, vidx) 287 if not isinstance(X, AnnData): 288 raise ValueError("X has to be an AnnData object.") --> 289 self._init_as_view(X, oidx, vidx) 290 else: 291 self._init_as_actual( 292 X=X, 293 obs=obs, (...) 305 filemode=filemode, 306 )

File //lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/anndata/_core/anndata.py:361, in AnnData._init_as_view(self, adata_ref, oidx, vidx) 358 # set raw, easy, as it’s immutable anyways... 359 if adata_ref._raw is not None: 360 # slicing along variables axis is ignored --> 361 self._raw = adata_ref.raw[oidx] 362 self._raw._adata = self 363 else:

File /cluster2/huanglab/lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/anndata/_core/raw.py:110, in Raw.getitem(self, index) 107 oidx = slice(oidx, oidx + 1, 1) 109 if not self._adata.isbacked: --> 110 X = _subset(self.X, (oidx, vidx)) 111 else: 112 X = None

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/functools.py:875, in singledispatch..wrapper(*args, *kw) 871 if not args: 872 raise TypeError(f'{funcname} requires at least ' 873 '1 positional argument') --> 875 return dispatch(args[0].class)(args, **kw)

File /lliu/Conda/R4b/stlearn2/stlearn_ky/lib/python3.8/site-packages/anndata/_core/index.py:124, in _subset(a, subset_idx) 122 if all(isinstance(x, cabc.Iterable) for x in subset_idx): 123 subsetidx = np.ix(*subset_idx) --> 124 return a[subset_idx]

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

duypham2108 commented 8 months ago

The purpose of local analysis is for only one cluster at a time. It's still not fully developed yet, so I don't think you should use that. Global analysis could be fine for trajectory reconstruction

cynthier commented 8 months ago

@duypham2108 Thank you for your reply.