Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "TypeError: expected Tensor as element 0 in argument 0, but got numpy.ndarray" when running "st.alignment.morpho_align" #277

Open
forrwill opened this issue Oct 15, 2024 · 11 comments

Comments

@forrwill
Copy link

forrwill commented Oct 15, 2024

Hi, I met the error "TypeError: expected Tensor as element 0 in argument 0, but got numpy.ndarray",
I had run the function successfully before. But now, It was failed with the error, could you please help me figure out the question.

^M|-----> [Models alignment based on morpho, mode: SN-S.] in progress: 100.0000%|-----> GPU is not available, resorting to torch cpu.
|-----> Filtered all samples for common genes. There are 30356 common genes.
|-----> Spatial coordinates normalization params:
|-----------> Scale: [1357.2966 1357.2966]...
|-----------> Scale: [[18417.703 23149.129 241.7397 ]
[19898.088 8253.436 331.55087]]...
|-----> Preprocess finished.
|-----> Performing coarse rigid alignment...
|-----> Coarse rigid alignment done.

Traceback (most recent call last):
File "./Spateo.test.py", line 60, in
align_8W9W, pis_8W9W = st.alignment.morpho_align(
File "/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
P = morpho_model.run()
File "
/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/torch/utils/_contextlib.>
return func(*args, **kwargs)
File "/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
self._update_assignment_P()
File "
/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
self.P, self.K_NA_spatial, self.K_NA_sigma2, sigma2_related = get_P_core(
File "/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
P = _dense_to_sparse(
File "
/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
results = _SparseTensor(nx=nx, row=row, col=col, value=val, sparse_sizes=(NA, NB))
File "~/Miniconda3/envs/spateo_env/lib/python3.9/site-packages/spateo_release-1.1.0-py3>
return SparseTensor(indices=torch.vstack((row, col)), values=value, size=sparse_sizes)
TypeError: expected Tensor as element 0 in argument 0, but got numpy.ndarray

It was confusing that I run the "st.alignment.morpho_align_ref" successfully.

@forrwill
Copy link
Author

I reinstalled the spateo-release with "yifan branch".
Then , I can run "st.alignment.morpho_align" successfully in cpu mode, But failed with GPU mode,

@YifanLu2000
Copy link
Contributor

Hi, could you provide more information? For example, the function you're running, so I can better understand what's going wrong and help troubleshoot.

@forrwill
Copy link
Author

Hi, could you provide more information? For example, the function you're running, so I can better understand what's going wrong and help troubleshoot.

align_8W9W, pis_8W9W = st.alignment.morpho_align(
models=[DEOE_8W, DEOE_9W],
spatial_key="3d_spatial",
key_added="3d_align_spatial",
SVI_mode=True,
device="0",
#sparse_calculation_mode=True,
#use_chunk=True,
#chunk_capacity=2
)
This is my code, the error appears when I run "st.alignment.morpho_align"

@YifanLu2000
Copy link
Contributor

Hi, thanks for providing the information. However, the issue seems a bit strange to me since the error appears to be related to sparse calculations. But from the code you've shared, it looks like the sparse_calculation_mode has been commented out. I have two suggestions:

  1. For 2D slice alignment: Ensure that the z-axis is not involved. If your z-axis data is in the third dimension, you can handle it like this:
    slice_a.obsm['spatial_2D'] = slice_a.obsm['spatial_3D'][:, :2].copy()
    This extracts only the xy-coordinates for alignment.
  2. For smaller datasets: If your data isn't too large (e.g., fewer than 10k cells per slice), I recommend not using sparse calculations or chunk operations. In that case, you can comment out the following lines:
sparse_calculation_mode=True,
use_chunk=True,
chunk_capacity=2

I hope this helps!

@forrwill
Copy link
Author

forrwill commented Oct 16, 2024

Hi, thanks for providing the information. However, the issue seems a bit strange to me since the error appears to be related to sparse calculations. But from the code you've shared, it looks like the sparse_calculation_mode has been commented out. I have two suggestions:

  1. For 2D slice alignment: Ensure that the z-axis is not involved. If your z-axis data is in the third dimension, you can handle it like this:
    slice_a.obsm['spatial_2D'] = slice_a.obsm['spatial_3D'][:, :2].copy()
    This extracts only the xy-coordinates for alignment.
  2. For smaller datasets: If your data isn't too large (e.g., fewer than 10k cells per slice), I recommend not using sparse calculations or chunk operations. In that case, you can comment out the following lines:
sparse_calculation_mode=True,
use_chunk=True,
chunk_capacity=2

I hope this helps!

when I run the code, I did not commented out the "sparse_calculation_mode=True", and I got the error. Then I reinstalled the spateo and rerun the code. and got the result when I comment out the "sparse_calculation_mode=True"

@YifanLu2000
Copy link
Contributor

Hi, thank you for your feedback! Just to confirm, did this solution work for you?

@forrwill
Copy link
Author

Hi, thank you for your feedback! Just to confirm, did this solution work for you?

My data is about 20k~60k cells. if I comment out "sparse_calculation_mode=True". The task would be memory out.

@YifanLu2000
Copy link
Contributor

Hi @forrwill, I apologize for this mistake and thank you for raising the issue. I believe I've found the bug. I noticed that your output includes: GPU is not available, resorting to torch cpu. This means the code is not utilizing the GPU for PyTorch calculations. When sparse_calculation_mode=True is enabled, we rely on torch SparseTensor, which is likely causing the issue. When you used st.alignment.morpho_align_ref, it worked because you likely didn’t enable sparse_calculation_mode=True.

We will need some time to fix this bug since it involves implementing sparse calculations using SciPy’s sparse operations for CPU mode. In the meantime, I suggest checking your PyTorch GPU installation. You can verify if the GPU is set up correctly by running:

import torch
print(torch.cuda.is_available())

Once PyTorch GPU is installed correctly, you should be able to use Spateo alignment without any issues.

In summary, the current issue seems to be with CPU + sparse calculation in Spateo alignment. I hope this helps, and please let me know if you have any other questions.

@forrwill
Copy link
Author

Hi @forrwill, I apologize for this mistake and thank you for raising the issue. I believe I've found the bug. I noticed that your output includes: GPU is not available, resorting to torch cpu. This means the code is not utilizing the GPU for PyTorch calculations. When sparse_calculation_mode=True is enabled, we rely on torch SparseTensor, which is likely causing the issue. When you used st.alignment.morpho_align_ref, it worked because you likely didn’t enable sparse_calculation_mode=True.

We will need some time to fix this bug since it involves implementing sparse calculations using SciPy’s sparse operations for CPU mode. In the meantime, I suggest checking your PyTorch GPU installation. You can verify if the GPU is set up correctly by running:

import torch
print(torch.cuda.is_available())

Once PyTorch GPU is installed correctly, you should be able to use Spateo alignment without any issues.

In summary, the current issue seems to be with CPU + sparse calculation in Spateo alignment. I hope this helps, and please let me know if you have any other questions.

In fact, I have tested it in GPU mode, But It was stoped without any error reported, So I am not sure whether it is a bug. Anyway, thank you very much. Please let me know the time point of the update version, Thank you very much!

@YifanLu2000
Copy link
Contributor

Hi @forrwill, thank you again for raising this issue. I have fixed the bugs related to CPU + sparse calculation in Spateo alignment. I tested the following configuration, and the results look good:
QQ_1729891966843
If you encounter any other problems, please feel free to raise an issue.

@YifanLu2000
Copy link
Contributor

Hi @forrwill , I have updated the code in the latest main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants