|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "f07fa1f2-187e-4ce0-af95-31d6120977fe", |
| 7 | + "metadata": { |
| 8 | + "pycharm": { |
| 9 | + "name": "#%%\n" |
| 10 | + } |
| 11 | + }, |
| 12 | + "outputs": [], |
| 13 | + "source": [ |
| 14 | + "import pandas as pd\n", |
| 15 | + "import numpy as np\n", |
| 16 | + "import matplotlib.pyplot as plt\n", |
| 17 | + "from scipy.integrate import odeint\n", |
| 18 | + "from numpy import linalg as la\n", |
| 19 | + "\n", |
| 20 | + "import sklearn.linear_model\n", |
| 21 | + "from sklearn.linear_model import LinearRegression\n", |
| 22 | + "from sklearn.linear_model import Ridge\n", |
| 23 | + "from sklearn.linear_model import Lasso\n", |
| 24 | + "from sklearn.linear_model import ElasticNet, ElasticNetCV\n", |
| 25 | + "from sklearn.model_selection import RepeatedKFold\n", |
| 26 | + "from sklearn.model_selection import cross_val_score\n", |
| 27 | + "from sklearn.model_selection import KFold\n", |
| 28 | + "from sklearn.model_selection import GridSearchCV\n", |
| 29 | + "\n", |
| 30 | + "import sys\n", |
| 31 | + "sys.path.append(\"../../\")\n", |
| 32 | + "sys.path.append(\"../../gMLV\")\n", |
| 33 | + "\n", |
| 34 | + "from gMLV import *\n", |
| 35 | + "from utilities import plot_gLV\n", |
| 36 | + "from utilities import set_all_seeds\n" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "id": "82eb9f01", |
| 42 | + "metadata": { |
| 43 | + "pycharm": { |
| 44 | + "name": "#%% md\n" |
| 45 | + } |
| 46 | + }, |
| 47 | + "source": [ |
| 48 | + "## Simulate some time course data from the gLV\n" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "id": "fbc11bbc", |
| 54 | + "metadata": { |
| 55 | + "pycharm": { |
| 56 | + "name": "#%% md\n" |
| 57 | + } |
| 58 | + }, |
| 59 | + "source": [ |
| 60 | + "### Five species, single time course" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "id": "3e0845a5", |
| 67 | + "metadata": { |
| 68 | + "collapsed": false, |
| 69 | + "jupyter": { |
| 70 | + "outputs_hidden": false |
| 71 | + }, |
| 72 | + "pycharm": { |
| 73 | + "name": "#%%\n" |
| 74 | + } |
| 75 | + }, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "# In this example n >> p and it it is basically same as standard regression\n", |
| 79 | + "# We have to be careful as most of these gLV models are very weakly identifiable\n", |
| 80 | + "\n", |
| 81 | + "set_all_seeds(1234)\n", |
| 82 | + "\n", |
| 83 | + "## SETUP MODEL\n", |
| 84 | + "# establish size of model\n", |
| 85 | + "num_species = 5\n", |
| 86 | + "num_metabolites = 0\n", |
| 87 | + "\n", |
| 88 | + "# construct interaction matrix\n", |
| 89 | + "#TODO do this programmatically\n", |
| 90 | + "M = np.zeros((num_species, num_species))\n", |
| 91 | + "np.fill_diagonal(M, [-0.05, -0.1, -0.15, -0.01, -0.2])\n", |
| 92 | + "M[0, 2] = -0.025\n", |
| 93 | + "M[1, 3] = 0.05\n", |
| 94 | + "M[4, 0] = 0.02\n", |
| 95 | + "\n", |
| 96 | + "# construct growth rates matrix\n", |
| 97 | + "mu = np.random.lognormal(0.01, 0.5, num_species)\n", |
| 98 | + "\n", |
| 99 | + "# instantiate simulator\n", |
| 100 | + "simulator = gMLV_sim(num_species=num_species,\n", |
| 101 | + " num_metabolites=num_metabolites,\n", |
| 102 | + " M=M,\n", |
| 103 | + " mu=mu)\n", |
| 104 | + "simulator.print()\n", |
| 105 | + "\n", |
| 106 | + "## PRODUCE SIMULATED RESULTS\n", |
| 107 | + "# initial conditions\n", |
| 108 | + "init_species = 10 * np.ones(num_species)\n", |
| 109 | + "init_metabolites = 10 * np.ones(num_metabolites)\n", |
| 110 | + "\n", |
| 111 | + "times = np.arange(0, 5, 0.1)\n", |
| 112 | + "yobs, sobs, sy0, mu, M, _ = simulator.simulate(times=times, sy0=np.hstack((init_species, init_metabolites)))\n", |
| 113 | + "\n", |
| 114 | + "# add some gaussian noise\n", |
| 115 | + "yobs = yobs + np.random.normal(loc=0, scale=0.1, size=yobs.shape)\n", |
| 116 | + "sobs = sobs + np.random.normal(loc=0, scale=0.1, size=sobs.shape)\n", |
| 117 | + "\n", |
| 118 | + "# plot simulation\n", |
| 119 | + "plot_gLV(yobs, sobs, times)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "id": "99ee1482", |
| 125 | + "metadata": { |
| 126 | + "pycharm": { |
| 127 | + "name": "#%% md\n" |
| 128 | + } |
| 129 | + }, |
| 130 | + "source": [ |
| 131 | + "### Five species, single time course, with a perturbation" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "id": "cbab2390", |
| 138 | + "metadata": { |
| 139 | + "collapsed": false, |
| 140 | + "jupyter": { |
| 141 | + "outputs_hidden": false |
| 142 | + }, |
| 143 | + "pycharm": { |
| 144 | + "name": "#%%\n" |
| 145 | + } |
| 146 | + }, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "set_all_seeds(1234)\n", |
| 150 | + "\n", |
| 151 | + "## SETUP MODEL\n", |
| 152 | + "# establish size of model\n", |
| 153 | + "num_species = 5\n", |
| 154 | + "num_metabolites = 0\n", |
| 155 | + "\n", |
| 156 | + "# construct interaction matrix\n", |
| 157 | + "#TODO do this programmatically\n", |
| 158 | + "M = np.zeros((num_species, num_species))\n", |
| 159 | + "np.fill_diagonal(M, [-0.05, -0.1, -0.15, -0.01, -0.2])\n", |
| 160 | + "M[0, 2] = -0.025\n", |
| 161 | + "M[1, 3] = 0.05\n", |
| 162 | + "M[4, 0] = 0.02\n", |
| 163 | + "\n", |
| 164 | + "# construct growth rates matrix\n", |
| 165 | + "mu = np.random.lognormal(0.01, 0.5, num_species)\n", |
| 166 | + "\n", |
| 167 | + "# construct perturbation matrix\n", |
| 168 | + "epsilon = np.array([0, -1, 0, -1, 0])\n", |
| 169 | + "\n", |
| 170 | + "# instantiate simulator\n", |
| 171 | + "simulator = gMLV_sim(num_species=num_species,\n", |
| 172 | + " num_metabolites=num_metabolites,\n", |
| 173 | + " M=M,\n", |
| 174 | + " mu=mu,\n", |
| 175 | + " epsilon=epsilon)\n", |
| 176 | + "simulator.print()\n", |
| 177 | + "\n", |
| 178 | + "## PRODUCE SIMULATED RESULTS\n", |
| 179 | + "# initial conditions\n", |
| 180 | + "init_species = 10 * np.ones(num_species)\n", |
| 181 | + "init_metabolites = 10 * np.ones(num_metabolites)\n", |
| 182 | + "\n", |
| 183 | + "# perturbation\n", |
| 184 | + "tp = 2\n", |
| 185 | + "\n", |
| 186 | + "times = np.arange(0, 5, 0.1)\n", |
| 187 | + "yobs, sobs, sy0, mu, M, _ = simulator.simulate(times=times, \n", |
| 188 | + " sy0=np.hstack((init_species, init_metabolites)),\n", |
| 189 | + " tp=tp)\n", |
| 190 | + "\n", |
| 191 | + "\n", |
| 192 | + "# add some gaussian noise\n", |
| 193 | + "yobs = yobs + np.random.normal(loc=0, scale=0.1, size=yobs.shape)\n", |
| 194 | + "sobs = sobs + np.random.normal(loc=0, scale=0.1, size=sobs.shape)\n", |
| 195 | + "\n", |
| 196 | + "# plot simulation\n", |
| 197 | + "plot_gLV(yobs, sobs, times)" |
| 198 | + ] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "kernelspec": { |
| 203 | + "display_name": "Python 3", |
| 204 | + "language": "python", |
| 205 | + "name": "python3" |
| 206 | + }, |
| 207 | + "language_info": { |
| 208 | + "codemirror_mode": { |
| 209 | + "name": "ipython", |
| 210 | + "version": 3 |
| 211 | + }, |
| 212 | + "file_extension": ".py", |
| 213 | + "mimetype": "text/x-python", |
| 214 | + "name": "python", |
| 215 | + "nbconvert_exporter": "python", |
| 216 | + "pygments_lexer": "ipython3", |
| 217 | + "version": "3.8.8" |
| 218 | + } |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 5 |
| 222 | +} |
0 commit comments