emdann / milopy

Python implementation of Milo for differential abundance testing on KNN graph
MIT License
60 stars 7 forks source link

Error in make_nhoods if the reduced dims are in pandas.DataFrame #34

Open emdann opened 1 year ago

emdann commented 1 year ago

The error:

milo.make_nhoods(adata, prop=0.05)
InvalidIndexError: (array([    6,    14,    31,    32,    36,    41,    44,    45,    46,
        1226,  1893,  2742,  3122,  3475,  3494,  3578,  3588,  4416,
        4446,  5146,  5274,  5686,  6432,  6524,  7218,  7323,  8239,
        8945, 13048, 14444, 14774, 15035, 15580, 16304, 20604, 20842,
       21208, 21289, 21313, 21352, 23599], dtype=int32), slice(None, None, None))

The quick fix:

adata.obsm[adata.uns['neighbors']['use_rep']] = adata.obsm[adata.uns['neighbors']['use_rep']].values.copy()

Solution: convert X_dimred to array if it's a DataFrame

james-cranley commented 1 year ago

thanks @emdann , i had this error today for the first time. I've used this milopy function in another conda env with no issues. is it something to do with pandas 2 (which was installed in the new env) that is causing this to be flagged as an error now?

and, for me unfortuantely the 'quick fix' code gave me:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[37], line 1
----> 1 adata.obsm[adata.uns['neighbors']['use_rep']] = adata.obsm[adata.uns['neighbors']['use_rep']].values.copy()

KeyError: 'use_rep'

I inspected adata.uns['neighbors']:

adata.uns['neighbors']

{'connectivities_key': 'connectivities',
 'distances_key': 'distances',
 'params': {'n_neighbors': 15,
  'method': 'umap',
  'random_state': 0,
  'metric': 'euclidean',
  'use_rep': 'X_scArches'}}

Anyway, this line of code did work for me as a quick fix adata.obsm[adata.uns['neighbors']['params']['use_rep']] = adata.obsm[adata.uns['neighbors']['params']['use_rep']].values.copy()