earmingol / cell2cell

User-friendly tool to infer cell-cell interactions and communication from gene expression of interacting proteins
BSD 3-Clause "New" or "Revised" License
55 stars 12 forks source link

Example not working #53

Open imerelli opened 4 months ago

imerelli commented 4 months ago

Hi, I'm trying to run this example: Tensor-cell2cell-PBMC.ipynb

with c2c.version = '0.7.3'

But at this step:

fig, error = tensor.elbow_rank_selection(upper_rank=25, runs=10, init='svd', # If it outputs a memory error, replace by 'random' automatic_elbow=True, random_state=888, filename=None # Put a path (e.g. ./Elbow.png) to save the figure )

I get this error:

 0%|          | 0/25 [00:00<?, ?it/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 fig, error = tensor.elbow_rank_selection(upper_rank=25,
      2                                          runs=10,
      3                                          init='svd', # If it outputs a memory error, replace by 'random'
      4                                          automatic_elbow=True,
      5                                          random_state=888,
      6                                          filename=None # Put a path (e.g. ./Elbow.png) to save the figure
      7                                         )

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/cell2cell/tensor/tensor.py:518, in BaseTensor.elbow_rank_selection(self, upper_rank, runs, tf_type, init, svd, metric, random_state, n_iter_max, tol, automatic_elbow, manual_elbow, smooth, mask, ci, figsize, fontsize, filename, output_fig, verbose, **kwargs)
    516         fig = None
    517 elif runs > 1:
--> 518     all_loss = _multiple_runs_elbow_analysis(tensor=self.tensor,
    519                                              upper_rank=upper_rank,
    520                                              runs=runs,
    521                                              tf_type=tf_type,
    522                                              init=init,
    523                                              svd=svd,
    524                                              metric=metric,
    525                                              random_state=random_state,
    526                                              mask=mask,
    527                                              n_iter_max=n_iter_max,
    528                                              tol=tol,
    529                                              verbose=verbose,
    530                                              **kwargs
    531                                              )
    533     # Same outputs as runs = 1
    534     loss = np.nanmean(all_loss, axis=0).tolist()

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/cell2cell/tensor/factorization.py:342, in _multiple_runs_elbow_analysis(tensor, upper_rank, runs, tf_type, init, svd, metric, random_state, mask, n_iter_max, tol, verbose, **kwargs)
    340 else:
    341     rs = None
--> 342 tl_object, errors = _compute_tensor_factorization(tensor=tensor,
    343                                                   rank=r,
    344                                                   tf_type=tf_type,
    345                                                   init=init,
    346                                                   svd=svd,
    347                                                   random_state=rs,
    348                                                   mask=mask,
    349                                                   n_iter_max=n_iter_max,
    350                                                   tol=tol,
    351                                                   verbose=verbose,
    352                                                   **kwargs)
    354 if metric == 'error':
    355     # This helps to obtain proper error when the mask is not None.
    356     if mask is None:

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/cell2cell/tensor/factorization.py:92, in _compute_tensor_factorization(tensor, rank, tf_type, init, svd, random_state, mask, n_iter_max, tol, verbose, **kwargs)
     89         return_errors = kwargs['return_errors']
     91 if tf_type == 'non_negative_cp':
---> 92     cp_tf = non_negative_parafac(tensor=tensor,
     93                                  rank=rank,
     94                                  init='random' if mask is not None else init,
     95                                  svd=svd,
     96                                  random_state=random_state,
     97                                  mask=mask,
     98                                  n_iter_max=n_iter_max,
     99                                  tol=tol,
    100                                  verbose=verbose,
    101                                  **kwargs)
    103     if return_errors:
    104         cp_tf, errors = cp_tf

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/tensorly/decomposition/_nn_cp.py:85, in non_negative_parafac(tensor, rank, n_iter_max, init, svd, tol, random_state, verbose, normalize_factors, return_errors, mask, cvg_criterion, fixed_modes)
     82 epsilon = tl.eps(tensor.dtype)
     83 rank = validate_cp_rank(tl.shape(tensor), rank=rank)
---> 85 weights, factors = initialize_cp(
     86     tensor,
     87     rank,
     88     init=init,
     89     svd=svd,
     90     non_negative=True,
     91     mask=mask,
     92     random_state=random_state,
     93     normalize_factors=normalize_factors,
     94 )
     95 rec_errors = []
     96 norm_tensor = tl.norm(tensor, 2)

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/tensorly/decomposition/_cp.py:76, in initialize_cp(tensor, rank, init, svd, non_negative, random_state, normalize_factors, mask, svd_mask_repeats)
     74 for mode in range(tl.ndim(tensor)):
     75     mask_unfold = None if mask is None else unfold(mask, mode)
---> 76     U, S, _ = svd_interface(
     77         unfold(tensor, mode),
     78         n_eigenvecs=rank,
     79         method=svd,
     80         non_negative=non_negative,
     81         mask=mask_unfold,
     82         n_iter_mask_imputation=svd_mask_repeats,
     83     )
     85     # Put SVD initialization on the same scaling as the tensor in case normalize_factors=False
     86     if mode == 0:

File /opt/tools/deg/miniforge3/envs/cell2cell/lib/python3.10/site-packages/tensorly/tenalg/svd.py:414, in svd_interface(matrix, method, n_eigenvecs, flip_sign, u_based_flip_sign, non_negative, mask, n_iter_mask_imputation, **kwargs)
    412     svd_fun = method
    413 else:
--> 414     raise ValueError(
    415         f"Got svd={method}. However, the possible choices are {SVD_FUNS} or to pass a callable."
    416     )
    418 U, S, V = svd_fun(matrix, n_eigenvecs=n_eigenvecs, **kwargs)
    420 if mask is not None:

ValueError: Got svd=numpy_svd. However, the possible choices are ['truncated_svd', 'symeig_svd', 'randomized_svd'] or to pass a callable.

Any idea about to solve this?

earmingol commented 4 months ago

Hi @imerelli

I don't recommend using that example. Instead follow any other on the readme: https://github.com/earmingol/cell2cell/blob/master/README.md.

Regarding your issue, a quick way to solve is to add to your command the parameter svd and pick any of the options that the error is giving you. For example, add svd='truncated_svd'.

fig, error = tensor.elbow_rank_selection(upper_rank=25,
                                         runs=10,
                                         init='svd', # If it outputs a memory error, replace by 'random'
                                         svd='truncated_svd',
                                         automatic_elbow=True,
                                         random_state=888,
                                         filename=None # Put a path (e.g. ./Elbow.png) to save the figure
                                         )

Alternatively replace init='svd' with init='random'.

fig, error = tensor.elbow_rank_selection(upper_rank=25,
                                         runs=10,
                                         init='random', # If it outputs a memory error, replace by 'random'
                                         automatic_elbow=True,
                                         random_state=888,
                                         filename=None # Put a path (e.g. ./Elbow.png) to save the figure
                                         )

Also, what versions of tensorly and numpy are you using?

I hope this helps.