Skills › Knowledge Work Plugins › scvi-tools › environment_setup.md
environment_setup.md
254 lines · bio-research/skills/scvi-tools/references/environment_setup.md at 2574e29
Most marks come from matching words in the file, so treat them as pointers.
1# Environment Setup for scvi-tools
3This reference covers installation and environment configuration for scvi-tools.
Environment Setup for scvi-tools
This reference covers installation and environment configuration for scvi-tools.
Raw lines 1 to 4Installation Options
Raw lines 5 to 67### Option 1: Conda Environment (Recommended)
9```bash
10# Create environment with GPU support
11conda create -n scvi-env python=3.10
12conda activate scvi-env
14# Install scvi-tools
15pip install scvi-tools
17# For GPU acceleration (recommended for large datasets)
18pip install torch --index-url https://download.pytorch.org/whl/cu118
20# Common dependencies
21pip install scanpy leidenalg
22```
Option 1: Conda Environment (Recommended)
# Create environment with GPU support
conda create -n scvi-env python=3.10
conda activate scvi-env
# Install scvi-tools
pip install scvi-tools
# For GPU acceleration (recommended for large datasets)
pip install torch --index-url https://download.pytorch.org/whl/cu118
# Common dependencies
pip install scanpy leidenalgRaw lines 7 to 2324### Option 2: Pip Only
26```bash
27# Create virtual environment
28python -m venv scvi-env
29source scvi-env/bin/activate # Linux/Mac
30# scvi-env\Scripts\activate # Windows
32# Install
33pip install scvi-tools scanpy
34```
Option 2: Pip Only
# Create virtual environment
python -m venv scvi-env
source scvi-env/bin/activate # Linux/Mac
# scvi-env\Scripts\activate # Windows
# Install
pip install scvi-tools scanpyRaw lines 24 to 35Runs code bin/activate :29
36### Option 3: With Spatial Analysis Support
38```bash
39conda create -n scvi-spatial python=3.10
40conda activate scvi-spatial
42pip install scvi-tools scanpy squidpy
43```
Option 3: With Spatial Analysis Support
conda create -n scvi-spatial python=3.10
conda activate scvi-spatial
pip install scvi-tools scanpy squidpyRaw lines 36 to 4451## Verify Installation
53```python
54import scvi
55import torch
56import scanpy as sc
58print(f"scvi-tools version: {scvi.__version__}")
59print(f"scanpy version: {sc.__version__}")
60print(f"PyTorch version: {torch.__version__}")
61print(f"GPU available: {torch.cuda.is_available()}")
63if torch.cuda.is_available():
64 print(f"GPU device: {torch.cuda.get_device_name(0)}")
65 print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
66```
Verify Installation
import scvi
import torch
import scanpy as sc
print(f"scvi-tools version: {scvi.__version__}")
print(f"scanpy version: {sc.__version__}")
print(f"PyTorch version: {torch.__version__}")
print(f"GPU available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"GPU device: {torch.cuda.get_device_name(0)}")
print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")Raw lines 51 to 67GPU Configuration
Raw lines 68 to 6977### PyTorch CUDA Versions
79| CUDA Version | PyTorch Install Command |
80|--------------|------------------------|
81| CUDA 11.8 | `pip install torch --index-url https://download.pytorch.org/whl/cu118` |
82| CUDA 12.1 | `pip install torch --index-url https://download.pytorch.org/whl/cu121` |
83| CPU only | `pip install torch --index-url https://download.pytorch.org/whl/cpu` |
PyTorch CUDA Versions
| CUDA Version | PyTorch Install Command |
|---|---|
| CUDA 11.8 | pip install torch --index-url https://download.pytorch.org/whl/cu118 |
| CUDA 12.1 | pip install torch --index-url https://download.pytorch.org/whl/cu121 |
| CPU only | pip install torch --index-url https://download.pytorch.org/whl/cpu |
85### Memory Management
87```python
88import torch
90# Clear GPU cache between models
91torch.cuda.empty_cache()
93# Monitor memory usage
94print(f"Allocated: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
95print(f"Cached: {torch.cuda.memory_reserved() / 1e9:.2f} GB")
96```
Memory Management
import torch
# Clear GPU cache between models
torch.cuda.empty_cache()
# Monitor memory usage
print(f"Allocated: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
print(f"Cached: {torch.cuda.memory_reserved() / 1e9:.2f} GB")Raw lines 85 to 9798## Common Issues
100| Issue | Cause | Solution |
101|-------|-------|----------|
102| `CUDA out of memory` | GPU memory exhausted | Reduce batch_size, use smaller model |
103| `No GPU detected` | CUDA not installed | Install CUDA toolkit matching PyTorch |
104| `Version mismatch` | PyTorch/CUDA incompatibility | Reinstall PyTorch with correct CUDA version |
105| `Import error scvi` | Missing dependencies | `pip install scvi-tools[all]` |
Common Issues
| Issue | Cause | Solution |
|---|---|---|
CUDA out of memory | GPU memory exhausted | Reduce batch_size, use smaller model |
No GPU detected | CUDA not installed | Install CUDA toolkit matching PyTorch |
Version mismatch | PyTorch/CUDA incompatibility | Reinstall PyTorch with correct CUDA version |
Import error scvi | Missing dependencies | pip install scvi-tools[all] |
107## Jupyter Setup
109```bash
110# Install Jupyter kernel
111pip install ipykernel
112python -m ipykernel install --user --name scvi-env --display-name "scvi-tools"
114# For interactive plots
115pip install matplotlib seaborn
116```
Jupyter Setup
# Install Jupyter kernel
pip install ipykernel
python -m ipykernel install --user --name scvi-env --display-name "scvi-tools"
# For interactive plots
pip install matplotlib seabornRaw lines 107 to 117118## Recommended Package Versions
120For reproducibility, pin versions:
122```bash
123pip install \
124 scvi-tools>=1.0.0 \
125 scanpy>=1.9.0 \
126 anndata>=0.9.0 \
127 torch>=2.0.0
128```
Recommended Package Versions
For reproducibility, pin versions:
pip install \
scvi-tools>=1.0.0 \
scanpy>=1.9.0 \
anndata>=0.9.0 \
torch>=2.0.0Raw lines 118 to 129Version Compatibility Guide
Raw lines 130 to 131132### scvi-tools 1.x vs 0.x API Changes
134The 1.x release introduced breaking changes. Key differences:
136| Operation | 0.x API (deprecated) | 1.x API (current) |
137|-----------|---------------------|-------------------|
138| Setup data | `scvi.data.setup_anndata(adata, ...)` | `scvi.model.SCVI.setup_anndata(adata, ...)` |
139| Register data | `scvi.data.register_tensor_from_anndata(...)` | Built into `setup_anndata` |
140| View setup | `scvi.data.view_anndata_setup(adata)` | `scvi.model.SCVI.view_anndata_setup(adata)` |
scvi-tools 1.x vs 0.x API Changes
The 1.x release introduced breaking changes. Key differences:
| Operation | 0.x API (deprecated) | 1.x API (current) |
|---|---|---|
| Setup data | scvi.data.setup_anndata(adata, ...) | scvi.model.SCVI.setup_anndata(adata, ...) |
| Register data | scvi.data.register_tensor_from_anndata(...) | Built into setup_anndata |
| View setup | scvi.data.view_anndata_setup(adata) | scvi.model.SCVI.view_anndata_setup(adata) |
142### Migration from 0.x to 1.x
144```python
145# OLD (0.x) - DEPRECATED
146import scvi
147scvi.data.setup_anndata(adata, layer="counts", batch_key="batch")
148model = scvi.model.SCVI(adata)
150# NEW (1.x) - CURRENT
151import scvi
152scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="batch")
153model = scvi.model.SCVI(adata)
154```
Migration from 0.x to 1.x
# OLD (0.x) - DEPRECATED
import scvi
scvi.data.setup_anndata(adata, layer="counts", batch_key="batch")
model = scvi.model.SCVI(adata)
# NEW (1.x) - CURRENT
import scvi
scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="batch")
model = scvi.model.SCVI(adata)Raw lines 142 to 155156### Model-Specific Setup (1.x)
158Each model has its own setup method:
160```python
161# scVI
162scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="batch")
164# scANVI
165scvi.model.SCANVI.setup_anndata(adata, layer="counts", batch_key="batch", labels_key="cell_type")
167# totalVI
168scvi.model.TOTALVI.setup_anndata(adata, layer="counts", protein_expression_obsm_key="protein")
170# MultiVI (uses MuData)
171scvi.model.MULTIVI.setup_mudata(mdata, rna_layer="counts", atac_layer="counts")
173# PeakVI
174scvi.model.PEAKVI.setup_anndata(adata, batch_key="batch")
176# veloVI
177scvi.external.VELOVI.setup_anndata(adata, spliced_layer="spliced", unspliced_layer="unspliced")
178```
Model-Specific Setup (1.x)
Each model has its own setup method:
# scVI
scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="batch")
# scANVI
scvi.model.SCANVI.setup_anndata(adata, layer="counts", batch_key="batch", labels_key="cell_type")
# totalVI
scvi.model.TOTALVI.setup_anndata(adata, layer="counts", protein_expression_obsm_key="protein")
# MultiVI (uses MuData)
scvi.model.MULTIVI.setup_mudata(mdata, rna_layer="counts", atac_layer="counts")
# PeakVI
scvi.model.PEAKVI.setup_anndata(adata, batch_key="batch")
# veloVI
scvi.external.VELOVI.setup_anndata(adata, spliced_layer="spliced", unspliced_layer="unspliced")Raw lines 156 to 179180### Minimum Version Requirements
182| Package | Minimum Version | Notes |
183|---------|-----------------|-------|
184| scvi-tools | 1.0.0 | Required for current API |
185| scanpy | 1.9.0 | HVG selection improvements |
186| anndata | 0.9.0 | Improved MuData support |
187| torch | 2.0.0 | Performance improvements |
188| mudata | 0.2.0 | Required for MultiVI |
189| scvelo | 0.2.5 | Required for veloVI |
Minimum Version Requirements
| Package | Minimum Version | Notes |
|---|---|---|
| scvi-tools | 1.0.0 | Required for current API |
| scanpy | 1.9.0 | HVG selection improvements |
| anndata | 0.9.0 | Improved MuData support |
| torch | 2.0.0 | Performance improvements |
| mudata | 0.2.0 | Required for MultiVI |
| scvelo | 0.2.5 | Required for veloVI |
191### Check Your Versions
193```python
194import scvi
195import scanpy as sc
196import anndata
197import torch
199print(f"scvi-tools: {scvi.__version__}")
200print(f"scanpy: {sc.__version__}")
201print(f"anndata: {anndata.__version__}")
202print(f"torch: {torch.__version__}")
204# Check if using 1.x API
205if hasattr(scvi.model.SCVI, 'setup_anndata'):
206 print("Using scvi-tools 1.x API")
207else:
208 print("WARNING: Using deprecated 0.x API - please upgrade")
209```
Check Your Versions
import scvi
import scanpy as sc
import anndata
import torch
print(f"scvi-tools: {scvi.__version__}")
print(f"scanpy: {sc.__version__}")
print(f"anndata: {anndata.__version__}")
print(f"torch: {torch.__version__}")
# Check if using 1.x API
if hasattr(scvi.model.SCVI, 'setup_anndata'):
print("Using scvi-tools 1.x API")
else:
print("WARNING: Using deprecated 0.x API - please upgrade")Raw lines 191 to 210211### Known Compatibility Issues
213| Issue | Affected Versions | Solution |
214|-------|-------------------|----------|
215| `setup_anndata` not found | scvi-tools < 1.0 | Upgrade to 1.0+ |
216| MuData errors | mudata < 0.2 | `pip install mudata>=0.2.0` |
217| CUDA version mismatch | Any | Reinstall PyTorch for your CUDA |
218| numpy 2.0 issues | Early 2024 builds | `pip install numpy<2.0` |
Known Compatibility Issues
| Issue | Affected Versions | Solution |
|---|---|---|
setup_anndata not found | scvi-tools < 1.0 | Upgrade to 1.0+ |
| MuData errors | mudata < 0.2 | pip install mudata>=0.2.0 |
| CUDA version mismatch | Any | Reinstall PyTorch for your CUDA |
| numpy 2.0 issues | Early 2024 builds | pip install numpy<2.0 |
220### Upgrading scvi-tools
222```bash
223# Upgrade to latest
224pip install --upgrade scvi-tools
226# Upgrade all dependencies
227pip install --upgrade scvi-tools scanpy anndata torch
229# If you have issues, clean install
230pip uninstall scvi-tools
231pip cache purge
232pip install scvi-tools
233```
Upgrading scvi-tools
# Upgrade to latest
pip install --upgrade scvi-tools
# Upgrade all dependencies
pip install --upgrade scvi-tools scanpy anndata torch
# If you have issues, clean install
pip uninstall scvi-tools
pip cache purge
pip install scvi-toolsRaw lines 220 to 234235## Testing Installation
237```python
238# Quick test with sample data
239import scvi
240import scanpy as sc
242# Load test dataset
243adata = scvi.data.heart_cell_atlas_subsampled()
244print(f"Loaded test data: {adata.shape}")
246# Setup and create model (quick test)
247scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="cell_source")
248model = scvi.model.SCVI(adata, n_latent=10)
249print("Model created successfully")
251# Quick training test (1 epoch)
252model.train(max_epochs=1)
253print("Training works!")
254```
Testing Installation
# Quick test with sample data
import scvi
import scanpy as sc
# Load test dataset
adata = scvi.data.heart_cell_atlas_subsampled()
print(f"Loaded test data: {adata.shape}")
# Setup and create model (quick test)
scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="cell_source")
model = scvi.model.SCVI(adata, n_latent=10)
print("Model created successfully")
# Quick training test (1 epoch)
model.train(max_epochs=1)
print("Training works!")Raw lines 235 to 254