Skip to content

Subsampling Tutorial #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions docs/notebooks/subsampling.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Subsampling cells\n",
"In this tutorial we show how subsampling cells in scverse data objects can be done.\n",
"\n",
"\n",
"This is a placeholder to be filled."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import scanpy as sc\n",
"from imblearn.under_sampling import RandomUnderSampler"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"louvain\n",
"CD4 T cells 1144\n",
"CD14+ Monocytes 480\n",
"B cells 342\n",
"CD8 T cells 316\n",
"NK cells 154\n",
"FCGR3A+ Monocytes 150\n",
"Dendritic cells 37\n",
"Megakaryocytes 15\n",
"Name: count, dtype: int64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adata = sc.datasets.pbmc3k_processed()\n",
"adata.obs.louvain.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/eljasroellin/Documents/scverse-tutorials/tutorials_venv_mar/lib/python3.11/site-packages/anndata/_core/aligned_df.py:67: ImplicitModificationWarning: Transforming to str index.\n",
" warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n"
]
},
{
"data": {
"text/plain": [
"louvain\n",
"CD4 T cells 15\n",
"CD14+ Monocytes 15\n",
"B cells 15\n",
"CD8 T cells 15\n",
"NK cells 15\n",
"FCGR3A+ Monocytes 15\n",
"Dendritic cells 15\n",
"Megakaryocytes 15\n",
"Name: count, dtype: int64"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rus = RandomUnderSampler(random_state=0)\n",
"X_resampled, y_resampled = rus.fit_resample(adata.X, adata.obs.louvain)\n",
"adata_resampled = sc.AnnData(X_resampled, obs=pd.DataFrame(y_resampled)) ##adata[y_resampled]\n",
"adata_resampled.obs.louvain.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "tutorials_venv_mar",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tutorial-registry/tutorials/large-datasets/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Subsampling cells
description: |
In this tutorial, we demonstrate subsampling with scverse data objects.
link: # TODO https://scverse-tutorials.readthedocs.io/en/latest/notebooks/scverse_data_backed.html#working-with-scverse-objects-in-backed-mode
primary_category: Large Datasets # Data structures
image: icon.png
tags:
- large datasets
packages:
- anndata
- mudata
- scanpy
authors:
- eroell
Loading