multigrate-reproducibility / notebooks / benchmarks / individual_datasets / metrics / sanger2021-metrics.ipynb
sanger2021-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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MOFA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AnnData object with n_obs × n_vars = 6334 × 13\n",
       "    obs: 'n_genes', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'total_counts_mt', 'log1p_total_counts_mt', 'pct_counts_mt', 'cell_type', 'batch'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/mofa/sagner-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",
      "ASW label/batch...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n",
      "Isolated label silhouette...\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>ASW_label/batch</th>\n",
       "      <td>0.916624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.882245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.588075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.740319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.553285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isolated_label_silhouette</th>\n",
       "      <td>0.540644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              score\n",
       "ASW_label/batch            0.916624\n",
       "graph_conn                 0.882245\n",
       "ASW_label                  0.588075\n",
       "NMI_cluster/label          0.740319\n",
       "ARI_cluster/label          0.553285\n",
       "isolated_label_silhouette  0.540644"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key='batch',\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      pcr_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='sagner-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 = 6334 × 20\n",
       "    obs: 'cell_type', 'batch', 'nCount_RNA', 'nFeature_RNA', 'nCount_SCT', 'nFeature_SCT', 'integrated.weight'\n",
       "    obsp: 'connectivities'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latent = sc.read('../data/integrated/seurat/sagner-seurat.h5ad')\n",
    "latent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "latent.obsm['latent'] = latent.X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clustering...\n",
      "ASW label/batch...\n",
      "Graph connectivity...\n",
      "ASW label...\n",
      "NMI cluster/label...\n",
      "ARI cluster/label...\n",
      "Isolated label silhouette...\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>ASW_label/batch</th>\n",
       "      <td>0.927233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>graph_conn</th>\n",
       "      <td>0.977470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ASW_label</th>\n",
       "      <td>0.662901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NMI_cluster/label</th>\n",
       "      <td>0.881507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARI_cluster/label</th>\n",
       "      <td>0.864936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isolated_label_silhouette</th>\n",
       "      <td>0.560829</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              score\n",
       "ASW_label/batch            0.927233\n",
       "graph_conn                 0.977470\n",
       "ASW_label                  0.662901\n",
       "NMI_cluster/label          0.881507\n",
       "ARI_cluster/label          0.864936\n",
       "isolated_label_silhouette  0.560829"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics = mtg.metrics.metrics(None, latent,\n",
    "                      batch_key='batch',\n",
    "                      label_key='cell_type',\n",
    "                      isolated_label_f1=False,\n",
    "                      pcr_batch=False,\n",
    "                      embed='latent', \n",
    "                      save='sagner-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
}