scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
424 stars 78 forks source link

Tangram integration #341

Closed ccruizm closed 3 years ago

ccruizm commented 3 years ago

Good day,

I have been trying to follow the vignette Cell-type deconvolution using Tangram but have problems with the deconvolution step. As stated in https://github.com/theislab/squidpy_notebooks/issues/58, the commands do not work with Tangram v0.3.0. I downgraded it to v0.1.3 as suggested, but that downgraded scanpy to 1.6.0 and >= 1.7.0 seems to be needed to run squidpy. I tried Tangram v0.1.3 and scanpy v1.8.0.dev90+gbcfa925 and ran tg.mapping_optimizer.MapperConstrained with no issues but output, F_out = mapper.train(learning_rate=learning_rate, num_epochs=num_epochs) showed the next error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-6b53317541ec> in <module>
----> 1 output, F_out = mapper.train(learning_rate=learning_rate, num_epochs=num_epochs)

/hpc/pmc_stunnenberg/cruiz/miniconda3/envs/python_pHGG_project/lib/python3.7/site-packages/tangram/mapping_optimizer.py in train(self, num_epochs, learning_rate, print_each)
    221                 loss = self._loss_fn(verbose=False)
    222             else:
--> 223                 loss = self._loss_fn(verbose=True)
    224             optimizer.zero_grad()
    225             loss.backward()

/hpc/pmc_stunnenberg/cruiz/miniconda3/envs/python_pHGG_project/lib/python3.7/site-packages/tangram/mapping_optimizer.py in _loss_fn(self, verbose)
    175 
    176         M_probs_filtered = M_probs * F_probs[:, np.newaxis]
--> 177         d_pred = torch.log(M_probs_filtered.sum(axis=0) / (F_probs.sum()))  # KL wants the log in first argument
    178         density_term = self.lambda_d * self._density_criterion(d_pred, self.d)
    179 

TypeError: sum() received an invalid combination of arguments - got (axis=int, ), but expected one of:
 * ()
      didn't match because some of the keywords were incorrect: axis
 * (torch.dtype dtype)
      didn't match because some of the keywords were incorrect: axis
 * (tuple of ints dim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim)

Any suggestions on how to run this part of the tutorial? I am using the last version of squidpy from the dev branch

Thanks in advance!

ccruizm commented 3 years ago

Good day, in the meantime, I wanted to reproduce the Tangram tutorial but was not able to recreate the environment used in that notebook. Could you please tell me how I can install Squidpy v0.0.0, please?

giovp commented 3 years ago

hi @ccruizm ,

sorry for late reply. That error seems related to tangram rather than version inconstistieces between squidpy and scanpy. We are in touch with tangram developer and hope to update very soon (max 2 weeks) an updated tutorial with latest squidpy and tangram version.

In the meantime, have you tried with the solution described here? https://github.com/theislab/squidpy_notebooks/issues/58#issue-869693405

sorry again for late reply, we are aware of the issue and are working actively toward a solution.

ccruizm commented 3 years ago

Hello @giovp,

No worries. Looking forward to the updated tutorial.

The problem I had, was once I downgraded Tangram to v0.1.3, it also downgraded scanpy to v1.6.0 and the current version of Squidpy relies on >v1.7.0. I saw the old notebook and there was an older version of Squidpy (v.0.0.0 - first release I guess) but could not find a way to reproduce that environment to run the current Tangram integration tutorial (where the versions used are scanpy==1.6.0 anndata==0.7.4 umap==0.4.6 numpy==1.20.0 scipy==1.6.0 pandas==1.1.4 scikit-learn==0.24.1 statsmodels==0.12.0 python-igraph==0.8.3 leidenalg==0.8.2 squidpy==0.0.0).

Is there a way I could set this environment in the meantime? Could not find the old version of Squidpy to do so

Thanks in advance!

giovp commented 3 years ago

The problem I had, was once I downgraded Tangram to v0.1.3, it also downgraded scanpy to v1.6.0 and the current version of Squidpy relies on >v1.7.0

can you try to install tangram and then install squidpy? There shouldn't be any problem on tangram side for higher version of scanpy.

ccruizm commented 3 years ago

