Skip to content

Commit 1cc7b98

Browse files
authored
Merge pull request #418 from scverse/ori-kron-wis-patch-1
Update interpretability.ipynb
2 parents e68df0f + c102948 commit 1cc7b98

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

scrna/velovi.ipynb

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"source": [
3131
"!pip install --quiet scvi-colab\n",
3232
"from scvi_colab import install\n",
33+
"\n",
3334
"install()"
3435
]
3536
},
@@ -47,15 +48,14 @@
4748
}
4849
],
4950
"source": [
51+
"import matplotlib.pyplot as plt\n",
5052
"import numpy as np\n",
5153
"import pandas as pd\n",
5254
"import scanpy as sc\n",
5355
"import scvelo as scv\n",
56+
"import seaborn as sns\n",
5457
"import torch\n",
55-
"from velovi import preprocess_data, VELOVI\n",
56-
"\n",
57-
"import matplotlib.pyplot as plt\n",
58-
"import seaborn as sns"
58+
"from velovi import VELOVI, preprocess_data"
5959
]
6060
},
6161
{
@@ -221,13 +221,11 @@
221221
" adata.var[\"fit_beta\"] = vae.get_rates()[\"beta\"] / scaling\n",
222222
" adata.var[\"fit_gamma\"] = vae.get_rates()[\"gamma\"] / scaling\n",
223223
" adata.var[\"fit_t_\"] = (\n",
224-
" torch.nn.functional.softplus(vae.module.switch_time_unconstr)\n",
225-
" .detach()\n",
226-
" .cpu()\n",
227-
" .numpy()\n",
224+
" torch.nn.functional.softplus(vae.module.switch_time_unconstr).detach().cpu().numpy()\n",
228225
" ) * scaling\n",
229226
" adata.layers[\"fit_t\"] = latent_time.values * scaling[np.newaxis, :]\n",
230-
" adata.var['fit_scaling'] = 1.0\n",
227+
" adata.var[\"fit_scaling\"] = 1.0\n",
228+
"\n",
231229
"\n",
232230
"add_velovi_outputs_to_adata(adata, vae)"
233231
]
@@ -297,7 +295,7 @@
297295
}
298296
],
299297
"source": [
300-
"scv.pl.velocity_embedding_stream(adata, basis='umap')"
298+
"scv.pl.velocity_embedding_stream(adata, basis=\"umap\")"
301299
]
302300
},
303301
{
@@ -507,7 +505,7 @@
507505
],
508506
"source": [
509507
"sc.pl.umap(\n",
510-
" adata, \n",
508+
" adata,\n",
511509
" color=\"directional_cosine_sim_variance\",\n",
512510
" cmap=\"Greys\",\n",
513511
" vmin=\"p1\",\n",
@@ -529,15 +527,16 @@
529527
"outputs": [],
530528
"source": [
531529
"def compute_extrinisic_uncertainty(adata, vae, n_samples=25) -> pd.DataFrame:\n",
532-
" from velovi._model import _compute_directional_statistics_tensor\n",
533-
" from scvi.utils import track\n",
534-
" from contextlib import redirect_stdout\n",
535530
" import io\n",
531+
" from contextlib import redirect_stdout\n",
532+
"\n",
533+
" from scvi.utils import track\n",
534+
" from velovi._model import _compute_directional_statistics_tensor\n",
536535
"\n",
537536
" extrapolated_cells_list = []\n",
538537
" for i in track(range(n_samples)):\n",
539538
" with io.StringIO() as buf, redirect_stdout(buf):\n",
540-
" vkey = \"velocities_velovi_{i}\".format(i=i)\n",
539+
" vkey = f\"velocities_velovi_{i}\"\n",
541540
" v = vae.get_velocity(n_samples=1, velo_statistic=\"mean\")\n",
542541
" adata.layers[vkey] = v\n",
543542
" scv.tl.velocity_graph(adata, vkey=vkey, sqrt_transform=False, approx=True)\n",
@@ -1134,10 +1133,10 @@
11341133
],
11351134
"source": [
11361135
"sc.pl.umap(\n",
1137-
" adata, \n",
1136+
" adata,\n",
11381137
" color=\"directional_cosine_sim_variance_extrinisic\",\n",
1139-
" vmin=\"p1\", \n",
1140-
" vmax=\"p99\", \n",
1138+
" vmin=\"p1\",\n",
1139+
" vmax=\"p99\",\n",
11411140
")"
11421141
]
11431142
},

use_cases/interpretability.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@
909909
],
910910
"source": [
911911
"df_class = attributions_class.head(n_plot)\n",
912-
"ci = 1.96 * df[\"attribution_std\"] / np.sqrt(df[\"cells\"])\n",
912+
"ci = 1.96 * df_class[\"attribution_std\"] / np.sqrt(df_class[\"cells\"])\n",
913913
"fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 2), dpi=200)\n",
914914
"sns.barplot(ax=ax, data=df_class, x=\"gene\", y=\"attribution_mean\", hue=\"gene\", dodge=False)\n",
915915
"ax.set_yticks([])\n",
@@ -943,7 +943,7 @@
943943
"cell_type": "markdown",
944944
"metadata": {},
945945
"source": [
946-
"SHAP (SHapley Additive exPlanations) values are a popular interpretability technique based on cooperative game theory. The core idea is to fairly allocate the \"credit\" for a model's prediction to each feature, by considering all possible combinations of features and their impact on the prediction. SHAP values are additive, meaning the sum of the SHAP values for all features equals the difference between the model’s output and the average prediction. This method works for any model type, providing a consistent way to explain individual predictions, making it highly versatile and widely applicable. Deep SHAP is an extension of the SHAP method designed specifically for deep learning models, such as the ones in SCVI-Tools. For more information see [this](\"https://www.nature.com/articles/s41592-024-02511-3\")\n",
946+
"SHAP (SHapley Additive exPlanations) values are a popular interpretability technique based on cooperative game theory. The core idea is to fairly allocate the \"credit\" for a model's prediction to each feature, by considering all possible combinations of features and their impact on the prediction. SHAP values are additive, meaning the sum of the SHAP values for all features equals the difference between the model’s output and the average prediction. This method works for any model type, providing a consistent way to explain individual predictions, making it highly versatile and widely applicable. Deep SHAP is an extension of the SHAP method designed specifically for deep learning models, such as the ones in SCVI-Tools. For more information see [this](https://www.nature.com/articles/s41592-024-02511-3)\n",
947947
"\n",
948948
"Calcualtion of SHAP for SC data usually takes a lot of time. In SCVI-Tools we are running an approximation of SHAP in order to reduce runtime, where we use just 100 cells at each iteration."
949949
]

0 commit comments

Comments
 (0)