JinmiaoChenLab / GraphST

GNU Affero General Public License v3.0
92 stars 14 forks source link

TypeError: invalid data type for einsum" #26

Closed aelhossiny closed 8 months ago

aelhossiny commented 8 months ago

Hi, Thanks for such a fantastic method, I get this error when I run it on my visium slide, any help?

{
    "name": "TypeError",
    "message": "invalid data type for einsum",
    "stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 model = GraphST.GraphST(adata, device=device)
      2 adata = model.train()

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/GraphST/GraphST.py:103, in GraphST.__init__(self, adata, adata_sc, device, learning_rate, learning_rate_sc, weight_decay, epochs, dim_input, dim_output, random_seed, alpha, beta, theta, lamda1, lamda2, deconvolution, datatype)
    101       construct_interaction_KNN(self.adata)
    102    else:    
--> 103       construct_interaction(self.adata)
    105 if 'label_CSL' not in adata.obsm.keys():    
    106    add_contrastive_label(self.adata)

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/GraphST/preprocess.py:55, in construct_interaction(adata, n_neighbors)
     52 position = adata.obsm['spatial']
     54 # calculate distance matrix
---> 55 distance_matrix = ot.dist(position, position, metric='euclidean')
     56 n_spot = distance_matrix.shape[0]
     58 adata.obsm['distance_matrix'] = distance_matrix

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/ot/utils.py:309, in dist(x1, x2, metric, p, w)
    307     return euclidean_distances(x1, x2, squared=True)
    308 elif metric == \"euclidean\":
--> 309     return euclidean_distances(x1, x2, squared=False)
    310 else:
    311     if not get_backend(x1, x2).__name__ == 'numpy':

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/ot/utils.py:253, in euclidean_distances(X, Y, squared)
    232 r\"\"\"
    233 Considering the rows of :math:`\\mathbf{X}` (and :math:`\\mathbf{Y} = \\mathbf{X}`) as vectors, compute the
    234 distance matrix between each pair of vectors.
   (...)
    248 distances : array-like, shape (`n_samples_1`, `n_samples_2`)
    249 \"\"\"
    251 nx = get_backend(X, Y)
--> 253 a2 = nx.einsum('ij,ij->i', X, X)
    254 b2 = nx.einsum('ij,ij->i', Y, Y)
    256 c = -2 * nx.dot(X, Y.T)

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/ot/backend.py:1164, in NumpyBackend.einsum(self, subscripts, *operands)
   1163 def einsum(self, subscripts, *operands):
-> 1164     return np.einsum(subscripts, *operands)

File <__array_function__ internals>:200, in einsum(*args, **kwargs)

File ~/.conda/envs/GraphST/lib/python3.8/site-packages/numpy/core/einsumfunc.py:1371, in einsum(out, optimize, *operands, **kwargs)
   1369     if specified_out:
   1370         kwargs['out'] = out
-> 1371     return c_einsum(*operands, **kwargs)
   1373 # Check the kwargs to avoid a more cryptic error later, without having to
   1374 # repeat default values here
   1375 valid_einsum_kwargs = ['dtype', 'order', 'casting']

TypeError: invalid data type for einsum"
}
aelhossiny commented 8 months ago

I figured it, the adata.obsm['spatial'] wasn't a numeric array .. maybe a problem with 10x output .. adata.obsm['spatial'] = adata.obsm['spatial'].astype('float64') solved the issue