{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3346d0c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Created AnnData: AnnData object with n_obs × n_vars = 200 × 500\n",
      "    obs: 'cell_type', 'batch'\n",
      "    var: 'gene_name'\n",
      "    obsm: 'X_pca', 'X_umap'\n",
      "Shape: (200, 500)\n",
      "Embeddings: ['X_pca', 'X_umap']"
     ]
    }
   ],
   "source": [
    "import scanpy as sc\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个测试 AnnData 对象\n",
    "n_obs = 200\n",
    "n_vars = 500\n",
    "\n",
    "# 创建随机表达矩阵\n",
    "X = np.random.negative_binomial(5, 0.3, (n_obs, n_vars))\n",
    "\n",
    "# 创建 obs (细胞注释)\n",
    "obs = pd.DataFrame({\n",
    "    'cell_type': np.random.choice(['TypeA', 'TypeB', 'TypeC'], n_obs),\n",
    "    'batch': np.random.choice(['batch1', 'batch2'], n_obs)\n",
    "})\n",
    "\n",
    "# 创建 var (基因注释)\n",
    "var = pd.DataFrame({\n",
    "    'gene_name': [f'Gene_{i}' for i in range(n_vars)]\n",
    "})\n",
    "\n",
    "# 创建 AnnData\n",
    "test_adata = sc.AnnData(X=X, obs=obs, var=var)\n",
    "\n",
    "# 添加一些 embedding (用于可视化)\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.manifold import TSNE\n",
    "\n",
    "# PCA\n",
    "pca = PCA(n_components=50)\n",
    "X_pca = pca.fit_transform(test_adata.X)\n",
    "test_adata.obsm['X_pca'] = X_pca\n",
    "\n",
    "# UMAP (简化版，使用 t-SNE 代替)\n",
    "tsne = TSNE(n_components=2, random_state=42)\n",
    "X_tsne = tsne.fit_transform(X_pca[:, :10])\n",
    "test_adata.obsm['X_umap'] = X_tsne\n",
    "\n",
    "print(f\"Created AnnData: {test_adata}\")\n",
    "print(f\"Shape: {test_adata.shape}\")\n",
    "print(f\"Embeddings: {list(test_adata.obsm.keys())}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1c246fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Step 0\n",
      "Step 1\n",
      "Step 2\n",
      "Step 3\n",
      "Step 4\n",
      "Step 5\n",
      "Step 6\n",
      "Step 7\n",
      "Step 8\n",
      "Step 9"
     ]
    }
   ],
   "source": [
    "import time                                                                                                                                                                                                                                      \n",
    "for i in range(10):                                                                                                                                                                                                                              \n",
    "    print(f\"Step {i}\")                                                                                                                                                                                                                           \n",
    "    time.sleep(0.5)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08b759e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Success, no output)"
     ]
    }
   ],
   "source": [
    "from __future__ import annotations\n",
    "\n",
    "import scanpy as sc\n",
    "from matplotlib.pyplot import rc_context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "877a0389",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Success, no output)"
     ]
    }
   ],
   "source": [
    "sc.set_figure_params(dpi=100, color_map=\"viridis_r\")\n",
    "sc.settings.verbosity = 0\n",
    "sc.logging.print_header()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "151aab33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AnnData object with n_obs × n_vars = 700 × 765\n",
      "    obs: 'bulk_labels', 'n_genes', 'percent_mito', 'n_counts', 'S_score', 'G2M_score', 'phase', 'louvain'\n",
      "    var: 'n_counts', 'means', 'dispersions', 'dispersions_norm', 'highly_variable'\n",
      "    uns: 'bulk_labels_colors', 'louvain', 'louvain_colors', 'neighbors', 'pca', 'rank_genes_groups'\n",
      "    obsm: 'X_pca', 'X_umap'\n",
      "    varm: 'PCs'\n",
      "    obsp: 'distances', 'connectivities'\n"
     ]
    }
   ],
   "source": [
    "pbmc = sc.datasets.pbmc68k_reduced()\n",
    "print(pbmc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c99f2062",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Success, no output)"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 52056 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# rc_context is used for the figure size, in this case 4x4\n",
    "with rc_context({\"figure.figsize\": (4, 4)}):\n",
    "    sc.pl.umap(pbmc, color=\"CD79A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e80fe21",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
