multigrate-reproducibility / notebooks / benchmarks / individual_datasets / metrics / 10xpbmc10k-metrics.ipynb
10xpbmc10k-metrics.ipynb
Raw
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)\n",
    "\n",
    "import multigrate as mtg\n",
    "import scanpy as sc\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MOFA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 10000 × 13\n",
       "    obs: 'cell_type'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/mofa/10xpbmc10k-mofa.h5ad')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "latent.obsm['latent'] = latent.X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clustering...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.980192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.618968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.770092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.707662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      score\n",
       "graph_conn         0.980192\n",
       "ASW_label          0.618968\n",
       "NMI_cluster/label  0.770092\n",
       "ARI_cluster/label  0.707662"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key=None,\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      isolated_label_asw=False,\n",
    "                      pcr_batch=False,\n",
    "                      asw_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='10xpbmc10k-mofa.csv', \n",
    "                      method='mofa')\n",
    "metrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Seurat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 10000 × 20\n",
       "    obs: 'cell_type', 'nCount_ATAC', 'nFeature_ATAC', 'nCount_SCT', 'nFeature_SCT', 'SCT.weight', 'ATAC.weight'\n",
       "    obsp: 'connectivities'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/seurat/10xpbmc10k-seurat.h5ad')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 10000 × 4000\n",
       "    obs: 'cell_type'\n",
       "    layers: 'count'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rna = sc.read('../data/10xpbmc10k-2020/expressions.h5ad')\n",
    "rna"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(rna.obs_names != latent.obs_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 10000 × 20\n",
       "    obs: 'cell_type', 'nCount_ATAC', 'nFeature_ATAC', 'nCount_SCT', 'nFeature_SCT', 'SCT.weight', 'ATAC.weight'\n",
       "    obsm: 'latent'\n",
       "    obsp: 'connectivities'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent.obsm['latent'] = latent.X\n",
    "latent.obs['cell_type'] = rna.obs['cell_type'].astype('category')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clustering...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.993777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.605867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.804092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.665098</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      score\n",
       "graph_conn         0.993777\n",
       "ASW_label          0.605867\n",
       "NMI_cluster/label  0.804092\n",
       "ARI_cluster/label  0.665098"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key=None,\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      isolated_label_asw=False,\n",
    "                      pcr_batch=False,\n",
    "                      asw_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='10xpbmc10k-seurat.csv', \n",
    "                      method='seurat')\n",
    "metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}