I tried in that way and did not also work. Since I am using 'pip install squidpy' after installing tangram, pip will upgrade scanpy and get the next: tangram-sc 0.1.3 requires scanpy==1.6.0, but you'll have scanpy 1.7.2 which is incompatible.. This is where the problem relies on. I would need to install an older version of squipy that uses scanpy 1.6.0

giovp commented 3 years ago

and get the next: tangram-sc 0.1.3 requires scanpy==1.6.0, but you'll have scanpy 1.7.2 which is incompatible.

but this is an internal check by pip you should still be able to run the notebook. Have you tried that?

ccruizm commented 3 years ago

Yes, when using an env set with scanpy==1.8.0.dev90+gbcfa925 anndata==0.7.5 umap==0.5.1 numpy==1.20.2 scipy==1.6.2 pandas==1.2.3 scikit-learn==0.24.1 statsmodels==0.12.2 python-igraph==0.9.1 louvain==0.7.0 leidenalg==0.8.3 pynndescent==0.5.2 squidpy==1.0.0 tangram==0.1.3 I was able to run

mapper = tg.mapping_optimizer.MapperConstrained(
    S=S,
    G=G,
    d=d,
    device=device,
    **hyperparm,
    target_count=adata_st.obs.cell_count.sum()
)

but in the next step got an error output, F_out = mapper.train(learning_rate=learning_rate, num_epochs=num_epochs)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-6b53317541ec> in <module>
----> 1 output, F_out = mapper.train(learning_rate=learning_rate, num_epochs=num_epochs)

/hpc/pmc_stunnenberg/cruiz/miniconda3/envs/python_pHGG_project/lib/python3.7/site-packages/tangram/mapping_optimizer.py in train(self, num_epochs, learning_rate, print_each)
    221                 loss = self._loss_fn(verbose=False)
    222             else:
--> 223                 loss = self._loss_fn(verbose=True)
    224             optimizer.zero_grad()
    225             loss.backward()

/hpc/pmc_stunnenberg/cruiz/miniconda3/envs/python_pHGG_project/lib/python3.7/site-packages/tangram/mapping_optimizer.py in _loss_fn(self, verbose)
    175 
    176         M_probs_filtered = M_probs * F_probs[:, np.newaxis]
--> 177         d_pred = torch.log(M_probs_filtered.sum(axis=0) / (F_probs.sum()))  # KL wants the log in first argument
    178         density_term = self.lambda_d * self._density_criterion(d_pred, self.d)
    179 

TypeError: sum() received an invalid combination of arguments - got (axis=int, ), but expected one of:
 * ()
      didn't match because some of the keywords were incorrect: axis
 * (torch.dtype dtype)
      didn't match because some of the keywords were incorrect: axis
 * (tuple of ints dim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim, torch.dtype dtype)
 * (tuple of ints dim, bool keepdim)

I assumed it was related to the incompatibility of the scanpy versions. Maybe it is linked to something else.

giovp commented 3 years ago

ah ok intersting, not that's something from tangram side. Sorry for that, you could either open an issue there or just wait that we push an updated version of the notebook. Thank you!

ccruizm commented 3 years ago

No problem. I will wait for the updated version 👍🏼

Thanks for the quick response and help

ccruizm commented 3 years ago

Good day! I just saw there is a new Squidpy update! Super nice. Is the tangram issue fixed in this version (v.1.1.0)? Thanks!

giovp commented 3 years ago

hello @ccruizm , indeed! Here's the link to the tutorial: https://squidpy.readthedocs.io/en/latest/external_tutorials/tutorial_tangram.html

you'd have to install the development branch of Tangram

git clone https://github.com/broadinstitute/Tangram.git
cd Tangram
git switch ziq
pip install -e .

let me know if it works. We'll update the tutorial once a new Tangram release is avaialble.

giovp commented 3 years ago

hi @ccruizm ,

wehave also updated the tutorial using the latest tangram, you can find it here: https://squidpy.readthedocs.io/en/latest/external_tutorials/tutorial_tangram.html

together with the updated conda environemnt https://github.com/theislab/squidpy_notebooks/blob/dev/envs/tangram_environment.yml

I'll close this, feel free to reopen if did not work

Giovanni