StatBiomed/SpatialDM

Code Update Request: Avoid 'np.float' Deprecation Error in melanoma.ipynb

yuifu opened this issue · 6 comments

yuifu commented

Thank you for creating such a wonderful software!

I immediately installed SpatialDM and tried running melanoma.ipynb, but I got an error.
https://github.com/StatBiomed/SpatialDM/blob/main/tutorial/melanoma.ipynb

Here is the code cell where the error occurred:

import time
start = time.time()
sdm.spatialdm_global(adata, 1000, specified_ind=None, method='both', nproc=1)     # global Moran selection
sdm.sig_pairs(adata, method='permutation', fdr=True, threshold=0.1)     # select significant pairs
print("%.3f seconds" %(time.time()-start))

The error is as follows:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 3
      1 import time
      2 start = time.time()
----> 3 sdm.spatialdm_global(adata, 1000, specified_ind=None, method='both', nproc=1)     # global Moran selection
      4 sdm.sig_pairs(adata, method='permutation', fdr=True, threshold=0.1)     # select significant pairs
      5 print("%.3f seconds" %(time.time()-start))

File /opt/conda/lib/python3.11/site-packages/spatialdm/main.py:205, in spatialdm_global(adata, n_perm, specified_ind, method, nproc)
    202     raise ValueError("Only one of ['z-score', 'both', 'permutation'] is supported")
    204 with threadpool_limits(limits=nproc, user_api='blas'):
--> 205     pair_selection_matrix(adata, n_perm, specified_ind, method)
    207 adata.uns['global_res'] = pd.concat((adata.uns['ligand'], adata.uns['receptor']),axis=1)
    208 # adata.uns['global_res'].columns = ['Ligand1', 'Ligand2', 'Ligand3', 'Receptor1', 'Receptor2', 'Receptor3', 'Receptor4']

File /opt/conda/lib/python3.11/site-packages/spatialdm/utils.py:114, in pair_selection_matrix(adata, n_perm, sel_ind, method)
    112 # averaged ligand values
    113 L1 = [pd.Series(x[0]).dropna().values for x in ligand.values]
--> 114 L_mat = [adata[:, L1[l]].X.astype(np.float)[:, 0] for l in range(len(L1))]
    115 for i, k in enumerate(ligand.index):
    116     if len(ligand.loc[k].dropna()) > 1:

File /opt/conda/lib/python3.11/site-packages/spatialdm/utils.py:114, in <listcomp>(.0)
    112 # averaged ligand values
    113 L1 = [pd.Series(x[0]).dropna().values for x in ligand.values]
--> 114 L_mat = [adata[:, L1[l]].X.astype(np.float)[:, 0] for l in range(len(L1))]
    115 for i, k in enumerate(ligand.index):
    116     if len(ligand.loc[k].dropna()) > 1:

File /opt/conda/lib/python3.11/site-packages/numpy/__init__.py:305, in __getattr__(attr)
    300     warnings.warn(
    301         f"In the future `np.{attr}` will be defined as the "
    302         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    304 if attr in __former_attrs__:
--> 305     raise AttributeError(__former_attrs__[attr])
    307 # Importing Tester requires importing all of UnitTest which is not a
    308 # cheap import Since it is mainly used in test suits, we lazy import it
    309 # here to save on the order of 10 ms of import time for most users
    310 #
    311 # The previous way Tester was imported also had a side effect of adding
    312 # the full `numpy.testing` namespace
    313 if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

My environment is

  • Python 3.11.4
  • numpy 1.24.4

So, I think the above issue would occur when users use numpy >1.20.

I would appreciate it if you could update the code to avoid deprecation concerning np.float.

I also encountered this issue, tried to set numpy to 1.19.5 but the python 3.11 is not supporting this

Also after getting the numpy error fixed I get another error :
Traceback (most recent call last): File "/Users/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 2142, in showtraceback stb = self.InteractiveTB.structured_traceback( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/miniconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1435, in structured_traceback ... ^^^^^ File "/Users/miniconda3/lib/python3.11/site-packages/executing/executing.py", line 116, in only raise NotOneValueFound('Expected one value, found 0')

I am getting the same error too.

I also encountered this error. I tried numpy 1.18 but this didn't work beacuse it is incompatible with pandas.

This error should have been fixed a few months ago. Could you try reinstalling development version or 0.2.0 version on Pypi? @yuifu @sealx017 @heksaani @biyang-bioinfo

Reinstalling version 0.2.0 fixed this issue. Thanks!

This error should have been fixed a few months ago. Could you try reinstalling development version or 0.2.0 version on Pypi? @yuifu @sealx017 @heksaani @biyang-bioinfo