{ "cells": [ { "cell_type": "markdown", "id": "3852c373", "metadata": {}, "source": [ "# Generalization of Steamboat to Visium Data\n", "\n", "- Data: [10x Genomics: Human Breast Cancer (Block A Section 1)](https://www.10xgenomics.com/datasets/human-breast-cancer-block-a-section-1-1-standard-1-1-0)\n", "- Tasks: Test how much Steamboat generalizes to spot-level data" ] }, { "cell_type": "code", "execution_count": 1, "id": "674f3337", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\dask\\dataframe\\__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.\n", " warnings.warn(\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\spatialdata\\_core\\query\\relational_query.py:530: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in enum.member() if you want to preserve the old behavior\n", " left = partial(_left_join_spatialelement_table)\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\spatialdata\\_core\\query\\relational_query.py:531: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in enum.member() if you want to preserve the old behavior\n", " left_exclusive = partial(_left_exclusive_join_spatialelement_table)\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\spatialdata\\_core\\query\\relational_query.py:532: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in enum.member() if you want to preserve the old behavior\n", " inner = partial(_inner_join_spatialelement_table)\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\spatialdata\\_core\\query\\relational_query.py:533: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in enum.member() if you want to preserve the old behavior\n", " right = partial(_right_join_spatialelement_table)\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\spatialdata\\_core\\query\\relational_query.py:534: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in enum.member() if you want to preserve the old behavior\n", " right_exclusive = partial(_right_exclusive_join_spatialelement_table)\n" ] } ], "source": [ "import sys\n", "sys.path.append(\"../\") # Append the parent directory to Steamboat package, not needed if installed via pip\n", "\n", "import scanpy as sc\n", "import squidpy as sq\n", "import pandas as pd\n", "from tqdm.notebook import tqdm\n", "import scipy as sp\n", "import numpy as np\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import pickle as pkl\n", "import torch\n", "import gc\n", "\n", "from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score\n", "\n", "import steamboat as sf\n", "\n", "device = \"cuda\" # this dataset is small and should also work with \"cpu\"\n", "\n", "\n", "plt.rcParams['pdf.fonttype'] = 42\n", "matplotlib.rcParams['mathtext.fontset'] = 'dejavuserif'\n", "matplotlib.rcParams['font.family'] = 'arial'" ] }, { "cell_type": "markdown", "id": "43792097", "metadata": {}, "source": [ "## Load data" ] }, { "cell_type": "code", "execution_count": 2, "id": "6406c663", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\anndata\\_core\\anndata.py:1798: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n", "c:\\Users\\lshh\\miniconda3\\envs\\py313_torch291_cuda130\\Lib\\site-packages\\anndata\\_core\\anndata.py:1798: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] } ], "source": [ "adata = sq.read.visium(\"../../SEDR_analyses/data/BRCA1/V1_Human_Breast_Cancer_Block_A_Section_1\")\n", "annotation = pd.read_csv(\"../../SEDR_analyses/data/BRCA1/metadata.tsv\", sep=\"\\t\", index_col=0)\n", "adata.obs = adata.obs.join(annotation)\n", "sc.pp.normalize_total(adata)\n", "# Logarithmize the data\n", "sc.pp.log1p(adata)\n", "sc.pp.highly_variable_genes(adata)\n", "adata.X = np.array(adata.X.todense())" ] }, { "cell_type": "markdown", "id": "76b8309a", "metadata": {}, "source": [ "## Train Steamboat model" ] }, { "cell_type": "code", "execution_count": 3, "id": "8ed3a505", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f3eaa218d42047f5855ae6c2dab06854", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sf.tools.calc_obs(adatas, dataset, model, get_recon=True)\n", "\n", "i = 0\n", "sf.tools.neighbors(adatas[i], use_rep='attn')\n", "sf.tools.leiden(adatas[i], resolution=0.6)\n", "\n", "fig, axes = plt.subplots(1, 2, figsize=(8, 4))\n", "sq.pl.spatial_scatter(adatas[i], color=[\"fine_annot_type\", \"steamboat_clusters\"], shape=None, fig=fig, ax=axes, size=2.5, frameon=False)\n", "\n", "labels_pred = adata.obs['steamboat_clusters'].values\n", "labels_true = adata.obs['fine_annot_type'].values\n", "nmi = normalized_mutual_info_score(labels_true, labels_pred)\n", "ari = adjusted_rand_score(labels_true, labels_pred)\n", "\n", "n_clusters = adata.obs['steamboat_clusters'].nunique()\n", "\n", "print(f\"NMI: {nmi:.4f}, ARI: {ari:.4f}, n_clusters: {n_clusters}\")\n", "\n", "axes[0].set_title(\"Manual annotation\", fontsize=11)\n", "axes[1].set_title(\"Steamboat clusters\", fontsize=11)\n", "fig.tight_layout(pad=2.)" ] } ], "metadata": { "kernelspec": { "display_name": "py313_torch291_cuda130", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.9" } }, "nbformat": 4, "nbformat_minor": 5 }