File size: 10,885 Bytes
1aacd08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
{
"cells": [
{
"cell_type": "markdown",
"id": "837a8ecd",
"metadata": {},
"source": [
"# VCC Submission Notebook\n",
"\n",
"Hello! \n",
"\n",
"This is a notebook that will help you prepare your predicted AnnData to be ready to be scored by `cell-eval` against a validation dataset.\n",
"\n",
"Before we begin you will need a few things:\n",
"\n",
"1. `cell-eval` installed and in your `$PATH` (see our [installation guide](https://github.com/ArcInstitute/cell-eval?tab=readme-ov-file#installation))\n",
"2. The number of expected cells / perturbation in the validation dataset (CSV) ([download](https://virtualcellchallenge.org/app))\n",
"3. The gene names to predict (CSV) ([download](https://virtualcellchallenge.org/app))\n",
"4. Your model predictions in an AnnData (h5ad)\n",
"5. (Optional) The training AnnData (if you are not predicting Non-Targeting Controls) ([download](https://virtualcellchallenge.org/app))\n",
"\n",
"\n",
"> Note: Your model predictions **may not exceed 100K cells total**"
]
},
{
"cell_type": "markdown",
"id": "b5cc204d",
"metadata": {},
"source": [
"## Building an Example Submission\n",
"\n",
"For the purposes of this tutorial we will be generating **random predictions** and preparing them to be evaluated.\n",
"\n",
"We will create an AnnData with *random gene abundances* for each cell, where the number of cells for each perturbation matches the number of cells in the validation dataset."
]
},
{
"cell_type": "markdown",
"id": "3e9c543f",
"metadata": {},
"source": [
"### Load in our Expected Counts"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2d172eea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimensions: (50, 3)\n"
]
},
{
"data": {
"text/html": [
"<div><style>\n",
".dataframe > thead > tr,\n",
".dataframe > tbody > tr {\n",
" text-align: right;\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<small>shape: (5, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>target_gene</th><th>n_cells</th><th>median_umi_per_cell</th></tr><tr><td>str</td><td>i64</td><td>f64</td></tr></thead><tbody><tr><td>"SH3BP4"</td><td>2925</td><td>54551.0</td></tr><tr><td>"ZNF581"</td><td>2502</td><td>53803.5</td></tr><tr><td>"ANXA6"</td><td>2496</td><td>55175.0</td></tr><tr><td>"PACSIN3"</td><td>2101</td><td>54088.0</td></tr><tr><td>"MGST1"</td><td>2096</td><td>54217.5</td></tr></tbody></table></div>"
],
"text/plain": [
"shape: (5, 3)\n",
"βββββββββββββββ¬ββββββββββ¬ββββββββββββββββββββββ\n",
"β target_gene β n_cells β median_umi_per_cell β\n",
"β --- β --- β --- β\n",
"β str β i64 β f64 β\n",
"βββββββββββββββͺββββββββββͺββββββββββββββββββββββ‘\n",
"β SH3BP4 β 2925 β 54551.0 β\n",
"β ZNF581 β 2502 β 53803.5 β\n",
"β ANXA6 β 2496 β 55175.0 β\n",
"β PACSIN3 β 2101 β 54088.0 β\n",
"β MGST1 β 2096 β 54217.5 β\n",
"βββββββββββββββ΄ββββββββββ΄ββββββββββββββββββββββ"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import polars as pl\n",
"\n",
"# Define our path\n",
"pert_counts_path = \"/home/datahouse/yijia/Projects/VCC2025/VirtualCellChallenge2025/Downloads/vcc_data/pert_counts_Validation.csv\"\n",
"\n",
"# Read in the csv\n",
"pert_counts = pl.read_csv(pert_counts_path)\n",
"\n",
"# Show the dimensions\n",
"print(f\"Dimensions: {pert_counts.shape}\")\n",
"pert_counts.head()"
]
},
{
"cell_type": "markdown",
"id": "34164d85",
"metadata": {},
"source": [
"### Load in our Expected Gene Names"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "04e3cfad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['SAMD11', 'NOC2L', 'KLHL17', ..., 'MT-ND5', 'MT-ND6', 'MT-CYB'],\n",
" dtype=object)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gene_names_path = \"/home/datahouse/yijia/Projects/VCC2025/VirtualCellChallenge2025/Downloads/vcc_data/gene_names.csv\"\n",
"\n",
"# Read this in and immediately convert to array\n",
"gene_names = pl.read_csv(gene_names_path, has_header=False).to_numpy().flatten()\n",
"\n",
"gene_names"
]
},
{
"cell_type": "markdown",
"id": "b6895f5b",
"metadata": {},
"source": [
"### Define our random predictor"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0262449f",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from numpy.typing import NDArray\n",
"import anndata as ad\n",
"\n",
"\n",
"def random_predictor(\n",
" pert_names: NDArray[np.str_],\n",
" cell_counts: NDArray[np.int64],\n",
" gene_names: NDArray[np.str_],\n",
" max_count: int | float = 1e4,\n",
" log1p: bool = True,\n",
") -> ad.AnnData:\n",
" \"\"\"Generate a random AnnData with the expected number of cells / perturbation.\n",
"\n",
" This is a dummy function that is meant to stand-in for a perturbation model.\n",
" \"\"\"\n",
" matrix = np.random.randint(\n",
" 0,\n",
" int(max_count),\n",
" size=(\n",
" cell_counts.sum(),\n",
" gene_names.size,\n",
" ),\n",
" )\n",
" if log1p:\n",
" matrix = np.log1p(matrix)\n",
" return ad.AnnData(\n",
" X=matrix,\n",
" obs=pd.DataFrame(\n",
" {\n",
" \"target_gene\": np.repeat(pert_names, cell_counts),\n",
" },\n",
" index=np.arange(cell_counts.sum()).astype(str),\n",
" ),\n",
" var=pd.DataFrame(index=gene_names),\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "d5dbd56d",
"metadata": {},
"source": [
"### Run our random predictor"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1273bea1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AnnData object with n_obs Γ n_vars = 60751 Γ 18080\n",
" obs: 'target_gene'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adata = random_predictor(\n",
" pert_names=pert_counts[\"target_gene\"].to_numpy(),\n",
" cell_counts=pert_counts[\"n_cells\"].to_numpy(),\n",
" gene_names=gene_names,\n",
")\n",
"adata"
]
},
{
"cell_type": "markdown",
"id": "9f9de9dd",
"metadata": {},
"source": [
"### Adding in Non-Targeting Controls if you are not predicting them\n",
"\n",
"Our evaluation framework expects non-targeting controls to be included in the predicted AnnData, but not all models may try to predict non-targeting controls.\n",
"If you are not predicting non-targeting controls, you can take the non-targeting from the training AnnData and just copy them over into your predicted AnnData for validation."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3879267c",
"metadata": {},
"outputs": [],
"source": [
"# Define our path to the training anndata\n",
"tr_adata_path = \"/home/datahouse/yijia/Projects/VCC2025/VirtualCellChallenge2025/Downloads/vcc_data//adata_Training.h5ad\"\n",
"\n",
"# Read in the anndata\n",
"tr_adata = ad.read_h5ad(tr_adata_path)\n",
"\n",
"# Filter for non-targeting\n",
"ntc_adata = tr_adata[tr_adata.obs[\"target_gene\"] == \"non-targeting\"]\n",
"\n",
"# Append the non-targeting controls to the example anndata if they're missing\n",
"if \"non-targeting\" not in adata.obs[\"target_gene\"].unique():\n",
" assert np.all(adata.var_names.values == ntc_adata.var_names.values), (\n",
" \"Gene-Names are out of order or unequal\"\n",
" )\n",
" adata = ad.concat(\n",
" [\n",
" adata,\n",
" ntc_adata,\n",
" ]\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "6f19ac72",
"metadata": {},
"source": [
"### Write our predictions to some output path"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "386ee994",
"metadata": {},
"outputs": [],
"source": [
"adata.write_h5ad(\"./cell-eval-tutorial-output-example.h5ad\")"
]
},
{
"cell_type": "markdown",
"id": "29cd57ef",
"metadata": {},
"source": [
"## Running `cell-eval prep`\n",
"\n",
"Now that we have our predictions, we will run `cell-eval` to prepare our AnnData for competition scoring."
]
},
{
"cell_type": "markdown",
"id": "7716a83e",
"metadata": {},
"source": [
"```bash\n",
"cell-eval prep \\\n",
" -i ./example.h5ad \\\n",
" --genes ./gene_names.csv\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e2d42201",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/datahouse/yijia/Projects/VCC2025/cell-eval/cell-eval/tutorials/vcc\n"
]
}
],
"source": [
"import os\n",
"print(os.getcwd())\n"
]
},
{
"cell_type": "markdown",
"id": "6f938a60",
"metadata": {},
"source": [
"And that's it! Your model outputs will be output to path: `./example.prep.vcc` are ready for scoring."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|