theislab / scarches

Reference mapping for single-cell genomics
https://docs.scarches.org/en/latest/
BSD 3-Clause "New" or "Revised" License
323 stars 50 forks source link

scGEN network.batch_removal error #235

Closed dKlee99 closed 2 months ago

dKlee99 commented 2 months ago

Hi, any ideas how to solve this issue?

adata format looks like following image

Here are commands I ran,

adata_hvg = adata[:, adata.var["highly_variable"]].copy() adata_hvg = remove_sparsity(adata_hvg) adata_hvg.X = adata_hvg.X.astype('float32')

network = sca.models.scgen(adata = adata_hvg,
                           hidden_layer_sizes=[256,128])

network.train(n_epochs=epoch, early_stopping_kwargs = early_stopping_kwargs) corrected_reference_adata = network.batch_removal(adata_hvg, batch_key="sample", cell_label_key="celltype",return_latent=True)

The error occured at the last command.


TypeError Traceback (most recent call last) Cell In[83], line 1 ----> 1 corrected_reference_adata = network.batch_removal(adata_hvg, batch_key="sample", cell_label_key="celltype",return_latent=True)

File ~/anaconda3/envs/scarches/lib/python3.9/site-packages/scarches/models/scgen/vaearith_model.py:147, in scgen.batch_removal(self, adata, batch_key, cell_label_key, return_latent) 127 def batch_removal(self, adata, batch_key, cell_label_key, return_latent = True): 128 """ 129 Removes batch effect of adata 130 (...) 145 adata of corrected gene expression in adata.X and corrected latent space in adata.obsm["latent_corrected"]. 146 """ --> 147 corrected = self.model.batch_removal(adata, batch_key, cell_label_key, return_latent) 148 return corrected

File ~/anaconda3/envs/scarches/lib/python3.9/site-packages/scarches/models/scgen/vaearith.py:261, in vaeArith.batch_removal(self, adata, batch_key, cell_label_key, return_latent) 259 adata_latent = anndata.AnnData(latent_all) 260 adata_latent.obs = adata.obs.copy(deep=True) --> 261 unique_cell_types = np.unique(adata_latent.obs[cell_label_key]) 262 shared_ct = [] 263 not_shared_ct = []

File ~/anaconda3/envs/scarches/lib/python3.9/site-packages/numpy/lib/arraysetops.py:274, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan) 272 ar = np.asanyarray(ar) 273 if axis is None: --> 274 ret = _unique1d(ar, return_index, return_inverse, return_counts, 275 equal_nan=equal_nan) 276 return _unpack_tuple(ret) 278 # axis was specified and not None

File ~/anaconda3/envs/scarches/lib/python3.9/site-packages/numpy/lib/arraysetops.py:336, in _unique1d(ar, return_index, return_inverse, return_counts, equalnan) 334 aux = ar[perm] 335 else: --> 336 ar.sort() 337 aux = ar 338 mask = np.empty(aux.shape, dtype=np.bool)

TypeError: '<' not supported between instances of 'float' and 'str'

dKlee99 commented 2 months ago

adata_hvg.obs['celltype'] = adata_hvg.obs['celltype'].astype(str) solved the issue