from scvi.model._utils import parse_use_gpu_arg ImportError: cannot import name 'parse_use_gpu_arg' from 'scvi.model._utils'
Cell2location采用贝叶斯层次框架。首先使用外部单细胞RNA测序数据作为参考数据,估计细胞类型特异性基因表达特征。然后,通过负二项分布(negative binomial regression)对观察到的空间表达计数矩阵建模,其中均值参数取决于参考细胞类型的特征,过度分散参数使用指数-伽马复合先验建模,旨在使大多数基因具有低过度分散。基因特异性技术灵敏度和基因-位置特异性的加性偏移被包括在均值参数的一部分,每个都使用单独的层次伽马先验进行建模。Cell2location进一步使用层次伽马先验对细胞类型特征的回归权重进行建模,并将回归权重分解为多个潜在组的贡献,这可以解释为具有共享细胞类型丰度特征的斑点,旨在跨具有相似细胞组成的位置借用强度。最后,cell2location采用变分贝叶斯推断来近似后验分布,并相应地生成参数估计。总的来说,Cell2location提供了一种可靠的途径来处理模型中的不确定因素,可以解释细胞类型丰度的线性依存关系。相比于其他的联合分析工具,Cell2location体现了更高的运算效率。
报错
(cell2loc) C:\Users\Administrator>python Python 3.9.19 (main, May 6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import cell2location Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\cell2location_init_.py", line 9, in <module> from . import models File "D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\cell2location\models_init_.py", line 1, in <module> from ._cell2location_model import Cell2location File "D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\cell2location\models_cell2location_model.py", line 29, in <module> from cell2location.models.base._pyro_base_loc_module import Cell2locationBaseModule File "D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\cell2location\models\base_pyro_base_loc_module.py", line 6, in <module> from ._pyro_mixin import AutoGuideMixinModule, init_to_value File "D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\cell2location\models\base_pyro_mixin.py", line 20, in <module> from scvi.model._utils import parse_use_gpu_arg ImportError: cannot import name 'parse_use_gpu_arg' from 'scvi.model._utils' (D:\ProgramData\Anaconda3\envs\cell2loc\lib\site-packages\scvi\model_utils.py)
安装cell2location
mamba remove -n cell2loc_env --all
export PYTHONNOUSERSITE="literallyanyletters"
mamba create -y -n cell2loc_env python=3.9
mamba activate cell2loc_env
pip3 install cell2location
无奈之下 找到scvi-tools github网站https://github.com/scverse/scvi-tools/blob/1.0.4/scvi/model/_utils.py,从上一个版本的scvi-tools 1.0.4中找到parse_use_gpu_arg
函数并增加到中D:/ProgramData/Anaconda3/envs/cell2loc/Lib/site-packages/scvi\model/_utils.py文件中,
即可成功import cell2location包
添加的函数是:
def parse_use_gpu_arg(
use_gpu: Optional[Union[str, int, bool]] = None,
return_device=True,
):
"""Parses the use_gpu arg in codebase.
Returned gpus are is compatible with PytorchLightning's gpus arg.
If return_device is True, will also return the device.
Parameters
----------
use_gpu
Use default GPU if available (if None or True), or index of GPU to use (if int),
or name of GPU (if str, e.g., `'cuda:0'`), or use CPU (if False).
return_device
If True, will return the torch.device of use_gpu.
Returns
-------
Arguments for lightning trainer, including the accelerator (str), devices
(int or sequence of int), and optionally the torch device.
"""
# Support Apple silicon
cuda_available = torch.cuda.is_available()
# If using an older version of torch.
try:
mps_available = torch.backends.mps.is_available()
except AttributeError:
mps_available = False
gpu_available = cuda_available
lightning_devices = None
if (use_gpu is None and not gpu_available) or (use_gpu is False):
accelerator = "cpu"
device = torch.device("cpu")
lightning_devices = "auto"
elif (use_gpu is None and gpu_available) or (use_gpu is True):
current = torch.cuda.current_device() if cuda_available else "mps"
if current != "mps":
lightning_devices = [current]
accelerator = "gpu"
else:
accelerator = "mps"
lightning_devices = 1
device = torch.device(current)
# Also captures bool case
elif isinstance(use_gpu, int):
device = torch.device(use_gpu) if not mps_available else torch.device("mps")
accelerator = "gpu" if not mps_available else "mps"
lightning_devices = [use_gpu] if not mps_available else 1
elif isinstance(use_gpu, str):
device = torch.device(use_gpu)
accelerator = "gpu"
# changes "cuda:0" to "0,"
lightning_devices = [int(use_gpu.split(":")[-1])]
else:
raise ValueError("use_gpu argument not understood.")
if return_device:
return accelerator, lightning_devices, device
else:
return accelerator, lightning_devices
再次运行
成功解决报错
注意Cell2location运行非常慢,建议使用GPU进行加速!
二. Cell2location代码实现
Step0.环境部署
github在https://github.com/BayraktarLab/cell2location#Installation
官方教程在:https://cell2location.readthedocs.io/en/latest/
可以使用Conda安装Cell2location:
conda create -y -n cell2loc python=3.9
conda activate cell2loc
pip install cell2location[tutorials]
如果需要衔接Jupyter notebook
使用的话,需要在cell2loc
这个环境里安装一下几个插件,然后就能在Jupyter notebook
里选择cell2loc
这个环境:
mamba install -y nb_conda_kernels ipykernel
python -m ipykernel install --user --name cell2loc --display-name cell2loc
Step1.加载示例数据及预处理
import scanpy as sc
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import cell2location
from matplotlib import rcParams
rcParams['pdf.fonttype'] = 42 # enables correct plotting of text for PDFs
注意:cell2location需要count原始数据作为input数据,不能用标准化后的。参考:https://github.com/BayraktarLab/cell2location/issues/249