{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import wot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "# Notebook 2: Computing transport matrices\n",
    "\n",
    "In this notebook we compute transport matrices connecting each pair of time-points, and we examine the effect of each parameter on the solution. \n",
    "\n",
    "To compute the transport matrix $\\pi_{t_1,t_2}$ connecting cells $x_1, \\ldots, x_n$ at time $t_1$ to cells $y_1, \\ldots, y_m$ at time $t_2$, we solve an optimization problem over all matrices $\\pi$ that obey certain row-sum and column-sum constraints. \n",
    "These constraints ensure that the total amount of mass flowing out of each cell $x_i$ and into each cell $y_j$ adds up the correct amount. \n",
    "We select the transport matrix with the lowest possible transport cost, subject to these constraints. \n",
    "\n",
    "The amount of mass flowing out of each cell $x_i$ depends on the *growth rate* of the cell. We do this in step 1 below, before proceeding to computing transport maps in step 2. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "# Step 1: Construct initial estimate of cell growth rates (optional)\n",
    "\n",
    "Before computing transport maps, we first form an initial estimate of the cell growth rate function $g(x)$. \n",
    "We do this using signatures of proliferation and apoptosis computed in Notebook 1. \n",
    "Note that we refine this initial estimate using unbalanced optimal transport, as explained later in this notebook. \n",
    "\n",
    "We model cellular growth with a Birth-Death Process, which assigns each cell $x$ a *rate* of division $\\beta(x)$ and a  rate of death $\\delta(x)$. \n",
    "In a small interval of time $dt$, these *rates* imply that the probability of a division is $\\beta(x)dt$, and the probability of death is $\\delta(x) dt$. \n",
    "The expected number of descendants after time $dt = t_2 - t_1$ is $e^{dt(\\beta(x) - \\delta(x))}$.\n",
    "\n",
    "To compute the birth (i.e. division) rate and death rate, we apply a logistic function to transform the gene set scores into biologically plausible vaules for our reprogramming dataset (where the most proliferative cells double every 9 hours, and the least proliferative cells double every 60 hours). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# load proliferation and apoptosis scores\n",
    "gene_set_scores = pd.read_csv('data/gene_set_scores.csv', index_col=0)\n",
    "proliferation=gene_set_scores['Cell.cycle']\n",
    "apoptosis = gene_set_scores['Apoptosis']\n",
    "\n",
    "# apply logistic function to transform to birth rate and death rate\n",
    "def logistic(x, L, k, x0=0):\n",
    "    f = L / (1 + np.exp(-k * (x - x0)))\n",
    "    return f\n",
    "def gen_logistic(p, beta_max, beta_min, pmax, pmin, center, width):\n",
    "    return beta_min + logistic(p, L=beta_max - beta_min, k=4 / width, x0=center)\n",
    "\n",
    "def beta(p, beta_max=1.7, beta_min=0.3, pmax=1.0, pmin=-0.5, center=0.25):\n",
    "    return gen_logistic(p, beta_max, beta_min, pmax, pmin, center, width=0.5)\n",
    "\n",
    "def delta(a, delta_max=1.7, delta_min=0.3, amax=0.5, amin=-0.4, center=0.1):\n",
    "    return gen_logistic(a, delta_max, delta_min, amax, amin, center,\n",
    "                          width=0.2)\n",
    "\n",
    "birth = beta(proliferation)\n",
    "death = delta(apoptosis)\n",
    "\n",
    "# growth rate is given by \n",
    "gr = np.exp(birth-death)\n",
    "growth_rates_df = pd.DataFrame(index=gene_set_scores.index, data={'cell_growth_rate':gr})\n",
    "growth_rates_df.to_csv('data/growth_gs_init.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "j1JdUgTXawEJ",
    "pycharm": {}
   },
   "source": [
    "# Step 2: Compute transport maps\n",
    "\n",
    "We are now ready to compute transport maps. \n",
    "\n",
    "The next code block reads in the expression matrix, cell days, and the initial cell growth rates we computed in step 1 above. If the cell growth rates are not provided, the algorithm will assume each cell grows at roughly the same rate.  Note that the expression matrix we use here contains variable gene expression data of 1,400 genes from two time-course experiments, and we apply a filter to select cells from the serum time course. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "OgHGRa2aIax6",
    "pycharm": {}
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(175472, 1479)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "VAR_GENE_DS_PATH = 'data/ExprMatrix.var.genes.h5ad'\n",
    "CELL_DAYS_PATH = 'data/cell_days.txt'\n",
    "SERUM_CELL_IDS_PATH = 'data/serum_cell_ids.txt'\n",
    "CELL_GROWTH_PATH = 'data/growth_gs_init.txt'\n",
    "\n",
    "# load data\n",
    "adata = wot.io.read_dataset(VAR_GENE_DS_PATH, obs=[CELL_DAYS_PATH, CELL_GROWTH_PATH], obs_filter=SERUM_CELL_IDS_PATH)\n",
    "adata.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "eXxmqmQua1I0",
    "pycharm": {}
   },
   "source": [
    "# The optimization problem\n",
    "\n",
    "We solve the following *unbalanced transport* optimization problem introduced in Chizat et al 2018, where we only enforce the row-sum constraints approximately and we add entropy to the transport matrix: \n",
    "\n",
    "\\begin{equation}\n",
    "\\begin{aligned}\n",
    "\\underset{\\pi}{\\text{minimize}} & \\qquad \\iint c(x,y) \\pi(x,y) dx dy - \\epsilon \\int \\pi(x,y) \\log \\pi(x,y) dx dy \\\\\n",
    "&\\qquad + \\lambda_2 {\\text{KL}} \\left ( \\int \\pi(x,y) dx  \\Big \\vert d \\hat {\\mathbb{P}}_{t_2} (y) \\int g(x)^{t_2 - t_1} d \\hat {\\mathbb{P}}_{t_1}(x) \\right ) \\\\ \n",
    "& \\qquad + \\lambda_1 {\\text{KL}} \\left ( \\int \\pi(x,y) dy \\Big \\vert d \\hat {\\mathbb{P}}_{t_1} (x) g(x)^{t_2 - t_1} \\right). \n",
    "\\end{aligned}\n",
    "\\end{equation}\n",
    "\n",
    "Here we use the notation  $\\hat {\\mathbb{P}}_{t_k} = \\frac 1 n \\sum_{i=1}^n \\delta_{x_i}$ for the empirical distribution of samples $x_1,\\ldots,x_n$ at time $t_k$, and $\\text{KL}(P \\vert Q)$ denotes the KL-divergence between distributions $P$ and $Q$. The function $c(x,y)$ encodes the cost of transporting a unit mass from $x$ to $y$. We define $c(x,y)$ to be the squared euclidean distance between cells in local PCA space. This PCA space is computed separately for each pair of time-points. Finally, the function $g(x)$ encodes the growth rate of cell $x$, and is used to specify the budget of descendant mass for each cell $x_i$ at time $t_1$. \n",
    "\n",
    "The optimization problem has three regularization parameters: \n",
    "* $\\epsilon$ controls the degree of entropy in the transport map. \n",
    "A larger value gives more entropic descendant distributions, where cells are able to obtain more fates. \n",
    "* $\\lambda_1$ controls the constraint on the row sums of $\\pi_{t_1,t_2}$, which depend on the growth rate function $g(x)$ \n",
    "A smaller value of $\\lambda_1$ enforces the constraints less strictly, which is useful when we do not have precise information about $g(x)$.\n",
    "* $\\lambda_2$ controls the constraint on the column sums of $\\pi_{t_1,t_2}$.\n",
    "\n",
    "In the following code block we initialize an OTModel, which is parameterized by $\\epsilon, \\lambda_1, \\lambda_2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1558,
     "status": "ok",
     "timestamp": 1556313217324,
     "user": {
      "displayName": "Joshua Gould",
      "photoUrl": "",
      "userId": "09978885733763067346"
     },
     "user_tz": 240
    },
    "id": "YKuGivoIca9G",
    "outputId": "c0639c77-d5ed-4e9e-9130-b994bfad5e9c",
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# create OTModel\n",
    "ot_model = wot.ot.OTModel(adata,epsilon = 0.05, lambda1 = 1,lambda2 = 50) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now use the OTModel to compute transport maps between any pair of time-points. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute a single transport map from day 7 to 7.5\n",
    "tmap_annotated = ot_model.compute_transport_map(7,7.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "The object tmap_annotated is an annotated transport matrix, and tmap_annotated.X contains the actual numerical matrix. \n",
    "Both rows and columns are annotated with cell barcodes and cell growth rates (just along rows). These annotations are accessed as follows: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {},
    "scrolled": true
   },
   "outputs": [
    {
     "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>g0</th>\n",
       "      <th>g1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGAGGAATCGC-1</th>\n",
       "      <td>1.268895</td>\n",
       "      <td>0.977589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGAGTAGATGT-1</th>\n",
       "      <td>2.978660</td>\n",
       "      <td>2.510199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCAACCGCCA-1</th>\n",
       "      <td>1.871918</td>\n",
       "      <td>1.789104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCACTATCTT-1</th>\n",
       "      <td>3.072989</td>\n",
       "      <td>2.790364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCAGCCTGTG-1</th>\n",
       "      <td>1.026225</td>\n",
       "      <td>0.918584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCATATACGC-1</th>\n",
       "      <td>1.412582</td>\n",
       "      <td>1.366065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCATCCGTGG-1</th>\n",
       "      <td>1.182596</td>\n",
       "      <td>1.144585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGTCGGAATCT-1</th>\n",
       "      <td>0.982687</td>\n",
       "      <td>1.047557</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGGTAGTACCT-1</th>\n",
       "      <td>0.417277</td>\n",
       "      <td>0.443068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTCTTATG-1</th>\n",
       "      <td>0.348761</td>\n",
       "      <td>0.282756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTGGGCCA-1</th>\n",
       "      <td>2.636122</td>\n",
       "      <td>2.038604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTGTCAAG-1</th>\n",
       "      <td>0.399207</td>\n",
       "      <td>0.446110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGAGAGCTC-1</th>\n",
       "      <td>1.564738</td>\n",
       "      <td>1.329046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGAGTACAT-1</th>\n",
       "      <td>1.485496</td>\n",
       "      <td>1.384023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGATCCGAG-1</th>\n",
       "      <td>0.699411</td>\n",
       "      <td>0.789952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGGGCACTA-1</th>\n",
       "      <td>1.776569</td>\n",
       "      <td>1.709204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGCACTTAACG-1</th>\n",
       "      <td>0.990188</td>\n",
       "      <td>0.850556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGGTCCCTTGT-1</th>\n",
       "      <td>3.447298</td>\n",
       "      <td>3.750008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCCGCAAGC-1</th>\n",
       "      <td>0.405894</td>\n",
       "      <td>0.475874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCGCGTTTC-1</th>\n",
       "      <td>1.291190</td>\n",
       "      <td>1.144758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCGTGGACC-1</th>\n",
       "      <td>3.467186</td>\n",
       "      <td>2.937608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGCAAAGAGTACCG-1</th>\n",
       "      <td>2.457092</td>\n",
       "      <td>1.622737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGCAATCACTTATC-1</th>\n",
       "      <td>1.255358</td>\n",
       "      <td>1.328561</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGAGTTGGC-1</th>\n",
       "      <td>1.755248</td>\n",
       "      <td>1.891669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGCTAACTC-1</th>\n",
       "      <td>1.652005</td>\n",
       "      <td>1.428341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGGAGTACC-1</th>\n",
       "      <td>0.543827</td>\n",
       "      <td>0.574121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGCAGTTTACG-1</th>\n",
       "      <td>2.881975</td>\n",
       "      <td>2.747883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGCATTAACCG-1</th>\n",
       "      <td>1.117299</td>\n",
       "      <td>0.860455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGGTACTCGCG-1</th>\n",
       "      <td>2.765129</td>\n",
       "      <td>2.744694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGTCTCGATGA-1</th>\n",
       "      <td>1.426713</td>\n",
       "      <td>1.493590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCAAACTGCT-1</th>\n",
       "      <td>0.546127</td>\n",
       "      <td>0.668312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCACATGACT-1</th>\n",
       "      <td>3.406330</td>\n",
       "      <td>3.133376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCATACTCTT-1</th>\n",
       "      <td>2.149502</td>\n",
       "      <td>1.922778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCGTGGCCCTA-1</th>\n",
       "      <td>2.583077</td>\n",
       "      <td>2.744961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCGTTTAAGCC-1</th>\n",
       "      <td>3.267880</td>\n",
       "      <td>2.653814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCAATAAGG-1</th>\n",
       "      <td>0.681796</td>\n",
       "      <td>0.808452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCATTATCC-1</th>\n",
       "      <td>3.685506</td>\n",
       "      <td>2.870278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCCTTGACC-1</th>\n",
       "      <td>0.337586</td>\n",
       "      <td>0.440907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCCTTTCTC-1</th>\n",
       "      <td>2.856774</td>\n",
       "      <td>2.429800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCTTGGGTA-1</th>\n",
       "      <td>0.425422</td>\n",
       "      <td>0.620381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCCATCGATTG-1</th>\n",
       "      <td>0.267748</td>\n",
       "      <td>0.358732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCGTAGTACCT-1</th>\n",
       "      <td>2.577453</td>\n",
       "      <td>2.360288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCGTTATGCGT-1</th>\n",
       "      <td>0.959450</td>\n",
       "      <td>1.076300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCCTTAATC-1</th>\n",
       "      <td>3.275354</td>\n",
       "      <td>3.232358</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCGATGAGG-1</th>\n",
       "      <td>0.470076</td>\n",
       "      <td>0.660028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCTGTTTGT-1</th>\n",
       "      <td>2.642529</td>\n",
       "      <td>2.428539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGCGCTCTTACCGC-1</th>\n",
       "      <td>0.257352</td>\n",
       "      <td>0.273753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTAGCGAGAAA-1</th>\n",
       "      <td>0.294904</td>\n",
       "      <td>0.404630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTAGGACACCA-1</th>\n",
       "      <td>0.512239</td>\n",
       "      <td>0.583090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTCAAAGGTGC-1</th>\n",
       "      <td>1.495061</td>\n",
       "      <td>1.318807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTGTGATGATA-1</th>\n",
       "      <td>2.325406</td>\n",
       "      <td>2.188960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTGTTGAACTC-1</th>\n",
       "      <td>0.321137</td>\n",
       "      <td>0.383881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTTCGTACCGG-1</th>\n",
       "      <td>1.524528</td>\n",
       "      <td>1.443170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTTCTAACGGT-1</th>\n",
       "      <td>0.792568</td>\n",
       "      <td>0.755125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAAGTGTCCCG-1</th>\n",
       "      <td>0.841414</td>\n",
       "      <td>0.897749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAAGTTAGCGG-1</th>\n",
       "      <td>0.338531</td>\n",
       "      <td>0.370486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCACAGTGGAGT-1</th>\n",
       "      <td>0.582584</td>\n",
       "      <td>0.609164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAGTAATTGGA-1</th>\n",
       "      <td>0.254926</td>\n",
       "      <td>0.296702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCATCGTGACAT-1</th>\n",
       "      <td>0.594715</td>\n",
       "      <td>0.876372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCATCTCCCTGA-1</th>\n",
       "      <td>1.198471</td>\n",
       "      <td>1.539571</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6507 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    g0        g1\n",
       "index                                           \n",
       "D7_Dox_C1_AAACCTGAGGAATCGC-1  1.268895  0.977589\n",
       "D7_Dox_C1_AAACCTGAGTAGATGT-1  2.978660  2.510199\n",
       "D7_Dox_C1_AAACCTGCAACCGCCA-1  1.871918  1.789104\n",
       "D7_Dox_C1_AAACCTGCACTATCTT-1  3.072989  2.790364\n",
       "D7_Dox_C1_AAACCTGCAGCCTGTG-1  1.026225  0.918584\n",
       "D7_Dox_C1_AAACCTGCATATACGC-1  1.412582  1.366065\n",
       "D7_Dox_C1_AAACCTGCATCCGTGG-1  1.182596  1.144585\n",
       "D7_Dox_C1_AAACCTGTCGGAATCT-1  0.982687  1.047557\n",
       "D7_Dox_C1_AAACGGGGTAGTACCT-1  0.417277  0.443068\n",
       "D7_Dox_C1_AAACGGGTCTCTTATG-1  0.348761  0.282756\n",
       "D7_Dox_C1_AAACGGGTCTGGGCCA-1  2.636122  2.038604\n",
       "D7_Dox_C1_AAACGGGTCTGTCAAG-1  0.399207  0.446110\n",
       "D7_Dox_C1_AAAGATGAGAGAGCTC-1  1.564738  1.329046\n",
       "D7_Dox_C1_AAAGATGAGAGTACAT-1  1.485496  1.384023\n",
       "D7_Dox_C1_AAAGATGAGATCCGAG-1  0.699411  0.789952\n",
       "D7_Dox_C1_AAAGATGAGGGCACTA-1  1.776569  1.709204\n",
       "D7_Dox_C1_AAAGATGCACTTAACG-1  0.990188  0.850556\n",
       "D7_Dox_C1_AAAGATGGTCCCTTGT-1  3.447298  3.750008\n",
       "D7_Dox_C1_AAAGATGTCCGCAAGC-1  0.405894  0.475874\n",
       "D7_Dox_C1_AAAGATGTCGCGTTTC-1  1.291190  1.144758\n",
       "D7_Dox_C1_AAAGATGTCGTGGACC-1  3.467186  2.937608\n",
       "D7_Dox_C1_AAAGCAAAGAGTACCG-1  2.457092  1.622737\n",
       "D7_Dox_C1_AAAGCAATCACTTATC-1  1.255358  1.328561\n",
       "D7_Dox_C1_AAAGTAGAGAGTTGGC-1  1.755248  1.891669\n",
       "D7_Dox_C1_AAAGTAGAGCTAACTC-1  1.652005  1.428341\n",
       "D7_Dox_C1_AAAGTAGAGGAGTACC-1  0.543827  0.574121\n",
       "D7_Dox_C1_AAAGTAGCAGTTTACG-1  2.881975  2.747883\n",
       "D7_Dox_C1_AAAGTAGCATTAACCG-1  1.117299  0.860455\n",
       "D7_Dox_C1_AAAGTAGGTACTCGCG-1  2.765129  2.744694\n",
       "D7_Dox_C1_AAAGTAGTCTCGATGA-1  1.426713  1.493590\n",
       "...                                ...       ...\n",
       "D7_Dox_C2_TTTATGCCAAACTGCT-1  0.546127  0.668312\n",
       "D7_Dox_C2_TTTATGCCACATGACT-1  3.406330  3.133376\n",
       "D7_Dox_C2_TTTATGCCATACTCTT-1  2.149502  1.922778\n",
       "D7_Dox_C2_TTTATGCGTGGCCCTA-1  2.583077  2.744961\n",
       "D7_Dox_C2_TTTATGCGTTTAAGCC-1  3.267880  2.653814\n",
       "D7_Dox_C2_TTTATGCTCAATAAGG-1  0.681796  0.808452\n",
       "D7_Dox_C2_TTTATGCTCATTATCC-1  3.685506  2.870278\n",
       "D7_Dox_C2_TTTATGCTCCTTGACC-1  0.337586  0.440907\n",
       "D7_Dox_C2_TTTATGCTCCTTTCTC-1  2.856774  2.429800\n",
       "D7_Dox_C2_TTTATGCTCTTGGGTA-1  0.425422  0.620381\n",
       "D7_Dox_C2_TTTCCTCCATCGATTG-1  0.267748  0.358732\n",
       "D7_Dox_C2_TTTCCTCGTAGTACCT-1  2.577453  2.360288\n",
       "D7_Dox_C2_TTTCCTCGTTATGCGT-1  0.959450  1.076300\n",
       "D7_Dox_C2_TTTCCTCTCCTTAATC-1  3.275354  3.232358\n",
       "D7_Dox_C2_TTTCCTCTCGATGAGG-1  0.470076  0.660028\n",
       "D7_Dox_C2_TTTCCTCTCTGTTTGT-1  2.642529  2.428539\n",
       "D7_Dox_C2_TTTGCGCTCTTACCGC-1  0.257352  0.273753\n",
       "D7_Dox_C2_TTTGGTTAGCGAGAAA-1  0.294904  0.404630\n",
       "D7_Dox_C2_TTTGGTTAGGACACCA-1  0.512239  0.583090\n",
       "D7_Dox_C2_TTTGGTTCAAAGGTGC-1  1.495061  1.318807\n",
       "D7_Dox_C2_TTTGGTTGTGATGATA-1  2.325406  2.188960\n",
       "D7_Dox_C2_TTTGGTTGTTGAACTC-1  0.321137  0.383881\n",
       "D7_Dox_C2_TTTGGTTTCGTACCGG-1  1.524528  1.443170\n",
       "D7_Dox_C2_TTTGGTTTCTAACGGT-1  0.792568  0.755125\n",
       "D7_Dox_C2_TTTGTCAAGTGTCCCG-1  0.841414  0.897749\n",
       "D7_Dox_C2_TTTGTCAAGTTAGCGG-1  0.338531  0.370486\n",
       "D7_Dox_C2_TTTGTCACAGTGGAGT-1  0.582584  0.609164\n",
       "D7_Dox_C2_TTTGTCAGTAATTGGA-1  0.254926  0.296702\n",
       "D7_Dox_C2_TTTGTCATCGTGACAT-1  0.594715  0.876372\n",
       "D7_Dox_C2_TTTGTCATCTCCCTGA-1  1.198471  1.539571\n",
       "\n",
       "[6507 rows x 2 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# row annotations include cell growth rates\n",
    "tmap_annotated.obs\n",
    "\n",
    "# columns annotated by cell barcodes\n",
    "# tmap_annotated.var"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The column `g0` displays the input growth rates and the column `g1` displays the row sums of the transport map (raised to the power $t_2 - t_1$. These values can differ, as we see in the plot below, because the row-sum constraints are only enforced loosely. We interpret the value `g1` as an *inferred growth rate*. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15864 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize how growth rates change with growth iterations\n",
    "plt.scatter(tmap_annotated.obs['g0'],tmap_annotated.obs['g1'])\n",
    "plt.xlabel(\"g0\")\n",
    "plt.ylabel(\"g1\")\n",
    "plt.title(\"Input vs Output Growth Rates\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "When we increase the regularization parameter $\\lambda_1$, the row sum constraints are enforced more strictly. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "ot_model_strict = wot.ot.OTModel(adata,epsilon = 0.05, lambda1 = 3,lambda2 = 50) \n",
    "tmap_anno_strict = ot_model_strict.compute_transport_map(7,7.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13976 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(tmap_anno_strict.obs['g0'],tmap_anno_strict.obs['g1'])\n",
    "plt.xlabel(\"g0\")\n",
    "plt.ylabel(\"g1\")\n",
    "plt.title(\"Input vs Output Growth Rates\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Updating growth estimates\n",
    "\n",
    "We can feed these new growth rates back into the OT solver and recompute transport maps with these new growth rates. Instead of doing this manually, we can specify the number of 'growth iterations' with the flag `growth_iters`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ot_model_gr2 = wot.ot.OTModel(adata,epsilon = 0.05, lambda1 = 1,lambda2 = 50,growth_iters=2) \n",
    "tmap_anno_gr2 = ot_model_gr2.compute_transport_map(7,7.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The row annotations of this transport map now have fields for multiple iterations of learning growth:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "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>g0</th>\n",
       "      <th>g1</th>\n",
       "      <th>g2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGAGGAATCGC-1</th>\n",
       "      <td>1.268895</td>\n",
       "      <td>0.977589</td>\n",
       "      <td>0.771923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGAGTAGATGT-1</th>\n",
       "      <td>2.978660</td>\n",
       "      <td>2.510199</td>\n",
       "      <td>2.130938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCAACCGCCA-1</th>\n",
       "      <td>1.871918</td>\n",
       "      <td>1.789104</td>\n",
       "      <td>1.692330</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCACTATCTT-1</th>\n",
       "      <td>3.072989</td>\n",
       "      <td>2.790364</td>\n",
       "      <td>2.516863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCAGCCTGTG-1</th>\n",
       "      <td>1.026225</td>\n",
       "      <td>0.918584</td>\n",
       "      <td>0.815139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCATATACGC-1</th>\n",
       "      <td>1.412582</td>\n",
       "      <td>1.366065</td>\n",
       "      <td>1.303655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGCATCCGTGG-1</th>\n",
       "      <td>1.182596</td>\n",
       "      <td>1.144585</td>\n",
       "      <td>1.093318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACCTGTCGGAATCT-1</th>\n",
       "      <td>0.982687</td>\n",
       "      <td>1.047557</td>\n",
       "      <td>1.093740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGGTAGTACCT-1</th>\n",
       "      <td>0.417277</td>\n",
       "      <td>0.443068</td>\n",
       "      <td>0.462439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTCTTATG-1</th>\n",
       "      <td>0.348761</td>\n",
       "      <td>0.282756</td>\n",
       "      <td>0.228207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTGGGCCA-1</th>\n",
       "      <td>2.636122</td>\n",
       "      <td>2.038604</td>\n",
       "      <td>1.597445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAACGGGTCTGTCAAG-1</th>\n",
       "      <td>0.399207</td>\n",
       "      <td>0.446110</td>\n",
       "      <td>0.482377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGAGAGCTC-1</th>\n",
       "      <td>1.564738</td>\n",
       "      <td>1.329046</td>\n",
       "      <td>1.122280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGAGTACAT-1</th>\n",
       "      <td>1.485496</td>\n",
       "      <td>1.384023</td>\n",
       "      <td>1.281136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGATCCGAG-1</th>\n",
       "      <td>0.699411</td>\n",
       "      <td>0.789952</td>\n",
       "      <td>0.872767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGAGGGCACTA-1</th>\n",
       "      <td>1.776569</td>\n",
       "      <td>1.709204</td>\n",
       "      <td>1.641042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGCACTTAACG-1</th>\n",
       "      <td>0.990188</td>\n",
       "      <td>0.850556</td>\n",
       "      <td>0.728069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGGTCCCTTGT-1</th>\n",
       "      <td>3.447298</td>\n",
       "      <td>3.750008</td>\n",
       "      <td>4.010355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCCGCAAGC-1</th>\n",
       "      <td>0.405894</td>\n",
       "      <td>0.475874</td>\n",
       "      <td>0.539814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCGCGTTTC-1</th>\n",
       "      <td>1.291190</td>\n",
       "      <td>1.144758</td>\n",
       "      <td>1.010505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGATGTCGTGGACC-1</th>\n",
       "      <td>3.467186</td>\n",
       "      <td>2.937608</td>\n",
       "      <td>2.486141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGCAAAGAGTACCG-1</th>\n",
       "      <td>2.457092</td>\n",
       "      <td>1.622737</td>\n",
       "      <td>1.104880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGCAATCACTTATC-1</th>\n",
       "      <td>1.255358</td>\n",
       "      <td>1.328561</td>\n",
       "      <td>1.376567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGAGTTGGC-1</th>\n",
       "      <td>1.755248</td>\n",
       "      <td>1.891669</td>\n",
       "      <td>2.018037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGCTAACTC-1</th>\n",
       "      <td>1.652005</td>\n",
       "      <td>1.428341</td>\n",
       "      <td>1.230901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGAGGAGTACC-1</th>\n",
       "      <td>0.543827</td>\n",
       "      <td>0.574121</td>\n",
       "      <td>0.591511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGCAGTTTACG-1</th>\n",
       "      <td>2.881975</td>\n",
       "      <td>2.747883</td>\n",
       "      <td>2.580114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGCATTAACCG-1</th>\n",
       "      <td>1.117299</td>\n",
       "      <td>0.860455</td>\n",
       "      <td>0.672185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGGTACTCGCG-1</th>\n",
       "      <td>2.765129</td>\n",
       "      <td>2.744694</td>\n",
       "      <td>2.694334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C1_AAAGTAGTCTCGATGA-1</th>\n",
       "      <td>1.426713</td>\n",
       "      <td>1.493590</td>\n",
       "      <td>1.543274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCAAACTGCT-1</th>\n",
       "      <td>0.546127</td>\n",
       "      <td>0.668312</td>\n",
       "      <td>0.792676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCACATGACT-1</th>\n",
       "      <td>3.406330</td>\n",
       "      <td>3.133376</td>\n",
       "      <td>2.934748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCCATACTCTT-1</th>\n",
       "      <td>2.149502</td>\n",
       "      <td>1.922778</td>\n",
       "      <td>1.724100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCGTGGCCCTA-1</th>\n",
       "      <td>2.583077</td>\n",
       "      <td>2.744961</td>\n",
       "      <td>2.847056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCGTTTAAGCC-1</th>\n",
       "      <td>3.267880</td>\n",
       "      <td>2.653814</td>\n",
       "      <td>2.172043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCAATAAGG-1</th>\n",
       "      <td>0.681796</td>\n",
       "      <td>0.808452</td>\n",
       "      <td>0.928986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCATTATCC-1</th>\n",
       "      <td>3.685506</td>\n",
       "      <td>2.870278</td>\n",
       "      <td>2.267717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCCTTGACC-1</th>\n",
       "      <td>0.337586</td>\n",
       "      <td>0.440907</td>\n",
       "      <td>0.554888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCCTTTCTC-1</th>\n",
       "      <td>2.856774</td>\n",
       "      <td>2.429800</td>\n",
       "      <td>2.113878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTATGCTCTTGGGTA-1</th>\n",
       "      <td>0.425422</td>\n",
       "      <td>0.620381</td>\n",
       "      <td>0.875085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCCATCGATTG-1</th>\n",
       "      <td>0.267748</td>\n",
       "      <td>0.358732</td>\n",
       "      <td>0.460588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCGTAGTACCT-1</th>\n",
       "      <td>2.577453</td>\n",
       "      <td>2.360288</td>\n",
       "      <td>2.148033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCGTTATGCGT-1</th>\n",
       "      <td>0.959450</td>\n",
       "      <td>1.076300</td>\n",
       "      <td>1.177374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCCTTAATC-1</th>\n",
       "      <td>3.275354</td>\n",
       "      <td>3.232358</td>\n",
       "      <td>3.133913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCGATGAGG-1</th>\n",
       "      <td>0.470076</td>\n",
       "      <td>0.660028</td>\n",
       "      <td>0.894440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTCCTCTCTGTTTGT-1</th>\n",
       "      <td>2.642529</td>\n",
       "      <td>2.428539</td>\n",
       "      <td>2.226831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGCGCTCTTACCGC-1</th>\n",
       "      <td>0.257352</td>\n",
       "      <td>0.273753</td>\n",
       "      <td>0.284005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTAGCGAGAAA-1</th>\n",
       "      <td>0.294904</td>\n",
       "      <td>0.404630</td>\n",
       "      <td>0.530570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTAGGACACCA-1</th>\n",
       "      <td>0.512239</td>\n",
       "      <td>0.583090</td>\n",
       "      <td>0.645941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTCAAAGGTGC-1</th>\n",
       "      <td>1.495061</td>\n",
       "      <td>1.318807</td>\n",
       "      <td>1.158094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTGTGATGATA-1</th>\n",
       "      <td>2.325406</td>\n",
       "      <td>2.188960</td>\n",
       "      <td>2.083658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTGTTGAACTC-1</th>\n",
       "      <td>0.321137</td>\n",
       "      <td>0.383881</td>\n",
       "      <td>0.442784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTTCGTACCGG-1</th>\n",
       "      <td>1.524528</td>\n",
       "      <td>1.443170</td>\n",
       "      <td>1.351901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGGTTTCTAACGGT-1</th>\n",
       "      <td>0.792568</td>\n",
       "      <td>0.755125</td>\n",
       "      <td>0.708005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAAGTGTCCCG-1</th>\n",
       "      <td>0.841414</td>\n",
       "      <td>0.897749</td>\n",
       "      <td>0.937937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAAGTTAGCGG-1</th>\n",
       "      <td>0.338531</td>\n",
       "      <td>0.370486</td>\n",
       "      <td>0.397700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCACAGTGGAGT-1</th>\n",
       "      <td>0.582584</td>\n",
       "      <td>0.609164</td>\n",
       "      <td>0.624181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCAGTAATTGGA-1</th>\n",
       "      <td>0.254926</td>\n",
       "      <td>0.296702</td>\n",
       "      <td>0.334487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCATCGTGACAT-1</th>\n",
       "      <td>0.594715</td>\n",
       "      <td>0.876372</td>\n",
       "      <td>1.247693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D7_Dox_C2_TTTGTCATCTCCCTGA-1</th>\n",
       "      <td>1.198471</td>\n",
       "      <td>1.539571</td>\n",
       "      <td>1.896885</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6507 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    g0        g1        g2\n",
       "index                                                     \n",
       "D7_Dox_C1_AAACCTGAGGAATCGC-1  1.268895  0.977589  0.771923\n",
       "D7_Dox_C1_AAACCTGAGTAGATGT-1  2.978660  2.510199  2.130938\n",
       "D7_Dox_C1_AAACCTGCAACCGCCA-1  1.871918  1.789104  1.692330\n",
       "D7_Dox_C1_AAACCTGCACTATCTT-1  3.072989  2.790364  2.516863\n",
       "D7_Dox_C1_AAACCTGCAGCCTGTG-1  1.026225  0.918584  0.815139\n",
       "D7_Dox_C1_AAACCTGCATATACGC-1  1.412582  1.366065  1.303655\n",
       "D7_Dox_C1_AAACCTGCATCCGTGG-1  1.182596  1.144585  1.093318\n",
       "D7_Dox_C1_AAACCTGTCGGAATCT-1  0.982687  1.047557  1.093740\n",
       "D7_Dox_C1_AAACGGGGTAGTACCT-1  0.417277  0.443068  0.462439\n",
       "D7_Dox_C1_AAACGGGTCTCTTATG-1  0.348761  0.282756  0.228207\n",
       "D7_Dox_C1_AAACGGGTCTGGGCCA-1  2.636122  2.038604  1.597445\n",
       "D7_Dox_C1_AAACGGGTCTGTCAAG-1  0.399207  0.446110  0.482377\n",
       "D7_Dox_C1_AAAGATGAGAGAGCTC-1  1.564738  1.329046  1.122280\n",
       "D7_Dox_C1_AAAGATGAGAGTACAT-1  1.485496  1.384023  1.281136\n",
       "D7_Dox_C1_AAAGATGAGATCCGAG-1  0.699411  0.789952  0.872767\n",
       "D7_Dox_C1_AAAGATGAGGGCACTA-1  1.776569  1.709204  1.641042\n",
       "D7_Dox_C1_AAAGATGCACTTAACG-1  0.990188  0.850556  0.728069\n",
       "D7_Dox_C1_AAAGATGGTCCCTTGT-1  3.447298  3.750008  4.010355\n",
       "D7_Dox_C1_AAAGATGTCCGCAAGC-1  0.405894  0.475874  0.539814\n",
       "D7_Dox_C1_AAAGATGTCGCGTTTC-1  1.291190  1.144758  1.010505\n",
       "D7_Dox_C1_AAAGATGTCGTGGACC-1  3.467186  2.937608  2.486141\n",
       "D7_Dox_C1_AAAGCAAAGAGTACCG-1  2.457092  1.622737  1.104880\n",
       "D7_Dox_C1_AAAGCAATCACTTATC-1  1.255358  1.328561  1.376567\n",
       "D7_Dox_C1_AAAGTAGAGAGTTGGC-1  1.755248  1.891669  2.018037\n",
       "D7_Dox_C1_AAAGTAGAGCTAACTC-1  1.652005  1.428341  1.230901\n",
       "D7_Dox_C1_AAAGTAGAGGAGTACC-1  0.543827  0.574121  0.591511\n",
       "D7_Dox_C1_AAAGTAGCAGTTTACG-1  2.881975  2.747883  2.580114\n",
       "D7_Dox_C1_AAAGTAGCATTAACCG-1  1.117299  0.860455  0.672185\n",
       "D7_Dox_C1_AAAGTAGGTACTCGCG-1  2.765129  2.744694  2.694334\n",
       "D7_Dox_C1_AAAGTAGTCTCGATGA-1  1.426713  1.493590  1.543274\n",
       "...                                ...       ...       ...\n",
       "D7_Dox_C2_TTTATGCCAAACTGCT-1  0.546127  0.668312  0.792676\n",
       "D7_Dox_C2_TTTATGCCACATGACT-1  3.406330  3.133376  2.934748\n",
       "D7_Dox_C2_TTTATGCCATACTCTT-1  2.149502  1.922778  1.724100\n",
       "D7_Dox_C2_TTTATGCGTGGCCCTA-1  2.583077  2.744961  2.847056\n",
       "D7_Dox_C2_TTTATGCGTTTAAGCC-1  3.267880  2.653814  2.172043\n",
       "D7_Dox_C2_TTTATGCTCAATAAGG-1  0.681796  0.808452  0.928986\n",
       "D7_Dox_C2_TTTATGCTCATTATCC-1  3.685506  2.870278  2.267717\n",
       "D7_Dox_C2_TTTATGCTCCTTGACC-1  0.337586  0.440907  0.554888\n",
       "D7_Dox_C2_TTTATGCTCCTTTCTC-1  2.856774  2.429800  2.113878\n",
       "D7_Dox_C2_TTTATGCTCTTGGGTA-1  0.425422  0.620381  0.875085\n",
       "D7_Dox_C2_TTTCCTCCATCGATTG-1  0.267748  0.358732  0.460588\n",
       "D7_Dox_C2_TTTCCTCGTAGTACCT-1  2.577453  2.360288  2.148033\n",
       "D7_Dox_C2_TTTCCTCGTTATGCGT-1  0.959450  1.076300  1.177374\n",
       "D7_Dox_C2_TTTCCTCTCCTTAATC-1  3.275354  3.232358  3.133913\n",
       "D7_Dox_C2_TTTCCTCTCGATGAGG-1  0.470076  0.660028  0.894440\n",
       "D7_Dox_C2_TTTCCTCTCTGTTTGT-1  2.642529  2.428539  2.226831\n",
       "D7_Dox_C2_TTTGCGCTCTTACCGC-1  0.257352  0.273753  0.284005\n",
       "D7_Dox_C2_TTTGGTTAGCGAGAAA-1  0.294904  0.404630  0.530570\n",
       "D7_Dox_C2_TTTGGTTAGGACACCA-1  0.512239  0.583090  0.645941\n",
       "D7_Dox_C2_TTTGGTTCAAAGGTGC-1  1.495061  1.318807  1.158094\n",
       "D7_Dox_C2_TTTGGTTGTGATGATA-1  2.325406  2.188960  2.083658\n",
       "D7_Dox_C2_TTTGGTTGTTGAACTC-1  0.321137  0.383881  0.442784\n",
       "D7_Dox_C2_TTTGGTTTCGTACCGG-1  1.524528  1.443170  1.351901\n",
       "D7_Dox_C2_TTTGGTTTCTAACGGT-1  0.792568  0.755125  0.708005\n",
       "D7_Dox_C2_TTTGTCAAGTGTCCCG-1  0.841414  0.897749  0.937937\n",
       "D7_Dox_C2_TTTGTCAAGTTAGCGG-1  0.338531  0.370486  0.397700\n",
       "D7_Dox_C2_TTTGTCACAGTGGAGT-1  0.582584  0.609164  0.624181\n",
       "D7_Dox_C2_TTTGTCAGTAATTGGA-1  0.254926  0.296702  0.334487\n",
       "D7_Dox_C2_TTTGTCATCGTGACAT-1  0.594715  0.876372  1.247693\n",
       "D7_Dox_C2_TTTGTCATCTCCCTGA-1  1.198471  1.539571  1.896885\n",
       "\n",
       "[6507 rows x 3 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# row annotations include initial cell growth rates g0, and also g1 and g2\n",
    "tmap_anno_gr2.obs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuning $\\lambda_1$ and $\\lambda_2$\n",
    "\n",
    "The numerical value of $\\lambda_1$ determines how different $g_{k+1}$ can be from $g_k$. If growth rates are known very well, then a large value of $\\lambda_1$ can be used to enforce the row-sum constraints strictly and we could just do one growth iteration. When we do more than one growth iteration, the value of $\\lambda_1$ can be interpreted intuitively as an *inverse step size* in our update of growth rates. A large value allows us to change growth by a lot at each iteration. \n",
    "\n",
    "You may have noticed that we set $\\lambda_2 = 50$, a much larger numerical value than $\\lambda_1 = 1$. This enforces the constraints on the column sums strictly (the amount of mass going into each cell $y$ at time $t_2$ is well specified). To see this, we plot a histogram of column sums, and we see a very narrow band. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([   2.,    2.,   23.,   58.,  200.,  624., 1517., 1869.,  726.,\n",
       "          40.]),\n",
       " array([1.404813 , 1.407358 , 1.4099029, 1.4124479, 1.4149928, 1.4175378,\n",
       "        1.4200828, 1.4226277, 1.4251727, 1.4277176, 1.4302626],\n",
       "       dtype=float32),\n",
       " <a list of 10 Patch objects>)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6616 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "colsums = tmap_anno_gr2.X.sum(axis=0);\n",
    "plt.hist(colsums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 545,
     "status": "ok",
     "timestamp": 1556313222255,
     "user": {
      "displayName": "Joshua Gould",
      "photoUrl": "",
      "userId": "09978885733763067346"
     },
     "user_tz": 240
    },
    "id": "P-ZQVITQe9m-",
    "outputId": "a864cea1-48cd-468b-b9ac-845724230979",
    "pycharm": {}
   },
   "source": [
    "# Tuning $\\epsilon$ \n",
    "\n",
    "The parameter $\\epsilon$ controls the level of entropy in the transport maps. A larger value promotes more entropic descendant distributions, so each cell can transition to a larger variety of descendants. We recommend sticking with the default value of $\\epsilon = 0.05$. Note that we rescale the cost matrix to have a median value of $1,$ so the default value of $\\epsilon$ should give similar results for datasets of different scales.  \n",
    "\n",
    "# Compute all transport maps in batch mode\n",
    "\n",
    "The following command computes all transport maps. Note that this can take a few hours on a laptop. For convenience, we include precomputed transport maps in the `tmaps` directory. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 164
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 370,
     "status": "error",
     "timestamp": 1556369739325,
     "user": {
      "displayName": "Joshua Gould",
      "photoUrl": "",
      "userId": "09978885733763067346"
     },
     "user_tz": 240
    },
    "id": "5lFF4FfhAexV",
    "outputId": "2c0fc345-a952-4619-fe70-8a5dcc1a7538",
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# ot_model = wot.ot.OTModel(adata, epsilon = 0.05, lambda1 = 1, lambda2 = 50, growth_iters = 3) \n",
    "# ot_model.compute_all_transport_maps(tmap_out='tmaps/serum')\n",
    "\n",
    "# we can speed this up by supplying g2 and doing 1 growth iter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This can also be run from the command line as follows:\n",
    "\n",
    "`wot optimal_transport --matrix data/ExprMatrix.var.genes.h5ad --cell_days data/cell_days.txt --cell_filter data/serum_cell_ids.txt --cell_growth_rates data/growth_gs_init.txt --growth_iters 3 --lambda1 1 --lambda2 50 --epsilon 0.05`\n",
    "\n",
    "We use these transport maps in the remaining notebooks. "
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "wot-2.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "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.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}